Home | History | Annotate | Line # | Download | only in rump_allserver
rump_allserver.c revision 1.4
      1 /*	$NetBSD: rump_allserver.c,v 1.4 2010/12/12 18:32:47 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.4 2010/12/12 18:32:47 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <sys/types.h>
     34 #include <sys/signal.h>
     35 
     36 #include <rump/rump.h>
     37 #include <rump/rump_syscalls.h>
     38 
     39 #include <err.h>
     40 #include <errno.h>
     41 #include <semaphore.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 
     47 static void
     48 usage(void)
     49 {
     50 
     51 	fprintf(stderr, "usage: %s [args] bindurl\n", getprogname());
     52 	exit(1);
     53 }
     54 
     55 static void
     56 die(int sflag, int error, const char *reason)
     57 {
     58 
     59 	warnx("%s: %s", reason, strerror(error));
     60 	if (!sflag)
     61 		rump_daemonize_done(error);
     62 	exit(1);
     63 }
     64 
     65 static sem_t sigsem;
     66 static void
     67 sigreboot(int sig)
     68 {
     69 
     70 	sem_post(&sigsem);
     71 }
     72 
     73 int
     74 main(int argc, char *argv[])
     75 {
     76 	const char *serverurl;
     77 	int error;
     78 	int ch, sflag;
     79 
     80 	setprogname(argv[0]);
     81 
     82 	sflag = 0;
     83 	while ((ch = getopt(argc, argv, "s")) != -1) {
     84 		switch (ch) {
     85 		case 's':
     86 			sflag = 1;
     87 			break;
     88 		default:
     89 			usage();
     90 			/*NOTREACHED*/
     91 		}
     92 	}
     93 
     94 	argc -= optind;
     95 	argv += optind;
     96 
     97 	if (argc != 1)
     98 		usage();
     99 
    100 	serverurl = argv[0];
    101 
    102 	if (!sflag) {
    103 		error = rump_daemonize_begin();
    104 		if (error)
    105 			errx(1, "rump daemonize: %s", strerror(error));
    106 	}
    107 
    108 	error = rump_init();
    109 	if (error)
    110 		die(sflag, error, "rump init failed");
    111 	error = rump_init_server(serverurl);
    112 	if (error)
    113 		die(sflag, error, "rump server init failed");
    114 
    115 	if (!sflag)
    116 		rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
    117 
    118 	sem_init(&sigsem, 0, 0);
    119 	signal(SIGTERM, sigreboot);
    120 	signal(SIGINT, sigreboot);
    121 	sem_wait(&sigsem);
    122 
    123 	rump_sys_reboot(0, NULL);
    124 	return 0;
    125 }
    126