1 /* Copyright (c) 1998-2006, 2007, 2008 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 /* Definitions of arguments for argp functions. */
95 static const struct argp_option options
[] =
97 { "config-file", 'f', N_("NAME"), 0,
98 N_("Read configuration data from NAME") },
99 { "debug", 'd', NULL
, 0,
100 N_("Do not fork and display messages on the current tty") },
101 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
102 { "shutdown", 'K', NULL
, 0, N_("Shut the server down") },
103 { "statistic", 'g', NULL
, 0, N_("Print current configuration statistic") },
104 { "invalidate", 'i', N_("TABLE"), 0,
105 N_("Invalidate the specified cache") },
106 { "secure", 'S', N_("TABLE,yes"), OPTION_HIDDEN
,
107 N_("Use separate cache for each user")},
108 { NULL
, 0, NULL
, 0, NULL
}
111 /* Short description of program. */
112 static const char doc
[] = N_("Name Service Cache Daemon.");
114 /* Prototype for option handler. */
115 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
117 /* Data structure to communicate with argp functions. */
118 static struct argp argp
=
120 options
, parse_opt
, NULL
, doc
,
123 /* True if only statistics are requested. */
124 static bool get_stats
;
127 main (int argc
, char **argv
)
131 /* Set locale via LC_ALL. */
132 setlocale (LC_ALL
, "");
133 /* Set the text message domain. */
134 textdomain (PACKAGE
);
136 /* Determine if the kernel has SELinux support. */
137 nscd_selinux_enabled (&selinux_enabled
);
139 /* Parse and process arguments. */
140 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
142 if (remaining
!= argc
)
144 error (0, 0, gettext ("wrong number of arguments"));
145 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
149 /* Read the configuration file. */
150 if (nscd_parse_file (conffile
, dbs
) != 0)
151 /* We couldn't read the configuration file. We don't start the
153 error (EXIT_FAILURE
, 0,
154 _("failure while reading configuration file; this is fatal"));
156 /* Do we only get statistics? */
158 /* Does not return. */
159 receive_print_stats ();
161 /* Check if we are already running. */
162 if (check_pid (_PATH_NSCDPID
))
163 error (EXIT_FAILURE
, 0, _("already running"));
165 /* Remember when we started. */
166 start_time
= time (NULL
);
168 /* Determine page size. */
169 pagesize_m1
= getpagesize () - 1;
171 /* Behave like a daemon. */
178 error (EXIT_FAILURE
, errno
, _("cannot fork"));
182 int nullfd
= open (_PATH_DEVNULL
, O_RDWR
);
187 if (fstat64 (nullfd
, &st
) == 0 && S_ISCHR (st
.st_mode
) != 0
188 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
189 && st
.st_rdev
== makedev (DEV_NULL_MAJOR
, DEV_NULL_MINOR
)
193 /* It is the /dev/null special device alright. */
194 (void) dup2 (nullfd
, STDIN_FILENO
);
195 (void) dup2 (nullfd
, STDOUT_FILENO
);
196 (void) dup2 (nullfd
, STDERR_FILENO
);
203 /* Ugh, somebody is trying to play a trick on us. */
208 int min_close_fd
= nullfd
== -1 ? 0 : STDERR_FILENO
+ 1;
210 DIR *d
= opendir ("/proc/self/fd");
213 struct dirent64
*dirent
;
214 int dfdn
= dirfd (d
);
216 while ((dirent
= readdir64 (d
)) != NULL
)
219 long int fdn
= strtol (dirent
->d_name
, &endp
, 10);
221 if (*endp
== '\0' && fdn
!= dfdn
&& fdn
>= min_close_fd
)
228 for (i
= min_close_fd
; i
< getdtablesize (); i
++)
233 error (EXIT_FAILURE
, errno
, _("cannot fork"));
239 if (chdir ("/") != 0)
240 error (EXIT_FAILURE
, errno
,
241 _("cannot change current working directory to \"/\""));
243 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
245 if (write_pid (_PATH_NSCDPID
) < 0)
246 dbg_log ("%s: %s", _PATH_NSCDPID
, strerror (errno
));
248 if (!init_logfile ())
249 dbg_log (_("Could not create log file"));
251 /* Ignore job control signals. */
252 signal (SIGTTOU
, SIG_IGN
);
253 signal (SIGTTIN
, SIG_IGN
);
254 signal (SIGTSTP
, SIG_IGN
);
257 /* In foreground mode we are not paranoid. */
260 /* Start the SELinux AVC. */
264 signal (SIGINT
, termination_handler
);
265 signal (SIGQUIT
, termination_handler
);
266 signal (SIGTERM
, termination_handler
);
267 signal (SIGPIPE
, SIG_IGN
);
269 /* Cleanup files created by a previous 'bind'. */
270 unlink (_PATH_NSCDSOCKET
);
272 /* Make sure we do not get recursive calls. */
273 __nss_disable_nscd ();
275 /* Init databases. */
278 /* Handle incoming requests */
285 /* Handle program arguments. */
287 parse_opt (int key
, char *arg
, struct argp_state
*state
)
302 error (4, 0, _("Only root is allowed to use this option!"));
304 int sock
= nscd_open_socket ();
310 req
.version
= NSCD_VERSION
;
314 ssize_t nbytes
= TEMP_FAILURE_RETRY (send (sock
, &req
,
315 sizeof (request_header
),
318 exit (nbytes
!= sizeof (request_header
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
327 error (4, 0, _("Only root is allowed to use this option!"));
330 int sock
= nscd_open_socket ();
336 for (cnt
= pwddb
; cnt
< lastdb
; ++cnt
)
337 if (strcmp (arg
, dbnames
[cnt
]) == 0)
341 return ARGP_ERR_UNKNOWN
;
343 size_t arg_len
= strlen (arg
) + 1;
350 reqdata
.req
.key_len
= strlen (arg
) + 1;
351 reqdata
.req
.version
= NSCD_VERSION
;
352 reqdata
.req
.type
= INVALIDATE
;
353 memcpy (reqdata
.arg
, arg
, arg_len
);
355 ssize_t nbytes
= TEMP_FAILURE_RETRY (send (sock
, &reqdata
,
356 sizeof (request_header
)
360 if (nbytes
!= sizeof (request_header
) + arg_len
)
364 error (EXIT_FAILURE
, err
, _("write incomplete"));
367 /* Wait for ack. Older nscd just closed the socket when
368 prune_cache finished, silently ignore that. */
370 nbytes
= TEMP_FAILURE_RETRY (read (sock
, &resp
, sizeof (resp
)));
371 if (nbytes
!= 0 && nbytes
!= sizeof (resp
))
375 error (EXIT_FAILURE
, err
, _("cannot read invalidate ACK"));
381 error (EXIT_FAILURE
, resp
, _("invalidation failed"));
387 nthreads
= atol (arg
);
391 error (0, 0, _("secure services not implemented anymore"));
395 return ARGP_ERR_UNKNOWN
;
401 /* Print the version information. */
403 print_version (FILE *stream
, struct argp_state
*state
)
405 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
406 fprintf (stream
, gettext ("\
407 Copyright (C) %s Free Software Foundation, Inc.\n\
408 This is free software; see the source for copying conditions. There is NO\n\
409 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
411 fprintf (stream
, gettext ("Written by %s.\n"),
412 "Thorsten Kukuk and Ulrich Drepper");
416 /* Create a socket connected to a name. */
418 nscd_open_socket (void)
420 struct sockaddr_un addr
;
423 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
427 addr
.sun_family
= AF_UNIX
;
428 assert (sizeof (addr
.sun_path
) >= sizeof (_PATH_NSCDSOCKET
));
429 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
430 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
442 termination_handler (int signum
)
446 /* Clean up the file created by 'bind'. */
447 unlink (_PATH_NSCDSOCKET
);
449 /* Clean up pid file. */
450 unlink (_PATH_NSCDPID
);
452 // XXX Terminate threads.
454 /* Synchronize memory. */
455 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
457 if (!dbs
[cnt
].enabled
)
460 /* Make sure nobody keeps using the database. */
461 dbs
[cnt
].head
->timestamp
= 0;
463 if (dbs
[cnt
].persistent
)
465 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
468 /* Shutdown the SELinux AVC. */
472 _exit (EXIT_SUCCESS
);
475 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
477 check_pid (const char *file
)
481 fp
= fopen (file
, "r");
487 n
= fscanf (fp
, "%d", &pid
);
490 /* If we cannot parse the file default to assuming nscd runs.
491 If the PID is alive, assume it is running. That all unless
492 the PID is the same as the current process' since tha latter
493 can mean we re-exec. */
494 if ((n
!= 1 || kill (pid
, 0) == 0) && pid
!= getpid ())
501 /* Write the current process id to the file FILE.
502 Returns 0 if successful, -1 if not. */
504 write_pid (const char *file
)
508 fp
= fopen (file
, "w");
512 fprintf (fp
, "%d\n", getpid ());
514 int result
= fflush (fp
) || ferror (fp
) ? -1 : 0;