]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/statd.c
Merge branch 'upstream'
[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 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <sys/stat.h>
15 #include <limits.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include <rpc/rpc.h>
23 #include <rpc/pmap_clnt.h>
24 #include <rpcmisc.h>
25 #include <sys/resource.h>
26 #include <sys/wait.h>
27 #include <grp.h>
28 #include "statd.h"
29 #include "version.h"
30 #include "nfslib.h"
31
32 /* Socket operations */
33 #include <sys/types.h>
34 #include <sys/socket.h>
35
36 /* Added to enable specification of state directory path at run-time
37  * j_carlos_gomez@yahoo.com
38  */
39
40 char * DIR_BASE = DEFAULT_DIR_BASE;
41
42 char *  SM_DIR = DEFAULT_SM_DIR;
43 char *  SM_BAK_DIR =  DEFAULT_SM_BAK_DIR;
44 char *  SM_STAT_PATH = DEFAULT_SM_STAT_PATH;
45
46 /* ----- end of state directory path stuff ------- */
47
48 int     run_mode = 0;           /* foreground logging mode */
49
50 /* LH - I had these local to main, but it seemed silly to have 
51  * two copies of each - one in main(), one static in log.c... 
52  * It also eliminates the 256-char static in log.c */
53 char *name_p = NULL;
54 const char *version_p = NULL;
55
56 /* PRC: a high-availability callout program can be specified with -H
57  * When this is done, the program will receive callouts whenever clients
58  * are added or deleted to the notify list */
59 char *ha_callout_prog = NULL;
60
61 static struct option longopts[] =
62 {
63         { "foreground", 0, 0, 'F' },
64         { "no-syslog", 0, 0, 'd' },
65         { "help", 0, 0, 'h' },
66         { "version", 0, 0, 'v' },
67         { "outgoing-port", 1, 0, 'o' },
68         { "port", 1, 0, 'p' },
69         { "name", 1, 0, 'n' },
70         { "state-directory-path", 1, 0, 'P' },
71         { "notify-mode", 0, 0, 'N' },
72         { "ha-callout", 1, 0, 'H' },
73         { "no-notify", 0, 0, 'L' },
74         { NULL, 0, 0, 0 }
75 };
76
77 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
78 extern int statd_get_socket(void);
79 static void load_state_number(void);
80
81 #ifdef SIMULATIONS
82 extern void simulator (int, char **);
83 #endif
84
85
86 #ifdef HAVE_TCP_WRAPPER 
87 #include "tcpwrapper.h"
88
89 static void 
90 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
91 {
92         /* remote host authorization check */
93         if (!check_default("statd", svc_getcaller(transp),
94                                  rqstp->rq_proc, SM_PROG)) {
95                 svcerr_auth (transp, AUTH_FAILED);
96                 return;
97         }
98
99         sm_prog_1 (rqstp, transp);
100 }
101
102 #define sm_prog_1 sm_prog_1_wrapper
103 #endif
104
105 /*
106  * Signal handler.
107  */
108 static void 
109 killer (int sig)
110 {
111         note (N_FATAL, "Caught signal %d, un-registering and exiting.", sig);
112         pmap_unset (SM_PROG, SM_VERS);
113
114         exit (0);
115 }
116
117 static void
118 sigusr (int sig)
119 {
120         extern void my_svc_exit (void);
121         dprintf (N_DEBUG, "Caught signal %d, re-notifying (state %d).", sig,
122                                                                 MY_STATE);
123         my_svc_exit();
124 }
125
126 /*
127  * Startup information.
128  */
129 static void log_modes(void)
130 {
131         char buf[128];          /* watch stack size... */
132
133         /* No flags = no message */
134         if (!run_mode) return;
135
136         memset(buf,0,128);
137         sprintf(buf,"Flags: ");
138         if (run_mode & MODE_NODAEMON)
139                 strcat(buf,"No-Daemon ");
140         if (run_mode & MODE_LOG_STDERR)
141                 strcat(buf,"Log-STDERR ");
142
143         note(N_WARNING,buf);
144 }
145
146 /*
147  * Since we do more than standard statd stuff, we might need to
148  * help the occasional admin. 
149  */
150 static void 
151 usage()
152 {
153         fprintf(stderr,"usage: %s [options]\n", name_p);
154         fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
155         fprintf(stderr,"      -F, --foreground     Foreground (no-daemon mode)\n");
156         fprintf(stderr,"      -d, --no-syslog      Verbose logging to stderr.  Foreground mode only.\n");
157         fprintf(stderr,"      -p, --port           Port to listen on\n");
158         fprintf(stderr,"      -o, --outgoing-port  Port for outgoing connections\n");
159         fprintf(stderr,"      -V, -v, --version    Display version information and exit.\n");
160         fprintf(stderr,"      -n, --name           Specify a local hostname.\n");
161         fprintf(stderr,"      -P                   State directory path.\n");
162         fprintf(stderr,"      -N                   Run in notify only mode.\n");
163         fprintf(stderr,"      -L, --no-notify      Do not perform any notification.\n");
164         fprintf(stderr,"      -H                   Specify a high-availability callout program.\n");
165 }
166
167 static const char *pidfile = "/var/run/rpc.statd.pid";
168
169 int pidfd = -1;
170 static void create_pidfile(void)
171 {
172         FILE *fp;
173
174         unlink(pidfile);
175         fp = fopen(pidfile, "w");
176         if (!fp)
177                 die("Opening %s failed: %s\n",
178                     pidfile, strerror(errno));
179         fprintf(fp, "%d\n", getpid());
180         pidfd = dup(fileno(fp));
181         if (fclose(fp) < 0)
182                 note(N_WARNING, "Flushing pid file failed.\n");
183 }
184
185 static void truncate_pidfile(void)
186 {
187         if (pidfd >= 0)
188                 ftruncate(pidfd, 0);
189 }
190
191 static void drop_privs(void)
192 {
193         struct stat st;
194
195         if (stat(SM_DIR, &st) == -1 &&
196             stat(DIR_BASE, &st) == -1) {
197                 st.st_uid = 0;
198                 st.st_gid = 0;
199         }
200
201         if (st.st_uid == 0) {
202                 note(N_WARNING, "statd running as root. chown %s to choose different user\n",
203                     SM_DIR);
204                 return;
205         }
206         /* better chown the pid file before dropping, as if it
207          * if over nfs we might loose access
208          */
209         if (pidfd >= 0)
210                 fchown(pidfd, st.st_uid, st.st_gid);
211
212         setgroups(0, NULL);
213         if (setgid(st.st_gid) == -1
214             || setuid(st.st_uid) == -1) {
215                 note(N_ERROR, "Fail to drop privileges");
216                 exit(1);
217         }
218 }
219
220 static void run_sm_notify(int outport)
221 {
222         char op[20];
223         char *av[6];
224         int ac = 0;
225
226         av[ac++] = "/usr/sbin/sm-notify";
227         if (run_mode & MODE_NODAEMON)
228                 av[ac++] = "-d";
229         if (outport) {
230                 sprintf(op, "-p%d", outport);
231                 av[ac++] = op;
232         }
233         if (run_mode & STATIC_HOSTNAME) {
234                 av[ac++] = "-v";
235                 av[ac++] = MY_NAME;
236         }
237         av[ac] = NULL;
238         execv(av[0], av);
239         fprintf(stderr, "%s: failed to run %s\n", name_p, av[0]);
240         exit(2);
241
242 }
243 /* 
244  * Entry routine/main loop.
245  */
246 int main (int argc, char **argv)
247 {
248         extern char *optarg;
249         int pid;
250         int arg;
251         int port = 0, out_port = 0;
252         struct rlimit rlim;
253
254         int pipefds[2] = { -1, -1};
255         char status;
256
257         /* Default: daemon mode, no other options */
258         run_mode = 0;
259
260         /* Set the basename */
261         if ((name_p = strrchr(argv[0],'/')) != NULL) {
262                 name_p ++;
263         } else {
264                 name_p = argv[0];
265         }
266
267         /* Get the version */
268         if ((version_p = strrchr(VERSION,' ')) != NULL) {
269                 version_p++;
270         } else {
271                 version_p = VERSION;
272         }
273         
274         /* Set hostname */
275         MY_NAME = NULL;
276
277         /* Process command line switches */
278         while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:L", longopts, NULL)) != EOF) {
279                 switch (arg) {
280                 case 'V':       /* Version */
281                 case 'v':
282                         printf("%s version %s\n",name_p,version_p);
283                         exit(0);
284                 case 'F':       /* Foreground/nodaemon mode */
285                         run_mode |= MODE_NODAEMON;
286                         break;
287                 case 'N':
288                         run_mode |= MODE_NOTIFY_ONLY;
289                         break;
290                 case 'L': /* Listen only */
291                         run_mode |= MODE_NO_NOTIFY;
292                         break;
293                 case 'd':       /* No daemon only - log to stderr */
294                         run_mode |= MODE_LOG_STDERR;
295                         break;
296                 case 'o':
297                         out_port = atoi(optarg);
298                         if (out_port < 1 || out_port > 65535) {
299                                 fprintf(stderr, "%s: bad port number: %s\n",
300                                         argv[0], optarg);
301                                 usage();
302                                 exit(1);
303                         }
304                         break;
305                 case 'p':
306                         port = atoi(optarg);
307                         if (port < 1 || port > 65535) {
308                                 fprintf(stderr, "%s: bad port number: %s\n",
309                                         argv[0], optarg);
310                                 usage();
311                                 exit(1);
312                         }
313                         break;
314                 case 'n':       /* Specify local hostname */
315                         run_mode |= STATIC_HOSTNAME;
316                         MY_NAME = xstrdup(optarg);
317                         break;
318                 case 'P':
319
320                         if ((DIR_BASE = xstrdup(optarg)) == NULL) {
321                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
322                                         argv[0], optarg);
323                                 exit(1);
324                         }
325
326                         SM_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm"));
327                         SM_BAK_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm.bak"));
328                         SM_STAT_PATH = xmalloc(strlen(DIR_BASE) + 1 + sizeof("state"));
329
330                         if ((SM_DIR == NULL) 
331                             || (SM_BAK_DIR == NULL) 
332                             || (SM_STAT_PATH == NULL)) {
333
334                                 fprintf(stderr, "%s: xmalloc() failed!\n",
335                                         argv[0]);
336                                 exit(1);
337                         }
338                         if (DIR_BASE[strlen(DIR_BASE)-1] == '/') {
339                                 sprintf(SM_DIR, "%ssm", DIR_BASE );
340                                 sprintf(SM_BAK_DIR, "%ssm.bak", DIR_BASE );
341                                 sprintf(SM_STAT_PATH, "%sstate", DIR_BASE );
342                         } else {
343                                 sprintf(SM_DIR, "%s/sm", DIR_BASE );
344                                 sprintf(SM_BAK_DIR, "%s/sm.bak", DIR_BASE );
345                                 sprintf(SM_STAT_PATH, "%s/state", DIR_BASE );
346                         }
347                         break;
348                 case 'H': /* PRC: specify the ha-callout program */
349                         if ((ha_callout_prog = xstrdup(optarg)) == NULL) {
350                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
351                                         argv[0], optarg);
352                                 exit(1);
353                         }
354                         break;
355                 case '?':       /* heeeeeelllllllpppp? heh */
356                 case 'h':
357                         usage();
358                         exit (0);
359                 default:        /* oh dear ... heh */
360                         usage();
361                         exit(-1);
362                 }
363         }
364
365         if (port == out_port && port != 0) {
366                 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
367                 exit(-1);
368         }
369
370         if (run_mode & MODE_NOTIFY_ONLY) {
371                 fprintf(stderr, "%s: -N deprecated, consider using /usr/sbin/sm-notify directly\n",
372                         name_p);
373                 run_sm_notify(out_port);
374         }
375
376
377         if (!(run_mode & MODE_NODAEMON)) {
378                 run_mode &= ~MODE_LOG_STDERR;   /* Never log to console in
379                                                    daemon mode. */
380         }
381
382         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
383                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
384                                 argv [0], strerror(errno));
385         else {
386                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
387                 if (rlim.rlim_cur > FD_SETSIZE) {
388                         rlim.rlim_cur = FD_SETSIZE;
389
390                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
391                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
392                                         argv [0], strerror(errno));
393                         }
394                 }
395         }
396
397 #ifdef SIMULATIONS
398         if (argc > 1)
399                 /* LH - I _really_ need to update simulator... */
400                 simulator (--argc, ++argv);     /* simulator() does exit() */
401 #endif
402         
403         if (!(run_mode & MODE_NODAEMON)) {
404                 int tempfd;
405
406                 if (pipe(pipefds)<0) {
407                         perror("statd: unable to create pipe");
408                         exit(1);
409                 }
410                 if ((pid = fork ()) < 0) {
411                         perror ("statd: Could not fork");
412                         exit (1);
413                 } else if (pid != 0) {
414                         /* Parent.
415                          * Wait for status from child.
416                          */
417                         close(pipefds[1]);
418                         if (read(pipefds[0], &status, 1) != 1)
419                                 exit(1);
420                         exit (0);
421                 }
422                 /* Child.       */
423                 close(pipefds[0]);
424                 setsid ();
425                 if (chdir (DIR_BASE) == -1) {
426                         perror("statd: Could not chdir");
427                         exit(1);
428                 }
429
430                 while (pipefds[1] <= 2) {
431                         pipefds[1] = dup(pipefds[1]);
432                         if (pipefds[1]<0) {
433                                 perror("statd: dup");
434                                 exit(1);
435                         }
436                 }
437                 tempfd = open("/dev/null", O_RDWR);
438                 dup2(tempfd, 0);
439                 dup2(tempfd, 1);
440                 dup2(tempfd, 2);
441                 dup2(pipefds[1], 3);
442                 pipefds[1] = 3;
443                 closeall(4);
444         }
445
446         /* Child. */
447
448         log_init (name_p,version_p);
449
450         log_modes();
451
452         signal (SIGHUP, killer);
453         signal (SIGINT, killer);
454         signal (SIGTERM, killer);
455         /* PRC: trap SIGUSR1 to re-read notify list from disk */
456         signal(SIGUSR1, sigusr);
457         /* WARNING: the following works on Linux and SysV, but not BSD! */
458         signal(SIGCHLD, SIG_IGN);
459         /*
460          * Ignore SIGPIPE to avoid statd dying when peers close their
461          * TCP connection while we're trying to reply to them.
462          */
463         signal(SIGPIPE, SIG_IGN);
464
465         create_pidfile();
466         atexit(truncate_pidfile);
467
468         if (! (run_mode & MODE_NO_NOTIFY))
469                 switch (pid = fork()) {
470                 case 0:
471                         run_sm_notify(out_port);
472                         break;
473                 case -1:
474                         break;
475                 default:
476                         waitpid(pid, NULL, 0);
477                 }
478
479         /* Make sure we have a privilege port for calling into the kernel */
480         statd_get_socket();
481
482         /* If sm-notify didn't take all the state files, load
483          * state information into our notify-list so we can
484          * pass on any SM_NOTIFY that arrives
485          */
486         load_state();
487         load_state_number();
488         pmap_unset (SM_PROG, SM_VERS);
489
490         /* this registers both UDP and TCP services */
491         rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
492
493         /* If we got this far, we have successfully started, so notify parent */
494         if (pipefds[1] > 0) {
495                 status = 0;
496                 write(pipefds[1], &status, 1);
497                 close(pipefds[1]);
498                 pipefds[1] = -1;
499         }
500
501         drop_privs();
502
503         for (;;) {
504                 /*
505                  * Handle incoming requests:  SM_NOTIFY socket requests, as
506                  * well as callbacks from lockd.
507                  */
508                 my_svc_run();   /* I rolled my own, Olaf made it better... */
509
510                 /* Only get here when simulating a crash so we should probably
511                  * start sm-notify running again.  As we have already dropped
512                  * privileges, this might not work, but I don't think
513                  * responding to SM_SIMU_CRASH is an important use cases to
514                  * get perfect.
515                  */
516                 if (! (run_mode & MODE_NO_NOTIFY))
517                         switch (pid = fork()) {
518                         case 0:
519                                 run_sm_notify(out_port);
520                                 break;
521                         case -1:
522                                 break;
523                         default:
524                                 waitpid(pid, NULL, 0);
525                         }
526
527         }
528         return 0;
529 }
530
531 static void
532 load_state_number(void)
533 {
534         int fd;
535
536         if ((fd = open(SM_STAT_PATH, O_RDONLY)) == -1)
537                 return;
538
539         read(fd, &MY_STATE, sizeof(MY_STATE));
540         close(fd);
541         fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
542         if (fd >= 0) {
543                 char buf[20];
544                 snprintf(buf, sizeof(buf), "%d", MY_STATE);
545                 write(fd, buf, strlen(buf));
546                 close(fd);
547         }
548
549 }