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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)mount_portal.c 8.6 (Berkeley) 4/26/95
38 * $FreeBSD: src/sbin/mount_portal/mount_portal.c,v 1.16 1999/10/09 11:54:11 phk Exp $
39 * $DragonFly: src/sbin/mount_portal/mount_portal.c,v 1.4 2003/11/01 17:16:00 drhodus Exp $
42 #include <sys/param.h>
44 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #include <sys/mount.h>
59 #include "pathnames.h"
62 struct mntopt mopts[] = {
67 static void usage(void) __dead2;
69 static sig_atomic_t readcf; /* Set when SIGHUP received */
71 static void sighup(int sig)
76 static void sigchld(int sig)
80 while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
82 /* wrtp - waitpid _doesn't_ return 0 when no children! */
84 if (pid < 0 && errno != ECHILD)
85 syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
90 main(int argc, char **argv)
92 struct portal_args args;
93 struct sockaddr_un un;
95 char mountpt[MAXPATHLEN];
107 * Crack command line args
111 while ((ch = getopt(argc, argv, "o:")) != -1) {
114 getmntopts(optarg, mopts, &mntflags, 0);
122 if (optind != (argc - 2))
129 * Get config file and mount point
133 /* resolve the mountpoint with realpath(3) */
134 (void)checkpath(argv[optind+1], mountpt);
137 * Construct the listening socket
139 un.sun_family = AF_UNIX;
140 if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
141 errx(EX_SOFTWARE, "portal socket name too long");
143 strcpy(un.sun_path, _PATH_TMPPORTAL);
145 un.sun_len = strlen(un.sun_path);
147 so = socket(AF_UNIX, SOCK_STREAM, 0);
149 err(EX_OSERR, "socket");
152 (void) unlink(un.sun_path);
153 if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
156 (void) unlink(un.sun_path);
159 (void) listen(so, 5);
162 sprintf(tag, "portal:%d", getpid());
163 args.pa_config = tag;
165 error = getvfsbyname("portal", &vfc);
166 if (error && vfsisloadable("portal")) {
167 if (vfsload("portal"))
168 err(EX_OSERR, "vfsload(portal)");
170 error = getvfsbyname("portal", &vfc);
173 errx(EX_OSERR, "portal filesystem is not available");
175 rc = mount(vfc.vfc_name, mountpt, mntflags, &args);
180 * Everything is ready to go - now is a good time to fork
187 * Start logging (and change name)
189 openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
191 q.q_forw = q.q_back = &q;
194 signal(SIGCHLD, sigchld);
195 signal(SIGHUP, sighup);
198 * Just loop waiting for new connections and activating them
201 struct sockaddr_un un2;
202 int len2 = sizeof(un2);
209 * Check whether we need to re-read the configuration file
213 printf ("re-reading configuration file\n");
221 * Accept a new connection
222 * Will get EINTR if a signal has arrived, so just
223 * ignore that error code
227 rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
231 syslog(LOG_ERR, "select: %s", strerror(errno));
236 so2 = accept(so, (struct sockaddr *) &un2, &len2);
239 * The unmount function does a shutdown on the socket
240 * which will generated ECONNABORTED on the accept.
242 if (errno == ECONNABORTED)
244 if (errno != EINTR) {
245 syslog(LOG_ERR, "accept: %s", strerror(errno));
252 * Now fork a new child to deal with the connection
255 switch (pid = fork()) {
257 if (errno == EAGAIN) {
261 syslog(LOG_ERR, "fork: %s", strerror(errno));
272 syslog(LOG_INFO, "%s unmounted", mountpt);
279 (void)fprintf(stderr,
280 "usage: mount_portal [-o options] config mount-point\n");