From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <13ab91b7d3a360a0ca444540bc498ba7@vitanuova.com> To: 9fans@cse.psu.edu From: C H Forsyth MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] fun with rfork Date: Fri, 31 Oct 2003 10:25:58 +0000 Topicbox-Message-UUID: 7b6c2ca8-eacc-11e9-9e20-41e7f4b1d025 here is part of an application of rfork(RFMEM|RFPROC ...). most irrelevant code has been removed. libinit was called early on before the process switched to a stack on the malloc heap, and that's where it is running when it calls oscmd. what happens next? -------- void libinit(char *imod) { ... int fd, n, pid; ... /* * guess at a safe stack for vstack */ ustack = (ulong)&fd; ... /* switch to stack on the heap ... */ tramp(sp+KSTACK, up->func, up->arg); } typedef struct Targ Targ; struct Targ { int fd; int* spin; char* cmd; }; void exectramp(Targ *targ) { *targ->spin = 0; ... exec(argv[0], argv); exits(""); } int oscmd(char *cmd, int *rfd, int *sfd) { Targ targ; int spin, *spinptr, fd[2]; if(pipe(fd) < 0) return -1; spinptr = &spin; spin = 1; targ.fd = fd[0]; targ.cmd = cmd; targ.spin = spinptr; switch(rfork(RFMEM|RFPROC|RFFDG|RFENVG|RFREND)) { case -1: return -1; case 0: vstack(&targ); /* Never returns */ default: while(*spinptr) ; break; } close(fd[0]); *rfd = fd[1]; *sfd = fd[1]; return 0; } TEXT vstack(SB),$0 MOVL arg+0(FP), AX MOVL ustack(SB), SP PUSHL AX CALL exectramp(SB) POPL AX /* dammit ken! */ RET