1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software donated to Berkeley by
|
|
|
|
* Jan-Simon Pendry.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1992, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-07-06 07:19:27 +00:00
|
|
|
#if 0
|
1997-03-11 12:40:45 +00:00
|
|
|
static char sccsid[] = "@(#)mount_portal.c 8.6 (Berkeley) 4/26/95";
|
1998-07-06 07:19:27 +00:00
|
|
|
#endif
|
1996-05-13 17:43:19 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
1998-07-06 07:19:27 +00:00
|
|
|
#include <sys/stat.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-05-13 17:43:19 +00:00
|
|
|
#include <sysexits.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mntopts.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "portald.h"
|
|
|
|
|
|
|
|
struct mntopt mopts[] = {
|
|
|
|
MOPT_STDOPTS,
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
static void usage __P((void)) __dead2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
static sig_atomic_t readcf; /* Set when SIGHUP received */
|
|
|
|
|
1995-04-19 12:24:08 +00:00
|
|
|
static void sighup(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
readcf ++;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
static void sigchld(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
|
|
|
|
;
|
1995-04-19 12:24:08 +00:00
|
|
|
/* wrtp - waitpid _doesn't_ return 0 when no children! */
|
|
|
|
#ifdef notdef
|
1997-03-11 12:40:45 +00:00
|
|
|
if (pid < 0 && errno != ECHILD)
|
1994-05-26 06:35:07 +00:00
|
|
|
syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
|
1995-04-19 12:24:08 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
struct portal_args args;
|
|
|
|
struct sockaddr_un un;
|
|
|
|
char *conf;
|
1999-10-09 11:54:14 +00:00
|
|
|
char mountpt[MAXPATHLEN];
|
1994-05-26 06:35:07 +00:00
|
|
|
int mntflags = 0;
|
|
|
|
char tag[32];
|
1997-03-11 12:40:45 +00:00
|
|
|
struct vfsconf vfc;
|
1997-03-24 05:53:12 +00:00
|
|
|
mode_t um;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
qelem q;
|
|
|
|
int rc;
|
|
|
|
int so;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Crack command line args
|
|
|
|
*/
|
|
|
|
int ch;
|
|
|
|
|
1997-03-29 03:33:12 +00:00
|
|
|
while ((ch = getopt(argc, argv, "o:")) != -1) {
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'o':
|
1994-11-01 23:51:53 +00:00
|
|
|
getmntopts(optarg, mopts, &mntflags, 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != (argc - 2))
|
|
|
|
error = 1;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get config file and mount point
|
|
|
|
*/
|
|
|
|
conf = argv[optind];
|
1999-10-09 11:54:14 +00:00
|
|
|
|
|
|
|
/* resolve the mountpoint with realpath(3) */
|
|
|
|
(void)checkpath(argv[optind+1], mountpt);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct the listening socket
|
|
|
|
*/
|
|
|
|
un.sun_family = AF_UNIX;
|
|
|
|
if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
|
1996-05-13 17:43:19 +00:00
|
|
|
errx(EX_SOFTWARE, "portal socket name too long");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
strcpy(un.sun_path, _PATH_TMPPORTAL);
|
|
|
|
mktemp(un.sun_path);
|
|
|
|
un.sun_len = strlen(un.sun_path);
|
|
|
|
|
|
|
|
so = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (so < 0) {
|
1996-05-13 17:43:19 +00:00
|
|
|
err(EX_OSERR, "socket");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1997-03-24 05:53:12 +00:00
|
|
|
um = umask(077);
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) unlink(un.sun_path);
|
|
|
|
if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
|
|
|
|
err(1, NULL);
|
1995-04-19 12:24:08 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) unlink(un.sun_path);
|
1997-03-24 05:53:12 +00:00
|
|
|
(void) umask(um);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
(void) listen(so, 5);
|
|
|
|
|
|
|
|
args.pa_socket = so;
|
|
|
|
sprintf(tag, "portal:%d", getpid());
|
|
|
|
args.pa_config = tag;
|
|
|
|
|
2001-05-24 15:20:11 +00:00
|
|
|
error = getvfsbyname("portalfs", &vfc);
|
|
|
|
if (error && vfsisloadable("portalfs")) {
|
|
|
|
if (vfsload("portalfs"))
|
|
|
|
err(EX_OSERR, "vfsload(portalfs)");
|
1997-03-11 12:40:45 +00:00
|
|
|
endvfsent();
|
2001-05-24 15:20:11 +00:00
|
|
|
error = getvfsbyname("portalfs", &vfc);
|
1994-09-22 22:17:02 +00:00
|
|
|
}
|
1997-03-11 12:40:45 +00:00
|
|
|
if (error)
|
1996-05-13 17:43:19 +00:00
|
|
|
errx(EX_OSERR, "portal filesystem is not available");
|
1994-09-22 22:17:02 +00:00
|
|
|
|
1997-03-11 12:40:45 +00:00
|
|
|
rc = mount(vfc.vfc_name, mountpt, mntflags, &args);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (rc < 0)
|
|
|
|
err(1, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Everything is ready to go - now is a good time to fork
|
|
|
|
*/
|
1998-12-15 18:59:07 +00:00
|
|
|
#ifndef DEBUG
|
1994-05-26 06:35:07 +00:00
|
|
|
daemon(0, 0);
|
1998-12-15 18:59:07 +00:00
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start logging (and change name)
|
|
|
|
*/
|
|
|
|
openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
|
|
|
|
|
|
|
|
q.q_forw = q.q_back = &q;
|
|
|
|
readcf = 1;
|
|
|
|
|
|
|
|
signal(SIGCHLD, sigchld);
|
1995-04-19 12:24:08 +00:00
|
|
|
signal(SIGHUP, sighup);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Just loop waiting for new connections and activating them
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
struct sockaddr_un un2;
|
|
|
|
int len2 = sizeof(un2);
|
|
|
|
int so2;
|
|
|
|
pid_t pid;
|
|
|
|
fd_set fdset;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether we need to re-read the configuration file
|
|
|
|
*/
|
|
|
|
if (readcf) {
|
1995-04-19 12:24:08 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf ("re-reading configuration file\n");
|
|
|
|
#endif
|
1994-05-26 06:35:07 +00:00
|
|
|
readcf = 0;
|
|
|
|
conf_read(&q, conf);
|
|
|
|
continue;
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Accept a new connection
|
|
|
|
* Will get EINTR if a signal has arrived, so just
|
|
|
|
* ignore that error code
|
|
|
|
*/
|
1995-04-19 12:24:08 +00:00
|
|
|
FD_ZERO(&fdset);
|
1994-05-26 06:35:07 +00:00
|
|
|
FD_SET(so, &fdset);
|
1995-04-19 12:24:08 +00:00
|
|
|
rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
syslog(LOG_ERR, "select: %s", strerror(errno));
|
1996-05-13 17:43:19 +00:00
|
|
|
exit(EX_OSERR);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
so2 = accept(so, (struct sockaddr *) &un2, &len2);
|
|
|
|
if (so2 < 0) {
|
|
|
|
/*
|
|
|
|
* The unmount function does a shutdown on the socket
|
|
|
|
* which will generated ECONNABORTED on the accept.
|
|
|
|
*/
|
|
|
|
if (errno == ECONNABORTED)
|
|
|
|
break;
|
|
|
|
if (errno != EINTR) {
|
|
|
|
syslog(LOG_ERR, "accept: %s", strerror(errno));
|
1996-05-13 17:43:19 +00:00
|
|
|
exit(EX_OSERR);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now fork a new child to deal with the connection
|
|
|
|
*/
|
|
|
|
eagain:;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
sleep(1);
|
|
|
|
goto eagain;
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "fork: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
(void) close(so);
|
|
|
|
activate(&q, so2);
|
1997-03-11 12:40:45 +00:00
|
|
|
exit(0);
|
1994-05-26 06:35:07 +00:00
|
|
|
default:
|
|
|
|
(void) close(so2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syslog(LOG_INFO, "%s unmounted", mountpt);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2001-05-23 14:58:19 +00:00
|
|
|
"usage: mount_portalfs [-o options] config mount-point\n");
|
1996-05-13 17:43:19 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|