aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/ffork-Linux.c
blob: 3b5c8ee08551eb4385c35fff40f3541ab2108b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <sched.h>
#include <signal.h>
#include <lib9.h>

int fforkstacksize = 16384;

int
ffork(int flags, void (*fn)(void*), void *arg)
{
	char *p;
	int cloneflag, pid;

	p = malloc(fforkstacksize);
	if(p == nil)
		return -1;
	cloneflag = 0;
	flags &= ~RFPROC;
	if(flags&RFMEM){
		cloneflag |= CLONE_VM;
		flags &= ~RFMEM;
	}
	if(!(flags&RFFDG))
		cloneflag |= CLONE_FILES;
	else
		flags &= ~RFFDG;
	if(!(flags&RFNOWAIT))
		cloneflag |= SIGCHLD;
	else
		flags &= ~RFNOWAIT;
	if(flags){
		fprint(2, "unknown rfork flags %x\n", flags);
		return -1;
	}
	pid = clone((int(*)(void*))fn, p+fforkstacksize-16, cloneflag, arg);
	if(pid < 0)
		free(p);
	return pid;
}

int
getfforkid(void)
{
	return getpid();
}