Deleted Added
full compact
mount_portalfs.c (77042) mount_portalfs.c (77133)
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
38char 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
45static char sccsid[] = "@(#)mount_portal.c 8.6 (Berkeley) 4/26/95";
46#endif
47static const char rcsid[] =
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
38char 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
45static char sccsid[] = "@(#)mount_portal.c 8.6 (Berkeley) 4/26/95";
46#endif
47static const char rcsid[] =
48 "$FreeBSD: head/usr.sbin/mount_portalfs/mount_portalfs.c 77042 2001-05-23 14:58:19Z ru $";
48 "$FreeBSD: head/usr.sbin/mount_portalfs/mount_portalfs.c 77133 2001-05-24 15:20:11Z ru $";
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
71struct mntopt mopts[] = {
72 MOPT_STDOPTS,
73 { NULL }
74};
75
76static void usage __P((void)) __dead2;
77
78static sig_atomic_t readcf; /* Set when SIGHUP received */
79
80static void sighup(sig)
81int sig;
82{
83 readcf ++;
84}
85
86static void sigchld(sig)
87int 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
100int
101main(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 struct vfsconf vfc;
112 mode_t um;
113
114 qelem q;
115 int rc;
116 int so;
117 int error = 0;
118
119 /*
120 * Crack command line args
121 */
122 int ch;
123
124 while ((ch = getopt(argc, argv, "o:")) != -1) {
125 switch (ch) {
126 case 'o':
127 getmntopts(optarg, mopts, &mntflags, 0);
128 break;
129 default:
130 error = 1;
131 break;
132 }
133 }
134
135 if (optind != (argc - 2))
136 error = 1;
137
138 if (error)
139 usage();
140
141 /*
142 * Get config file and mount point
143 */
144 conf = argv[optind];
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
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
71struct mntopt mopts[] = {
72 MOPT_STDOPTS,
73 { NULL }
74};
75
76static void usage __P((void)) __dead2;
77
78static sig_atomic_t readcf; /* Set when SIGHUP received */
79
80static void sighup(sig)
81int sig;
82{
83 readcf ++;
84}
85
86static void sigchld(sig)
87int 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
100int
101main(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 struct vfsconf vfc;
112 mode_t um;
113
114 qelem q;
115 int rc;
116 int so;
117 int error = 0;
118
119 /*
120 * Crack command line args
121 */
122 int ch;
123
124 while ((ch = getopt(argc, argv, "o:")) != -1) {
125 switch (ch) {
126 case 'o':
127 getmntopts(optarg, mopts, &mntflags, 0);
128 break;
129 default:
130 error = 1;
131 break;
132 }
133 }
134
135 if (optind != (argc - 2))
136 error = 1;
137
138 if (error)
139 usage();
140
141 /*
142 * Get config file and mount point
143 */
144 conf = argv[optind];
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 error = getvfsbyname("portal", &vfc);
179 if (error && vfsisloadable("portal")) {
180 if (vfsload("portal"))
181 err(EX_OSERR, "vfsload(portal)");
178 error = getvfsbyname("portalfs", &vfc);
179 if (error && vfsisloadable("portalfs")) {
180 if (vfsload("portalfs"))
181 err(EX_OSERR, "vfsload(portalfs)");
182 endvfsent();
182 endvfsent();
183 error = getvfsbyname("portal", &vfc);
183 error = getvfsbyname("portalfs", &vfc);
184 }
185 if (error)
186 errx(EX_OSERR, "portal filesystem is not available");
187
188 rc = mount(vfc.vfc_name, mountpt, mntflags, &args);
189 if (rc < 0)
190 err(1, NULL);
191
192 /*
193 * Everything is ready to go - now is a good time to fork
194 */
195#ifndef DEBUG
196 daemon(0, 0);
197#endif
198
199 /*
200 * Start logging (and change name)
201 */
202 openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
203
204 q.q_forw = q.q_back = &q;
205 readcf = 1;
206
207 signal(SIGCHLD, sigchld);
208 signal(SIGHUP, sighup);
209
210 /*
211 * Just loop waiting for new connections and activating them
212 */
213 for (;;) {
214 struct sockaddr_un un2;
215 int len2 = sizeof(un2);
216 int so2;
217 pid_t pid;
218 fd_set fdset;
219 int rc;
220
221 /*
222 * Check whether we need to re-read the configuration file
223 */
224 if (readcf) {
225#ifdef DEBUG
226 printf ("re-reading configuration file\n");
227#endif
228 readcf = 0;
229 conf_read(&q, conf);
230 continue;
231 }
232
233 /*
234 * Accept a new connection
235 * Will get EINTR if a signal has arrived, so just
236 * ignore that error code
237 */
238 FD_ZERO(&fdset);
239 FD_SET(so, &fdset);
240 rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
241 if (rc < 0) {
242 if (errno == EINTR)
243 continue;
244 syslog(LOG_ERR, "select: %s", strerror(errno));
245 exit(EX_OSERR);
246 }
247 if (rc == 0)
248 break;
249 so2 = accept(so, (struct sockaddr *) &un2, &len2);
250 if (so2 < 0) {
251 /*
252 * The unmount function does a shutdown on the socket
253 * which will generated ECONNABORTED on the accept.
254 */
255 if (errno == ECONNABORTED)
256 break;
257 if (errno != EINTR) {
258 syslog(LOG_ERR, "accept: %s", strerror(errno));
259 exit(EX_OSERR);
260 }
261 continue;
262 }
263
264 /*
265 * Now fork a new child to deal with the connection
266 */
267 eagain:;
268 switch (pid = fork()) {
269 case -1:
270 if (errno == EAGAIN) {
271 sleep(1);
272 goto eagain;
273 }
274 syslog(LOG_ERR, "fork: %s", strerror(errno));
275 break;
276 case 0:
277 (void) close(so);
278 activate(&q, so2);
279 exit(0);
280 default:
281 (void) close(so2);
282 break;
283 }
284 }
285 syslog(LOG_INFO, "%s unmounted", mountpt);
286 exit(0);
287}
288
289static void
290usage()
291{
292 (void)fprintf(stderr,
293 "usage: mount_portalfs [-o options] config mount-point\n");
294 exit(EX_USAGE);
295}
184 }
185 if (error)
186 errx(EX_OSERR, "portal filesystem is not available");
187
188 rc = mount(vfc.vfc_name, mountpt, mntflags, &args);
189 if (rc < 0)
190 err(1, NULL);
191
192 /*
193 * Everything is ready to go - now is a good time to fork
194 */
195#ifndef DEBUG
196 daemon(0, 0);
197#endif
198
199 /*
200 * Start logging (and change name)
201 */
202 openlog("portald", LOG_CONS|LOG_PID, LOG_DAEMON);
203
204 q.q_forw = q.q_back = &q;
205 readcf = 1;
206
207 signal(SIGCHLD, sigchld);
208 signal(SIGHUP, sighup);
209
210 /*
211 * Just loop waiting for new connections and activating them
212 */
213 for (;;) {
214 struct sockaddr_un un2;
215 int len2 = sizeof(un2);
216 int so2;
217 pid_t pid;
218 fd_set fdset;
219 int rc;
220
221 /*
222 * Check whether we need to re-read the configuration file
223 */
224 if (readcf) {
225#ifdef DEBUG
226 printf ("re-reading configuration file\n");
227#endif
228 readcf = 0;
229 conf_read(&q, conf);
230 continue;
231 }
232
233 /*
234 * Accept a new connection
235 * Will get EINTR if a signal has arrived, so just
236 * ignore that error code
237 */
238 FD_ZERO(&fdset);
239 FD_SET(so, &fdset);
240 rc = select(so+1, &fdset, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
241 if (rc < 0) {
242 if (errno == EINTR)
243 continue;
244 syslog(LOG_ERR, "select: %s", strerror(errno));
245 exit(EX_OSERR);
246 }
247 if (rc == 0)
248 break;
249 so2 = accept(so, (struct sockaddr *) &un2, &len2);
250 if (so2 < 0) {
251 /*
252 * The unmount function does a shutdown on the socket
253 * which will generated ECONNABORTED on the accept.
254 */
255 if (errno == ECONNABORTED)
256 break;
257 if (errno != EINTR) {
258 syslog(LOG_ERR, "accept: %s", strerror(errno));
259 exit(EX_OSERR);
260 }
261 continue;
262 }
263
264 /*
265 * Now fork a new child to deal with the connection
266 */
267 eagain:;
268 switch (pid = fork()) {
269 case -1:
270 if (errno == EAGAIN) {
271 sleep(1);
272 goto eagain;
273 }
274 syslog(LOG_ERR, "fork: %s", strerror(errno));
275 break;
276 case 0:
277 (void) close(so);
278 activate(&q, so2);
279 exit(0);
280 default:
281 (void) close(so2);
282 break;
283 }
284 }
285 syslog(LOG_INFO, "%s unmounted", mountpt);
286 exit(0);
287}
288
289static void
290usage()
291{
292 (void)fprintf(stderr,
293 "usage: mount_portalfs [-o options] config mount-point\n");
294 exit(EX_USAGE);
295}