1*11be35a1SLionel Sambuc /* $NetBSD: h_mdserv.c,v 1.4 2011/02/10 13:29:02 pooka Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/mman.h>
5*11be35a1SLionel Sambuc #include <sys/ioctl.h>
6*11be35a1SLionel Sambuc
7*11be35a1SLionel Sambuc #include <dev/md.h>
8*11be35a1SLionel Sambuc
9*11be35a1SLionel Sambuc #include <err.h>
10*11be35a1SLionel Sambuc #include <errno.h>
11*11be35a1SLionel Sambuc #include <fcntl.h>
12*11be35a1SLionel Sambuc #include <pthread.h>
13*11be35a1SLionel Sambuc #include <stdio.h>
14*11be35a1SLionel Sambuc #include <stdlib.h>
15*11be35a1SLionel Sambuc #include <unistd.h>
16*11be35a1SLionel Sambuc
17*11be35a1SLionel Sambuc #include <rump/rump.h>
18*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
19*11be35a1SLionel Sambuc
20*11be35a1SLionel Sambuc #define MDSIZE (1024*1024)
21*11be35a1SLionel Sambuc
22*11be35a1SLionel Sambuc #define REQUIRE(a, msg) if ((a) != 0) err(1, msg);
23*11be35a1SLionel Sambuc
24*11be35a1SLionel Sambuc static void *
prober(void * arg)25*11be35a1SLionel Sambuc prober(void *arg)
26*11be35a1SLionel Sambuc {
27*11be35a1SLionel Sambuc int fd, error;
28*11be35a1SLionel Sambuc char buf[4];
29*11be35a1SLionel Sambuc ssize_t n;
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc fd = rump_sys_open(arg, O_RDONLY);
32*11be35a1SLionel Sambuc for (;;) {
33*11be35a1SLionel Sambuc n = rump_sys_read(fd, buf, sizeof(buf));
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc switch (n) {
36*11be35a1SLionel Sambuc case 4:
37*11be35a1SLionel Sambuc error = 0;
38*11be35a1SLionel Sambuc goto out;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc case -1:
41*11be35a1SLionel Sambuc if (errno == ENXIO) {
42*11be35a1SLionel Sambuc usleep(1000);
43*11be35a1SLionel Sambuc continue;
44*11be35a1SLionel Sambuc }
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc /* FALLTHROUGH */
47*11be35a1SLionel Sambuc default:
48*11be35a1SLionel Sambuc error = EPIPE;
49*11be35a1SLionel Sambuc goto out;
50*11be35a1SLionel Sambuc }
51*11be35a1SLionel Sambuc }
52*11be35a1SLionel Sambuc out:
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc error = rump_daemonize_done(error);
55*11be35a1SLionel Sambuc REQUIRE(error, "rump_daemonize_done");
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc if (error)
58*11be35a1SLionel Sambuc exit(1);
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc return NULL;
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc int
main(int argc,char * argv[])64*11be35a1SLionel Sambuc main(int argc, char *argv[])
65*11be35a1SLionel Sambuc {
66*11be35a1SLionel Sambuc pthread_t pt;
67*11be35a1SLionel Sambuc struct md_conf md;
68*11be35a1SLionel Sambuc int fd, error;
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc if (argc != 2)
71*11be35a1SLionel Sambuc exit(1);
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc md.md_addr = calloc(1, MDSIZE);
74*11be35a1SLionel Sambuc md.md_size = MDSIZE;
75*11be35a1SLionel Sambuc md.md_type = MD_UMEM_SERVER;
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc error = rump_daemonize_begin();
78*11be35a1SLionel Sambuc REQUIRE(error, "rump_daemonize_begin");
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc error = rump_init();
81*11be35a1SLionel Sambuc REQUIRE(error, "rump_init");
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc error = rump_init_server("unix://commsock");
84*11be35a1SLionel Sambuc REQUIRE(error, "init server");
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc if ((fd = rump_sys_open(argv[1], O_RDWR)) == -1)
87*11be35a1SLionel Sambuc err(1, "open");
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc /*
90*11be35a1SLionel Sambuc * Now, configuring the md driver also causes our process
91*11be35a1SLionel Sambuc * to start acting as the worker for the md. Splitting it
92*11be35a1SLionel Sambuc * into two steps in the driver is not easy, since md is
93*11be35a1SLionel Sambuc * supposed to be unconfigured when the process dies
94*11be35a1SLionel Sambuc * (process may exit between calling ioctl1 and ioctl2).
95*11be35a1SLionel Sambuc * So, start a probe thread which attempts to read the md
96*11be35a1SLionel Sambuc * and declares the md as configured when the read is
97*11be35a1SLionel Sambuc * succesful.
98*11be35a1SLionel Sambuc */
99*11be35a1SLionel Sambuc error = pthread_create(&pt, NULL, prober, argv[1]);
100*11be35a1SLionel Sambuc REQUIRE(error, "pthread_create");
101*11be35a1SLionel Sambuc pthread_detach(pt);
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc if (rump_sys_ioctl(fd, MD_SETCONF, &md) == -1) {
104*11be35a1SLionel Sambuc rump_daemonize_done(errno);
105*11be35a1SLionel Sambuc exit(1);
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc return 0;
109*11be35a1SLionel Sambuc }
110