rump_allserver.c revision 1.7 1 1.7 pooka /* $NetBSD: rump_allserver.c,v 1.7 2010/12/13 14:13:21 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/cdefs.h>
29 1.1 pooka #ifndef lint
30 1.7 pooka __RCSID("$NetBSD: rump_allserver.c,v 1.7 2010/12/13 14:13:21 pooka Exp $");
31 1.1 pooka #endif /* !lint */
32 1.1 pooka
33 1.1 pooka #include <sys/types.h>
34 1.4 pooka #include <sys/signal.h>
35 1.7 pooka #include <sys/module.h>
36 1.1 pooka
37 1.1 pooka #include <rump/rump.h>
38 1.4 pooka #include <rump/rump_syscalls.h>
39 1.1 pooka
40 1.6 pooka #include <dlfcn.h>
41 1.1 pooka #include <err.h>
42 1.1 pooka #include <errno.h>
43 1.4 pooka #include <semaphore.h>
44 1.1 pooka #include <stdio.h>
45 1.1 pooka #include <stdlib.h>
46 1.1 pooka #include <string.h>
47 1.1 pooka #include <unistd.h>
48 1.1 pooka
49 1.1 pooka static void
50 1.1 pooka usage(void)
51 1.1 pooka {
52 1.1 pooka
53 1.1 pooka fprintf(stderr, "usage: %s [args] bindurl\n", getprogname());
54 1.1 pooka exit(1);
55 1.1 pooka }
56 1.1 pooka
57 1.2 pooka static void
58 1.2 pooka die(int sflag, int error, const char *reason)
59 1.2 pooka {
60 1.2 pooka
61 1.2 pooka warnx("%s: %s", reason, strerror(error));
62 1.2 pooka if (!sflag)
63 1.2 pooka rump_daemonize_done(error);
64 1.2 pooka exit(1);
65 1.2 pooka }
66 1.2 pooka
67 1.4 pooka static sem_t sigsem;
68 1.4 pooka static void
69 1.4 pooka sigreboot(int sig)
70 1.4 pooka {
71 1.4 pooka
72 1.4 pooka sem_post(&sigsem);
73 1.4 pooka }
74 1.4 pooka
75 1.1 pooka int
76 1.1 pooka main(int argc, char *argv[])
77 1.1 pooka {
78 1.2 pooka const char *serverurl;
79 1.7 pooka char **modarray = NULL;
80 1.7 pooka unsigned nmods = 0, curmod = 0, i;
81 1.1 pooka int error;
82 1.2 pooka int ch, sflag;
83 1.1 pooka
84 1.1 pooka setprogname(argv[0]);
85 1.1 pooka
86 1.2 pooka sflag = 0;
87 1.7 pooka while ((ch = getopt(argc, argv, "l:m:s")) != -1) {
88 1.2 pooka switch (ch) {
89 1.6 pooka case 'l':
90 1.6 pooka if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL)
91 1.6 pooka errx(1, "dlopen %s failed: %s",
92 1.6 pooka optarg, dlerror());
93 1.6 pooka break;
94 1.7 pooka case 'm':
95 1.7 pooka if (nmods - curmod == 0) {
96 1.7 pooka modarray = realloc(modarray,
97 1.7 pooka (nmods+16) * sizeof(char *));
98 1.7 pooka if (modarray == NULL)
99 1.7 pooka err(1, "realloc");
100 1.7 pooka nmods += 16;
101 1.7 pooka }
102 1.7 pooka modarray[curmod++] = optarg;
103 1.7 pooka break;
104 1.2 pooka case 's':
105 1.2 pooka sflag = 1;
106 1.2 pooka break;
107 1.2 pooka default:
108 1.2 pooka usage();
109 1.2 pooka /*NOTREACHED*/
110 1.2 pooka }
111 1.2 pooka }
112 1.2 pooka
113 1.2 pooka argc -= optind;
114 1.2 pooka argv += optind;
115 1.2 pooka
116 1.2 pooka if (argc != 1)
117 1.1 pooka usage();
118 1.1 pooka
119 1.2 pooka serverurl = argv[0];
120 1.2 pooka
121 1.2 pooka if (!sflag) {
122 1.2 pooka error = rump_daemonize_begin();
123 1.2 pooka if (error)
124 1.2 pooka errx(1, "rump daemonize: %s", strerror(error));
125 1.2 pooka }
126 1.2 pooka
127 1.1 pooka error = rump_init();
128 1.1 pooka if (error)
129 1.2 pooka die(sflag, error, "rump init failed");
130 1.7 pooka
131 1.7 pooka for (i = 0; i < curmod; i++) {
132 1.7 pooka struct modctl_load ml;
133 1.7 pooka
134 1.7 pooka #define ETFSKEY "/module.mod"
135 1.7 pooka if ((error = rump_pub_etfs_register(ETFSKEY,
136 1.7 pooka modarray[0], RUMP_ETFS_REG)) != 0)
137 1.7 pooka die(sflag, error, "module etfs register failed");
138 1.7 pooka memset(&ml, 0, sizeof(ml));
139 1.7 pooka ml.ml_filename = ETFSKEY;
140 1.7 pooka if (rump_sys_modctl(MODCTL_LOAD, &ml) == -1)
141 1.7 pooka die(sflag, errno, "module load failed");
142 1.7 pooka rump_pub_etfs_remove(ETFSKEY);
143 1.7 pooka }
144 1.7 pooka
145 1.2 pooka error = rump_init_server(serverurl);
146 1.1 pooka if (error)
147 1.2 pooka die(sflag, error, "rump server init failed");
148 1.2 pooka
149 1.2 pooka if (!sflag)
150 1.3 pooka rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
151 1.2 pooka
152 1.4 pooka sem_init(&sigsem, 0, 0);
153 1.4 pooka signal(SIGTERM, sigreboot);
154 1.4 pooka signal(SIGINT, sigreboot);
155 1.4 pooka sem_wait(&sigsem);
156 1.4 pooka
157 1.4 pooka rump_sys_reboot(0, NULL);
158 1.5 pooka /*NOTREACHED*/
159 1.5 pooka
160 1.4 pooka return 0;
161 1.1 pooka }
162