Home | History | Annotate | Line # | Download | only in librumpclient
h_execthr.c revision 1.5
      1 /*	$NetBSD: h_execthr.c,v 1.5 2016/11/21 06:19:26 dholland 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 #define P1_0 3
     55 #define P1_1 4
     56 #define P2_0 5
     57 #define P2_1 6
     58 
     59 static void *
     60 wrk(void *arg)
     61 {
     62 	int fd = (uintptr_t)arg;
     63 
     64 	rump_sys_read(fd, &fd, sizeof(fd));
     65 	if (!canreturn)
     66 		errx(1, "should not have returned");
     67 	if (fd != 37)
     68 		errx(1, "got invalid magic");
     69 
     70 	return NULL;
     71 }
     72 
     73 static int
     74 getproc(pid_t mypid, struct kinfo_proc2 *p)
     75 {
     76 	int name[6];
     77 	size_t len = sizeof(*p);
     78 
     79 	name[0] = CTL_KERN;
     80 	name[1] = KERN_PROC2;
     81 	name[2] = KERN_PROC_PID;
     82 	name[3] = mypid;
     83 	name[4] = len;
     84 	name[5] = 1;
     85 
     86 	return rump_sys___sysctl(name, __arraycount(name), p, &len, NULL, 0);
     87 }
     88 
     89 int
     90 main(int argc, char *argv[], char *envp[])
     91 {
     92 	struct kinfo_proc2 p;
     93 	char *execarg[3];
     94 	int p1[2], p2[2];
     95 	pid_t mypid;
     96 	pthread_t pt;
     97 	ssize_t n;
     98 	int i, execd;
     99 	char nexec[16];
    100 
    101 	if (argc > 1)
    102 		execd = atoi(argv[1]);
    103 	else
    104 		execd = 0;
    105 	sprintf(nexec, "%d", execd+1);
    106 
    107 	if (rumpclient_init() == -1) {
    108 		if (execd)
    109 			err(1, "init execd");
    110 		else
    111 			err(1, "init");
    112 	}
    113 	mypid = rump_sys_getpid();
    114 
    115 	if (execd) {
    116 		canreturn = 1;
    117 		errno = pthread_create(&pt, NULL,
    118 		    wrk, (void *)(uintptr_t)P2_0);
    119 		if (errno != 0)
    120 			err(1, "exec pthread_create");
    121 
    122 		i = 37;
    123 		rump_sys_write(P2_1, &i, sizeof(i));
    124 		pthread_join(pt, NULL);
    125 
    126 		n = rump_sys_read(P1_0, &i, sizeof(i));
    127 		if (n != -1 || errno != EBADF)
    128 			errx(1, "post-exec cloexec works");
    129 
    130 		getproc(mypid, &p);
    131 		if (p.p_nlwps != 2)
    132 			errx(1, "invalid nlwps: %lld", (long long)p.p_nlwps);
    133 
    134 		/* we passed? */
    135 		if (execd > 10)
    136 			exit(0);
    137 
    138 		rump_sys_close(P2_0);
    139 		rump_sys_close(P2_1);
    140 	}
    141 
    142 	if (rump_sys_pipe(p1) == -1)
    143 		err(1, "pipe1");
    144 	if (p1[0] != P1_0 || p1[1] != P1_1)
    145 		errx(1, "p1 assumptions failed %d %d", p1[0], p1[1]);
    146 	if (rump_sys_pipe(p2) == -1)
    147 		err(1, "pipe1");
    148 	if (p2[0] != P2_0 || p2[1] != P2_1)
    149 		errx(1, "p2 assumptions failed");
    150 	if (rump_sys_fcntl(p1[0], F_SETFD, FD_CLOEXEC) == -1)
    151 		err(1, "cloexec");
    152 	if (rump_sys_fcntl(p1[1], F_SETFD, FD_CLOEXEC) == -1)
    153 		err(1, "cloexec");
    154 
    155 	for (i = 0; i < NTHR; i++) {
    156 		errno = pthread_create(&pt, NULL,
    157 		    wrk, (void *)(uintptr_t)p1[0]);
    158 		if (errno != 0)
    159 			err(1, "pthread_create 1 %d", i);
    160 	}
    161 
    162 	for (i = 0; i < NTHR; i++) {
    163 		errno = pthread_create(&pt, NULL,
    164 		    wrk, (void *)(uintptr_t)p2[0]);
    165 		if (errno != 0)
    166 			err(1, "pthread_create 2 %d", i);
    167 	}
    168 
    169 	/* wait for all the threads to be enjoying themselves */
    170 	for (;;) {
    171 		getproc(mypid, &p);
    172 		if (p.p_nlwps == 2*NTHR + 2)
    173 			break;
    174 		usleep(10000);
    175 	}
    176 
    177 	/*
    178 	 * load up one more (big) set.  these won't start executing, though,
    179 	 * but we're interested in if they create blockage
    180 	 */
    181 	for (i = 0; i < 3*NTHR; i++) {
    182 		errno = pthread_create(&pt, NULL,
    183 		    wrk, (void *)(uintptr_t)p1[0]);
    184 		if (errno != 0)
    185 			err(1, "pthread_create 3 %d", i);
    186 	}
    187 
    188 	/* then, we exec! */
    189 	execarg[0] = argv[0];
    190 	execarg[1] = nexec;
    191 	execarg[2] = NULL;
    192 	if (rumpclient_exec(argv[0], execarg, envp) == -1)
    193 		err(1, "exec");
    194 }
    195