xref: /minix3/tests/fs/ffs/h_ffs_server.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: h_ffs_server.c,v 1.2 2012/08/24 20:25:50 jmmv Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * rump server for advanced quota tests
5*11be35a1SLionel Sambuc  */
6*11be35a1SLionel Sambuc 
7*11be35a1SLionel Sambuc #include "../common/h_fsmacros.h"
8*11be35a1SLionel Sambuc 
9*11be35a1SLionel Sambuc #include <err.h>
10*11be35a1SLionel Sambuc #include <semaphore.h>
11*11be35a1SLionel Sambuc #include <sys/types.h>
12*11be35a1SLionel Sambuc #include <sys/mount.h>
13*11be35a1SLionel Sambuc 
14*11be35a1SLionel Sambuc #include <stdlib.h>
15*11be35a1SLionel Sambuc #include <unistd.h>
16*11be35a1SLionel Sambuc 
17*11be35a1SLionel Sambuc #include <ufs/ufs/ufsmount.h>
18*11be35a1SLionel Sambuc 
19*11be35a1SLionel Sambuc #include <rump/rump.h>
20*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
21*11be35a1SLionel Sambuc 
22*11be35a1SLionel Sambuc int background = 0;
23*11be35a1SLionel Sambuc 
24*11be35a1SLionel Sambuc static void
usage(void)25*11be35a1SLionel Sambuc usage(void)
26*11be35a1SLionel Sambuc {
27*11be35a1SLionel Sambuc 	fprintf(stderr, "usage: %s [-b] [-l] diskimage bindurl\n",
28*11be35a1SLionel Sambuc 	    getprogname());
29*11be35a1SLionel Sambuc 	exit(1);
30*11be35a1SLionel Sambuc }
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc static void
die(const char * reason,int error)33*11be35a1SLionel Sambuc die(const char *reason, int error)
34*11be35a1SLionel Sambuc {
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc 	warnx("%s: %s", reason, strerror(error));
37*11be35a1SLionel Sambuc 	if (background)
38*11be35a1SLionel Sambuc 		rump_daemonize_done(error);
39*11be35a1SLionel Sambuc 	exit(1);
40*11be35a1SLionel Sambuc }
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc static sem_t sigsem;
43*11be35a1SLionel Sambuc static void
sigreboot(int sig)44*11be35a1SLionel Sambuc sigreboot(int sig)
45*11be35a1SLionel Sambuc {
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc 	sem_post(&sigsem);
48*11be35a1SLionel Sambuc }
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc int
main(int argc,char ** argv)51*11be35a1SLionel Sambuc main(int argc, char **argv)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc 	int error;
54*11be35a1SLionel Sambuc 	struct ufs_args uargs;
55*11be35a1SLionel Sambuc 	const char *filename;
56*11be35a1SLionel Sambuc 	const char *serverurl;
57*11be35a1SLionel Sambuc 	int log = 0;
58*11be35a1SLionel Sambuc 	int ch;
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc 	while ((ch = getopt(argc, argv, "bl")) != -1) {
61*11be35a1SLionel Sambuc 		switch(ch) {
62*11be35a1SLionel Sambuc 		case 'b':
63*11be35a1SLionel Sambuc 			background = 1;
64*11be35a1SLionel Sambuc 			break;
65*11be35a1SLionel Sambuc 		case 'l':
66*11be35a1SLionel Sambuc 			log = 1;
67*11be35a1SLionel Sambuc 			break;
68*11be35a1SLionel Sambuc 		default:
69*11be35a1SLionel Sambuc 			usage();
70*11be35a1SLionel Sambuc 		}
71*11be35a1SLionel Sambuc 	}
72*11be35a1SLionel Sambuc 	argc -= optind;
73*11be35a1SLionel Sambuc 	argv += optind;
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc 	if (argc != 2)
76*11be35a1SLionel Sambuc 		usage();
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc 	filename = argv[0];
79*11be35a1SLionel Sambuc 	serverurl = argv[1];
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc 	if (background) {
82*11be35a1SLionel Sambuc 		error = rump_daemonize_begin();
83*11be35a1SLionel Sambuc 		if (error)
84*11be35a1SLionel Sambuc 			errx(1, "rump daemonize: %s", strerror(error));
85*11be35a1SLionel Sambuc 	}
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 	error = rump_init();
88*11be35a1SLionel Sambuc 	if (error)
89*11be35a1SLionel Sambuc 		die("rump init failed", error);
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 	if (rump_sys_mkdir(FSTEST_MNTNAME, 0777) == -1)
92*11be35a1SLionel Sambuc 		die("mount point create", errno);
93*11be35a1SLionel Sambuc 	rump_pub_etfs_register("/diskdev", filename, RUMP_ETFS_BLK);
94*11be35a1SLionel Sambuc 	uargs.fspec = __UNCONST("/diskdev");
95*11be35a1SLionel Sambuc 	if (rump_sys_mount(MOUNT_FFS, FSTEST_MNTNAME, (log) ? MNT_LOG : 0,
96*11be35a1SLionel Sambuc 	    &uargs, sizeof(uargs)) == -1)
97*11be35a1SLionel Sambuc 		die("mount ffs", errno);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 	error = rump_init_server(serverurl);
100*11be35a1SLionel Sambuc 	if (error)
101*11be35a1SLionel Sambuc 		die("rump server init failed", error);
102*11be35a1SLionel Sambuc 	if (background)
103*11be35a1SLionel Sambuc 		rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 	sem_init(&sigsem, 0, 0);
106*11be35a1SLionel Sambuc 	signal(SIGTERM, sigreboot);
107*11be35a1SLionel Sambuc 	signal(SIGINT, sigreboot);
108*11be35a1SLionel Sambuc 	sem_wait(&sigsem);
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc 	rump_sys_reboot(0, NULL);
111*11be35a1SLionel Sambuc 	/*NOTREACHED*/
112*11be35a1SLionel Sambuc 	return 0;
113*11be35a1SLionel Sambuc }
114