]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/statd.c
-N for statd
[nfs-utils.git] / utils / statd / statd.c
1 /* 
2  * Copyright (C) 1995, 1997-1999 Jeffrey A. Uphoff
3  * Modified by Olaf Kirch, Oct. 1996.
4  * Modified by H.J. Lu, 1998.
5  * Modified by L. Hohberger of Mission Critical Linux, 2000.
6  *
7  * NSM for Linux.
8  */
9
10 #include "config.h"
11 #include <limits.h>
12 #include <signal.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <getopt.h>
17 #include <rpc/rpc.h>
18 #include <rpc/pmap_clnt.h>
19 #include <rpcmisc.h>
20 #include "statd.h"
21 #include "version.h"
22
23 /* Socket operations */
24 #include <sys/types.h>
25 #include <sys/socket.h>
26
27 /* Added to enable specification of state directory path at run-time
28  * j_carlos_gomez@yahoo.com
29  */
30
31 char * DIR_BASE = DEFAULT_DIR_BASE;
32
33 char *  SM_DIR = DEFAULT_SM_DIR;
34 char *  SM_BAK_DIR =  DEFAULT_SM_BAK_DIR;
35 char *  SM_STAT_PATH = DEFAULT_SM_STAT_PATH;
36
37 /* ----- end of state directory path stuff ------- */
38
39 short int restart = 0;
40 int     run_mode = 0;           /* foreground logging mode */
41
42 /* LH - I had these local to main, but it seemed silly to have 
43  * two copies of each - one in main(), one static in log.c... 
44  * It also eliminates the 256-char static in log.c */
45 char *name_p = NULL;
46 char *version_p = NULL;
47
48 static struct option longopts[] =
49 {
50         { "foreground", 0, 0, 'F' },
51         { "no-syslog", 0, 0, 'd' },
52         { "help", 0, 0, 'h' },
53         { "version", 0, 0, 'v' },
54         { "outgoing-port", 1, 0, 'o' },
55         { "port", 1, 0, 'p' },
56         { "name", 1, 0, 'n' },
57         { "state-directory-path", 1, 0, 'P' },
58         { "notify-mode", 0, 0, 'N' },
59         { NULL, 0, 0, 0 }
60 };
61
62 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
63 extern int statd_get_socket(int port);
64
65 #ifdef SIMULATIONS
66 extern void simulator (int, char **);
67 #endif
68
69
70 #ifdef HAVE_TCP_WRAPPER 
71 #include "tcpwrapper.h"
72
73 static void 
74 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
75 {
76         /* remote host authorization check */
77         if (!check_default("statd", svc_getcaller(transp),
78                                  rqstp->rq_proc, SM_PROG)) {
79                 svcerr_auth (transp, AUTH_FAILED);
80                 return;
81         }
82
83         sm_prog_1 (rqstp, transp);
84 }
85
86 #define sm_prog_1 sm_prog_1_wrapper
87 #endif
88
89 /*
90  * Signal handler.
91  */
92 static void 
93 killer (int sig)
94 {
95         log (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
96         pmap_unset (SM_PROG, SM_VERS);
97         exit (0);
98 }
99
100 /*
101  * Startup information.
102  */
103 static void log_modes(void)
104 {
105         char buf[128];          /* watch stack size... */
106
107         /* No flags = no message */
108         if (!run_mode) return;
109
110         memset(buf,0,128);
111         sprintf(buf,"Flags: ");
112         if (run_mode & MODE_NODAEMON)
113                 strcat(buf,"No-Daemon ");
114         if (run_mode & MODE_LOG_STDERR)
115                 strcat(buf,"Log-STDERR ");
116
117         if (run_mode & MODE_NOTIFY_ONLY)
118         {
119                 strcat(buf,"Notify-Only ");
120         }
121         log(L_WARNING,buf);
122         /* future: IP aliasing
123         if (run_mode & MODE_NOTIFY_ONLY)
124         {
125                 dprintf(L_DEBUG,"Notify IP: %s",svr_addr);
126         } */
127 }
128
129 /*
130  * Since we do more than standard statd stuff, we might need to
131  * help the occasional admin. 
132  */
133 static void 
134 usage()
135 {
136         fprintf(stderr,"usage: %s [options]\n", name_p);
137         fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
138         fprintf(stderr,"      -F, --foreground     Foreground (no-daemon mode)\n");
139         fprintf(stderr,"      -d, --no-syslog      Verbose logging to stderr.  Foreground mode only.\n");
140         fprintf(stderr,"      -p, --port           Port to listen on\n");
141         fprintf(stderr,"      -o, --outgoing-port  Port for outgoing connections\n");
142         fprintf(stderr,"      -V, -v, --version    Display version information and exit.\n");
143         fprintf(stderr,"      -n, --name           Specify a local hostname.\n");
144         fprintf(stderr,"      -P                   State directory path.\n");
145         fprintf(stderr,"      -N                   Run in notify only mode.\n");
146 }
147
148 /* 
149  * Entry routine/main loop.
150  */
151 int main (int argc, char **argv)
152 {
153         extern char *optarg;
154         int pid;
155         int arg;
156         int port = 0, out_port = 0;
157
158         /* Default: daemon mode, no other options */
159         run_mode = 0;
160
161         /* Set the basename */
162         if ((name_p = strrchr(argv[0],'/')) != NULL) {
163                 name_p ++;
164         } else {
165                 name_p = argv[0];
166         }
167
168         /* Get the version */
169         if ((version_p = strrchr(VERSION,' ')) != NULL) {
170                 version_p++;
171         } else {
172                 version_p = VERSION;
173         }
174         
175         /* Set hostname */
176         MY_NAME = NULL;
177
178         /* Process command line switches */
179         while ((arg = getopt_long(argc, argv, "h?vVFNdn:p:o:P:", longopts, NULL)) != EOF) {
180                 switch (arg) {
181                 case 'V':       /* Version */
182                 case 'v':
183                         printf("%s version %s\n",name_p,version_p);
184                         exit(0);
185                 case 'F':       /* Foreground/nodaemon mode */
186                         run_mode |= MODE_NODAEMON;
187                         break;
188                 case 'N':
189                         run_mode |= MODE_NOTIFY_ONLY;
190                         break;
191                 case 'd':       /* No daemon only - log to stderr */
192                         run_mode |= MODE_LOG_STDERR;
193                         break;
194                 case 'o':
195                         out_port = atoi(optarg);
196                         if (out_port < 1 || out_port > 65535) {
197                                 fprintf(stderr, "%s: bad port number: %s\n",
198                                         argv[0], optarg);
199                                 usage();
200                                 exit(1);
201                         }
202                         break;
203                 case 'p':
204                         port = atoi(optarg);
205                         if (port < 1 || port > 65535) {
206                                 fprintf(stderr, "%s: bad port number: %s\n",
207                                         argv[0], optarg);
208                                 usage();
209                                 exit(1);
210                         }
211                         break;
212                 case 'n':       /* Specify local hostname */
213                         MY_NAME = xstrdup(optarg);
214                         break;
215                 case 'P':
216
217                         if ((DIR_BASE = xstrdup(optarg)) == NULL) {
218                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
219                                         argv[0], optarg);
220                                 exit(1);
221                         }
222
223                         SM_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm"));
224                         SM_BAK_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm.bak"));
225                         SM_STAT_PATH = xmalloc(strlen(DIR_BASE) + 1 + sizeof("state"));
226
227                         if ((SM_DIR == NULL) 
228                             || (SM_BAK_DIR == NULL) 
229                             || (SM_STAT_PATH == NULL)) {
230
231                                 fprintf(stderr, "%s: xmalloc() failed!\n",
232                                         argv[0]);
233                                 exit(1);
234                         }
235                         if (DIR_BASE[strlen(DIR_BASE)-1] == '/') {
236                                 sprintf(SM_DIR, "%ssm", DIR_BASE );
237                                 sprintf(SM_BAK_DIR, "%ssm.bak", DIR_BASE );
238                                 sprintf(SM_STAT_PATH, "%sstate", DIR_BASE );
239                         } else {
240                                 sprintf(SM_DIR, "%s/sm", DIR_BASE );
241                                 sprintf(SM_BAK_DIR, "%s/sm.bak", DIR_BASE );
242                                 sprintf(SM_STAT_PATH, "%s/state", DIR_BASE );
243                         }
244                         break;
245                 case '?':       /* heeeeeelllllllpppp? heh */
246                 case 'h':
247                         usage();
248                         exit (0);
249                 default:        /* oh dear ... heh */
250                         usage();
251                         exit(-1);
252                 }
253         }
254
255         if (port == out_port && port != 0) {
256                 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
257                 exit(-1);
258         }
259
260         if (!(run_mode & MODE_NODAEMON)) {
261                 run_mode &= ~MODE_LOG_STDERR;   /* Never log to console in
262                                                    daemon mode. */
263         }
264
265         log_init (name_p,version_p);
266
267         log_modes();
268
269 #ifdef SIMULATIONS
270         if (argc > 1)
271                 /* LH - I _really_ need to update simulator... */
272                 simulator (--argc, ++argv);     /* simulator() does exit() */
273 #endif
274         
275         if (!(run_mode & MODE_NODAEMON)) {
276                 int filedes, fdmax, tempfd;
277
278                 if ((pid = fork ()) < 0) {
279                         perror ("Could not fork");
280                         exit (1);
281                 } else if (pid != 0) {
282                         /* Parent. */
283                         exit (0);
284                 }
285                 /* Child.       */
286                 setsid ();
287                 chdir (DIR_BASE);
288
289                 tempfd = open("/dev/null", O_RDWR);
290                 close(0); dup2(tempfd, 0);
291                 close(1); dup2(tempfd, 1);
292                 close(2); dup2(tempfd, 2);
293                 fdmax = sysconf (_SC_OPEN_MAX);
294                 for (filedes = 3; filedes < fdmax; filedes++) {
295                         close (filedes);
296                 }
297         }
298
299         /* Child. */
300         signal (SIGHUP, killer);
301         signal (SIGINT, killer);
302         signal (SIGTERM, killer);
303         /* WARNING: the following works on Linux and SysV, but not BSD! */
304         signal(SIGCHLD, SIG_IGN);
305
306         /* initialize out_port */
307         statd_get_socket(out_port);
308
309         for (;;) {
310                 pmap_unset (SM_PROG, SM_VERS);
311                 change_state ();
312                 shuffle_dirs ();        /* Move directory names around */
313                 notify_hosts ();        /* Send out notify requests */
314                 ++restart;
315
316                 /* this registers both UDP and TCP services */
317                 if (!(run_mode & MODE_NOTIFY_ONLY)) {
318                         rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
319                 } 
320
321                 /*
322                  * Handle incoming requests:  SM_NOTIFY socket requests, as
323                  * well as callbacks from lockd.
324                  */
325                 my_svc_run();   /* I rolled my own, Olaf made it better... */
326
327                 if ((run_mode & MODE_NOTIFY_ONLY))
328                         break;                  
329         }
330         return 0;
331 }