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