9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] fun with rfork
@ 2003-10-31 10:25 C H Forsyth
  2003-10-31 23:35 ` Bruce Ellis
  0 siblings, 1 reply; 5+ messages in thread
From: C H Forsyth @ 2003-10-31 10:25 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-11-01 12:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-31 10:25 [9fans] fun with rfork C H Forsyth
2003-10-31 23:35 ` Bruce Ellis
2003-11-01 10:03   ` Charles Forsyth
2003-11-01 11:40     ` Bruce Ellis
2003-11-01 12:41       ` Charles Forsyth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).