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 { "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
,
99 main (int argc
, char **argv
)
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
);
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
);
130 /* Behave like a daemon. */
133 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
135 if (daemon (0, 0) < 0)
137 fprintf (stderr
, _("connot auto-background: %s\n"),
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 */
157 if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
159 fputs (_("Not enough memory\n"), stderr
);
162 /* Handle incoming requests */
169 /* Handle program arguments. */
171 parse_opt (int key
, char *arg
, struct argp_state
*state
)
185 printf (_("Only root is allowed to use this option!\n\n"));
189 int sock
= __nscd_open_socket ();
196 req
.version
= NSCD_VERSION
;
199 nbytes
= write (sock
, &req
, sizeof (request_header
));
201 if (nbytes
!= req
.key_len
)
210 return ARGP_ERR_UNKNOWN
;
215 /* Print the version information. */
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\
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
;
236 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
240 addr
.sun_family
= AF_UNIX
;
241 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
242 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
253 termination_handler (int signum
)
257 /* Clean up the files created by `bind'. */
258 unlink (_PATH_NSCDSOCKET
);
260 /* Clean up pid file. */
261 unlink (_PATH_NSCDPID
);
266 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
268 check_pid (const char *file
)
272 fp
= fopen (file
, "r");
277 fscanf (fp
, "%d", &pid
);
280 if (kill (pid
, 0) == 0)
287 /* Write the current process id to the file FILE.
288 Returns 0 if successful, -1 if not. */
290 write_pid (const char *file
)
294 fp
= fopen (file
, "w");
298 fprintf (fp
, "%d\n", getpid ());
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. */
313 void handle_requests (void)
316 int conn
; /* Handle on which connection (client) the request came from. */
323 get_request (&conn
, &req
, &key
);
325 dbg_log (_("handle_requests: request received (Version = %d)"),
331 param_t
*param
= malloc (sizeof (param_t
));
335 dbg_log ("\tGETPWBYNAME (%s)", key
);
339 pthread_create (&thread
, NULL
, cache_pw_disabled
, (void *)param
);
341 pthread_create (&thread
, NULL
, cache_getpwnam
, (void *)param
);
342 pthread_detach (thread
);
347 param_t
*param
= malloc (sizeof (param_t
));
351 dbg_log ("\tGETPWBYUID (%s)", key
);
355 pthread_create (&thread
, NULL
, cache_pw_disabled
, (void *)param
);
357 pthread_create (&thread
, NULL
, cache_getpwuid
, (void *)param
);
358 pthread_detach (thread
);
363 param_t
*param
= malloc (sizeof (param_t
));
367 dbg_log ("\tGETGRBYNAME (%s)", key
);
371 pthread_create (&thread
, NULL
, cache_gr_disabled
, (void *)param
);
373 pthread_create (&thread
, NULL
, cache_getgrnam
, (void *)param
);
374 pthread_detach (thread
);
379 param_t
*param
= malloc (sizeof (param_t
));
383 dbg_log ("\tGETGRBYGID (%s)", key
);
387 pthread_create (&thread
, NULL
, cache_gr_disabled
, (void *)param
);
389 pthread_create (&thread
, NULL
, cache_getgrgid
, (void *)param
);
390 pthread_detach (thread
);
394 /* Not yetimplemented. */
398 /* Not yet implemented. */
405 /* Clean up the files created by `bind'. */
406 unlink (_PATH_NSCDSOCKET
);
407 /* Clean up pid file. */
408 unlink (_PATH_NSCDPID
);
413 stat_response_header resp
;
416 dbg_log ("\tGETSTAT");
420 resp
.debug_level
= debug_flag
;
421 resp
.pw_enabled
= !disabled_passwd
;
422 resp
.gr_enabled
= !disabled_group
;
424 stat_send (conn
, &resp
);
430 dbg_log (_("Unknown request (%d)"), req
.type
);