xref: /minix3/tests/rump/rumpkern/h_client/h_sigcli.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: h_sigcli.c,v 1.3 2011/02/07 20:05:09 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 <err.h>
7*11be35a1SLionel Sambuc #include <stdlib.h>
8*11be35a1SLionel Sambuc #include <stdio.h>
9*11be35a1SLionel Sambuc #include <string.h>
10*11be35a1SLionel Sambuc #include <unistd.h>
11*11be35a1SLionel Sambuc 
12*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
13*11be35a1SLionel Sambuc #include <rump/rumpclient.h>
14*11be35a1SLionel Sambuc 
15*11be35a1SLionel Sambuc static const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME };
16*11be35a1SLionel Sambuc static char hostnamebuf[128];
17*11be35a1SLionel Sambuc 
18*11be35a1SLionel Sambuc static volatile sig_atomic_t sigexecs;
19*11be35a1SLionel Sambuc 
20*11be35a1SLionel Sambuc static void
sighand(int sig)21*11be35a1SLionel Sambuc sighand(int sig)
22*11be35a1SLionel Sambuc {
23*11be35a1SLionel Sambuc 	char buf[128];
24*11be35a1SLionel Sambuc 	size_t blen = sizeof(buf);
25*11be35a1SLionel Sambuc 
26*11be35a1SLionel Sambuc 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
27*11be35a1SLionel Sambuc 	    buf, &blen, NULL, 0) == -1)
28*11be35a1SLionel Sambuc 		err(1, "sighand sysctl");
29*11be35a1SLionel Sambuc 	if (strcmp(buf, hostnamebuf) != 0)
30*11be35a1SLionel Sambuc 		errx(1, "sighandler hostname");
31*11be35a1SLionel Sambuc 	sigexecs++;
32*11be35a1SLionel Sambuc }
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc int
main(void)35*11be35a1SLionel Sambuc main(void)
36*11be35a1SLionel Sambuc {
37*11be35a1SLionel Sambuc 	char buf[128];
38*11be35a1SLionel Sambuc 	time_t tstart;
39*11be35a1SLionel Sambuc 	struct itimerval itv;
40*11be35a1SLionel Sambuc 	size_t hnbsize;
41*11be35a1SLionel Sambuc 	int i;
42*11be35a1SLionel Sambuc 	size_t blen;
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc 	if (rumpclient_init() == -1)
45*11be35a1SLionel Sambuc 		err(1, "rumpclient init");
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc 	hnbsize = sizeof(hostnamebuf);
48*11be35a1SLionel Sambuc 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
49*11be35a1SLionel Sambuc 	    hostnamebuf, &hnbsize, NULL, 0) == -1)
50*11be35a1SLionel Sambuc 		err(1, "sysctl");
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 	if (signal(SIGALRM, sighand) == SIG_ERR)
53*11be35a1SLionel Sambuc 		err(1, "signal");
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 	itv.it_interval.tv_sec = itv.it_value.tv_sec = 0;
56*11be35a1SLionel Sambuc 	itv.it_interval.tv_usec = itv.it_value.tv_usec = 10000; /* 10ms */
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
59*11be35a1SLionel Sambuc 		err(1, "itimer");
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc 	tstart = time(NULL);
62*11be35a1SLionel Sambuc 	for (i = 0;; i++) {
63*11be35a1SLionel Sambuc 		blen = sizeof(buf);
64*11be35a1SLionel Sambuc 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
65*11be35a1SLionel Sambuc 		    buf, &blen, NULL, 0) == -1)
66*11be35a1SLionel Sambuc 			err(1, "sysctl");
67*11be35a1SLionel Sambuc 		if (strcmp(buf, hostnamebuf) != 0)
68*11be35a1SLionel Sambuc 			errx(1, "main hostname");
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc 		/*
71*11be35a1SLionel Sambuc 		 * check every 100 cycles to avoid doing
72*11be35a1SLionel Sambuc 		 * nothing but gettimeofday()
73*11be35a1SLionel Sambuc 		 */
74*11be35a1SLionel Sambuc 		if (i == 100) {
75*11be35a1SLionel Sambuc 			if (time(NULL) - tstart > 5)
76*11be35a1SLionel Sambuc 				break;
77*11be35a1SLionel Sambuc 			i = 0;
78*11be35a1SLionel Sambuc 		}
79*11be35a1SLionel Sambuc 	}
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc 	if (!sigexecs) {
82*11be35a1SLionel Sambuc 		printf("no signal handlers run.  test busted?\n");
83*11be35a1SLionel Sambuc 	}
84*11be35a1SLionel Sambuc }
85