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