#include <stdlib.h>
#include <string.h>
#include <syslog.h>
+#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
{ "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 }
};
exit (EXIT_FAILURE);
}
- signal (SIGINT, termination_handler);
- signal (SIGQUIT, termination_handler);
- signal (SIGTERM, termination_handler);
- signal (SIGPIPE, SIG_IGN);
-
/* 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);
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);
}