Update.
[glibc.git] / nscd / nscd.c
blob9ddbb5f54ef6a9f210e0835cc7bdc06a4113b5cb
1 /* Copyright (c) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* nscd - Name Service Cache Daemon. Caches passwd and group. */
22 #include <argp.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <libintl.h>
26 #include <locale.h>
27 #include <pthread.h>
28 #include <pwd.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 = 0;
61 int disabled_passwd = 0;
62 int disabled_group = 0;
63 int go_background = 1;
64 const char *conffile = _PATH_NSCDCONF;
66 static void termination_handler (int signum);
67 static int check_pid (const char *file);
68 static int write_pid (const char *file);
69 static void handle_requests (void);
71 /* Name and version of program. */
72 static void print_version (FILE *stream, struct argp_state *state);
73 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
75 /* Definitions of arguments for argp functions. */
76 static const struct argp_option options[] =
78 { "config-file", 'f', N_("NAME"), 0,
79 N_("Read configuration data from NAME") },
80 { "debug", 'd', NULL, 0,
81 N_("Do not fork and display messages on the current tty") },
82 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
83 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
84 { NULL, 0, NULL, 0, NULL }
87 /* Short description of program. */
88 static const char doc[] = N_("Name Switch Cache Daemon.");
90 /* Prototype for option handler. */
91 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
93 /* Data structure to communicate with argp functions. */
94 static struct argp argp =
96 options, parse_opt, NULL, doc,
99 int
100 main (int argc, char **argv)
102 int remaining;
104 /* Set locale via LC_ALL. */
105 setlocale (LC_ALL, "");
106 /* Set the text message domain. */
107 textdomain (PACKAGE);
109 /* Parse and process arguments. */
110 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
112 if (remaining != argc)
114 error (0, 0, gettext ("wrong number of arguments"));
115 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
116 exit (EXIT_FAILURE);
119 /* Check if we are already running. */
120 if (check_pid (_PATH_NSCDPID))
122 fputs (_("already running"), stderr);
123 exit (EXIT_FAILURE);
126 /* Behave like a daemon. */
127 if (go_background)
129 int i;
131 if (fork ())
132 exit (0);
134 for (i = 0; i < getdtablesize (); i++)
135 close (i);
137 if (fork ())
138 exit (0);
140 chdir ("/");
142 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
144 if (write_pid (_PATH_NSCDPID) < 0)
145 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
147 /* Ignore job control signals */
148 signal (SIGTTOU, SIG_IGN);
149 signal (SIGTTIN, SIG_IGN);
150 signal (SIGTSTP, SIG_IGN);
153 signal (SIGINT, termination_handler);
154 signal (SIGQUIT, termination_handler);
155 signal (SIGTERM, termination_handler);
156 signal (SIGPIPE, SIG_IGN);
158 /* Cleanup files created by a previous `bind' */
159 unlink (_PATH_NSCDSOCKET);
161 nscd_parse_file (conffile);
163 /* Create first sockets */
164 init_sockets ();
165 /* Init databases */
166 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
168 fputs (_("Not enough memory\n"), stderr);
169 return 1;
171 /* Handle incoming requests */
172 handle_requests ();
174 return 0;
178 /* Handle program arguments. */
179 static error_t
180 parse_opt (int key, char *arg, struct argp_state *state)
182 switch (key)
184 case 'd':
185 debug_flag = 1;
186 go_background = 0;
187 break;
188 case 'f':
189 conffile = arg;
190 break;
191 case 'K':
192 if (getuid () != 0)
194 printf (_("Only root is allowed to use this option!\n\n"));
195 exit (EXIT_FAILURE);
198 int sock = __nscd_open_socket ();
199 request_header req;
200 ssize_t nbytes;
202 if (sock == -1)
203 exit (EXIT_FAILURE);
205 req.version = NSCD_VERSION;
206 req.type = SHUTDOWN;
207 req.key_len = 0;
208 nbytes = write (sock, &req, sizeof (request_header));
209 close (sock);
210 if (nbytes != req.key_len)
211 exit (EXIT_FAILURE);
212 else
213 exit (EXIT_SUCCESS);
215 case 'g':
216 print_stat ();
217 exit (EXIT_SUCCESS);
218 default:
219 return ARGP_ERR_UNKNOWN;
221 return 0;
224 /* Print the version information. */
225 static void
226 print_version (FILE *stream, struct argp_state *state)
228 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
229 fprintf (stream, gettext ("\
230 Copyright (C) %s Free Software Foundation, Inc.\n\
231 This is free software; see the source for copying conditions. There is NO\n\
232 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
233 "), "1998");
234 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
238 /* Create a socket connected to a name. */
240 __nscd_open_socket (void)
242 struct sockaddr_un addr;
243 int sock;
245 sock = socket (PF_UNIX, SOCK_STREAM, 0);
246 if (sock < 0)
247 return -1;
249 addr.sun_family = AF_UNIX;
250 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
251 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
253 close (sock);
254 return -1;
257 return sock;
260 /* Cleanup. */
261 static void
262 termination_handler (int signum)
264 close_sockets ();
266 /* Clean up the files created by `bind'. */
267 unlink (_PATH_NSCDSOCKET);
269 /* Clean up pid file. */
270 unlink (_PATH_NSCDPID);
272 exit (EXIT_SUCCESS);
275 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
276 static int
277 check_pid (const char *file)
279 FILE *fp;
281 fp = fopen (file, "r");
282 if (fp)
284 pid_t pid;
286 fscanf (fp, "%d", &pid);
287 fclose (fp);
289 if (kill (pid, 0) == 0)
290 return 1;
293 return 0;
296 /* Write the current process id to the file FILE.
297 Returns 0 if successful, -1 if not. */
298 static int
299 write_pid (const char *file)
301 FILE *fp;
303 fp = fopen (file, "w");
304 if (fp == NULL)
305 return -1;
307 fprintf (fp, "%d\n", getpid ());
308 if (ferror (fp))
309 return -1;
311 fclose (fp);
313 return 0;
316 /* Type of the lookup function for netname2user. */
317 typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
318 char *buffer, size_t buflen);
320 /* Handle incoming requests. */
321 static
322 void handle_requests (void)
324 request_header req;
325 int conn; /* Handle on which connection (client) the request came from. */
326 int done = 0;
327 char *key;
328 pthread_attr_t th_attr;
330 /* We will create all threads detached. Therefore prepare an attribute
331 now. */
332 pthread_attr_init (&th_attr);
333 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
335 while (!done)
337 key = NULL;
338 get_request (&conn, &req, &key);
339 if (debug_flag)
340 dbg_log (_("handle_requests: request received (Version = %d)"),
341 req.version);
342 switch (req.type)
344 case GETPWBYNAME:
346 param_t *param = malloc (sizeof (param_t));
347 pthread_t thread;
348 int status;
350 if (debug_flag)
351 dbg_log ("\tGETPWBYNAME (%s)", key);
352 param->key = key;
353 param->conn = conn;
354 if (disabled_passwd)
355 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
356 (void *)param);
357 else
358 status = pthread_create (&thread, &th_attr, cache_getpwnam,
359 (void *)param);
360 if (status != 0)
362 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
363 close_socket (conn);
365 pthread_detach (thread);
367 break;
368 case GETPWBYUID:
370 param_t *param = malloc (sizeof (param_t));
371 pthread_t thread;
372 int status;
374 if (debug_flag)
375 dbg_log ("\tGETPWBYUID (%s)", key);
376 param->key = key;
377 param->conn = conn;
378 if (disabled_passwd)
379 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
380 (void *)param);
381 else
382 status = pthread_create (&thread, &th_attr, cache_getpwuid,
383 (void *)param);
384 if (status != 0)
386 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
387 close_socket (conn);
390 break;
391 case GETGRBYNAME:
393 param_t *param = malloc (sizeof (param_t));
394 pthread_t thread;
395 int status;
397 if (debug_flag)
398 dbg_log ("\tGETGRBYNAME (%s)", key);
399 param->key = key;
400 param->conn = conn;
401 if (disabled_group)
402 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
403 (void *)param);
404 else
405 status = pthread_create (&thread, &th_attr, cache_getgrnam,
406 (void *)param);
407 if (status != 0)
409 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
410 close_socket (conn);
413 break;
414 case GETGRBYGID:
416 param_t *param = malloc (sizeof (param_t));
417 pthread_t thread;
418 int status;
420 if (debug_flag)
421 dbg_log ("\tGETGRBYGID (%s)", key);
422 param->key = key;
423 param->conn = conn;
424 if (disabled_group)
425 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
426 (void *)param);
427 else
428 status = pthread_create (&thread, &th_attr, cache_getgrgid,
429 (void *)param);
430 if (status != 0)
432 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
433 close_socket (conn);
436 break;
437 case GETHOSTBYNAME:
438 /* Not yetimplemented. */
439 close_socket (conn);
440 break;
441 case GETHOSTBYADDR:
442 /* Not yet implemented. */
443 close_socket (conn);
444 break;
445 case SHUTDOWN:
446 do_shutdown = 1;
447 close_socket (0);
448 close_socket (conn);
449 /* Clean up the files created by `bind'. */
450 unlink (_PATH_NSCDSOCKET);
451 /* Clean up pid file. */
452 unlink (_PATH_NSCDPID);
453 done = 1;
454 break;
455 case GETSTAT:
457 stat_response_header resp;
459 if (debug_flag)
460 dbg_log ("\tGETSTAT");
462 get_pw_stat (&resp);
463 get_gr_stat (&resp);
464 resp.debug_level = debug_flag;
465 resp.pw_enabled = !disabled_passwd;
466 resp.gr_enabled = !disabled_group;
468 stat_send (conn, &resp);
470 close_socket (conn);
472 break;
473 default:
474 dbg_log (_("Unknown request (%d)"), req.type);
475 break;
479 pthread_attr_destroy (&th_attr);