]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mount_portalfs/mount_portalfs.c
Changed an LSDEV 1 to LSDEV 8 .
[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  * 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 static char sccsid[] = "@(#)mount_portal.c      8.4 (Berkeley) 3/27/94";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/wait.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
51 #include <sys/syslog.h>
52 #include <sys/mount.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.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 __P((void));
72
73 static sig_atomic_t readcf;     /* Set when SIGHUP received */
74
75 static void sigchld(sig)
76 int sig;
77 {
78         pid_t pid;
79
80         while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
81                 ;
82         if (pid < 0)
83                 syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
84 }
85
86 int
87 main(argc, argv)
88         int argc;
89         char *argv[];
90 {
91         struct portal_args args;
92         struct sockaddr_un un;
93         char *conf;
94         char *mountpt;
95         int mntflags = 0;
96         char tag[32];
97         struct vfsconf *vfc;
98
99         qelem q;
100         int rc;
101         int so;
102         int error = 0;
103
104         /*
105          * Crack command line args
106          */
107         int ch;
108
109         while ((ch = getopt(argc, argv, "o:")) != EOF) {
110                 switch (ch) {
111                 case 'o':
112                         getmntopts(optarg, mopts, &mntflags, 0);
113                         break;
114                 default:
115                         error = 1;
116                         break;
117                 }
118         }
119
120         if (optind != (argc - 2))
121                 error = 1;
122
123         if (error)
124                 usage();
125
126         /*
127          * Get config file and mount point
128          */
129         conf = argv[optind];
130         mountpt = argv[optind+1];
131
132         /*
133          * Construct the listening socket
134          */
135         un.sun_family = AF_UNIX;
136         if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
137                 fprintf(stderr, "mount_portal: portal socket name too long\n");
138                 exit(1);
139         }
140         strcpy(un.sun_path, _PATH_TMPPORTAL);
141         mktemp(un.sun_path);
142         un.sun_len = strlen(un.sun_path);
143
144         so = socket(AF_UNIX, SOCK_STREAM, 0);
145         if (so < 0) {
146                 fprintf(stderr, "mount_portal: socket: %s\n", strerror(errno));
147                 exit(1);
148         }
149         (void) unlink(un.sun_path);
150         if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
151                 err(1, NULL);
152         (void) unlink(un.sun_path);
153
154         (void) listen(so, 5);
155
156         args.pa_socket = so;
157         sprintf(tag, "portal:%d", getpid());
158         args.pa_config = tag;
159
160         vfc = getvfsbyname("portal");
161         if(!vfc && vfsisloadable("portal")) {
162                 if(vfsload("portal"))
163                         err(1, "vfsload(portal)");
164                 endvfsent();    /* flush cache */
165                 vfc = getvfsbyname("portal");
166         }
167
168         rc = mount(vfc ? vfc->vfc_index : MOUNT_PORTAL, mountpt, mntflags, &args);
169         if (rc < 0)
170                 err(1, NULL);
171
172 #ifdef notdef
173         /*
174          * Everything is ready to go - now is a good time to fork
175          */
176         daemon(0, 0);
177 #endif
178
179         /*
180          * Start logging (and change name)
181          */
182         openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
183
184         q.q_forw = q.q_back = &q;
185         readcf = 1;
186
187         signal(SIGCHLD, sigchld);
188
189         /*
190          * Just loop waiting for new connections and activating them
191          */
192         for (;;) {
193                 struct sockaddr_un un2;
194                 int len2 = sizeof(un2);
195                 int so2;
196                 pid_t pid;
197                 fd_set fdset;
198                 int rc;
199
200                 /*
201                  * Check whether we need to re-read the configuration file
202                  */
203                 if (readcf) {
204                         readcf = 0;
205                         conf_read(&q, conf);
206                         continue;
207                 }
208         
209                 /*
210                  * Accept a new connection
211                  * Will get EINTR if a signal has arrived, so just
212                  * ignore that error code
213                  */
214                 FD_SET(so, &fdset);
215                 rc = select(so+1, &fdset, (void *) 0, (void *) 0, (void *) 0);
216                 if (rc < 0) {
217                         if (errno == EINTR)
218                                 continue;
219                         syslog(LOG_ERR, "select: %s", strerror(errno));
220                         exit(1);
221                 }
222                 if (rc == 0)
223                         break;
224                 so2 = accept(so, (struct sockaddr *) &un2, &len2);
225                 if (so2 < 0) {
226                         /*
227                          * The unmount function does a shutdown on the socket
228                          * which will generated ECONNABORTED on the accept.
229                          */
230                         if (errno == ECONNABORTED)
231                                 break;
232                         if (errno != EINTR) {
233                                 syslog(LOG_ERR, "accept: %s", strerror(errno));
234                                 exit(1);
235                         }
236                         continue;
237                 }
238
239                 /*
240                  * Now fork a new child to deal with the connection
241                  */
242         eagain:;
243                 switch (pid = fork()) {
244                 case -1:
245                         if (errno == EAGAIN) {
246                                 sleep(1);
247                                 goto eagain;
248                         }
249                         syslog(LOG_ERR, "fork: %s", strerror(errno));
250                         break;
251                 case 0:
252                         (void) close(so);
253                         activate(&q, so2);
254                         break;
255                 default:
256                         (void) close(so2);
257                         break;
258                 }
259         }
260         syslog(LOG_INFO, "%s unmounted", mountpt);
261         exit(0);
262 }
263
264 static void
265 usage()
266 {
267         (void)fprintf(stderr,
268                 "usage: mount_portal [-o options] config mount-point\n");
269         exit(1);
270 }