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.
19 #include <rpc/pmap_clnt.h>
21 #include <sys/resource.h>
26 /* Socket operations */
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 /* Added to enable specification of state directory path at run-time
31 * j_carlos_gomez@yahoo.com
34 char * DIR_BASE = DEFAULT_DIR_BASE;
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;
40 /* ----- end of state directory path stuff ------- */
42 short int restart = 0;
43 int run_mode = 0; /* foreground logging mode */
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 */
49 char *version_p = NULL;
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;
56 static struct option longopts[] =
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' },
71 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
72 extern int statd_get_socket(int port);
75 extern void simulator (int, char **);
79 #ifdef HAVE_TCP_WRAPPER
80 #include "tcpwrapper.h"
83 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
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);
92 sm_prog_1 (rqstp, transp);
95 #define sm_prog_1 sm_prog_1_wrapper
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);
114 dprintf (N_DEBUG, "Caught signal %d, re-reading notify list.", sig);
119 * Startup information.
121 static void log_modes(void)
123 char buf[128]; /* watch stack size... */
125 /* No flags = no message */
126 if (!run_mode) return;
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 ");
135 if (run_mode & MODE_NOTIFY_ONLY)
137 strcat(buf,"Notify-Only ");
140 /* future: IP aliasing
141 if (run_mode & MODE_NOTIFY_ONLY)
143 dprintf(N_DEBUG,"Notify IP: %s",svr_addr);
148 * Since we do more than standard statd stuff, we might need to
149 * help the occasional admin.
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");
167 static const char *pidfile = "/var/run/rpc.statd.pid";
170 static void create_pidfile(void)
175 fp = fopen(pidfile, "w");
177 die("Opening %s failed: %s\n",
178 pidfile, strerror(errno));
179 fprintf(fp, "%d\n", getpid());
180 pidfd = dup(fileno(fp));
182 note(N_WARNING, "Flushing pid file failed.\n");
185 static void truncate_pidfile(void)
191 static void drop_privs(void)
195 if (stat(SM_DIR, &st) == -1 &&
196 stat(DIR_BASE, &st) == -1)
199 if (st.st_uid == 0) {
200 note(N_WARNING, "statd running as root. chown %s to choose different user\n",
204 /* better chown the pid file before dropping, as if it
205 * if over nfs we might loose access
208 fchown(pidfd, st.st_uid, st.st_gid);
211 if (setgid(st.st_gid) == -1
212 || setuid(st.st_uid) == -1) {
213 note(N_ERROR, "Fail to drop privileges");
219 * Entry routine/main loop.
221 int main (int argc, char **argv)
226 int port = 0, out_port = 0;
229 int pipefds[2] = { -1, -1};
232 /* Default: daemon mode, no other options */
235 /* Set the basename */
236 if ((name_p = strrchr(argv[0],'/')) != NULL) {
242 /* Get the version */
243 if ((version_p = strrchr(VERSION,' ')) != NULL) {
252 /* Process command line switches */
253 while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:", longopts, NULL)) != EOF) {
255 case 'V': /* Version */
257 printf("%s version %s\n",name_p,version_p);
259 case 'F': /* Foreground/nodaemon mode */
260 run_mode |= MODE_NODAEMON;
263 run_mode |= MODE_NOTIFY_ONLY;
265 case 'd': /* No daemon only - log to stderr */
266 run_mode |= MODE_LOG_STDERR;
269 out_port = atoi(optarg);
270 if (out_port < 1 || out_port > 65535) {
271 fprintf(stderr, "%s: bad port number: %s\n",
279 if (port < 1 || port > 65535) {
280 fprintf(stderr, "%s: bad port number: %s\n",
286 case 'n': /* Specify local hostname */
287 MY_NAME = xstrdup(optarg);
291 if ((DIR_BASE = xstrdup(optarg)) == NULL) {
292 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
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"));
302 || (SM_BAK_DIR == NULL)
303 || (SM_STAT_PATH == NULL)) {
305 fprintf(stderr, "%s: xmalloc() failed!\n",
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 );
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 );
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",
326 case '?': /* heeeeeelllllllpppp? heh */
330 default: /* oh dear ... heh */
336 if (port == out_port && port != 0) {
337 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
341 if (!(run_mode & MODE_NODAEMON)) {
342 run_mode &= ~MODE_LOG_STDERR; /* Never log to console in
346 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
347 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
348 argv [0], strerror(errno));
350 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
351 if (rlim.rlim_cur > FD_SETSIZE) {
352 rlim.rlim_cur = FD_SETSIZE;
354 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
355 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
356 argv [0], strerror(errno));
363 /* LH - I _really_ need to update simulator... */
364 simulator (--argc, ++argv); /* simulator() does exit() */
367 if (!(run_mode & MODE_NODAEMON)) {
368 int filedes, fdmax, tempfd;
370 if (pipe(pipefds)<0) {
371 perror("statd: unable to create pipe");
374 if ((pid = fork ()) < 0) {
375 perror ("statd: Could not fork");
377 } else if (pid != 0) {
379 * Wait for status from child.
382 if (read(pipefds[0], &status, 1) != 1)
389 if (chdir (DIR_BASE) == -1) {
390 perror("statd: Could not chdir");
394 while (pipefds[1] <= 2) {
395 pipefds[1] = dup(pipefds[1]);
397 perror("statd: dup");
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])
414 log_init (name_p,version_p);
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);
426 * Ignore SIGPIPE to avoid statd dying when peers close their
427 * TCP connection while we're trying to reply to them.
429 signal(SIGPIPE, SIG_IGN);
431 /* initialize out_port */
432 statd_get_socket(out_port);
435 atexit(truncate_pidfile);
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
445 pmap_unset (SM_PROG, SM_VERS);
448 shuffle_dirs (); /* Move directory names around */
450 /* If we got this far, we have successfully started, so notify parent */
451 if (pipefds[1] > 0) {
453 write(pipefds[1], &status, 1);
458 notify_hosts (); /* Send out notify requests */
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);
467 * Handle incoming requests: SM_NOTIFY socket requests, as
468 * well as callbacks from lockd.
470 my_svc_run(); /* I rolled my own, Olaf made it better... */
472 if ((run_mode & MODE_NOTIFY_ONLY))