1 /* Copyright (c) 1998-2008, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
39 #include <sys/socket.h>
47 #include "../nss/nsswitch.h"
48 #include <device-nrs.h>
50 /* Get libc version number. */
53 #define PACKAGE _libc_intl_domainname
55 /* Structure used by main() thread to keep track of the number of
56 active threads. Used to limit how many threads it will create
57 and under a shutdown condition to wait till all in-progress
58 requests have finished before "turning off the lights". */
63 pthread_cond_t thread_exit_cv
;
64 pthread_mutex_t mutex
;
67 thread_info_t thread_info
;
72 int go_background
= 1;
74 static const char *conffile
= _PATH_NSCDCONF
;
78 uintptr_t pagesize_m1
;
82 time_t restart_interval
= RESTART_INTERVAL
;
87 static int check_pid (const char *file
);
88 static int write_pid (const char *file
);
90 /* Name and version of program. */
91 static void print_version (FILE *stream
, struct argp_state
*state
);
92 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
94 /* Function to print some extra text in the help message. */
95 static char *more_help (int key
, const char *text
, void *input
);
97 /* Definitions of arguments for argp functions. */
98 static const struct argp_option options
[] =
100 { "config-file", 'f', N_("NAME"), 0,
101 N_("Read configuration data from NAME") },
102 { "debug", 'd', NULL
, 0,
103 N_("Do not fork and display messages on the current tty") },
104 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
105 { "shutdown", 'K', NULL
, 0, N_("Shut the server down") },
106 { "statistics", 'g', NULL
, 0, N_("Print current configuration statistics") },
107 { "invalidate", 'i', N_("TABLE"), 0,
108 N_("Invalidate the specified cache") },
109 { "secure", 'S', N_("TABLE,yes"), OPTION_HIDDEN
,
110 N_("Use separate cache for each user")},
111 { NULL
, 0, NULL
, 0, NULL
}
114 /* Short description of program. */
115 static const char doc
[] = N_("Name Service Cache Daemon.");
117 /* Prototype for option handler. */
118 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
120 /* Data structure to communicate with argp functions. */
121 static struct argp argp
=
123 options
, parse_opt
, NULL
, doc
, NULL
, more_help
126 /* True if only statistics are requested. */
127 static bool get_stats
;
130 main (int argc
, char **argv
)
134 /* Set locale via LC_ALL. */
135 setlocale (LC_ALL
, "");
136 /* Set the text message domain. */
137 textdomain (PACKAGE
);
139 /* Determine if the kernel has SELinux support. */
140 nscd_selinux_enabled (&selinux_enabled
);
142 /* Parse and process arguments. */
143 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
145 if (remaining
!= argc
)
147 error (0, 0, gettext ("wrong number of arguments"));
148 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
152 /* Read the configuration file. */
153 if (nscd_parse_file (conffile
, dbs
) != 0)
154 /* We couldn't read the configuration file. We don't start the
156 error (EXIT_FAILURE
, 0,
157 _("failure while reading configuration file; this is fatal"));
159 /* Do we only get statistics? */
161 /* Does not return. */
162 receive_print_stats ();
164 /* Check if we are already running. */
165 if (check_pid (_PATH_NSCDPID
))
166 error (EXIT_FAILURE
, 0, _("already running"));
168 /* Remember when we started. */
169 start_time
= time (NULL
);
171 /* Determine page size. */
172 pagesize_m1
= getpagesize () - 1;
174 /* Behave like a daemon. */
181 error (EXIT_FAILURE
, errno
, _("cannot fork"));
185 int nullfd
= open (_PATH_DEVNULL
, O_RDWR
);
190 if (fstat64 (nullfd
, &st
) == 0 && S_ISCHR (st
.st_mode
) != 0
191 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
192 && st
.st_rdev
== makedev (DEV_NULL_MAJOR
, DEV_NULL_MINOR
)
196 /* It is the /dev/null special device alright. */
197 (void) dup2 (nullfd
, STDIN_FILENO
);
198 (void) dup2 (nullfd
, STDOUT_FILENO
);
199 (void) dup2 (nullfd
, STDERR_FILENO
);
206 /* Ugh, somebody is trying to play a trick on us. */
211 int min_close_fd
= nullfd
== -1 ? 0 : STDERR_FILENO
+ 1;
213 DIR *d
= opendir ("/proc/self/fd");
216 struct dirent64
*dirent
;
217 int dfdn
= dirfd (d
);
219 while ((dirent
= readdir64 (d
)) != NULL
)
222 long int fdn
= strtol (dirent
->d_name
, &endp
, 10);
224 if (*endp
== '\0' && fdn
!= dfdn
&& fdn
>= min_close_fd
)
231 for (i
= min_close_fd
; i
< getdtablesize (); i
++)
236 error (EXIT_FAILURE
, errno
, _("cannot fork"));
242 if (chdir ("/") != 0)
243 error (EXIT_FAILURE
, errno
,
244 _("cannot change current working directory to \"/\""));
246 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
248 if (write_pid (_PATH_NSCDPID
) < 0)
249 dbg_log ("%s: %s", _PATH_NSCDPID
, strerror (errno
));
251 if (!init_logfile ())
252 dbg_log (_("Could not create log file"));
254 /* Ignore job control signals. */
255 signal (SIGTTOU
, SIG_IGN
);
256 signal (SIGTTIN
, SIG_IGN
);
257 signal (SIGTSTP
, SIG_IGN
);
260 /* In foreground mode we are not paranoid. */
263 /* Start the SELinux AVC. */
267 signal (SIGINT
, termination_handler
);
268 signal (SIGQUIT
, termination_handler
);
269 signal (SIGTERM
, termination_handler
);
270 signal (SIGPIPE
, SIG_IGN
);
272 /* Cleanup files created by a previous 'bind'. */
273 unlink (_PATH_NSCDSOCKET
);
275 /* Make sure we do not get recursive calls. */
276 __nss_disable_nscd ();
278 /* Init databases. */
281 /* Handle incoming requests */
288 /* Handle program arguments. */
290 parse_opt (int key
, char *arg
, struct argp_state
*state
)
305 error (4, 0, _("Only root is allowed to use this option!"));
307 int sock
= nscd_open_socket ();
313 req
.version
= NSCD_VERSION
;
317 ssize_t nbytes
= TEMP_FAILURE_RETRY (send (sock
, &req
,
318 sizeof (request_header
),
321 exit (nbytes
!= sizeof (request_header
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
330 error (4, 0, _("Only root is allowed to use this option!"));
333 int sock
= nscd_open_socket ();
339 for (cnt
= pwddb
; cnt
< lastdb
; ++cnt
)
340 if (strcmp (arg
, dbnames
[cnt
]) == 0)
345 argp_error (state
, _("'%s' is not a known database"), arg
);
349 size_t arg_len
= strlen (arg
) + 1;
356 reqdata
.req
.key_len
= strlen (arg
) + 1;
357 reqdata
.req
.version
= NSCD_VERSION
;
358 reqdata
.req
.type
= INVALIDATE
;
359 memcpy (reqdata
.arg
, arg
, arg_len
);
361 ssize_t nbytes
= TEMP_FAILURE_RETRY (send (sock
, &reqdata
,
362 sizeof (request_header
)
366 if (nbytes
!= sizeof (request_header
) + arg_len
)
370 error (EXIT_FAILURE
, err
, _("write incomplete"));
373 /* Wait for ack. Older nscd just closed the socket when
374 prune_cache finished, silently ignore that. */
376 nbytes
= TEMP_FAILURE_RETRY (read (sock
, &resp
, sizeof (resp
)));
377 if (nbytes
!= 0 && nbytes
!= sizeof (resp
))
381 error (EXIT_FAILURE
, err
, _("cannot read invalidate ACK"));
387 error (EXIT_FAILURE
, resp
, _("invalidation failed"));
393 nthreads
= atol (arg
);
397 error (0, 0, _("secure services not implemented anymore"));
401 return ARGP_ERR_UNKNOWN
;
407 /* Print bug-reporting information in the help message. */
409 more_help (int key
, const char *text
, void *input
)
413 case ARGP_KEY_HELP_EXTRA
:
414 /* We print some extra information. */
415 return strdup (gettext ("\
416 For bug reporting instructions, please see:\n\
417 <http://www.gnu.org/software/libc/bugs.html>.\n"));
421 return (char *) text
;
424 /* Print the version information. */
426 print_version (FILE *stream
, struct argp_state
*state
)
428 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
429 fprintf (stream
, gettext ("\
430 Copyright (C) %s Free Software Foundation, Inc.\n\
431 This is free software; see the source for copying conditions. There is NO\n\
432 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
434 fprintf (stream
, gettext ("Written by %s.\n"),
435 "Thorsten Kukuk and Ulrich Drepper");
439 /* Create a socket connected to a name. */
441 nscd_open_socket (void)
443 struct sockaddr_un addr
;
446 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
450 addr
.sun_family
= AF_UNIX
;
451 assert (sizeof (addr
.sun_path
) >= sizeof (_PATH_NSCDSOCKET
));
452 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
453 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
465 termination_handler (int signum
)
469 /* Clean up the file created by 'bind'. */
470 unlink (_PATH_NSCDSOCKET
);
472 /* Clean up pid file. */
473 unlink (_PATH_NSCDPID
);
475 // XXX Terminate threads.
477 /* Synchronize memory. */
478 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
480 if (!dbs
[cnt
].enabled
)
483 /* Make sure nobody keeps using the database. */
484 dbs
[cnt
].head
->timestamp
= 0;
486 if (dbs
[cnt
].persistent
)
488 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
491 _exit (EXIT_SUCCESS
);
494 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
496 check_pid (const char *file
)
500 fp
= fopen (file
, "r");
506 n
= fscanf (fp
, "%d", &pid
);
509 /* If we cannot parse the file default to assuming nscd runs.
510 If the PID is alive, assume it is running. That all unless
511 the PID is the same as the current process' since tha latter
512 can mean we re-exec. */
513 if ((n
!= 1 || kill (pid
, 0) == 0) && pid
!= getpid ())
520 /* Write the current process id to the file FILE.
521 Returns 0 if successful, -1 if not. */
523 write_pid (const char *file
)
527 fp
= fopen (file
, "w");
531 fprintf (fp
, "%d\n", getpid ());
533 int result
= fflush (fp
) || ferror (fp
) ? -1 : 0;