| @@ -10,6 +10,7 @@ · 9 unmodified lines |
| 10 | | 10 | |
| 11 | #include <pthread.h> | 11 | #include <pthread.h> |
| 12 | #include <time.h> | 12 | #include <time.h> |
| | 13 | #include <sys/wait.h> |
| 13 | #include <sys/time.h> | 14 | #include <sys/time.h> |
| 14 | #include <sys/select.h> | 15 | #include <sys/select.h> |
| 15 | #include <signal.h> | 16 | #include <signal.h> |
| @@ -54,9 +55,10 @@ osinit(void) · 38 unmodified lines |
| 54 | { | 55 | { |
| 55 | if(pthread_key_create(&prdakey, 0)) | 56 | if(pthread_key_create(&prdakey, 0)) |
| 56 | panic("cannot pthread_key_create"); | 57 | panic("cannot pthread_key_create"); |
| | 58 | |
| | 59 | signal(SIGPIPE, SIG_IGN); |
| 57 | } | 60 | } |
| 58 | | 61 | |
| 59 | #undef pipe | | |
| 60 | void | 62 | void |
| 61 | osnewproc(Proc *p) | 63 | osnewproc(Proc *p) |
| 62 | { | 64 | { |
| @@ -166,33 +168,78 @@ procwakeup(Proc *p) · 103 unmodified lines |
| 166 | pthread_mutex_unlock(&op->mutex); | 168 | pthread_mutex_unlock(&op->mutex); |
| 167 | } | 169 | } |
| 168 | | 170 | |
| | 171 | #undef pipe |
| | 172 | #undef fork |
| | 173 | #undef close |
| | 174 | #undef dup2 |
| | 175 | #undef execv |
| | 176 | #undef setsid |
| | 177 | #undef exit |
| 169 | void* | 178 | void* |
| 170 | oscmd(char **argv, int nice, char *dir, Chan **fd) | 179 | oscmd(char **argv, int nice, char *dir, Chan **fd) |
| 171 | { | 180 | { |
| 172 | USED(argv); | 181 | int p[3][2]; |
| 173 | USED(nice); | 182 | int i, pid; |
| 174 | USED(dir); | 183 | |
| 175 | USED(fd); | 184 | for(i = 0; i<3; i++){ |
| | 185 | if(pipe(p[i]) < 0){ |
| | 186 | while(--i >= 0){ |
| | 187 | close(p[i][0]); |
| | 188 | close(p[i][1]); |
| | 189 | } |
| | 190 | oserror(); |
| | 191 | } |
| | 192 | } |
| | 193 | if(waserror()){ |
| | 194 | for(i = 0; i < 3; i++){ |
| | 195 | close(p[i][0]); |
| | 196 | close(p[i][1]); |
| | 197 | } |
| | 198 | nexterror(); |
| | 199 | } |
| | 200 | pid = fork(); |
| | 201 | if(pid == -1) |
| | 202 | oserror(); |
| | 203 | if(pid == 0){ |
| | 204 | setsid(); |
| | 205 | dup2(p[0][0], 0); |
| | 206 | dup2(p[1][1], 1); |
| | 207 | dup2(p[2][1], 2); |
| | 208 | for(i = 3; i < 1000; i++) |
| | 209 | close(i); |
| | 210 | execv(argv[0], argv); |
| | 211 | exit(0); |
| | 212 | } |
| | 213 | poperror(); |
| | 214 | close(p[0][0]); |
| | 215 | close(p[1][1]); |
| | 216 | close(p[2][1]); |
| 176 | | 217 | |
| 177 | error("not implemented"); | 218 | fd[0] = lfdchan((void*)(uintptr)p[0][1]); |
| 178 | return nil; | 219 | fd[1] = lfdchan((void*)(uintptr)p[1][0]); |
| | 220 | fd[2] = lfdchan((void*)(uintptr)p[2][0]); |
| | 221 | |
| | 222 | return (void*)(uintptr)pid; |
| 179 | } | 223 | } |
| 180 | | 224 | |
| | 225 | #undef waitpid |
| 181 | int | 226 | int |
| 182 | oscmdwait(void *c, char *status, int nstatus) | 227 | oscmdwait(void *c, char *status, int nstatus) |
| 183 | { | 228 | { |
| 184 | USED(c); | 229 | int pid = (int)(uintptr)c; |
| 185 | USED(status); | 230 | int wstatus = -1; |
| 186 | USED(nstatus); | | |
| 187 | | 231 | |
| 188 | return -1; | 232 | if(waitpid(pid, &wstatus, 0) < 0) |
| | 233 | return -1; |
| | 234 | return snprint(status, nstatus, "%d", wstatus); |
| 189 | } | 235 | } |
| 190 | | 236 | |
| | 237 | #undef kill |
| 191 | int | 238 | int |
| 192 | oscmdkill(void *c) | 239 | oscmdkill(void *c) |
| 193 | { | 240 | { |
| 194 | USED(c); | 241 | int pid = (int)(uintptr)c; |
| 195 | return -1; | 242 | return kill(pid, SIGTERM); |
| 196 | } | 243 | } |
| 197 | | 244 | |
| 198 | void | 245 | void |