/* Copyright (c) 1998-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */
/* nscd - Name Service Cache Daemon. Caches passwd, group, and hosts. */
#include <argp.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <paths.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <stdarg.h>
#include "dbg_log.h"
#include "nscd.h"
#include "selinux.h"
#include "../nss/nsswitch.h"
#include <device-nrs.h>
#ifdef HAVE_INOTIFY
# include <sys/inotify.h>
#endif
#include <kernel-features.h>
/* Get libc version number. */
#include <version.h>
#define PACKAGE _libc_intl_domainname
int do_shutdown;
int disabled_passwd;
int disabled_group;
typedef enum
{
/* Running in background as daemon. */
RUN_DAEMONIZE,
/* 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
} run_modes;
static run_modes run_mode = RUN_DAEMONIZE;
static const char *conffile = _PATH_NSCDCONF;
static const char *print_cache = NULL;
time_t start_time;
uintptr_t pagesize_m1;
int paranoia;
time_t restart_time;
time_t restart_interval = RESTART_INTERVAL;
const char *oldcwd;
uid_t old_uid;
gid_t old_gid;
static int check_pid (const char *file);
static int write_pid (const char *file);
static int monitor_child (int fd);
/* 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;
/* Function to print some extra text in the help message. */
static char *more_help (int key, const char *text, void *input);
/* 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") },
{ "print", 'p', N_("NAME"), 0,
N_("Print contents of the offline cache file NAME") },
{ "foreground", 'F', NULL, 0,
N_("Do not fork, but otherwise behave like a daemon") },