2 * Copyright (c) 1997-1999 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.4 1999/02/04 07:24:16 ezk Exp $
47 #endif /* HAVE_CONFIG_H */
51 #ifndef SELECT_MAXWAIT
52 # define SELECT_MAXWAIT 16
53 #endif /* not SELECT_MAXWAIT */
59 SVCXPRT *autofsxprt = NULL;
60 u_short autofs_port = 0;
61 int amd_use_autofs = 0;
62 #endif /* HAVE_FS_AUTOFS */
64 #ifndef HAVE_SIGACTION
65 # define MASKED_SIGS (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
66 #endif /* not HAVE_SIGACTION */
70 * Check that we are not burning resources
76 static int max_fd = 0;
77 static char *max_mem = 0;
80 caddr_t next_mem = sbrk(0);
83 if (max_fd < next_fd) {
84 dlog("%d new fds allocated; total is %d",
85 next_fd - max_fd, next_fd);
88 if (max_mem < next_mem) {
89 #ifdef HAVE_GETPAGESIZE
90 dlog("%#lx bytes of memory allocated; total is %#lx (%ld pages)",
91 (long) (next_mem - max_mem), (unsigned long) next_mem,
92 ((long) next_mem + getpagesize() - 1) / getpagesize());
93 #else /* not HAVE_GETPAGESIZE */
94 dlog("%#lx bytes of memory allocated; total is %#lx",
95 (long) (next_mem - max_mem), (unsigned long) next_mem);
96 #endif /* not HAVE_GETPAGESIZE */
105 #ifdef HAVE_SIGACTION
106 do_select(sigset_t smask, int fds, fd_set *fdp, struct timeval *tvp)
107 #else /* not HAVE_SIGACTION */
108 do_select(int smask, int fds, fd_set *fdp, struct timeval *tvp)
109 #endif /* not HAVE_SIGACTION */
115 if ((sig = setjmp(select_intr))) {
116 select_intr_valid = 0;
121 amd_state = Finishing;
122 reschedule_timeout_mp();
128 select_intr_valid = 1;
130 * Invalidate the current clock value
134 * Allow interrupts. If a signal
135 * occurs, then it will cause a longjmp
138 #ifdef HAVE_SIGACTION
139 sigprocmask(SIG_SETMASK, &smask, NULL);
140 #else /* not HAVE_SIGACTION */
141 (void) sigsetmask(smask);
142 #endif /* not HAVE_SIGACTION */
147 nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
148 tvp->tv_sec ? tvp : (struct timeval *) 0);
151 #ifdef HAVE_SIGACTION
152 sigprocmask(SIG_BLOCK, &masked_sigs, NULL);
153 #else /* not HAVE_SIGACTION */
154 (void) sigblock(MASKED_SIGS);
155 #endif /* not HAVE_SIGACTION */
158 * Perhaps reload the cache?
160 if (do_mapc_reload < clocktime()) {
162 do_mapc_reload = clocktime() + ONE_HOUR;
169 * Determine whether anything is left in
170 * the RPC input queue.
173 rpc_pending_now(void)
181 FD_SET(fwd_sock, &readfds);
182 #else /* not FD_SET */
183 int readfds = (1 << fwd_sock);
184 #endif /* not FD_SET */
186 tvv.tv_sec = tvv.tv_usec = 0;
187 nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
191 if (FD_ISSET(fwd_sock, &readfds))
193 #else /* not FD_SET */
194 if (readfds & (1 << fwd_sock))
196 #endif /* not FD_SET */
205 #ifdef HAVE_SIGACTION
207 sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
208 #else /* not HAVE_SIGACTION */
209 int smask = sigblock(MASKED_SIGS);
210 #endif /* not HAVE_SIGACTION */
212 next_softclock = clocktime();
217 * Keep on trucking while we are in Run mode. This state
218 * is switched to Quit after all the file systems have
221 while ((int) amd_state <= (int) Finishing) {
225 #ifdef HAVE_SVC_GETREQSET
228 memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
229 FD_SET(fwd_sock, &readfds);
230 #else /* not HAVE_SVC_GETREQSET */
234 readfds.fds_bits[0] = svc_fds;
235 FD_SET(fwd_sock, &readfds);
236 # else /* not FD_SET */
237 int readfds = svc_fds | (1 << fwd_sock);
238 # endif /* not FD_SET */
239 #endif /* not HAVE_SVC_GETREQSET */
246 * If the full timeout code is not called,
247 * then recompute the time delta manually.
251 if (next_softclock <= now) {
252 if (amd_state == Finishing)
254 tvv.tv_sec = softclock();
256 tvv.tv_sec = next_softclock - now;
260 if (amd_state == Finishing && last_used_map < 0) {
266 tvv.tv_sec = SELECT_MAXWAIT;
269 dlog("Select waits for %ds", (int) tvv.tv_sec);
271 dlog("Select waits for Godot");
275 nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
279 if (errno == EINTR) {
281 dlog("select interrupted");
293 * Read all pending NFS responses at once to avoid having responses.
294 * queue up as a consequence of retransmissions.
297 if (FD_ISSET(fwd_sock, &readfds)) {
298 FD_CLR(fwd_sock, &readfds);
299 #else /* not FD_SET */
300 if (readfds & (1 << fwd_sock)) {
301 readfds &= ~(1 << fwd_sock);
302 #endif /* not FD_SET */
306 } while (rpc_pending_now() > 0);
311 * Anything left must be a normal
314 #ifdef HAVE_SVC_GETREQSET
315 svc_getreqset(&readfds);
316 #else /* not HAVE_SVC_GETREQSET */
318 svc_getreq(readfds.fds_bits[0]);
319 # else /* not FD_SET */
321 # endif /* not FD_SET */
322 #endif /* not HAVE_SVC_GETREQSET */
328 #ifdef HAVE_SIGACTION
329 sigprocmask(SIG_SETMASK, &smask, NULL);
330 #else /* not HAVE_SIGACTION */
331 (void) sigsetmask(smask);
332 #endif /* not HAVE_SIGACTION */
334 if (amd_state == Quit)
342 mount_automounter(int ppid)
345 * Old code replaced by rpc-trash patch.
346 * Erez Zadok <ezk@cs.columbia.edu>
347 int so = socket(AF_INET, SOCK_DGRAM, 0);
349 SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
352 int udp_soAMQ, tcp_soAMQ;
353 #ifdef HAVE_TRANSPORT_TYPE_TLI
354 struct netconfig *udp_amqncp, *tcp_amqncp;
355 #endif /* HAVE_TRANSPORT_TYPE_TLI */
356 #ifdef HAVE_FS_AUTOFS
358 #endif /* HAVE_FS_AUTOFS */
361 * Create the nfs service for amd
363 #ifdef HAVE_TRANSPORT_TYPE_TLI
364 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
367 ret = create_amq_service(&udp_soAMQ, &udp_amqp, &udp_amqncp, &tcp_soAMQ, &tcp_amqp, &tcp_amqncp);
368 #else /* not HAVE_TRANSPORT_TYPE_TLI */
369 ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
372 ret = create_amq_service(&udp_soAMQ, &udp_amqp, &tcp_soAMQ, &tcp_amqp);
373 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
377 #ifdef HAVE_FS_AUTOFS
378 if (amd_use_autofs) {
380 * Create the autofs service for amd, but only if autofs maps
381 * were defined (so amd doesn't clash with automountd.)
383 plog(XLOG_INFO, "creating autofs service listener");
384 ret = create_autofs_service(&soAUTOFS, &autofs_port, &autofsxprt, autofs_program_1);
385 /* if autofs service fails it is OK if using a test amd */
386 if (ret != 0 && gopt.portmap_program == AMQ_PROGRAM)
389 #endif /* HAVE_FS_AUTOFS */
392 * Start RPC forwarding
398 * Construct the root automount node
403 * Pick up the pieces from a previous run
404 * This is likely to (indirectly) need the rpc_fwd package
405 * so it *must* come after the call to fwd_init().
407 if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
411 * Mount the top-level auto-mountpoints
413 nmount = mount_exported();
416 * Now safe to tell parent that we are up and running
422 plog(XLOG_FATAL, "No work to do - quitting");
431 * Complete registration of amq (first TCP service then UDP)
435 #ifdef HAVE_TRANSPORT_TYPE_TLI
436 ret = svc_reg(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
437 amq_program_1, tcp_amqncp);
438 #else /* not HAVE_TRANSPORT_TYPE_TLI */
439 ret = svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
440 amq_program_1, IPPROTO_TCP);
441 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
443 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
447 #ifdef HAVE_TRANSPORT_TYPE_TLI
448 ret = svc_reg(udp_amqp, get_amd_program_number(), AMQ_VERSION,
449 amq_program_1, udp_amqncp);
450 #else /* not HAVE_TRANSPORT_TYPE_TLI */
451 ret = svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
452 amq_program_1, IPPROTO_UDP);
453 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
455 plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, udp)", get_amd_program_number());
464 * Start timeout_mp rolling
466 reschedule_timeout_mp();
471 if (run_rpc() != Done) {
472 plog(XLOG_FATAL, "run_rpc failed");