1*6ee4aeaeSpooka /* $NetBSD: rump_allserver.c,v 1.39 2015/04/16 10:05:43 pooka Exp $ */
26eb225bbSpooka
36eb225bbSpooka /*-
474135a6cSpooka * Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
56eb225bbSpooka *
66eb225bbSpooka * Redistribution and use in source and binary forms, with or without
76eb225bbSpooka * modification, are permitted provided that the following conditions
86eb225bbSpooka * are met:
96eb225bbSpooka * 1. Redistributions of source code must retain the above copyright
106eb225bbSpooka * notice, this list of conditions and the following disclaimer.
116eb225bbSpooka * 2. Redistributions in binary form must reproduce the above copyright
126eb225bbSpooka * notice, this list of conditions and the following disclaimer in the
136eb225bbSpooka * documentation and/or other materials provided with the distribution.
146eb225bbSpooka *
156eb225bbSpooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
166eb225bbSpooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
176eb225bbSpooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
186eb225bbSpooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
196eb225bbSpooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206eb225bbSpooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
216eb225bbSpooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226eb225bbSpooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236eb225bbSpooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246eb225bbSpooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256eb225bbSpooka * SUCH DAMAGE.
266eb225bbSpooka */
276eb225bbSpooka
284e5f4292Spooka #include <rump/rumpuser_port.h>
294e5f4292Spooka
306eb225bbSpooka #ifndef lint
31*6ee4aeaeSpooka __RCSID("$NetBSD: rump_allserver.c,v 1.39 2015/04/16 10:05:43 pooka Exp $");
326eb225bbSpooka #endif /* !lint */
336eb225bbSpooka
346eb225bbSpooka #include <sys/types.h>
354e5f4292Spooka #include <sys/stat.h>
366eb225bbSpooka
37ac0678feSpooka #include <dlfcn.h>
386eb225bbSpooka #include <errno.h>
39dbc10b5aSpooka #include <fcntl.h>
40ae8a3e28Spooka #include <semaphore.h>
414b870e37Spooka #include <signal.h>
426eb225bbSpooka #include <stdio.h>
436eb225bbSpooka #include <stdlib.h>
444e5f4292Spooka #include <stdint.h>
456eb225bbSpooka #include <string.h>
466eb225bbSpooka #include <unistd.h>
474e5f4292Spooka
484e5f4292Spooka #include <rump/rump.h>
494e5f4292Spooka #include <rump/rump_syscalls.h>
508b205e86Spooka #include <rump/rumpdefs.h>
5157d34ab9Spooka #include <rump/rumperr.h>
526eb225bbSpooka
536818646aSjoerg __dead static void
usage(void)546eb225bbSpooka usage(void)
556eb225bbSpooka {
566eb225bbSpooka
5723dfcd74Spooka #ifndef HAVE_GETPROGNAME
584e5f4292Spooka #define getprogname() "rump_server"
594e5f4292Spooka #endif
6023e23530Swiz fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
616fd00c3fSpooka "[-m modules] bindurl\n", getprogname());
626eb225bbSpooka exit(1);
636eb225bbSpooka }
646eb225bbSpooka
656818646aSjoerg __dead static void
diedie(int sflag,const char * reason,int error,const char * errstr)6657d34ab9Spooka diedie(int sflag, const char *reason, int error, const char *errstr)
67126e32a1Spooka {
68126e32a1Spooka
69a88c18e5Sbad if (reason != NULL)
70a88c18e5Sbad fputs(reason, stderr);
7157d34ab9Spooka if (errstr) {
7257d34ab9Spooka fprintf(stderr, ": %s", errstr);
7357d34ab9Spooka }
74a88c18e5Sbad fputc('\n', stderr);
75126e32a1Spooka if (!sflag)
76126e32a1Spooka rump_daemonize_done(error);
77126e32a1Spooka exit(1);
78126e32a1Spooka }
79126e32a1Spooka
8057d34ab9Spooka __dead static void
die(int sflag,int error,const char * reason)8157d34ab9Spooka die(int sflag, int error, const char *reason)
8257d34ab9Spooka {
8357d34ab9Spooka
8457d34ab9Spooka diedie(sflag, reason, error, error == 0 ? NULL : strerror(error));
8557d34ab9Spooka }
8657d34ab9Spooka
8757d34ab9Spooka __dead static void
die_rumperr(int sflag,int error,const char * reason)8857d34ab9Spooka die_rumperr(int sflag, int error, const char *reason)
8957d34ab9Spooka {
9057d34ab9Spooka
9157d34ab9Spooka diedie(sflag, reason, error, error == 0 ? NULL : rump_strerror(error));
9257d34ab9Spooka }
9357d34ab9Spooka
94ae8a3e28Spooka static sem_t sigsem;
95ae8a3e28Spooka static void
sigreboot(int sig)96ae8a3e28Spooka sigreboot(int sig)
97ae8a3e28Spooka {
98ae8a3e28Spooka
99ae8a3e28Spooka sem_post(&sigsem);
100ae8a3e28Spooka }
101ae8a3e28Spooka
102dbc10b5aSpooka static const char *const disktokens[] = {
103dbc10b5aSpooka #define DKEY 0
104dbc10b5aSpooka "key",
105dbc10b5aSpooka #define DFILE 1
106dbc10b5aSpooka "hostpath",
107dbc10b5aSpooka #define DSIZE 2
108f6adb4bdSpooka #define DSIZE_E -1
109dbc10b5aSpooka "size",
110ed37916eSpooka #define DOFFSET 3
111ed37916eSpooka "offset",
11274135a6cSpooka #define DLABEL 4
11374135a6cSpooka "disklabel",
114aaf2cb63Spooka #define DTYPE 5
115aaf2cb63Spooka "type",
116dbc10b5aSpooka NULL
117dbc10b5aSpooka };
118dbc10b5aSpooka
119dbc10b5aSpooka struct etfsreg {
120dbc10b5aSpooka const char *key;
121dbc10b5aSpooka const char *hostpath;
122dbc10b5aSpooka off_t flen;
123ed37916eSpooka off_t foffset;
12474135a6cSpooka char partition;
125dbc10b5aSpooka enum rump_etfs_type type;
126dbc10b5aSpooka };
127dbc10b5aSpooka
128aaf2cb63Spooka struct etfstype {
129aaf2cb63Spooka const char *name;
130aaf2cb63Spooka enum rump_etfs_type type;
131aaf2cb63Spooka } etfstypes[] = {
132aaf2cb63Spooka { "blk", RUMP_ETFS_BLK },
133aaf2cb63Spooka { "chr", RUMP_ETFS_CHR },
134aaf2cb63Spooka { "reg", RUMP_ETFS_REG },
135aaf2cb63Spooka };
136aaf2cb63Spooka
137cbdf0ef4Spooka static void processlabel(int, int, int, off_t *, off_t *);
138cbdf0ef4Spooka
1395304903cSpooka #define ALLOCCHUNK 32
1405304903cSpooka
1416eb225bbSpooka int
main(int argc,char * argv[])1426eb225bbSpooka main(int argc, char *argv[])
1436eb225bbSpooka {
144126e32a1Spooka const char *serverurl;
145dbc10b5aSpooka struct etfsreg *etfs = NULL;
146dbc10b5aSpooka unsigned netfs = 0, curetfs = 0;
1476eb225bbSpooka int error;
1485304903cSpooka int ch, sflag, onthepath;
1494e5f4292Spooka unsigned i;
1505304903cSpooka char **modarray = NULL, **libarray = NULL;
151c06e5f96Spooka unsigned nmods = 0, curmod = 0, nlibs = 0, curlib = 0, libidx;
152c06e5f96Spooka unsigned liblast = -1; /* XXXgcc */
1534e5f4292Spooka
1546eb225bbSpooka setprogname(argv[0]);
155126e32a1Spooka sflag = 0;
15629d11b77Spooka while ((ch = getopt(argc, argv, "c:d:l:m:r:sv")) != -1) {
157126e32a1Spooka switch (ch) {
1586fd00c3fSpooka case 'c':
1596fd00c3fSpooka setenv("RUMP_NCPU", optarg, 1);
1606fd00c3fSpooka break;
161dbc10b5aSpooka case 'd': {
162dbc10b5aSpooka char *options, *value;
163dbc10b5aSpooka char *key, *hostpath;
164ed37916eSpooka long long flen, foffset;
16574135a6cSpooka char partition;
166aaf2cb63Spooka int ftype;
167dbc10b5aSpooka
168ed37916eSpooka flen = foffset = 0;
16974135a6cSpooka partition = 0;
170dbc10b5aSpooka key = hostpath = NULL;
171aaf2cb63Spooka ftype = -1;
172dbc10b5aSpooka options = optarg;
173dbc10b5aSpooka while (*options) {
174dbc10b5aSpooka switch (getsubopt(&options,
175dbc10b5aSpooka __UNCONST(disktokens), &value)) {
176dbc10b5aSpooka case DKEY:
177bd7256bbSpooka if (key != NULL) {
178bd7256bbSpooka fprintf(stderr,
179bd7256bbSpooka "key already given\n");
180bd7256bbSpooka usage();
181bd7256bbSpooka }
182dbc10b5aSpooka key = value;
183dbc10b5aSpooka break;
18474135a6cSpooka
185dbc10b5aSpooka case DFILE:
186bd7256bbSpooka if (hostpath != NULL) {
187bd7256bbSpooka fprintf(stderr,
188bd7256bbSpooka "hostpath already given\n");
189bd7256bbSpooka usage();
190bd7256bbSpooka }
191dbc10b5aSpooka hostpath = value;
192dbc10b5aSpooka break;
19374135a6cSpooka
194dbc10b5aSpooka case DSIZE:
195bd7256bbSpooka if (flen != 0) {
196bd7256bbSpooka fprintf(stderr,
197bd7256bbSpooka "size already given\n");
198bd7256bbSpooka usage();
199bd7256bbSpooka }
200090a5e7dSpooka if (strcmp(value, "host") == 0) {
201f6adb4bdSpooka if (foffset != 0) {
202f6adb4bdSpooka fprintf(stderr,
203f6adb4bdSpooka "cannot specify "
204f6adb4bdSpooka "offset with "
205b6c13d85Spooka "size=host\n");
206f6adb4bdSpooka usage();
207f6adb4bdSpooka }
208f6adb4bdSpooka flen = DSIZE_E;
209f6adb4bdSpooka } else {
21023dfcd74Spooka #ifdef HAVE_STRSUFTOLL
211dbc10b5aSpooka /* XXX: off_t max? */
212f6adb4bdSpooka flen = strsuftoll("-d size",
213f6adb4bdSpooka value, 0, LLONG_MAX);
2144e5f4292Spooka #else
2154e5f4292Spooka flen = strtoull(value,
2164e5f4292Spooka NULL, 10);
2174e5f4292Spooka #endif
218f6adb4bdSpooka }
219dbc10b5aSpooka break;
220ed37916eSpooka case DOFFSET:
221ed37916eSpooka if (foffset != 0) {
222ed37916eSpooka fprintf(stderr,
223ed37916eSpooka "offset already given\n");
224ed37916eSpooka usage();
225ed37916eSpooka }
226f6adb4bdSpooka if (flen == DSIZE_E) {
227f6adb4bdSpooka fprintf(stderr, "cannot "
228f6adb4bdSpooka "specify offset with "
229b6c13d85Spooka "size=host\n");
230f6adb4bdSpooka usage();
231f6adb4bdSpooka }
23223dfcd74Spooka #ifdef HAVE_STRSUFTOLL
233ed37916eSpooka /* XXX: off_t max? */
234ed37916eSpooka foffset = strsuftoll("-d offset", value,
235ed37916eSpooka 0, LLONG_MAX);
2364e5f4292Spooka #else
2374e5f4292Spooka foffset = strtoull(value, NULL, 10);
2384e5f4292Spooka #endif
239ed37916eSpooka break;
24074135a6cSpooka
24174135a6cSpooka case DLABEL:
24274135a6cSpooka if (foffset != 0 || flen != 0) {
24374135a6cSpooka fprintf(stderr,
24474135a6cSpooka "disklabel needs to be "
24574135a6cSpooka "used alone\n");
24674135a6cSpooka usage();
24774135a6cSpooka }
24874135a6cSpooka if (strlen(value) != 1 ||
24974135a6cSpooka *value < 'a' || *value > 'z') {
25074135a6cSpooka fprintf(stderr,
25174135a6cSpooka "invalid label part\n");
25274135a6cSpooka usage();
25374135a6cSpooka }
25474135a6cSpooka partition = *value;
25574135a6cSpooka break;
25674135a6cSpooka
257aaf2cb63Spooka case DTYPE:
258aaf2cb63Spooka if (ftype != -1) {
259aaf2cb63Spooka fprintf(stderr,
260aaf2cb63Spooka "type already specified\n");
261aaf2cb63Spooka usage();
262aaf2cb63Spooka }
263aaf2cb63Spooka
264aaf2cb63Spooka for (i = 0;
265aaf2cb63Spooka i < __arraycount(etfstypes);
266aaf2cb63Spooka i++) {
267aaf2cb63Spooka if (strcmp(etfstypes[i].name,
268aaf2cb63Spooka value) == 0)
269aaf2cb63Spooka break;
270aaf2cb63Spooka }
271aaf2cb63Spooka if (i == __arraycount(etfstypes)) {
272aaf2cb63Spooka fprintf(stderr,
273aaf2cb63Spooka "invalid type %s\n", value);
274aaf2cb63Spooka usage();
275aaf2cb63Spooka }
276aaf2cb63Spooka ftype = etfstypes[i].type;
277aaf2cb63Spooka break;
278aaf2cb63Spooka
279dbc10b5aSpooka default:
280dbc10b5aSpooka fprintf(stderr, "invalid dtoken\n");
281dbc10b5aSpooka usage();
282dbc10b5aSpooka break;
283dbc10b5aSpooka }
284dbc10b5aSpooka }
285dbc10b5aSpooka
28674135a6cSpooka if (key == NULL || hostpath == NULL ||
287*6ee4aeaeSpooka (flen == 0
288*6ee4aeaeSpooka && partition == 0 && ftype != RUMP_ETFS_REG)) {
289dbc10b5aSpooka fprintf(stderr, "incomplete drivespec\n");
290dbc10b5aSpooka usage();
291dbc10b5aSpooka }
292aaf2cb63Spooka if (ftype == -1)
293aaf2cb63Spooka ftype = RUMP_ETFS_BLK;
294dbc10b5aSpooka
295dbc10b5aSpooka if (netfs - curetfs == 0) {
2965304903cSpooka etfs = realloc(etfs,
2975304903cSpooka (netfs+ALLOCCHUNK)*sizeof(*etfs));
298dbc10b5aSpooka if (etfs == NULL)
299d5e9a0d6Spooka die(1, errno, "realloc etfs");
3005304903cSpooka netfs += ALLOCCHUNK;
301dbc10b5aSpooka }
302dbc10b5aSpooka
303dbc10b5aSpooka etfs[curetfs].key = key;
304dbc10b5aSpooka etfs[curetfs].hostpath = hostpath;
305dbc10b5aSpooka etfs[curetfs].flen = flen;
306ed37916eSpooka etfs[curetfs].foffset = foffset;
30774135a6cSpooka etfs[curetfs].partition = partition;
308aaf2cb63Spooka etfs[curetfs].type = ftype;
309dbc10b5aSpooka curetfs++;
310dbc10b5aSpooka
311dbc10b5aSpooka break;
312dbc10b5aSpooka }
313ac0678feSpooka case 'l':
3145304903cSpooka if (nlibs - curlib == 0) {
3155304903cSpooka libarray = realloc(libarray,
3165304903cSpooka (nlibs+ALLOCCHUNK) * sizeof(char *));
3175304903cSpooka if (libarray == NULL)
3185304903cSpooka die(1, errno, "realloc");
3195304903cSpooka nlibs += ALLOCCHUNK;
320dde3f793Spooka }
3215304903cSpooka libarray[curlib++] = optarg;
322ac0678feSpooka break;
3235304903cSpooka case 'm':
324d3ab3edeSpooka if (nmods - curmod == 0) {
325d3ab3edeSpooka modarray = realloc(modarray,
3265304903cSpooka (nmods+ALLOCCHUNK) * sizeof(char *));
327d3ab3edeSpooka if (modarray == NULL)
328d5e9a0d6Spooka die(1, errno, "realloc");
3295304903cSpooka nmods += ALLOCCHUNK;
330d3ab3edeSpooka }
331d3ab3edeSpooka modarray[curmod++] = optarg;
3325304903cSpooka break;
33329d11b77Spooka case 'r':
33429d11b77Spooka setenv("RUMP_MEMLIMIT", optarg, 1);
33529d11b77Spooka break;
336126e32a1Spooka case 's':
337126e32a1Spooka sflag = 1;
338126e32a1Spooka break;
33929d11b77Spooka case 'v':
34029d11b77Spooka setenv("RUMP_VERBOSE", "1", 1);
34129d11b77Spooka break;
342126e32a1Spooka default:
3436eb225bbSpooka usage();
344126e32a1Spooka /*NOTREACHED*/
345126e32a1Spooka }
346126e32a1Spooka }
347126e32a1Spooka
348126e32a1Spooka argc -= optind;
349126e32a1Spooka argv += optind;
350126e32a1Spooka
351126e32a1Spooka if (argc != 1)
352126e32a1Spooka usage();
353126e32a1Spooka
3545304903cSpooka /*
3555304903cSpooka * Automatically "resolve" component dependencies, i.e.
3565304903cSpooka * try to load libs in a loop until all are loaded or a
3575304903cSpooka * full loop completes with no loads (latter case is an error).
3585304903cSpooka */
3595304903cSpooka for (onthepath = 1, nlibs = curlib; onthepath && nlibs > 0;) {
3605304903cSpooka onthepath = 0;
3615304903cSpooka for (libidx = 0; libidx < curlib; libidx++) {
3625304903cSpooka /* loaded already? */
3635304903cSpooka if (libarray[libidx] == NULL)
3645304903cSpooka continue;
3655304903cSpooka
3665304903cSpooka /* try to load */
3675304903cSpooka liblast = libidx;
3685304903cSpooka if (dlopen(libarray[libidx],
3695304903cSpooka RTLD_LAZY|RTLD_GLOBAL) == NULL) {
3705304903cSpooka char pb[MAXPATHLEN];
3715304903cSpooka /* try to mimic linker -l syntax */
3725304903cSpooka snprintf(pb, sizeof(pb),
3735304903cSpooka "lib%s.so", libarray[libidx]);
3745304903cSpooka if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL)
3755304903cSpooka continue;
3765304903cSpooka }
3775304903cSpooka
3785304903cSpooka /* managed to load that one */
3795304903cSpooka libarray[libidx] = NULL;
3805304903cSpooka nlibs--;
3815304903cSpooka onthepath = 1;
3825304903cSpooka }
3835304903cSpooka }
3845304903cSpooka if (nlibs > 0) {
3855304903cSpooka fprintf(stderr,
3865304903cSpooka "failed to load -libraries, last error from \"%s\":\n",
3875304903cSpooka libarray[liblast]);
3885304903cSpooka fprintf(stderr, " %s", dlerror());
3895304903cSpooka die(1, 0, NULL);
3905304903cSpooka }
3915304903cSpooka free(libarray);
3925304903cSpooka
393126e32a1Spooka serverurl = argv[0];
394126e32a1Spooka
395126e32a1Spooka if (!sflag) {
396126e32a1Spooka error = rump_daemonize_begin();
397126e32a1Spooka if (error)
39857d34ab9Spooka die_rumperr(1, error, "rump daemonize");
399126e32a1Spooka }
4006eb225bbSpooka
4016eb225bbSpooka error = rump_init();
4026eb225bbSpooka if (error)
40357d34ab9Spooka die_rumperr(sflag, error, "rump init failed");
404d3ab3edeSpooka
405dbc10b5aSpooka /* load modules */
406d3ab3edeSpooka for (i = 0; i < curmod; i++) {
4078b205e86Spooka struct rump_modctl_load ml;
408d3ab3edeSpooka
409d3ab3edeSpooka #define ETFSKEY "/module.mod"
410d3ab3edeSpooka if ((error = rump_pub_etfs_register(ETFSKEY,
411d3ab3edeSpooka modarray[0], RUMP_ETFS_REG)) != 0)
41257d34ab9Spooka die_rumperr(sflag,
41357d34ab9Spooka error, "module etfs register failed");
414d3ab3edeSpooka memset(&ml, 0, sizeof(ml));
415d3ab3edeSpooka ml.ml_filename = ETFSKEY;
41657d34ab9Spooka /*
41757d34ab9Spooka * XXX: since this is a syscall, error namespace depends
41857d34ab9Spooka * on loaded emulations. revisit and fix.
41957d34ab9Spooka */
4208b205e86Spooka if (rump_sys_modctl(RUMP_MODCTL_LOAD, &ml) == -1)
421d3ab3edeSpooka die(sflag, errno, "module load failed");
422d3ab3edeSpooka rump_pub_etfs_remove(ETFSKEY);
423dbc10b5aSpooka #undef ETFSKEY
424dbc10b5aSpooka }
4255304903cSpooka free(modarray);
426dbc10b5aSpooka
427dbc10b5aSpooka /* register host drives */
428dbc10b5aSpooka for (i = 0; i < curetfs; i++) {
429ed37916eSpooka struct stat sb;
43074135a6cSpooka off_t foffset, flen, fendoff;
431f6adb4bdSpooka int fd, oflags;
432dbc10b5aSpooka
433f6adb4bdSpooka oflags = etfs[i].flen == DSIZE_E ? 0 : O_CREAT;
434f6adb4bdSpooka fd = open(etfs[i].hostpath, O_RDWR | oflags, 0644);
435dbc10b5aSpooka if (fd == -1)
436f6adb4bdSpooka die(sflag, errno, "etfs hostpath open");
43774135a6cSpooka
43874135a6cSpooka if (etfs[i].partition) {
439cbdf0ef4Spooka processlabel(sflag, fd, etfs[i].partition - 'a',
440cbdf0ef4Spooka &foffset, &flen);
44174135a6cSpooka } else {
44274135a6cSpooka foffset = etfs[i].foffset;
44374135a6cSpooka flen = etfs[i].flen;
44474135a6cSpooka }
44574135a6cSpooka
446ed37916eSpooka if (fstat(fd, &sb) == -1)
447ed37916eSpooka die(sflag, errno, "fstat etfs hostpath");
448f6adb4bdSpooka if (flen == DSIZE_E) {
449b6c13d85Spooka if (sb.st_size == 0)
450b6c13d85Spooka die(sflag, EINVAL, "size=host, but cannot "
451b6c13d85Spooka "query non-zero size");
452f6adb4bdSpooka flen = sb.st_size;
453f6adb4bdSpooka }
454f6adb4bdSpooka fendoff = foffset + flen;
45574135a6cSpooka if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
45674135a6cSpooka if (ftruncate(fd, fendoff) == -1)
457ed37916eSpooka die(sflag, errno, "truncate");
458ed37916eSpooka }
459dbc10b5aSpooka close(fd);
460dbc10b5aSpooka
461ed37916eSpooka if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
46274135a6cSpooka etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
46357d34ab9Spooka die_rumperr(sflag, error, "etfs register");
464d3ab3edeSpooka }
465d3ab3edeSpooka
466126e32a1Spooka error = rump_init_server(serverurl);
4676eb225bbSpooka if (error)
46857d34ab9Spooka die_rumperr(sflag, error, "rump server init failed");
469126e32a1Spooka
470126e32a1Spooka if (!sflag)
4716b9a77d4Spooka rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
472126e32a1Spooka
473ae8a3e28Spooka sem_init(&sigsem, 0, 0);
474ae8a3e28Spooka signal(SIGTERM, sigreboot);
475ae8a3e28Spooka signal(SIGINT, sigreboot);
476ae8a3e28Spooka sem_wait(&sigsem);
477ae8a3e28Spooka
478ae8a3e28Spooka rump_sys_reboot(0, NULL);
4794e637316Spooka /*NOTREACHED*/
4804e637316Spooka
481ae8a3e28Spooka return 0;
4826eb225bbSpooka }
483cbdf0ef4Spooka
484cbdf0ef4Spooka /*
485cbdf0ef4Spooka * Copyright (c) 1987, 1988, 1993
486cbdf0ef4Spooka * The Regents of the University of California. All rights reserved.
487cbdf0ef4Spooka *
488cbdf0ef4Spooka * Redistribution and use in source and binary forms, with or without
489cbdf0ef4Spooka * modification, are permitted provided that the following conditions
490cbdf0ef4Spooka * are met:
491cbdf0ef4Spooka * 1. Redistributions of source code must retain the above copyright
492cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer.
493cbdf0ef4Spooka * 2. Redistributions in binary form must reproduce the above copyright
494cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer in the
495cbdf0ef4Spooka * documentation and/or other materials provided with the distribution.
496cbdf0ef4Spooka * 3. Neither the name of the University nor the names of its contributors
497cbdf0ef4Spooka * may be used to endorse or promote products derived from this software
498cbdf0ef4Spooka * without specific prior written permission.
499cbdf0ef4Spooka *
500cbdf0ef4Spooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
501cbdf0ef4Spooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
502cbdf0ef4Spooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
503cbdf0ef4Spooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
504cbdf0ef4Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
505cbdf0ef4Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
506cbdf0ef4Spooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
507cbdf0ef4Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
508cbdf0ef4Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
509cbdf0ef4Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510cbdf0ef4Spooka * SUCH DAMAGE.
511cbdf0ef4Spooka *
512cbdf0ef4Spooka * @(#)disklabel.h 8.2 (Berkeley) 7/10/94
513cbdf0ef4Spooka */
514cbdf0ef4Spooka
515cbdf0ef4Spooka #define RUMPSERVER_MAXPARTITIONS 22
516cbdf0ef4Spooka #define RUMPSERVER_DISKMAGIC ((uint32_t)0x82564557) /* magic */
517cbdf0ef4Spooka #define RUMPSERVER_DEVSHIFT 9
518cbdf0ef4Spooka
519cbdf0ef4Spooka struct rumpserver_disklabel {
520cbdf0ef4Spooka uint32_t d_magic; /* the magic number */
521cbdf0ef4Spooka uint16_t d_type; /* drive type */
522cbdf0ef4Spooka uint16_t d_subtype; /* controller/d_type specific */
523cbdf0ef4Spooka char d_typename[16]; /* type name, e.g. "eagle" */
524cbdf0ef4Spooka
525cbdf0ef4Spooka /*
526cbdf0ef4Spooka * d_packname contains the pack identifier and is returned when
527cbdf0ef4Spooka * the disklabel is read off the disk or in-core copy.
528cbdf0ef4Spooka * d_boot0 and d_boot1 are the (optional) names of the
529cbdf0ef4Spooka * primary (block 0) and secondary (block 1-15) bootstraps
530cbdf0ef4Spooka * as found in /usr/mdec. These are returned when using
531cbdf0ef4Spooka * getdiskbyname(3) to retrieve the values from /etc/disktab.
532cbdf0ef4Spooka */
533cbdf0ef4Spooka union {
534cbdf0ef4Spooka char un_d_packname[16]; /* pack identifier */
535cbdf0ef4Spooka struct {
536cbdf0ef4Spooka char *un_d_boot0; /* primary bootstrap name */
537cbdf0ef4Spooka char *un_d_boot1; /* secondary bootstrap name */
538cbdf0ef4Spooka } un_b;
539cbdf0ef4Spooka } d_un;
540cbdf0ef4Spooka #define d_packname d_un.un_d_packname
541cbdf0ef4Spooka #define d_boot0 d_un.un_b.un_d_boot0
542cbdf0ef4Spooka #define d_boot1 d_un.un_b.un_d_boot1
543cbdf0ef4Spooka
544cbdf0ef4Spooka /* disk geometry: */
545cbdf0ef4Spooka uint32_t d_secsize; /* # of bytes per sector */
546cbdf0ef4Spooka uint32_t d_nsectors; /* # of data sectors per track */
547cbdf0ef4Spooka uint32_t d_ntracks; /* # of tracks per cylinder */
548cbdf0ef4Spooka uint32_t d_ncylinders; /* # of data cylinders per unit */
549cbdf0ef4Spooka uint32_t d_secpercyl; /* # of data sectors per cylinder */
550cbdf0ef4Spooka uint32_t d_secperunit; /* # of data sectors per unit */
551cbdf0ef4Spooka
552cbdf0ef4Spooka /*
553cbdf0ef4Spooka * Spares (bad sector replacements) below are not counted in
554cbdf0ef4Spooka * d_nsectors or d_secpercyl. Spare sectors are assumed to
555cbdf0ef4Spooka * be physical sectors which occupy space at the end of each
556cbdf0ef4Spooka * track and/or cylinder.
557cbdf0ef4Spooka */
558cbdf0ef4Spooka uint16_t d_sparespertrack; /* # of spare sectors per track */
559cbdf0ef4Spooka uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
560cbdf0ef4Spooka /*
561cbdf0ef4Spooka * Alternative cylinders include maintenance, replacement,
562cbdf0ef4Spooka * configuration description areas, etc.
563cbdf0ef4Spooka */
564cbdf0ef4Spooka uint32_t d_acylinders; /* # of alt. cylinders per unit */
565cbdf0ef4Spooka
566cbdf0ef4Spooka /* hardware characteristics: */
567cbdf0ef4Spooka /*
568cbdf0ef4Spooka * d_interleave, d_trackskew and d_cylskew describe perturbations
569cbdf0ef4Spooka * in the media format used to compensate for a slow controller.
570cbdf0ef4Spooka * Interleave is physical sector interleave, set up by the
571cbdf0ef4Spooka * formatter or controller when formatting. When interleaving is
572cbdf0ef4Spooka * in use, logically adjacent sectors are not physically
573cbdf0ef4Spooka * contiguous, but instead are separated by some number of
574cbdf0ef4Spooka * sectors. It is specified as the ratio of physical sectors
575cbdf0ef4Spooka * traversed per logical sector. Thus an interleave of 1:1
576cbdf0ef4Spooka * implies contiguous layout, while 2:1 implies that logical
577cbdf0ef4Spooka * sector 0 is separated by one sector from logical sector 1.
578cbdf0ef4Spooka * d_trackskew is the offset of sector 0 on track N relative to
579cbdf0ef4Spooka * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew
580cbdf0ef4Spooka * is the offset of sector 0 on cylinder N relative to sector 0
581cbdf0ef4Spooka * on cylinder N-1.
582cbdf0ef4Spooka */
583cbdf0ef4Spooka uint16_t d_rpm; /* rotational speed */
584cbdf0ef4Spooka uint16_t d_interleave; /* hardware sector interleave */
585cbdf0ef4Spooka uint16_t d_trackskew; /* sector 0 skew, per track */
586cbdf0ef4Spooka uint16_t d_cylskew; /* sector 0 skew, per cylinder */
587cbdf0ef4Spooka uint32_t d_headswitch; /* head switch time, usec */
588cbdf0ef4Spooka uint32_t d_trkseek; /* track-to-track seek, usec */
589cbdf0ef4Spooka uint32_t d_flags; /* generic flags */
590cbdf0ef4Spooka #define NDDATA 5
591cbdf0ef4Spooka uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
592cbdf0ef4Spooka #define NSPARE 5
593cbdf0ef4Spooka uint32_t d_spare[NSPARE]; /* reserved for future use */
594cbdf0ef4Spooka uint32_t d_magic2; /* the magic number (again) */
595cbdf0ef4Spooka uint16_t d_checksum; /* xor of data incl. partitions */
596cbdf0ef4Spooka
597cbdf0ef4Spooka /* filesystem and partition information: */
598cbdf0ef4Spooka uint16_t d_npartitions; /* number of partitions in following */
599cbdf0ef4Spooka uint32_t d_bbsize; /* size of boot area at sn0, bytes */
600cbdf0ef4Spooka uint32_t d_sbsize; /* max size of fs superblock, bytes */
601cbdf0ef4Spooka struct rumpserver_partition { /* the partition table */
602cbdf0ef4Spooka uint32_t p_size; /* number of sectors in partition */
603cbdf0ef4Spooka uint32_t p_offset; /* starting sector */
604cbdf0ef4Spooka union {
605cbdf0ef4Spooka uint32_t fsize; /* FFS, ADOS:
606cbdf0ef4Spooka filesystem basic fragment size */
607cbdf0ef4Spooka uint32_t cdsession; /* ISO9660: session offset */
608cbdf0ef4Spooka } __partition_u2;
609cbdf0ef4Spooka #define p_fsize __partition_u2.fsize
610cbdf0ef4Spooka #define p_cdsession __partition_u2.cdsession
611cbdf0ef4Spooka uint8_t p_fstype; /* filesystem type, see below */
612cbdf0ef4Spooka uint8_t p_frag; /* filesystem fragments per block */
613cbdf0ef4Spooka union {
614cbdf0ef4Spooka uint16_t cpg; /* UFS: FS cylinders per group */
615cbdf0ef4Spooka uint16_t sgs; /* LFS: FS segment shift */
616cbdf0ef4Spooka } __partition_u1;
617cbdf0ef4Spooka #define p_cpg __partition_u1.cpg
618cbdf0ef4Spooka #define p_sgs __partition_u1.sgs
619cbdf0ef4Spooka } d_partitions[RUMPSERVER_MAXPARTITIONS]; /* actually may be more */
620cbdf0ef4Spooka };
621cbdf0ef4Spooka
622cbdf0ef4Spooka
623cbdf0ef4Spooka /* for swapping disklabel, so don't care about perf, just portability */
624cbdf0ef4Spooka #define bs32(x) \
625cbdf0ef4Spooka ((((x) & 0xff000000) >> 24)| \
626cbdf0ef4Spooka (((x) & 0x00ff0000) >> 8) | \
627cbdf0ef4Spooka (((x) & 0x0000ff00) << 8) | \
628cbdf0ef4Spooka (((x) & 0x000000ff) << 24))
629cbdf0ef4Spooka #define bs16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
630cbdf0ef4Spooka
631cbdf0ef4Spooka /*
632cbdf0ef4Spooka * From:
633cbdf0ef4Spooka * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
634cbdf0ef4Spooka */
635cbdf0ef4Spooka
636cbdf0ef4Spooka /*-
637cbdf0ef4Spooka * Copyright (c) 1991, 1993
638cbdf0ef4Spooka * The Regents of the University of California. All rights reserved.
639cbdf0ef4Spooka *
640cbdf0ef4Spooka * Redistribution and use in source and binary forms, with or without
641cbdf0ef4Spooka * modification, are permitted provided that the following conditions
642cbdf0ef4Spooka * are met:
643cbdf0ef4Spooka * 1. Redistributions of source code must retain the above copyright
644cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer.
645cbdf0ef4Spooka * 2. Redistributions in binary form must reproduce the above copyright
646cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer in the
647cbdf0ef4Spooka * documentation and/or other materials provided with the distribution.
648cbdf0ef4Spooka * 3. Neither the name of the University nor the names of its contributors
649cbdf0ef4Spooka * may be used to endorse or promote products derived from this software
650cbdf0ef4Spooka * without specific prior written permission.
651cbdf0ef4Spooka *
652cbdf0ef4Spooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
653cbdf0ef4Spooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
654cbdf0ef4Spooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
655cbdf0ef4Spooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
656cbdf0ef4Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
657cbdf0ef4Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
658cbdf0ef4Spooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
659cbdf0ef4Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
660cbdf0ef4Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
661cbdf0ef4Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
662cbdf0ef4Spooka * SUCH DAMAGE.
663cbdf0ef4Spooka */
664cbdf0ef4Spooka
665cbdf0ef4Spooka static uint16_t
rs_dl_dkcksum(struct rumpserver_disklabel * lp,int imswapped)666cbdf0ef4Spooka rs_dl_dkcksum(struct rumpserver_disklabel *lp, int imswapped)
667cbdf0ef4Spooka {
668cbdf0ef4Spooka uint16_t *start, *end;
669cbdf0ef4Spooka uint16_t sum;
670cbdf0ef4Spooka uint16_t npart;
671cbdf0ef4Spooka
672cbdf0ef4Spooka if (imswapped)
673cbdf0ef4Spooka npart = bs16(lp->d_npartitions);
674cbdf0ef4Spooka else
675cbdf0ef4Spooka npart = lp->d_npartitions;
676cbdf0ef4Spooka
677cbdf0ef4Spooka sum = 0;
678cbdf0ef4Spooka start = (uint16_t *)(void *)lp;
679cbdf0ef4Spooka end = (uint16_t *)(void *)&lp->d_partitions[npart];
680cbdf0ef4Spooka while (start < end) {
681cbdf0ef4Spooka if (imswapped)
682cbdf0ef4Spooka sum ^= bs16(*start);
683cbdf0ef4Spooka else
684cbdf0ef4Spooka sum ^= *start;
685cbdf0ef4Spooka start++;
686cbdf0ef4Spooka }
687cbdf0ef4Spooka return (sum);
688cbdf0ef4Spooka }
689cbdf0ef4Spooka
690cbdf0ef4Spooka /*
691cbdf0ef4Spooka * From:
692cbdf0ef4Spooka * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
693cbdf0ef4Spooka */
694cbdf0ef4Spooka
695cbdf0ef4Spooka /*-
696cbdf0ef4Spooka * Copyright (c) 2002 The NetBSD Foundation, Inc.
697cbdf0ef4Spooka * All rights reserved.
698cbdf0ef4Spooka *
699cbdf0ef4Spooka * This code is derived from software contributed to The NetBSD Foundation
700cbdf0ef4Spooka * by Roland C. Dowdeswell.
701cbdf0ef4Spooka *
702cbdf0ef4Spooka * Redistribution and use in source and binary forms, with or without
703cbdf0ef4Spooka * modification, are permitted provided that the following conditions
704cbdf0ef4Spooka * are met:
705cbdf0ef4Spooka * 1. Redistributions of source code must retain the above copyright
706cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer.
707cbdf0ef4Spooka * 2. Redistributions in binary form must reproduce the above copyright
708cbdf0ef4Spooka * notice, this list of conditions and the following disclaimer in the
709cbdf0ef4Spooka * documentation and/or other materials provided with the distribution.
710cbdf0ef4Spooka *
711cbdf0ef4Spooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
712cbdf0ef4Spooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
713cbdf0ef4Spooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
714cbdf0ef4Spooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
715cbdf0ef4Spooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
716cbdf0ef4Spooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
717cbdf0ef4Spooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
718cbdf0ef4Spooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
719cbdf0ef4Spooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
720cbdf0ef4Spooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
721cbdf0ef4Spooka * POSSIBILITY OF SUCH DAMAGE.
722cbdf0ef4Spooka */
723cbdf0ef4Spooka
724cbdf0ef4Spooka static int
rs_dl_scan(struct rumpserver_disklabel * lp,int * isswapped,char * buf,size_t buflen)725cbdf0ef4Spooka rs_dl_scan(struct rumpserver_disklabel *lp, int *isswapped,
726cbdf0ef4Spooka char *buf, size_t buflen)
727cbdf0ef4Spooka {
728cbdf0ef4Spooka size_t i;
729cbdf0ef4Spooka int imswapped;
730cbdf0ef4Spooka uint16_t npart;
731cbdf0ef4Spooka
732cbdf0ef4Spooka /* scan for the correct magic numbers. */
733cbdf0ef4Spooka
734cbdf0ef4Spooka for (i=0; i <= buflen - sizeof(*lp); i += 4) {
735cbdf0ef4Spooka memcpy(lp, buf + i, sizeof(*lp));
736cbdf0ef4Spooka if (lp->d_magic == RUMPSERVER_DISKMAGIC &&
737cbdf0ef4Spooka lp->d_magic2 == RUMPSERVER_DISKMAGIC) {
738cbdf0ef4Spooka imswapped = 0;
739cbdf0ef4Spooka goto sanity;
740cbdf0ef4Spooka }
741cbdf0ef4Spooka if (lp->d_magic == bs32(RUMPSERVER_DISKMAGIC) &&
742cbdf0ef4Spooka lp->d_magic2 == bs32(RUMPSERVER_DISKMAGIC)) {
743cbdf0ef4Spooka imswapped = 1;
744cbdf0ef4Spooka goto sanity;
745cbdf0ef4Spooka }
746cbdf0ef4Spooka }
747cbdf0ef4Spooka
748cbdf0ef4Spooka return 1;
749cbdf0ef4Spooka
750cbdf0ef4Spooka sanity:
751cbdf0ef4Spooka if (imswapped)
752cbdf0ef4Spooka npart = bs16(lp->d_npartitions);
753cbdf0ef4Spooka else
754cbdf0ef4Spooka npart = lp->d_npartitions;
755cbdf0ef4Spooka /* we've found something, let's sanity check it */
756cbdf0ef4Spooka if (npart > RUMPSERVER_MAXPARTITIONS
757cbdf0ef4Spooka || rs_dl_dkcksum(lp, imswapped))
758cbdf0ef4Spooka return 1;
759cbdf0ef4Spooka
760cbdf0ef4Spooka *isswapped = imswapped;
761cbdf0ef4Spooka return 0;
762cbdf0ef4Spooka }
763cbdf0ef4Spooka
764cbdf0ef4Spooka static void
processlabel(int sflag,int fd,int partition,off_t * foffp,off_t * flenp)765cbdf0ef4Spooka processlabel(int sflag, int fd, int partition, off_t *foffp, off_t *flenp)
766cbdf0ef4Spooka {
767cbdf0ef4Spooka struct rumpserver_disklabel dl;
768cbdf0ef4Spooka char buf[1<<16];
769cbdf0ef4Spooka uint32_t foffset, flen;
770cbdf0ef4Spooka int imswapped;
771cbdf0ef4Spooka
772cbdf0ef4Spooka if (pread(fd, buf, sizeof(buf), 0) == -1)
773cbdf0ef4Spooka die(sflag, errno, "could not read disk device");
774cbdf0ef4Spooka if (rs_dl_scan(&dl, &imswapped, buf, sizeof(buf)))
775cbdf0ef4Spooka die(sflag, ENOENT, "disklabel not found");
776cbdf0ef4Spooka
777cbdf0ef4Spooka if (partition >= dl.d_npartitions)
778cbdf0ef4Spooka die(sflag, ENOENT, "partition not available");
779cbdf0ef4Spooka
780cbdf0ef4Spooka foffset = dl.d_partitions[partition].p_offset << RUMPSERVER_DEVSHIFT;
781cbdf0ef4Spooka flen = dl.d_partitions[partition].p_size << RUMPSERVER_DEVSHIFT;
782cbdf0ef4Spooka if (imswapped) {
783cbdf0ef4Spooka foffset = bs32(foffset);
784cbdf0ef4Spooka flen = bs32(flen);
785cbdf0ef4Spooka }
786cbdf0ef4Spooka
787cbdf0ef4Spooka *foffp = (off_t)foffset;
788cbdf0ef4Spooka *flenp = (off_t)flen;
789cbdf0ef4Spooka }
790