2002-09-21 22:43:31 +04:00
|
|
|
/* $NetBSD: mount_portal.c,v 1.22 2002/09/21 18:43:37 christos Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1994-01-12 23:01:45 +03:00
|
|
|
/*
|
1994-06-08 23:23:02 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-12 23:01:45 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 09:58:22 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-06-08 23:23:02 +04:00
|
|
|
#ifndef lint
|
1997-09-15 09:58:22 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1994-06-08 23:23:02 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 16:32:03 +04:00
|
|
|
static char sccsid[] = "@(#)mount_portal.c 8.6 (Berkeley) 4/26/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2002-09-21 22:43:31 +04:00
|
|
|
__RCSID("$NetBSD: mount_portal.c,v 1.22 2002/09/21 18:43:37 christos Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1994-06-08 23:23:02 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-01-12 23:01:45 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/syslog.h>
|
1998-02-17 14:38:26 +03:00
|
|
|
#include <sys/time.h>
|
1994-01-12 23:01:45 +03:00
|
|
|
#include <sys/mount.h>
|
|
|
|
|
1994-06-08 23:23:02 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2002-09-21 22:24:40 +04:00
|
|
|
#include <poll.h>
|
1994-06-08 23:23:02 +04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mntopts.h"
|
1994-01-12 23:01:45 +03:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "portald.h"
|
|
|
|
|
2000-11-06 17:00:38 +03:00
|
|
|
static const struct mntopt mopts[] = {
|
1994-06-08 23:23:02 +04:00
|
|
|
MOPT_STDOPTS,
|
2002-09-21 22:43:31 +04:00
|
|
|
MOPT_GETARGS,
|
1994-06-08 23:23:02 +04:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2000-11-06 17:00:38 +03:00
|
|
|
static const char *mountpt; /* made available to signal handler */
|
1996-03-23 05:37:48 +03:00
|
|
|
|
1997-09-15 09:58:22 +04:00
|
|
|
int main __P((int, char *[]));
|
|
|
|
static void sigchld __P((int));
|
|
|
|
static void sighup __P((int));
|
|
|
|
static void sigterm __P((int));
|
|
|
|
static void usage __P((void));
|
1994-06-08 23:23:02 +04:00
|
|
|
|
1994-01-12 23:01:45 +03:00
|
|
|
static sig_atomic_t readcf; /* Set when SIGHUP received */
|
|
|
|
|
1995-06-06 23:51:19 +04:00
|
|
|
static void
|
|
|
|
sigchld(sig)
|
|
|
|
int sig;
|
1994-01-12 23:01:45 +03:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
|
|
|
|
;
|
1994-06-08 23:23:02 +04:00
|
|
|
if (pid < 0 && errno != ECHILD)
|
1997-09-21 06:35:40 +04:00
|
|
|
syslog(LOG_WARNING, "waitpid: %m");
|
1994-01-12 23:01:45 +03:00
|
|
|
}
|
|
|
|
|
1995-06-06 23:51:19 +04:00
|
|
|
static void
|
|
|
|
sighup(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
|
|
|
|
readcf = 1;
|
|
|
|
}
|
|
|
|
|
1996-03-23 05:37:48 +03:00
|
|
|
static void
|
|
|
|
sigterm(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1997-09-21 06:35:40 +04:00
|
|
|
|
1996-03-23 05:37:48 +03:00
|
|
|
if (unmount(mountpt, MNT_FORCE) < 0)
|
1997-09-21 06:35:40 +04:00
|
|
|
syslog(LOG_WARNING, "sigterm: unmounting %s failed: %m",
|
|
|
|
mountpt);
|
1996-03-23 05:37:48 +03:00
|
|
|
}
|
|
|
|
|
1994-06-08 23:23:02 +04:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1994-01-12 23:01:45 +03:00
|
|
|
{
|
|
|
|
struct portal_args args;
|
|
|
|
struct sockaddr_un un;
|
|
|
|
char *conf;
|
|
|
|
int mntflags = 0;
|
|
|
|
char tag[32];
|
2000-11-06 17:00:38 +03:00
|
|
|
char tmpdir[PATH_MAX];
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
qelem q;
|
|
|
|
int rc;
|
|
|
|
int so;
|
1994-06-08 23:23:02 +04:00
|
|
|
int error = 0;
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Crack command line args
|
|
|
|
*/
|
|
|
|
int ch;
|
|
|
|
|
1997-09-15 09:58:22 +04:00
|
|
|
while ((ch = getopt(argc, argv, "o:")) != -1) {
|
1994-01-12 23:01:45 +03:00
|
|
|
switch (ch) {
|
1994-06-08 23:23:02 +04:00
|
|
|
case 'o':
|
1997-09-16 16:32:03 +04:00
|
|
|
getmntopts(optarg, mopts, &mntflags, 0);
|
1994-01-12 23:01:45 +03:00
|
|
|
break;
|
|
|
|
default:
|
1994-06-08 23:23:02 +04:00
|
|
|
error = 1;
|
1994-01-12 23:01:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-06-08 23:23:02 +04:00
|
|
|
if (optind != (argc - 2))
|
|
|
|
error = 1;
|
1994-01-12 23:01:45 +03:00
|
|
|
|
1994-06-08 23:23:02 +04:00
|
|
|
if (error)
|
|
|
|
usage();
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get config file and mount point
|
|
|
|
*/
|
1994-06-08 23:23:02 +04:00
|
|
|
conf = argv[optind];
|
|
|
|
mountpt = argv[optind+1];
|
1994-01-12 23:01:45 +03:00
|
|
|
|
1999-08-16 10:34:50 +04:00
|
|
|
/*
|
|
|
|
* If configuration file is not specified with an
|
|
|
|
* absolute pathname, complain and die. We want to
|
|
|
|
* check for this before we call daemon() and lose
|
|
|
|
* access to stderr.
|
|
|
|
*/
|
|
|
|
if (conf[0] != '/') {
|
|
|
|
errx(-1, "Error: the configuration file must be specified as an\n"
|
2002-07-20 12:36:17 +04:00
|
|
|
"absolute path, as the daemon chdir's to / immediately.");
|
1999-08-16 10:34:50 +04:00
|
|
|
}
|
1994-01-12 23:01:45 +03:00
|
|
|
/*
|
|
|
|
* Construct the listening socket
|
|
|
|
*/
|
1998-07-18 09:04:35 +04:00
|
|
|
un.sun_family = AF_LOCAL;
|
2000-11-06 17:00:38 +03:00
|
|
|
strcpy(tmpdir, _PATH_TMPPORTAL);
|
|
|
|
mkdtemp(tmpdir);
|
|
|
|
un.sun_len = snprintf(un.sun_path, sizeof(un.sun_path),
|
|
|
|
"%s/%s", tmpdir, _PATH_PORTAL_FILE);
|
|
|
|
if (un.sun_len >= sizeof(un.sun_path))
|
1997-09-21 06:35:40 +04:00
|
|
|
errx(1, "portal socket name too long");
|
1994-01-12 23:01:45 +03:00
|
|
|
|
1998-07-18 09:04:35 +04:00
|
|
|
so = socket(AF_LOCAL, SOCK_STREAM, 0);
|
1997-09-21 06:35:40 +04:00
|
|
|
if (so < 0)
|
|
|
|
err(1, "socket");
|
1994-06-08 23:23:02 +04:00
|
|
|
if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
|
1999-03-04 06:02:43 +03:00
|
|
|
err(1, "bind() call");
|
2000-11-06 17:00:38 +03:00
|
|
|
|
|
|
|
/* path no longer needed */
|
1994-01-12 23:01:45 +03:00
|
|
|
(void) unlink(un.sun_path);
|
2000-11-06 17:00:38 +03:00
|
|
|
if (rmdir(tmpdir) != 0)
|
|
|
|
warn("failed to rmdir(%s)", tmpdir);
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
(void) listen(so, 5);
|
|
|
|
|
2000-11-06 17:00:38 +03:00
|
|
|
/*
|
|
|
|
* Now it's good time to fork - all but the actual mount(2)
|
|
|
|
* call is performed, and we need the new pid to fill in
|
|
|
|
* the tag contents correctly. Since we need to print error message
|
|
|
|
* in case mount(2) fails or DEBUG case, we don't let daemon(3) to
|
|
|
|
* close the standard streams and handle them on our own later.
|
|
|
|
*/
|
|
|
|
daemon(0, 1);
|
|
|
|
|
1994-01-12 23:01:45 +03:00
|
|
|
args.pa_socket = so;
|
2000-11-06 17:00:38 +03:00
|
|
|
sprintf(tag, "portal:%d", getpid());
|
1994-01-12 23:01:45 +03:00
|
|
|
args.pa_config = tag;
|
|
|
|
|
1997-09-21 06:35:40 +04:00
|
|
|
rc = mount(MOUNT_PORTAL, mountpt, mntflags, &args);
|
1994-06-08 23:23:02 +04:00
|
|
|
if (rc < 0)
|
1999-03-04 06:02:43 +03:00
|
|
|
err(1, "mount attempt on %s", mountpt);
|
1994-01-12 23:01:45 +03:00
|
|
|
|
2000-11-06 17:00:38 +03:00
|
|
|
/* mount(2) call succeeded, redirect standard streams to /dev/null */
|
|
|
|
freopen("/dev/null", "r", stdin);
|
|
|
|
freopen("/dev/null", "w", stdout);
|
1999-08-16 10:34:50 +04:00
|
|
|
#ifndef DEBUG
|
2000-11-06 17:00:38 +03:00
|
|
|
freopen("/dev/null", "w", stderr);
|
1999-08-16 10:34:50 +04:00
|
|
|
#endif
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start logging (and change name)
|
|
|
|
*/
|
2001-01-10 06:33:16 +03:00
|
|
|
openlog("portald", LOG_PID, LOG_DAEMON);
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
q.q_forw = q.q_back = &q;
|
|
|
|
readcf = 1;
|
|
|
|
|
|
|
|
signal(SIGCHLD, sigchld);
|
1995-06-06 23:51:19 +04:00
|
|
|
signal(SIGHUP, sighup);
|
1996-03-23 05:37:48 +03:00
|
|
|
signal(SIGTERM, sigterm);
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Just loop waiting for new connections and activating them
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
struct sockaddr_un un2;
|
|
|
|
int len2 = sizeof(un2);
|
|
|
|
int so2;
|
|
|
|
pid_t pid;
|
2002-09-21 22:24:40 +04:00
|
|
|
struct pollfd fdset[1];
|
1994-01-12 23:01:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether we need to re-read the configuration file
|
|
|
|
*/
|
|
|
|
if (readcf) {
|
|
|
|
readcf = 0;
|
|
|
|
conf_read(&q, conf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept a new connection
|
|
|
|
* Will get EINTR if a signal has arrived, so just
|
|
|
|
* ignore that error code
|
|
|
|
*/
|
2002-09-21 22:24:40 +04:00
|
|
|
fdset[0].fd = so;
|
|
|
|
fdset[0].events = POLLIN;
|
|
|
|
rc = poll(fdset, 1, INFTIM);
|
1994-01-12 23:01:45 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2002-09-21 22:24:40 +04:00
|
|
|
syslog(LOG_ERR, "poll: %m");
|
1994-01-12 23:01:45 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
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) {
|
1997-09-21 06:35:40 +04:00
|
|
|
syslog(LOG_ERR, "accept: %m");
|
1994-01-12 23:01:45 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now fork a new child to deal with the connection
|
|
|
|
*/
|
|
|
|
eagain:;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
sleep(1);
|
|
|
|
goto eagain;
|
|
|
|
}
|
2001-01-10 06:33:16 +03:00
|
|
|
syslog(LOG_WARNING, "fork: %m");
|
1994-01-12 23:01:45 +03:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
(void) close(so);
|
|
|
|
activate(&q, so2);
|
1994-06-08 23:23:02 +04:00
|
|
|
exit(0);
|
1994-01-12 23:01:45 +03:00
|
|
|
default:
|
|
|
|
(void) close(so2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
1994-06-08 23:23:02 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
1997-09-21 06:35:40 +04:00
|
|
|
|
1994-06-08 23:23:02 +04:00
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: mount_portal [-o options] config mount-point\n");
|
|
|
|
exit(1);
|
|
|
|
}
|