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