History

cmd(3): implement command execution for posix

cinap_lenrek committed 6 years ago

Commit 580a5141 parent 9c784142 files · +60 −14Browse files

Unified · Split

2 files
  1. kern/devcmd.c +0 −1
  2. kern/posix.c +60 −13
kern/devcmd.c+0−1
@@ -343,7 +343,6 @@ cmdclose(Chan *c) · 342 unmodified lines
343 if(cc->child != nil){343 if(cc->child != nil){
344 if(!cc->killed)344 if(!cc->killed)
345 if(r == 0 || (cc->killonclose && TYPE(c->qid) == Qctl)){345 if(r == 0 || (cc->killonclose && TYPE(c->qid) == Qctl)){
346 iprint("cmdclose killing (killonclose %d)\n", cc->killonclose);
347 oscmdkill(cc->child);346 oscmdkill(cc->child);
348 cc->killed = 1;347 cc->killed = 1;
349 }348 }
kern/posix.c+60−13
@@ -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
60void62void
61osnewproc(Proc *p)63osnewproc(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
169void*178void*
170oscmd(char **argv, int nice, char *dir, Chan **fd)179oscmd(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
181int226int
182oscmdwait(void *c, char *status, int nstatus)227oscmdwait(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
191int238int
192oscmdkill(void *c)239oscmdkill(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
198void245void

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t