[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; |
---|
[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 | pe->winactive=0; |
---|
| 26 | pe->pid=0; |
---|
| 27 | pe->refcount=0; |
---|
| 28 | |
---|
[03ca005] | 29 | pw = owl_popwin_new(); |
---|
| 30 | owl_global_set_popwin(&g, pw); |
---|
| 31 | owl_popwin_up(pw); |
---|
[9eb38bb] | 32 | pe->vwin = v = owl_viewwin_new_text(owl_popwin_get_content(pw), ""); |
---|
| 33 | owl_global_set_viewwin(&g, v); |
---|
| 34 | owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe); |
---|
[afbf668] | 35 | |
---|
[07b59ea] | 36 | owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL); |
---|
[afbf668] | 37 | pe->refcount++; |
---|
| 38 | |
---|
| 39 | if (0 != pipe(pipefds)) { |
---|
| 40 | owl_function_error("owl_function_popless_exec: pipe failed\n"); |
---|
| 41 | return NULL; |
---|
| 42 | } |
---|
| 43 | parent_read_fd = pipefds[0]; |
---|
| 44 | child_write_fd = pipefds[1]; |
---|
| 45 | pid = fork(); |
---|
| 46 | if (pid == -1) { |
---|
| 47 | close(pipefds[0]); |
---|
| 48 | close(pipefds[1]); |
---|
| 49 | owl_function_error("owl_function_popless_exec: fork failed\n"); |
---|
| 50 | return NULL; |
---|
| 51 | } else if (pid != 0) { |
---|
[40c6657] | 52 | close(child_write_fd); |
---|
[afbf668] | 53 | /* still in owl */ |
---|
| 54 | pe->pid=pid; |
---|
| 55 | pe->winactive=1; |
---|
[18fdd5f9] | 56 | 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] | 57 | pe->refcount++; |
---|
| 58 | } else { |
---|
| 59 | /* in the child process */ |
---|
| 60 | int i; |
---|
| 61 | int fdlimit = sysconf(_SC_OPEN_MAX); |
---|
| 62 | |
---|
| 63 | for (i=0; i<fdlimit; i++) { |
---|
| 64 | if (i!=child_write_fd) close(i); |
---|
| 65 | } |
---|
| 66 | dup2(child_write_fd, 1 /*stdout*/); |
---|
| 67 | dup2(child_write_fd, 2 /*stderr*/); |
---|
| 68 | close(child_write_fd); |
---|
| 69 | |
---|
[7565f8f] | 70 | execl("/bin/sh", "sh", "-c", command, (const char *)NULL); |
---|
[afbf668] | 71 | _exit(127); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | return pe; |
---|
| 75 | } |
---|
| 76 | |
---|
[18fdd5f9] | 77 | void owl_popexec_inputhandler(const owl_io_dispatch *d, void *data) |
---|
[1971b59] | 78 | { |
---|
[18fdd5f9] | 79 | owl_popexec *pe = data; |
---|
[afbf668] | 80 | int navail, bread, rv_navail; |
---|
| 81 | char *buf; |
---|
| 82 | int status; |
---|
| 83 | |
---|
| 84 | if (!pe) return; |
---|
| 85 | |
---|
| 86 | /* If pe->winactive is 0 then the vwin has closed. |
---|
| 87 | * If pe->pid is 0 then the child has already been reaped. |
---|
[40c6657] | 88 | * if d->fd is -1 then the fd has been closed out. |
---|
[afbf668] | 89 | * Under these cases we want to get to a state where: |
---|
| 90 | * - data read until end if child running |
---|
| 91 | * - child reaped |
---|
| 92 | * - fd closed |
---|
| 93 | * - callback removed |
---|
| 94 | */ |
---|
| 95 | |
---|
| 96 | /* the viewwin has closed */ |
---|
[40c6657] | 97 | if (!pe->pid && !pe->winactive) { |
---|
[18fdd5f9] | 98 | owl_select_remove_io_dispatch(d); |
---|
[125fd21] | 99 | pe->dispatch = NULL; |
---|
[afbf668] | 100 | return; |
---|
| 101 | } |
---|
| 102 | |
---|
[4d86e06] | 103 | if (0 != (rv_navail = ioctl(d->fd, FIONREAD, &navail))) { |
---|
[afbf668] | 104 | owl_function_debugmsg("ioctl error"); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /* check to see if the child has ended gracefully and no more data is |
---|
| 108 | * ready to be read... */ |
---|
| 109 | if (navail==0 && pe->pid>0 && waitpid(pe->pid, &status, WNOHANG) > 0) { |
---|
| 110 | owl_function_debugmsg("waitpid got child status: <%d>\n", status); |
---|
| 111 | pe->pid = 0; |
---|
| 112 | if (pe->winactive) { |
---|
| 113 | owl_viewwin_append_text(pe->vwin, "\n"); |
---|
| 114 | } |
---|
[18fdd5f9] | 115 | owl_select_remove_io_dispatch(d); |
---|
[125fd21] | 116 | pe->dispatch = NULL; |
---|
[afbf668] | 117 | return; |
---|
| 118 | } |
---|
| 119 | |
---|
[40c6657] | 120 | if (d->fd<0 || !pe->pid || !pe->winactive || rv_navail) { |
---|
[afbf668] | 121 | owl_function_error("popexec should not have reached this point"); |
---|
| 122 | return; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (navail<=0) return; |
---|
| 126 | if (navail>1024) { navail = 1024; } |
---|
[96828e4] | 127 | buf = g_new(char, navail+1); |
---|
[40c6657] | 128 | owl_function_debugmsg("about to read %d", navail); |
---|
| 129 | bread = read(d->fd, buf, navail); |
---|
[afbf668] | 130 | if (bread<0) { |
---|
| 131 | perror("read"); |
---|
| 132 | owl_function_debugmsg("read error"); |
---|
| 133 | } |
---|
| 134 | if (buf[navail-1] != '\0') { |
---|
| 135 | buf[navail] = '\0'; |
---|
| 136 | } |
---|
[40c6657] | 137 | owl_function_debugmsg("got data: <%s>", buf); |
---|
[afbf668] | 138 | if (pe->winactive) { |
---|
| 139 | owl_viewwin_append_text(pe->vwin, buf); |
---|
| 140 | } |
---|
[ddbbcffa] | 141 | g_free(buf); |
---|
[afbf668] | 142 | |
---|
| 143 | } |
---|
| 144 | |
---|
[18fdd5f9] | 145 | void owl_popexec_delete_dispatch(const owl_io_dispatch *d) |
---|
[40c6657] | 146 | { |
---|
| 147 | owl_popexec *pe = d->data; |
---|
| 148 | close(d->fd); |
---|
| 149 | owl_popexec_unref(pe); |
---|
| 150 | } |
---|
| 151 | |
---|
[1971b59] | 152 | void owl_popexec_viewwin_onclose(owl_viewwin *vwin, void *data) |
---|
| 153 | { |
---|
[4d86e06] | 154 | owl_popexec *pe = data; |
---|
[afbf668] | 155 | int status, rv; |
---|
| 156 | |
---|
| 157 | pe->winactive = 0; |
---|
[125fd21] | 158 | if (pe->dispatch) { |
---|
[18fdd5f9] | 159 | owl_select_remove_io_dispatch(pe->dispatch); |
---|
[125fd21] | 160 | pe->dispatch = NULL; |
---|
[afbf668] | 161 | } |
---|
| 162 | if (pe->pid) { |
---|
| 163 | /* TODO: we should handle the case where SIGTERM isn't good enough */ |
---|
| 164 | rv = kill(pe->pid, SIGTERM); |
---|
| 165 | owl_function_debugmsg("kill of pid %d returned %d", pe->pid, rv); |
---|
| 166 | rv = waitpid(pe->pid, &status, 0); |
---|
| 167 | owl_function_debugmsg("waidpid returned %d, status %d", rv, status); |
---|
| 168 | pe->pid = 0; |
---|
| 169 | } |
---|
[8721756] | 170 | owl_function_debugmsg("unref of %p from onclose", pe); |
---|
[afbf668] | 171 | owl_popexec_unref(pe); |
---|
| 172 | } |
---|
| 173 | |
---|
[1971b59] | 174 | void owl_popexec_unref(owl_popexec *pe) |
---|
| 175 | { |
---|
[8721756] | 176 | owl_function_debugmsg("unref of %p was %d", pe, pe->refcount); |
---|
[afbf668] | 177 | pe->refcount--; |
---|
| 178 | if (pe->refcount<=0) { |
---|
[8721756] | 179 | owl_function_debugmsg("doing free of %p", pe); |
---|
[ddbbcffa] | 180 | g_free(pe); |
---|
[afbf668] | 181 | } |
---|
| 182 | } |
---|