]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/statd.c
2001-03-21 H.J. Lu <hjl@lucon.org>
[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 <string.h>
16 #include <getopt.h>
17 #include <rpc/rpc.h>
18 #include <rpc/pmap_clnt.h>
19 #include <rpcmisc.h>
20 #include "statd.h"
21 #include "version.h"
22
23 /* Socket operations */
24 #include <sys/types.h>
25 #include <sys/socket.h>
26
27
28 short int restart = 0;
29 int     run_mode = 0;           /* foreground logging mode */
30
31 /* LH - I had these local to main, but it seemed silly to have 
32  * two copies of each - one in main(), one static in log.c... 
33  * It also eliminates the 256-char static in log.c */
34 char *name_p = NULL;
35 char *version_p = NULL;
36
37 static struct option longopts[] =
38 {
39         { "foreground", 0, 0, 'F' },
40         { "no-syslog", 0, 0, 'd' },
41         { "help", 0, 0, 'h' },
42         { "version", 0, 0, 'v' },
43         { "outgoing-port", 1, 0, 'o' },
44         { "port", 1, 0, 'p' },
45         { NULL, 0, 0, 0 }
46 };
47
48 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
49 extern int statd_get_socket(int port);
50
51 #ifdef SIMULATIONS
52 extern void simulator (int, char **);
53 #endif
54
55
56 #ifdef HAVE_TCP_WRAPPER 
57 #include "tcpwrapper.h"
58
59 static void 
60 sm_prog_1_wrapper (struct svc_req *rqstp, register SVCXPRT *transp)
61 {
62         /* remote host authorization check */
63         if (!check_default("statd", svc_getcaller(transp),
64                                  rqstp->rq_proc, SM_PROG)) {
65                 svcerr_auth (transp, AUTH_FAILED);
66                 return;
67         }
68
69         sm_prog_1 (rqstp, transp);
70 }
71
72 #define sm_prog_1 sm_prog_1_wrapper
73 #endif
74
75 /*
76  * Signal handler.
77  */
78 static void 
79 killer (int sig)
80 {
81         log (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
82         pmap_unset (SM_PROG, SM_VERS);
83         exit (0);
84 }
85
86 /*
87  * Startup information.
88  */
89 static void log_modes(void)
90 {
91         char buf[128];          /* watch stack size... */
92
93         /* No flags = no message */
94         if (!run_mode) return;
95
96         memset(buf,0,128);
97         sprintf(buf,"Flags: ");
98         if (run_mode & MODE_NODAEMON)
99                 strcat(buf,"No-Daemon ");
100         if (run_mode & MODE_LOG_STDERR)
101                 strcat(buf,"Log-STDERR ");
102         /* future: IP aliasing
103         if (run_mode & MODE_NOTIFY_ONLY)
104         {
105                 strcat(buf,"Notify-Only ");
106         } */
107         log(L_WARNING,buf);
108         /* future: IP aliasing
109         if (run_mode & MODE_NOTIFY_ONLY)
110         {
111                 dprintf(L_DEBUG,"Notify IP: %s",svr_addr);
112         } */
113 }
114
115 /*
116  * Since we do more than standard statd stuff, we might need to
117  * help the occasional admin. 
118  */
119 static void 
120 usage()
121 {
122         fprintf(stderr,"usage: %s [options]\n", name_p);
123         fprintf(stderr,"      -h, -?, --help       Print this help screen.\n");
124         fprintf(stderr,"      -F, --foreground     Foreground (no-daemon mode)\n");
125         fprintf(stderr,"      -d, --no-syslog      Verbose logging to stderr.  Foreground mode only.\n");
126         fprintf(stderr,"      -p, --port           Port to listen on\n");
127         fprintf(stderr,"      -o, --outgoing-port  Port for outgoing connections\n");
128         fprintf(stderr,"      -V, -v, --version    Display version information and exit.\n");
129 }
130
131 /* 
132  * Entry routine/main loop.
133  */
134 int main (int argc, char **argv)
135 {
136         extern char *optarg;
137         int pid;
138         int arg;
139         int port = 0, out_port = 0;
140
141         /* Default: daemon mode, no other options */
142         run_mode = 0;
143
144         /* Set the basename */
145         if ((name_p = strrchr(argv[0],'/')) != NULL) {
146                 name_p ++;
147         } else {
148                 name_p = argv[0];
149         }
150
151         /* Get the version */
152         if ((version_p = strrchr(VERSION,' ')) != NULL) {
153                 version_p++;
154         } else {
155                 version_p = VERSION;
156         }
157         
158         /* Process command line switches */
159         while ((arg = getopt_long(argc, argv, "h?vVFdp:o:", longopts, NULL)) != EOF) {
160                 switch (arg) {
161                 case 'V':       /* Version */
162                 case 'v':
163                         printf("%s version %s\n",name_p,version_p);
164                         exit(0);
165                 case 'F':       /* Foreground/nodaemon mode */
166                         run_mode |= MODE_NODAEMON;
167                         break;
168                 case 'd':       /* No daemon only - log to stderr */
169                         run_mode |= MODE_LOG_STDERR;
170                         break;
171                 case 'o':
172                         out_port = atoi(optarg);
173                         if (out_port < 1 || out_port > 65535) {
174                                 fprintf(stderr, "%s: bad port number: %s\n",
175                                         argv[0], optarg);
176                                 usage();
177                                 exit(1);
178                         }
179                         break;
180                 case 'p':
181                         port = atoi(optarg);
182                         if (port < 1 || port > 65535) {
183                                 fprintf(stderr, "%s: bad port number: %s\n",
184                                         argv[0], optarg);
185                                 usage();
186                                 exit(1);
187                         }
188                         break;
189                 case '?':       /* heeeeeelllllllpppp? heh */
190                 case 'h':
191                         usage();
192                         exit (0);
193                 default:        /* oh dear ... heh */
194                         usage();
195                         exit(-1);
196                 }
197         }
198
199         if (port == out_port && port != 0) {
200                 fprintf(stderr, "Listening and outgoing ports cannot be the same!\n");
201                 exit(-1);
202         }
203
204         if (!(run_mode & MODE_NODAEMON)) {
205                 run_mode &= ~MODE_LOG_STDERR;   /* Never log to console in
206                                                    daemon mode. */
207         }
208
209         log_init (name_p,version_p);
210
211         log_modes();
212
213 #ifdef SIMULATIONS
214         if (argc > 1)
215                 /* LH - I _really_ need to update simulator... */
216                 simulator (--argc, ++argv);     /* simulator() does exit() */
217 #endif
218         
219         if (!(run_mode & MODE_NODAEMON)) {
220                 int filedes, fdmax, tempfd;
221
222                 if ((pid = fork ()) < 0) {
223                         perror ("Could not fork");
224                         exit (1);
225                 } else if (pid != 0) {
226                         /* Parent. */
227                         exit (0);
228                 }
229                 /* Child.       */
230                 setsid ();
231                 chdir (DIR_BASE);
232
233                 tempfd = open("/dev/null", O_RDWR);
234                 close(0); dup2(tempfd, 0);
235                 close(1); dup2(tempfd, 1);
236                 close(2); dup2(tempfd, 2);
237                 fdmax = sysconf (_SC_OPEN_MAX);
238                 for (filedes = 3; filedes < fdmax; filedes++) {
239                         close (filedes);
240                 }
241         }
242
243         /* Child. */
244         signal (SIGHUP, killer);
245         signal (SIGINT, killer);
246         signal (SIGTERM, killer);
247         /* WARNING: the following works on Linux and SysV, but not BSD! */
248         signal(SIGCHLD, SIG_IGN);
249
250         /* initialize out_port */
251         statd_get_socket(out_port);
252
253         for (;;) {
254                 pmap_unset (SM_PROG, SM_VERS);
255                 change_state ();
256                 shuffle_dirs ();        /* Move directory names around */
257                 notify_hosts ();        /* Send out notify requests */
258                 ++restart;
259
260                 /* future: IP aliasing 
261                 if (!(run_mode & MODE_NOTIFY_ONLY)) {
262                         rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
263                 } */
264                 /* this registers both UDP and TCP services */
265                 rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port);
266
267                 /*
268                  * Handle incoming requests:  SM_NOTIFY socket requests, as
269                  * well as callbacks from lockd.
270                  */
271                 my_svc_run();   /* I rolled my own, Olaf made it better... */
272         }
273         return 0;
274 }