Home | History | Annotate | Line # | Download | only in rump_syspuffs
rump_syspuffs.c revision 1.2
      1 /*	$NetBSD: rump_syspuffs.c,v 1.2 2008/08/08 16:59:02 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Research Foundation of Helsinki University of Technology
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/types.h>
     32 #include <sys/mount.h>
     33 #include <sys/socket.h>
     34 #include <sys/syslimits.h>
     35 
     36 #include <assert.h>
     37 #include <err.h>
     38 #include <paths.h>
     39 #include <puffs.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <unistd.h>
     43 
     44 #include <rump/p2k.h>
     45 
     46 #include "puffs_rumpglue.h"
     47 
     48 static void
     49 usage(void)
     50 {
     51 
     52 	errx(1, "usage: %s file_server fs_opts", getprogname());
     53 }
     54 
     55 int
     56 main(int argc, char *argv[])
     57 {
     58 	struct puffs_kargs kargs;
     59 	extern char **environ;
     60 	char *mntpath, *fromname, *shcmd;
     61 	size_t len;
     62 	char comfd[16];
     63 	char *newargv[4];
     64 	int sv[2];
     65 	int mntflags, pflags, rv, i;
     66 
     67 #if 1
     68 	extern int puffsdebug;
     69 	extern int putterdebug;
     70 
     71 	puffsdebug = putterdebug = 1;
     72 #endif
     73 
     74 	setprogname(argv[0]);
     75 
     76 	if (argc < 2)
     77 		usage();
     78 
     79 	/* Create sucketpair for communication with the real file server */
     80 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
     81 		err(1, "socketpair");
     82 	snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
     83 	if (setenv("PUFFS_COMFD", comfd, 1) == -1)
     84 		err(1, "setenv");
     85 
     86 	switch (fork()) {
     87 	case 0:
     88 		shcmd = malloc(ARG_MAX);
     89 		*shcmd = 0;
     90 		for (i = 1; i < argc; i++) {
     91 			strcat(shcmd, argv[i]);
     92 			strcat(shcmd, " ");
     93 		}
     94 		newargv[0] = _PATH_BSHELL;
     95 		newargv[1] = "-c";
     96 		newargv[2] = shcmd;
     97 		newargv[3] = '\0';
     98 		if (execve(_PATH_BSHELL, newargv, environ) == -1)
     99 			err(1, "execvp");
    100 		/*NOTREACHED*/
    101 	case -1:
    102 		err(1, "fork");
    103 		/*NOTREACHED*/
    104 	default:
    105 		unsetenv("PUFFS_COMFD");
    106 		break;
    107 	}
    108 
    109 	/* read args */
    110 	if (read(sv[1], &len, sizeof(len)) != sizeof(len))
    111 		err(1, "mp 1");
    112 	mntpath = malloc(len);
    113 	if (mntpath == NULL)
    114 		err(1, "malloc %zu", len);
    115 	if (read(sv[1], mntpath, len) != len)
    116 		err(1, "mp 2");
    117 	if (read(sv[1], &len, sizeof(len)) != sizeof(len))
    118 		err(1, "fn 1");
    119 	fromname = malloc(len);
    120 	if (fromname == NULL)
    121 		err(1, "malloc %zu", len);
    122 	if (read(sv[1], fromname, len) != len)
    123 		err(1, "fn 2");
    124 	if (read(sv[1], &mntflags, sizeof(mntflags)) != sizeof(mntflags))
    125 		err(1, "mntflags");
    126 	if (read(sv[1], &kargs, sizeof(kargs)) != sizeof(kargs))
    127 		err(1, "puffs_args");
    128 	if (read(sv[1], &pflags, sizeof(pflags)) != sizeof(pflags))
    129 		err(1, "pflags");
    130 
    131 	/* XXX: some adjustments */
    132 	pflags |= PUFFS_KFLAG_NOCACHE;
    133 	pflags &= ~PUFFS_FLAG_BUILDPATH;
    134 
    135 	rv = puffs_rumpglue_init(sv[1], &kargs.pa_fd);
    136 	assert(rv == 0);
    137 	rv = p2k_run_fs(MOUNT_PUFFS, fromname, mntpath, mntflags,
    138 		&kargs, sizeof(kargs), pflags);
    139 	if (rv)
    140 		err(1, "mount");
    141 
    142 	return 0;
    143 }
    144