source: popexec.c @ 099597c

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 099597c was 9eb38bb, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Likewise, don't reuse a global owl_viewwin This means we don't have to support plugging and unplugging that thing over and over. We also can almost entirely drop the global owl_viewwin. Most of the code gets it from the context anyway.
  • Property mode set to 100644
File size: 4.7 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
10/* starts up popexec in a new viewwin */
11owl_popexec *owl_popexec_new(const char *command)
12{
13  owl_popexec *pe;
14  owl_popwin *pw;
15  owl_viewwin *v;
16  int pipefds[2], child_write_fd, parent_read_fd;
17  pid_t pid;
18
19  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
20    owl_function_error("Popwin already in use.");
21    return NULL;
22  }
23
24  pe = owl_malloc(sizeof(owl_popexec));
25  if (!pe) return NULL;
26  pe->winactive=0;
27  pe->pid=0;
28  pe->refcount=0;
29
30  pw = owl_popwin_new();
31  owl_global_set_popwin(&g, pw);
32  owl_popwin_up(pw);
33  pe->vwin = v = owl_viewwin_new_text(owl_popwin_get_content(pw), "");
34  owl_global_set_viewwin(&g, v);
35  owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
36
37  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
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    close(child_write_fd);
54    /* still in owl */
55    pe->pid=pid;
56    pe->winactive=1;
57    pe->dispatch = owl_select_add_io_dispatch(parent_read_fd, OWL_IO_READ|OWL_IO_EXCEPT, &owl_popexec_inputhandler, &owl_popexec_delete_dispatch, pe);
58    pe->refcount++;
59  } else {
60    /* in the child process */
61    int i;
62    int fdlimit = sysconf(_SC_OPEN_MAX);
63
64    for (i=0; i<fdlimit; i++) {
65      if (i!=child_write_fd) close(i);
66    }
67    dup2(child_write_fd, 1 /*stdout*/);
68    dup2(child_write_fd, 2 /*stderr*/);
69    close(child_write_fd);
70
71    execl("/bin/sh", "sh", "-c", command, (const char *)NULL);
72    _exit(127);
73  }
74
75  return pe;
76}
77
78void owl_popexec_inputhandler(const owl_io_dispatch *d, void *data)
79{
80  owl_popexec *pe = data;
81  int navail, bread, rv_navail;
82  char *buf;
83  int status;
84
85  if (!pe) return;
86
87  /* If pe->winactive is 0 then the vwin has closed.
88   * If pe->pid is 0 then the child has already been reaped.
89   * if d->fd is -1 then the fd has been closed out.
90   * Under these cases we want to get to a state where:
91   *   - data read until end if child running
92   *   - child reaped
93   *   - fd closed
94   *   - callback removed
95   */
96
97  /* the viewwin has closed */
98  if (!pe->pid && !pe->winactive) {
99    owl_select_remove_io_dispatch(d);
100    pe->dispatch = NULL;
101    return;
102  }
103
104  if (0 != (rv_navail = ioctl(d->fd, FIONREAD, &navail))) {
105    owl_function_debugmsg("ioctl error");
106  }
107
108  /* check to see if the child has ended gracefully and no more data is
109   * ready to be read... */
110  if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) {
111    owl_function_debugmsg("waitpid got child status: <%d>\n", status);
112    pe->pid = 0;
113    if (pe->winactive) { 
114      owl_viewwin_append_text(pe->vwin, "\n");
115    }
116    owl_select_remove_io_dispatch(d);
117    pe->dispatch = NULL;
118    return;
119  }
120
121  if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) {
122    owl_function_error("popexec should not have reached this point");
123    return;
124  }
125
126  if (navail<=0) return;
127  if (navail>1024) { navail = 1024; }
128  buf = owl_malloc(navail+1);
129  owl_function_debugmsg("about to read %d", navail);
130  bread = read(d->fd, buf, navail);
131  if (bread<0) {
132    perror("read");
133    owl_function_debugmsg("read error");
134  }
135  if (buf[navail-1] != '\0') {
136    buf[navail] = '\0';
137  }
138  owl_function_debugmsg("got data:  <%s>", buf);
139  if (pe->winactive) {
140    owl_viewwin_append_text(pe->vwin, buf);
141  }
142  owl_free(buf);
143 
144}
145
146void owl_popexec_delete_dispatch(const owl_io_dispatch *d)
147{
148  owl_popexec *pe = d->data;
149  close(d->fd);
150  owl_popexec_unref(pe);
151}
152
153void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
154{
155  owl_popexec *pe = data;
156  int status, rv;
157
158  pe->winactive = 0;
159  if (pe->dispatch) {
160    owl_select_remove_io_dispatch(pe->dispatch);
161    pe->dispatch = NULL;
162  }
163  if (pe->pid) {
164    /* TODO: we should handle the case where SIGTERM isn't good enough */
165    rv = kill(pe->pid, SIGTERM);
166    owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv);
167    rv = waitpid(pe->pid, &status, 0);
168    owl_function_debugmsg("waidpid returned %d, status %d", rv, status);
169    pe->pid = 0;
170  }
171  owl_function_debugmsg("unref of %p from onclose", pe);
172  owl_popexec_unref(pe);
173}
174
175void owl_popexec_unref(owl_popexec *pe)
176{
177  owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
178  pe->refcount--;
179  if (pe->refcount<=0) {
180    owl_function_debugmsg("doing free of %p", pe);
181    owl_free(pe);
182  }
183}
Note: See TracBrowser for help on using the repository browser.