 |
|
|
| |
1 /*
2 * Copyright (c) 1997-2005 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *
40 * File: am-utils/amd/nfs_start.c
41 *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 #include <am_defs.h>
48 #include <amd.h>
49
50 #ifndef SELECT_MAXWAIT
51 # define SELECT_MAXWAIT 16
52 #endif /* not SELECT_MAXWAIT */
53
54 SVCXPRT *nfsxprt = NULL;
55 u_short nfs_port = 0;
56
57 #ifndef HAVE_SIGACTION
58 # define MASKED_SIGS (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
59 #endif /* not HAVE_SIGACTION */
60
61 #ifdef DEBUG
62 /*
63 * Check that we are not burning resources
64 */
65 static void
66 checkup(void)
67 {
68
69 static int max_fd = 0;
70 static char *max_mem = 0;
71
72 int next_fd = dup(0);
73 caddr_t next_mem = sbrk(0);
74 close(next_fd);
75
76 if (max_fd < next_fd) {
77 dlog("%d new fds allocated; total is %d",
78 next_fd - max_fd, next_fd);
79 max_fd = next_fd;
80 }
81 if (max_mem < next_mem) {
82 #ifdef HAVE_GETPAGESIZE
83 dlog("%#lx bytes of memory allocated; total is %#lx (%ld pages)",
84 (long) (next_mem - max_mem), (unsigned long) next_mem,
85 ((long) next_mem + getpagesize() - 1) / (long) getpagesize());
86 #else /* not HAVE_GETPAGESIZE */
87 dlog("%#lx bytes of memory allocated; total is %#lx",
88 (long) (next_mem - max_mem), (unsigned long) next_mem);
89 #endif /* not HAVE_GETPAGESIZE */
90 max_mem = next_mem;
91
92 }
93 }
94 #else /* not DEBUG */
95 #define checkup()
96 #endif /* not DEBUG */
97
98
99 static int
100 #ifdef HAVE_SIGACTION
101 do_select(sigset_t smask, int fds, fd_set *fdp, struct timeval *tvp)
102 #else /* not HAVE_SIGACTION */
103 do_select(int smask, int fds, fd_set *fdp, struct timeval *tvp)
104 #endif /* not HAVE_SIGACTION */
105 {
106
107 int sig;
108 int nsel;
109
110 if ((sig = setjmp(select_intr))) {
111 select_intr_valid = 0;
112 /* Got a signal */
113 switch (sig) {
114 case SIGINT:
115 case SIGTERM:
116 amd_state = Finishing;
117 reschedule_timeout_mp();
118 break;
119 }
120 nsel = -1;
121 errno = EINTR;
122 } else {
123 select_intr_valid = 1;
124 /*
125 * Invalidate the current clock value
126 */
127 clock_valid = 0;
128 /*
129 * Allow interrupts. If a signal
130 * occurs, then it will cause a longjmp
131 * up above.
132 */
133 #ifdef HAVE_SIGACTION
134 sigprocmask(SIG_SETMASK, &smask, NULL);
135 #else /* not HAVE_SIGACTION */
136 (void) sigsetmask(smask);
137 #endif /* not HAVE_SIGACTION */
138
139 /*
140 * Wait for input
141 */
142 nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
143 tvp->tv_sec ? tvp : (struct timeval *) 0);
144 }
145
146 #ifdef HAVE_SIGACTION
147 sigprocmask(SIG_BLOCK, &masked_sigs, NULL);
148 #else /* not HAVE_SIGACTION */
149 (void) sigblock(MASKED_SIGS);
150 #endif /* not HAVE_SIGACTION */
151
152 /*
153 * Perhaps reload the cache?
154 */
155 if (do_mapc_reload < clocktime()) {
156 mapc_reload();
157 do_mapc_reload = clocktime() + gopt.map_reload_interval;
158 }
159 return nsel;
160 }
161
162
163 /*
164 * Determine whether anything is left in
165 * the RPC input queue.
166 */
167 static int
168 rpc_pending_now(void)
169 {
170 struct timeval tvv;
171 int nsel;
172 fd_set readfds;
173
174 FD_ZERO(&readfds);
175 FD_SET(fwd_sock, &readfds);
176
177 tvv.tv_sec = tvv.tv_usec = 0;
178 nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
179 if (nsel < 1)
180 return (0);
181 if (FD_ISSET(fwd_sock, &readfds))
182 return (1);
183
184 return (0);
185 }
186
187
188 static serv_state
189 run_rpc(void)
190 {
191 #ifdef HAVE_SIGACTION
192 sigset_t smask;
193 sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
194 #else /* not HAVE_SIGACTION */
195 int smask = sigblock(MASKED_SIGS);
196 #endif /* not HAVE_SIGACTION */
197
198 next_softclock = clocktime();
199
200 amd_state = Run;
201
202 /*
203 * Keep on trucking while we are in Run mode. This state
204 * is switched to Quit after all the file systems have
205 * been unmounted.
206 */
207 while ((int) amd_state <= (int) Finishing) {
208 struct timeval tvv;
209 int nsel;
210 time_t now;
211 fd_set readfds;
212
213 #ifdef HAVE_SVC_GETREQSET
214 memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
215 #else /* not HAVE_SVC_GETREQSET */
216 FD_ZERO(&readfds);
217 # ifdef HAVE_FD_SET_FDS_BITS
218 readfds.fds_bits[0] = svc_fds;
219 # else /* not HAVE_FD_SET_FDS_BITS */
220 readfds = svc_fds;
221 # endif /* not HAVE_FD_SET_FDS_BITS */
222 #endif /* not HAVE_SVC_GETREQSET */
223 FD_SET(fwd_sock, &readfds);
224
225 checkup();
226
227 /*
228 * If the full timeout code is not called,
229 * then recompute the time delta manually.
230 */
231 now = clocktime();
232
233 if (next_softclock <= now) {
234 if (amd_state == Finishing)
235 umount_exported();
236 tvv.tv_sec = softclock();
237 } else {
238 tvv.tv_sec = next_softclock - now;
239 }
240 tvv.tv_usec = 0;
241
242 if (amd_state == Finishing && get_exported_ap(0) == 0) {
243 flush_mntfs();
244 amd_state = Quit;
245 break;
246 }
247
248 #ifdef HAVE_FS_AUTOFS
249 autofs_add_fdset(&readfds);
250 #endif /* HAVE_FS_AUTOFS */
251
252 if (tvv.tv_sec <= 0)
253 tvv.tv_sec = SELECT_MAXWAIT;
254 if (tvv.tv_sec) {
255 dlog("Select waits for %ds", (int) tvv.tv_sec);
256 } else {
257 dlog("Select waits for Godot");
258 }
259
260 nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
261
262 switch (nsel) {
263 case -1:
264 if (errno == EINTR) {
265 dlog("select interrupted");
266 continue;
267 }
268 plog(XLOG_ERROR, "select: %m");
269 break;
270
271 case 0:
272 break;
273
274 default:
275 /*
276 * Read all pending NFS responses at once to avoid having responses
277 * queue up as a consequence of retransmissions.
278 */
279 if (FD_ISSET(fwd_sock, &readfds)) {
280 FD_CLR(fwd_sock, &readfds);
281 --nsel;
282 do {
283 fwd_reply();
284 } while (rpc_pending_now() > 0);
285 }
286
287 #ifdef HAVE_FS_AUTOFS
288 if (nsel)
289 nsel = autofs_handle_fdset(&readfds, nsel);
290 #endif /* HAVE_FS_AUTOFS */
291
292 if (nsel) {
293 /*
294 * Anything left must be a normal
295 * RPC request.
296 */
297 #ifdef HAVE_SVC_GETREQSET
298 svc_getreqset(&readfds);
299 #else /* not HAVE_SVC_GETREQSET */
300 # ifdef HAVE_FD_SET_FDS_BITS
301 svc_getreq(readfds.fds_bits[0]);
302 # else /* not HAVE_FD_SET_FDS_BITS */
303 svc_getreq(readfds);
304 # endif /* not HAVE_FD_SET_FDS_BITS */
305 #endif /* not HAVE_SVC_GETREQSET */
306 }
307 break;
308 }
309 }
310
311 #ifdef HAVE_SIGACTION
312 sigprocmask(SIG_SETMASK, &smask, NULL);
313 #else /* not HAVE_SIGACTION */
314 (void) sigsetmask(smask);
315 #endif /* not HAVE_SIGACTION */
316
317 if (amd_state == Quit)
318 amd_state = Done;
319
320 return amd_state;
321 }
322
323
324 int
325 mount_automounter(int ppid)
326 {
327 /*
328 * Old code replaced by rpc-trash patch.
329 * Erez Zadok <ezk@cs.columbia.edu>
330 int so = socket(AF_INET, SOCK_DGRAM, 0);
331 */
332 SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
333 int nmount, ret;
334 int soNFS;
335 int udp_soAMQ, tcp_soAMQ;
336 struct netconfig *udp_amqncp, *tcp_amqncp;
337
338 /*
339 * This must be done first, because it attempts to bind
340 * to various UDP ports and we don't want anything else
341 * potentially taking over those ports before we get a chance
342 * to reserve them.
343 */
344 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
345 restart_automounter_nodes();
346
347 /*
348 * Start RPC forwarding
349 */
350 if (fwd_init() != 0)
351 return 3;
352
353 /*
354 * Construct the root automount node
355 */
356 make_root_node();
357
358 /*
359 * Pick up the pieces from a previous run
360 * This is likely to (indirectly) need the rpc_fwd package
361 * so it *must* come after the call to fwd_init().
362 */
363 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
364 restart();
365
366 /*
367 * Create the nfs service for amd
368 * If nfs_port is already initialized, it means we
369 * already created the service during restart_automounter_nodes().
370 */
371 if (nfs_port == 0) {
372 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
373 if (ret != 0)
374 return ret;
375 }
376 sprintf(pid_fsname, "%s:(pid%ld,port%u)", am_get_hostname(), (long) am_mypid, nfs_port);
377
378 /* security: if user sets -D amq, don't even create listening socket */
379 if (!amuDebug(D_AMQ)) {
380 ret = create_amq_service(&udp_soAMQ,
381 &udp_amqp,
382 &udp_amqncp,
383 &tcp_soAMQ,
384 &tcp_amqp,
385 &tcp_amqncp,
386 gopt.preferred_amq_port);
387 if (ret != 0)
388 return ret;
389 }
390
391 #ifdef HAVE_FS_AUTOFS
392 if (amd_use_autofs) {
393 /*
394 * Create the autofs service for amd.
395 */
396 ret = create_autofs_service();
397 /* if autofs service fails it is OK if using a test amd */
398 if (ret != 0) {
399 plog(XLOG_WARNING, "autofs service registration failed, turning off autofs support");
400 amd_use_autofs = 0;
401 }
402 }
403 #endif /* HAVE_FS_AUTOFS */
404
405 /*
406 * Mount the top-level auto-mountpoints
407 */
408 nmount = mount_exported();
409
410 /*
411 * Now safe to tell parent that we are up and running
412 */
413 if (ppid)
414 kill(ppid, SIGQUIT);
415
416 if (nmount == 0) {
417 plog(XLOG_FATAL, "No work to do - quitting");
418 amd_state = Done;
419 return 0;
420 }
421
422 if (!amuDebug(D_AMQ)) {
423 /*
424 * Complete registration of amq (first TCP service then UDP)
425 */
426 unregister_amq();
427
428 ret = amu_svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
429 amq_program_1, IPPROTO_TCP, tcp_amqncp);
430 if (ret != 1) {
431 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
432 return 3;
433 }
434
435 ret = amu_svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
436 amq_program_1, IPPROTO_UDP, udp_amqncp);
437 if (ret != 1) {
438 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, udp)", get_amd_program_number());
439 return 4;
440 }
441 }
442
443 /*
444 * Start timeout_mp rolling
445 */
446 reschedule_timeout_mp();
447
448 /*
449 * Start the server
450 */
451 if (run_rpc() != Done) {
452 plog(XLOG_FATAL, "run_rpc failed");
453 amd_state = Done;
454 }
455 return 0;
456 }
457