[!__ASSEMBLER__] (declare_symbol_1): Add missing comma to .type directive.
[glibc.git] / nscd / nscd.c
blob045256b07f060d6ea7ddb5eb14b746ec144da612
1 /* Copyright (c) 1998, 1999, 2000, 2001, 2002 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
18 02111-1307 USA. */
20 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
22 #include <argp.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <error.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <unistd.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
38 #include "dbg_log.h"
39 #include "nscd.h"
41 /* Get libc version number. */
42 #include <version.h>
44 #define PACKAGE _libc_intl_domainname
46 /* Structure used by main() thread to keep track of the number of
47 active threads. Used to limit how many threads it will create
48 and under a shutdown condition to wait till all in-progress
49 requests have finished before "turning off the lights". */
51 typedef struct
53 int num_active;
54 pthread_cond_t thread_exit_cv;
55 pthread_mutex_t mutex;
56 } thread_info_t;
58 thread_info_t thread_info;
60 int do_shutdown;
61 int disabled_passwd;
62 int disabled_group;
63 int go_background = 1;
65 int secure[lastdb];
66 int secure_in_use;
67 static const char *conffile = _PATH_NSCDCONF;
69 static int check_pid (const char *file);
70 static int write_pid (const char *file);
72 /* Name and version of program. */
73 static void print_version (FILE *stream, struct argp_state *state);
74 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
76 /* Definitions of arguments for argp functions. */
77 static const struct argp_option options[] =
79 { "config-file", 'f', N_("NAME"), 0,
80 N_("Read configuration data from NAME") },
81 { "debug", 'd', NULL, 0,
82 N_("Do not fork and display messages on the current tty") },
83 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
84 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
85 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
86 { "invalidate", 'i', N_("TABLE"), 0,
87 N_("Invalidate the specified cache") },
88 { "secure", 'S', N_("TABLE,yes"), 0, N_("Use separate cache for each user")},
89 { NULL, 0, NULL, 0, NULL }
92 /* Short description of program. */
93 static const char doc[] = N_("Name Service Cache Daemon.");
95 /* Prototype for option handler. */
96 static error_t parse_opt (int key, char *arg, struct argp_state *state);
98 /* Data structure to communicate with argp functions. */
99 static struct argp argp =
101 options, parse_opt, NULL, doc,
105 main (int argc, char **argv)
107 int remaining;
109 /* Set locale via LC_ALL. */
110 setlocale (LC_ALL, "");
111 /* Set the text message domain. */
112 textdomain (PACKAGE);
114 /* Parse and process arguments. */
115 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
117 if (remaining != argc)
119 error (0, 0, gettext ("wrong number of arguments"));
120 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
121 exit (EXIT_FAILURE);
124 /* Check if we are already running. */
125 if (check_pid (_PATH_NSCDPID))
126 error (EXIT_FAILURE, 0, _("already running"));
128 /* Behave like a daemon. */
129 if (go_background)
131 int i;
133 if (fork ())
134 exit (0);
136 for (i = 0; i < getdtablesize (); i++)
137 close (i);
139 if (fork ())
140 exit (0);
142 setsid ();
144 chdir ("/");
146 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
148 if (write_pid (_PATH_NSCDPID) < 0)
149 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
151 /* Ignore job control signals. */
152 signal (SIGTTOU, SIG_IGN);
153 signal (SIGTTIN, SIG_IGN);
154 signal (SIGTSTP, SIG_IGN);
157 signal (SIGINT, termination_handler);
158 signal (SIGQUIT, termination_handler);
159 signal (SIGTERM, termination_handler);
160 signal (SIGPIPE, SIG_IGN);
162 /* Cleanup files created by a previous `bind'. */
163 unlink (_PATH_NSCDSOCKET);
165 /* Init databases. */
166 nscd_init (conffile);
168 /* Handle incoming requests */
169 start_threads ();
171 return 0;
175 /* Handle program arguments. */
176 static error_t
177 parse_opt (int key, char *arg, struct argp_state *state)
179 switch (key)
181 case 'd':
182 ++debug_level;
183 go_background = 0;
184 break;
186 case 'f':
187 conffile = arg;
188 break;
190 case 'K':
191 if (getuid () != 0)
192 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
194 int sock = nscd_open_socket ();
195 request_header req;
196 ssize_t nbytes;
198 if (sock == -1)
199 exit (EXIT_FAILURE);
201 req.version = NSCD_VERSION;
202 req.type = SHUTDOWN;
203 req.key_len = 0;
204 nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
205 sizeof (request_header)));
206 close (sock);
207 exit (nbytes != sizeof (request_header) ? EXIT_FAILURE : EXIT_SUCCESS);
210 case 'g':
211 if (getuid () != 0)
212 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
213 receive_print_stats ();
214 /* Does not return. */
216 case 'i':
217 if (getuid () != 0)
218 error (EXIT_FAILURE, 0, _("Only root is allowed to use this option!"));
219 else
221 int sock = nscd_open_socket ();
222 request_header req;
223 ssize_t nbytes;
225 if (sock == -1)
226 exit (EXIT_FAILURE);
228 if (strcmp (arg, "passwd") == 0)
229 req.key_len = sizeof "passwd";
230 else if (strcmp (arg, "group") == 0)
231 req.key_len = sizeof "group";
232 else if (strcmp (arg, "hosts") == 0)
233 req.key_len = sizeof "hosts";
234 else
235 return ARGP_ERR_UNKNOWN;
237 req.version = NSCD_VERSION;
238 req.type = INVALIDATE;
239 nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
240 sizeof (request_header)));
241 if (nbytes != sizeof (request_header))
243 close (sock);
244 exit (EXIT_FAILURE);
247 nbytes = TEMP_FAILURE_RETRY (write (sock, (void *)arg, req.key_len));
249 close (sock);
251 exit (nbytes != req.key_len ? EXIT_FAILURE : EXIT_SUCCESS);
254 case 't':
255 nthreads = atol (arg);
256 break;
258 case 'S':
259 if (strcmp (arg, "passwd,yes") == 0)
260 secure_in_use = secure[pwddb] = 1;
261 else if (strcmp (arg, "group,yes") == 0)
262 secure_in_use = secure[grpdb] = 1;
263 else if (strcmp (arg, "hosts,yes") == 0)
264 secure_in_use = secure[hstdb] = 1;
265 break;
267 default:
268 return ARGP_ERR_UNKNOWN;
271 return 0;
274 /* Print the version information. */
275 static void
276 print_version (FILE *stream, struct argp_state *state)
278 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
279 fprintf (stream, gettext ("\
280 Copyright (C) %s Free Software Foundation, Inc.\n\
281 This is free software; see the source for copying conditions. There is NO\n\
282 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
283 "), "2002");
284 fprintf (stream, gettext ("Written by %s.\n"),
285 "Thorsten Kukuk and Ulrich Drepper");
289 /* Create a socket connected to a name. */
291 nscd_open_socket (void)
293 struct sockaddr_un addr;
294 int sock;
296 sock = socket (PF_UNIX, SOCK_STREAM, 0);
297 if (sock < 0)
298 return -1;
300 addr.sun_family = AF_UNIX;
301 assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
302 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
303 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
305 close (sock);
306 return -1;
309 return sock;
312 /* Cleanup. */
313 void
314 termination_handler (int signum)
316 close_sockets ();
318 /* Clean up the file created by `bind'. */
319 unlink (_PATH_NSCDSOCKET);
321 /* Clean up pid file. */
322 unlink (_PATH_NSCDPID);
324 exit (EXIT_SUCCESS);
327 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
328 static int
329 check_pid (const char *file)
331 FILE *fp;
333 fp = fopen (file, "r");
334 if (fp)
336 pid_t pid;
337 int n;
339 n = fscanf (fp, "%d", &pid);
340 fclose (fp);
342 if (n != 1 || kill (pid, 0) == 0)
343 return 1;
346 return 0;
349 /* Write the current process id to the file FILE.
350 Returns 0 if successful, -1 if not. */
351 static int
352 write_pid (const char *file)
354 FILE *fp;
356 fp = fopen (file, "w");
357 if (fp == NULL)
358 return -1;
360 fprintf (fp, "%d\n", getpid ());
361 if (fflush (fp) || ferror (fp))
362 return -1;
364 fclose (fp);
366 return 0;