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