rump_allserver.c revision 1.9 1 /* $NetBSD: rump_allserver.c,v 1.9 2010/12/14 16:40:05 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: rump_allserver.c,v 1.9 2010/12/14 16:40:05 pooka Exp $");
31 #endif /* !lint */
32
33 #include <sys/types.h>
34 #include <sys/signal.h>
35 #include <sys/module.h>
36
37 #include <rump/rump.h>
38 #include <rump/rump_syscalls.h>
39
40 #include <dlfcn.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <semaphore.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 static void
51 usage(void)
52 {
53
54 fprintf(stderr, "usage: %s [-d drivespec][-l libs] [-m modules][-s] "
55 "bindurl\n", getprogname());
56 exit(1);
57 }
58
59 static void
60 die(int sflag, int error, const char *reason)
61 {
62
63 warnx("%s: %s", reason, strerror(error));
64 if (!sflag)
65 rump_daemonize_done(error);
66 exit(1);
67 }
68
69 static sem_t sigsem;
70 static void
71 sigreboot(int sig)
72 {
73
74 sem_post(&sigsem);
75 }
76
77 static const char *const disktokens[] = {
78 #define DKEY 0
79 "key",
80 #define DFILE 1
81 "hostpath",
82 #define DSIZE 2
83 "size",
84 NULL
85 };
86
87 struct etfsreg {
88 const char *key;
89 const char *hostpath;
90 off_t flen;
91 enum rump_etfs_type type;
92 };
93
94 int
95 main(int argc, char *argv[])
96 {
97 const char *serverurl;
98 char **modarray = NULL;
99 unsigned nmods = 0, curmod = 0, i;
100 struct etfsreg *etfs = NULL;
101 unsigned netfs = 0, curetfs = 0;
102 int error;
103 int ch, sflag;
104
105 setprogname(argv[0]);
106
107 sflag = 0;
108 while ((ch = getopt(argc, argv, "d:l:m:s")) != -1) {
109 switch (ch) {
110 case 'd': {
111 char *options, *value;
112 char *key, *hostpath;
113 long long flen;
114
115 flen = 0;
116 key = hostpath = NULL;
117 options = optarg;
118 while (*options) {
119 switch (getsubopt(&options,
120 __UNCONST(disktokens), &value)) {
121 case DKEY:
122 key = value;
123 break;
124 case DFILE:
125 hostpath = value;
126 break;
127 case DSIZE:
128 /* XXX: off_t max? */
129 flen = strsuftoll("-d size", value,
130 0, LLONG_MAX);
131 break;
132 default:
133 fprintf(stderr, "invalid dtoken\n");
134 usage();
135 break;
136 }
137 }
138
139 if (key == NULL || hostpath == NULL || flen == 0) {
140 fprintf(stderr, "incomplete drivespec\n");
141 usage();
142 }
143
144 if (netfs - curetfs == 0) {
145 etfs = realloc(etfs, (netfs+16)*sizeof(*etfs));
146 if (etfs == NULL)
147 err(1, "realloc etfs");
148 netfs += 16;
149 }
150
151 etfs[curetfs].key = key;
152 etfs[curetfs].hostpath = hostpath;
153 etfs[curetfs].flen = flen;
154 etfs[curetfs].type = RUMP_ETFS_BLK;
155 curetfs++;
156
157 break;
158 }
159 case 'l':
160 if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
161 char pb[MAXPATHLEN];
162 /* try to mimic linker -l syntax */
163
164 snprintf(pb, sizeof(pb), "lib%s.so", optarg);
165 if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
166 errx(1, "dlopen %s failed: %s",
167 pb, dlerror());
168 }
169 }
170 break;
171 case 'm':
172 if (nmods - curmod == 0) {
173 modarray = realloc(modarray,
174 (nmods+16) * sizeof(char *));
175 if (modarray == NULL)
176 err(1, "realloc");
177 nmods += 16;
178 }
179 modarray[curmod++] = optarg;
180 break;
181 case 's':
182 sflag = 1;
183 break;
184 default:
185 usage();
186 /*NOTREACHED*/
187 }
188 }
189
190 argc -= optind;
191 argv += optind;
192
193 if (argc != 1)
194 usage();
195
196 serverurl = argv[0];
197
198 if (!sflag) {
199 error = rump_daemonize_begin();
200 if (error)
201 errx(1, "rump daemonize: %s", strerror(error));
202 }
203
204 error = rump_init();
205 if (error)
206 die(sflag, error, "rump init failed");
207
208 /* load modules */
209 for (i = 0; i < curmod; i++) {
210 struct modctl_load ml;
211
212 #define ETFSKEY "/module.mod"
213 if ((error = rump_pub_etfs_register(ETFSKEY,
214 modarray[0], RUMP_ETFS_REG)) != 0)
215 die(sflag, error, "module etfs register failed");
216 memset(&ml, 0, sizeof(ml));
217 ml.ml_filename = ETFSKEY;
218 if (rump_sys_modctl(MODCTL_LOAD, &ml) == -1)
219 die(sflag, errno, "module load failed");
220 rump_pub_etfs_remove(ETFSKEY);
221 #undef ETFSKEY
222 }
223
224 /* register host drives */
225 for (i = 0; i < curetfs; i++) {
226 int fd;
227
228 fd = open(etfs[i].hostpath, O_RDWR | O_CREAT, 0755);
229 if (fd == -1)
230 die(sflag, error, "etfs hostpath create");
231 if (ftruncate(fd, etfs[i].flen) == -1)
232 die(sflag, error, "truncate");
233 close(fd);
234
235 if ((error = rump_pub_etfs_register(etfs[i].key,
236 etfs[i].hostpath, etfs[i].type)) != 0)
237 die(sflag, error, "etfs register");
238 }
239
240 error = rump_init_server(serverurl);
241 if (error)
242 die(sflag, error, "rump server init failed");
243
244 if (!sflag)
245 rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
246
247 sem_init(&sigsem, 0, 0);
248 signal(SIGTERM, sigreboot);
249 signal(SIGINT, sigreboot);
250 sem_wait(&sigsem);
251
252 rump_sys_reboot(0, NULL);
253 /*NOTREACHED*/
254
255 return 0;
256 }
257