perfused.c revision 1.7 1 1.7 manu /* $NetBSD: perfused.c,v 1.7 2010/09/07 02:11:04 manu Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu *
15 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
26 1.1 manu */
27 1.1 manu
28 1.1 manu #include <stdio.h>
29 1.1 manu #include <unistd.h>
30 1.1 manu #include <stdlib.h>
31 1.1 manu #include <fcntl.h>
32 1.1 manu #include <syslog.h>
33 1.1 manu #include <ctype.h>
34 1.1 manu #include <paths.h>
35 1.1 manu #include <stdarg.h>
36 1.1 manu #include <err.h>
37 1.1 manu #include <errno.h>
38 1.1 manu #include <string.h>
39 1.1 manu #include <sysexits.h>
40 1.1 manu #include <signal.h>
41 1.1 manu #include <puffs.h>
42 1.1 manu #include <sys/wait.h>
43 1.1 manu #include <sys/param.h>
44 1.1 manu #include <sys/queue.h>
45 1.1 manu #include <sys/uio.h>
46 1.1 manu #include <sys/socket.h>
47 1.1 manu #include <sys/un.h>
48 1.1 manu #include <machine/vmparam.h>
49 1.1 manu
50 1.1 manu #include "../../lib/libperfuse/perfuse_if.h"
51 1.1 manu #include "perfused.h"
52 1.1 manu
53 1.1 manu static int getpeerid(int, pid_t *, uid_t *, gid_t *);
54 1.1 manu static int access_mount(const char *, uid_t, int);
55 1.7 manu static void new_mount(int, int);
56 1.1 manu static int parse_debug(char *);
57 1.1 manu static void siginfo_handler(int);
58 1.3 manu static int parse_options(int, char **);
59 1.1 manu static void get_mount_info(int, struct perfuse_mount_info *);
60 1.1 manu int main(int, char **);
61 1.1 manu
62 1.7 manu /*
63 1.7 manu * Flags for new_mount()
64 1.7 manu */
65 1.7 manu #define PMNT_DEVFUSE 0x0 /* We use /dev/fuse */
66 1.7 manu #define PMNT_SOCKPAIR 0x1 /* We use socketpair */
67 1.7 manu
68 1.1 manu
69 1.1 manu static int
70 1.1 manu getpeerid(s, pidp, uidp, gidp)
71 1.1 manu int s;
72 1.1 manu pid_t *pidp;
73 1.1 manu uid_t *uidp;
74 1.1 manu gid_t *gidp;
75 1.1 manu {
76 1.1 manu struct unpcbid unp;
77 1.1 manu socklen_t len;
78 1.1 manu int error;
79 1.1 manu
80 1.1 manu len = sizeof(unp);
81 1.1 manu error = getsockopt(s, 0, LOCAL_PEEREID, &unp, &len);
82 1.1 manu if (error != 0)
83 1.1 manu return error;
84 1.1 manu
85 1.1 manu if (pidp != NULL)
86 1.1 manu *pidp = unp.unp_pid;
87 1.1 manu
88 1.1 manu if (uidp != NULL)
89 1.1 manu *uidp = unp.unp_euid;
90 1.1 manu
91 1.1 manu if (gidp != NULL)
92 1.1 manu *gidp = unp.unp_egid;
93 1.1 manu
94 1.1 manu return 0;
95 1.1 manu }
96 1.1 manu
97 1.1 manu static int
98 1.1 manu access_mount(mnt, uid, ro)
99 1.1 manu const char *mnt;
100 1.1 manu uid_t uid;
101 1.1 manu int ro;
102 1.1 manu {
103 1.1 manu struct stat st;
104 1.1 manu mode_t mode;
105 1.1 manu
106 1.1 manu if (uid == 0)
107 1.1 manu return 0;
108 1.1 manu
109 1.1 manu if (stat(mnt, &st) == -1)
110 1.1 manu return -1;
111 1.1 manu
112 1.1 manu if (st.st_uid != uid)
113 1.1 manu return -1;
114 1.1 manu
115 1.1 manu mode = S_IRUSR;
116 1.1 manu if (!ro)
117 1.1 manu mode |= S_IWUSR;
118 1.1 manu
119 1.1 manu if ((st.st_mode & mode) == mode)
120 1.1 manu return 0;
121 1.1 manu
122 1.1 manu return -1;
123 1.1 manu }
124 1.1 manu
125 1.1 manu static void
126 1.1 manu get_mount_info(fd, pmi)
127 1.1 manu int fd;
128 1.1 manu struct perfuse_mount_info *pmi;
129 1.1 manu {
130 1.1 manu struct perfuse_mount_out *pmo;
131 1.1 manu char *source = NULL;
132 1.1 manu char *target = NULL;
133 1.1 manu char *filesystemtype = NULL;
134 1.1 manu long mountflags = 0;
135 1.1 manu void *data;
136 1.1 manu size_t len;
137 1.1 manu
138 1.1 manu pmo = (struct perfuse_mount_out *)perfuse_recv_early(fd, sizeof(*pmo));
139 1.1 manu if (pmo == NULL) {
140 1.1 manu if (shutdown(fd, SHUT_RDWR) != 0)
141 1.1 manu DERR(EX_OSERR, "shutdown failed");
142 1.1 manu exit(EX_PROTOCOL);
143 1.1 manu }
144 1.1 manu
145 1.1 manu #ifdef PERFUSE_DEBUG
146 1.7 manu if (perfuse_diagflags & PDF_MISC)
147 1.7 manu DPRINTF("perfuse lengths: source = %"PRId32", "
148 1.7 manu "target = %"PRId32", filesystemtype = %"PRId32", "
149 1.7 manu "data = %"PRId32"\n", pmo->pmo_source_len,
150 1.7 manu pmo->pmo_target_len, pmo->pmo_filesystemtype_len,
151 1.7 manu pmo->pmo_data_len);
152 1.1 manu #endif
153 1.1 manu len = pmo->pmo_source_len;
154 1.1 manu source = perfuse_recv_early(fd, len);
155 1.1 manu
156 1.1 manu len = pmo->pmo_target_len;
157 1.1 manu target = perfuse_recv_early(fd, len);
158 1.1 manu
159 1.1 manu len = pmo->pmo_filesystemtype_len;
160 1.1 manu filesystemtype = perfuse_recv_early(fd, len);
161 1.1 manu
162 1.1 manu mountflags = pmo->pmo_mountflags;
163 1.1 manu
164 1.1 manu len = pmo->pmo_data_len;
165 1.1 manu data = perfuse_recv_early(fd, len);
166 1.1 manu
167 1.1 manu #ifdef PERFUSE_DEBUG
168 1.7 manu if (perfuse_diagflags & PDF_MISC)
169 1.7 manu DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n",
170 1.7 manu __func__, source, target, filesystemtype,
171 1.7 manu mountflags, (const char *)data);
172 1.1 manu #endif
173 1.1 manu pmi->pmi_source = source;
174 1.1 manu pmi->pmi_target = target;
175 1.1 manu pmi->pmi_filesystemtype = filesystemtype;
176 1.5 manu pmi->pmi_mountflags = (int)mountflags;
177 1.1 manu pmi->pmi_data = data;
178 1.1 manu
179 1.1 manu return;
180 1.1 manu }
181 1.1 manu
182 1.3 manu static void
183 1.7 manu new_mount(fd, pmnt_flags)
184 1.3 manu int fd;
185 1.7 manu int pmnt_flags;
186 1.1 manu {
187 1.1 manu struct puffs_usermount *pu;
188 1.1 manu struct perfuse_mount_info pmi;
189 1.1 manu struct perfuse_callbacks pc;
190 1.1 manu int ro_flag;
191 1.1 manu pid_t pid;
192 1.1 manu int flags;
193 1.1 manu
194 1.1 manu
195 1.1 manu pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
196 1.1 manu switch(pid) {
197 1.1 manu case -1:
198 1.1 manu DERR(EX_OSERR, "cannot fork");
199 1.1 manu break;
200 1.1 manu case 0:
201 1.1 manu break;
202 1.1 manu default:
203 1.3 manu return;
204 1.1 manu /* NOTREACHED */
205 1.1 manu break;
206 1.1 manu }
207 1.1 manu
208 1.1 manu /*
209 1.1 manu * Mount information (source, target, mount flags...)
210 1.1 manu */
211 1.1 manu get_mount_info(fd, &pmi);
212 1.1 manu
213 1.1 manu /*
214 1.7 manu * Get peer identity. If we use socketpair (-i option),
215 1.7 manu * peer identity if the same as us.
216 1.1 manu */
217 1.7 manu if (pmnt_flags & PMNT_SOCKPAIR) {
218 1.7 manu pmi.pmi_uid = getuid();
219 1.7 manu } else {
220 1.7 manu if (getpeerid(fd, NULL, &pmi.pmi_uid, NULL) != 0) {
221 1.7 manu DWARNX("Unable to retreive peer identity");
222 1.7 manu pmi.pmi_uid = (uid_t)-1;
223 1.7 manu }
224 1.7 manu }
225 1.1 manu
226 1.1 manu /*
227 1.1 manu * Check that peer owns mountpoint and read (and write) on it?
228 1.1 manu */
229 1.1 manu ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
230 1.1 manu if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
231 1.7 manu DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
232 1.1 manu pmi.pmi_target);
233 1.1 manu
234 1.1 manu
235 1.1 manu /*
236 1.1 manu * Initialize libperfuse, which will initialize libpuffs
237 1.1 manu */
238 1.1 manu pc.pc_new_msg = perfuse_new_pb;
239 1.1 manu pc.pc_xchg_msg = perfuse_xchg_pb;
240 1.1 manu pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
241 1.1 manu pc.pc_get_inhdr = perfuse_get_inhdr;
242 1.1 manu pc.pc_get_inpayload = perfuse_get_inpayload;
243 1.1 manu pc.pc_get_outhdr = perfuse_get_outhdr;
244 1.1 manu pc.pc_get_outpayload = perfuse_get_outpayload;
245 1.1 manu
246 1.1 manu pu = perfuse_init(&pc, &pmi);
247 1.1 manu
248 1.1 manu puffs_framev_init(pu, perfuse_readframe, perfuse_writeframe,
249 1.3 manu perfuse_cmpframe, perfuse_gotframe, perfuse_fdnotify);
250 1.1 manu
251 1.1 manu if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
252 1.1 manu DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
253 1.1 manu
254 1.4 manu perfuse_setspecific(pu, (void *)(long)fd);
255 1.1 manu
256 1.1 manu setproctitle("perfused %s", pmi.pmi_target);
257 1.1 manu (void)kill(getpid(), SIGINFO); /* This is for -s option */
258 1.1 manu
259 1.1 manu perfuse_fs_init(pu);
260 1.1 manu
261 1.1 manu /*
262 1.1 manu * Non blocking I/O on /dev/fuse
263 1.1 manu * This must be done after perfuse_fs_init
264 1.1 manu */
265 1.1 manu if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
266 1.1 manu DERR(EX_OSERR, "fcntl failed");
267 1.1 manu if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
268 1.1 manu DERR(EX_OSERR, "fcntl failed");
269 1.1 manu
270 1.1 manu /*
271 1.1 manu * Hand over control to puffs main loop.
272 1.1 manu */
273 1.3 manu (void)perfuse_mainloop(pu);
274 1.3 manu
275 1.3 manu DERRX(EX_SOFTWARE, "perfuse_mainloop exit");
276 1.3 manu
277 1.3 manu /* NOTREACHED */
278 1.3 manu return;
279 1.1 manu }
280 1.1 manu
281 1.1 manu static int
282 1.1 manu parse_debug(optstr)
283 1.1 manu char *optstr;
284 1.1 manu {
285 1.1 manu int retval = PDF_SYSLOG;
286 1.1 manu char *opt;
287 1.1 manu char *lastp;
288 1.1 manu
289 1.1 manu for (opt = strtok_r(optstr, ",", &lastp);
290 1.1 manu opt;
291 1.1 manu opt = strtok_r(NULL, ",", &lastp)) {
292 1.1 manu if (strcmp(opt, "fuse") == 0)
293 1.1 manu retval |= PDF_FUSE;
294 1.1 manu else if (strcmp(opt, "puffs") == 0)
295 1.1 manu retval |= PDF_PUFFS;
296 1.1 manu else if (strcmp(opt, "dump") == 0)
297 1.1 manu retval |= PDF_DUMP;
298 1.1 manu else if (strcmp(opt, "fh") == 0)
299 1.1 manu retval |= PDF_FH;
300 1.1 manu else if (strcmp(opt, "readdir") == 0)
301 1.1 manu retval |= PDF_READDIR;
302 1.1 manu else if (strcmp(opt, "reclaim") == 0)
303 1.1 manu retval |= PDF_RECLAIM;
304 1.1 manu else if (strcmp(opt, "requeue") == 0)
305 1.1 manu retval |= PDF_REQUEUE;
306 1.2 manu else if (strcmp(opt, "sync") == 0)
307 1.2 manu retval |= PDF_SYNC;
308 1.1 manu else if (strcmp(opt, "misc") == 0)
309 1.1 manu retval |= PDF_MISC;
310 1.1 manu else
311 1.1 manu DERRX(EX_USAGE, "unknown debug flag \"%s\"", opt);
312 1.1 manu }
313 1.1 manu
314 1.1 manu return retval;
315 1.1 manu }
316 1.1 manu
317 1.1 manu /* ARGSUSED0 */
318 1.1 manu static void
319 1.1 manu siginfo_handler(sig)
320 1.1 manu int sig;
321 1.1 manu {
322 1.1 manu static int old_flags = 0;
323 1.1 manu int swap;
324 1.1 manu
325 1.1 manu swap = perfuse_diagflags;
326 1.1 manu perfuse_diagflags = old_flags;
327 1.1 manu old_flags = swap;
328 1.1 manu
329 1.1 manu DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
330 1.1 manu
331 1.1 manu return;
332 1.1 manu }
333 1.1 manu
334 1.3 manu static int
335 1.1 manu parse_options(argc, argv)
336 1.1 manu int argc;
337 1.1 manu char **argv;
338 1.1 manu {
339 1.1 manu int ch;
340 1.1 manu int foreground = 0;
341 1.3 manu int retval = -1;
342 1.1 manu
343 1.1 manu perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
344 1.1 manu
345 1.3 manu while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
346 1.1 manu switch (ch) {
347 1.1 manu case 'd':
348 1.1 manu perfuse_diagflags |= parse_debug(optarg);
349 1.1 manu break;
350 1.1 manu case 's':
351 1.1 manu if (signal(SIGINFO, siginfo_handler) != 0)
352 1.1 manu DERR(EX_OSERR, "signal failed");
353 1.1 manu break;
354 1.1 manu case 'f':
355 1.1 manu foreground = 1;
356 1.7 manu perfuse_diagflags |= PDF_MISC;
357 1.1 manu break;
358 1.3 manu case 'i':
359 1.3 manu retval = atoi(optarg);
360 1.3 manu foreground = 1;
361 1.3 manu break;
362 1.1 manu default:
363 1.6 wiz DERR(EX_USAGE, "%s [-fs] [-d level] [-i fd]", argv[0]);
364 1.1 manu break;
365 1.1 manu }
366 1.1 manu }
367 1.1 manu
368 1.1 manu if (!foreground)
369 1.1 manu perfuse_diagflags &= ~PDF_FOREGROUND;
370 1.1 manu
371 1.3 manu return retval;
372 1.1 manu }
373 1.1 manu
374 1.1 manu int
375 1.1 manu main(argc, argv)
376 1.1 manu int argc;
377 1.1 manu char **argv;
378 1.1 manu {
379 1.1 manu int s;
380 1.1 manu
381 1.3 manu s = parse_options(argc, argv);
382 1.1 manu
383 1.1 manu if (perfuse_diagflags & PDF_SYSLOG)
384 1.1 manu openlog("perfused", 0, LOG_DAEMON);
385 1.1 manu
386 1.1 manu if (!(perfuse_diagflags & PDF_FOREGROUND))
387 1.1 manu if (daemon(0, 0) != 0)
388 1.1 manu DERR(EX_OSERR, "daemon failed");
389 1.1 manu
390 1.3 manu if (s != -1) {
391 1.7 manu new_mount(s, PMNT_SOCKPAIR);
392 1.3 manu DERRX(EX_SOFTWARE, "new_mount exit while -i is used");
393 1.3 manu }
394 1.3 manu
395 1.1 manu s = perfuse_open_sock();
396 1.1 manu
397 1.1 manu do {
398 1.3 manu struct sockaddr *sa;
399 1.3 manu struct sockaddr_storage ss;
400 1.3 manu socklen_t ss_len;
401 1.3 manu int fd;
402 1.3 manu
403 1.3 manu #ifdef PERFUSE_DEBUG
404 1.3 manu if (perfuse_diagflags & PDF_MISC)
405 1.3 manu DPRINTF("waiting connexion\n");
406 1.3 manu #endif
407 1.3 manu sa = (struct sockaddr *)(void *)&ss;
408 1.3 manu ss_len = sizeof(ss);
409 1.3 manu if ((fd = accept(s, sa, &ss_len)) == -1)
410 1.3 manu DERR(EX_OSERR, "accept failed");
411 1.3 manu #ifdef PERFUSE_DEBUG
412 1.3 manu if (perfuse_diagflags & PDF_MISC)
413 1.3 manu DPRINTF("connexion accepted\n");
414 1.3 manu #endif
415 1.7 manu new_mount(fd, PMNT_DEVFUSE);
416 1.1 manu } while (1 /* CONSTCOND */);
417 1.1 manu
418 1.1 manu /* NOTREACHED */
419 1.1 manu return 0;
420 1.1 manu }
421