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