xref: /minix3/tests/rump/rumpkern/h_client/h_reconcli.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: h_reconcli.c,v 1.2 2011/02/19 09:56:45 pooka Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/sysctl.h>
5*11be35a1SLionel Sambuc 
6*11be35a1SLionel Sambuc #include <rump/rumpclient.h>
7*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
8*11be35a1SLionel Sambuc 
9*11be35a1SLionel Sambuc #include <err.h>
10*11be35a1SLionel Sambuc #include <pthread.h>
11*11be35a1SLionel Sambuc #include <stdio.h>
12*11be35a1SLionel Sambuc #include <stdlib.h>
13*11be35a1SLionel Sambuc #include <string.h>
14*11be35a1SLionel Sambuc #include <unistd.h>
15*11be35a1SLionel Sambuc 
16*11be35a1SLionel Sambuc static volatile int quit, riseandwhine;
17*11be35a1SLionel Sambuc 
18*11be35a1SLionel Sambuc static pthread_mutex_t closermtx;
19*11be35a1SLionel Sambuc static pthread_cond_t closercv;
20*11be35a1SLionel Sambuc 
21*11be35a1SLionel Sambuc static void *
closer(void * arg)22*11be35a1SLionel Sambuc closer(void *arg)
23*11be35a1SLionel Sambuc {
24*11be35a1SLionel Sambuc 
25*11be35a1SLionel Sambuc 	pthread_mutex_lock(&closermtx);
26*11be35a1SLionel Sambuc 	while (!quit) {
27*11be35a1SLionel Sambuc 		while (!riseandwhine)
28*11be35a1SLionel Sambuc 			pthread_cond_wait(&closercv, &closermtx);
29*11be35a1SLionel Sambuc 		riseandwhine = 0;
30*11be35a1SLionel Sambuc 		pthread_mutex_unlock(&closermtx);
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc 		/* try to catch a random slot */
33*11be35a1SLionel Sambuc 		usleep(random() % 100000);
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc 		/*
36*11be35a1SLionel Sambuc 		 * wide-angle disintegration beam, but takes care
37*11be35a1SLionel Sambuc 		 * of the client rumpkernel communication socket.
38*11be35a1SLionel Sambuc 		 */
39*11be35a1SLionel Sambuc 		closefrom(3);
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc 		pthread_mutex_lock(&closermtx);
42*11be35a1SLionel Sambuc 	}
43*11be35a1SLionel Sambuc 	pthread_mutex_unlock(&closermtx);
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc 	return NULL;
46*11be35a1SLionel Sambuc }
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc static const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME };
49*11be35a1SLionel Sambuc static char goodhostname[128];
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc static void *
worker(void * arg)52*11be35a1SLionel Sambuc worker(void *arg)
53*11be35a1SLionel Sambuc {
54*11be35a1SLionel Sambuc 	char hostnamebuf[128];
55*11be35a1SLionel Sambuc 	size_t blen;
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc 	pthread_mutex_lock(&closermtx);
58*11be35a1SLionel Sambuc 	while (!quit) {
59*11be35a1SLionel Sambuc 		pthread_mutex_unlock(&closermtx);
60*11be35a1SLionel Sambuc 		if (rump_sys_getpid() == -1)
61*11be35a1SLionel Sambuc 			err(1, "getpid");
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 		blen = sizeof(hostnamebuf);
64*11be35a1SLionel Sambuc 		memset(hostnamebuf, 0, sizeof(hostnamebuf));
65*11be35a1SLionel Sambuc 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
66*11be35a1SLionel Sambuc 		    hostnamebuf, &blen, NULL, 0) == -1)
67*11be35a1SLionel Sambuc 			err(1, "sysctl");
68*11be35a1SLionel Sambuc 		if (strcmp(hostnamebuf, goodhostname) != 0)
69*11be35a1SLionel Sambuc 			exit(1);
70*11be35a1SLionel Sambuc 		pthread_mutex_lock(&closermtx);
71*11be35a1SLionel Sambuc 		riseandwhine = 1;
72*11be35a1SLionel Sambuc 		pthread_cond_signal(&closercv);
73*11be35a1SLionel Sambuc 	}
74*11be35a1SLionel Sambuc 	riseandwhine = 1;
75*11be35a1SLionel Sambuc 	pthread_cond_signal(&closercv);
76*11be35a1SLionel Sambuc 	pthread_mutex_unlock(&closermtx);
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc 	return NULL;
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc int
main(int argc,char * argv[])82*11be35a1SLionel Sambuc main(int argc, char *argv[])
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc 	pthread_t pt, w1, w2, w3, w4;
85*11be35a1SLionel Sambuc 	size_t blen;
86*11be35a1SLionel Sambuc 	int timecount;
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 	if (argc != 2)
89*11be35a1SLionel Sambuc 		errx(1, "need timecount");
90*11be35a1SLionel Sambuc 	timecount = atoi(argv[1]);
91*11be35a1SLionel Sambuc 	if (timecount <= 0)
92*11be35a1SLionel Sambuc 		errx(1, "invalid timecount %d\n", timecount);
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 	srandom(time(NULL));
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc 	rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
97*11be35a1SLionel Sambuc 	if (rumpclient_init() == -1)
98*11be35a1SLionel Sambuc 		err(1, "init");
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc 	blen = sizeof(goodhostname);
101*11be35a1SLionel Sambuc 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
102*11be35a1SLionel Sambuc 	    goodhostname, &blen, NULL, 0) == -1)
103*11be35a1SLionel Sambuc 		err(1, "sysctl");
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 	pthread_create(&pt, NULL, closer, NULL);
106*11be35a1SLionel Sambuc 	pthread_create(&w1, NULL, worker, NULL);
107*11be35a1SLionel Sambuc 	pthread_create(&w2, NULL, worker, NULL);
108*11be35a1SLionel Sambuc 	pthread_create(&w3, NULL, worker, NULL);
109*11be35a1SLionel Sambuc 	pthread_create(&w4, NULL, worker, NULL);
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 	sleep(timecount);
112*11be35a1SLionel Sambuc 	quit = 1;
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	pthread_join(pt, NULL);
115*11be35a1SLionel Sambuc 	pthread_join(w1, NULL);
116*11be35a1SLionel Sambuc 	pthread_join(w2, NULL);
117*11be35a1SLionel Sambuc 	pthread_join(w3, NULL);
118*11be35a1SLionel Sambuc 	pthread_join(w4, NULL);
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 	exit(0);
121*11be35a1SLionel Sambuc }
122