2005-01-13 00:43:44 +01:00
|
|
|
/* Copyright (c) 1998-2003, 2004, 2005 Free Software Foundation, Inc.
|
1998-01-31 09:39:55 +01:00
|
|
|
This file is part of the GNU C Library.
|
1999-06-11 22:58:21 +02:00
|
|
|
Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
1998-01-31 09:39:55 +01:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2004-12-22 21:10:10 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
/* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
#include <argp.h>
|
1998-10-18 17:16:22 +02:00
|
|
|
#include <assert.h>
|
2003-01-15 20:52:59 +01:00
|
|
|
#include <dirent.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <errno.h>
|
1998-01-31 13:11:10 +01:00
|
|
|
#include <error.h>
|
2003-01-15 20:52:59 +01:00
|
|
|
#include <fcntl.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <locale.h>
|
2003-01-15 20:52:59 +01:00
|
|
|
#include <paths.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
2003-05-04 09:00:44 +02:00
|
|
|
#include <stdbool.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
1998-07-01 13:30:38 +02:00
|
|
|
#include <unistd.h>
|
2004-08-26 20:35:05 +02:00
|
|
|
#include <sys/mman.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <sys/socket.h>
|
2003-01-15 20:52:59 +01:00
|
|
|
#include <sys/stat.h>
|
2004-09-03 10:15:41 +02:00
|
|
|
#include <sys/uio.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include "dbg_log.h"
|
|
|
|
#include "nscd.h"
|
2004-09-17 02:04:18 +02:00
|
|
|
#include "selinux.h"
|
2003-04-17 10:42:06 +02:00
|
|
|
#include "../nss/nsswitch.h"
|
2003-01-15 20:52:59 +01:00
|
|
|
#include <device-nrs.h>
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
/* Get libc version number. */
|
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
#define PACKAGE _libc_intl_domainname
|
|
|
|
|
|
|
|
/* Structure used by main() thread to keep track of the number of
|
|
|
|
active threads. Used to limit how many threads it will create
|
|
|
|
and under a shutdown condition to wait till all in-progress
|
|
|
|
requests have finished before "turning off the lights". */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int num_active;
|
|
|
|
pthread_cond_t thread_exit_cv;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
} thread_info_t;
|
|
|
|
|
|
|
|
thread_info_t thread_info;
|
|
|
|
|
1999-06-11 22:58:21 +02:00
|
|
|
int do_shutdown;
|
|
|
|
int disabled_passwd;
|
|
|
|
int disabled_group;
|
1998-01-31 13:11:10 +01:00
|
|
|
int go_background = 1;
|
1999-06-11 22:58:21 +02:00
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
int secure_in_use;
|
1998-10-18 17:16:22 +02:00
|
|
|
static const char *conffile = _PATH_NSCDCONF;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2003-04-26 06:15:50 +02:00
|
|
|
time_t start_time;
|
|
|
|
|
2004-09-11 01:56:29 +02:00
|
|
|
uintptr_t pagesize_m1;
|
|
|
|
|
2004-10-03 21:33:48 +02:00
|
|
|
int paranoia;
|
|
|
|
time_t restart_time;
|
|
|
|
time_t restart_interval = RESTART_INTERVAL;
|
|
|
|
const char *oldcwd;
|
|
|
|
uid_t old_uid;
|
|
|
|
gid_t old_gid;
|
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
static int check_pid (const char *file);
|
|
|
|
static int write_pid (const char *file);
|
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
/* 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") },
|
1998-10-18 17:16:22 +02:00
|
|
|
{ "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
|
1998-01-31 13:11:10 +01:00
|
|
|
{ "shutdown", 'K', NULL, 0, N_("Shut the server down") },
|
1998-03-24 18:03:23 +01:00
|
|
|
{ "statistic", 'g', NULL, 0, N_("Print current configuration statistic") },
|
1999-09-27 02:22:04 +02:00
|
|
|
{ "invalidate", 'i', N_("TABLE"), 0,
|
|
|
|
N_("Invalidate the specified cache") },
|
2004-09-21 01:16:22 +02:00
|
|
|
{ "secure", 'S', N_("TABLE,yes"), OPTION_HIDDEN,
|
|
|
|
N_("Use separate cache for each user")},
|
1998-01-31 13:11:10 +01:00
|
|
|
{ NULL, 0, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Short description of program. */
|
1998-10-29 16:36:59 +01:00
|
|
|
static const char doc[] = N_("Name Service Cache Daemon.");
|
1998-01-31 13:11:10 +01:00
|
|
|
|
|
|
|
/* Prototype for option handler. */
|
1999-12-31 19:51:25 +01:00
|
|
|
static error_t parse_opt (int key, char *arg, struct argp_state *state);
|
1998-01-31 13:11:10 +01:00
|
|
|
|
|
|
|
/* Data structure to communicate with argp functions. */
|
|
|
|
static struct argp argp =
|
|
|
|
{
|
|
|
|
options, parse_opt, NULL, doc,
|
|
|
|
};
|
|
|
|
|
2003-05-04 09:00:44 +02:00
|
|
|
/* True if only statistics are requested. */
|
|
|
|
static bool get_stats;
|
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
1998-01-31 13:11:10 +01:00
|
|
|
int remaining;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
/* Set locale via LC_ALL. */
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
/* Set the text message domain. */
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
2004-09-17 02:04:18 +02:00
|
|
|
/* Determine if the kernel has SELinux support. */
|
|
|
|
nscd_selinux_enabled (&selinux_enabled);
|
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
/* Parse and process arguments. */
|
|
|
|
argp_parse (&argp, argc, argv, 0, &remaining, NULL);
|
|
|
|
|
1998-02-24 16:22:29 +01:00
|
|
|
if (remaining != argc)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
1998-01-31 13:11:10 +01:00
|
|
|
error (0, 0, gettext ("wrong number of arguments"));
|
|
|
|
argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
|
2005-06-20 17:49:08 +02:00
|
|
|
exit (1);
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
|
2003-05-04 09:00:44 +02:00
|
|
|
/* Read the configuration file. */
|
|
|
|
if (nscd_parse_file (conffile, dbs) != 0)
|
2004-12-22 21:10:10 +01:00
|
|
|
{
|
|
|
|
/* We couldn't read the configuration file. We don't start the
|
|
|
|
server. */
|
|
|
|
dbg_log (_("cannot read configuration file; this is fatal"));
|
|
|
|
exit (1);
|
|
|
|
}
|
2003-05-04 09:00:44 +02:00
|
|
|
|
|
|
|
/* Do we only get statistics? */
|
|
|
|
if (get_stats)
|
|
|
|
/* Does not return. */
|
|
|
|
receive_print_stats ();
|
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
/* Check if we are already running. */
|
|
|
|
if (check_pid (_PATH_NSCDPID))
|
1998-10-18 17:16:22 +02:00
|
|
|
error (EXIT_FAILURE, 0, _("already running"));
|
1998-01-31 09:39:55 +01:00
|
|
|
|
2003-04-26 06:15:50 +02:00
|
|
|
/* Remember when we started. */
|
|
|
|
start_time = time (NULL);
|
|
|
|
|
2004-09-11 01:56:29 +02:00
|
|
|
/* Determine page size. */
|
|
|
|
pagesize_m1 = getpagesize () - 1;
|
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
/* Behave like a daemon. */
|
|
|
|
if (go_background)
|
|
|
|
{
|
1998-07-24 14:58:36 +02:00
|
|
|
int i;
|
|
|
|
|
2004-02-21 02:57:26 +01:00
|
|
|
pid_t pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
error (EXIT_FAILURE, errno, _("cannot fork"));
|
|
|
|
if (pid != 0)
|
1998-07-24 14:58:36 +02:00
|
|
|
exit (0);
|
|
|
|
|
2003-01-15 20:52:59 +01:00
|
|
|
int nullfd = open (_PATH_DEVNULL, O_RDWR);
|
|
|
|
if (nullfd != -1)
|
|
|
|
{
|
|
|
|
struct stat64 st;
|
|
|
|
|
|
|
|
if (fstat64 (nullfd, &st) == 0 && S_ISCHR (st.st_mode) != 0
|
|
|
|
#if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
|
|
|
|
&& st.st_rdev == makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/* It is the /dev/null special device alright. */
|
|
|
|
(void) dup2 (nullfd, STDIN_FILENO);
|
|
|
|
(void) dup2 (nullfd, STDOUT_FILENO);
|
|
|
|
(void) dup2 (nullfd, STDERR_FILENO);
|
|
|
|
|
|
|
|
if (nullfd > 2)
|
|
|
|
close (nullfd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Ugh, somebody is trying to play a trick on us. */
|
|
|
|
close (nullfd);
|
|
|
|
nullfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int min_close_fd = nullfd == -1 ? 0 : STDERR_FILENO + 1;
|
|
|
|
|
|
|
|
DIR *d = opendir ("/proc/self/fd");
|
|
|
|
if (d != NULL)
|
|
|
|
{
|
|
|
|
struct dirent64 *dirent;
|
|
|
|
int dfdn = dirfd (d);
|
|
|
|
|
|
|
|
while ((dirent = readdir64 (d)) != NULL)
|
|
|
|
{
|
|
|
|
char *endp;
|
2003-04-22 21:52:59 +02:00
|
|
|
long int fdn = strtol (dirent->d_name, &endp, 10);
|
2003-01-15 20:52:59 +01:00
|
|
|
|
|
|
|
if (*endp == '\0' && fdn != dfdn && fdn >= min_close_fd)
|
|
|
|
close ((int) fdn);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir (d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (i = min_close_fd; i < getdtablesize (); i++)
|
|
|
|
close (i);
|
1998-07-24 14:58:36 +02:00
|
|
|
|
2004-02-21 02:57:26 +01:00
|
|
|
pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
error (EXIT_FAILURE, errno, _("cannot fork"));
|
|
|
|
if (pid != 0)
|
1998-07-24 14:58:36 +02:00
|
|
|
exit (0);
|
|
|
|
|
2000-04-30 08:52:59 +02:00
|
|
|
setsid ();
|
|
|
|
|
2005-07-19 17:30:46 +02:00
|
|
|
if (chdir ("/") != 0)
|
|
|
|
error (EXIT_FAILURE, errno,
|
|
|
|
_("cannot change current working cirectory to \"/\""));
|
1998-07-24 14:58:36 +02:00
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
|
|
|
|
|
|
|
|
if (write_pid (_PATH_NSCDPID) < 0)
|
|
|
|
dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
|
|
|
|
|
2004-02-17 06:16:49 +01:00
|
|
|
if (!init_logfile ())
|
|
|
|
dbg_log (_("Could not create log file"));
|
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
/* Ignore job control signals. */
|
1998-01-31 09:39:55 +01:00
|
|
|
signal (SIGTTOU, SIG_IGN);
|
|
|
|
signal (SIGTTIN, SIG_IGN);
|
|
|
|
signal (SIGTSTP, SIG_IGN);
|
|
|
|
}
|
2004-10-03 21:33:48 +02:00
|
|
|
else
|
|
|
|
/* In foreground mode we are not paranoid. */
|
|
|
|
paranoia = 0;
|
1998-07-24 14:58:36 +02:00
|
|
|
|
2004-09-17 02:04:18 +02:00
|
|
|
/* Start the SELinux AVC. */
|
|
|
|
if (selinux_enabled)
|
|
|
|
nscd_avc_init ();
|
|
|
|
|
1998-07-24 14:58:36 +02:00
|
|
|
signal (SIGINT, termination_handler);
|
|
|
|
signal (SIGQUIT, termination_handler);
|
|
|
|
signal (SIGTERM, termination_handler);
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
2004-02-21 02:57:26 +01:00
|
|
|
/* Cleanup files created by a previous 'bind'. */
|
1998-01-31 09:39:55 +01:00
|
|
|
unlink (_PATH_NSCDSOCKET);
|
|
|
|
|
2003-04-17 10:42:06 +02:00
|
|
|
/* Make sure we do not get recursive calls. */
|
|
|
|
__nss_disable_nscd ();
|
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
/* Init databases. */
|
2003-05-04 09:00:44 +02:00
|
|
|
nscd_init ();
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
/* Handle incoming requests */
|
1998-10-18 17:16:22 +02:00
|
|
|
start_threads ();
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
|
|
|
|
/* Handle program arguments. */
|
|
|
|
static error_t
|
|
|
|
parse_opt (int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'd':
|
1998-10-18 17:16:22 +02:00
|
|
|
++debug_level;
|
1998-01-31 13:11:10 +01:00
|
|
|
go_background = 0;
|
|
|
|
break;
|
1998-10-18 17:16:22 +02:00
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
case 'f':
|
|
|
|
conffile = arg;
|
|
|
|
break;
|
1998-10-18 17:16:22 +02:00
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
case 'K':
|
|
|
|
if (getuid () != 0)
|
2005-06-20 17:49:08 +02:00
|
|
|
error (4, 0, _("Only root is allowed to use this option!"));
|
1998-01-31 13:11:10 +01:00
|
|
|
{
|
1998-10-18 17:16:22 +02:00
|
|
|
int sock = nscd_open_socket ();
|
1998-01-31 13:11:10 +01:00
|
|
|
request_header req;
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
if (sock == -1)
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
|
|
|
req.version = NSCD_VERSION;
|
|
|
|
req.type = SHUTDOWN;
|
|
|
|
req.key_len = 0;
|
2004-12-22 21:10:10 +01:00
|
|
|
nbytes = TEMP_FAILURE_RETRY (write (sock, &req,
|
|
|
|
sizeof (request_header)));
|
1998-01-31 13:11:10 +01:00
|
|
|
close (sock);
|
1998-10-18 17:16:22 +02:00
|
|
|
exit (nbytes != sizeof (request_header) ? EXIT_FAILURE : EXIT_SUCCESS);
|
1998-01-31 13:11:10 +01:00
|
|
|
}
|
1998-10-18 17:16:22 +02:00
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
case 'g':
|
2003-05-04 09:00:44 +02:00
|
|
|
get_stats = true;
|
|
|
|
break;
|
1998-10-18 17:16:22 +02:00
|
|
|
|
1999-09-27 02:22:04 +02:00
|
|
|
case 'i':
|
|
|
|
if (getuid () != 0)
|
2005-06-20 17:49:08 +02:00
|
|
|
error (4, 0, _("Only root is allowed to use this option!"));
|
1999-09-27 02:22:04 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int sock = nscd_open_socket ();
|
|
|
|
|
|
|
|
if (sock == -1)
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
2004-09-03 10:15:41 +02:00
|
|
|
request_header req;
|
2004-12-22 21:10:10 +01:00
|
|
|
ssize_t nbytes;
|
|
|
|
struct iovec iov[2];
|
|
|
|
|
1999-09-27 02:22:04 +02:00
|
|
|
if (strcmp (arg, "passwd") == 0)
|
|
|
|
req.key_len = sizeof "passwd";
|
|
|
|
else if (strcmp (arg, "group") == 0)
|
|
|
|
req.key_len = sizeof "group";
|
|
|
|
else if (strcmp (arg, "hosts") == 0)
|
|
|
|
req.key_len = sizeof "hosts";
|
|
|
|
else
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
|
|
|
|
req.version = NSCD_VERSION;
|
|
|
|
req.type = INVALIDATE;
|
|
|
|
|
2004-09-03 10:15:41 +02:00
|
|
|
iov[0].iov_base = &req;
|
|
|
|
iov[0].iov_len = sizeof (req);
|
2004-09-17 21:51:26 +02:00
|
|
|
iov[1].iov_base = arg;
|
2004-09-03 10:15:41 +02:00
|
|
|
iov[1].iov_len = req.key_len;
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
nbytes = TEMP_FAILURE_RETRY (writev (sock, iov, 2));
|
1999-09-27 02:22:04 +02:00
|
|
|
|
|
|
|
close (sock);
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
exit (nbytes != iov[0].iov_len + iov[1].iov_len
|
|
|
|
? EXIT_FAILURE : EXIT_SUCCESS);
|
1999-09-27 02:22:04 +02:00
|
|
|
}
|
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
case 't':
|
|
|
|
nthreads = atol (arg);
|
|
|
|
break;
|
|
|
|
|
1999-06-11 22:58:21 +02:00
|
|
|
case 'S':
|
2004-12-22 21:10:10 +01:00
|
|
|
#if 0
|
|
|
|
if (strcmp (arg, "passwd,yes") == 0)
|
|
|
|
secure_in_use = dbs[pwddb].secure = 1;
|
|
|
|
else if (strcmp (arg, "group,yes") == 0)
|
|
|
|
secure_in_use = dbs[grpdb].secure = 1;
|
|
|
|
else if (strcmp (arg, "hosts,yes") == 0)
|
|
|
|
secure_in_use = dbs[hstdb].secure = 1;
|
|
|
|
#else
|
2004-09-21 01:16:22 +02:00
|
|
|
error (0, 0, _("secure services not implemented anymore"));
|
2004-12-22 21:10:10 +01:00
|
|
|
#endif
|
1999-06-11 22:58:21 +02:00
|
|
|
break;
|
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
1998-10-18 17:16:22 +02:00
|
|
|
|
1998-01-31 13:11:10 +01:00
|
|
|
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\
|
2005-01-13 00:43:44 +01:00
|
|
|
"), "2005");
|
1998-10-18 17:16:22 +02:00
|
|
|
fprintf (stream, gettext ("Written by %s.\n"),
|
|
|
|
"Thorsten Kukuk and Ulrich Drepper");
|
1998-01-31 13:11:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
/* Create a socket connected to a name. */
|
1998-01-31 09:39:55 +01:00
|
|
|
int
|
1998-10-18 17:16:22 +02:00
|
|
|
nscd_open_socket (void)
|
1998-01-31 09:39:55 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = socket (PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
1998-10-18 17:16:22 +02:00
|
|
|
assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
|
1998-01-31 09:39:55 +01:00
|
|
|
strcpy (addr.sun_path, _PATH_NSCDSOCKET);
|
|
|
|
if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
|
|
|
|
{
|
|
|
|
close (sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2004-10-03 21:33:48 +02:00
|
|
|
|
1998-01-31 09:39:55 +01:00
|
|
|
/* Cleanup. */
|
1998-10-18 17:16:22 +02:00
|
|
|
void
|
1998-01-31 09:39:55 +01:00
|
|
|
termination_handler (int signum)
|
|
|
|
{
|
|
|
|
close_sockets ();
|
|
|
|
|
2004-02-21 02:57:26 +01:00
|
|
|
/* Clean up the file created by 'bind'. */
|
1998-01-31 09:39:55 +01:00
|
|
|
unlink (_PATH_NSCDSOCKET);
|
|
|
|
|
|
|
|
/* Clean up pid file. */
|
|
|
|
unlink (_PATH_NSCDPID);
|
|
|
|
|
2004-08-26 20:35:05 +02:00
|
|
|
// XXX Terminate threads.
|
|
|
|
|
|
|
|
/* Synchronize memory. */
|
|
|
|
for (int cnt = 0; cnt < lastdb; ++cnt)
|
2004-09-30 10:27:48 +02:00
|
|
|
{
|
2005-02-07 23:56:07 +01:00
|
|
|
if (!dbs[cnt].enabled)
|
|
|
|
continue;
|
|
|
|
|
2004-09-30 10:27:48 +02:00
|
|
|
/* Make sure nobody keeps using the database. */
|
|
|
|
dbs[cnt].head->timestamp = 0;
|
|
|
|
|
|
|
|
if (dbs[cnt].persistent)
|
|
|
|
// XXX async OK?
|
|
|
|
msync (dbs[cnt].head, dbs[cnt].memsize, MS_ASYNC);
|
|
|
|
}
|
2004-08-26 20:35:05 +02:00
|
|
|
|
2004-09-17 02:04:18 +02:00
|
|
|
/* Shutdown the SELinux AVC. */
|
|
|
|
if (selinux_enabled)
|
|
|
|
nscd_avc_destroy ();
|
|
|
|
|
2004-02-21 02:57:26 +01:00
|
|
|
_exit (EXIT_SUCCESS);
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 1 if the process in pid file FILE is running, 0 if not. */
|
|
|
|
static int
|
|
|
|
check_pid (const char *file)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen (file, "r");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
1998-10-18 17:16:22 +02:00
|
|
|
int n;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
1998-10-18 17:16:22 +02:00
|
|
|
n = fscanf (fp, "%d", &pid);
|
1998-01-31 09:39:55 +01:00
|
|
|
fclose (fp);
|
|
|
|
|
2004-10-03 21:33:48 +02:00
|
|
|
/* If we cannot parse the file default to assuming nscd runs.
|
|
|
|
If the PID is alive, assume it is running. That all unless
|
|
|
|
the PID is the same as the current process' since tha latter
|
|
|
|
can mean we re-exec. */
|
|
|
|
if ((n != 1 || kill (pid, 0) == 0) && pid != getpid ())
|
1998-01-31 09:39:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the current process id to the file FILE.
|
|
|
|
Returns 0 if successful, -1 if not. */
|
|
|
|
static int
|
|
|
|
write_pid (const char *file)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen (file, "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fprintf (fp, "%d\n", getpid ());
|
2004-12-22 21:10:10 +01:00
|
|
|
if (fflush (fp) || ferror (fp))
|
|
|
|
return -1;
|
1998-01-31 09:39:55 +01:00
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
2004-12-22 21:10:10 +01:00
|
|
|
return 0;
|
1998-01-31 09:39:55 +01:00
|
|
|
}
|