[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] | 11 | owl_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; |
---|
[a6a9ddb] | 18 | GIOChannel *channel; |
---|
[afbf668] | 19 | |
---|
[9eb38bb] | 20 | if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) { |
---|
[03ca005] | 21 | owl_function_error("Popwin already in use."); |
---|
| 22 | return NULL; |
---|
| 23 | } |
---|
| 24 | |
---|
[96828e4] | 25 | pe = g_new(owl_popexec, 1); |
---|
[afbf668] | 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; |
---|
[a6a9ddb] | 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_PRI | G_IO_ERR | G_IO_HUP, |
---|
| 61 | owl_popexec_inputhandler, pe, |
---|
| 62 | (GDestroyNotify)owl_popexec_unref); |
---|
| 63 | g_io_channel_unref(channel); |
---|
[afbf668] | 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 | |
---|
[7565f8f] | 77 | execl("/bin/sh", "sh", "-c", command, (const char *)NULL); |
---|
[afbf668] | 78 | _exit(127); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | return pe; |
---|
| 82 | } |
---|
| 83 | |
---|
[a6a9ddb] | 84 | gboolean owl_popexec_inputhandler(GIOChannel *source, GIOCondition condition, void *data) |
---|
[1971b59] | 85 | { |
---|
[18fdd5f9] | 86 | owl_popexec *pe = data; |
---|
[afbf668] | 87 | int navail, bread, rv_navail; |
---|
| 88 | char *buf; |
---|
| 89 | int status; |
---|
[a6a9ddb] | 90 | int fd = g_io_channel_unix_get_fd(source); |
---|
[afbf668] | 91 | |
---|
[a6a9ddb] | 92 | /* TODO: Reading from GIOChannel may be more convenient. */ |
---|
| 93 | |
---|
| 94 | if (!pe) return FALSE; |
---|
[afbf668] | 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. |
---|
[40c6657] | 98 | * if d->fd is -1 then the fd has been closed out. |
---|
[afbf668] | 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 */ |
---|
[40c6657] | 107 | if (!pe->pid && !pe->winactive) { |
---|
[a6a9ddb] | 108 | pe->io_watch = 0; |
---|
| 109 | return FALSE; |
---|
[afbf668] | 110 | } |
---|
| 111 | |
---|
[a6a9ddb] | 112 | if (0 != (rv_navail = ioctl(fd, FIONREAD, &navail))) { |
---|
[afbf668] | 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 | } |
---|
[a6a9ddb] | 124 | pe->io_watch = 0; |
---|
| 125 | return FALSE; |
---|
[afbf668] | 126 | } |
---|
| 127 | |
---|
[a6a9ddb] | 128 | if (fd<0 || !pe->pid || !pe->winactive || rv_navail) { |
---|
[afbf668] | 129 | owl_function_error("popexec should not have reached this point"); |
---|
[a6a9ddb] | 130 | return FALSE; |
---|
[afbf668] | 131 | } |
---|
| 132 | |
---|
[a6a9ddb] | 133 | if (navail<=0) return TRUE; |
---|
[afbf668] | 134 | if (navail>1024) { navail = 1024; } |
---|
[96828e4] | 135 | buf = g_new(char, navail+1); |
---|
[40c6657] | 136 | owl_function_debugmsg("about to read %d", navail); |
---|
[a6a9ddb] | 137 | bread = read(fd, buf, navail); |
---|
[afbf668] | 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 | } |
---|
[40c6657] | 145 | owl_function_debugmsg("got data: <%s>", buf); |
---|
[afbf668] | 146 | if (pe->winactive) { |
---|
| 147 | owl_viewwin_append_text(pe->vwin, buf); |
---|
| 148 | } |
---|
[ddbbcffa] | 149 | g_free(buf); |
---|
[a6a9ddb] | 150 | return TRUE; |
---|
[40c6657] | 151 | } |
---|
| 152 | |
---|
[1971b59] | 153 | void 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; |
---|
[a6a9ddb] | 159 | if (pe->io_watch) { |
---|
| 160 | g_source_remove(pe->io_watch); |
---|
| 161 | pe->io_watch = 0; |
---|
[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] | 175 | void 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 | } |
---|