Home | History | Annotate | Line # | Download | only in rump_allserver
rump_allserver.c revision 1.7
      1 /*	$NetBSD: rump_allserver.c,v 1.7 2010/12/13 14:13:21 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.7 2010/12/13 14:13:21 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 <semaphore.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 static void
     50 usage(void)
     51 {
     52 
     53 	fprintf(stderr, "usage: %s [args] bindurl\n", getprogname());
     54 	exit(1);
     55 }
     56 
     57 static void
     58 die(int sflag, int error, const char *reason)
     59 {
     60 
     61 	warnx("%s: %s", reason, strerror(error));
     62 	if (!sflag)
     63 		rump_daemonize_done(error);
     64 	exit(1);
     65 }
     66 
     67 static sem_t sigsem;
     68 static void
     69 sigreboot(int sig)
     70 {
     71 
     72 	sem_post(&sigsem);
     73 }
     74 
     75 int
     76 main(int argc, char *argv[])
     77 {
     78 	const char *serverurl;
     79 	char **modarray = NULL;
     80 	unsigned nmods = 0, curmod = 0, i;
     81 	int error;
     82 	int ch, sflag;
     83 
     84 	setprogname(argv[0]);
     85 
     86 	sflag = 0;
     87 	while ((ch = getopt(argc, argv, "l:m:s")) != -1) {
     88 		switch (ch) {
     89 		case 'l':
     90 			if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL)
     91 				errx(1, "dlopen %s failed: %s",
     92 				    optarg, dlerror());
     93 			break;
     94 		case 'm':
     95 			if (nmods - curmod == 0) {
     96 				modarray = realloc(modarray,
     97 				    (nmods+16) * sizeof(char *));
     98 				if (modarray == NULL)
     99 					err(1, "realloc");
    100 				nmods += 16;
    101 			}
    102 			modarray[curmod++] = optarg;
    103 			break;
    104 		case 's':
    105 			sflag = 1;
    106 			break;
    107 		default:
    108 			usage();
    109 			/*NOTREACHED*/
    110 		}
    111 	}
    112 
    113 	argc -= optind;
    114 	argv += optind;
    115 
    116 	if (argc != 1)
    117 		usage();
    118 
    119 	serverurl = argv[0];
    120 
    121 	if (!sflag) {
    122 		error = rump_daemonize_begin();
    123 		if (error)
    124 			errx(1, "rump daemonize: %s", strerror(error));
    125 	}
    126 
    127 	error = rump_init();
    128 	if (error)
    129 		die(sflag, error, "rump init failed");
    130 
    131 	for (i = 0; i < curmod; i++) {
    132 		struct modctl_load ml;
    133 
    134 #define ETFSKEY "/module.mod"
    135 		if ((error = rump_pub_etfs_register(ETFSKEY,
    136 		    modarray[0], RUMP_ETFS_REG)) != 0)
    137 			die(sflag, error, "module etfs register failed");
    138 		memset(&ml, 0, sizeof(ml));
    139 		ml.ml_filename = ETFSKEY;
    140 		if (rump_sys_modctl(MODCTL_LOAD, &ml) == -1)
    141 			die(sflag, errno, "module load failed");
    142 		rump_pub_etfs_remove(ETFSKEY);
    143 	}
    144 
    145 	error = rump_init_server(serverurl);
    146 	if (error)
    147 		die(sflag, error, "rump server init failed");
    148 
    149 	if (!sflag)
    150 		rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
    151 
    152 	sem_init(&sigsem, 0, 0);
    153 	signal(SIGTERM, sigreboot);
    154 	signal(SIGINT, sigreboot);
    155 	sem_wait(&sigsem);
    156 
    157 	rump_sys_reboot(0, NULL);
    158 	/*NOTREACHED*/
    159 
    160 	return 0;
    161 }
    162