]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/statd.c
Ignore SIGPIPE in 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 <errno.h>
16 #include <string.h>
17 #include <getopt.h>
18 #include <rpc/rpc.h>
19 #include <rpc/pmap_clnt.h>
20 #include <rpcmisc.h>
21 #include <sys/resource.h>
22 #include <grp.h>
23 #include "statd.h"
24 #include "version.h"
25
26 /* Socket operations */
27 #include <sys/types.h>
28 #include <sys/socket.h>
29
30 /* Added to enable specification of state directory path at run-time
31  * j_carlos_gomez@yahoo.com
32  */
33
34 char * DIR_BASE = DEFAULT_DIR_BASE;
35
36 char *  SM_DIR = DEFAULT_SM_DIR;
37 char *  SM_BAK_DIR =  DEFAULT_SM_BAK_DIR;
38 char *  SM_STAT_PATH = DEFAULT_SM_STAT_PATH;
39
40 /* ----- end of state directory path stuff ------- */
41
42 short int restart = 0;
43 int     run_mode = 0;           /* foreground logging mode */
44
45 /* LH - I had these local to main, but it seemed silly to have 
46  * two copies of each - one in main(), one static in log.c... 
47  * It also eliminates the 256-char static in log.c */
48 char *name_p = NULL;
49 char *version_p = NULL;
50
51 /* PRC: a high-availability callout program can be specified with -H
52  * When this is done, the program will receive callouts whenever clients
53  * are added or deleted to the notify list */
54 char *ha_callout_prog = NULL;
55
56 static struct option longopts[] =
57 {
58         { "foreground", 0, 0, 'F' },
59         { "no-syslog", 0, 0, 'd' },
60         { "help", 0, 0, 'h' },
61         { "version", 0, 0, 'v' },
62         { "outgoing-port", 1, 0, 'o' },
63         { "port", 1, 0, 'p' },
64         { "name", 1, 0, 'n' },
65         { "state-directory-path", 1, 0, 'P' },
66         { "notify-mode", 0, 0, 'N' },
67         { "ha-callout", 1, 0, 'H' },
68         { NULL, 0, 0, 0 }
69 };
70
71 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
72 extern int statd_get_socket(int port);
73
74 #ifdef SIMULATIONS
75 extern void simulator (int, char **);
76 #endif
77
78
79 #ifdef HAVE_TCP_WRAPPER 
80 #include "tcpwrapper.h"
81
82 static void 
83 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
84 {
85         /* remote host authorization check */
86         if (!check_default("statd", svc_getcaller(transp),
87                                  rqstp->rq_proc, SM_PROG)) {
88                 svcerr_auth (transp, AUTH_FAILED);
89                 return;
90         }
91
92         sm_prog_1 (rqstp, transp);
93 }
94
95 #define sm_prog_1 sm_prog_1_wrapper
96 #endif
97
98 /*
99  * Signal handler.
100  */
101 static void 
102 killer (int sig)
103 {
104         note (N_FATAL, "Caught signal %d, un-registering and exiting.", sig);
105         if (!(run_mode & MODE_NOTIFY_ONLY))
106                 pmap_unset (SM_PROG, SM_VERS);
107
108         exit (0);
109 }
110
111 static void
112 sigusr (int sig)
113 {
114         dprintf (N_DEBUG, "Caught signal %d, re-reading notify list.", sig);
115         re_notify = 1;
116 }
117
118 /*
119  * Startup information.
120  */
121 static void log_modes(void)
122 {
123         char buf[128];          /* watch stack size... */
124
125         /* No flags = no message */
126         if (!run_mode) return;
127
128         memset(buf,0,128);
129         sprintf(buf,"Flags: ");
130         if (run_mode & MODE_NODAEMON)
131                 strcat(buf,"No-Daemon ");
132         if (run_mode & MODE_LOG_STDERR)
133                 strcat(buf,"Log-STDERR ");
134
135         if (run_mode & MODE_NOTIFY_ONLY)
136         {
137                 strcat(buf,"Notify-Only ");
138         }
139         note(N_WARNING,buf);
140         /* future: IP aliasing
141         if (run_mode & MODE_NOTIFY_ONLY)
142         {
143                 dprintf(N_DEBUG,"Notify IP: %s",svr_addr);
144         } */
145 }
146
147 /*
148  * Since we do more than standard statd stuff, we might need to
149  * help the occasional admin. 
150  */
151 static void 
152 usage()
153 {
154         fprintf(stderr,"usage: %s [options]\n", name_p);
155         fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
156         fprintf(stderr,"      -F, --foreground     Foreground (no-daemon mode)\n");
157         fprintf(stderr,"      -d, --no-syslog      Verbose logging to stderr.  Foreground mode only.\n");
158         fprintf(stderr,"      -p, --port           Port to listen on\n");
159         fprintf(stderr,"      -o, --outgoing-port  Port for outgoing connections\n");
160         fprintf(stderr,"      -V, -v, --version    Display version information and exit.\n");
161         fprintf(stderr,"      -n, --name           Specify a local hostname.\n");
162         fprintf(stderr,"      -P                   State directory path.\n");
163         fprintf(stderr,"      -N                   Run in notify only mode.\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
199         if (st.st_uid == 0) {
200                 note(N_WARNING, "statd running as root. chown %s to choose different user\n",
201                     SM_DIR);
202                 return;
203         }
204         /* better chown the pid file before dropping, as if it
205          * if over nfs we might loose access
206          */
207         if (pidfd >= 0)
208                 fchown(pidfd, st.st_uid, st.st_gid);
209
210         setgroups(0, NULL);
211         if (setgid(st.st_gid) == -1
212             || setuid(st.st_uid) == -1) {
213                 note(N_ERROR, "Fail to drop privileges");
214                 exit(1);
215         }
216 }
217
218 /* 
219  * Entry routine/main loop.
220  */
221 int main (int argc, char **argv)
222 {
223         extern char *optarg;
224         int pid;
225         int arg;
226         int port = 0, out_port = 0;
227         struct rlimit rlim;
228
229         int pipefds[2] = { -1, -1};
230         char status;
231
232         /* Default: daemon mode, no other options */
233         run_mode = 0;
234
235         /* Set the basename */
236         if ((name_p = strrchr(argv[0],'/')) != NULL) {
237                 name_p ++;
238         } else {
239                 name_p = argv[0];
240         }
241
242         /* Get the version */
243         if ((version_p = strrchr(VERSION,' ')) != NULL) {
244                 version_p++;
245         } else {
246                 version_p = VERSION;
247         }
248         
249         /* Set hostname */
250         MY_NAME = NULL;
251
252         /* Process command line switches */
253         while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:", longopts, NULL)) != EOF) {
254                 switch (arg) {
255                 case 'V':       /* Version */
256                 case 'v':
257                         printf("%s version %s\n",name_p,version_p);
258                         exit(0);
259                 case 'F':       /* Foreground/nodaemon mode */
260                         run_mode |= MODE_NODAEMON;
261                         break;
262                 case 'N':
263                         run_mode |= MODE_NOTIFY_ONLY;
264                         break;
265                 case 'd':       /* No daemon only - log to stderr */
266                         run_mode |= MODE_LOG_STDERR;
267                         break;
268                 case 'o':
269                         out_port = atoi(optarg);
270                         if (out_port < 1 || out_port > 65535) {
271                                 fprintf(stderr, "%s: bad port number: %s\n",
272                                         argv[0], optarg);
273                                 usage();
274                                 exit(1);
275                         }
276                         break;
277                 case 'p':
278                         port = atoi(optarg);
279                         if (port < 1 || port > 65535) {
280                                 fprintf(stderr, "%s: bad port number: %s\n",
281                                         argv[0], optarg);
282                                 usage();
283                                 exit(1);
284                         }
285                         break;
286                 case 'n':       /* Specify local hostname */
287                         MY_NAME = xstrdup(optarg);
288                         break;
289                 case 'P':
290
291                         if ((DIR_BASE = xstrdup(optarg)) == NULL) {
292                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
293                                         argv[0], optarg);
294                                 exit(1);
295                         }
296
297                         SM_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm"));
298                         SM_BAK_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm.bak"));
299                         SM_STAT_PATH = xmalloc(strlen(DIR_BASE) + 1 + sizeof("state"));
300
301                         if ((SM_DIR == NULL) 
302                             || (SM_BAK_DIR == NULL) 
303                             || (SM_STAT_PATH == NULL)) {
304
305                                 fprintf(stderr, "%s: xmalloc() failed!\n",
306                                         argv[0]);
307                                 exit(1);
308                         }
309                         if (DIR_BASE[strlen(DIR_BASE)-1] == '/') {
310                                 sprintf(SM_DIR, "%ssm", DIR_BASE );
311                                 sprintf(SM_BAK_DIR, "%ssm.bak", DIR_BASE );
312                                 sprintf(SM_STAT_PATH, "%sstate", DIR_BASE );
313                         } else {
314                                 sprintf(SM_DIR, "%s/sm", DIR_BASE );
315                                 sprintf(SM_BAK_DIR, "%s/sm.bak", DIR_BASE );
316                                 sprintf(SM_STAT_PATH, "%s/state", DIR_BASE );
317                         }
318                         break;
319                 case 'H': /* PRC: specify the ha-callout program */
320                         if ((ha_callout_prog = xstrdup(optarg)) == NULL) {
321                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
322                                         argv[0], optarg);
323                                 exit(1);
324                         }
325                         break;
326                 case '?':       /* heeeeeelllllllpppp? heh */
327                 case 'h':
328                         usage();
329                         exit (0);
330                 default:        /* oh dear ... heh */
331                         usage();
332                         exit(-1);
333                 }
334         }
335
336         if (port == out_port && port != 0) {
337                 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
338                 exit(-1);
339         }
340
341         if (!(run_mode & MODE_NODAEMON)) {
342                 run_mode &= ~MODE_LOG_STDERR;   /* Never log to console in
343                                                    daemon mode. */
344         }
345
346         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
347                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
348                                 argv [0], strerror(errno));
349         else {
350                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
351                 if (rlim.rlim_cur > FD_SETSIZE) {
352                         rlim.rlim_cur = FD_SETSIZE;
353
354                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
355                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
356                                         argv [0], strerror(errno));
357                         }
358                 }
359         }
360
361 #ifdef SIMULATIONS
362         if (argc > 1)
363                 /* LH - I _really_ need to update simulator... */
364                 simulator (--argc, ++argv);     /* simulator() does exit() */
365 #endif
366         
367         if (!(run_mode & MODE_NODAEMON)) {
368                 int filedes, fdmax, tempfd;
369
370                 if (pipe(pipefds)<0) {
371                         perror("statd: unable to create pipe");
372                         exit(1);
373                 }
374                 if ((pid = fork ()) < 0) {
375                         perror ("statd: Could not fork");
376                         exit (1);
377                 } else if (pid != 0) {
378                         /* Parent.
379                          * Wait for status from child.
380                          */
381                         close(pipefds[1]);
382                         if (read(pipefds[0], &status, 1) != 1)
383                                 exit(1);
384                         exit (0);
385                 }
386                 /* Child.       */
387                 close(pipefds[0]);
388                 setsid ();
389                 if (chdir (DIR_BASE) == -1) {
390                         perror("statd: Could not chdir");
391                         exit(1);
392                 }
393
394                 while (pipefds[1] <= 2) {
395                         pipefds[1] = dup(pipefds[1]);
396                         if (pipefds[1]<0) {
397                                 perror("statd: dup");
398                                 exit(1);
399                         }
400                 }
401                 tempfd = open("/dev/null", O_RDWR);
402                 close(0); dup2(tempfd, 0);
403                 close(1); dup2(tempfd, 1);
404                 close(2); dup2(tempfd, 2);
405                 fdmax = sysconf (_SC_OPEN_MAX);
406                 for (filedes = 3; filedes < fdmax; filedes++) 
407                         if (filedes != pipefds[1])
408                                 close (filedes);
409
410         }
411
412         /* Child. */
413
414         log_init (name_p,version_p);
415
416         log_modes();
417
418         signal (SIGHUP, killer);
419         signal (SIGINT, killer);
420         signal (SIGTERM, killer);
421         /* PRC: trap SIGUSR1 to re-read notify list from disk */
422         signal(SIGUSR1, sigusr);
423         /* WARNING: the following works on Linux and SysV, but not BSD! */
424         signal(SIGCHLD, SIG_IGN);
425         /*
426          * Ignore SIGPIPE to avoid statd dying when peers close their
427          * TCP connection while we're trying to reply to them.
428          */
429         signal(SIGPIPE, SIG_IGN);
430
431         /* initialize out_port */
432         statd_get_socket(out_port);
433
434         create_pidfile();
435         atexit(truncate_pidfile);
436         drop_privs();
437
438         for (;;) {
439                 if (!(run_mode & MODE_NOTIFY_ONLY)) {
440                         /* Do not do pmap_unset() when running in notify mode.
441                          * We may clear the portmapper record for a statd not
442                          * running in notify mode disabling it.
443                          * Juan C. Gomez j_carlos_gomez@yahoo.com
444                          */
445                         pmap_unset (SM_PROG, SM_VERS);
446                 }
447                 change_state ();
448                 shuffle_dirs ();        /* Move directory names around */
449
450                 /* If we got this far, we have successfully started, so notify parent */
451                 if (pipefds[1] > 0) {
452                         status = 0;
453                         write(pipefds[1], &status, 1);
454                         close(pipefds[1]);
455                         pipefds[1] = -1;
456                 }
457
458                 notify_hosts ();        /* Send out notify requests */
459                 ++restart;
460
461                 /* this registers both UDP and TCP services */
462                 if (!(run_mode & MODE_NOTIFY_ONLY)) {
463                         rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
464                 } 
465
466                 /*
467                  * Handle incoming requests:  SM_NOTIFY socket requests, as
468                  * well as callbacks from lockd.
469                  */
470                 my_svc_run();   /* I rolled my own, Olaf made it better... */
471
472                 if ((run_mode & MODE_NOTIFY_ONLY))
473                         break;                  
474         }
475         return 0;
476 }