2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/nfs_start.c
42 #endif /* HAVE_CONFIG_H */
46 #ifndef SELECT_MAXWAIT
47 # define SELECT_MAXWAIT 16
48 #endif /* not SELECT_MAXWAIT */
50 SVCXPRT *nfsxprt = NULL;
53 #ifndef HAVE_SIGACTION
54 # define MASKED_SIGS (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
55 #endif /* not HAVE_SIGACTION */
59 * Check that we are not burning resources
64 static int max_fd = 0;
65 static char *max_mem = NULL;
67 caddr_t next_mem = sbrk(0);
71 if (max_fd < next_fd) {
72 dlog("%d new fds allocated; total is %d",
73 next_fd - max_fd, next_fd);
76 if (max_mem < next_mem) {
77 #ifdef HAVE_GETPAGESIZE
78 dlog("%#lx bytes of memory allocated; total is %#lx (%ld pages)",
79 (long) (next_mem - max_mem), (unsigned long) next_mem,
80 ((long) next_mem + getpagesize() - 1) / (long) getpagesize());
81 #else /* not HAVE_GETPAGESIZE */
82 dlog("%#lx bytes of memory allocated; total is %#lx",
83 (long) (next_mem - max_mem), (unsigned long) next_mem);
84 #endif /* not HAVE_GETPAGESIZE */
91 #endif /* not DEBUG */
96 do_select(sigset_t smask, int fds, fd_set *fdp, struct timeval *tvp)
97 #else /* not HAVE_SIGACTION */
98 do_select(int smask, int fds, fd_set *fdp, struct timeval *tvp)
99 #endif /* not HAVE_SIGACTION */
105 if ((sig = setjmp(select_intr))) {
106 select_intr_valid = 0;
111 amd_state = Finishing;
112 reschedule_timeout_mp();
118 select_intr_valid = 1;
120 * Allow interrupts. If a signal
121 * occurs, then it will cause a longjmp
124 #ifdef HAVE_SIGACTION
125 sigprocmask(SIG_SETMASK, &smask, NULL);
126 #else /* not HAVE_SIGACTION */
127 (void) sigsetmask(smask);
128 #endif /* not HAVE_SIGACTION */
133 nsel = select(fds, fdp, (fd_set *) NULL, (fd_set *) NULL,
134 tvp->tv_sec ? tvp : (struct timeval *) NULL);
137 #ifdef HAVE_SIGACTION
138 sigprocmask(SIG_BLOCK, &masked_sigs, NULL);
139 #else /* not HAVE_SIGACTION */
140 (void) sigblock(MASKED_SIGS);
141 #endif /* not HAVE_SIGACTION */
144 * Perhaps reload the cache?
146 if (do_mapc_reload < clocktime(NULL)) {
148 do_mapc_reload = clocktime(NULL) + gopt.map_reload_interval;
155 * Determine whether anything is left in
156 * the RPC input queue.
159 rpc_pending_now(void)
166 FD_SET(fwd_sock, &readfds);
168 tvv.tv_sec = tvv.tv_usec = 0;
169 nsel = select(FD_SETSIZE, &readfds, (fd_set *) NULL, (fd_set *) NULL, &tvv);
172 if (FD_ISSET(fwd_sock, &readfds))
182 #ifdef HAVE_SIGACTION
184 sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
185 #else /* not HAVE_SIGACTION */
186 int smask = sigblock(MASKED_SIGS);
187 #endif /* not HAVE_SIGACTION */
189 next_softclock = clocktime(NULL);
194 * Keep on trucking while we are in Run mode. This state
195 * is switched to Quit after all the file systems have
198 while ((int) amd_state <= (int) Finishing) {
204 #ifdef HAVE_SVC_GETREQSET
205 memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
206 #else /* not HAVE_SVC_GETREQSET */
208 # ifdef HAVE_FD_SET_FDS_BITS
209 readfds.fds_bits[0] = svc_fds;
210 # else /* not HAVE_FD_SET_FDS_BITS */
212 # endif /* not HAVE_FD_SET_FDS_BITS */
213 #endif /* not HAVE_SVC_GETREQSET */
214 FD_SET(fwd_sock, &readfds);
219 * If the full timeout code is not called,
220 * then recompute the time delta manually.
222 now = clocktime(NULL);
224 if (next_softclock <= now) {
225 if (amd_state == Finishing)
227 tvv.tv_sec = softclock();
229 tvv.tv_sec = next_softclock - now;
233 if (amd_state == Finishing && get_exported_ap(0) == NULL) {
239 #ifdef HAVE_FS_AUTOFS
240 autofs_add_fdset(&readfds);
241 #endif /* HAVE_FS_AUTOFS */
244 tvv.tv_sec = SELECT_MAXWAIT;
246 dlog("Select waits for %ds", (int) tvv.tv_sec);
248 dlog("Select waits for Godot");
251 nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
255 if (errno == EINTR) {
256 dlog("select interrupted");
259 plog(XLOG_ERROR, "select: %m");
267 * Read all pending NFS responses at once to avoid having responses
268 * queue up as a consequence of retransmissions.
270 if (FD_ISSET(fwd_sock, &readfds)) {
271 FD_CLR(fwd_sock, &readfds);
275 } while (rpc_pending_now() > 0);
278 #ifdef HAVE_FS_AUTOFS
280 nsel = autofs_handle_fdset(&readfds, nsel);
281 #endif /* HAVE_FS_AUTOFS */
285 * Anything left must be a normal
288 #ifdef HAVE_SVC_GETREQSET
289 svc_getreqset(&readfds);
290 #else /* not HAVE_SVC_GETREQSET */
291 # ifdef HAVE_FD_SET_FDS_BITS
292 svc_getreq(readfds.fds_bits[0]);
293 # else /* not HAVE_FD_SET_FDS_BITS */
295 # endif /* not HAVE_FD_SET_FDS_BITS */
296 #endif /* not HAVE_SVC_GETREQSET */
302 #ifdef HAVE_SIGACTION
303 sigprocmask(SIG_SETMASK, &smask, NULL);
304 #else /* not HAVE_SIGACTION */
305 (void) sigsetmask(smask);
306 #endif /* not HAVE_SIGACTION */
308 if (amd_state == Quit)
316 mount_automounter(int ppid)
319 * Old code replaced by rpc-trash patch.
320 * Erez Zadok <ezk@cs.columbia.edu>
321 int so = socket(AF_INET, SOCK_DGRAM, 0);
323 SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
326 int udp_soAMQ, tcp_soAMQ;
327 struct netconfig *udp_amqncp, *tcp_amqncp;
330 * This must be done first, because it attempts to bind
331 * to various UDP ports and we don't want anything else
332 * potentially taking over those ports before we get a chance
335 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
336 restart_automounter_nodes();
339 * Start RPC forwarding
345 * Construct the root automount node
350 * Pick up the pieces from a previous run
351 * This is likely to (indirectly) need the rpc_fwd package
352 * so it *must* come after the call to fwd_init().
354 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
358 * Create the nfs service for amd
359 * If nfs_port is already initialized, it means we
360 * already created the service during restart_automounter_nodes().
363 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_dispatcher,
364 get_nfs_dispatcher_version(nfs_dispatcher));
368 xsnprintf(pid_fsname, sizeof(pid_fsname), "%s:(pid%ld,port%u)",
369 am_get_hostname(), (long) am_mypid, nfs_port);
371 /* security: if user sets -D noamq, don't even create listening socket */
372 if (amuDebug(D_AMQ)) {
373 ret = create_amq_service(&udp_soAMQ,
379 gopt.preferred_amq_port);
384 #ifdef HAVE_FS_AUTOFS
385 if (amd_use_autofs) {
387 * Create the autofs service for amd.
389 ret = create_autofs_service();
390 /* if autofs service fails it is OK if using a test amd */
392 plog(XLOG_WARNING, "autofs service registration failed, turning off autofs support");
396 #endif /* HAVE_FS_AUTOFS */
399 * Mount the top-level auto-mountpoints
401 nmount = mount_exported();
404 * Now safe to tell parent that we are up and running
410 plog(XLOG_FATAL, "No work to do - quitting");
415 if (amuDebug(D_AMQ)) {
417 * Complete registration of amq (first TCP service then UDP)
419 int tcp_ok = 0, udp_ok = 0;
421 unregister_amq(); /* unregister leftover Amd, if any, just in case */
423 tcp_ok = amu_svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
424 amq_program_1, IPPROTO_TCP, tcp_amqncp);
427 "unable to register (AMQ_PROGRAM=%lu, AMQ_VERSION, tcp)",
428 get_amd_program_number());
430 udp_ok = amu_svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
431 amq_program_1, IPPROTO_UDP, udp_amqncp);
434 "unable to register (AMQ_PROGRAM=%lu, AMQ_VERSION, udp)",
435 get_amd_program_number());
437 /* return error only if both failed */
438 if (!tcp_ok && !udp_ok) {
445 * Start timeout_mp rolling
447 reschedule_timeout_mp();
452 if (run_rpc() != Done) {
453 plog(XLOG_FATAL, "run_rpc failed");