source: popexec.c @ e56303f

release-1.10release-1.8release-1.9
Last change on this file since e56303f was ddbbcffa, checked in by Anders Kaseorg <andersk@mit.edu>, 14 years ago
Replace owl_free with g_free. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Karl Ramm <kcr@mit.edu>
  • Property mode set to 100644
File size: 4.6 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
10/* starts up popexec in a new viewwin */
[e19eb97]11owl_popexec *owl_popexec_new(const char *command)
[afbf668]12{
13  owl_popexec *pe;
14  owl_popwin *pw;
15  owl_viewwin *v;
16  int pipefds[2], child_write_fd, parent_read_fd;
[0e5afa2]17  pid_t pid;
[afbf668]18
[9eb38bb]19  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
[03ca005]20    owl_function_error("Popwin already in use.");
21    return NULL;
22  }
23
[96828e4]24  pe = g_new(owl_popexec, 1);
[afbf668]25  if (!pe) return NULL;
26  pe->winactive=0;
27  pe->pid=0;
28  pe->refcount=0;
29
[03ca005]30  pw = owl_popwin_new();
31  owl_global_set_popwin(&g, pw);
32  owl_popwin_up(pw);
[9eb38bb]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);
[afbf668]36
[07b59ea]37  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
[afbf668]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;
[18fdd5f9]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);
[afbf668]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
[7565f8f]71    execl("/bin/sh", "sh", "-c", command, (const char *)NULL);
[afbf668]72    _exit(127);
73  }
74
75  return pe;
76}
77
[18fdd5f9]78void owl_popexec_inputhandler(const owl_io_dispatch *d, void *data)
[1971b59]79{
[18fdd5f9]80  owl_popexec *pe = data;
[afbf668]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.
[40c6657]89   * if d->fd is -1 then the fd has been closed out.
[afbf668]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 */
[40c6657]98  if (!pe->pid && !pe->winactive) {
[18fdd5f9]99    owl_select_remove_io_dispatch(d);
[125fd21]100    pe->dispatch = NULL;
[afbf668]101    return;
102  }
103
[4d86e06]104  if (0 != (rv_navail = ioctl(d->fd, FIONREAD, &navail))) {
[afbf668]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    }
[18fdd5f9]116    owl_select_remove_io_dispatch(d);
[125fd21]117    pe->dispatch = NULL;
[afbf668]118    return;
119  }
120
[40c6657]121  if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) {
[afbf668]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; }
[96828e4]128  buf = g_new(char, navail+1);
[40c6657]129  owl_function_debugmsg("about to read %d", navail);
130  bread = read(d->fd, buf, navail);
[afbf668]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  }
[40c6657]138  owl_function_debugmsg("got data:  <%s>", buf);
[afbf668]139  if (pe->winactive) {
140    owl_viewwin_append_text(pe->vwin, buf);
141  }
[ddbbcffa]142  g_free(buf);
[afbf668]143 
144}
145
[18fdd5f9]146void owl_popexec_delete_dispatch(const owl_io_dispatch *d)
[40c6657]147{
148  owl_popexec *pe = d->data;
149  close(d->fd);
150  owl_popexec_unref(pe);
151}
152
[1971b59]153void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
154{
[4d86e06]155  owl_popexec *pe = data;
[afbf668]156  int status, rv;
157
158  pe->winactive = 0;
[125fd21]159  if (pe->dispatch) {
[18fdd5f9]160    owl_select_remove_io_dispatch(pe->dispatch);
[125fd21]161    pe->dispatch = NULL;
[afbf668]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  }
[8721756]171  owl_function_debugmsg("unref of %p from onclose", pe);
[afbf668]172  owl_popexec_unref(pe);
173}
174
[1971b59]175void owl_popexec_unref(owl_popexec *pe)
176{
[8721756]177  owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
[afbf668]178  pe->refcount--;
179  if (pe->refcount<=0) {
[8721756]180    owl_function_debugmsg("doing free of %p", pe);
[ddbbcffa]181    g_free(pe);
[afbf668]182  }
183}
Note: See TracBrowser for help on using the repository browser.