aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/execproc.ch
blob: 4382be402347886592df25c8aafa044ed52a2f42 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Set up a dedicated proc to handle calls to exec.
 * The proc also waits for child messages.  
 * This way, each proc scheduler need not worry
 * about calling wait in its main loop.
 * 
 * To be included from other files (e.g., Linux-clone.c).
 */

typedef struct Xarg Xarg;
struct Xarg
{
	Channel *pidc;
	int fd[3];
	char *prog;
	char **args;
	int freeargs;
	Channel *ret;
	int errn;
	char errstr[ERRMAX];
};

static Proc *_threadexecproc;
static Channel *_threadexecchan;
static Lock threadexeclock;

/*
 * Called to poll for any kids of this pthread.
 * We have a separate proc responsible for exec,
 * so this is a no-op.
 */
void
_threadwaitkids(Proc *p)
{
}

#define WAITSIG SIGCHLD

/*
 * Separate process to wait for child messages.
 * Also runs signal handlers and runs all execs.
 */
static void
nop(int sig)
{
	USED(sig);
}

static void
_threadwaitproc(void *v)
{
	Channel *c;
	Waitmsg *w;
	sigset_t mask;
	int ret, nkids;
	Xarg *xa;

	nkids = 0;

	sigemptyset(&mask);
	siginterrupt(WAITSIG, 1);
	signal(WAITSIG, nop);
	sigaddset(&mask, WAITSIG);
	sigprocmask(SIG_BLOCK, &mask, nil);
	USED(v);
	for(;;){
		while((nkids > 0 ? nbrecv : recv)(_threadexecchan, &xa) == 1){
			ret = _threadexec(xa->pidc, xa->fd, xa->prog, xa->args, xa->freeargs);
			if(ret > 0)
				nkids++;
			else{
				rerrstr(xa->errstr, sizeof xa->errstr);
				xa->errn = errno;
			}
			sendul(xa->ret, ret);
		}
		if(nkids > 0){
			sigprocmask(SIG_UNBLOCK, &mask, nil);
			w = wait();
			sigprocmask(SIG_BLOCK, &mask, nil);
			if(w == nil && errno == ECHILD){
				fprint(2, "wait returned ECHILD but nkids=%d; reset\n", nkids);
				nkids = 0;
			}
			if(w){
				nkids--;
				if((c = _threadwaitchan) != nil)
					sendp(c, w);
				else
					free(w);
			}
		}
	}
}

static void _kickexecproc(void);

int
_callthreadexec(Channel *pidc, int fd[3], char *prog, char *args[], int freeargs)
{
	int ret;
	Xarg xa;

	if(_threadexecchan == nil){
		lock(&threadexeclock);
		if(_threadexecchan == nil)
			_threadfirstexec();
		unlock(&threadexeclock);
	}

	xa.pidc = pidc;
	xa.fd[0] = fd[0];
	xa.fd[1] = fd[1];
	xa.fd[2] = fd[2];
	xa.prog = prog;
	xa.args = args;
	xa.freeargs = freeargs;
	xa.ret = chancreate(sizeof(ulong), 1);
	sendp(_threadexecchan, &xa);
	_kickexecproc();
	ret = recvul(xa.ret);
	if(ret < 0){
		werrstr("%s", xa.errstr);
		errno = xa.errn;
	}
	chanfree(xa.ret);
	return ret;
}

/* 
 * Called before the first exec.
 */
void
_threadfirstexec(void)
{
	int id;
	Proc *p;

	_threadexecchan = chancreate(sizeof(Xarg*), 1);
	id = proccreate(_threadwaitproc, nil, 32*1024);

	/*
	 * Sleazy: decrement threadnprocs so that 
	 * the existence of the _threadwaitproc proc
	 * doesn't keep us from exiting.
	 */
	lock(&_threadpq.lock);
	--_threadnprocs;
	for(p=_threadpq.head; p; p=p->next)
		if(p->threads.head && p->threads.head->id == id)
			break;
	if(p == nil)
		sysfatal("cannot find exec proc");
	unlock(&_threadpq.lock);
	_threadexecproc = p;
}

/*
 * Called after the thread t has been rescheduled.
 * Kick the exec proc in case it is in the middle of a wait.
 */
static void
_kickexecproc(void)
{
	kill(_threadexecproc->pid, WAITSIG);
}

/*
 * Called before exec.
 */
void
_threadbeforeexec(void)
{
}

/*
 * Called after exec.
 */
void
_threadafterexec(void)
{
}