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
);
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 */
157 /* Handle incoming requests */
164 /* Handle program arguments. */
166 parse_opt (int key
, char *arg
, struct argp_state
*state
)
180 printf (_("Only root is allowed to use this option!\n\n"));
184 int sock
= __nscd_open_socket ();
191 req
.version
= NSCD_VERSION
;
194 nbytes
= write (sock
, &req
, sizeof (request_header
));
196 if (nbytes
!= req
.key_len
)
205 return ARGP_ERR_UNKNOWN
;
210 /* Print the version information. */
212 print_version (FILE *stream
, struct argp_state
*state
)
214 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
215 fprintf (stream
, gettext ("\
216 Copyright (C) %s Free Software Foundation, Inc.\n\
217 This is free software; see the source for copying conditions. There is NO\n\
218 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
220 fprintf (stream
, gettext ("Written by %s.\n"), "Thorsten Kukuk");
224 /* Create a socket connected to a name. */
226 __nscd_open_socket (void)
228 struct sockaddr_un addr
;
231 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
235 addr
.sun_family
= AF_UNIX
;
236 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
237 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
248 termination_handler (int signum
)
252 /* Clean up the files created by `bind'. */
253 unlink (_PATH_NSCDSOCKET
);
255 /* Clean up pid file. */
256 unlink (_PATH_NSCDPID
);
261 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
263 check_pid (const char *file
)
267 fp
= fopen (file
, "r");
272 fscanf (fp
, "%d", &pid
);
275 if (kill (pid
, 0) == 0)
282 /* Write the current process id to the file FILE.
283 Returns 0 if successful, -1 if not. */
285 write_pid (const char *file
)
289 fp
= fopen (file
, "w");
293 fprintf (fp
, "%d\n", getpid ());
302 /* Type of the lookup function for netname2user. */
303 typedef int (*pwbyname_function
) (const char *name
, struct passwd
*pw
,
304 char *buffer
, size_t buflen
);
306 /* Hanlde incoming requests. */
308 void handle_requests (void)
311 int conn
; /* Handle on which connection (client) the request came from. */
318 get_request (&conn
, &req
, &key
);
320 dbg_log (_("handle_requests: request received (Version = %d)"),
326 param_t
*param
= malloc (sizeof (param_t
));
330 dbg_log ("\tGETPWBYNAME (%s)", key
);
334 pthread_create (&thread
, NULL
, cache_pw_disabled
, (void *)param
);
336 pthread_create (&thread
, NULL
, cache_getpwnam
, (void *)param
);
337 pthread_detach (thread
);
342 param_t
*param
= malloc (sizeof (param_t
));
346 dbg_log ("\tGETPWBYUID (%s)", key
);
350 pthread_create (&thread
, NULL
, cache_pw_disabled
, (void *)param
);
352 pthread_create (&thread
, NULL
, cache_getpwuid
, (void *)param
);
353 pthread_detach (thread
);
358 param_t
*param
= malloc (sizeof (param_t
));
362 dbg_log ("\tGETGRBYNAME (%s)", key
);
366 pthread_create (&thread
, NULL
, cache_gr_disabled
, (void *)param
);
368 pthread_create (&thread
, NULL
, cache_getgrnam
, (void *)param
);
369 pthread_detach (thread
);
374 param_t
*param
= malloc (sizeof (param_t
));
378 dbg_log ("\tGETGRBYGID (%s)", key
);
382 pthread_create (&thread
, NULL
, cache_gr_disabled
, (void *)param
);
384 pthread_create (&thread
, NULL
, cache_getgrgid
, (void *)param
);
385 pthread_detach (thread
);
389 /* Not yetimplemented. */
393 /* Not yet implemented. */
400 /* Clean up the files created by `bind'. */
401 unlink (_PATH_NSCDSOCKET
);
402 /* Clean up pid file. */
403 unlink (_PATH_NSCDPID
);
408 stat_response_header resp
;
411 dbg_log ("\tGETSTAT");
415 resp
.debug_level
= debug_flag
;
416 resp
.pw_enabled
= !disabled_passwd
;
417 resp
.gr_enabled
= !disabled_group
;
419 stat_send (conn
, &resp
);
425 dbg_log (_("Unknown request (%d)"), req
.type
);