source: popexec.c @ 7ba2ad4

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