Update.
[glibc.git] / nscd / nscd.c
blobb2e5843a9114dc7c51e71d108e8bb325d9bc31d5
1 /* Copyright (c) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
22 #include <argp.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <error.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <pthread.h>
29 #include <pwd.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
39 #include "dbg_log.h"
40 #include "nscd.h"
42 /* Get libc version number. */
43 #include <version.h>
45 #define PACKAGE _libc_intl_domainname
47 /* Structure used by main() thread to keep track of the number of
48 active threads. Used to limit how many threads it will create
49 and under a shutdown condition to wait till all in-progress
50 requests have finished before "turning off the lights". */
52 typedef struct
54 int num_active;
55 pthread_cond_t thread_exit_cv;
56 pthread_mutex_t mutex;
57 } thread_info_t;
59 thread_info_t thread_info;
61 int do_shutdown = 0;
62 int disabled_passwd = 0;
63 int disabled_group = 0;
64 int go_background = 1;
65 static const char *conffile = _PATH_NSCDCONF;
67 static int check_pid (const char *file);
68 static int write_pid (const char *file);
70 /* Name and version of program. */
71 static void print_version (FILE *stream, struct argp_state *state);
72 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
74 /* Definitions of arguments for argp functions. */
75 static const struct argp_option options[] =
77 { "config-file", 'f', N_("NAME"), 0,
78 N_("Read configuration data from NAME") },
79 { "debug", 'd', NULL, 0,
80 N_("Do not fork and display messages on the current tty") },
81 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
82 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
83 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
84 { NULL, 0, NULL, 0, NULL }
87 /* Short description of program. */
88 static const char doc[] = N_("Name Service Cache Daemon.");
90 /* Prototype for option handler. */
91 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
93 /* Data structure to communicate with argp functions. */
94 static struct argp argp =
96 options, parse_opt, NULL, doc,
99 int
100 main (int argc, char **argv)
102 int remaining;
104 /* Set locale via LC_ALL. */
105 setlocale (LC_ALL, "");
106 /* Set the text message domain. */
107 textdomain (PACKAGE);
109 /* Parse and process arguments. */
110 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
112 if (remaining != argc)
114 error (0, 0, gettext ("wrong number of arguments"));
115 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
116 exit (EXIT_FAILURE);
119 /* Check if we are already running. */
120 if (check_pid (_PATH_NSCDPID))
121 error (EXIT_FAILURE, 0, _("already running"));
123 /* Behave like a daemon. */
124 if (go_background)
126 int i;
128 if (fork ())
129 exit (0);
131 for (i = 0; i < getdtablesize (); i++)
132 close (i);
134 if (fork ())
135 exit (0);
137 chdir ("/");
139 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
141 if (write_pid (_PATH_NSCDPID) < 0)
142 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
144 /* Ignore job control signals. */
145 signal (SIGTTOU, SIG_IGN);
146 signal (SIGTTIN, SIG_IGN);
147 signal (SIGTSTP, SIG_IGN);
150 signal (SIGINT, termination_handler);
151 signal (SIGQUIT, termination_handler);
152 signal (SIGTERM, termination_handler);
153 signal (SIGPIPE, SIG_IGN);
155 /* Cleanup files created by a previous `bind'. */
156 unlink (_PATH_NSCDSOCKET);
158 /* Init databases. */
159 nscd_init (conffile);
161 /* Handle incoming requests */
162 start_threads ();
164 return 0;
168 /* Handle program arguments. */
169 static error_t
170 parse_opt (int key, char *arg, struct argp_state *state)
172 switch (key)
174 case 'd':
175 ++debug_level;
176 go_background = 0;
177 break;
179 case 'f':
180 conffile = arg;
181 break;
183 case 'K':
184 if (getuid () != 0)
185 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
187 int sock = nscd_open_socket ();
188 request_header req;
189 ssize_t nbytes;
191 if (sock == -1)
192 exit (EXIT_FAILURE);
194 req.version = NSCD_VERSION;
195 req.type = SHUTDOWN;
196 req.key_len = 0;
197 nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
198 sizeof (request_header)));
199 close (sock);
200 exit (nbytes != sizeof (request_header) ? EXIT_FAILURE : EXIT_SUCCESS);
203 case 'g':
204 receive_print_stats ();
205 /* Does not return. */
207 case 't':
208 nthreads = atol (arg);
209 break;
211 default:
212 return ARGP_ERR_UNKNOWN;
215 return 0;
218 /* Print the version information. */
219 static void
220 print_version (FILE *stream, struct argp_state *state)
222 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
223 fprintf (stream, gettext ("\
224 Copyright (C) %s Free Software Foundation, Inc.\n\
225 This is free software; see the source for copying conditions. There is NO\n\
226 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
227 "), "1999");
228 fprintf (stream, gettext ("Written by %s.\n"),
229 "Thorsten Kukuk and Ulrich Drepper");
233 /* Create a socket connected to a name. */
235 nscd_open_socket (void)
237 struct sockaddr_un addr;
238 int sock;
240 sock = socket (PF_UNIX, SOCK_STREAM, 0);
241 if (sock < 0)
242 return -1;
244 addr.sun_family = AF_UNIX;
245 assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
246 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
247 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
249 close (sock);
250 return -1;
253 return sock;
256 /* Cleanup. */
257 void
258 termination_handler (int signum)
260 close_sockets ();
262 /* Clean up the file created by `bind'. */
263 unlink (_PATH_NSCDSOCKET);
265 /* Clean up pid file. */
266 unlink (_PATH_NSCDPID);
268 exit (EXIT_SUCCESS);
271 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
272 static int
273 check_pid (const char *file)
275 FILE *fp;
277 fp = fopen (file, "r");
278 if (fp)
280 pid_t pid;
281 int n;
283 n = fscanf (fp, "%d", &pid);
284 fclose (fp);
286 if (n != 1 || kill (pid, 0) == 0)
287 return 1;
290 return 0;
293 /* Write the current process id to the file FILE.
294 Returns 0 if successful, -1 if not. */
295 static int
296 write_pid (const char *file)
298 FILE *fp;
300 fp = fopen (file, "w");
301 if (fp == NULL)
302 return -1;
304 fprintf (fp, "%d\n", getpid ());
305 if (fflush (fp) || ferror (fp))
306 return -1;
308 fclose (fp);
310 return 0;