Clean (void) casts from sbin
[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  * $DragonFly: src/sbin/mount_portal/mount_portal.c,v 1.5 2004/12/18 21:43:39 swildner Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/wait.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <sys/stat.h>
47 #include <sys/syslog.h>
48 #include <sys/mount.h>
49
50 #include <err.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sysexits.h>
56 #include <unistd.h>
57
58 #include "mntopts.h"
59 #include "pathnames.h"
60 #include "portald.h"
61
62 struct mntopt mopts[] = {
63         MOPT_STDOPTS,
64         { NULL }
65 };
66
67 static void usage(void) __dead2;
68
69 static sig_atomic_t readcf;     /* Set when SIGHUP received */
70
71 static void sighup(int sig)
72 {
73         readcf ++;
74 }
75
76 static void sigchld(int sig)
77 {
78         pid_t pid;
79
80         while ((pid = waitpid((pid_t) -1, (int *) 0, WNOHANG)) > 0)
81                 ;
82         /* wrtp - waitpid _doesn't_ return 0 when no children! */
83 #ifdef notdef
84         if (pid < 0 && errno != ECHILD)
85                 syslog(LOG_WARNING, "waitpid: %s", strerror(errno));
86 #endif
87 }
88
89 int
90 main(int argc, char **argv)
91 {
92         struct portal_args args;
93         struct sockaddr_un un;
94         char *conf;
95         char mountpt[MAXPATHLEN];
96         int mntflags = 0;
97         char tag[32];
98         struct vfsconf vfc;
99         mode_t um;
100
101         qelem q;
102         int rc;
103         int so;
104         int error = 0;
105
106         /*
107          * Crack command line args
108          */
109         int ch;
110
111         while ((ch = getopt(argc, argv, "o:")) != -1) {
112                 switch (ch) {
113                 case 'o':
114                         getmntopts(optarg, mopts, &mntflags, 0);
115                         break;
116                 default:
117                         error = 1;
118                         break;
119                 }
120         }
121
122         if (optind != (argc - 2))
123                 error = 1;
124
125         if (error)
126                 usage();
127
128         /*
129          * Get config file and mount point
130          */
131         conf = argv[optind];
132
133         /* resolve the mountpoint with realpath(3) */
134         checkpath(argv[optind+1], mountpt);
135
136         /*
137          * Construct the listening socket
138          */
139         un.sun_family = AF_UNIX;
140         if (sizeof(_PATH_TMPPORTAL) >= sizeof(un.sun_path)) {
141                 errx(EX_SOFTWARE, "portal socket name too long");
142         }
143         strcpy(un.sun_path, _PATH_TMPPORTAL);
144         mktemp(un.sun_path);
145         un.sun_len = strlen(un.sun_path);
146
147         so = socket(AF_UNIX, SOCK_STREAM, 0);
148         if (so < 0) {
149                 err(EX_OSERR, "socket");
150         }
151         um = umask(077);
152         unlink(un.sun_path);
153         if (bind(so, (struct sockaddr *) &un, sizeof(un)) < 0)
154                 err(1, NULL);
155
156         unlink(un.sun_path);
157         umask(um);
158
159         listen(so, 5);
160
161         args.pa_socket = so;
162         sprintf(tag, "portal:%d", getpid());
163         args.pa_config = tag;
164
165         error = getvfsbyname("portal", &vfc);
166         if (error && vfsisloadable("portal")) {
167                 if (vfsload("portal"))
168                         err(EX_OSERR, "vfsload(portal)");
169                 endvfsent();
170                 error = getvfsbyname("portal", &vfc);
171         }
172         if (error)
173                 errx(EX_OSERR, "portal filesystem is not available");
174
175         rc = mount(vfc.vfc_name, mountpt, mntflags, &args);
176         if (rc < 0)
177                 err(1, NULL);
178
179         /*
180          * Everything is ready to go - now is a good time to fork
181          */
182 #ifndef DEBUG
183         daemon(0, 0);
184 #endif
185
186         /*
187          * Start logging (and change name)
188          */
189         openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
190
191         q.q_forw = q.q_back = &q;
192         readcf = 1;
193
194         signal(SIGCHLD, sigchld);
195         signal(SIGHUP, sighup);
196
197         /*
198          * Just loop waiting for new connections and activating them
199          */
200         for (;;) {
201                 struct sockaddr_un un2;
202                 int len2 = sizeof(un2);
203                 int so2;
204                 pid_t pid;
205                 fd_set fdset;
206                 int rc;
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, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
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 }