]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/amd/amd/nfs_start.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / amd / amd / nfs_start.c
1 /*
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.
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      * Allow interrupts.  If a signal
126      * occurs, then it will cause a longjmp
127      * up above.
128      */
129 #ifdef HAVE_SIGACTION
130     sigprocmask(SIG_SETMASK, &smask, NULL);
131 #else /* not HAVE_SIGACTION */
132     (void) sigsetmask(smask);
133 #endif /* not HAVE_SIGACTION */
134
135     /*
136      * Wait for input
137      */
138     nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
139                   tvp->tv_sec ? tvp : (struct timeval *) 0);
140   }
141
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 */
147
148   /*
149    * Perhaps reload the cache?
150    */
151   if (do_mapc_reload < clocktime(NULL)) {
152     mapc_reload();
153     do_mapc_reload = clocktime(NULL) + gopt.map_reload_interval;
154   }
155   return nsel;
156 }
157
158
159 /*
160  * Determine whether anything is left in
161  * the RPC input queue.
162  */
163 static int
164 rpc_pending_now(void)
165 {
166   struct timeval tvv;
167   int nsel;
168   fd_set readfds;
169
170   FD_ZERO(&readfds);
171   FD_SET(fwd_sock, &readfds);
172
173   tvv.tv_sec = tvv.tv_usec = 0;
174   nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
175   if (nsel < 1)
176     return (0);
177   if (FD_ISSET(fwd_sock, &readfds))
178     return (1);
179
180   return (0);
181 }
182
183
184 static serv_state
185 run_rpc(void)
186 {
187 #ifdef HAVE_SIGACTION
188   sigset_t smask;
189   sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
190 #else /* not HAVE_SIGACTION */
191   int smask = sigblock(MASKED_SIGS);
192 #endif /* not HAVE_SIGACTION */
193
194   next_softclock = clocktime(NULL);
195
196   amd_state = Run;
197
198   /*
199    * Keep on trucking while we are in Run mode.  This state
200    * is switched to Quit after all the file systems have
201    * been unmounted.
202    */
203   while ((int) amd_state <= (int) Finishing) {
204     struct timeval tvv;
205     int nsel;
206     time_t now;
207     fd_set readfds;
208
209 #ifdef HAVE_SVC_GETREQSET
210     memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
211 #else /* not HAVE_SVC_GETREQSET */
212     FD_ZERO(&readfds);
213 # ifdef HAVE_FD_SET_FDS_BITS
214     readfds.fds_bits[0] = svc_fds;
215 # else /* not HAVE_FD_SET_FDS_BITS */
216     readfds = svc_fds;
217 # endif  /* not HAVE_FD_SET_FDS_BITS */
218 #endif /* not HAVE_SVC_GETREQSET */
219     FD_SET(fwd_sock, &readfds);
220
221     checkup();
222
223     /*
224      * If the full timeout code is not called,
225      * then recompute the time delta manually.
226      */
227     now = clocktime(NULL);
228
229     if (next_softclock <= now) {
230       if (amd_state == Finishing)
231         umount_exported();
232       tvv.tv_sec = softclock();
233     } else {
234       tvv.tv_sec = next_softclock - now;
235     }
236     tvv.tv_usec = 0;
237
238     if (amd_state == Finishing && get_exported_ap(0) == NULL) {
239       flush_mntfs();
240       amd_state = Quit;
241       break;
242     }
243
244 #ifdef HAVE_FS_AUTOFS
245     autofs_add_fdset(&readfds);
246 #endif /* HAVE_FS_AUTOFS */
247
248     if (tvv.tv_sec <= 0)
249       tvv.tv_sec = SELECT_MAXWAIT;
250     if (tvv.tv_sec) {
251       dlog("Select waits for %ds", (int) tvv.tv_sec);
252     } else {
253       dlog("Select waits for Godot");
254     }
255
256     nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
257
258     switch (nsel) {
259     case -1:
260       if (errno == EINTR) {
261         dlog("select interrupted");
262         continue;
263       }
264       plog(XLOG_ERROR, "select: %m");
265       break;
266
267     case 0:
268       break;
269
270     default:
271       /*
272        * Read all pending NFS responses at once to avoid having responses
273        * queue up as a consequence of retransmissions.
274        */
275       if (FD_ISSET(fwd_sock, &readfds)) {
276         FD_CLR(fwd_sock, &readfds);
277         --nsel;
278         do {
279           fwd_reply();
280         } while (rpc_pending_now() > 0);
281       }
282
283 #ifdef HAVE_FS_AUTOFS
284       if (nsel)
285         nsel = autofs_handle_fdset(&readfds, nsel);
286 #endif /* HAVE_FS_AUTOFS */
287
288       if (nsel) {
289         /*
290          * Anything left must be a normal
291          * RPC request.
292          */
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 */
299         svc_getreq(readfds);
300 # endif /* not HAVE_FD_SET_FDS_BITS */
301 #endif /* not HAVE_SVC_GETREQSET */
302       }
303       break;
304     }
305   }
306
307 #ifdef HAVE_SIGACTION
308   sigprocmask(SIG_SETMASK, &smask, NULL);
309 #else /* not HAVE_SIGACTION */
310   (void) sigsetmask(smask);
311 #endif /* not HAVE_SIGACTION */
312
313   if (amd_state == Quit)
314     amd_state = Done;
315
316   return amd_state;
317 }
318
319
320 int
321 mount_automounter(int ppid)
322 {
323   /*
324    * Old code replaced by rpc-trash patch.
325    * Erez Zadok <ezk@cs.columbia.edu>
326    int so = socket(AF_INET, SOCK_DGRAM, 0);
327    */
328   SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
329   int nmount, ret;
330   int soNFS;
331   int udp_soAMQ, tcp_soAMQ;
332   struct netconfig *udp_amqncp, *tcp_amqncp;
333
334   /*
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
338    * to reserve them.
339    */
340   if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
341     restart_automounter_nodes();
342
343   /*
344    * Start RPC forwarding
345    */
346   if (fwd_init() != 0)
347     return 3;
348
349   /*
350    * Construct the root automount node
351    */
352   make_root_node();
353
354   /*
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().
358    */
359   if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
360     restart();
361
362   /*
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().
366    */
367   if (nfs_port == 0) {
368     ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
369     if (ret != 0)
370       return ret;
371   }
372   xsnprintf(pid_fsname, sizeof(pid_fsname), "%s:(pid%ld,port%u)",
373             am_get_hostname(), (long) am_mypid, nfs_port);
374
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,
378                              &udp_amqp,
379                              &udp_amqncp,
380                              &tcp_soAMQ,
381                              &tcp_amqp,
382                              &tcp_amqncp,
383                              gopt.preferred_amq_port);
384     if (ret != 0)
385       return ret;
386   }
387
388 #ifdef HAVE_FS_AUTOFS
389   if (amd_use_autofs) {
390     /*
391      * Create the autofs service for amd.
392      */
393     ret = create_autofs_service();
394     /* if autofs service fails it is OK if using a test amd */
395     if (ret != 0) {
396       plog(XLOG_WARNING, "autofs service registration failed, turning off autofs support");
397       amd_use_autofs = 0;
398     }
399   }
400 #endif /* HAVE_FS_AUTOFS */
401
402   /*
403    * Mount the top-level auto-mountpoints
404    */
405   nmount = mount_exported();
406
407   /*
408    * Now safe to tell parent that we are up and running
409    */
410   if (ppid)
411     kill(ppid, SIGQUIT);
412
413   if (nmount == 0) {
414     plog(XLOG_FATAL, "No work to do - quitting");
415     amd_state = Done;
416     return 0;
417   }
418
419   if (!amuDebug(D_AMQ)) {
420     /*
421      * Complete registration of amq (first TCP service then UDP)
422      */
423     unregister_amq();
424
425     ret = amu_svc_register(tcp_amqp, get_amd_program_number(), AMQ_VERSION,
426                            amq_program_1, IPPROTO_TCP, tcp_amqncp);
427     if (ret != 1) {
428       plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
429       return 3;
430     }
431
432     ret = amu_svc_register(udp_amqp, get_amd_program_number(), AMQ_VERSION,
433                            amq_program_1, IPPROTO_UDP, udp_amqncp);
434     if (ret != 1) {
435       plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, udp)", get_amd_program_number());
436       return 4;
437     }
438   }
439
440   /*
441    * Start timeout_mp rolling
442    */
443   reschedule_timeout_mp();
444
445   /*
446    * Start the server
447    */
448   if (run_rpc() != Done) {
449     plog(XLOG_FATAL, "run_rpc failed");
450     amd_state = Done;
451   }
452   return 0;
453 }