Merge branch 'vendor/TNFTP'
[dragonfly.git] / contrib / amd / amd / nfs_start.c
1 /*
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.
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  *      %W% (Berkeley) %G%
40  *
41  * $Id: nfs_start.c,v 1.4 1999/02/04 07:24:16 ezk Exp $
42  *
43  */
44
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #endif /* HAVE_CONFIG_H */
48 #include <am_defs.h>
49 #include <amd.h>
50
51 #ifndef SELECT_MAXWAIT
52 # define SELECT_MAXWAIT 16
53 #endif /* not SELECT_MAXWAIT */
54
55 SVCXPRT *nfsxprt;
56 u_short nfs_port;
57
58 #ifdef HAVE_FS_AUTOFS
59 SVCXPRT *autofsxprt = NULL;
60 u_short autofs_port = 0;
61 int amd_use_autofs = 0;
62 #endif /* HAVE_FS_AUTOFS */
63
64 #ifndef HAVE_SIGACTION
65 # define MASKED_SIGS    (sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGCHLD)|sigmask(SIGHUP))
66 #endif /* not HAVE_SIGACTION */
67
68 #ifdef DEBUG
69 /*
70  * Check that we are not burning resources
71  */
72 static void
73 checkup(void)
74 {
75
76   static int max_fd = 0;
77   static char *max_mem = 0;
78
79   int next_fd = dup(0);
80   caddr_t next_mem = sbrk(0);
81   close(next_fd);
82
83   if (max_fd < next_fd) {
84     dlog("%d new fds allocated; total is %d",
85          next_fd - max_fd, next_fd);
86     max_fd = next_fd;
87   }
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 */
97     max_mem = next_mem;
98
99   }
100 }
101 #endif /* DEBUG */
102
103
104 static int
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 */
110 {
111
112   int sig;
113   int nsel;
114
115   if ((sig = setjmp(select_intr))) {
116     select_intr_valid = 0;
117     /* Got a signal */
118     switch (sig) {
119     case SIGINT:
120     case SIGTERM:
121       amd_state = Finishing;
122       reschedule_timeout_mp();
123       break;
124     }
125     nsel = -1;
126     errno = EINTR;
127   } else {
128     select_intr_valid = 1;
129     /*
130      * Invalidate the current clock value
131      */
132     clock_valid = 0;
133     /*
134      * Allow interrupts.  If a signal
135      * occurs, then it will cause a longjmp
136      * up above.
137      */
138 #ifdef HAVE_SIGACTION
139     sigprocmask(SIG_SETMASK, &smask, NULL);
140 #else /* not HAVE_SIGACTION */
141     (void) sigsetmask(smask);
142 #endif /* not HAVE_SIGACTION */
143
144     /*
145      * Wait for input
146      */
147     nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0,
148                   tvp->tv_sec ? tvp : (struct timeval *) 0);
149   }
150
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 */
156
157   /*
158    * Perhaps reload the cache?
159    */
160   if (do_mapc_reload < clocktime()) {
161     mapc_reload();
162     do_mapc_reload = clocktime() + ONE_HOUR;
163   }
164   return nsel;
165 }
166
167
168 /*
169  * Determine whether anything is left in
170  * the RPC input queue.
171  */
172 static int
173 rpc_pending_now(void)
174 {
175   struct timeval tvv;
176   int nsel;
177 #ifdef FD_SET
178   fd_set readfds;
179
180   FD_ZERO(&readfds);
181   FD_SET(fwd_sock, &readfds);
182 #else /* not FD_SET */
183   int readfds = (1 << fwd_sock);
184 #endif /* not FD_SET */
185
186   tvv.tv_sec = tvv.tv_usec = 0;
187   nsel = select(FD_SETSIZE, &readfds, (fd_set *) 0, (fd_set *) 0, &tvv);
188   if (nsel < 1)
189     return (0);
190 #ifdef FD_SET
191   if (FD_ISSET(fwd_sock, &readfds))
192     return (1);
193 #else /* not FD_SET */
194   if (readfds & (1 << fwd_sock))
195     return (1);
196 #endif /* not FD_SET */
197
198   return (0);
199 }
200
201
202 static serv_state
203 run_rpc(void)
204 {
205 #ifdef HAVE_SIGACTION
206   sigset_t smask;
207   sigprocmask(SIG_BLOCK, &masked_sigs, &smask);
208 #else /* not HAVE_SIGACTION */
209   int smask = sigblock(MASKED_SIGS);
210 #endif /* not HAVE_SIGACTION */
211
212   next_softclock = clocktime();
213
214   amd_state = Run;
215
216   /*
217    * Keep on trucking while we are in Run mode.  This state
218    * is switched to Quit after all the file systems have
219    * been unmounted.
220    */
221   while ((int) amd_state <= (int) Finishing) {
222     struct timeval tvv;
223     int nsel;
224     time_t now;
225 #ifdef HAVE_SVC_GETREQSET
226     fd_set readfds;
227
228     memmove(&readfds, &svc_fdset, sizeof(svc_fdset));
229     FD_SET(fwd_sock, &readfds);
230 #else /* not HAVE_SVC_GETREQSET */
231 # ifdef FD_SET
232     fd_set readfds;
233     FD_ZERO(&readfds);
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 */
240
241 #ifdef DEBUG
242     checkup();
243 #endif /* DEBUG */
244
245     /*
246      * If the full timeout code is not called,
247      * then recompute the time delta manually.
248      */
249     now = clocktime();
250
251     if (next_softclock <= now) {
252       if (amd_state == Finishing)
253         umount_exported();
254       tvv.tv_sec = softclock();
255     } else {
256       tvv.tv_sec = next_softclock - now;
257     }
258     tvv.tv_usec = 0;
259
260     if (amd_state == Finishing && last_used_map < 0) {
261       flush_mntfs();
262       amd_state = Quit;
263       break;
264     }
265     if (tvv.tv_sec <= 0)
266       tvv.tv_sec = SELECT_MAXWAIT;
267 #ifdef DEBUG
268     if (tvv.tv_sec) {
269       dlog("Select waits for %ds", (int) tvv.tv_sec);
270     } else {
271       dlog("Select waits for Godot");
272     }
273 #endif /* DEBUG */
274
275     nsel = do_select(smask, FD_SETSIZE, &readfds, &tvv);
276
277     switch (nsel) {
278     case -1:
279       if (errno == EINTR) {
280 #ifdef DEBUG
281         dlog("select interrupted");
282 #endif /* DEBUG */
283         continue;
284       }
285       perror("select");
286       break;
287
288     case 0:
289       break;
290
291     default:
292       /*
293        * Read all pending NFS responses at once to avoid having responses.
294        * queue up as a consequence of retransmissions.
295        */
296 #ifdef FD_SET
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 */
303         --nsel;
304         do {
305           fwd_reply();
306         } while (rpc_pending_now() > 0);
307       }
308
309       if (nsel) {
310         /*
311          * Anything left must be a normal
312          * RPC request.
313          */
314 #ifdef HAVE_SVC_GETREQSET
315         svc_getreqset(&readfds);
316 #else /* not HAVE_SVC_GETREQSET */
317 # ifdef FD_SET
318         svc_getreq(readfds.fds_bits[0]);
319 # else /* not FD_SET */
320         svc_getreq(readfds);
321 # endif /* not FD_SET */
322 #endif /* not HAVE_SVC_GETREQSET */
323       }
324       break;
325     }
326   }
327
328 #ifdef HAVE_SIGACTION
329   sigprocmask(SIG_SETMASK, &smask, NULL);
330 #else /* not HAVE_SIGACTION */
331   (void) sigsetmask(smask);
332 #endif /* not HAVE_SIGACTION */
333
334   if (amd_state == Quit)
335     amd_state = Done;
336
337   return amd_state;
338 }
339
340
341 int
342 mount_automounter(int ppid)
343 {
344   /*
345    * Old code replaced by rpc-trash patch.
346    * Erez Zadok <ezk@cs.columbia.edu>
347    int so = socket(AF_INET, SOCK_DGRAM, 0);
348    */
349   SVCXPRT *udp_amqp = NULL, *tcp_amqp = NULL;
350   int nmount, ret;
351   int soNFS;
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
357   int soAUTOFS;
358 #endif /* HAVE_FS_AUTOFS */
359
360   /*
361    * Create the nfs service for amd
362    */
363 #ifdef HAVE_TRANSPORT_TYPE_TLI
364   ret = create_nfs_service(&soNFS, &nfs_port, &nfsxprt, nfs_program_2);
365   if (ret != 0)
366     return ret;
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);
370   if (ret != 0)
371     return ret;
372   ret = create_amq_service(&udp_soAMQ, &udp_amqp, &tcp_soAMQ, &tcp_amqp);
373 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
374   if (ret != 0)
375     return ret;
376
377 #ifdef HAVE_FS_AUTOFS
378   if (amd_use_autofs) {
379     /*
380      * Create the autofs service for amd, but only if autofs maps
381      * were defined (so amd doesn't clash with automountd.)
382      */
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)
387       return ret;
388   }
389 #endif /* HAVE_FS_AUTOFS */
390
391   /*
392    * Start RPC forwarding
393    */
394   if (fwd_init() != 0)
395     return 3;
396
397   /*
398    * Construct the root automount node
399    */
400   make_root_node();
401
402   /*
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().
406    */
407   if (gopt.flags & CFM_RESTART_EXISTING_MOUNTS)
408     restart();
409
410   /*
411    * Mount the top-level auto-mountpoints
412    */
413   nmount = mount_exported();
414
415   /*
416    * Now safe to tell parent that we are up and running
417    */
418   if (ppid)
419     kill(ppid, SIGQUIT);
420
421   if (nmount == 0) {
422     plog(XLOG_FATAL, "No work to do - quitting");
423     amd_state = Done;
424     return 0;
425   }
426
427 #ifdef DEBUG
428   amuDebug(D_AMQ) {
429 #endif /* DEBUG */
430     /*
431      * Complete registration of amq (first TCP service then UDP)
432      */
433     unregister_amq();
434
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 */
442     if (ret != 1) {
443       plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, tcp)", get_amd_program_number());
444       return 3;
445     }
446
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 */
454     if (ret != 1) {
455       plog(XLOG_FATAL, "unable to register (AMQ_PROGRAM=%d, AMQ_VERSION, udp)", get_amd_program_number());
456       return 4;
457     }
458
459 #ifdef DEBUG
460   }
461 #endif /* DEBUG */
462
463   /*
464    * Start timeout_mp rolling
465    */
466   reschedule_timeout_mp();
467
468   /*
469    * Start the server
470    */
471   if (run_rpc() != Done) {
472     plog(XLOG_FATAL, "run_rpc failed");
473     amd_state = Done;
474   }
475   return 0;
476 }