1 /* Copyright (c) 1998-2003, 2004, 2005 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
20 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
40 #include <sys/socket.h>
48 #include "../nss/nsswitch.h"
49 #include <device-nrs.h>
51 /* Get libc version number. */
54 #define PACKAGE _libc_intl_domainname
56 /* Structure used by main() thread to keep track of the number of
57 active threads. Used to limit how many threads it will create
58 and under a shutdown condition to wait till all in-progress
59 requests have finished before "turning off the lights". */
64 pthread_cond_t thread_exit_cv
;
65 pthread_mutex_t mutex
;
68 thread_info_t thread_info
;
73 int go_background
= 1;
76 static const char *conffile
= _PATH_NSCDCONF
;
80 uintptr_t pagesize_m1
;
84 time_t restart_interval
= RESTART_INTERVAL
;
89 static int check_pid (const char *file
);
90 static int write_pid (const char *file
);
92 /* Name and version of program. */
93 static void print_version (FILE *stream
, struct argp_state
*state
);
94 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
96 /* Definitions of arguments for argp functions. */
97 static const struct argp_option options
[] =
99 { "config-file", 'f', N_("NAME"), 0,
100 N_("Read configuration data from NAME") },
101 { "debug", 'd', NULL
, 0,
102 N_("Do not fork and display messages on the current tty") },
103 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
104 { "shutdown", 'K', NULL
, 0, N_("Shut the server down") },
105 { "statistic", 'g', NULL
, 0, N_("Print current configuration statistic") },
106 { "invalidate", 'i', N_("TABLE"), 0,
107 N_("Invalidate the specified cache") },
108 { "secure", 'S', N_("TABLE,yes"), OPTION_HIDDEN
,
109 N_("Use separate cache for each user")},
110 { NULL
, 0, NULL
, 0, NULL
}
113 /* Short description of program. */
114 static const char doc
[] = N_("Name Service Cache Daemon.");
116 /* Prototype for option handler. */
117 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
119 /* Data structure to communicate with argp functions. */
120 static struct argp argp
=
122 options
, parse_opt
, NULL
, doc
,
125 /* True if only statistics are requested. */
126 static bool get_stats
;
129 main (int argc
, char **argv
)
133 /* Set locale via LC_ALL. */
134 setlocale (LC_ALL
, "");
135 /* Set the text message domain. */
136 textdomain (PACKAGE
);
138 /* Determine if the kernel has SELinux support. */
139 nscd_selinux_enabled (&selinux_enabled
);
141 /* Parse and process arguments. */
142 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
144 if (remaining
!= argc
)
146 error (0, 0, gettext ("wrong number of arguments"));
147 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
151 /* Read the configuration file. */
152 if (nscd_parse_file (conffile
, dbs
) != 0)
154 /* We couldn't read the configuration file. We don't start the
156 dbg_log (_("cannot read configuration file; this is fatal"));
160 /* Do we only get statistics? */
162 /* Does not return. */
163 receive_print_stats ();
165 /* Check if we are already running. */
166 if (check_pid (_PATH_NSCDPID
))
167 error (EXIT_FAILURE
, 0, _("already running"));
169 /* Remember when we started. */
170 start_time
= time (NULL
);
172 /* Determine page size. */
173 pagesize_m1
= getpagesize () - 1;
175 /* Behave like a daemon. */
182 error (EXIT_FAILURE
, errno
, _("cannot fork"));
186 int nullfd
= open (_PATH_DEVNULL
, O_RDWR
);
191 if (fstat64 (nullfd
, &st
) == 0 && S_ISCHR (st
.st_mode
) != 0
192 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
193 && st
.st_rdev
== makedev (DEV_NULL_MAJOR
, DEV_NULL_MINOR
)
197 /* It is the /dev/null special device alright. */
198 (void) dup2 (nullfd
, STDIN_FILENO
);
199 (void) dup2 (nullfd
, STDOUT_FILENO
);
200 (void) dup2 (nullfd
, STDERR_FILENO
);
207 /* Ugh, somebody is trying to play a trick on us. */
212 int min_close_fd
= nullfd
== -1 ? 0 : STDERR_FILENO
+ 1;
214 DIR *d
= opendir ("/proc/self/fd");
217 struct dirent64
*dirent
;
218 int dfdn
= dirfd (d
);
220 while ((dirent
= readdir64 (d
)) != NULL
)
223 long int fdn
= strtol (dirent
->d_name
, &endp
, 10);
225 if (*endp
== '\0' && fdn
!= dfdn
&& fdn
>= min_close_fd
)
232 for (i
= min_close_fd
; i
< getdtablesize (); i
++)
237 error (EXIT_FAILURE
, errno
, _("cannot fork"));
243 if (chdir ("/") != 0)
244 error (EXIT_FAILURE
, errno
,
245 _("cannot change current working cirectory to \"/\""));
247 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
249 if (write_pid (_PATH_NSCDPID
) < 0)
250 dbg_log ("%s: %s", _PATH_NSCDPID
, strerror (errno
));
252 if (!init_logfile ())
253 dbg_log (_("Could not create log file"));
255 /* Ignore job control signals. */
256 signal (SIGTTOU
, SIG_IGN
);
257 signal (SIGTTIN
, SIG_IGN
);
258 signal (SIGTSTP
, SIG_IGN
);
261 /* In foreground mode we are not paranoid. */
264 /* Start the SELinux AVC. */
268 signal (SIGINT
, termination_handler
);
269 signal (SIGQUIT
, termination_handler
);
270 signal (SIGTERM
, termination_handler
);
271 signal (SIGPIPE
, SIG_IGN
);
273 /* Cleanup files created by a previous 'bind'. */
274 unlink (_PATH_NSCDSOCKET
);
276 /* Make sure we do not get recursive calls. */
277 __nss_disable_nscd ();
279 /* Init databases. */
282 /* Handle incoming requests */
289 /* Handle program arguments. */
291 parse_opt (int key
, char *arg
, struct argp_state
*state
)
306 error (4, 0, _("Only root is allowed to use this option!"));
308 int sock
= nscd_open_socket ();
315 req
.version
= NSCD_VERSION
;
318 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
319 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 ();
342 if (strcmp (arg
, "passwd") == 0)
343 req
.key_len
= sizeof "passwd";
344 else if (strcmp (arg
, "group") == 0)
345 req
.key_len
= sizeof "group";
346 else if (strcmp (arg
, "hosts") == 0)
347 req
.key_len
= sizeof "hosts";
349 return ARGP_ERR_UNKNOWN
;
351 req
.version
= NSCD_VERSION
;
352 req
.type
= INVALIDATE
;
354 iov
[0].iov_base
= &req
;
355 iov
[0].iov_len
= sizeof (req
);
356 iov
[1].iov_base
= arg
;
357 iov
[1].iov_len
= req
.key_len
;
359 nbytes
= TEMP_FAILURE_RETRY (writev (sock
, iov
, 2));
363 exit (nbytes
!= iov
[0].iov_len
+ iov
[1].iov_len
364 ? EXIT_FAILURE
: EXIT_SUCCESS
);
368 nthreads
= atol (arg
);
373 if (strcmp (arg
, "passwd,yes") == 0)
374 secure_in_use
= dbs
[pwddb
].secure
= 1;
375 else if (strcmp (arg
, "group,yes") == 0)
376 secure_in_use
= dbs
[grpdb
].secure
= 1;
377 else if (strcmp (arg
, "hosts,yes") == 0)
378 secure_in_use
= dbs
[hstdb
].secure
= 1;
380 error (0, 0, _("secure services not implemented anymore"));
385 return ARGP_ERR_UNKNOWN
;
391 /* Print the version information. */
393 print_version (FILE *stream
, struct argp_state
*state
)
395 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
396 fprintf (stream
, gettext ("\
397 Copyright (C) %s Free Software Foundation, Inc.\n\
398 This is free software; see the source for copying conditions. There is NO\n\
399 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
401 fprintf (stream
, gettext ("Written by %s.\n"),
402 "Thorsten Kukuk and Ulrich Drepper");
406 /* Create a socket connected to a name. */
408 nscd_open_socket (void)
410 struct sockaddr_un addr
;
413 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
417 addr
.sun_family
= AF_UNIX
;
418 assert (sizeof (addr
.sun_path
) >= sizeof (_PATH_NSCDSOCKET
));
419 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
420 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
432 termination_handler (int signum
)
436 /* Clean up the file created by 'bind'. */
437 unlink (_PATH_NSCDSOCKET
);
439 /* Clean up pid file. */
440 unlink (_PATH_NSCDPID
);
442 // XXX Terminate threads.
444 /* Synchronize memory. */
445 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
447 if (!dbs
[cnt
].enabled
)
450 /* Make sure nobody keeps using the database. */
451 dbs
[cnt
].head
->timestamp
= 0;
453 if (dbs
[cnt
].persistent
)
455 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
458 /* Shutdown the SELinux AVC. */
462 _exit (EXIT_SUCCESS
);
465 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
467 check_pid (const char *file
)
471 fp
= fopen (file
, "r");
477 n
= fscanf (fp
, "%d", &pid
);
480 /* If we cannot parse the file default to assuming nscd runs.
481 If the PID is alive, assume it is running. That all unless
482 the PID is the same as the current process' since tha latter
483 can mean we re-exec. */
484 if ((n
!= 1 || kill (pid
, 0) == 0) && pid
!= getpid ())
491 /* Write the current process id to the file FILE.
492 Returns 0 if successful, -1 if not. */
494 write_pid (const char *file
)
498 fp
= fopen (file
, "w");
502 fprintf (fp
, "%d\n", getpid ());
503 if (fflush (fp
) || ferror (fp
))