/* nscd - Name Service Cache Daemon. Caches passwd and group. */
+#include <argp.h>
#include <errno.h>
-#include <getopt.h>
+#include <error.h>
#include <libintl.h>
#include <locale.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
+#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
int do_shutdown = 0;
int disabled_passwd = 0;
int disabled_group = 0;
+int go_background = 1;
+const char *conffile = _PATH_NSCDCONF;
static void termination_handler (int signum);
static int check_pid (const char *file);
static int write_pid (const char *file);
-static void usage (int status) __attribute__ ((noreturn));
static void handle_requests (void);
+/* Name and version of program. */
+static void print_version (FILE *stream, struct argp_state *state);
+void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
+
+/* Definitions of arguments for argp functions. */
+static const struct argp_option options[] =
+{
+ { "config-file", 'f', N_("NAME"), 0,
+ N_("Read configuration data from NAME") },
+ { "debug", 'd', NULL, 0,
+ N_("Do not fork and display messages on the current tty") },
+ { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
+ { "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
+ { NULL, 0, NULL, 0, NULL }
+};
+
+/* Short description of program. */
+static const char doc[] = N_("Name Switch Cache Daemon.");
+
+/* Prototype for option handler. */
+static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
+
+/* Data structure to communicate with argp functions. */
+static struct argp argp =
+{
+ options, parse_opt, NULL, doc,
+};
+
int
main (int argc, char **argv)
{
- int go_background = 1;
- const char *conffile = _PATH_NSCDCONF;
+ int remaining;
/* Set locale via LC_ALL. */
setlocale (LC_ALL, "");
/* Set the text message domain. */
textdomain (PACKAGE);
- while (1)
+ /* Parse and process arguments. */
+ argp_parse (&argp, argc, argv, 0, &remaining, NULL);
+
+ if (remaining != argc)
{
- int c;
- int option_index = 0;
- static struct option long_options[] = {
- { "debug", no_argument, NULL, 'd' },
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, 'V' },
- { "shutdown", no_argument, NULL, 'K' },
- {NULL, 0, NULL, '\0'}
- };
-
- c = getopt_long (argc, argv, "df:ghKV", long_options, &option_index);
- if (c == (-1))
- break;
- switch (c)
- {
- case 'd':
- debug_flag = 1;
- go_background = 0;
- break;
- case 'f':
- conffile = optarg;
- break;
- case 'h':
- usage (EXIT_SUCCESS);
- break;
- case 'K':
- if (getuid () != 0)
- {
- printf (_("Only root is allowed to use this option!\n\n"));
- usage (EXIT_FAILURE);
- }
- {
- int sock = __nscd_open_socket ();
- request_header req;
- ssize_t nbytes;
-
- if (sock == -1)
- exit (EXIT_FAILURE);
-
- req.version = NSCD_VERSION;
- req.type = SHUTDOWN;
- req.key_len = 0;
- nbytes = write (sock, &req, sizeof (request_header));
- close (sock);
- if (nbytes != req.key_len)
- exit (EXIT_FAILURE);
- else
- exit (EXIT_SUCCESS);
- }
- case 'g':
- print_stat ();
- exit (EXIT_SUCCESS);
- case 'V':
- printf ("nscd (GNU %s) %s\n", PACKAGE, VERSION);
- printf (_("\
-Copyright (C) %s Free Software Foundation, Inc.\n\
-This is free software; see the source for copying conditions. There is NO\n\
-warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
-"), "1998");
- printf (_("Written by %s.\n"), "Thorsten Kukuk");
- exit (EXIT_SUCCESS);
- default:
- usage (EXIT_FAILURE);
- }
+ error (0, 0, gettext ("wrong number of arguments"));
+ argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
+ exit (EXIT_FAILURE);
}
- signal (SIGINT, termination_handler);
- signal (SIGQUIT, termination_handler);
- signal (SIGTERM, termination_handler);
-
/* Check if we are already running. */
if (check_pid (_PATH_NSCDPID))
{
/* Behave like a daemon. */
if (go_background)
{
+ int i;
+
+ if (fork ())
+ exit (0);
+
+ for (i = 0; i < getdtablesize (); i++)
+ close (i);
+
+ if (fork ())
+ exit (0);
+
+ chdir ("/");
+
openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
- if (daemon (0, 0) < 0)
- {
- fprintf (stderr, _("connot auto-background: %s\n"),
- strerror (errno));
- exit (EXIT_FAILURE);
- }
if (write_pid (_PATH_NSCDPID) < 0)
dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
signal (SIGTTIN, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
}
+
+ signal (SIGINT, termination_handler);
+ signal (SIGQUIT, termination_handler);
+ signal (SIGTERM, termination_handler);
+ signal (SIGPIPE, SIG_IGN);
+
/* Cleanup files created by a previous `bind' */
unlink (_PATH_NSCDSOCKET);
/* Create first sockets */
init_sockets ();
/* Init databases */
- cache_pwdinit ();
- cache_grpinit ();
+ if ((cache_pwdinit () < 0) || (cache_grpinit () < 0))
+ {
+ fputs (_("Not enough memory\n"), stderr);
+ return 1;
+ }
/* Handle incoming requests */
handle_requests ();
return 0;
}
+
+/* Handle program arguments. */
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case 'd':
+ debug_flag = 1;
+ go_background = 0;
+ break;
+ case 'f':
+ conffile = arg;
+ break;
+ case 'K':
+ if (getuid () != 0)
+ {
+ printf (_("Only root is allowed to use this option!\n\n"));
+ exit (EXIT_FAILURE);
+ }
+ {
+ int sock = __nscd_open_socket ();
+ request_header req;
+ ssize_t nbytes;
+
+ if (sock == -1)
+ exit (EXIT_FAILURE);
+
+ req.version = NSCD_VERSION;
+ req.type = SHUTDOWN;
+ req.key_len = 0;
+ nbytes = write (sock, &req, sizeof (request_header));
+ close (sock);
+ if (nbytes != req.key_len)
+ exit (EXIT_FAILURE);
+ else
+ exit (EXIT_SUCCESS);
+ }
+ case 'g':
+ print_stat ();
+ exit (EXIT_SUCCESS);
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+/* Print the version information. */
+static void
+print_version (FILE *stream, struct argp_state *state)
+{
+ fprintf (stream, "nscd (GNU %s) %s\n", PACKAGE, VERSION);
+ fprintf (stream, gettext ("\
+Copyright (C) %s Free Software Foundation, Inc.\n\
+This is free software; see the source for copying conditions. There is NO\n\
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
+"), "1998");
+ fprintf (stream, gettext ("Written by %s.\n"), "Thorsten Kukuk");
+}
+
+
/* Create a socket connected to a name. */
int
__nscd_open_socket (void)
exit (EXIT_SUCCESS);
}
-/* Display usage information and exit. */
-static void
-usage (int status)
-{
- if (status != EXIT_SUCCESS)
- fprintf (stderr, _("Try `%s --help' for more information.\n"),
- program_invocation_name);
- else
- {
- printf (_("\
-Usage: %s [OPTION]...\n\
- -d, --debug do not fork and display messages on the current tty\n\
- -h, --help display this help and exit\n\
- -V, --version output version information and exit\n\
- -f configuration-file read configuration data from the specified file.\n\
- -K, --shutdown shut the server down.\n\
- -g Prints configuration and statistics to stdout.\n"),
- program_invocation_name);
- fputs (_("\
-Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"),
- stdout);
- }
- exit (status);
-}
-
/* Returns 1 if the process in pid file FILE is running, 0 if not. */
static int
check_pid (const char *file)
typedef int (*pwbyname_function) (const char *name, struct passwd *pw,
char *buffer, size_t buflen);
-/* Hanlde incoming requests. */
+/* Handle incoming requests. */
static
void handle_requests (void)
{
int conn; /* Handle on which connection (client) the request came from. */
int done = 0;
char *key;
+ pthread_attr_t th_attr;
+
+ /* We will create all threads detached. Therefore prepare an attribute
+ now. */
+ pthread_attr_init (&th_attr);
+ pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
while (!done)
{
{
param_t *param = malloc (sizeof (param_t));
pthread_t thread;
+ int status;
if (debug_flag)
dbg_log ("\tGETPWBYNAME (%s)", key);
param->key = key;
param->conn = conn;
if (disabled_passwd)
- pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
+ status = pthread_create (&thread, &th_attr, cache_pw_disabled,
+ (void *)param);
else
- pthread_create (&thread, NULL, cache_getpwnam, (void *)param);
+ status = pthread_create (&thread, &th_attr, cache_getpwnam,
+ (void *)param);
+ if (status != 0)
+ {
+ dbg_log (_("Creation of thread failed: %s"), strerror (errno));
+ close_socket (conn);
+ }
pthread_detach (thread);
}
break;
{
param_t *param = malloc (sizeof (param_t));
pthread_t thread;
+ int status;
if (debug_flag)
dbg_log ("\tGETPWBYUID (%s)", key);
param->key = key;
param->conn = conn;
if (disabled_passwd)
- pthread_create (&thread, NULL, cache_pw_disabled, (void *)param);
+ status = pthread_create (&thread, &th_attr, cache_pw_disabled,
+ (void *)param);
else
- pthread_create (&thread, NULL, cache_getpwuid, (void *)param);
- pthread_detach (thread);
+ status = pthread_create (&thread, &th_attr, cache_getpwuid,
+ (void *)param);
+ if (status != 0)
+ {
+ dbg_log (_("Creation of thread failed: %s"), strerror (errno));
+ close_socket (conn);
+ }
}
break;
case GETGRBYNAME:
{
param_t *param = malloc (sizeof (param_t));
pthread_t thread;
+ int status;
if (debug_flag)
dbg_log ("\tGETGRBYNAME (%s)", key);
param->key = key;
param->conn = conn;
if (disabled_group)
- pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
+ status = pthread_create (&thread, &th_attr, cache_gr_disabled,
+ (void *)param);
else
- pthread_create (&thread, NULL, cache_getgrnam, (void *)param);
- pthread_detach (thread);
+ status = pthread_create (&thread, &th_attr, cache_getgrnam,
+ (void *)param);
+ if (status != 0)
+ {
+ dbg_log (_("Creation of thread failed: %s"), strerror (errno));
+ close_socket (conn);
+ }
}
break;
case GETGRBYGID:
{
param_t *param = malloc (sizeof (param_t));
pthread_t thread;
+ int status;
if (debug_flag)
dbg_log ("\tGETGRBYGID (%s)", key);
param->key = key;
param->conn = conn;
if (disabled_group)
- pthread_create (&thread, NULL, cache_gr_disabled, (void *)param);
+ status = pthread_create (&thread, &th_attr, cache_gr_disabled,
+ (void *)param);
else
- pthread_create (&thread, NULL, cache_getgrgid, (void *)param);
- pthread_detach (thread);
+ status = pthread_create (&thread, &th_attr, cache_getgrgid,
+ (void *)param);
+ if (status != 0)
+ {
+ dbg_log (_("Creation of thread failed: %s"), strerror (errno));
+ close_socket (conn);
+ }
}
break;
case GETHOSTBYNAME:
break;
}
}
+
+ pthread_attr_destroy (&th_attr);
}