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. */
34 #include <sys/socket.h>
40 /* Get libc version number. */
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". */
53 pthread_cond_t thread_exit_cv;
54 pthread_mutex_t mutex;
57 thread_info_t thread_info;
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 { NULL, 0, NULL, 0, NULL }
85 /* Short description of program. */
86 static const char doc[] = N_("Name Switch Cache Daemon.");
88 /* Prototype for option handler. */
89 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
91 /* Data structure to communicate with argp functions. */
92 static struct argp argp =
94 options, parse_opt, NULL, doc,
98 main (int argc, char **argv)
102 /* Set locale via LC_ALL. */
103 setlocale (LC_ALL, "");
104 /* Set the text message domain. */
105 textdomain (PACKAGE);
107 /* Parse and process arguments. */
108 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
110 if (remaining != argc)
112 error (0, 0, gettext ("wrong number of arguments"));
113 argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
117 signal (SIGINT, termination_handler);
118 signal (SIGQUIT, termination_handler);
119 signal (SIGTERM, termination_handler);
121 /* Check if we are already running. */
122 if (check_pid (_PATH_NSCDPID))
124 fputs (_("already running"), stderr);
128 /* Behave like a daemon. */
131 openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
133 if (daemon (0, 0) < 0)
135 fprintf (stderr, _("connot auto-background: %s\n"),
139 if (write_pid (_PATH_NSCDPID) < 0)
140 dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
142 /* Ignore job control signals */
143 signal (SIGTTOU, SIG_IGN);
144 signal (SIGTTIN, SIG_IGN);
145 signal (SIGTSTP, SIG_IGN);
147 /* Cleanup files created by a previous `bind' */
148 unlink (_PATH_NSCDSOCKET);
150 nscd_parse_file (conffile);
152 /* Create first sockets */
155 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
157 fputs (_("Not enough memory\n"), stderr);
160 /* Handle incoming requests */
167 /* Handle program arguments. */
169 parse_opt (int key, char *arg, struct argp_state *state)
183 printf (_("Only root is allowed to use this option!\n\n"));
187 int sock = __nscd_open_socket ();
194 req.version = NSCD_VERSION;
197 nbytes = write (sock, &req, sizeof (request_header));
199 if (nbytes != req.key_len)
208 return ARGP_ERR_UNKNOWN;
213 /* Print the version information. */
215 print_version (FILE *stream, struct argp_state *state)
217 fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
218 fprintf (stream, gettext ("\
219 Copyright (C) %s Free Software Foundation, Inc.\n\
220 This is free software; see the source for copying conditions. There is NO\n\
221 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
223 fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
227 /* Create a socket connected to a name. */
229 __nscd_open_socket (void)
231 struct sockaddr_un addr;
234 sock = socket (PF_UNIX, SOCK_STREAM, 0);
238 addr.sun_family = AF_UNIX;
239 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
240 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
251 termination_handler (int signum)
255 /* Clean up the files created by `bind'. */
256 unlink (_PATH_NSCDSOCKET);
258 /* Clean up pid file. */
259 unlink (_PATH_NSCDPID);
264 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
266 check_pid (const char *file)
270 fp = fopen (file, "r");
275 fscanf (fp, "%d", &pid);
278 if (kill (pid, 0) == 0)
285 /* Write the current process id to the file FILE.
286 Returns 0 if successful, -1 if not. */
288 write_pid (const char *file)
292 fp = fopen (file, "w");
296 fprintf (fp, "%d\n", getpid ());
305 /* Type of the lookup function for netname2user. */
306 typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
307 char *buffer, size_t buflen);
309 /* Hanlde incoming requests. */
311 void handle_requests (void)
314 int conn; /* Handle on which connection (client) the request came from. */
321 get_request (&conn, &req, &key);
323 dbg_log (_("handle_requests: request received (Version = %d)"),
329 param_t *param = malloc (sizeof (param_t));
333 dbg_log ("\tGETPWBYNAME (%s)", key);
337 pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
339 pthread_create (&thread, NULL, cache_getpwnam, (void *)param);
340 pthread_detach (thread);
345 param_t *param = malloc (sizeof (param_t));
349 dbg_log ("\tGETPWBYUID (%s)", key);
353 pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
355 pthread_create (&thread, NULL, cache_getpwuid, (void *)param);
356 pthread_detach (thread);
361 param_t *param = malloc (sizeof (param_t));
365 dbg_log ("\tGETGRBYNAME (%s)", key);
369 pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
371 pthread_create (&thread, NULL, cache_getgrnam, (void *)param);
372 pthread_detach (thread);
377 param_t *param = malloc (sizeof (param_t));
381 dbg_log ("\tGETGRBYGID (%s)", key);
385 pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
387 pthread_create (&thread, NULL, cache_getgrgid, (void *)param);
388 pthread_detach (thread);
392 /* Not yetimplemented. */
396 /* Not yet implemented. */
403 /* Clean up the files created by `bind'. */
404 unlink (_PATH_NSCDSOCKET);
405 /* Clean up pid file. */
406 unlink (_PATH_NSCDPID);
411 stat_response_header resp;
414 dbg_log ("\tGETSTAT");
418 resp.debug_level = debug_flag;
419 resp.pw_enabled = !disabled_passwd;
420 resp.gr_enabled = !disabled_group;
422 stat_send (conn, &resp);
428 dbg_log (_("Unknown request (%d)"), req.type);