2 * Copyright (c) 1997-2006 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.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
40 * File: am-utils/amd/nfs_start.c
46 #endif /* HAVE_CONFIG_H */
50 #ifndef SELECT_MAXWAIT
51 # define SELECT_MAXWAIT 16
52 #endif /* not SELECT_MAXWAIT */
54 SVCXPRT *nfsxprt = NULL;
57 #ifndef HAVE_SIGACTION
58 # define MASKED_SIGS (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
59 #endif /* not HAVE_SIGACTION */
63 * Check that we are not burning resources
69 static int max_fd = 0;
70 static char *max_mem = 0;
73 caddr_t next_mem = sbrk(0);
76 if (max_fd < next_fd) {
77 dlog("%d new fds allocated; total is %d",
78 next_fd - max_fd, next_fd);
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 */
96 #endif /* not DEBUG */
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 */
110 if ((sig = setjmp(select_intr))) {
111 select_intr_valid = 0;
116 amd_state = Finishing;
117 reschedule_timeout_mp();
123 select_intr_valid = 1;
125 * Allow interrupts. If a signal
126 * occurs, then it will cause a longjmp
129 #ifdef HAVE_SIGACTION
130 sigprocmask(SIG_SETMASK, &smask, NULL);
131 #else /* not HAVE_SIGACTION */
132 (void) sigsetmask(smask);
133 #endif /* not HAVE_SIGACTION */
138 nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
139 tvp->tv_sec ? tvp : (struct timeval *) 0);
142 #ifdef HAVE_SIGACTION
143 sigprocmask(SIG_BLOCK, &masked_sigs, NULL);
144 #else /* not HAVE_SIGACTION */
145 (void) sigblock(MASKED_SIGS);
146 #endif /* not HAVE_SIGACTION */
149 * Perhaps reload the cache?
151 if (do_mapc_reload < clocktime(NULL)) {
153 do_mapc_reload = clocktime(NULL) + gopt.map_reload_interval;
160 * Determine whether anything is left in
161 * the RPC input queue.
164 rpc_pending_now(void)
171 FD_SET(fwd_sock, &readfds);
173 tvv.tv_sec = tvv.tv_usec = 0;
174 nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
177 if (FD_ISSET(fwd_sock, &readfds))
187 #ifdef HAVE_SIGACTION
189 sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
190 #else /* not HAVE_SIGACTION */
191 int smask = sigblock(MASKED_SIGS);
192 #endif /* not HAVE_SIGACTION */
194 next_softclock = clocktime(NULL);
199 * Keep on trucking while we are in Run mode. This state
200 * is switched to Quit after all the file systems have
203 while ((int) amd_state <= (int) Finishing) {
209 #ifdef HAVE_SVC_GETREQSET
210 memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
211 #else /* not HAVE_SVC_GETREQSET */
213 # ifdef HAVE_FD_SET_FDS_BITS
214 readfds.fds_bits[0] = svc_fds;
215 # else /* not HAVE_FD_SET_FDS_BITS */
217 # endif /* not HAVE_FD_SET_FDS_BITS */
218 #endif /* not HAVE_SVC_GETREQSET */
219 FD_SET(fwd_sock, &readfds);
224 * If the full timeout code is not called,
225 * then recompute the time delta manually.
227 now = clocktime(NULL);
229 if (next_softclock <= now) {
230 if (amd_state == Finishing)
232 tvv.tv_sec = softclock();
234 tvv.tv_sec = next_softclock - now;
238 if (amd_state == Finishing && get_exported_ap(0) == NULL) {
244 #ifdef HAVE_FS_AUTOFS
245 autofs_add_fdset(&readfds);
246 #endif /* HAVE_FS_AUTOFS */
249 tvv.tv_sec = SELECT_MAXWAIT;
251 dlog("Select waits for %ds", (int) tvv.tv_sec);
253 dlog("Select waits for Godot");
256 nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
260 if (errno == EINTR) {
261 dlog("select interrupted");
264 plog(XLOG_ERROR, "select: %m");
272 * Read all pending NFS responses at once to avoid having responses
273 * queue up as a consequence of retransmissions.
275 if (FD_ISSET(fwd_sock, &readfds)) {
276 FD_CLR(fwd_sock, &readfds);
280 } while (rpc_pending_now() > 0);
283 #ifdef HAVE_FS_AUTOFS
285 nsel = autofs_handle_fdset(&readfds, nsel);
286 #endif /* HAVE_FS_AUTOFS */
290 * Anything left must be a normal
293 #ifdef HAVE_SVC_GETREQSET
294 svc_getreqset(&readfds);
295 #else /* not HAVE_SVC_GETREQSET */
296 # ifdef HAVE_FD_SET_FDS_BITS
297 svc_getreq(readfds.fds_bits[0]);
298 # else /* not HAVE_FD_SET_FDS_BITS */
300 # endif /* not HAVE_FD_SET_FDS_BITS */
301 #endif /* not HAVE_SVC_GETREQSET */
307 #ifdef HAVE_SIGACTION
308 sigprocmask(SIG_SETMASK, &smask, NULL);
309 #else /* not HAVE_SIGACTION */
310 (void) sigsetmask(smask);
311 #endif /* not HAVE_SIGACTION */
313 if (amd_state == Quit)
321 mount_automounter(int ppid)
324 * Old code replaced by rpc-trash patch.
325 * Erez Zadok <ezk@cs.columbia.edu>
326 int so = socket(AF_INET, SOCK_DGRAM, 0);
328 SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
331 int udp_soAMQ, tcp_soAMQ;
332 struct netconfig *udp_amqncp, *tcp_amqncp;
335 * This must be done first, because it attempts to bind
336 * to various UDP ports and we don't want anything else
337 * potentially taking over those ports before we get a chance
340 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
341 restart_automounter_nodes();
344 * Start RPC forwarding
350 * Construct the root automount node
355 * Pick up the pieces from a previous run
356 * This is likely to (indirectly) need the rpc_fwd package
357 * so it *must* come after the call to fwd_init().
359 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
363 * Create the nfs service for amd
364 * If nfs_port is already initialized, it means we
365 * already created the service during restart_automounter_nodes().
368 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
372 xsnprintf(pid_fsname, sizeof(pid_fsname), "%s:(pid%ld,port%u)",
373 am_get_hostname(), (long) am_mypid, nfs_port);
375 /* security: if user sets -D amq, don't even create listening socket */
376 if (!amuDebug(D_AMQ)) {
377 ret = create_amq_service(&udp_soAMQ,
383 gopt.preferred_amq_port);
388 #ifdef HAVE_FS_AUTOFS
389 if (amd_use_autofs) {
391 * Create the autofs service for amd.
393 ret = create_autofs_service();
394 /* if autofs service fails it is OK if using a test amd */
396 plog(XLOG_WARNING, "autofs service registration failed, turning off autofs support");
400 #endif /* HAVE_FS_AUTOFS */
403 * Mount the top-level auto-mountpoints
405 nmount = mount_exported();
408 * Now safe to tell parent that we are up and running
414 plog(XLOG_FATAL, "No work to do - quitting");
419 if (!amuDebug(D_AMQ)) {
421 * Complete registration of amq (first TCP service then UDP)
425 ret = amu_svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
426 amq_program_1, IPPROTO_TCP, tcp_amqncp);
428 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
432 ret = amu_svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
433 amq_program_1, IPPROTO_UDP, udp_amqncp);
435 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, udp)", get_amd_program_number());
441 * Start timeout_mp rolling
443 reschedule_timeout_mp();
448 if (run_rpc() != Done) {
449 plog(XLOG_FATAL, "run_rpc failed");