1 /* $NetBSD: h_execthr.c,v 1.3 2014/08/13 00:03:00 pooka Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
42 #include <rump/rumpclient.h>
43 #include <rump/rump_syscalls.h>
45 static int canreturn
= 0;
48 * Use a fairly large number of threads so that we have
49 * a better chance catching races. XXX: this is rumpuser's
62 int fd
= (uintptr_t)arg
;
64 rump_sys_read(fd
, &fd
, sizeof(fd
));
66 errx(1, "should not have returned");
68 errx(1, "got invalid magic");
74 getproc(pid_t mypid
, struct kinfo_proc2
*p
)
77 size_t len
= sizeof(*p
);
81 name
[2] = KERN_PROC_PID
;
86 return rump_sys___sysctl(name
, __arraycount(name
), p
, &len
, NULL
, 0);
90 main(int argc
, char *argv
[], char *envp
[])
102 execd
= atoi(argv
[1]);
105 sprintf(nexec
, "%d", execd
+1);
107 if (rumpclient_init() == -1) {
109 err(1, "init execd");
113 mypid
= rump_sys_getpid();
117 if (pthread_create(&pt
, NULL
,
118 wrk
, (void *)(uintptr_t)P2_0
) != 0)
119 errx(1, "exec pthread_create");
122 rump_sys_write(P2_1
, &i
, sizeof(i
));
123 pthread_join(pt
, NULL
);
125 n
= rump_sys_read(P1_0
, &i
, sizeof(i
));
126 if (n
!= -1 || errno
!= EBADF
)
127 errx(1, "post-exec cloexec works");
131 errx(1, "invalid nlwps: %lld", (long long)p
.p_nlwps
);
137 rump_sys_close(P2_0
);
138 rump_sys_close(P2_1
);
141 if (rump_sys_pipe(p1
) == -1)
143 if (p1
[0] != P1_0
|| p1
[1] != P1_1
)
144 errx(1, "p1 assumptions failed %d %d", p1
[0], p1
[1]);
145 if (rump_sys_pipe(p2
) == -1)
147 if (p2
[0] != P2_0
|| p2
[1] != P2_1
)
148 errx(1, "p2 assumptions failed");
149 if (rump_sys_fcntl(p1
[0], F_SETFD
, FD_CLOEXEC
) == -1)
151 if (rump_sys_fcntl(p1
[1], F_SETFD
, FD_CLOEXEC
) == -1)
154 for (i
= 0; i
< NTHR
; i
++)
155 if (pthread_create(&pt
, NULL
,
156 wrk
, (void *)(uintptr_t)p1
[0]) != 0)
157 errx(1, "pthread_create 1 %d", i
);
159 for (i
= 0; i
< NTHR
; i
++)
160 if (pthread_create(&pt
, NULL
,
161 wrk
, (void *)(uintptr_t)p2
[0]) != 0)
162 errx(1, "pthread_create 2 %d", i
);
164 /* wait for all the threads to be enjoying themselves */
167 if (p
.p_nlwps
== 2*NTHR
+ 2)
173 * load up one more (big) set. these won't start executing, though,
174 * but we're interested in if they create blockage
176 for (i
= 0; i
< 3*NTHR
; i
++)
177 if (pthread_create(&pt
, NULL
,
178 wrk
, (void *)(uintptr_t)p1
[0]) != 0)
179 errx(1, "pthread_create 1 %d", i
);
182 execarg
[0] = argv
[0];
185 if (rumpclient_exec(argv
[0], execarg
, envp
) == -1)