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