xref: /netbsd-src/usr.sbin/puffs/rump_syspuffs/rump_syspuffs.c (revision 506971ff58c130f512700bb67574a7e2fdcd2f8c)
1 /*	$NetBSD: rump_syspuffs.c,v 1.14 2023/08/03 20:45:50 andvar 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/rump.h>
45 
46 #include "rump_syspuffs.h"
47 
48 #ifndef MOUNT_NOMAIN
49 #include <rump/p2k.h>
50 
51 int
main(int argc,char * argv[])52 main(int argc, char *argv[])
53 {
54 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
55 	struct syspuffs_args args;
56 	int mntflags, rv;
57 
58 #if 0
59 	extern int rumpns_puffsdebug;
60 	extern int rumpns_putterdebug;
61 
62 	rumpns_puffsdebug = rumpns_putterdebug = 1;
63 #endif
64 
65 	setprogname(argv[0]);
66 
67 	mount_syspuffs_parseargs(argc, argv, &args, &mntflags,
68 	    canon_dev, canon_dir);
69 
70 	rv = p2k_run_fs(MOUNT_PUFFS, canon_dev, canon_dir, mntflags,
71 		&args.us_kargs, sizeof(args.us_kargs), args.us_pflags);
72 	if (rv)
73 		err(1, "mount");
74 
75 	return 0;
76 }
77 #endif /* MOUNT_NOMAIN */
78 
79 __dead static void
usage(void)80 usage(void)
81 {
82 
83 	errx(1, "%s: server server_parameters", getprogname());
84 }
85 
86 void
mount_syspuffs_parseargs(int argc,char * argv[],struct syspuffs_args * args,int * mntflags,char * canon_dev,char * canon_dir)87 mount_syspuffs_parseargs(int argc, char *argv[],
88 	struct syspuffs_args *args, int *mntflags,
89 	char *canon_dev, char *canon_dir)
90 {
91 	struct puffs_kargs *kargs = &args->us_kargs;
92 	int *pflags = &args->us_pflags;
93 	char comfd[16];
94 	int sv[2];
95 	size_t len;
96 	int rv;
97 
98 	if (argc < 2)
99 		usage();
100 
101 	/* Create socketpair for communication with the real file server */
102 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
103 		err(1, "socketpair");
104 
105 	if ((rv = rump_init()) == -1)
106 		err(1, "rump_init");
107 
108 	switch (fork()) {
109 	case 0:
110 		close(sv[1]);
111 		snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
112 		if (setenv("PUFFS_COMFD", comfd, 1) == -1)
113 			err(1, "setenv");
114 
115 		argv++;
116 		(void)execvp(argv[0], argv);
117 		err(1, "execvp");
118 		/*NOTREACHED*/
119 	case -1:
120 		err(1, "fork");
121 		/*NOTREACHED*/
122 	default:
123 		close(sv[0]);
124 		break;
125 	}
126 
127 	/* read args */
128 	if (read(sv[1], &len, sizeof(len)) != sizeof(len))
129 		err(1, "mp 1");
130 	if (len > MAXPATHLEN)
131 		err(1, "mntpath > MAXPATHLEN");
132 	if ((size_t)read(sv[1], canon_dir, len) != len)
133 		err(1, "mp 2");
134 	if (read(sv[1], &len, sizeof(len)) != sizeof(len))
135 		err(1, "fn 1");
136 	if (len > MAXPATHLEN)
137 		err(1, "devpath > MAXPATHLEN");
138 	if ((size_t)read(sv[1], canon_dev, len) != len)
139 		err(1, "fn 2");
140 	if (read(sv[1], mntflags, sizeof(*mntflags)) != sizeof(*mntflags))
141 		err(1, "mntflags");
142 	if (read(sv[1], kargs, sizeof(len)) != sizeof(len)) /* unused now */
143 		err(1, "unused len");
144 	if (read(sv[1], kargs, sizeof(*kargs)) != sizeof(*kargs))
145 		err(1, "puffs_args");
146 	if (read(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
147 		err(1, "pflags");
148 
149 	/* XXX: some adjustments */
150 	*pflags |= PUFFS_KFLAG_NOCACHE;
151 	*pflags &= ~PUFFS_FLAG_BUILDPATH;
152 
153 	rv = rump_pub_syspuffs_glueinit(sv[1], &kargs->pa_fd);
154 	assert(rv == 0);
155 }
156