source: popexec.c @ e1ed6f4

release-1.10release-1.8release-1.9
Last change on this file since e1ed6f4 was e146cd7, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Remove all uses of G_IO_PRI Any use of G_IO_PRI (and correspondingly OWL_IO_EXCEPT) is likely a bug as it's not really used for anything useful. The main use case is TCP out-of-band data which really isn't used by anyone. Reported-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 4.8 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  GIOChannel *channel;
19
20  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
21    owl_function_error("Popwin already in use.");
22    return NULL;
23  }
24
25  pe = g_new(owl_popexec, 1);
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    channel = g_io_channel_unix_new(parent_read_fd);
58    g_io_channel_set_close_on_unref(channel, TRUE);
59    pe->io_watch = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
60                                       G_IO_IN | G_IO_ERR | G_IO_HUP,
61                                       owl_popexec_inputhandler, pe,
62                                       (GDestroyNotify)owl_popexec_unref);
63    g_io_channel_unref(channel);
64    pe->refcount++;
65  } else {
66    /* in the child process */
67    int i;
68    int fdlimit = sysconf(_SC_OPEN_MAX);
69
70    for (i=0; i<fdlimit; i++) {
71      if (i!=child_write_fd) close(i);
72    }
73    dup2(child_write_fd, 1 /*stdout*/);
74    dup2(child_write_fd, 2 /*stderr*/);
75    close(child_write_fd);
76
77    execl("/bin/sh", "sh", "-c", command, (const char *)NULL);
78    _exit(127);
79  }
80
81  return pe;
82}
83
84gboolean owl_popexec_inputhandler(GIOChannel *source, GIOCondition condition, void *data)
85{
86  owl_popexec *pe = data;
87  int navail, bread, rv_navail;
88  char *buf;
89  int status;
90  int fd = g_io_channel_unix_get_fd(source);
91
92  /* TODO: Reading from GIOChannel may be more convenient. */
93
94  if (!pe) return FALSE;
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 d->fd 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->pid && !pe->winactive) {
108    pe->io_watch = 0;
109    return FALSE;
110  }
111
112  if (0 != (rv_navail = ioctl(fd, FIONREAD, &navail))) {
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    }
124    pe->io_watch = 0;
125    return FALSE;
126  }
127
128  if (fd<0 || !pe->pid || !pe->winactive || rv_navail) {
129    owl_function_error("popexec should not have reached this point");
130    return FALSE;
131  }
132
133  if (navail<=0) return TRUE;
134  if (navail>1024) { navail = 1024; }
135  buf = g_new(char, navail+1);
136  owl_function_debugmsg("about to read %d", navail);
137  bread = read(fd, buf, navail);
138  if (bread<0) {
139    perror("read");
140    owl_function_debugmsg("read error");
141  }
142  if (buf[navail-1] != '\0') {
143    buf[navail] = '\0';
144  }
145  owl_function_debugmsg("got data:  <%s>", buf);
146  if (pe->winactive) {
147    owl_viewwin_append_text(pe->vwin, buf);
148  }
149  g_free(buf);
150  return TRUE;
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->io_watch) {
160    g_source_remove(pe->io_watch);
161    pe->io_watch = 0;
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    g_free(pe);
182  }
183}
Note: See TracBrowser for help on using the repository browser.