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