source: popexec.c @ 3e36085

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 3e36085 was 40c6657, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Use owl_select instead of owl_muxevents for owl_popexec.
  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[afbf668]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) {
[40c6657]53    close(child_write_fd);
[afbf668]54    /* still in owl */
55    pe->pid=pid;
56    pe->winactive=1;
[40c6657]57    pe->dispatch.fd = parent_read_fd;
58    pe->dispatch.cfunc = owl_popexec_inputhandler;
59    pe->dispatch.destroy = owl_popexec_free_dispatch;
60    pe->dispatch.data = pe;
61    owl_select_add_dispatch(&pe->dispatch);
[afbf668]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
[40c6657]87void owl_popexec_inputhandler(owl_dispatch *d)
[1971b59]88{
[40c6657]89  owl_popexec *pe = d->data;
[afbf668]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.
[40c6657]98   * if d->fd is -1 then the fd has been closed out.
[afbf668]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 */
[40c6657]107  if (!pe->pid && !pe->winactive) {
108    owl_select_remove_dispatch(d->fd);
[afbf668]109    return;
110  }
111
[40c6657]112  if (0 != (rv_navail = ioctl(d->fd, FIONREAD, (void*)&navail))) {
[afbf668]113    owl_function_debugmsg("ioctl error");
114  }
115
116  /* check to see if the child has ended gracefully and no more data is
117   * ready to be read... */
118  if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) {
119    owl_function_debugmsg("waitpid got child status: <%d>\n", status);
120    pe->pid = 0;
121    if (pe->winactive) { 
122      owl_viewwin_append_text(pe->vwin, "\n");
123      owl_viewwin_redisplay(pe->vwin, 1);
124    }
[40c6657]125    owl_select_remove_dispatch(d->fd);
[afbf668]126    return;
127  }
128
[40c6657]129  if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) {
[afbf668]130    owl_function_error("popexec should not have reached this point");
131    return;
132  }
133
134  if (navail<=0) return;
135  if (navail>1024) { navail = 1024; }
136  buf = owl_malloc(navail+1);
[40c6657]137  owl_function_debugmsg("about to read %d", navail);
138  bread = read(d->fd, buf, navail);
[afbf668]139  if (bread<0) {
140    perror("read");
141    owl_function_debugmsg("read error");
142  }
143  if (buf[navail-1] != '\0') {
144    buf[navail] = '\0';
145  }
[40c6657]146  owl_function_debugmsg("got data:  <%s>", buf);
[afbf668]147  if (pe->winactive) {
148    owl_viewwin_append_text(pe->vwin, buf);
149    owl_viewwin_redisplay(pe->vwin, 1);
150  }
151  owl_free(buf);
152 
153}
154
[40c6657]155void owl_popexec_free_dispatch(owl_dispatch *d)
156{
157  owl_popexec *pe = d->data;
158  close(d->fd);
159  owl_popexec_unref(pe);
160}
161
[1971b59]162void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
163{
[afbf668]164  owl_popexec *pe = (owl_popexec*)data;
165  int status, rv;
166
167  pe->winactive = 0;
[40c6657]168  if (pe->dispatch.fd>0) {
169    owl_select_remove_dispatch(pe->dispatch.fd);
[afbf668]170  }
171  if (pe->pid) {
172    /* TODO: we should handle the case where SIGTERM isn't good enough */
173    rv = kill(pe->pid, SIGTERM);
174    owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv);
175    rv = waitpid(pe->pid, &status, 0);
176    owl_function_debugmsg("waidpid returned %d, status %d", rv, status);
177    pe->pid = 0;
178  }
[8721756]179  owl_function_debugmsg("unref of %p from onclose", pe);
[afbf668]180  owl_popexec_unref(pe);
181}
182
[1971b59]183void owl_popexec_unref(owl_popexec *pe)
184{
[8721756]185  owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
[afbf668]186  pe->refcount--;
187  if (pe->refcount<=0) {
[8721756]188    owl_function_debugmsg("doing free of %p", pe);
[afbf668]189    owl_free(pe);
190  }
191}
Note: See TracBrowser for help on using the repository browser.