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