xref: /freebsd/contrib/netbsd-tests/rump/rumpkern/h_client/h_sigcli.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*	$NetBSD: h_sigcli.c,v 1.3 2011/02/07 20:05:09 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 	time_t tstart;
39 	struct itimerval itv;
40 	size_t hnbsize;
41 	int i;
42 	size_t blen;
43 
44 	if (rumpclient_init() == -1)
45 		err(1, "rumpclient init");
46 
47 	hnbsize = sizeof(hostnamebuf);
48 	if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
49 	    hostnamebuf, &hnbsize, NULL, 0) == -1)
50 		err(1, "sysctl");
51 
52 	if (signal(SIGALRM, sighand) == SIG_ERR)
53 		err(1, "signal");
54 
55 	itv.it_interval.tv_sec = itv.it_value.tv_sec = 0;
56 	itv.it_interval.tv_usec = itv.it_value.tv_usec = 10000; /* 10ms */
57 
58 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
59 		err(1, "itimer");
60 
61 	tstart = time(NULL);
62 	for (i = 0;; i++) {
63 		blen = sizeof(buf);
64 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
65 		    buf, &blen, NULL, 0) == -1)
66 			err(1, "sysctl");
67 		if (strcmp(buf, hostnamebuf) != 0)
68 			errx(1, "main hostname");
69 
70 		/*
71 		 * check every 100 cycles to avoid doing
72 		 * nothing but gettimeofday()
73 		 */
74 		if (i == 100) {
75 			if (time(NULL) - tstart > 5)
76 				break;
77 			i = 0;
78 		}
79 	}
80 
81 	if (!sigexecs) {
82 		printf("no signal handlers run.  test busted?\n");
83 	}
84 }
85