Home | History | Annotate | Line # | Download | only in rump_allserver
rump_allserver.c revision 1.13.2.1
      1  1.13.2.1  bouyer /*	$NetBSD: rump_allserver.c,v 1.13.2.1 2011/02/08 16:20:12 bouyer Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*-
      4  1.13.2.1  bouyer  * Copyright (c) 2010, 2011 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.13.2.1  bouyer __RCSID("$NetBSD: rump_allserver.c,v 1.13.2.1 2011/02/08 16:20:12 bouyer Exp $");
     31       1.1   pooka #endif /* !lint */
     32       1.1   pooka 
     33       1.1   pooka #include <sys/types.h>
     34  1.13.2.1  bouyer #include <sys/disklabel.h>
     35       1.4   pooka #include <sys/signal.h>
     36       1.7   pooka #include <sys/module.h>
     37       1.1   pooka 
     38       1.1   pooka #include <rump/rump.h>
     39       1.4   pooka #include <rump/rump_syscalls.h>
     40       1.1   pooka 
     41       1.6   pooka #include <dlfcn.h>
     42       1.1   pooka #include <err.h>
     43       1.1   pooka #include <errno.h>
     44       1.9   pooka #include <fcntl.h>
     45       1.4   pooka #include <semaphore.h>
     46       1.1   pooka #include <stdio.h>
     47       1.1   pooka #include <stdlib.h>
     48       1.1   pooka #include <string.h>
     49       1.1   pooka #include <unistd.h>
     50  1.13.2.1  bouyer #include <util.h>
     51       1.1   pooka 
     52       1.1   pooka static void
     53       1.1   pooka usage(void)
     54       1.1   pooka {
     55       1.1   pooka 
     56      1.13     wiz 	fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
     57      1.12   pooka 	    "[-m modules] bindurl\n", getprogname());
     58       1.1   pooka 	exit(1);
     59       1.1   pooka }
     60       1.1   pooka 
     61       1.2   pooka static void
     62       1.2   pooka die(int sflag, int error, const char *reason)
     63       1.2   pooka {
     64       1.2   pooka 
     65       1.2   pooka 	warnx("%s: %s", reason, strerror(error));
     66       1.2   pooka 	if (!sflag)
     67       1.2   pooka 		rump_daemonize_done(error);
     68       1.2   pooka 	exit(1);
     69       1.2   pooka }
     70       1.2   pooka 
     71       1.4   pooka static sem_t sigsem;
     72       1.4   pooka static void
     73       1.4   pooka sigreboot(int sig)
     74       1.4   pooka {
     75       1.4   pooka 
     76       1.4   pooka 	sem_post(&sigsem);
     77       1.4   pooka }
     78       1.4   pooka 
     79       1.9   pooka static const char *const disktokens[] = {
     80       1.9   pooka #define DKEY 0
     81       1.9   pooka 	"key",
     82       1.9   pooka #define DFILE 1
     83       1.9   pooka 	"hostpath",
     84       1.9   pooka #define DSIZE 2
     85       1.9   pooka 	"size",
     86  1.13.2.1  bouyer #define DOFFSET 3
     87  1.13.2.1  bouyer 	"offset",
     88  1.13.2.1  bouyer #define DLABEL 4
     89  1.13.2.1  bouyer 	"disklabel",
     90       1.9   pooka 	NULL
     91       1.9   pooka };
     92       1.9   pooka 
     93       1.9   pooka struct etfsreg {
     94       1.9   pooka 	const char *key;
     95       1.9   pooka 	const char *hostpath;
     96       1.9   pooka 	off_t flen;
     97  1.13.2.1  bouyer 	off_t foffset;
     98  1.13.2.1  bouyer 	char partition;
     99       1.9   pooka 	enum rump_etfs_type type;
    100       1.9   pooka };
    101       1.9   pooka 
    102       1.1   pooka int
    103       1.1   pooka main(int argc, char *argv[])
    104       1.1   pooka {
    105       1.2   pooka 	const char *serverurl;
    106       1.7   pooka 	char **modarray = NULL;
    107       1.7   pooka 	unsigned nmods = 0, curmod = 0, i;
    108       1.9   pooka 	struct etfsreg *etfs = NULL;
    109       1.9   pooka 	unsigned netfs = 0, curetfs = 0;
    110       1.1   pooka 	int error;
    111       1.2   pooka 	int ch, sflag;
    112      1.12   pooka 	int ncpu;
    113       1.1   pooka 
    114       1.1   pooka 	setprogname(argv[0]);
    115       1.1   pooka 
    116       1.2   pooka 	sflag = 0;
    117      1.12   pooka 	while ((ch = getopt(argc, argv, "c:d:l:m:s")) != -1) {
    118       1.2   pooka 		switch (ch) {
    119      1.12   pooka 		case 'c':
    120      1.12   pooka 			ncpu = atoi(optarg);
    121      1.12   pooka 			/* XXX: MAXCPUS is from host, not from kernel */
    122      1.12   pooka 			if (ncpu < 1 || ncpu > MAXCPUS)
    123      1.12   pooka 				err(1, "CPU count needs to be between "
    124      1.12   pooka 				    "1 and %d\n", MAXCPUS);
    125      1.12   pooka 			setenv("RUMP_NCPU", optarg, 1);
    126      1.12   pooka 			break;
    127       1.9   pooka 		case 'd': {
    128       1.9   pooka 			char *options, *value;
    129       1.9   pooka 			char *key, *hostpath;
    130  1.13.2.1  bouyer 			long long flen, foffset;
    131  1.13.2.1  bouyer 			char partition;
    132       1.9   pooka 
    133  1.13.2.1  bouyer 			flen = foffset = 0;
    134  1.13.2.1  bouyer 			partition = 0;
    135       1.9   pooka 			key = hostpath = NULL;
    136       1.9   pooka 			options = optarg;
    137       1.9   pooka 			while (*options) {
    138       1.9   pooka 				switch (getsubopt(&options,
    139       1.9   pooka 				    __UNCONST(disktokens), &value)) {
    140       1.9   pooka 				case DKEY:
    141      1.11   pooka 					if (key != NULL) {
    142      1.11   pooka 						fprintf(stderr,
    143      1.11   pooka 						    "key already given\n");
    144      1.11   pooka 						usage();
    145      1.11   pooka 					}
    146       1.9   pooka 					key = value;
    147       1.9   pooka 					break;
    148  1.13.2.1  bouyer 
    149       1.9   pooka 				case DFILE:
    150      1.11   pooka 					if (hostpath != NULL) {
    151      1.11   pooka 						fprintf(stderr,
    152      1.11   pooka 						    "hostpath already given\n");
    153      1.11   pooka 						usage();
    154      1.11   pooka 					}
    155       1.9   pooka 					hostpath = value;
    156       1.9   pooka 					break;
    157  1.13.2.1  bouyer 
    158       1.9   pooka 				case DSIZE:
    159      1.11   pooka 					if (flen != 0) {
    160      1.11   pooka 						fprintf(stderr,
    161      1.11   pooka 						    "size already given\n");
    162      1.11   pooka 						usage();
    163      1.11   pooka 					}
    164       1.9   pooka 					/* XXX: off_t max? */
    165       1.9   pooka 					flen = strsuftoll("-d size", value,
    166       1.9   pooka 					    0, LLONG_MAX);
    167       1.9   pooka 					break;
    168  1.13.2.1  bouyer 				case DOFFSET:
    169  1.13.2.1  bouyer 					if (foffset != 0) {
    170  1.13.2.1  bouyer 						fprintf(stderr,
    171  1.13.2.1  bouyer 						    "offset already given\n");
    172  1.13.2.1  bouyer 						usage();
    173  1.13.2.1  bouyer 					}
    174  1.13.2.1  bouyer 					/* XXX: off_t max? */
    175  1.13.2.1  bouyer 					foffset = strsuftoll("-d offset", value,
    176  1.13.2.1  bouyer 					    0, LLONG_MAX);
    177  1.13.2.1  bouyer 					break;
    178  1.13.2.1  bouyer 
    179  1.13.2.1  bouyer 				case DLABEL:
    180  1.13.2.1  bouyer 					if (foffset != 0 || flen != 0) {
    181  1.13.2.1  bouyer 						fprintf(stderr,
    182  1.13.2.1  bouyer 						    "disklabel needs to be "
    183  1.13.2.1  bouyer 						    "used alone\n");
    184  1.13.2.1  bouyer 						usage();
    185  1.13.2.1  bouyer 					}
    186  1.13.2.1  bouyer 					if (strlen(value) != 1 ||
    187  1.13.2.1  bouyer 					    *value < 'a' || *value > 'z') {
    188  1.13.2.1  bouyer 						fprintf(stderr,
    189  1.13.2.1  bouyer 						    "invalid label part\n");
    190  1.13.2.1  bouyer 						usage();
    191  1.13.2.1  bouyer 					}
    192  1.13.2.1  bouyer 					partition = *value;
    193  1.13.2.1  bouyer 					break;
    194  1.13.2.1  bouyer 
    195       1.9   pooka 				default:
    196       1.9   pooka 					fprintf(stderr, "invalid dtoken\n");
    197       1.9   pooka 					usage();
    198       1.9   pooka 					break;
    199       1.9   pooka 				}
    200       1.9   pooka 			}
    201       1.9   pooka 
    202  1.13.2.1  bouyer 			if (key == NULL || hostpath == NULL ||
    203  1.13.2.1  bouyer 			    (flen == 0 && partition == 0)) {
    204       1.9   pooka 				fprintf(stderr, "incomplete drivespec\n");
    205       1.9   pooka 				usage();
    206       1.9   pooka 			}
    207       1.9   pooka 
    208       1.9   pooka 			if (netfs - curetfs == 0) {
    209       1.9   pooka 				etfs = realloc(etfs, (netfs+16)*sizeof(*etfs));
    210       1.9   pooka 				if (etfs == NULL)
    211       1.9   pooka 					err(1, "realloc etfs");
    212       1.9   pooka 				netfs += 16;
    213       1.9   pooka 			}
    214       1.9   pooka 
    215       1.9   pooka 			etfs[curetfs].key = key;
    216       1.9   pooka 			etfs[curetfs].hostpath = hostpath;
    217       1.9   pooka 			etfs[curetfs].flen = flen;
    218  1.13.2.1  bouyer 			etfs[curetfs].foffset = foffset;
    219  1.13.2.1  bouyer 			etfs[curetfs].partition = partition;
    220       1.9   pooka 			etfs[curetfs].type = RUMP_ETFS_BLK;
    221       1.9   pooka 			curetfs++;
    222       1.9   pooka 
    223       1.9   pooka 			break;
    224       1.9   pooka 		}
    225       1.6   pooka 		case 'l':
    226       1.8   pooka 			if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
    227       1.8   pooka 				char pb[MAXPATHLEN];
    228       1.8   pooka 				/* try to mimic linker -l syntax */
    229       1.8   pooka 
    230       1.8   pooka 				snprintf(pb, sizeof(pb), "lib%s.so", optarg);
    231       1.8   pooka 				if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
    232       1.8   pooka 					errx(1, "dlopen %s failed: %s",
    233       1.8   pooka 					    pb, dlerror());
    234       1.8   pooka 				}
    235       1.8   pooka 			}
    236       1.6   pooka 			break;
    237       1.7   pooka 		case 'm':
    238       1.7   pooka 			if (nmods - curmod == 0) {
    239       1.7   pooka 				modarray = realloc(modarray,
    240       1.7   pooka 				    (nmods+16) * sizeof(char *));
    241       1.7   pooka 				if (modarray == NULL)
    242       1.7   pooka 					err(1, "realloc");
    243       1.7   pooka 				nmods += 16;
    244       1.7   pooka 			}
    245       1.7   pooka 			modarray[curmod++] = optarg;
    246       1.7   pooka 			break;
    247       1.2   pooka 		case 's':
    248       1.2   pooka 			sflag = 1;
    249       1.2   pooka 			break;
    250       1.2   pooka 		default:
    251       1.2   pooka 			usage();
    252       1.2   pooka 			/*NOTREACHED*/
    253       1.2   pooka 		}
    254       1.2   pooka 	}
    255       1.2   pooka 
    256       1.2   pooka 	argc -= optind;
    257       1.2   pooka 	argv += optind;
    258       1.2   pooka 
    259       1.2   pooka 	if (argc != 1)
    260       1.1   pooka 		usage();
    261       1.1   pooka 
    262       1.2   pooka 	serverurl = argv[0];
    263       1.2   pooka 
    264       1.2   pooka 	if (!sflag) {
    265       1.2   pooka 		error = rump_daemonize_begin();
    266       1.2   pooka 		if (error)
    267       1.2   pooka 			errx(1, "rump daemonize: %s", strerror(error));
    268       1.2   pooka 	}
    269       1.2   pooka 
    270       1.1   pooka 	error = rump_init();
    271       1.1   pooka 	if (error)
    272       1.2   pooka 		die(sflag, error, "rump init failed");
    273       1.7   pooka 
    274       1.9   pooka 	/* load modules */
    275       1.7   pooka 	for (i = 0; i < curmod; i++) {
    276       1.7   pooka 		struct modctl_load ml;
    277       1.7   pooka 
    278       1.7   pooka #define ETFSKEY "/module.mod"
    279       1.7   pooka 		if ((error = rump_pub_etfs_register(ETFSKEY,
    280       1.7   pooka 		    modarray[0], RUMP_ETFS_REG)) != 0)
    281       1.7   pooka 			die(sflag, error, "module etfs register failed");
    282       1.7   pooka 		memset(&ml, 0, sizeof(ml));
    283       1.7   pooka 		ml.ml_filename = ETFSKEY;
    284       1.7   pooka 		if (rump_sys_modctl(MODCTL_LOAD, &ml) == -1)
    285       1.7   pooka 			die(sflag, errno, "module load failed");
    286       1.7   pooka 		rump_pub_etfs_remove(ETFSKEY);
    287       1.9   pooka #undef ETFSKEY
    288       1.9   pooka 	}
    289       1.9   pooka 
    290       1.9   pooka 	/* register host drives */
    291       1.9   pooka 	for (i = 0; i < curetfs; i++) {
    292  1.13.2.1  bouyer 		char buf[1<<16];
    293  1.13.2.1  bouyer 		struct disklabel dl;
    294  1.13.2.1  bouyer 		struct stat sb;
    295  1.13.2.1  bouyer 		off_t foffset, flen, fendoff;
    296       1.9   pooka 		int fd;
    297       1.9   pooka 
    298  1.13.2.1  bouyer 		fd = open(etfs[i].hostpath, O_RDWR | O_CREAT, 0644);
    299       1.9   pooka 		if (fd == -1)
    300  1.13.2.1  bouyer 			die(sflag, errno, "etfs hostpath create");
    301  1.13.2.1  bouyer 
    302  1.13.2.1  bouyer 		if (etfs[i].partition) {
    303  1.13.2.1  bouyer 			int partition = etfs[i].partition - 'a';
    304  1.13.2.1  bouyer 
    305  1.13.2.1  bouyer 			pread(fd, buf, sizeof(buf), 0);
    306  1.13.2.1  bouyer 			if (disklabel_scan(&dl, buf, sizeof(buf)))
    307  1.13.2.1  bouyer 				die(sflag, ENOENT, "disklabel not found");
    308  1.13.2.1  bouyer 
    309  1.13.2.1  bouyer 			if (partition >= dl.d_npartitions)
    310  1.13.2.1  bouyer 				die(sflag, ENOENT, "partition not available");
    311  1.13.2.1  bouyer 
    312  1.13.2.1  bouyer 			foffset = dl.d_partitions[partition].p_offset
    313  1.13.2.1  bouyer 			    << DEV_BSHIFT;
    314  1.13.2.1  bouyer 			flen = dl.d_partitions[partition].p_size
    315  1.13.2.1  bouyer 			    << DEV_BSHIFT;
    316  1.13.2.1  bouyer 		} else {
    317  1.13.2.1  bouyer 			foffset = etfs[i].foffset;
    318  1.13.2.1  bouyer 			flen = etfs[i].flen;
    319  1.13.2.1  bouyer 		}
    320  1.13.2.1  bouyer 		fendoff = foffset + flen;
    321  1.13.2.1  bouyer 
    322  1.13.2.1  bouyer 		if (fstat(fd, &sb) == -1)
    323  1.13.2.1  bouyer 			die(sflag, errno, "fstat etfs hostpath");
    324  1.13.2.1  bouyer 		if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
    325  1.13.2.1  bouyer 			if (ftruncate(fd, fendoff) == -1)
    326  1.13.2.1  bouyer 				die(sflag, errno, "truncate");
    327  1.13.2.1  bouyer 		}
    328       1.9   pooka 		close(fd);
    329       1.9   pooka 
    330  1.13.2.1  bouyer 		if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
    331  1.13.2.1  bouyer 		    etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
    332       1.9   pooka 			die(sflag, error, "etfs register");
    333       1.7   pooka 	}
    334       1.7   pooka 
    335       1.2   pooka 	error = rump_init_server(serverurl);
    336       1.1   pooka 	if (error)
    337       1.2   pooka 		die(sflag, error, "rump server init failed");
    338       1.2   pooka 
    339       1.2   pooka 	if (!sflag)
    340       1.3   pooka 		rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
    341       1.2   pooka 
    342       1.4   pooka 	sem_init(&sigsem, 0, 0);
    343       1.4   pooka 	signal(SIGTERM, sigreboot);
    344       1.4   pooka 	signal(SIGINT, sigreboot);
    345       1.4   pooka 	sem_wait(&sigsem);
    346       1.4   pooka 
    347       1.4   pooka 	rump_sys_reboot(0, NULL);
    348       1.5   pooka 	/*NOTREACHED*/
    349       1.5   pooka 
    350       1.4   pooka 	return 0;
    351       1.1   pooka }
    352