]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/svc_run.c
Define and use HIAVE_IFADDRS_H
[nfs-utils.git] / utils / statd / svc_run.c
1 /*
2  * Copyright (C) 1984 Sun Microsystems, Inc.
3  * Modified by Jeffrey A. Uphoff, 1995, 1997-1999.
4  * Modified by Olaf Kirch, 1996.
5  *
6  * NSM for Linux.
7  */
8
9 /* 
10  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
11  * unrestricted use provided that this legend is included on all tape
12  * media and as a part of the software program in whole or part.  Users
13  * may copy or modify Sun RPC without charge, but are not authorized
14  * to license or distribute it to anyone else except as part of a product or
15  * program developed by the user.
16  * 
17  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
18  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
20  * 
21  * Sun RPC is provided with no support and without any obligation on the
22  * part of Sun Microsystems, Inc. to assist in its use, correction,
23  * modification or enhancement.
24  * 
25  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
26  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
27  * OR ANY PART THEREOF.
28  * 
29  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
30  * or profits or other special, indirect and consequential damages, even if
31  * Sun has been advised of the possibility of such damages.
32  * 
33  * Sun Microsystems, Inc.
34  * 2550 Garcia Avenue
35  * Mountain View, California  94043
36  */
37
38 /* 
39  * This has been modified for my own evil purposes to prevent deadlocks
40  * when two hosts start NSM's simultaneously and try to notify each
41  * other (which mainly occurs during testing), or to stop and smell the
42  * roses when I have callbacks due.
43  * --Jeff Uphoff.
44  */
45
46 /* 
47  * This is the RPC server side idle loop.
48  * Wait for input, call server program.
49  */
50
51 #ifdef HAVE_CONFIG_H
52 #include <config.h>
53 #endif
54
55 #include <errno.h>
56 #include <time.h>
57 #include "statd.h"
58 #include "notlist.h"
59
60 static int      svc_stop = 0;
61
62 /*
63  * This is the global notify list onto which all SM_NOTIFY and CALLBACK
64  * requests are put.
65  */
66 notify_list *   notify = NULL;
67 int     re_notify = 0;
68
69 /*
70  * Jump-off function.
71  */
72 void
73 my_svc_exit(void)
74 {
75         svc_stop = 1;
76 }
77
78
79 /*
80  * The heart of the server.  A crib from libc for the most part...
81  */
82 void
83 my_svc_run(void)
84 {
85         FD_SET_TYPE     readfds;
86         int             selret;
87         time_t          now;
88
89         svc_stop = 0;
90
91         for (;;) {
92                 if (svc_stop)
93                         return;
94                 if (re_notify) {
95                         change_state();
96                         dprintf(N_DEBUG, "Notifying...(new state %d)",
97                                                                 MY_STATE);
98                         notify_hosts();
99                         re_notify = 0;
100                 }
101
102                 /* Ah, there are some notifications to be processed */
103                 while (notify && NL_WHEN(notify) <= time(&now)) {
104                         process_notify_list();
105                 }
106
107                 readfds = SVC_FDSET;
108                 if (notify) {
109                         struct timeval  tv;
110
111                         tv.tv_sec  = NL_WHEN(notify) - now;
112                         tv.tv_usec = 0;
113                         dprintf(N_DEBUG, "Waiting for reply... (timeo %d)",
114                                                         tv.tv_sec);
115                         selret = select(FD_SETSIZE, &readfds,
116                                 (void *) 0, (void *) 0, &tv);
117                 } else if (run_mode & MODE_NOTIFY_ONLY)
118                         return;
119                 else {
120                         dprintf(N_DEBUG, "Waiting for client connections.");
121                         selret = select(FD_SETSIZE, &readfds,
122                                 (void *) 0, (void *) 0, (struct timeval *) 0);
123                 }
124
125                 switch (selret) {
126                 case -1:
127                         if (errno == EINTR || errno == ECONNREFUSED
128                          || errno == ENETUNREACH || errno == EHOSTUNREACH)
129                                 continue;
130                         note(N_ERROR, "my_svc_run() - select: %s",
131                                 strerror (errno));
132                         return;
133
134                 case 0:
135                         /* A notify/callback timed out. */
136                         continue;
137
138                 default:
139                         selret -= process_reply(&readfds);
140                         if (selret)
141                                 svc_getreqset(&readfds);
142                 }
143         }
144 }