xref: /netbsd-src/tests/lib/librumpclient/h_execthr.c (revision c0590f9e77ae554e0e8cdf335a8f8a11193b3c3f)
1*c0590f9eSdholland /*	$NetBSD: h_execthr.c,v 1.7 2016/11/24 00:37:29 dholland Exp $	*/
246dfa511Spooka 
346dfa511Spooka /*
446dfa511Spooka  * Copyright (c) 2011 The NetBSD Foundation, Inc.
546dfa511Spooka  * All rights reserved.
646dfa511Spooka  *
746dfa511Spooka  * Redistribution and use in source and binary forms, with or without
846dfa511Spooka  * modification, are permitted provided that the following conditions
946dfa511Spooka  * are met:
1046dfa511Spooka  * 1. Redistributions of source code must retain the above copyright
1146dfa511Spooka  *    notice, this list of conditions and the following disclaimer.
1246dfa511Spooka  * 2. Redistributions in binary form must reproduce the above copyright
1346dfa511Spooka  *    notice, this list of conditions and the following disclaimer in the
1446dfa511Spooka  *    documentation and/or other materials provided with the distribution.
1546dfa511Spooka  *
1646dfa511Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1746dfa511Spooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1846dfa511Spooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1946dfa511Spooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2046dfa511Spooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2146dfa511Spooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2246dfa511Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2346dfa511Spooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2446dfa511Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2546dfa511Spooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2646dfa511Spooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2746dfa511Spooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2846dfa511Spooka  */
2946dfa511Spooka 
3046dfa511Spooka #include <sys/types.h>
3146dfa511Spooka #include <sys/sysctl.h>
3246dfa511Spooka 
3346dfa511Spooka #include <err.h>
3446dfa511Spooka #include <errno.h>
3546dfa511Spooka #include <fcntl.h>
3646dfa511Spooka #include <pthread.h>
3746dfa511Spooka #include <stdio.h>
3846dfa511Spooka #include <stdlib.h>
3946dfa511Spooka #include <string.h>
4046dfa511Spooka #include <unistd.h>
4146dfa511Spooka 
4246dfa511Spooka #include <rump/rumpclient.h>
4346dfa511Spooka #include <rump/rump_syscalls.h>
4446dfa511Spooka 
45*c0590f9eSdholland //#define VERBOSE
4630d50936Sdholland 
4730d50936Sdholland #ifdef VERBOSE
4830d50936Sdholland #define SAY(...) printf(__VA_ARGS__)
4930d50936Sdholland #else
5030d50936Sdholland #define SAY(...)
5130d50936Sdholland #endif
5230d50936Sdholland 
5346dfa511Spooka static int canreturn = 0;
5446dfa511Spooka 
5546dfa511Spooka /*
5646dfa511Spooka  * Use a fairly large number of threads so that we have
5746dfa511Spooka  * a better chance catching races.  XXX: this is rumpuser's
5846dfa511Spooka  * MAXWORKER-1.
5946dfa511Spooka  */
6046dfa511Spooka #define NTHR 63
6146dfa511Spooka 
6248fac693Spooka #define P1_0 3
6348fac693Spooka #define P1_1 4
6448fac693Spooka #define P2_0 5
6548fac693Spooka #define P2_1 6
6648fac693Spooka 
6746dfa511Spooka static void *
wrk(void * arg)6846dfa511Spooka wrk(void *arg)
6946dfa511Spooka {
7046dfa511Spooka 	int fd = (uintptr_t)arg;
7146dfa511Spooka 
7246dfa511Spooka 	rump_sys_read(fd, &fd, sizeof(fd));
7346dfa511Spooka 	if (!canreturn)
7446dfa511Spooka 		errx(1, "should not have returned");
7546dfa511Spooka 	if (fd != 37)
7646dfa511Spooka 		errx(1, "got invalid magic");
7746dfa511Spooka 
7846dfa511Spooka 	return NULL;
7946dfa511Spooka }
8046dfa511Spooka 
8146dfa511Spooka static int
getproc(pid_t mypid,struct kinfo_proc2 * p)8246dfa511Spooka getproc(pid_t mypid, struct kinfo_proc2 *p)
8346dfa511Spooka {
8446dfa511Spooka 	int name[6];
8546dfa511Spooka 	size_t len = sizeof(*p);
8646dfa511Spooka 
8746dfa511Spooka 	name[0] = CTL_KERN;
8846dfa511Spooka 	name[1] = KERN_PROC2;
8946dfa511Spooka 	name[2] = KERN_PROC_PID;
9046dfa511Spooka 	name[3] = mypid;
9146dfa511Spooka 	name[4] = len;
9246dfa511Spooka 	name[5] = 1;
9346dfa511Spooka 
9446dfa511Spooka 	return rump_sys___sysctl(name, __arraycount(name), p, &len, NULL, 0);
9546dfa511Spooka }
9646dfa511Spooka 
9746dfa511Spooka int
main(int argc,char * argv[],char * envp[])9846dfa511Spooka main(int argc, char *argv[], char *envp[])
9946dfa511Spooka {
10046dfa511Spooka 	struct kinfo_proc2 p;
10146dfa511Spooka 	char *execarg[3];
10246dfa511Spooka 	int p1[2], p2[2];
10346dfa511Spooka 	pid_t mypid;
10446dfa511Spooka 	pthread_t pt;
10546dfa511Spooka 	ssize_t n;
10646dfa511Spooka 	int i, execd;
10746dfa511Spooka 	char nexec[16];
10846dfa511Spooka 
10946dfa511Spooka 	if (argc > 1)
11046dfa511Spooka 		execd = atoi(argv[1]);
11146dfa511Spooka 	else
11246dfa511Spooka 		execd = 0;
11346dfa511Spooka 	sprintf(nexec, "%d", execd+1);
11430d50936Sdholland 	SAY("execd: %d\n", execd);
11546dfa511Spooka 
11646dfa511Spooka 	if (rumpclient_init() == -1) {
11746dfa511Spooka 		if (execd)
11846dfa511Spooka 			err(1, "init execd");
11946dfa511Spooka 		else
12046dfa511Spooka 			err(1, "init");
12146dfa511Spooka 	}
12246dfa511Spooka 	mypid = rump_sys_getpid();
12330d50936Sdholland 	SAY("rumpclient_init finished.\n");
12446dfa511Spooka 
12546dfa511Spooka 	if (execd) {
12646dfa511Spooka 		canreturn = 1;
12744f59790Sdholland 		errno = pthread_create(&pt, NULL,
12844f59790Sdholland 		    wrk, (void *)(uintptr_t)P2_0);
12944f59790Sdholland 		if (errno != 0)
13044f59790Sdholland 			err(1, "exec pthread_create");
13130d50936Sdholland 		SAY("startup pthread_create finished.\n");
13246dfa511Spooka 
13346dfa511Spooka 		i = 37;
13448fac693Spooka 		rump_sys_write(P2_1, &i, sizeof(i));
13546dfa511Spooka 		pthread_join(pt, NULL);
13630d50936Sdholland 		SAY("startup pthread_join finished.\n");
13746dfa511Spooka 
13848fac693Spooka 		n = rump_sys_read(P1_0, &i, sizeof(i));
13946dfa511Spooka 		if (n != -1 || errno != EBADF)
14046dfa511Spooka 			errx(1, "post-exec cloexec works");
14130d50936Sdholland 		SAY("startup rump_sys_read finished.\n");
14246dfa511Spooka 
14346dfa511Spooka 		getproc(mypid, &p);
14430d50936Sdholland 		SAY("startup getproc finished.\n");
14546dfa511Spooka 		if (p.p_nlwps != 2)
14646dfa511Spooka 			errx(1, "invalid nlwps: %lld", (long long)p.p_nlwps);
14746dfa511Spooka 
14846dfa511Spooka 		/* we passed? */
14930d50936Sdholland 		if (execd > 10) {
15030d50936Sdholland 			SAY("done.\n");
15146dfa511Spooka 			exit(0);
15230d50936Sdholland 		}
15346dfa511Spooka 
15448fac693Spooka 		rump_sys_close(P2_0);
15548fac693Spooka 		rump_sys_close(P2_1);
15646dfa511Spooka 	}
15746dfa511Spooka 
15830d50936Sdholland 	SAY("making pipes...\n");
15930d50936Sdholland 
16046dfa511Spooka 	if (rump_sys_pipe(p1) == -1)
16146dfa511Spooka 		err(1, "pipe1");
16248fac693Spooka 	if (p1[0] != P1_0 || p1[1] != P1_1)
16346dfa511Spooka 		errx(1, "p1 assumptions failed %d %d", p1[0], p1[1]);
16446dfa511Spooka 	if (rump_sys_pipe(p2) == -1)
16546dfa511Spooka 		err(1, "pipe1");
16648fac693Spooka 	if (p2[0] != P2_0 || p2[1] != P2_1)
16746dfa511Spooka 		errx(1, "p2 assumptions failed");
16846dfa511Spooka 	if (rump_sys_fcntl(p1[0], F_SETFD, FD_CLOEXEC) == -1)
16946dfa511Spooka 		err(1, "cloexec");
17046dfa511Spooka 	if (rump_sys_fcntl(p1[1], F_SETFD, FD_CLOEXEC) == -1)
17146dfa511Spooka 		err(1, "cloexec");
17246dfa511Spooka 
17330d50936Sdholland 	SAY("making threads...\n");
17430d50936Sdholland 
1755e5fae2dSdholland 	for (i = 0; i < NTHR; i++) {
1765e5fae2dSdholland 		errno = pthread_create(&pt, NULL,
1775e5fae2dSdholland 		    wrk, (void *)(uintptr_t)p1[0]);
1785e5fae2dSdholland 		if (errno != 0)
1795e5fae2dSdholland 			err(1, "pthread_create 1 %d", i);
1805e5fae2dSdholland 	}
18146dfa511Spooka 
1825e5fae2dSdholland 	for (i = 0; i < NTHR; i++) {
1835e5fae2dSdholland 		errno = pthread_create(&pt, NULL,
1845e5fae2dSdholland 		    wrk, (void *)(uintptr_t)p2[0]);
1855e5fae2dSdholland 		if (errno != 0)
1865e5fae2dSdholland 			err(1, "pthread_create 2 %d", i);
1875e5fae2dSdholland 	}
18846dfa511Spooka 
18930d50936Sdholland 	SAY("waiting for threads to start...\n");
19030d50936Sdholland 
19146dfa511Spooka 	/* wait for all the threads to be enjoying themselves */
19246dfa511Spooka 	for (;;) {
19346dfa511Spooka 		getproc(mypid, &p);
19430d50936Sdholland 		SAY("getproc finished.\n");
19546dfa511Spooka 		if (p.p_nlwps == 2*NTHR + 2)
19646dfa511Spooka 			break;
19746dfa511Spooka 		usleep(10000);
19846dfa511Spooka 	}
19946dfa511Spooka 
20030d50936Sdholland 	SAY("making some more threads start...\n");
20130d50936Sdholland 
20246dfa511Spooka 	/*
20346dfa511Spooka 	 * load up one more (big) set.  these won't start executing, though,
20446dfa511Spooka 	 * but we're interested in if they create blockage
20546dfa511Spooka 	 */
20644f59790Sdholland 	for (i = 0; i < 3*NTHR; i++) {
20744f59790Sdholland 		errno = pthread_create(&pt, NULL,
20844f59790Sdholland 		    wrk, (void *)(uintptr_t)p1[0]);
20944f59790Sdholland 		if (errno != 0)
21044f59790Sdholland 			err(1, "pthread_create 3 %d", i);
21144f59790Sdholland 	}
21246dfa511Spooka 
21330d50936Sdholland 	SAY("calling exec...\n");
21430d50936Sdholland 
21546dfa511Spooka 	/* then, we exec! */
21646dfa511Spooka 	execarg[0] = argv[0];
21746dfa511Spooka 	execarg[1] = nexec;
21846dfa511Spooka 	execarg[2] = NULL;
21946dfa511Spooka 	if (rumpclient_exec(argv[0], execarg, envp) == -1)
22046dfa511Spooka 		err(1, "exec");
22146dfa511Spooka }
222