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.
18 #include <rpc/pmap_clnt.h>
23 /* Socket operations */
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 /* Added to enable specification of state directory path at run-time
28 * j_carlos_gomez@yahoo.com
31 char * DIR_BASE = DEFAULT_DIR_BASE;
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;
37 /* ----- end of state directory path stuff ------- */
39 short int restart = 0;
40 int run_mode = 0; /* foreground logging mode */
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 */
46 char *version_p = NULL;
48 static struct option longopts[] =
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' },
61 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
62 extern int statd_get_socket(int port);
65 extern void simulator (int, char **);
69 #ifdef HAVE_TCP_WRAPPER
70 #include "tcpwrapper.h"
73 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
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);
82 sm_prog_1 (rqstp, transp);
85 #define sm_prog_1 sm_prog_1_wrapper
94 log (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
95 pmap_unset (SM_PROG, SM_VERS);
100 * Startup information.
102 static void log_modes(void)
104 char buf[128]; /* watch stack size... */
106 /* No flags = no message */
107 if (!run_mode) return;
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)
118 strcat(buf,"Notify-Only ");
121 /* future: IP aliasing
122 if (run_mode & MODE_NOTIFY_ONLY)
124 dprintf(L_DEBUG,"Notify IP: %s",svr_addr);
129 * Since we do more than standard statd stuff, we might need to
130 * help the occasional admin.
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");
147 * Entry routine/main loop.
149 int main (int argc, char **argv)
154 int port = 0, out_port = 0;
156 /* Default: daemon mode, no other options */
159 /* Set the basename */
160 if ((name_p = strrchr(argv[0],'/')) != NULL) {
166 /* Get the version */
167 if ((version_p = strrchr(VERSION,' ')) != NULL) {
176 /* Process command line switches */
177 while ((arg = getopt_long(argc, argv, "h?vVFdn:p:o:P:", longopts, NULL)) != EOF) {
179 case 'V': /* Version */
181 printf("%s version %s\n",name_p,version_p);
183 case 'F': /* Foreground/nodaemon mode */
184 run_mode |= MODE_NODAEMON;
186 case 'd': /* No daemon only - log to stderr */
187 run_mode |= MODE_LOG_STDERR;
190 out_port = atoi(optarg);
191 if (out_port < 1 || out_port > 65535) {
192 fprintf(stderr, "%s: bad port number: %s\n",
200 if (port < 1 || port > 65535) {
201 fprintf(stderr, "%s: bad port number: %s\n",
207 case 'n': /* Specify local hostname */
208 MY_NAME = xstrdup(optarg);
212 if ((DIR_BASE = xstrdup(optarg)) == NULL) {
213 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
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"));
223 || (SM_BAK_DIR == NULL)
224 || (SM_STAT_PATH == NULL)) {
226 fprintf(stderr, "%s: xmalloc() failed!\n",
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 );
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 );
240 case '?': /* heeeeeelllllllpppp? heh */
244 default: /* oh dear ... heh */
250 if (port == out_port && port != 0) {
251 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
255 if (!(run_mode & MODE_NODAEMON)) {
256 run_mode &= ~MODE_LOG_STDERR; /* Never log to console in
260 log_init (name_p,version_p);
266 /* LH - I _really_ need to update simulator... */
267 simulator (--argc, ++argv); /* simulator() does exit() */
270 if (!(run_mode & MODE_NODAEMON)) {
271 int filedes, fdmax, tempfd;
273 if ((pid = fork ()) < 0) {
274 perror ("Could not fork");
276 } else if (pid != 0) {
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++) {
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);
301 /* initialize out_port */
302 statd_get_socket(out_port);
305 pmap_unset (SM_PROG, SM_VERS);
307 shuffle_dirs (); /* Move directory names around */
308 notify_hosts (); /* Send out notify requests */
311 /* future: IP aliasing
312 if (!(run_mode & MODE_NOTIFY_ONLY)) {
313 rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
315 /* this registers both UDP and TCP services */
316 rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
319 * Handle incoming requests: SM_NOTIFY socket requests, as
320 * well as callbacks from lockd.
322 my_svc_run(); /* I rolled my own, Olaf made it better... */