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
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. */
165 int nullfd
= open (_PATH_DEVNULL
, O_RDWR
);
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
)
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
);
186 /* Ugh, somebody is trying to play a trick on us. */
191 int min_close_fd
= nullfd
== -1 ? 0 : STDERR_FILENO
+ 1;
193 DIR *d
= opendir ("/proc/self/fd");
196 struct dirent64
*dirent
;
197 int dfdn
= dirfd (d
);
199 while ((dirent
= readdir64 (d
)) != NULL
)
202 long int fdn
= strtol (dirent
->d_name
, &endp
, 10);
204 if (*endp
== '\0' && fdn
!= dfdn
&& fdn
>= min_close_fd
)
211 for (i
= min_close_fd
; i
< getdtablesize (); i
++)
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. */
246 /* Handle incoming requests */
253 /* Handle program arguments. */
255 parse_opt (int key
, char *arg
, struct argp_state
*state
)
270 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
272 int sock
= nscd_open_socket ();
279 req
.version
= NSCD_VERSION
;
282 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
283 sizeof (request_header
)));
285 exit (nbytes
!= sizeof (request_header
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
294 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
297 int sock
= nscd_open_socket ();
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";
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
))
323 nbytes
= TEMP_FAILURE_RETRY (write (sock
, (void *)arg
, req
.key_len
));
327 exit (nbytes
!= req
.key_len
? EXIT_FAILURE
: EXIT_SUCCESS
);
331 nthreads
= atol (arg
);
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;
344 return ARGP_ERR_UNKNOWN
;
350 /* Print the version information. */
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\
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
;
372 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
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)
390 termination_handler (int signum
)
394 /* Clean up the file created by `bind'. */
395 unlink (_PATH_NSCDSOCKET
);
397 /* Clean up pid file. */
398 unlink (_PATH_NSCDPID
);
403 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
405 check_pid (const char *file
)
409 fp
= fopen (file
, "r");
415 n
= fscanf (fp
, "%d", &pid
);
418 if (n
!= 1 || kill (pid
, 0) == 0)
425 /* Write the current process id to the file FILE.
426 Returns 0 if successful, -1 if not. */
428 write_pid (const char *file
)
432 fp
= fopen (file
, "w");
436 fprintf (fp
, "%d\n", getpid ());
437 if (fflush (fp
) || ferror (fp
))