]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mount_portalfs/mount_portalfs.c
This commit was generated by cvs2svn to compensate for changes in r151513,
[FreeBSD/FreeBSD.git] / usr.sbin / mount_portalfs / mount_portalfs.c
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 char copyright[] =
35 "@(#) Copyright (c) 1992, 1993, 1994\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #if 0
40 static char sccsid[] = "@(#)mount_portal.c      8.6 (Berkeley) 4/26/95";
41 #endif
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/wait.h>
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <sys/stat.h>
51 #include <sys/syslog.h>
52 #include <sys/mount.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sysexits.h>
60 #include <unistd.h>
61
62 #include "mntopts.h"
63 #include "pathnames.h"
64 #include "portald.h"
65
66 struct mntopt mopts[] = {
67         MOPT_STDOPTS,
68         { NULL }
69 };
70
71 static void usage(void) __dead2;
72
73 static sig_atomic_t readcf;     /* Set when SIGHUP received */
74
75 static void sighup(sig)
76 int sig;
77 {
78         readcf ++;
79 }
80
81 static void sigchld(sig)
82 int sig;
83 {
84         pid_t pid;
85
86         while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
87                 ;
88         /* wrtp - waitpid _doesn't_ return 0 when no children! */
89 #ifdef notdef
90         if (pid < 0 && errno != ECHILD)
91                 syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
92 #endif
93 }
94
95 int
96 main(argc, argv)
97         int argc;
98         char *argv[];
99 {
100         struct portal_args args;
101         struct sockaddr_un un;
102         char *conf;
103         char mountpt[MAXPATHLEN];
104         int mntflags = 0;
105         char tag[32];
106         mode_t um;
107
108         qelem q;
109         int rc;
110         int so;
111         int error = 0;
112
113         /*
114          * Crack command line args
115          */
116         int ch;
117
118         while ((ch = getopt(argc, argv, "o:")) != -1) {
119                 switch (ch) {
120                 case 'o':
121                         getmntopts(optarg, mopts, &mntflags, 0);
122                         break;
123                 default:
124                         error = 1;
125                         break;
126                 }
127         }
128
129         if (optind != (argc - 2))
130                 error = 1;
131
132         if (error)
133                 usage();
134
135         /*
136          * Get config file and mount point
137          */
138         conf = argv[optind];
139         if (conf[0] != '/') {
140                 (void)fprintf(stderr,
141                     "The configuration file must be specified"
142                     "through an absolute file path.\n");
143                 exit(EX_USAGE);
144         }
145
146         /* resolve the mountpoint with realpath(3) */
147         (void)checkpath(argv[optind+1], mountpt);
148
149         /*
150          * Construct the listening socket
151          */
152         un.sun_family = AF_UNIX;
153         if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
154                 errx(EX_SOFTWARE, "portal socket name too long");
155         }
156         strcpy(un.sun_path, _PATH_TMPPORTAL);
157         mktemp(un.sun_path);
158         un.sun_len = strlen(un.sun_path);
159
160         so = socket(AF_UNIX, SOCK_STREAM, 0);
161         if (so < 0) {
162                 err(EX_OSERR, "socket");
163         }
164         um = umask(077);
165         (void) unlink(un.sun_path);
166         if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
167                 err(1, NULL);
168
169         (void) unlink(un.sun_path);
170         (void) umask(um);
171
172         (void) listen(so, 5);
173
174         args.pa_socket = so;
175         sprintf(tag, "portal:%d", getpid());
176         args.pa_config = tag;
177
178         rc = mount("portalfs", mountpt, mntflags, &args);
179         if (rc < 0)
180                 err(1, NULL);
181
182         /*
183          * Everything is ready to go - now is a good time to fork
184          */
185 #ifndef DEBUG
186         daemon(0, 0);
187 #endif
188
189         /*
190          * Start logging (and change name)
191          */
192         openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
193
194         q.q_forw = q.q_back = &q;
195         readcf = 1;
196
197         signal(SIGCHLD, sigchld);
198         signal(SIGHUP, sighup);
199
200         /*
201          * Just loop waiting for new connections and activating them
202          */
203         for (;;) {
204                 struct sockaddr_un un2;
205                 int len2 = sizeof(un2);
206                 int so2;
207                 pid_t pid;
208                 fd_set fdset;
209                 int rc;
210
211                 /*
212                  * Check whether we need to re-read the configuration file
213                  */
214                 if (readcf) {
215 #ifdef DEBUG
216                         printf ("re-reading configuration file\n");
217 #endif
218                         readcf = 0;
219                         conf_read(&q, conf);
220                         continue;
221                 }
222
223                 /*
224                  * Accept a new connection
225                  * Will get EINTR if a signal has arrived, so just
226                  * ignore that error code
227                  */
228                 FD_ZERO(&fdset);
229                 FD_SET(so, &fdset);
230                 rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
231                 if (rc < 0) {
232                         if (errno == EINTR)
233                                 continue;
234                         syslog(LOG_ERR, "select: %s", strerror(errno));
235                         exit(EX_OSERR);
236                 }
237                 if (rc == 0)
238                         break;
239                 so2 = accept(so, (struct sockaddr *) &un2, &len2);
240                 if (so2 < 0) {
241                         /*
242                          * The unmount function does a shutdown on the socket
243                          * which will generated ECONNABORTED on the accept.
244                          */
245                         if (errno == ECONNABORTED)
246                                 break;
247                         if (errno != EINTR) {
248                                 syslog(LOG_ERR, "accept: %s", strerror(errno));
249                                 exit(EX_OSERR);
250                         }
251                         continue;
252                 }
253
254                 /*
255                  * Now fork a new child to deal with the connection
256                  */
257         eagain:;
258                 switch (pid = fork()) {
259                 case -1:
260                         if (errno == EAGAIN) {
261                                 sleep(1);
262                                 goto eagain;
263                         }
264                         syslog(LOG_ERR, "fork: %s", strerror(errno));
265                         break;
266                 case 0:
267                         (void) close(so);
268                         activate(&q, so2);
269                         exit(0);
270                 default:
271                         (void) close(so2);
272                         break;
273                 }
274         }
275         syslog(LOG_INFO, "%s unmounted", mountpt);
276         exit(0);
277 }
278
279 static void
280 usage()
281 {
282         (void)fprintf(stderr,
283                 "usage: mount_portalfs [-o options] config mount-point\n");
284         exit(EX_USAGE);
285 }