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