2021-01-02 19:32:25 +00:00
|
|
|
/* Copyright (c) 1998-2021 Free Software Foundation, Inc.
|
1998-01-31 08:39:55 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
2007-07-16 00:56:07 +00:00
|
|
|
it under the terms of the GNU General Public License as published
|
|
|
|
by the Free Software Foundation; version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
1998-01-31 08:39:55 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2005-12-07 05:49:17 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2005-12-07 05:49:17 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
along with this program; if not, see <https://www.gnu.org/licenses/>. */
|
1998-01-31 08:39:55 +00:00
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
/* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
|
1998-01-31 08:39:55 +00:00
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
#include <argp.h>
|
1998-10-18 15:16:22 +00:00
|
|
|
#include <assert.h>
|
2003-01-15 19:52:59 +00:00
|
|
|
#include <dirent.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <errno.h>
|
1998-01-31 12:11:10 +00:00
|
|
|
#include <error.h>
|
2003-01-15 19:52:59 +00:00
|
|
|
#include <fcntl.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <locale.h>
|
2003-01-15 19:52:59 +00:00
|
|
|
#include <paths.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
2003-05-04 07:00:44 +00:00
|
|
|
#include <stdbool.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
1998-07-01 11:30:38 +00:00
|
|
|
#include <unistd.h>
|
2004-08-26 18:35:05 +00:00
|
|
|
#include <sys/mman.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <sys/socket.h>
|
2003-01-15 19:52:59 +00:00
|
|
|
#include <sys/stat.h>
|
2004-09-03 08:15:41 +00:00
|
|
|
#include <sys/uio.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
#include <sys/un.h>
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdarg.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
|
|
|
|
#include "dbg_log.h"
|
|
|
|
#include "nscd.h"
|
2004-09-17 00:04:18 +00:00
|
|
|
#include "selinux.h"
|
2003-04-17 08:42:06 +00:00
|
|
|
#include "../nss/nsswitch.h"
|
2003-01-15 19:52:59 +00:00
|
|
|
#include <device-nrs.h>
|
2011-07-11 18:50:24 +00:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
# include <sys/inotify.h>
|
|
|
|
#endif
|
2014-06-20 23:24:00 +00:00
|
|
|
#include <kernel-features.h>
|
1998-01-31 08:39:55 +00:00
|
|
|
|
|
|
|
/* Get libc version number. */
|
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
#define PACKAGE _libc_intl_domainname
|
|
|
|
|
1999-06-11 20:58:21 +00:00
|
|
|
int do_shutdown;
|
|
|
|
int disabled_passwd;
|
|
|
|
int disabled_group;
|
2012-05-09 14:54:38 +00:00
|
|
|
|
2012-05-09 19:34:47 +00:00
|
|
|
typedef enum
|
2012-05-09 14:54:38 +00:00
|
|
|
{
|
2012-05-09 19:34:47 +00:00
|
|
|
/* Running in background as daemon. */
|
|
|
|
RUN_DAEMONIZE,
|
2012-05-09 14:54:38 +00:00
|
|
|
/* Running in foreground but otherwise behave like a daemon,
|
|
|
|
i.e., detach from terminal and use syslog. This allows
|
|
|
|
better integration with services like systemd. */
|
|
|
|
RUN_FOREGROUND,
|
|
|
|
/* Run in foreground in debug mode. */
|
|
|
|
RUN_DEBUG
|
2012-05-09 19:34:47 +00:00
|
|
|
} run_modes;
|
|
|
|
|
|
|
|
static run_modes run_mode = RUN_DAEMONIZE;
|
1999-06-11 20:58:21 +00:00
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
static const char *conffile = _PATH_NSCDCONF;
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2020-02-19 17:31:09 +00:00
|
|
|
static const char *print_cache = NULL;
|
|
|
|
|
2003-04-26 04:15:50 +00:00
|
|
|
time_t start_time;
|
|
|
|
|
2004-09-10 23:56:29 +00:00
|
|
|
uintptr_t pagesize_m1;
|
|
|
|
|
2004-10-03 19:33:48 +00: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 08:39:55 +00:00
|
|
|
static int check_pid (const char *file);
|
|
|
|
static int write_pid (const char *file);
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
static int monitor_child (int fd);
|
1998-01-31 08:39:55 +00:00
|
|
|
|
1998-01-31 12:11:10 +00: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;
|
|
|
|
|
2009-02-06 20:13:07 +00:00
|
|
|
/* Function to print some extra text in the help message. */
|
|
|
|
static char *more_help (int key, const char *text, void *input);
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
/* 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") },
|
2020-02-19 17:31:09 +00:00
|
|
|
{ "print", 'p', N_("NAME"), 0,
|
|
|
|
N_("Print contents of the offline cache file NAME") },
|
2012-05-09 14:54:38 +00:00
|
|
|
{ "foreground", 'F', NULL, 0,
|
2012-05-09 19:34:47 +00:00
|
|
|
N_("Do not fork, but otherwise behave like a daemon") },
|
1998-10-18 15:16:22 +00:00
|
|
|
{ "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
|
1998-01-31 12:11:10 +00:00
|
|
|
{ "shutdown", 'K', NULL, 0, N_("Shut the server down") },
|
2008-03-29 18:12:46 +00:00
|
|
|
{ "statistics", 'g', NULL, 0, N_("Print current configuration statistics") },
|
1999-09-27 00:22:04 +00:00
|
|
|
{ "invalidate", 'i', N_("TABLE"), 0,
|
|
|
|
N_("Invalidate the specified cache") },
|
2004-09-20 23:16:22 +00:00
|
|
|
{ "secure", 'S', N_("TABLE,yes"), OPTION_HIDDEN,
|
|
|
|
N_("Use separate cache for each user")},
|
1998-01-31 12:11:10 +00:00
|
|
|
{ NULL, 0, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Short description of program. */
|
1998-10-29 15:36:59 +00:00
|
|
|
static const char doc[] = N_("Name Service Cache Daemon.");
|
1998-01-31 12:11:10 +00:00
|
|
|
|
|
|
|
/* Prototype for option handler. */
|
1999-12-31 18:51:25 +00:00
|
|
|
static error_t parse_opt (int key, char *arg, struct argp_state *state);
|
1998-01-31 12:11:10 +00:00
|
|
|
|
|
|
|
/* Data structure to communicate with argp functions. */
|
|
|
|
static struct argp argp =
|
|
|
|
{
|
2009-02-06 20:13:07 +00:00
|
|
|
options, parse_opt, NULL, doc, NULL, more_help
|
1998-01-31 12:11:10 +00:00
|
|
|
};
|
|
|
|
|
2003-05-04 07:00:44 +00:00
|
|
|
/* True if only statistics are requested. */
|
|
|
|
static bool get_stats;
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
static int parent_fd = -1;
|
2003-05-04 07:00:44 +00:00
|
|
|
|
1998-01-31 08:39:55 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
1998-01-31 12:11:10 +00:00
|
|
|
int remaining;
|
1998-01-31 08:39:55 +00:00
|
|
|
|
|
|
|
/* Set locale via LC_ALL. */
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
/* Set the text message domain. */
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
2004-09-17 00:04:18 +00:00
|
|
|
/* Determine if the kernel has SELinux support. */
|
|
|
|
nscd_selinux_enabled (&selinux_enabled);
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
/* Parse and process arguments. */
|
|
|
|
argp_parse (&argp, argc, argv, 0, &remaining, NULL);
|
|
|
|
|
1998-02-24 15:22:29 +00:00
|
|
|
if (remaining != argc)
|
1998-01-31 08:39:55 +00:00
|
|
|
{
|
1998-01-31 12:11:10 +00:00
|
|
|
error (0, 0, gettext ("wrong number of arguments"));
|
|
|
|
argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
|
2005-06-20 15:49:08 +00:00
|
|
|
exit (1);
|
1998-01-31 08:39:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:31:09 +00:00
|
|
|
/* Print the contents of the indicated cache file. */
|
|
|
|
if (print_cache != NULL)
|
|
|
|
/* Does not return. */
|
|
|
|
nscd_print_cache (print_cache);
|
|
|
|
|
2003-05-04 07:00:44 +00:00
|
|
|
/* Read the configuration file. */
|
|
|
|
if (nscd_parse_file (conffile, dbs) != 0)
|
2005-08-24 06:37:27 +00:00
|
|
|
/* We couldn't read the configuration file. We don't start the
|
|
|
|
server. */
|
|
|
|
error (EXIT_FAILURE, 0,
|
|
|
|
_("failure while reading configuration file; this is fatal"));
|
2003-05-04 07:00:44 +00:00
|
|
|
|
|
|
|
/* Do we only get statistics? */
|
|
|
|
if (get_stats)
|
|
|
|
/* Does not return. */
|
|
|
|
receive_print_stats ();
|
|
|
|
|
1998-01-31 08:39:55 +00:00
|
|
|
/* Check if we are already running. */
|
|
|
|
if (check_pid (_PATH_NSCDPID))
|
1998-10-18 15:16:22 +00:00
|
|
|
error (EXIT_FAILURE, 0, _("already running"));
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2003-04-26 04:15:50 +00:00
|
|
|
/* Remember when we started. */
|
|
|
|
start_time = time (NULL);
|
|
|
|
|
2004-09-10 23:56:29 +00:00
|
|
|
/* Determine page size. */
|
|
|
|
pagesize_m1 = getpagesize () - 1;
|
|
|
|
|
2012-05-09 15:41:25 +00:00
|
|
|
if (run_mode == RUN_DAEMONIZE || run_mode == RUN_FOREGROUND)
|
1998-01-31 08:39:55 +00:00
|
|
|
{
|
1998-07-24 12:58:36 +00:00
|
|
|
int i;
|
2012-05-09 14:54:38 +00:00
|
|
|
pid_t pid;
|
1998-07-24 12:58:36 +00:00
|
|
|
|
2012-05-09 14:54:38 +00:00
|
|
|
/* Behave like a daemon. */
|
|
|
|
if (run_mode == RUN_DAEMONIZE)
|
|
|
|
{
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
if (pipe (fd) != 0)
|
|
|
|
error (EXIT_FAILURE, errno,
|
|
|
|
_("cannot create a pipe to talk to the child"));
|
|
|
|
|
2012-05-09 14:54:38 +00:00
|
|
|
pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
error (EXIT_FAILURE, errno, _("cannot fork"));
|
|
|
|
if (pid != 0)
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
{
|
|
|
|
/* The parent only reads from the child. */
|
|
|
|
close (fd[1]);
|
|
|
|
exit (monitor_child (fd[0]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The child only writes to the parent. */
|
|
|
|
close (fd[0]);
|
|
|
|
parent_fd = fd[1];
|
|
|
|
}
|
2012-05-09 14:54:38 +00:00
|
|
|
}
|
1998-07-24 12:58:36 +00:00
|
|
|
|
2003-01-15 19:52:59 +00: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 19:52:59 +00:00
|
|
|
long int fdn = strtol (dirent->d_name, &endp, 10);
|
2003-01-15 19:52:59 +00:00
|
|
|
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
if (*endp == '\0' && fdn != dfdn && fdn >= min_close_fd
|
|
|
|
&& fdn != parent_fd)
|
2003-01-15 19:52:59 +00:00
|
|
|
close ((int) fdn);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir (d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (i = min_close_fd; i < getdtablesize (); i++)
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
if (i != parent_fd)
|
|
|
|
close (i);
|
1998-07-24 12:58:36 +00:00
|
|
|
|
2000-04-30 06:52:59 +00:00
|
|
|
setsid ();
|
|
|
|
|
2005-07-19 15:30:46 +00:00
|
|
|
if (chdir ("/") != 0)
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
do_exit (EXIT_FAILURE, errno,
|
|
|
|
_("cannot change current working directory to \"/\""));
|
1998-07-24 12:58:36 +00:00
|
|
|
|
1998-01-31 08:39:55 +00:00
|
|
|
openlog ("nscd", LOG_CONS | LOG_ODELAY, LOG_DAEMON);
|
|
|
|
|
|
|
|
if (write_pid (_PATH_NSCDPID) < 0)
|
2010-03-15 18:50:59 +00:00
|
|
|
dbg_log ("%s: %s", _PATH_NSCDPID, strerror (errno));
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2004-02-17 05:16:49 +00:00
|
|
|
if (!init_logfile ())
|
|
|
|
dbg_log (_("Could not create log file"));
|
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
/* Ignore job control signals. */
|
1998-01-31 08:39:55 +00:00
|
|
|
signal (SIGTTOU, SIG_IGN);
|
|
|
|
signal (SIGTTIN, SIG_IGN);
|
|
|
|
signal (SIGTSTP, SIG_IGN);
|
|
|
|
}
|
2004-10-03 19:33:48 +00:00
|
|
|
else
|
2012-05-09 14:54:38 +00:00
|
|
|
/* In debug mode we are not paranoid. */
|
2004-10-03 19:33:48 +00:00
|
|
|
paranoia = 0;
|
1998-07-24 12:58:36 +00:00
|
|
|
|
|
|
|
signal (SIGINT, termination_handler);
|
|
|
|
signal (SIGQUIT, termination_handler);
|
|
|
|
signal (SIGTERM, termination_handler);
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
2004-02-21 01:57:26 +00:00
|
|
|
/* Cleanup files created by a previous 'bind'. */
|
1998-01-31 08:39:55 +00:00
|
|
|
unlink (_PATH_NSCDSOCKET);
|
|
|
|
|
2011-07-11 18:50:24 +00:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
/* Use inotify to recognize changed files. */
|
|
|
|
inotify_fd = inotify_init1 (IN_NONBLOCK);
|
|
|
|
# ifndef __ASSUME_IN_NONBLOCK
|
|
|
|
if (inotify_fd == -1 && errno == ENOSYS)
|
|
|
|
{
|
|
|
|
inotify_fd = inotify_init ();
|
|
|
|
if (inotify_fd != -1)
|
|
|
|
fcntl (inotify_fd, F_SETFL, O_RDONLY | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 20:31:12 +00:00
|
|
|
#ifdef USE_NSCD
|
2003-04-17 08:42:06 +00:00
|
|
|
/* Make sure we do not get recursive calls. */
|
2011-07-11 18:50:24 +00:00
|
|
|
__nss_disable_nscd (register_traced_file);
|
2012-08-22 20:31:12 +00:00
|
|
|
#endif
|
2003-04-17 08:42:06 +00:00
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
/* Init databases. */
|
2003-05-04 07:00:44 +00:00
|
|
|
nscd_init ();
|
1998-01-31 08:39:55 +00:00
|
|
|
|
2011-09-02 09:13:39 +00:00
|
|
|
/* Start the SELinux AVC. */
|
|
|
|
if (selinux_enabled)
|
|
|
|
nscd_avc_init ();
|
|
|
|
|
1998-01-31 08:39:55 +00:00
|
|
|
/* Handle incoming requests */
|
1998-10-18 15:16:22 +00:00
|
|
|
start_threads ();
|
1998-01-31 08:39:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
|
2014-10-22 20:17:20 +00:00
|
|
|
static void __attribute__ ((noreturn))
|
|
|
|
invalidate_db (const char *dbname)
|
|
|
|
{
|
|
|
|
int sock = nscd_open_socket ();
|
|
|
|
|
|
|
|
if (sock == -1)
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
|
|
|
size_t dbname_len = strlen (dbname) + 1;
|
|
|
|
size_t reqlen = sizeof (request_header) + dbname_len;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
request_header req;
|
|
|
|
char dbname[];
|
|
|
|
} *reqdata = alloca (reqlen);
|
|
|
|
|
|
|
|
reqdata->req.key_len = dbname_len;
|
|
|
|
reqdata->req.version = NSCD_VERSION;
|
|
|
|
reqdata->req.type = INVALIDATE;
|
|
|
|
memcpy (reqdata->dbname, dbname, dbname_len);
|
|
|
|
|
|
|
|
ssize_t nbytes = TEMP_FAILURE_RETRY (send (sock, reqdata, reqlen,
|
|
|
|
MSG_NOSIGNAL));
|
|
|
|
|
|
|
|
if (nbytes != reqlen)
|
|
|
|
{
|
|
|
|
int err = errno;
|
|
|
|
close (sock);
|
|
|
|
error (EXIT_FAILURE, err, _("write incomplete"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for ack. Older nscd just closed the socket when
|
|
|
|
prune_cache finished, silently ignore that. */
|
|
|
|
int32_t resp = 0;
|
|
|
|
nbytes = TEMP_FAILURE_RETRY (read (sock, &resp, sizeof (resp)));
|
|
|
|
if (nbytes != 0 && nbytes != sizeof (resp))
|
|
|
|
{
|
|
|
|
int err = errno;
|
|
|
|
close (sock);
|
|
|
|
error (EXIT_FAILURE, err, _("cannot read invalidate ACK"));
|
|
|
|
}
|
|
|
|
|
|
|
|
close (sock);
|
|
|
|
|
|
|
|
if (resp != 0)
|
|
|
|
error (EXIT_FAILURE, resp, _("invalidation failed"));
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__ ((noreturn))
|
|
|
|
send_shutdown (void)
|
|
|
|
{
|
|
|
|
int sock = nscd_open_socket ();
|
|
|
|
|
|
|
|
if (sock == -1)
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
|
|
|
request_header req;
|
|
|
|
req.version = NSCD_VERSION;
|
|
|
|
req.type = SHUTDOWN;
|
|
|
|
req.key_len = 0;
|
|
|
|
|
|
|
|
ssize_t nbytes = TEMP_FAILURE_RETRY (send (sock, &req, sizeof req,
|
|
|
|
MSG_NOSIGNAL));
|
|
|
|
close (sock);
|
|
|
|
exit (nbytes != sizeof (request_header) ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
/* Handle program arguments. */
|
|
|
|
static error_t
|
|
|
|
parse_opt (int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'd':
|
1998-10-18 15:16:22 +00:00
|
|
|
++debug_level;
|
2012-05-09 14:54:38 +00:00
|
|
|
run_mode = RUN_DEBUG;
|
|
|
|
break;
|
|
|
|
|
2020-02-19 17:31:09 +00:00
|
|
|
case 'p':
|
|
|
|
print_cache = arg;
|
|
|
|
break;
|
|
|
|
|
2012-05-09 14:54:38 +00:00
|
|
|
case 'F':
|
|
|
|
run_mode = RUN_FOREGROUND;
|
1998-01-31 12:11:10 +00:00
|
|
|
break;
|
1998-10-18 15:16:22 +00:00
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
case 'f':
|
|
|
|
conffile = arg;
|
|
|
|
break;
|
1998-10-18 15:16:22 +00:00
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
case 'K':
|
|
|
|
if (getuid () != 0)
|
2005-06-20 15:49:08 +00:00
|
|
|
error (4, 0, _("Only root is allowed to use this option!"));
|
2014-10-22 20:17:20 +00:00
|
|
|
else
|
|
|
|
send_shutdown ();
|
|
|
|
break;
|
1998-10-18 15:16:22 +00:00
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
case 'g':
|
2003-05-04 07:00:44 +00:00
|
|
|
get_stats = true;
|
|
|
|
break;
|
1998-10-18 15:16:22 +00:00
|
|
|
|
1999-09-27 00:22:04 +00:00
|
|
|
case 'i':
|
2014-10-22 20:17:20 +00:00
|
|
|
{
|
|
|
|
/* Validate the database name. */
|
|
|
|
|
|
|
|
dbtype cnt;
|
|
|
|
for (cnt = pwddb; cnt < lastdb; ++cnt)
|
|
|
|
if (strcmp (arg, dbnames[cnt]) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (cnt == lastdb)
|
|
|
|
{
|
|
|
|
argp_error (state, _("'%s' is not a known database"), arg);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
1999-09-27 00:22:04 +00:00
|
|
|
if (getuid () != 0)
|
2005-06-20 15:49:08 +00:00
|
|
|
error (4, 0, _("Only root is allowed to use this option!"));
|
1999-09-27 00:22:04 +00:00
|
|
|
else
|
2014-10-22 20:17:20 +00:00
|
|
|
invalidate_db (arg);
|
|
|
|
break;
|
1999-09-27 00:22:04 +00:00
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
case 't':
|
|
|
|
nthreads = atol (arg);
|
|
|
|
break;
|
|
|
|
|
1999-06-11 20:58:21 +00:00
|
|
|
case 'S':
|
2004-09-20 23:16:22 +00:00
|
|
|
error (0, 0, _("secure services not implemented anymore"));
|
1999-06-11 20:58:21 +00:00
|
|
|
break;
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
1998-10-18 15:16:22 +00:00
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-06 20:13:07 +00:00
|
|
|
/* Print bug-reporting information in the help message. */
|
|
|
|
static char *
|
|
|
|
more_help (int key, const char *text, void *input)
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case ARGP_KEY_HELP_EXTRA:
|
2014-01-03 21:00:56 +00:00
|
|
|
{
|
2014-10-08 22:36:12 +00:00
|
|
|
/* We print some extra information. */
|
2014-01-03 21:00:56 +00:00
|
|
|
|
2014-10-08 22:36:12 +00:00
|
|
|
char *tables = xstrdup (dbnames[0]);
|
|
|
|
for (dbtype i = 1; i < lastdb; ++i)
|
2014-01-03 21:00:56 +00:00
|
|
|
{
|
2014-10-08 22:36:12 +00:00
|
|
|
char *more_tables;
|
|
|
|
if (asprintf (&more_tables, "%s %s", tables, dbnames[i]) < 0)
|
|
|
|
more_tables = NULL;
|
|
|
|
free (tables);
|
|
|
|
if (more_tables == NULL)
|
|
|
|
return NULL;
|
|
|
|
tables = more_tables;
|
2014-01-03 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 22:36:12 +00:00
|
|
|
char *tp;
|
|
|
|
if (asprintf (&tp, gettext ("\
|
2014-01-03 21:00:56 +00:00
|
|
|
Supported tables:\n\
|
|
|
|
%s\n\
|
|
|
|
\n\
|
2009-02-06 20:13:07 +00:00
|
|
|
For bug reporting instructions, please see:\n\
|
2014-01-03 21:00:56 +00:00
|
|
|
%s.\n\
|
|
|
|
"), tables, REPORT_BUGS_TO) < 0)
|
2014-10-08 22:36:12 +00:00
|
|
|
tp = NULL;
|
|
|
|
free (tables);
|
|
|
|
return tp;
|
|
|
|
}
|
2014-01-03 21:00:56 +00:00
|
|
|
|
2009-02-06 20:13:07 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-01-03 21:00:56 +00:00
|
|
|
|
2009-02-06 20:13:07 +00:00
|
|
|
return (char *) text;
|
|
|
|
}
|
|
|
|
|
1998-01-31 12:11:10 +00:00
|
|
|
/* Print the version information. */
|
|
|
|
static void
|
|
|
|
print_version (FILE *stream, struct argp_state *state)
|
|
|
|
{
|
2012-11-09 22:13:45 +00:00
|
|
|
fprintf (stream, "nscd %s%s\n", PKGVERSION, VERSION);
|
1998-01-31 12:11:10 +00:00
|
|
|
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\
|
2021-01-02 19:47:54 +00:00
|
|
|
"), "2021");
|
1998-10-18 15:16:22 +00:00
|
|
|
fprintf (stream, gettext ("Written by %s.\n"),
|
|
|
|
"Thorsten Kukuk and Ulrich Drepper");
|
1998-01-31 12:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
/* Create a socket connected to a name. */
|
1998-01-31 08:39:55 +00:00
|
|
|
int
|
1998-10-18 15:16:22 +00:00
|
|
|
nscd_open_socket (void)
|
1998-01-31 08:39:55 +00: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 15:16:22 +00:00
|
|
|
assert (sizeof (addr.sun_path) >= sizeof (_PATH_NSCDSOCKET));
|
1998-01-31 08:39:55 +00: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 19:33:48 +00:00
|
|
|
|
1998-01-31 08:39:55 +00:00
|
|
|
/* Cleanup. */
|
1998-10-18 15:16:22 +00:00
|
|
|
void
|
1998-01-31 08:39:55 +00:00
|
|
|
termination_handler (int signum)
|
|
|
|
{
|
|
|
|
close_sockets ();
|
|
|
|
|
2004-02-21 01:57:26 +00:00
|
|
|
/* Clean up the file created by 'bind'. */
|
1998-01-31 08:39:55 +00:00
|
|
|
unlink (_PATH_NSCDSOCKET);
|
|
|
|
|
|
|
|
/* Clean up pid file. */
|
|
|
|
unlink (_PATH_NSCDPID);
|
|
|
|
|
2004-08-26 18:35:05 +00:00
|
|
|
// XXX Terminate threads.
|
|
|
|
|
|
|
|
/* Synchronize memory. */
|
|
|
|
for (int cnt = 0; cnt < lastdb; ++cnt)
|
2004-09-30 08:27:48 +00:00
|
|
|
{
|
2011-07-19 17:59:57 +00:00
|
|
|
if (!dbs[cnt].enabled || dbs[cnt].head == NULL)
|
2005-02-07 22:56:07 +00:00
|
|
|
continue;
|
|
|
|
|
2004-09-30 08:27:48 +00: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 18:35:05 +00:00
|
|
|
|
2004-02-21 01:57:26 +00:00
|
|
|
_exit (EXIT_SUCCESS);
|
1998-01-31 08:39:55 +00: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 15:16:22 +00:00
|
|
|
int n;
|
1998-01-31 08:39:55 +00:00
|
|
|
|
1998-10-18 15:16:22 +00:00
|
|
|
n = fscanf (fp, "%d", &pid);
|
1998-01-31 08:39:55 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
2004-10-03 19:33:48 +00: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 ())
|
2010-03-15 18:50:59 +00:00
|
|
|
return 1;
|
1998-01-31 08:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ());
|
2006-04-07 04:29:25 +00:00
|
|
|
|
|
|
|
int result = fflush (fp) || ferror (fp) ? -1 : 0;
|
1998-01-31 08:39:55 +00:00
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
2006-04-07 04:29:25 +00:00
|
|
|
return result;
|
1998-01-31 08:39:55 +00:00
|
|
|
}
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
monitor_child (int fd)
|
|
|
|
{
|
|
|
|
int child_ret = 0;
|
|
|
|
int ret = read (fd, &child_ret, sizeof (child_ret));
|
|
|
|
|
|
|
|
/* The child terminated with an error, either via exit or some other abnormal
|
|
|
|
method, like a segfault. */
|
|
|
|
if (ret <= 0 || child_ret != 0)
|
|
|
|
{
|
2014-06-27 18:01:47 +00:00
|
|
|
int status;
|
|
|
|
int err = wait (&status);
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
2014-06-27 18:01:47 +00:00
|
|
|
fprintf (stderr, _("'wait' failed\n"));
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-27 18:01:47 +00:00
|
|
|
if (WIFEXITED (status))
|
2014-10-08 22:36:12 +00:00
|
|
|
{
|
|
|
|
child_ret = WEXITSTATUS (status);
|
|
|
|
fprintf (stderr, _("child exited with status %d\n"), child_ret);
|
|
|
|
}
|
2014-06-27 18:01:47 +00:00
|
|
|
if (WIFSIGNALED (status))
|
2014-10-08 22:36:12 +00:00
|
|
|
{
|
|
|
|
child_ret = WTERMSIG (status);
|
|
|
|
fprintf (stderr, _("child terminated by signal %d\n"), child_ret);
|
|
|
|
}
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have the child status, so exit with that code. */
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return child_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
do_exit (int child_ret, int errnum, const char *format, ...)
|
|
|
|
{
|
|
|
|
if (parent_fd != -1)
|
|
|
|
{
|
2016-01-15 17:44:07 +00:00
|
|
|
int ret __attribute__ ((unused));
|
|
|
|
ret = write (parent_fd, &child_ret, sizeof (child_ret));
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
assert (ret == sizeof (child_ret));
|
|
|
|
close (parent_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format != NULL)
|
|
|
|
{
|
|
|
|
/* Emulate error() since we don't have a va_list variant for it. */
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
fprintf (stderr, "%s: ", program_invocation_name);
|
|
|
|
|
|
|
|
va_start (argp, format);
|
|
|
|
vfprintf (stderr, format, argp);
|
|
|
|
va_end (argp);
|
|
|
|
|
|
|
|
fprintf (stderr, ": %s\n", strerror (errnum));
|
|
|
|
fflush (stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, exit. */
|
|
|
|
exit (child_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notify_parent (int child_ret)
|
|
|
|
{
|
|
|
|
if (parent_fd == -1)
|
|
|
|
return;
|
|
|
|
|
2016-01-15 17:44:07 +00:00
|
|
|
int ret __attribute__ ((unused));
|
|
|
|
ret = write (parent_fd, &child_ret, sizeof (child_ret));
|
nscd: Improved support for tracking startup failure in nscd service (BZ #16639)
Currently, the nscd parent process parses commandline options and
configuration, forks on startup and immediately exits with a success.
If the child process encounters some error after this, it goes
undetected and any services started up after it may have to repeatedly
check to make sure that the nscd service did actually start up and is
serving requests.
To make this process more reliable, I have added a pipe between the
parent and child process, through which the child process sends a
notification to the parent informing it of its status. The parent
waits for this status and once it receives it, exits with the
corresponding exit code. So if the child service sends a success
status (0), the parent exits with a success status. Similarly for
error conditions, the child sends the non-zero status code, which the
parent passes on as the exit code.
This, along with setting the nscd service type to forking in its
systemd configuration file, allows systemd to be certain that the nscd
service is ready and is accepting connections.
2014-03-03 17:21:39 +00:00
|
|
|
assert (ret == sizeof (child_ret));
|
|
|
|
close (parent_fd);
|
|
|
|
parent_fd = -1;
|
|
|
|
}
|