xref: /netbsd-src/tests/lib/librumpclient/h_execthr.c (revision 46dfa511bc3cc294a0ee8149735e7b1320156d1f)
1 /*	$NetBSD: h_execthr.c,v 1.1 2011/03/08 12:40:25 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <rump/rumpclient.h>
43 #include <rump/rump_syscalls.h>
44 
45 static int canreturn = 0;
46 
47 /*
48  * Use a fairly large number of threads so that we have
49  * a better chance catching races.  XXX: this is rumpuser's
50  * MAXWORKER-1.
51  */
52 #define NTHR 63
53 
54 static void *
55 wrk(void *arg)
56 {
57 	int fd = (uintptr_t)arg;
58 
59 	rump_sys_read(fd, &fd, sizeof(fd));
60 	if (!canreturn)
61 		errx(1, "should not have returned");
62 	if (fd != 37)
63 		errx(1, "got invalid magic");
64 
65 	return NULL;
66 }
67 
68 static int
69 getproc(pid_t mypid, struct kinfo_proc2 *p)
70 {
71 	int name[6];
72 	size_t len = sizeof(*p);
73 
74 	name[0] = CTL_KERN;
75 	name[1] = KERN_PROC2;
76 	name[2] = KERN_PROC_PID;
77 	name[3] = mypid;
78 	name[4] = len;
79 	name[5] = 1;
80 
81 	return rump_sys___sysctl(name, __arraycount(name), p, &len, NULL, 0);
82 }
83 
84 int
85 main(int argc, char *argv[], char *envp[])
86 {
87 	struct kinfo_proc2 p;
88 	char *execarg[3];
89 	int p1[2], p2[2];
90 	pid_t mypid;
91 	pthread_t pt;
92 	ssize_t n;
93 	int i, execd;
94 	char nexec[16];
95 
96 	if (argc > 1)
97 		execd = atoi(argv[1]);
98 	else
99 		execd = 0;
100 	sprintf(nexec, "%d", execd+1);
101 
102 	if (rumpclient_init() == -1) {
103 		if (execd)
104 			err(1, "init execd");
105 		else
106 			err(1, "init");
107 	}
108 	mypid = rump_sys_getpid();
109 
110 	if (execd) {
111 		canreturn = 1;
112 		if (pthread_create(&pt, NULL,
113 		    wrk, (void *)(uintptr_t)2) != 0)
114 			errx(1, "exec pthread_create");
115 
116 		i = 37;
117 		rump_sys_write(3, &i, sizeof(i));
118 		pthread_join(pt, NULL);
119 
120 		n = rump_sys_read(0, &i, sizeof(i));
121 		if (n != -1 || errno != EBADF)
122 			errx(1, "post-exec cloexec works");
123 
124 		getproc(mypid, &p);
125 		if (p.p_nlwps != 2)
126 			errx(1, "invalid nlwps: %lld", (long long)p.p_nlwps);
127 
128 		/* we passed? */
129 		if (execd > 10)
130 			exit(0);
131 
132 		rump_sys_close(2);
133 		rump_sys_close(3);
134 	}
135 
136 	if (rump_sys_pipe(p1) == -1)
137 		err(1, "pipe1");
138 	if (p1[0] != 0 || p1[1] != 1)
139 		errx(1, "p1 assumptions failed %d %d", p1[0], p1[1]);
140 	if (rump_sys_pipe(p2) == -1)
141 		err(1, "pipe1");
142 	if (p2[0] != 2 || p2[1] != 3)
143 		errx(1, "p2 assumptions failed");
144 	if (rump_sys_fcntl(p1[0], F_SETFD, FD_CLOEXEC) == -1)
145 		err(1, "cloexec");
146 	if (rump_sys_fcntl(p1[1], F_SETFD, FD_CLOEXEC) == -1)
147 		err(1, "cloexec");
148 
149 	for (i = 0; i < NTHR; i++)
150 		if (pthread_create(&pt, NULL,
151 		    wrk, (void *)(uintptr_t)p1[0]) != 0)
152 			errx(1, "pthread_create 1 %d", i);
153 
154 	for (i = 0; i < NTHR; i++)
155 		if (pthread_create(&pt, NULL,
156 		    wrk, (void *)(uintptr_t)p2[0]) != 0)
157 			errx(1, "pthread_create 2 %d", i);
158 
159 	/* wait for all the threads to be enjoying themselves */
160 	for (;;) {
161 		getproc(mypid, &p);
162 		if (p.p_nlwps == 2*NTHR + 2)
163 			break;
164 		usleep(10000);
165 	}
166 
167 #ifdef notyet
168 	/*
169 	 * load up one more (big) set.  these won't start executing, though,
170 	 * but we're interested in if they create blockage
171 	 */
172 	for (i = 0; i < 3*NTHR; i++)
173 		if (pthread_create(&pt, NULL,
174 		    wrk, (void *)(uintptr_t)p1[0]) != 0)
175 			errx(1, "pthread_create 1 %d", i);
176 #endif
177 
178 	/* then, we exec! */
179 	execarg[0] = argv[0];
180 	execarg[1] = nexec;
181 	execarg[2] = NULL;
182 	if (rumpclient_exec(argv[0], execarg, envp) == -1)
183 		err(1, "exec");
184 }
185