Update.
[glibc.git] / nscd / nscd.c
blobc6fb29ee9ebdec1ac3270ce2cc32764ccfdc57fb
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 <sys/socket.h>
35 #include <sys/un.h>
37 #include "dbg_log.h"
38 #include "nscd.h"
40 /* Get libc version number. */
41 #include <version.h>
43 #define PACKAGE _libc_intl_domainname
45 /* Structure used by main() thread to keep track of the number of
46 active threads. Used to limit how many threads it will create
47 and under a shutdown condition to wait till all in-progress
48 requests have finished before "turning off the lights". */
50 typedef struct
52 int num_active;
53 pthread_cond_t thread_exit_cv;
54 pthread_mutex_t mutex;
55 } thread_info_t;
57 thread_info_t thread_info;
59 int do_shutdown = 0;
60 int disabled_passwd = 0;
61 int disabled_group = 0;
62 int go_background = 1;
63 const char *conffile = _PATH_NSCDCONF;
65 static void termination_handler (int signum);
66 static int check_pid (const char *file);
67 static int write_pid (const char *file);
68 static void handle_requests (void);
70 /* Name and version of program. */
71 static void print_version (FILE *stream, struct argp_state *state);
72 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
74 /* Definitions of arguments for argp functions. */
75 static const struct argp_option options[] =
77 { "config-file", 'f', N_("NAME"), 0,
78 N_("Read configuration data from NAME") },
79 { "debug", 'd', NULL, 0,
80 N_("Do not fork and display messages on the current tty") },
81 { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
82 { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
83 { NULL, 0, NULL, 0, NULL }
86 /* Short description of program. */
87 static const char doc[] = N_("Name Switch Cache Daemon.");
89 /* Prototype for option handler. */
90 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
92 /* Data structure to communicate with argp functions. */
93 static struct argp argp =
95 options, parse_opt, NULL, doc,
98 int
99 main (int argc, char **argv)
101 int remaining;
103 /* Set locale via LC_ALL. */
104 setlocale (LC_ALL, "");
105 /* Set the text message domain. */
106 textdomain (PACKAGE);
108 /* Parse and process arguments. */
109 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
111 if (remaining != argc)
113 error (0, 0, gettext ("wrong number of arguments"));
114 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
115 exit (EXIT_FAILURE);
118 signal (SIGINT, termination_handler);
119 signal (SIGQUIT, termination_handler);
120 signal (SIGTERM, termination_handler);
121 signal (SIGPIPE, SIG_IGN);
123 /* Check if we are already running. */
124 if (check_pid (_PATH_NSCDPID))
126 fputs (_("already running"), stderr);
127 exit (EXIT_FAILURE);
130 /* Behave like a daemon. */
131 if (go_background)
133 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
135 if (daemon (0, 0) < 0)
137 fprintf (stderr, _("connot auto-background: %s\n"),
138 strerror (errno));
139 exit (EXIT_FAILURE);
141 if (write_pid (_PATH_NSCDPID) < 0)
142 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
144 /* Ignore job control signals */
145 signal (SIGTTOU, SIG_IGN);
146 signal (SIGTTIN, SIG_IGN);
147 signal (SIGTSTP, SIG_IGN);
149 /* Cleanup files created by a previous `bind' */
150 unlink (_PATH_NSCDSOCKET);
152 nscd_parse_file (conffile);
154 /* Create first sockets */
155 init_sockets ();
156 /* Init databases */
157 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
159 fputs (_("Not enough memory\n"), stderr);
160 return 1;
162 /* Handle incoming requests */
163 handle_requests ();
165 return 0;
169 /* Handle program arguments. */
170 static error_t
171 parse_opt (int key, char *arg, struct argp_state *state)
173 switch (key)
175 case 'd':
176 debug_flag = 1;
177 go_background = 0;
178 break;
179 case 'f':
180 conffile = arg;
181 break;
182 case 'K':
183 if (getuid () != 0)
185 printf (_("Only root is allowed to use this option!\n\n"));
186 exit (EXIT_FAILURE);
189 int sock = __nscd_open_socket ();
190 request_header req;
191 ssize_t nbytes;
193 if (sock == -1)
194 exit (EXIT_FAILURE);
196 req.version = NSCD_VERSION;
197 req.type = SHUTDOWN;
198 req.key_len = 0;
199 nbytes = write (sock, &req, sizeof (request_header));
200 close (sock);
201 if (nbytes != req.key_len)
202 exit (EXIT_FAILURE);
203 else
204 exit (EXIT_SUCCESS);
206 case 'g':
207 print_stat ();
208 exit (EXIT_SUCCESS);
209 default:
210 return ARGP_ERR_UNKNOWN;
212 return 0;
215 /* Print the version information. */
216 static void
217 print_version (FILE *stream, struct argp_state *state)
219 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
220 fprintf (stream, gettext ("\
221 Copyright (C) %s Free Software Foundation, Inc.\n\
222 This is free software; see the source for copying conditions. There is NO\n\
223 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
224 "), "1998");
225 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
229 /* Create a socket connected to a name. */
231 __nscd_open_socket (void)
233 struct sockaddr_un addr;
234 int sock;
236 sock = socket (PF_UNIX, SOCK_STREAM, 0);
237 if (sock < 0)
238 return -1;
240 addr.sun_family = AF_UNIX;
241 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
242 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
244 close (sock);
245 return -1;
248 return sock;
251 /* Cleanup. */
252 static void
253 termination_handler (int signum)
255 close_sockets ();
257 /* Clean up the files created by `bind'. */
258 unlink (_PATH_NSCDSOCKET);
260 /* Clean up pid file. */
261 unlink (_PATH_NSCDPID);
263 exit (EXIT_SUCCESS);
266 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
267 static int
268 check_pid (const char *file)
270 FILE *fp;
272 fp = fopen (file, "r");
273 if (fp)
275 pid_t pid;
277 fscanf (fp, "%d", &pid);
278 fclose (fp);
280 if (kill (pid, 0) == 0)
281 return 1;
284 return 0;
287 /* Write the current process id to the file FILE.
288 Returns 0 if successful, -1 if not. */
289 static int
290 write_pid (const char *file)
292 FILE *fp;
294 fp = fopen (file, "w");
295 if (fp == NULL)
296 return -1;
298 fprintf (fp, "%d\n", getpid ());
299 if (ferror (fp))
300 return -1;
302 fclose (fp);
304 return 0;
307 /* Type of the lookup function for netname2user. */
308 typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
309 char *buffer, size_t buflen);
311 /* Hanlde incoming requests. */
312 static
313 void handle_requests (void)
315 request_header req;
316 int conn; /* Handle on which connection (client) the request came from. */
317 int done = 0;
318 char *key;
320 while (!done)
322 key = NULL;
323 get_request (&conn, &req, &key);
324 if (debug_flag)
325 dbg_log (_("handle_requests: request received (Version = %d)"),
326 req.version);
327 switch (req.type)
329 case GETPWBYNAME:
331 param_t *param = malloc (sizeof (param_t));
332 pthread_t thread;
334 if (debug_flag)
335 dbg_log ("\tGETPWBYNAME (%s)", key);
336 param->key = key;
337 param->conn = conn;
338 if (disabled_passwd)
339 pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
340 else
341 pthread_create (&thread, NULL, cache_getpwnam, (void *)param);
342 pthread_detach (thread);
344 break;
345 case GETPWBYUID:
347 param_t *param = malloc (sizeof (param_t));
348 pthread_t thread;
350 if (debug_flag)
351 dbg_log ("\tGETPWBYUID (%s)", key);
352 param->key = key;
353 param->conn = conn;
354 if (disabled_passwd)
355 pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
356 else
357 pthread_create (&thread, NULL, cache_getpwuid, (void *)param);
358 pthread_detach (thread);
360 break;
361 case GETGRBYNAME:
363 param_t *param = malloc (sizeof (param_t));
364 pthread_t thread;
366 if (debug_flag)
367 dbg_log ("\tGETGRBYNAME (%s)", key);
368 param->key = key;
369 param->conn = conn;
370 if (disabled_group)
371 pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
372 else
373 pthread_create (&thread, NULL, cache_getgrnam, (void *)param);
374 pthread_detach (thread);
376 break;
377 case GETGRBYGID:
379 param_t *param = malloc (sizeof (param_t));
380 pthread_t thread;
382 if (debug_flag)
383 dbg_log ("\tGETGRBYGID (%s)", key);
384 param->key = key;
385 param->conn = conn;
386 if (disabled_group)
387 pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
388 else
389 pthread_create (&thread, NULL, cache_getgrgid, (void *)param);
390 pthread_detach (thread);
392 break;
393 case GETHOSTBYNAME:
394 /* Not yetimplemented. */
395 close_socket (conn);
396 break;
397 case GETHOSTBYADDR:
398 /* Not yet implemented. */
399 close_socket (conn);
400 break;
401 case SHUTDOWN:
402 do_shutdown = 1;
403 close_socket (0);
404 close_socket (conn);
405 /* Clean up the files created by `bind'. */
406 unlink (_PATH_NSCDSOCKET);
407 /* Clean up pid file. */
408 unlink (_PATH_NSCDPID);
409 done = 1;
410 break;
411 case GETSTAT:
413 stat_response_header resp;
415 if (debug_flag)
416 dbg_log ("\tGETSTAT");
418 get_pw_stat (&resp);
419 get_gr_stat (&resp);
420 resp.debug_level = debug_flag;
421 resp.pw_enabled = !disabled_passwd;
422 resp.gr_enabled = !disabled_group;
424 stat_send (conn, &resp);
426 close_socket (conn);
428 break;
429 default:
430 dbg_log (_("Unknown request (%d)"), req.type);
431 break;