xref: /netbsd-src/tests/rump/rumpkern/h_client/h_reconcli.c (revision 362dcd536621815f39482e4e00dd42c48fcc945b)
1*362dcd53Spooka /*	$NetBSD: h_reconcli.c,v 1.2 2011/02/19 09:56:45 pooka Exp $	*/
27605725bSpooka 
37605725bSpooka #include <sys/types.h>
47605725bSpooka #include <sys/sysctl.h>
57605725bSpooka 
67605725bSpooka #include <rump/rumpclient.h>
77605725bSpooka #include <rump/rump_syscalls.h>
87605725bSpooka 
97605725bSpooka #include <err.h>
107605725bSpooka #include <pthread.h>
117605725bSpooka #include <stdio.h>
127605725bSpooka #include <stdlib.h>
137605725bSpooka #include <string.h>
147605725bSpooka #include <unistd.h>
157605725bSpooka 
167605725bSpooka static volatile int quit, riseandwhine;
177605725bSpooka 
187605725bSpooka static pthread_mutex_t closermtx;
197605725bSpooka static pthread_cond_t closercv;
207605725bSpooka 
217605725bSpooka static void *
closer(void * arg)227605725bSpooka closer(void *arg)
237605725bSpooka {
247605725bSpooka 
257605725bSpooka 	pthread_mutex_lock(&closermtx);
267605725bSpooka 	while (!quit) {
277605725bSpooka 		while (!riseandwhine)
287605725bSpooka 			pthread_cond_wait(&closercv, &closermtx);
297605725bSpooka 		riseandwhine = 0;
307605725bSpooka 		pthread_mutex_unlock(&closermtx);
317605725bSpooka 
327605725bSpooka 		/* try to catch a random slot */
337605725bSpooka 		usleep(random() % 100000);
347605725bSpooka 
357605725bSpooka 		/*
367605725bSpooka 		 * wide-angle disintegration beam, but takes care
377605725bSpooka 		 * of the client rumpkernel communication socket.
387605725bSpooka 		 */
397605725bSpooka 		closefrom(3);
407605725bSpooka 
417605725bSpooka 		pthread_mutex_lock(&closermtx);
427605725bSpooka 	}
437605725bSpooka 	pthread_mutex_unlock(&closermtx);
447605725bSpooka 
457605725bSpooka 	return NULL;
467605725bSpooka }
477605725bSpooka 
487605725bSpooka static const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME };
497605725bSpooka static char goodhostname[128];
507605725bSpooka 
517605725bSpooka static void *
worker(void * arg)527605725bSpooka worker(void *arg)
537605725bSpooka {
547605725bSpooka 	char hostnamebuf[128];
557605725bSpooka 	size_t blen;
567605725bSpooka 
577605725bSpooka 	pthread_mutex_lock(&closermtx);
587605725bSpooka 	while (!quit) {
597605725bSpooka 		pthread_mutex_unlock(&closermtx);
607605725bSpooka 		if (rump_sys_getpid() == -1)
617605725bSpooka 			err(1, "getpid");
627605725bSpooka 
637605725bSpooka 		blen = sizeof(hostnamebuf);
647605725bSpooka 		memset(hostnamebuf, 0, sizeof(hostnamebuf));
657605725bSpooka 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
667605725bSpooka 		    hostnamebuf, &blen, NULL, 0) == -1)
677605725bSpooka 			err(1, "sysctl");
687605725bSpooka 		if (strcmp(hostnamebuf, goodhostname) != 0)
697605725bSpooka 			exit(1);
707605725bSpooka 		pthread_mutex_lock(&closermtx);
717605725bSpooka 		riseandwhine = 1;
727605725bSpooka 		pthread_cond_signal(&closercv);
737605725bSpooka 	}
747605725bSpooka 	riseandwhine = 1;
757605725bSpooka 	pthread_cond_signal(&closercv);
767605725bSpooka 	pthread_mutex_unlock(&closermtx);
777605725bSpooka 
787605725bSpooka 	return NULL;
797605725bSpooka }
807605725bSpooka 
817605725bSpooka int
main(int argc,char * argv[])827605725bSpooka main(int argc, char *argv[])
837605725bSpooka {
847605725bSpooka 	pthread_t pt, w1, w2, w3, w4;
857605725bSpooka 	size_t blen;
867605725bSpooka 	int timecount;
877605725bSpooka 
887605725bSpooka 	if (argc != 2)
897605725bSpooka 		errx(1, "need timecount");
907605725bSpooka 	timecount = atoi(argv[1]);
917605725bSpooka 	if (timecount <= 0)
927605725bSpooka 		errx(1, "invalid timecount %d\n", timecount);
937605725bSpooka 
947605725bSpooka 	srandom(time(NULL));
957605725bSpooka 
96*362dcd53Spooka 	rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
977605725bSpooka 	if (rumpclient_init() == -1)
987605725bSpooka 		err(1, "init");
997605725bSpooka 
1007605725bSpooka 	blen = sizeof(goodhostname);
1017605725bSpooka 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
1027605725bSpooka 	    goodhostname, &blen, NULL, 0) == -1)
1037605725bSpooka 		err(1, "sysctl");
1047605725bSpooka 
1057605725bSpooka 	pthread_create(&pt, NULL, closer, NULL);
1067605725bSpooka 	pthread_create(&w1, NULL, worker, NULL);
1077605725bSpooka 	pthread_create(&w2, NULL, worker, NULL);
1087605725bSpooka 	pthread_create(&w3, NULL, worker, NULL);
1097605725bSpooka 	pthread_create(&w4, NULL, worker, NULL);
1107605725bSpooka 
1117605725bSpooka 	sleep(timecount);
1127605725bSpooka 	quit = 1;
1137605725bSpooka 
1147605725bSpooka 	pthread_join(pt, NULL);
1157605725bSpooka 	pthread_join(w1, NULL);
1167605725bSpooka 	pthread_join(w2, NULL);
1177605725bSpooka 	pthread_join(w3, NULL);
1187605725bSpooka 	pthread_join(w4, NULL);
1197605725bSpooka 
1207605725bSpooka 	exit(0);
1217605725bSpooka }
122