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