2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 "@(#) Copyright (c) 1992, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)mount_portal.c 8.6 (Berkeley) 4/26/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/mount.h>
63 #include "pathnames.h"
66 struct mntopt mopts[] = {
71 static void usage(void) __dead2;
73 static volatile sig_atomic_t readcf; /* Set when SIGHUP received */
75 static void sighup(int sig __unused)
80 static void sigchld(int sig __unused)
84 while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
86 /* wrtp - waitpid _doesn't_ return 0 when no children! */
88 if (pid < 0 && errno != ECHILD)
89 syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
94 main(int argc, char *argv[])
96 struct portal_args args;
97 struct sockaddr_un un;
99 char mountpt[MAXPATHLEN];
110 * Crack command line args
114 while ((ch = getopt(argc, argv, "o:")) != -1) {
117 getmntopts(optarg, mopts, &mntflags, 0);
125 if (optind != (argc - 2))
132 * Get config file and mount point
135 if (conf[0] != '/') {
136 (void)fprintf(stderr,
137 "The configuration file must be specified"
138 "through an absolute file path.\n");
142 /* resolve the mountpoint with realpath(3) */
143 (void)checkpath(argv[optind+1], mountpt);
146 * Construct the listening socket
148 un.sun_family = AF_UNIX;
149 if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
150 errx(EX_SOFTWARE, "portal socket name too long");
152 strcpy(un.sun_path, _PATH_TMPPORTAL);
154 un.sun_len = strlen(un.sun_path);
156 so = socket(AF_UNIX, SOCK_STREAM, 0);
158 err(EX_OSERR, "socket");
161 (void) unlink(un.sun_path);
162 if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
165 (void) unlink(un.sun_path);
168 (void) listen(so, 5);
171 sprintf(tag, "portal:%d", getpid());
172 args.pa_config = tag;
174 rc = mount("portalfs", mountpt, mntflags, &args);
179 * Everything is ready to go - now is a good time to fork
186 * Start logging (and change name)
188 openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
190 q.q_forw = q.q_back = &q;
193 signal(SIGCHLD, sigchld);
194 signal(SIGHUP, sighup);
197 * Just loop waiting for new connections and activating them
200 struct sockaddr_un un2;
201 int len2 = sizeof(un2);
207 * Check whether we need to re-read the configuration file
211 printf ("re-reading configuration file\n");
219 * Accept a new connection
220 * Will get EINTR if a signal has arrived, so just
221 * ignore that error code
225 rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
229 syslog(LOG_ERR, "select: %s", strerror(errno));
234 so2 = accept(so, (struct sockaddr *) &un2, &len2);
237 * The unmount function does a shutdown on the socket
238 * which will generated ECONNABORTED on the accept.
240 if (errno == ECONNABORTED)
242 if (errno != EINTR) {
243 syslog(LOG_ERR, "accept: %s", strerror(errno));
250 * Now fork a new child to deal with the connection
253 switch (pid = fork()) {
255 if (errno == EAGAIN) {
259 syslog(LOG_ERR, "fork: %s", strerror(errno));
270 syslog(LOG_INFO, "%s unmounted", mountpt);
277 (void)fprintf(stderr,
278 "usage: mount_portalfs [-o options] config mount-point\n");