]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/statd.c
rpc.statd: Stop overloading sockfd in utils/statd/rmtcall.c
[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 static void load_state_number(void);
79
80 #ifdef SIMULATIONS
81 extern void simulator (int, char **);
82 #endif
83
84
85 #ifdef HAVE_TCP_WRAPPER 
86 #include "tcpwrapper.h"
87
88 static void 
89 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
90 {
91         /* remote host authorization check */
92         if (!check_default("statd", svc_getcaller(transp),
93                                  rqstp->rq_proc, SM_PROG)) {
94                 svcerr_auth (transp, AUTH_FAILED);
95                 return;
96         }
97
98         sm_prog_1 (rqstp, transp);
99 }
100
101 #define sm_prog_1 sm_prog_1_wrapper
102 #endif
103
104 /*
105  * Signal handler.
106  */
107 static void 
108 killer (int sig)
109 {
110         note (N_FATAL, "Caught signal %d, un-registering and exiting.", sig);
111         pmap_unset (SM_PROG, SM_VERS);
112
113         exit (0);
114 }
115
116 static void
117 sigusr (int sig)
118 {
119         extern void my_svc_exit (void);
120         dprintf (N_DEBUG, "Caught signal %d, re-notifying (state %d).", sig,
121                                                                 MY_STATE);
122         my_svc_exit();
123 }
124
125 /*
126  * Startup information.
127  */
128 static void log_modes(void)
129 {
130         char buf[128];          /* watch stack size... */
131
132         /* No flags = no message */
133         if (!run_mode) return;
134
135         memset(buf,0,128);
136         sprintf(buf,"Flags: ");
137         if (run_mode & MODE_NODAEMON)
138                 strcat(buf,"No-Daemon ");
139         if (run_mode & MODE_LOG_STDERR)
140                 strcat(buf,"Log-STDERR ");
141
142         note(N_WARNING,buf);
143 }
144
145 /*
146  * Since we do more than standard statd stuff, we might need to
147  * help the occasional admin. 
148  */
149 static void 
150 usage(void)
151 {
152         fprintf(stderr,"usage: %s [options]\n", name_p);
153         fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
154         fprintf(stderr,"      -F, --foreground     Foreground (no-daemon mode)\n");
155         fprintf(stderr,"      -d, --no-syslog      Verbose logging to stderr.  Foreground mode only.\n");
156         fprintf(stderr,"      -p, --port           Port to listen on\n");
157         fprintf(stderr,"      -o, --outgoing-port  Port for outgoing connections\n");
158         fprintf(stderr,"      -V, -v, --version    Display version information and exit.\n");
159         fprintf(stderr,"      -n, --name           Specify a local hostname.\n");
160         fprintf(stderr,"      -P                   State directory path.\n");
161         fprintf(stderr,"      -N                   Run in notify only mode.\n");
162         fprintf(stderr,"      -L, --no-notify      Do not perform any notification.\n");
163         fprintf(stderr,"      -H                   Specify a high-availability callout program.\n");
164 }
165
166 static const char *pidfile = "/var/run/rpc.statd.pid";
167
168 int pidfd = -1;
169 static void create_pidfile(void)
170 {
171         FILE *fp;
172
173         unlink(pidfile);
174         fp = fopen(pidfile, "w");
175         if (!fp)
176                 die("Opening %s failed: %s\n",
177                     pidfile, strerror(errno));
178         fprintf(fp, "%d\n", getpid());
179         pidfd = dup(fileno(fp));
180         if (fclose(fp) < 0)
181                 note(N_WARNING, "Flushing pid file failed.\n");
182 }
183
184 static void truncate_pidfile(void)
185 {
186         if (pidfd >= 0)
187                 ftruncate(pidfd, 0);
188 }
189
190 static void drop_privs(void)
191 {
192         struct stat st;
193
194         if (stat(SM_DIR, &st) == -1 &&
195             stat(DIR_BASE, &st) == -1) {
196                 st.st_uid = 0;
197                 st.st_gid = 0;
198         }
199
200         if (st.st_uid == 0) {
201                 note(N_WARNING, "statd running as root. chown %s to choose different user\n",
202                     SM_DIR);
203                 return;
204         }
205         /* better chown the pid file before dropping, as if it
206          * if over nfs we might loose access
207          */
208         if (pidfd >= 0)
209                 fchown(pidfd, st.st_uid, st.st_gid);
210
211         setgroups(0, NULL);
212         if (setgid(st.st_gid) == -1
213             || setuid(st.st_uid) == -1) {
214                 note(N_ERROR, "Fail to drop privileges");
215                 exit(1);
216         }
217 }
218
219 static void run_sm_notify(int outport)
220 {
221         char op[20];
222         char *av[6];
223         int ac = 0;
224
225         av[ac++] = "/usr/sbin/sm-notify";
226         if (run_mode & MODE_NODAEMON)
227                 av[ac++] = "-d";
228         if (outport) {
229                 sprintf(op, "-p%d", outport);
230                 av[ac++] = op;
231         }
232         if (run_mode & STATIC_HOSTNAME) {
233                 av[ac++] = "-v";
234                 av[ac++] = MY_NAME;
235         }
236         av[ac] = NULL;
237         execv(av[0], av);
238         fprintf(stderr, "%s: failed to run %s\n", name_p, av[0]);
239         exit(2);
240
241 }
242 /* 
243  * Entry routine/main loop.
244  */
245 int main (int argc, char **argv)
246 {
247         extern char *optarg;
248         int pid;
249         int arg;
250         int port = 0, out_port = 0;
251         struct rlimit rlim;
252
253         int pipefds[2] = { -1, -1};
254         char status;
255
256         /* Default: daemon mode, no other options */
257         run_mode = 0;
258
259         /* Set the basename */
260         if ((name_p = strrchr(argv[0],'/')) != NULL) {
261                 name_p ++;
262         } else {
263                 name_p = argv[0];
264         }
265
266         /* Get the version */
267         if ((version_p = strrchr(VERSION,' ')) != NULL) {
268                 version_p++;
269         } else {
270                 version_p = VERSION;
271         }
272         
273         /* Set hostname */
274         MY_NAME = NULL;
275
276         /* Process command line switches */
277         while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:L", longopts, NULL)) != EOF) {
278                 switch (arg) {
279                 case 'V':       /* Version */
280                 case 'v':
281                         printf("%s version %s\n",name_p,version_p);
282                         exit(0);
283                 case 'F':       /* Foreground/nodaemon mode */
284                         run_mode |= MODE_NODAEMON;
285                         break;
286                 case 'N':
287                         run_mode |= MODE_NOTIFY_ONLY;
288                         break;
289                 case 'L': /* Listen only */
290                         run_mode |= MODE_NO_NOTIFY;
291                         break;
292                 case 'd':       /* No daemon only - log to stderr */
293                         run_mode |= MODE_LOG_STDERR;
294                         break;
295                 case 'o':
296                         out_port = atoi(optarg);
297                         if (out_port < 1 || out_port > 65535) {
298                                 fprintf(stderr, "%s: bad port number: %s\n",
299                                         argv[0], optarg);
300                                 usage();
301                                 exit(1);
302                         }
303                         break;
304                 case 'p':
305                         port = atoi(optarg);
306                         if (port < 1 || port > 65535) {
307                                 fprintf(stderr, "%s: bad port number: %s\n",
308                                         argv[0], optarg);
309                                 usage();
310                                 exit(1);
311                         }
312                         break;
313                 case 'n':       /* Specify local hostname */
314                         run_mode |= STATIC_HOSTNAME;
315                         MY_NAME = xstrdup(optarg);
316                         break;
317                 case 'P':
318
319                         if ((DIR_BASE = xstrdup(optarg)) == NULL) {
320                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
321                                         argv[0], optarg);
322                                 exit(1);
323                         }
324
325                         SM_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm"));
326                         SM_BAK_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm.bak"));
327                         SM_STAT_PATH = xmalloc(strlen(DIR_BASE) + 1 + sizeof("state"));
328
329                         if ((SM_DIR == NULL) 
330                             || (SM_BAK_DIR == NULL) 
331                             || (SM_STAT_PATH == NULL)) {
332
333                                 fprintf(stderr, "%s: xmalloc() failed!\n",
334                                         argv[0]);
335                                 exit(1);
336                         }
337                         if (DIR_BASE[strlen(DIR_BASE)-1] == '/') {
338                                 sprintf(SM_DIR, "%ssm", DIR_BASE );
339                                 sprintf(SM_BAK_DIR, "%ssm.bak", DIR_BASE );
340                                 sprintf(SM_STAT_PATH, "%sstate", DIR_BASE );
341                         } else {
342                                 sprintf(SM_DIR, "%s/sm", DIR_BASE );
343                                 sprintf(SM_BAK_DIR, "%s/sm.bak", DIR_BASE );
344                                 sprintf(SM_STAT_PATH, "%s/state", DIR_BASE );
345                         }
346                         break;
347                 case 'H': /* PRC: specify the ha-callout program */
348                         if ((ha_callout_prog = xstrdup(optarg)) == NULL) {
349                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
350                                         argv[0], optarg);
351                                 exit(1);
352                         }
353                         break;
354                 case '?':       /* heeeeeelllllllpppp? heh */
355                 case 'h':
356                         usage();
357                         exit (0);
358                 default:        /* oh dear ... heh */
359                         usage();
360                         exit(-1);
361                 }
362         }
363
364         if (port == out_port && port != 0) {
365                 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
366                 exit(-1);
367         }
368
369         if (run_mode & MODE_NOTIFY_ONLY) {
370                 fprintf(stderr, "%s: -N deprecated, consider using /usr/sbin/sm-notify directly\n",
371                         name_p);
372                 run_sm_notify(out_port);
373         }
374
375
376         if (!(run_mode & MODE_NODAEMON)) {
377                 run_mode &= ~MODE_LOG_STDERR;   /* Never log to console in
378                                                    daemon mode. */
379         }
380
381         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
382                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
383                                 argv [0], strerror(errno));
384         else {
385                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
386                 if (rlim.rlim_cur > FD_SETSIZE) {
387                         rlim.rlim_cur = FD_SETSIZE;
388
389                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
390                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
391                                         argv [0], strerror(errno));
392                         }
393                 }
394         }
395
396 #ifdef SIMULATIONS
397         if (argc > 1)
398                 /* LH - I _really_ need to update simulator... */
399                 simulator (--argc, ++argv);     /* simulator() does exit() */
400 #endif
401         
402         if (!(run_mode & MODE_NODAEMON)) {
403                 int tempfd;
404
405                 if (pipe(pipefds)<0) {
406                         perror("statd: unable to create pipe");
407                         exit(1);
408                 }
409                 if ((pid = fork ()) < 0) {
410                         perror ("statd: Could not fork");
411                         exit (1);
412                 } else if (pid != 0) {
413                         /* Parent.
414                          * Wait for status from child.
415                          */
416                         close(pipefds[1]);
417                         if (read(pipefds[0], &status, 1) != 1)
418                                 exit(1);
419                         exit (0);
420                 }
421                 /* Child.       */
422                 close(pipefds[0]);
423                 setsid ();
424                 if (chdir (DIR_BASE) == -1) {
425                         perror("statd: Could not chdir");
426                         exit(1);
427                 }
428
429                 while (pipefds[1] <= 2) {
430                         pipefds[1] = dup(pipefds[1]);
431                         if (pipefds[1]<0) {
432                                 perror("statd: dup");
433                                 exit(1);
434                         }
435                 }
436                 tempfd = open("/dev/null", O_RDWR);
437                 dup2(tempfd, 0);
438                 dup2(tempfd, 1);
439                 dup2(tempfd, 2);
440                 dup2(pipefds[1], 3);
441                 pipefds[1] = 3;
442                 closeall(4);
443         }
444
445         /* Child. */
446
447         log_init (/*name_p,version_p*/);
448
449         log_modes();
450
451         signal (SIGHUP, killer);
452         signal (SIGINT, killer);
453         signal (SIGTERM, killer);
454         /* PRC: trap SIGUSR1 to re-read notify list from disk */
455         signal(SIGUSR1, sigusr);
456         /* WARNING: the following works on Linux and SysV, but not BSD! */
457         signal(SIGCHLD, SIG_IGN);
458         /*
459          * Ignore SIGPIPE to avoid statd dying when peers close their
460          * TCP connection while we're trying to reply to them.
461          */
462         signal(SIGPIPE, SIG_IGN);
463
464         create_pidfile();
465         atexit(truncate_pidfile);
466
467         if (! (run_mode & MODE_NO_NOTIFY))
468                 switch (pid = fork()) {
469                 case 0:
470                         run_sm_notify(out_port);
471                         break;
472                 case -1:
473                         break;
474                 default:
475                         waitpid(pid, NULL, 0);
476                 }
477
478         /* Make sure we have a privilege port for calling into the kernel */
479         if (statd_get_socket() < 0)
480                 exit(1);
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 }