source: popexec.c @ 44fc93b

Last change on this file since 44fc93b was 44fc93b, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
Fix three small warnings.
  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include "owl.h"
2#ifdef HAVE_SYS_IOCTL_H
3#include <sys/ioctl.h>
4#endif
5#ifdef HAVE_SYS_FILIO_H
6#include <sys/filio.h>
7#endif
8#include <sys/wait.h>
9
10static const char fileIdent[] = "$Id$";
11
12/* starts up popexec in a new viewwin */
13owl_popexec *owl_popexec_new(char *command)
14{
15  owl_popexec *pe;
16  owl_popwin *pw;
17  owl_viewwin *v;
18  int pipefds[2], child_write_fd, parent_read_fd;
19  int pid;
20
21  pe = owl_malloc(sizeof(owl_popexec));
22  if (!pe) return NULL;
23  pe->winactive=0;
24  pe->pid=0;
25  pe->refcount=0;
26
27  pw=owl_global_get_popwin(&g);
28  pe->vwin=v=owl_global_get_viewwin(&g);
29
30  owl_popwin_up(pw);
31  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
32                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
33                        "");
34  owl_popwin_refresh(pw);
35  owl_viewwin_redisplay(v, 0);
36  owl_global_set_needrefresh(&g);
37  owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
38  pe->refcount++;
39
40  if (0 != pipe(pipefds)) {
41    owl_function_error("owl_function_popless_exec: pipe failed\n");
42    return NULL;
43  }
44  parent_read_fd = pipefds[0];
45  child_write_fd = pipefds[1];
46  pid = fork();
47  if (pid == -1) {
48    close(pipefds[0]);
49    close(pipefds[1]);
50    owl_function_error("owl_function_popless_exec: fork failed\n");
51    return NULL;
52  } else if (pid != 0) {
53    /* still in owl */
54    int muxhandle;
55    pe->pid=pid;
56    pe->winactive=1;
57    pe->rfd = parent_read_fd;
58    close(child_write_fd);
59    muxhandle = owl_muxevents_add(owl_global_get_muxevents(&g),
60                                  parent_read_fd, OWL_MUX_READ|OWL_MUX_EXCEPT,
61                                  owl_popexec_inputhandler, (void*)pe);
62    pe->refcount++;
63  } else {
64    /* in the child process */
65    char *argv[4];
66    int i;
67    int fdlimit = sysconf(_SC_OPEN_MAX);
68
69    for (i=0; i<fdlimit; i++) {
70      if (i!=child_write_fd) close(i);
71    }
72    dup2(child_write_fd, 1 /*stdout*/);
73    dup2(child_write_fd, 2 /*stderr*/);
74    close(child_write_fd);
75
76    argv[0] = "sh";
77    argv[1] = "-c";
78    argv[2] = command;
79    argv[3] = 0;
80    execv("/bin/sh", argv);
81    _exit(127);
82  }
83
84  return pe;
85}
86
87void owl_popexec_inputhandler(int handle, int fd, int eventmask, void *data)
88{
89  owl_popexec *pe = (owl_popexec*)data;
90  int navail, bread, rv_navail;
91  char *buf;
92  int status;
93
94  if (!pe) return;
95
96  /* If pe->winactive is 0 then the vwin has closed.
97   * If pe->pid is 0 then the child has already been reaped.
98   * if pe->rfd is -1 then the fd has been closed out.
99   * Under these cases we want to get to a state where:
100   *   - data read until end if child running
101   *   - child reaped
102   *   - fd closed
103   *   - callback removed
104   */
105
106  /* the viewwin has closed */
107  if (pe->rfd<0 && !pe->pid && !pe->winactive) {
108    owl_muxevents_remove(owl_global_get_muxevents(&g), handle);
109    owl_function_debugmsg("unref of %p from input handler at A", pe);
110    owl_popexec_unref(pe);
111    return;
112  }
113
114  if (0 != (rv_navail = ioctl(fd, FIONREAD, (void*)&navail))) {
115    owl_function_debugmsg("ioctl error");
116  }
117
118  /* check to see if the child has ended gracefully and no more data is
119   * ready to be read... */
120  if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) {
121    owl_function_debugmsg("waitpid got child status: <%d>\n", status);
122    pe->pid = 0;
123    if (pe->winactive) { 
124      owl_viewwin_append_text(pe->vwin, "\n");
125      owl_viewwin_redisplay(pe->vwin, 1);
126    }
127    if (pe->rfd>0) {
128      close(pe->rfd);
129      pe->rfd = -1;
130    }
131    owl_muxevents_remove(owl_global_get_muxevents(&g), handle);
132    owl_function_debugmsg("unref of %p from input handler at B", pe);
133    owl_popexec_unref(pe);
134    return;
135  }
136
137  if (pe->rfd<0 || !pe->pid || !pe->winactive || rv_navail) {
138    owl_function_error("popexec should not have reached this point");
139    return;
140  }
141
142  if (navail<=0) return;
143  if (navail>1024) { navail = 1024; }
144  buf = owl_malloc(navail+1);
145  owl_function_debugmsg("about to read %d\n", navail);
146  bread = read(fd, buf, navail);
147  if (bread<0) {
148    perror("read");
149    owl_function_debugmsg("read error");
150  }
151  if (buf[navail-1] != '\0') {
152    buf[navail] = '\0';
153  }
154  owl_function_debugmsg("got data:  <%s>\n", buf);
155  if (pe->winactive) {
156    owl_viewwin_append_text(pe->vwin, buf);
157    owl_viewwin_redisplay(pe->vwin, 1);
158  }
159  owl_free(buf);
160 
161}
162
163void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
164{
165  owl_popexec *pe = (owl_popexec*)data;
166  int status, rv;
167
168  pe->winactive = 0;
169  if (pe->rfd>0) {
170    close(pe->rfd);
171    pe->rfd = -1;
172  }
173  if (pe->pid) {
174    /* TODO: we should handle the case where SIGTERM isn't good enough */
175    rv = kill(pe->pid, SIGTERM);
176    owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv);
177    rv = waitpid(pe->pid, &status, 0);
178    owl_function_debugmsg("waidpid returned %d, status %d", rv, status);
179    pe->pid = 0;
180  }
181  owl_function_debugmsg("unref of %p from onclose", pe);
182  owl_popexec_unref(pe);
183}
184
185void owl_popexec_unref(owl_popexec *pe)
186{
187  owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
188  pe->refcount--;
189  if (pe->refcount<=0) {
190    owl_function_debugmsg("doing free of %p", pe);
191    owl_free(pe);
192  }
193}
Note: See TracBrowser for help on using the repository browser.