Fix parameter name.
[glibc/pb-stable.git] / nscd / nscd.c
blob69cfa90ab50816fd9c2afeb256279532688aa13a
1 /* Copyright (c) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
22 #include <argp.h>
23 #include <assert.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <error.h>
27 #include <fcntl.h>
28 #include <libintl.h>
29 #include <locale.h>
30 #include <paths.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <unistd.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/un.h>
43 #include "dbg_log.h"
44 #include "nscd.h"
45 #include "../nss/nsswitch.h"
46 #include <device-nrs.h>
48 /* Get libc version number. */
49 #include <version.h>
51 #define PACKAGE _libc_intl_domainname
53 /* Structure used by main() thread to keep track of the number of
54 active threads. Used to limit how many threads it will create
55 and under a shutdown condition to wait till all in-progress
56 requests have finished before "turning off the lights". */
58 typedef struct
60 int num_active;
61 pthread_cond_t thread_exit_cv;
62 pthread_mutex_t mutex;
63 } thread_info_t;
65 thread_info_t thread_info;
67 int do_shutdown;
68 int disabled_passwd;
69 int disabled_group;
70 int go_background = 1;
72 int secure[lastdb];
73 int secure_in_use;
74 static const char *conffile = _PATH_NSCDCONF;
76 time_t start_time;
78 static int check_pid (const char *file);
79 static int write_pid (const char *file);
81 /* Name and version of program. */
82 static void print_version (FILE *stream, struct argp_state *state);
83 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
85 /* Definitions of arguments for argp functions. */
86 static const struct argp_option options[] =
88 { "config-file", 'f', N_("NAME"), 0,
89 N_("Read configuration data from NAME") },
90 { "debug", 'd', NULL, 0,
91 N_("Do not fork and display messages on the current tty") },
92 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
93 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
94 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
95 { "invalidate", 'i', N_("TABLE"), 0,
96 N_("Invalidate the specified cache") },
97 { "secure", 'S', N_("TABLE,yes"), 0, N_("Use separate cache for each user")},
98 { NULL, 0, NULL, 0, NULL }
101 /* Short description of program. */
102 static const char doc[] = N_("Name Service Cache Daemon.");
104 /* Prototype for option handler. */
105 static error_t parse_opt (int key, char *arg, struct argp_state *state);
107 /* Data structure to communicate with argp functions. */
108 static struct argp argp =
110 options, parse_opt, NULL, doc,
113 /* True if only statistics are requested. */
114 static bool get_stats;
117 main (int argc, char **argv)
119 int remaining;
121 /* Set locale via LC_ALL. */
122 setlocale (LC_ALL, "");
123 /* Set the text message domain. */
124 textdomain (PACKAGE);
126 /* Parse and process arguments. */
127 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
129 if (remaining != argc)
131 error (0, 0, gettext ("wrong number of arguments"));
132 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
133 exit (EXIT_FAILURE);
136 /* Read the configuration file. */
137 if (nscd_parse_file (conffile, dbs) != 0)
139 /* We couldn't read the configuration file. We don't start the
140 server. */
141 dbg_log (_("cannot read configuration file; this is fatal"));
142 exit (1);
145 /* Do we only get statistics? */
146 if (get_stats)
147 /* Does not return. */
148 receive_print_stats ();
150 /* Check if we are already running. */
151 if (check_pid (_PATH_NSCDPID))
152 error (EXIT_FAILURE, 0, _("already running"));
154 /* Remember when we started. */
155 start_time = time (NULL);
157 /* Behave like a daemon. */
158 if (go_background)
160 int i;
162 if (fork ())
163 exit (0);
165 int nullfd = open (_PATH_DEVNULL, O_RDWR);
166 if (nullfd != -1)
168 struct stat64 st;
170 if (fstat64 (nullfd, &st) == 0 && S_ISCHR (st.st_mode) != 0
171 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
172 && st.st_rdev == makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR)
173 #endif
176 /* It is the /dev/null special device alright. */
177 (void) dup2 (nullfd, STDIN_FILENO);
178 (void) dup2 (nullfd, STDOUT_FILENO);
179 (void) dup2 (nullfd, STDERR_FILENO);
181 if (nullfd > 2)
182 close (nullfd);
184 else
186 /* Ugh, somebody is trying to play a trick on us. */
187 close (nullfd);
188 nullfd = -1;
191 int min_close_fd = nullfd == -1 ? 0 : STDERR_FILENO + 1;
193 DIR *d = opendir ("/proc/self/fd");
194 if (d != NULL)
196 struct dirent64 *dirent;
197 int dfdn = dirfd (d);
199 while ((dirent = readdir64 (d)) != NULL)
201 char *endp;
202 long int fdn = strtol (dirent->d_name, &endp, 10);
204 if (*endp == '\0' && fdn != dfdn && fdn >= min_close_fd)
205 close ((int) fdn);
208 closedir (d);
210 else
211 for (i = min_close_fd; i < getdtablesize (); i++)
212 close (i);
214 if (fork ())
215 exit (0);
217 setsid ();
219 chdir ("/");
221 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
223 if (write_pid (_PATH_NSCDPID) < 0)
224 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
226 /* Ignore job control signals. */
227 signal (SIGTTOU, SIG_IGN);
228 signal (SIGTTIN, SIG_IGN);
229 signal (SIGTSTP, SIG_IGN);
232 signal (SIGINT, termination_handler);
233 signal (SIGQUIT, termination_handler);
234 signal (SIGTERM, termination_handler);
235 signal (SIGPIPE, SIG_IGN);
237 /* Cleanup files created by a previous `bind'. */
238 unlink (_PATH_NSCDSOCKET);
240 /* Make sure we do not get recursive calls. */
241 __nss_disable_nscd ();
243 /* Init databases. */
244 nscd_init ();
246 /* Handle incoming requests */
247 start_threads ();
249 return 0;
253 /* Handle program arguments. */
254 static error_t
255 parse_opt (int key, char *arg, struct argp_state *state)
257 switch (key)
259 case 'd':
260 ++debug_level;
261 go_background = 0;
262 break;
264 case 'f':
265 conffile = arg;
266 break;
268 case 'K':
269 if (getuid () != 0)
270 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
272 int sock = nscd_open_socket ();
273 request_header req;
274 ssize_t nbytes;
276 if (sock == -1)
277 exit (EXIT_FAILURE);
279 req.version = NSCD_VERSION;
280 req.type = SHUTDOWN;
281 req.key_len = 0;
282 nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
283 sizeof (request_header)));
284 close (sock);
285 exit (nbytes != sizeof (request_header) ? EXIT_FAILURE : EXIT_SUCCESS);
288 case 'g':
289 get_stats = true;
290 break;
292 case 'i':
293 if (getuid () != 0)
294 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
295 else
297 int sock = nscd_open_socket ();
298 request_header req;
299 ssize_t nbytes;
301 if (sock == -1)
302 exit (EXIT_FAILURE);
304 if (strcmp (arg, "passwd") == 0)
305 req.key_len = sizeof "passwd";
306 else if (strcmp (arg, "group") == 0)
307 req.key_len = sizeof "group";
308 else if (strcmp (arg, "hosts") == 0)
309 req.key_len = sizeof "hosts";
310 else
311 return ARGP_ERR_UNKNOWN;
313 req.version = NSCD_VERSION;
314 req.type = INVALIDATE;
315 nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
316 sizeof (request_header)));
317 if (nbytes != sizeof (request_header))
319 close (sock);
320 exit (EXIT_FAILURE);
323 nbytes = TEMP_FAILURE_RETRY (write (sock, (void *)arg, req.key_len));
325 close (sock);
327 exit (nbytes != req.key_len ? EXIT_FAILURE : EXIT_SUCCESS);
330 case 't':
331 nthreads = atol (arg);
332 break;
334 case 'S':
335 if (strcmp (arg, "passwd,yes") == 0)
336 secure_in_use = secure[pwddb] = 1;
337 else if (strcmp (arg, "group,yes") == 0)
338 secure_in_use = secure[grpdb] = 1;
339 else if (strcmp (arg, "hosts,yes") == 0)
340 secure_in_use = secure[hstdb] = 1;
341 break;
343 default:
344 return ARGP_ERR_UNKNOWN;
347 return 0;
350 /* Print the version information. */
351 static void
352 print_version (FILE *stream, struct argp_state *state)
354 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
355 fprintf (stream, gettext ("\
356 Copyright (C) %s Free Software Foundation, Inc.\n\
357 This is free software; see the source for copying conditions. There is NO\n\
358 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
359 "), "2003");
360 fprintf (stream, gettext ("Written by %s.\n"),
361 "Thorsten Kukuk and Ulrich Drepper");
365 /* Create a socket connected to a name. */
367 nscd_open_socket (void)
369 struct sockaddr_un addr;
370 int sock;
372 sock = socket (PF_UNIX, SOCK_STREAM, 0);
373 if (sock < 0)
374 return -1;
376 addr.sun_family = AF_UNIX;
377 assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
378 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
379 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
381 close (sock);
382 return -1;
385 return sock;
388 /* Cleanup. */
389 void
390 termination_handler (int signum)
392 close_sockets ();
394 /* Clean up the file created by `bind'. */
395 unlink (_PATH_NSCDSOCKET);
397 /* Clean up pid file. */
398 unlink (_PATH_NSCDPID);
400 exit (EXIT_SUCCESS);
403 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
404 static int
405 check_pid (const char *file)
407 FILE *fp;
409 fp = fopen (file, "r");
410 if (fp)
412 pid_t pid;
413 int n;
415 n = fscanf (fp, "%d", &pid);
416 fclose (fp);
418 if (n != 1 || kill (pid, 0) == 0)
419 return 1;
422 return 0;
425 /* Write the current process id to the file FILE.
426 Returns 0 if successful, -1 if not. */
427 static int
428 write_pid (const char *file)
430 FILE *fp;
432 fp = fopen (file, "w");
433 if (fp == NULL)
434 return -1;
436 fprintf (fp, "%d\n", getpid ());
437 if (fflush (fp) || ferror (fp))
438 return -1;
440 fclose (fp);
442 return 0;