xref: /netbsd/tests/rump/rumpkern/h_client/h_sigcli.c (revision 6550d01e)
1 /*	$NetBSD: h_sigcli.c,v 1.2 2011/01/10 19:30:21 pooka Exp $	*/
2 
3 #include <sys/types.h>
4 #include <sys/sysctl.h>
5 
6 #include <err.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 
12 #include <rump/rump_syscalls.h>
13 #include <rump/rumpclient.h>
14 
15 static const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME };
16 static char hostnamebuf[128];
17 
18 static volatile sig_atomic_t sigexecs;
19 
20 static void
21 sighand(int sig)
22 {
23 	char buf[128];
24 	size_t blen = sizeof(buf);
25 
26 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
27 	    buf, &blen, NULL, 0) == -1)
28 		err(1, "sighand sysctl");
29 	if (strcmp(buf, hostnamebuf) != 0)
30 		errx(1, "sighandler hostname");
31 	sigexecs++;
32 }
33 
34 int
35 main(void)
36 {
37 	char buf[128];
38 	struct itimerval itv;
39 	size_t hnbsize;
40 	int i;
41 	size_t blen;
42 
43 	if (rumpclient_init() == -1)
44 		err(1, "rumpclient init");
45 
46 	hnbsize = sizeof(hostnamebuf);
47 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
48 	    hostnamebuf, &hnbsize, NULL, 0) == -1)
49 		err(1, "sysctl");
50 
51 	if (signal(SIGALRM, sighand) == SIG_ERR)
52 		err(1, "signal");
53 
54 	itv.it_interval.tv_sec = itv.it_value.tv_sec = 0;
55 	itv.it_interval.tv_usec = itv.it_value.tv_usec = 10000; /* 10ms */
56 
57 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
58 		err(1, "itimer");
59 
60 	for (i = 0; i < 20000; i++) {
61 		blen = sizeof(buf);
62 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
63 		    buf, &blen, NULL, 0) == -1)
64 			err(1, "sysctl");
65 		if (strcmp(buf, hostnamebuf) != 0)
66 			errx(1, "main hostname");
67 	}
68 
69 	if (!sigexecs) {
70 		printf("no signal handlers run.  test busted?\n");
71 	}
72 }
73