1 /* Copyright (c) 1998-2003, 2004 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. */
39 #include <sys/socket.h>
45 #include "../nss/nsswitch.h"
46 #include <device-nrs.h>
48 /* Get libc version number. */
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". */
61 pthread_cond_t thread_exit_cv
;
62 pthread_mutex_t mutex
;
65 thread_info_t thread_info
;
70 int go_background
= 1;
74 static const char *conffile
= _PATH_NSCDCONF
;
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
)
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
);
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
141 dbg_log (_("cannot read configuration file; this is fatal"));
145 /* Do we only get statistics? */
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. */
164 error (EXIT_FAILURE
, errno
, _("cannot fork"));
168 int nullfd
= open (_PATH_DEVNULL
, O_RDWR
);
173 if (fstat64 (nullfd
, &st
) == 0 && S_ISCHR (st
.st_mode
) != 0
174 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
175 && st
.st_rdev
== makedev (DEV_NULL_MAJOR
, DEV_NULL_MINOR
)
179 /* It is the /dev/null special device alright. */
180 (void) dup2 (nullfd
, STDIN_FILENO
);
181 (void) dup2 (nullfd
, STDOUT_FILENO
);
182 (void) dup2 (nullfd
, STDERR_FILENO
);
189 /* Ugh, somebody is trying to play a trick on us. */
194 int min_close_fd
= nullfd
== -1 ? 0 : STDERR_FILENO
+ 1;
196 DIR *d
= opendir ("/proc/self/fd");
199 struct dirent64
*dirent
;
200 int dfdn
= dirfd (d
);
202 while ((dirent
= readdir64 (d
)) != NULL
)
205 long int fdn
= strtol (dirent
->d_name
, &endp
, 10);
207 if (*endp
== '\0' && fdn
!= dfdn
&& fdn
>= min_close_fd
)
214 for (i
= min_close_fd
; i
< getdtablesize (); i
++)
219 error (EXIT_FAILURE
, errno
, _("cannot fork"));
227 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
229 if (write_pid (_PATH_NSCDPID
) < 0)
230 dbg_log ("%s: %s", _PATH_NSCDPID
, strerror (errno
));
232 if (!init_logfile ())
233 dbg_log (_("Could not create log file"));
235 /* Ignore job control signals. */
236 signal (SIGTTOU
, SIG_IGN
);
237 signal (SIGTTIN
, SIG_IGN
);
238 signal (SIGTSTP
, SIG_IGN
);
241 signal (SIGINT
, termination_handler
);
242 signal (SIGQUIT
, termination_handler
);
243 signal (SIGTERM
, termination_handler
);
244 signal (SIGPIPE
, SIG_IGN
);
246 /* Cleanup files created by a previous 'bind'. */
247 unlink (_PATH_NSCDSOCKET
);
249 /* Make sure we do not get recursive calls. */
250 __nss_disable_nscd ();
252 /* Init databases. */
255 /* Handle incoming requests */
262 /* Handle program arguments. */
264 parse_opt (int key
, char *arg
, struct argp_state
*state
)
279 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
281 int sock
= nscd_open_socket ();
288 req
.version
= NSCD_VERSION
;
291 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
292 sizeof (request_header
)));
294 exit (nbytes
!= sizeof (request_header
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
303 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
306 int sock
= nscd_open_socket ();
313 if (strcmp (arg
, "passwd") == 0)
314 req
.key_len
= sizeof "passwd";
315 else if (strcmp (arg
, "group") == 0)
316 req
.key_len
= sizeof "group";
317 else if (strcmp (arg
, "hosts") == 0)
318 req
.key_len
= sizeof "hosts";
320 return ARGP_ERR_UNKNOWN
;
322 req
.version
= NSCD_VERSION
;
323 req
.type
= INVALIDATE
;
324 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
325 sizeof (request_header
)));
326 if (nbytes
!= sizeof (request_header
))
332 nbytes
= TEMP_FAILURE_RETRY (write (sock
, (void *)arg
, req
.key_len
));
336 exit (nbytes
!= req
.key_len
? EXIT_FAILURE
: EXIT_SUCCESS
);
340 nthreads
= atol (arg
);
344 if (strcmp (arg
, "passwd,yes") == 0)
345 secure_in_use
= secure
[pwddb
] = 1;
346 else if (strcmp (arg
, "group,yes") == 0)
347 secure_in_use
= secure
[grpdb
] = 1;
348 else if (strcmp (arg
, "hosts,yes") == 0)
349 secure_in_use
= secure
[hstdb
] = 1;
353 return ARGP_ERR_UNKNOWN
;
359 /* Print the version information. */
361 print_version (FILE *stream
, struct argp_state
*state
)
363 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
364 fprintf (stream
, gettext ("\
365 Copyright (C) %s Free Software Foundation, Inc.\n\
366 This is free software; see the source for copying conditions. There is NO\n\
367 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
369 fprintf (stream
, gettext ("Written by %s.\n"),
370 "Thorsten Kukuk and Ulrich Drepper");
374 /* Create a socket connected to a name. */
376 nscd_open_socket (void)
378 struct sockaddr_un addr
;
381 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
385 addr
.sun_family
= AF_UNIX
;
386 assert (sizeof (addr
.sun_path
) >= sizeof (_PATH_NSCDSOCKET
));
387 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
388 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
399 termination_handler (int signum
)
403 /* Clean up the file created by 'bind'. */
404 unlink (_PATH_NSCDSOCKET
);
406 /* Clean up pid file. */
407 unlink (_PATH_NSCDPID
);
409 _exit (EXIT_SUCCESS
);
412 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
414 check_pid (const char *file
)
418 fp
= fopen (file
, "r");
424 n
= fscanf (fp
, "%d", &pid
);
427 if (n
!= 1 || kill (pid
, 0) == 0)
434 /* Write the current process id to the file FILE.
435 Returns 0 if successful, -1 if not. */
437 write_pid (const char *file
)
441 fp
= fopen (file
, "w");
445 fprintf (fp
, "%d\n", getpid ());
446 if (fflush (fp
) || ferror (fp
))