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. */
35 #include <sys/socket.h>
41 /* Get libc version number. */
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". */
54 pthread_cond_t thread_exit_cv;
55 pthread_mutex_t mutex;
58 thread_info_t thread_info;
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,
100 main (int argc, char **argv)
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);
119 signal (SIGINT, termination_handler);
120 signal (SIGQUIT, termination_handler);
121 signal (SIGTERM, termination_handler);
122 signal (SIGPIPE, SIG_IGN);
124 /* Check if we are already running. */
125 if (check_pid (_PATH_NSCDPID))
127 fputs (_("already running"), stderr);
131 /* Behave like a daemon. */
134 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
136 if (daemon (0, 0) < 0)
138 fprintf (stderr, _("connot auto-background: %s\n"),
142 if (write_pid (_PATH_NSCDPID) < 0)
143 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
145 /* Ignore job control signals */
146 signal (SIGTTOU, SIG_IGN);
147 signal (SIGTTIN, SIG_IGN);
148 signal (SIGTSTP, SIG_IGN);
150 /* Cleanup files created by a previous `bind' */
151 unlink (_PATH_NSCDSOCKET);
153 nscd_parse_file (conffile);
155 /* Create first sockets */
158 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
160 fputs (_("Not enough memory\n"), stderr);
163 /* Handle incoming requests */
170 /* Handle program arguments. */
172 parse_opt (int key, char *arg, struct argp_state *state)
186 printf (_("Only root is allowed to use this option!\n\n"));
190 int sock = __nscd_open_socket ();
197 req.version = NSCD_VERSION;
200 nbytes = write (sock, &req, sizeof (request_header));
202 if (nbytes != req.key_len)
211 return ARGP_ERR_UNKNOWN;
216 /* Print the version information. */
218 print_version (FILE *stream, struct argp_state *state)
220 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
221 fprintf (stream, gettext ("\
222 Copyright (C) %s Free Software Foundation, Inc.\n\
223 This is free software; see the source for copying conditions. There is NO\n\
224 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
226 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
230 /* Create a socket connected to a name. */
232 __nscd_open_socket (void)
234 struct sockaddr_un addr;
237 sock = socket (PF_UNIX, SOCK_STREAM, 0);
241 addr.sun_family = AF_UNIX;
242 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
243 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
254 termination_handler (int signum)
258 /* Clean up the files created by `bind'. */
259 unlink (_PATH_NSCDSOCKET);
261 /* Clean up pid file. */
262 unlink (_PATH_NSCDPID);
267 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
269 check_pid (const char *file)
273 fp = fopen (file, "r");
278 fscanf (fp, "%d", &pid);
281 if (kill (pid, 0) == 0)
288 /* Write the current process id to the file FILE.
289 Returns 0 if successful, -1 if not. */
291 write_pid (const char *file)
295 fp = fopen (file, "w");
299 fprintf (fp, "%d\n", getpid ());
308 /* Type of the lookup function for netname2user. */
309 typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
310 char *buffer, size_t buflen);
312 /* Handle incoming requests. */
314 void handle_requests (void)
317 int conn; /* Handle on which connection (client) the request came from. */
320 pthread_attr_t th_attr;
322 /* We will create all threads detached. Therefore prepare an attribute
324 pthread_attr_init (&th_attr);
325 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
330 get_request (&conn, &req, &key);
332 dbg_log (_("handle_requests: request received (Version = %d)"),
338 param_t *param = malloc (sizeof (param_t));
343 dbg_log ("\tGETPWBYNAME (%s)", key);
347 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
350 status = pthread_create (&thread, &th_attr, cache_getpwnam,
354 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
357 pthread_detach (thread);
362 param_t *param = malloc (sizeof (param_t));
367 dbg_log ("\tGETPWBYUID (%s)", key);
371 status = pthread_create (&thread, &th_attr, cache_pw_disabled,
374 status = pthread_create (&thread, &th_attr, cache_getpwuid,
378 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
385 param_t *param = malloc (sizeof (param_t));
390 dbg_log ("\tGETGRBYNAME (%s)", key);
394 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
397 status = pthread_create (&thread, &th_attr, cache_getgrnam,
401 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
408 param_t *param = malloc (sizeof (param_t));
413 dbg_log ("\tGETGRBYGID (%s)", key);
417 status = pthread_create (&thread, &th_attr, cache_gr_disabled,
420 status = pthread_create (&thread, &th_attr, cache_getgrgid,
424 dbg_log (_("Creation of thread failed: %s"), strerror (errno));
430 /* Not yetimplemented. */
434 /* Not yet implemented. */
441 /* Clean up the files created by `bind'. */
442 unlink (_PATH_NSCDSOCKET);
443 /* Clean up pid file. */
444 unlink (_PATH_NSCDPID);
449 stat_response_header resp;
452 dbg_log ("\tGETSTAT");
456 resp.debug_level = debug_flag;
457 resp.pw_enabled = !disabled_passwd;
458 resp.gr_enabled = !disabled_group;
460 stat_send (conn, &resp);
466 dbg_log (_("Unknown request (%d)"), req.type);
471 pthread_attr_destroy (&th_attr);