source: popexec.c @ fa00c5c

owl
Last change on this file since fa00c5c was fa00c5c, checked in by James M. Kretchmar <kretch@mit.edu>, 15 years ago
Correct license.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
2 *
3 * This file is part of Owl.
4 *
5 * Owl is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Owl is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * ---------------------------------------------------------------
19 *
20 * As of Owl version 2.1.12 there are patches contributed by
21 * developers of the branched BarnOwl project, Copyright (c)
22 * 2006-2009 The BarnOwl Developers. All rights reserved.
23 */
24
25#include "owl.h"
26#ifdef HAVE_SYS_IOCTL_H
27#include <sys/ioctl.h>
28#endif
29#ifdef HAVE_SYS_FILIO_H
30#include <sys/filio.h>
31#endif
32#include <sys/wait.h>
33
34static const char fileIdent[] = "$Id$";
35
36/* starts up popexec in a new viewwin */
37owl_popexec *owl_popexec_new(char *command)
38{
39  owl_popexec *pe;
40  owl_popwin *pw;
41  owl_viewwin *v;
42  int pipefds[2], child_write_fd, parent_read_fd;
43  int pid;
44
45  pe = owl_malloc(sizeof(owl_popexec));
46  if (!pe) return NULL;
47  pe->winactive=0;
48  pe->pid=0;
49  pe->refcount=0;
50
51  pw=owl_global_get_popwin(&g);
52  pe->vwin=v=owl_global_get_viewwin(&g);
53
54  owl_popwin_up(pw);
55  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
56                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
57                        "");
58  owl_popwin_refresh(pw);
59  owl_viewwin_redisplay(v, 0);
60  owl_global_set_needrefresh(&g);
61  owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
62  pe->refcount++;
63
64  if (0 != pipe(pipefds)) {
65    owl_function_error("owl_function_popless_exec: pipe failed\n");
66    return NULL;
67  }
68  parent_read_fd = pipefds[0];
69  child_write_fd = pipefds[1];
70  pid = fork();
71  if (pid == -1) {
72    close(pipefds[0]);
73    close(pipefds[1]);
74    owl_function_error("owl_function_popless_exec: fork failed\n");
75    return NULL;
76  } else if (pid != 0) {
77    close(child_write_fd);
78    /* still in owl */
79    pe->pid=pid;
80    pe->winactive=1;
81    pe->dispatch.fd = parent_read_fd;
82    pe->dispatch.cfunc = owl_popexec_inputhandler;
83    pe->dispatch.destroy = owl_popexec_free_dispatch;
84    pe->dispatch.data = pe;
85    owl_select_add_dispatch(&pe->dispatch);
86    pe->refcount++;
87  } else {
88    /* in the child process */
89    char *argv[4];
90    int i;
91    int fdlimit = sysconf(_SC_OPEN_MAX);
92
93    for (i=0; i<fdlimit; i++) {
94      if (i!=child_write_fd) close(i);
95    }
96    dup2(child_write_fd, 1 /*stdout*/);
97    dup2(child_write_fd, 2 /*stderr*/);
98    close(child_write_fd);
99
100    argv[0] = "sh";
101    argv[1] = "-c";
102    argv[2] = command;
103    argv[3] = 0;
104    execv("/bin/sh", argv);
105    _exit(127);
106  }
107
108  return pe;
109}
110
111void owl_popexec_inputhandler(owl_dispatch *d)
112{
113  owl_popexec *pe = d->data;
114  int navail, bread, rv_navail;
115  char *buf;
116  int status;
117
118  if (!pe) return;
119
120  /* If pe->winactive is 0 then the vwin has closed.
121   * If pe->pid is 0 then the child has already been reaped.
122   * if d->fd is -1 then the fd has been closed out.
123   * Under these cases we want to get to a state where:
124   *   - data read until end if child running
125   *   - child reaped
126   *   - fd closed
127   *   - callback removed
128   */
129
130  /* the viewwin has closed */
131  if (!pe->pid && !pe->winactive) {
132    owl_select_remove_dispatch(d->fd);
133    return;
134  }
135
136  if (0 != (rv_navail = ioctl(d->fd, FIONREAD, (void*)&navail))) {
137    owl_function_debugmsg("ioctl error");
138  }
139
140  /* check to see if the child has ended gracefully and no more data is
141   * ready to be read... */
142  if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) {
143    owl_function_debugmsg("waitpid got child status: <%d>\n", status);
144    pe->pid = 0;
145    if (pe->winactive) { 
146      owl_viewwin_append_text(pe->vwin, "\n");
147      owl_viewwin_redisplay(pe->vwin, 1);
148    }
149    owl_select_remove_dispatch(d->fd);
150    return;
151  }
152
153  if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) {
154    owl_function_error("popexec should not have reached this point");
155    return;
156  }
157
158  if (navail<=0) return;
159  if (navail>1024) { navail = 1024; }
160  buf = owl_malloc(navail+1);
161  owl_function_debugmsg("about to read %d", navail);
162  bread = read(d->fd, buf, navail);
163  if (bread<0) {
164    perror("read");
165    owl_function_debugmsg("read error");
166  }
167  if (buf[navail-1] != '\0') {
168    buf[navail] = '\0';
169  }
170  owl_function_debugmsg("got data:  <%s>", buf);
171  if (pe->winactive) {
172    owl_viewwin_append_text(pe->vwin, buf);
173    owl_viewwin_redisplay(pe->vwin, 1);
174  }
175  owl_free(buf);
176 
177}
178
179void owl_popexec_free_dispatch(owl_dispatch *d)
180{
181  owl_popexec *pe = d->data;
182  close(d->fd);
183  owl_popexec_unref(pe);
184}
185
186void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
187{
188  owl_popexec *pe = (owl_popexec*)data;
189  int status, rv;
190
191  pe->winactive = 0;
192  if (pe->dispatch.fd>0) {
193    owl_select_remove_dispatch(pe->dispatch.fd);
194  }
195  if (pe->pid) {
196    /* TODO: we should handle the case where SIGTERM isn't good enough */
197    rv = kill(pe->pid, SIGTERM);
198    owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv);
199    rv = waitpid(pe->pid, &status, 0);
200    owl_function_debugmsg("waidpid returned %d, status %d", rv, status);
201    pe->pid = 0;
202  }
203  owl_function_debugmsg("unref of %p from onclose", pe);
204  owl_popexec_unref(pe);
205}
206
207void owl_popexec_unref(owl_popexec *pe)
208{
209  owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
210  pe->refcount--;
211  if (pe->refcount<=0) {
212    owl_function_debugmsg("doing free of %p", pe);
213    owl_free(pe);
214  }
215}
Note: See TracBrowser for help on using the repository browser.