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