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
343343 if(cc->child != nil){
344344 if(!cc->killed)
345345 if(r == 0 || (cc->killonclose && TYPE(c->qid) == Qctl)){
346− iprint("cmdclose killing (killonclose %d)\n", cc->killonclose);
347346 oscmdkill(cc->child);
348347 cc->killed = 1;
349348 }
kern/posix.c+60−13
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010
1111 #include <pthread.h>
1212 #include <time.h>
13+#include <sys/wait.h>
1314 #include <sys/time.h>
1415 #include <sys/select.h>
1516 #include <signal.h>
@@ -54,9 +55,10 @@ osinit(void) · 38 unmodified lines
5455 {
5556 if(pthread_key_create(&prdakey, 0))
5657 panic("cannot pthread_key_create");
58+
59+ signal(SIGPIPE, SIG_IGN);
5760 }
5861
59−#undef pipe
6062 void
6163 osnewproc(Proc *p)
6264 {
@@ -166,33 +168,78 @@ procwakeup(Proc *p) · 103 unmodified lines
166168 pthread_mutex_unlock(&op->mutex);
167169 }
168170
171+#undef pipe
172+#undef fork
173+#undef close
174+#undef dup2
175+#undef execv
176+#undef setsid
177+#undef exit
169178 void*
170179 oscmd(char **argv, int nice, char *dir, Chan **fd)
171180 {
172− USED(argv);
173− USED(nice);
174− USED(dir);
175− USED(fd);
181+ int p[3][2];
182+ int i, pid;
183+
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]);
176217
177− error("not implemented");
178− return nil;
218+ fd[0] = lfdchan((void*)(uintptr)p[0][1]);
219+ fd[1] = lfdchan((void*)(uintptr)p[1][0]);
220+ fd[2] = lfdchan((void*)(uintptr)p[2][0]);
221+
222+ return (void*)(uintptr)pid;
179223 }
180224
225+#undef waitpid
181226 int
182227 oscmdwait(void *c, char *status, int nstatus)
183228 {
184− USED(c);
185− USED(status);
186− USED(nstatus);
229+ int pid = (int)(uintptr)c;
230+ int wstatus = -1;
187231
188− return -1;
232+ if(waitpid(pid, &wstatus, 0) < 0)
233+ return -1;
234+ return snprint(status, nstatus, "%d", wstatus);
189235 }
190236
237+#undef kill
191238 int
192239 oscmdkill(void *c)
193240 {
194− USED(c);
195− return -1;
241+ int pid = (int)(uintptr)c;
242+ return kill(pid, SIGTERM);
196243 }
197244
198245 void

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