2 * Copyright (c) 1997-2004 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
41 * $Id: nfs_start.c,v 1.5.2.7 2004/01/06 03:15:16 ezk Exp $
47 #endif /* HAVE_CONFIG_H */
51 #ifndef SELECT_MAXWAIT
52 # define SELECT_MAXWAIT 16
53 #endif /* not SELECT_MAXWAIT */
58 #ifndef HAVE_SIGACTION
59 # define MASKED_SIGS (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
60 #endif /* not HAVE_SIGACTION */
64 * Check that we are not burning resources
70 static int max_fd = 0;
71 static char *max_mem = 0;
74 caddr_t next_mem = sbrk(0);
77 if (max_fd < next_fd) {
78 dlog("%d new fds allocated; total is %d",
79 next_fd - max_fd, next_fd);
82 if (max_mem < next_mem) {
83 #ifdef HAVE_GETPAGESIZE
84 dlog("%#lx bytes of memory allocated; total is %#lx (%ld pages)",
85 (long) (next_mem - max_mem), (unsigned long) next_mem,
86 ((long) next_mem + getpagesize() - 1) / (long) getpagesize());
87 #else /* not HAVE_GETPAGESIZE */
88 dlog("%#lx bytes of memory allocated; total is %#lx",
89 (long) (next_mem - max_mem), (unsigned long) next_mem);
90 #endif /* not HAVE_GETPAGESIZE */
100 do_select(sigset_t smask, int fds, fd_set *fdp, struct timeval *tvp)
101 #else /* not HAVE_SIGACTION */
102 do_select(int smask, int fds, fd_set *fdp, struct timeval *tvp)
103 #endif /* not HAVE_SIGACTION */
109 if ((sig = setjmp(select_intr))) {
110 select_intr_valid = 0;
115 amd_state = Finishing;
116 reschedule_timeout_mp();
122 select_intr_valid = 1;
124 * Invalidate the current clock value
128 * Allow interrupts. If a signal
129 * occurs, then it will cause a longjmp
132 #ifdef HAVE_SIGACTION
133 sigprocmask(SIG_SETMASK, &smask, NULL);
134 #else /* not HAVE_SIGACTION */
135 (void) sigsetmask(smask);
136 #endif /* not HAVE_SIGACTION */
141 nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
142 tvp->tv_sec ? tvp : (struct timeval *) 0);
145 #ifdef HAVE_SIGACTION
146 sigprocmask(SIG_BLOCK, &masked_sigs, NULL);
147 #else /* not HAVE_SIGACTION */
148 (void) sigblock(MASKED_SIGS);
149 #endif /* not HAVE_SIGACTION */
152 * Perhaps reload the cache?
154 if (do_mapc_reload < clocktime()) {
156 do_mapc_reload = clocktime() + ONE_HOUR;
163 * Determine whether anything is left in
164 * the RPC input queue.
167 rpc_pending_now(void)
175 FD_SET(fwd_sock, &readfds);
176 #else /* not FD_SET */
177 int readfds = (1 << fwd_sock);
178 #endif /* not FD_SET */
180 tvv.tv_sec = tvv.tv_usec = 0;
181 nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
185 if (FD_ISSET(fwd_sock, &readfds))
187 #else /* not FD_SET */
188 if (readfds & (1 << fwd_sock))
190 #endif /* not FD_SET */
199 #ifdef HAVE_SIGACTION
201 sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
202 #else /* not HAVE_SIGACTION */
203 int smask = sigblock(MASKED_SIGS);
204 #endif /* not HAVE_SIGACTION */
206 next_softclock = clocktime();
211 * Keep on trucking while we are in Run mode. This state
212 * is switched to Quit after all the file systems have
215 while ((int) amd_state <= (int) Finishing) {
219 #ifdef HAVE_SVC_GETREQSET
222 memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
223 FD_SET(fwd_sock, &readfds);
224 #else /* not HAVE_SVC_GETREQSET */
228 readfds.fds_bits[0] = svc_fds;
229 FD_SET(fwd_sock, &readfds);
230 # else /* not FD_SET */
231 int readfds = svc_fds | (1 << fwd_sock);
232 # endif /* not FD_SET */
233 #endif /* not HAVE_SVC_GETREQSET */
240 * If the full timeout code is not called,
241 * then recompute the time delta manually.
245 if (next_softclock <= now) {
246 if (amd_state == Finishing)
248 tvv.tv_sec = softclock();
250 tvv.tv_sec = next_softclock - now;
254 if (amd_state == Finishing && last_used_map < 0) {
260 tvv.tv_sec = SELECT_MAXWAIT;
263 dlog("Select waits for %ds", (int) tvv.tv_sec);
265 dlog("Select waits for Godot");
269 nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
273 if (errno == EINTR) {
275 dlog("select interrupted");
279 plog(XLOG_ERROR, "select: %m");
287 * Read all pending NFS responses at once to avoid having responses.
288 * queue up as a consequence of retransmissions.
291 if (FD_ISSET(fwd_sock, &readfds)) {
292 FD_CLR(fwd_sock, &readfds);
293 #else /* not FD_SET */
294 if (readfds & (1 << fwd_sock)) {
295 readfds &= ~(1 << fwd_sock);
296 #endif /* not FD_SET */
300 } while (rpc_pending_now() > 0);
305 * Anything left must be a normal
308 #ifdef HAVE_SVC_GETREQSET
309 svc_getreqset(&readfds);
310 #else /* not HAVE_SVC_GETREQSET */
312 svc_getreq(readfds.fds_bits[0]);
313 # else /* not FD_SET */
315 # endif /* not FD_SET */
316 #endif /* not HAVE_SVC_GETREQSET */
322 #ifdef HAVE_SIGACTION
323 sigprocmask(SIG_SETMASK, &smask, NULL);
324 #else /* not HAVE_SIGACTION */
325 (void) sigsetmask(smask);
326 #endif /* not HAVE_SIGACTION */
328 if (amd_state == Quit)
336 mount_automounter(int ppid)
339 * Old code replaced by rpc-trash patch.
340 * Erez Zadok <ezk@cs.columbia.edu>
341 int so = socket(AF_INET, SOCK_DGRAM, 0);
343 SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
346 int udp_soAMQ, tcp_soAMQ;
347 #ifdef HAVE_TRANSPORT_TYPE_TLI
348 struct netconfig *udp_amqncp, *tcp_amqncp;
349 #endif /* HAVE_TRANSPORT_TYPE_TLI */
352 * Create the nfs service for amd
354 #ifdef HAVE_TRANSPORT_TYPE_TLI
355 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
358 ret = create_amq_service(&udp_soAMQ, &udp_amqp, &udp_amqncp, &tcp_soAMQ, &tcp_amqp, &tcp_amqncp);
359 #else /* not HAVE_TRANSPORT_TYPE_TLI */
360 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
363 ret = create_amq_service(&udp_soAMQ, &udp_amqp, &tcp_soAMQ, &tcp_amqp);
364 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
369 * Start RPC forwarding
375 * Construct the root automount node
380 * Pick up the pieces from a previous run
381 * This is likely to (indirectly) need the rpc_fwd package
382 * so it *must* come after the call to fwd_init().
384 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
388 * Mount the top-level auto-mountpoints
390 nmount = mount_exported();
393 * Now safe to tell parent that we are up and running
399 plog(XLOG_FATAL, "No work to do - quitting");
408 * Complete registration of amq (first TCP service then UDP)
412 #ifdef HAVE_TRANSPORT_TYPE_TLI
413 ret = svc_reg(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
414 amq_program_1, tcp_amqncp);
415 #else /* not HAVE_TRANSPORT_TYPE_TLI */
416 ret = svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
417 amq_program_1, IPPROTO_TCP);
418 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
420 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
424 #ifdef HAVE_TRANSPORT_TYPE_TLI
425 ret = svc_reg(udp_amqp, get_amd_program_number(), AMQ_VERSION,
426 amq_program_1, udp_amqncp);
427 #else /* not HAVE_TRANSPORT_TYPE_TLI */
428 ret = svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
429 amq_program_1, IPPROTO_UDP);
430 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
432 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");