1 /* Copyright (c) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
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
;
63 int go_background
= 1;
67 static const char *conffile
= _PATH_NSCDCONF
;
69 static int check_pid (const char *file
);
70 static int write_pid (const char *file
);
72 /* Name and version of program. */
73 static void print_version (FILE *stream
, struct argp_state
*state
);
74 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
76 /* Definitions of arguments for argp functions. */
77 static const struct argp_option options
[] =
79 { "config-file", 'f', N_("NAME"), 0,
80 N_("Read configuration data from NAME") },
81 { "debug", 'd', NULL
, 0,
82 N_("Do not fork and display messages on the current tty") },
83 { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
84 { "shutdown", 'K', NULL
, 0, N_("Shut the server down") },
85 { "statistic", 'g', NULL
, 0, N_("Print current configuration statistic") },
86 { "invalidate", 'i', N_("TABLE"), 0,
87 N_("Invalidate the specified cache") },
88 { "secure", 'S', N_("TABLE,yes"), 0, N_("Use separate cache for each user")},
89 { NULL
, 0, NULL
, 0, NULL
}
92 /* Short description of program. */
93 static const char doc
[] = N_("Name Service Cache Daemon.");
95 /* Prototype for option handler. */
96 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
98 /* Data structure to communicate with argp functions. */
99 static struct argp argp
=
101 options
, parse_opt
, NULL
, doc
,
105 main (int argc
, char **argv
)
109 /* Set locale via LC_ALL. */
110 setlocale (LC_ALL
, "");
111 /* Set the text message domain. */
112 textdomain (PACKAGE
);
114 /* Parse and process arguments. */
115 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
117 if (remaining
!= argc
)
119 error (0, 0, gettext ("wrong number of arguments"));
120 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
124 /* Check if we are already running. */
125 if (check_pid (_PATH_NSCDPID
))
126 error (EXIT_FAILURE
, 0, _("already running"));
128 /* Behave like a daemon. */
136 for (i
= 0; i
< getdtablesize (); i
++)
146 openlog ("nscd", LOG_CONS
| LOG_ODELAY
, LOG_DAEMON
);
148 if (write_pid (_PATH_NSCDPID
) < 0)
149 dbg_log ("%s: %s", _PATH_NSCDPID
, strerror (errno
));
151 /* Ignore job control signals. */
152 signal (SIGTTOU
, SIG_IGN
);
153 signal (SIGTTIN
, SIG_IGN
);
154 signal (SIGTSTP
, SIG_IGN
);
157 signal (SIGINT
, termination_handler
);
158 signal (SIGQUIT
, termination_handler
);
159 signal (SIGTERM
, termination_handler
);
160 signal (SIGPIPE
, SIG_IGN
);
162 /* Cleanup files created by a previous `bind'. */
163 unlink (_PATH_NSCDSOCKET
);
165 /* Init databases. */
166 nscd_init (conffile
);
168 /* Handle incoming requests */
175 /* Handle program arguments. */
177 parse_opt (int key
, char *arg
, struct argp_state
*state
)
192 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
194 int sock
= nscd_open_socket ();
201 req
.version
= NSCD_VERSION
;
204 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
205 sizeof (request_header
)));
207 exit (nbytes
!= sizeof (request_header
) ? EXIT_FAILURE
: EXIT_SUCCESS
);
212 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
213 receive_print_stats ();
214 /* Does not return. */
218 error (EXIT_FAILURE
, 0, _("Only root is allowed to use this option!"));
221 int sock
= nscd_open_socket ();
228 if (strcmp (arg
, "passwd") == 0)
229 req
.key_len
= sizeof "passwd";
230 else if (strcmp (arg
, "group") == 0)
231 req
.key_len
= sizeof "group";
232 else if (strcmp (arg
, "hosts") == 0)
233 req
.key_len
= sizeof "hosts";
235 return ARGP_ERR_UNKNOWN
;
237 req
.version
= NSCD_VERSION
;
238 req
.type
= INVALIDATE
;
239 nbytes
= TEMP_FAILURE_RETRY (write (sock
, &req
,
240 sizeof (request_header
)));
241 if (nbytes
!= sizeof (request_header
))
247 nbytes
= TEMP_FAILURE_RETRY (write (sock
, (void *)arg
, req
.key_len
));
251 exit (nbytes
!= req
.key_len
? EXIT_FAILURE
: EXIT_SUCCESS
);
255 nthreads
= atol (arg
);
259 if (strcmp (arg
, "passwd,yes") == 0)
260 secure_in_use
= secure
[pwddb
] = 1;
261 else if (strcmp (arg
, "group,yes") == 0)
262 secure_in_use
= secure
[grpdb
] = 1;
263 else if (strcmp (arg
, "hosts,yes") == 0)
264 secure_in_use
= secure
[hstdb
] = 1;
268 return ARGP_ERR_UNKNOWN
;
274 /* Print the version information. */
276 print_version (FILE *stream
, struct argp_state
*state
)
278 fprintf (stream
, "nscd (GNU %s) %s\n", PACKAGE
, VERSION
);
279 fprintf (stream
, gettext ("\
280 Copyright (C) %s Free Software Foundation, Inc.\n\
281 This is free software; see the source for copying conditions. There is NO\n\
282 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
284 fprintf (stream
, gettext ("Written by %s.\n"),
285 "Thorsten Kukuk and Ulrich Drepper");
289 /* Create a socket connected to a name. */
291 nscd_open_socket (void)
293 struct sockaddr_un addr
;
296 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
300 addr
.sun_family
= AF_UNIX
;
301 assert (sizeof (addr
.sun_path
) >= sizeof (_PATH_NSCDSOCKET
));
302 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
303 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
314 termination_handler (int signum
)
318 /* Clean up the file created by `bind'. */
319 unlink (_PATH_NSCDSOCKET
);
321 /* Clean up pid file. */
322 unlink (_PATH_NSCDPID
);
327 /* Returns 1 if the process in pid file FILE is running, 0 if not. */
329 check_pid (const char *file
)
333 fp
= fopen (file
, "r");
339 n
= fscanf (fp
, "%d", &pid
);
342 if (n
!= 1 || kill (pid
, 0) == 0)
349 /* Write the current process id to the file FILE.
350 Returns 0 if successful, -1 if not. */
352 write_pid (const char *file
)
356 fp
= fopen (file
, "w");
360 fprintf (fp
, "%d\n", getpid ());
361 if (fflush (fp
) || ferror (fp
))