source: popexec.c @ d70f45f

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