1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include "owl.h"
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <sys/wait.h>
/* starts up popexec in a new viewwin */
owl_popexec *owl_popexec_new(const char *command)
{
owl_popexec *pe;
owl_popwin *pw;
owl_viewwin *v;
int pipefds[2], child_write_fd, parent_read_fd;
pid_t pid;
GIOChannel *channel;
if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
owl_function_error("Popwin already in use.");
return NULL;
}
pe = g_slice_new(owl_popexec);
pe->winactive=0;
pe->pid=0;
pe->refcount=0;
pw = owl_popwin_new();
owl_global_set_popwin(&g, pw);
owl_popwin_up(pw);
pe->vwin = v = owl_viewwin_new_text(owl_popwin_get_content(pw), "");
owl_global_set_viewwin(&g, v);
owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
pe->refcount++;
if (0 != pipe(pipefds)) {
owl_function_error("owl_function_popless_exec: pipe failed\n");
return NULL;
}
parent_read_fd = pipefds[0];
child_write_fd = pipefds[1];
pid = fork();
if (pid == -1) {
close(pipefds[0]);
close(pipefds[1]);
owl_function_error("owl_function_popless_exec: fork failed\n");
return NULL;
} else if (pid != 0) {
close(child_write_fd);
/* still in owl */
pe->pid=pid;
pe->winactive=1;
channel = g_io_channel_unix_new(parent_read_fd);
g_io_channel_set_close_on_unref(channel, TRUE);
pe->io_watch = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_ERR | G_IO_HUP,
owl_popexec_inputhandler, pe,
(GDestroyNotify)owl_popexec_unref);
g_io_channel_unref(channel);
pe->refcount++;
} else {
/* in the child process */
int i;
int fdlimit = sysconf(_SC_OPEN_MAX);
for (i=0; i<fdlimit; i++) {
if (i!=child_write_fd) close(i);
}
dup2(child_write_fd, 1 /*stdout*/);
dup2(child_write_fd, 2 /*stderr*/);
close(child_write_fd);
execl("/bin/sh", "sh", "-c", command, (const char *)NULL);
_exit(127);
}
return pe;
}
gboolean owl_popexec_inputhandler(GIOChannel *source, GIOCondition condition, void *data)
{
owl_popexec *pe = data;
int navail, bread, rv_navail;
char *buf;
int status;
int fd = g_io_channel_unix_get_fd(source);
/* TODO: Reading from GIOChannel may be more convenient. */
if (!pe) return FALSE;
/* If pe->winactive is 0 then the vwin has closed.
* If pe->pid is 0 then the child has already been reaped.
* if d->fd is -1 then the fd has been closed out.
* Under these cases we want to get to a state where:
* - data read until end if child running
* - child reaped
* - fd closed
* - callback removed
*/
/* the viewwin has closed */
if (!pe->pid && !pe->winactive) {
pe->io_watch = 0;
return FALSE;
}
if (0 != (rv_navail = ioctl(fd, FIONREAD, &navail))) {
owl_function_debugmsg("ioctl error");
}
/* check to see if the child has ended gracefully and no more data is
* ready to be read... */
if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) {
owl_function_debugmsg("waitpid got child status: <%d>\n", status);
pe->pid = 0;
if (pe->winactive) {
owl_viewwin_append_text(pe->vwin, "\n");
}
pe->io_watch = 0;
return FALSE;
}
if (fd<0 || !pe->pid || !pe->winactive || rv_navail) {
owl_function_error("popexec should not have reached this point");
return FALSE;
}
if (navail<=0) return TRUE;
if (navail>1024) { navail = 1024; }
buf = g_new(char, navail+1);
owl_function_debugmsg("about to read %d", navail);
bread = read(fd, buf, navail);
if (bread<0) {
perror("read");
owl_function_debugmsg("read error");
}
if (buf[navail-1] != '\0') {
buf[navail] = '\0';
}
owl_function_debugmsg("got data: <%s>", buf);
if (pe->winactive) {
owl_viewwin_append_text(pe->vwin, buf);
}
g_free(buf);
return TRUE;
}
void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data)
{
owl_popexec *pe = data;
int status, rv;
pe->winactive = 0;
if (pe->io_watch) {
g_source_remove(pe->io_watch);
pe->io_watch = 0;
}
if (pe->pid) {
/* TODO: we should handle the case where SIGTERM isn't good enough */
rv = kill(pe->pid, SIGTERM);
owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv);
rv = waitpid(pe->pid, &status, 0);
owl_function_debugmsg("waidpid returned %d, status %d", rv, status);
pe->pid = 0;
}
owl_function_debugmsg("unref of %p from onclose", pe);
owl_popexec_unref(pe);
}
void owl_popexec_unref(owl_popexec *pe)
{
owl_function_debugmsg("unref of %p was %d", pe, pe->refcount);
pe->refcount--;
if (pe->refcount<=0) {
owl_function_debugmsg("doing free of %p", pe);
g_slice_free(owl_popexec, pe);
}
}
|