1998-10-18 23:16:22 +08:00
|
|
|
/* Inner loops of cache daemon.
|
2017-01-01 08:14:16 +08:00
|
|
|
Copyright (C) 1998-2017 Free Software Foundation, Inc.
|
1998-01-31 16:39:55 +08:00
|
|
|
This file is part of the GNU C Library.
|
1998-10-18 23:16:22 +08:00
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2005-12-07 13:49:17 +08:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
2007-07-16 08:56:07 +08: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 16:39:55 +08:00
|
|
|
|
2005-12-07 13:49:17 +08:00
|
|
|
This program is distributed in the hope that it will be useful,
|
1998-01-31 16:39:55 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2005-12-07 13:49:17 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2005-12-07 13:49:17 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
2012-02-10 07:18:22 +08:00
|
|
|
along with this program; if not, see <http://www.gnu.org/licenses/>. */
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2004-10-04 03:33:48 +08:00
|
|
|
#include <alloca.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <assert.h>
|
2003-04-28 10:43:30 +08:00
|
|
|
#include <atomic.h>
|
1998-01-31 16:39:55 +08:00
|
|
|
#include <error.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <errno.h>
|
2004-08-26 01:24:52 +08:00
|
|
|
#include <fcntl.h>
|
2002-01-18 10:10:41 +08:00
|
|
|
#include <grp.h>
|
2011-10-31 13:51:16 +08:00
|
|
|
#include <ifaddrs.h>
|
2004-08-27 02:35:05 +08:00
|
|
|
#include <libintl.h>
|
1998-01-31 16:39:55 +08:00
|
|
|
#include <pthread.h>
|
2002-01-18 10:10:41 +08:00
|
|
|
#include <pwd.h>
|
2004-06-28 12:42:05 +08:00
|
|
|
#include <resolv.h>
|
2002-01-18 10:10:41 +08:00
|
|
|
#include <stdio.h>
|
1998-01-31 16:39:55 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2013-05-01 23:46:34 +08:00
|
|
|
#include <stdint.h>
|
1999-02-07 08:06:12 +08:00
|
|
|
#include <arpa/inet.h>
|
2011-10-31 13:51:16 +08:00
|
|
|
#ifdef HAVE_NETLINK
|
2011-11-01 20:43:33 +08:00
|
|
|
# include <linux/netlink.h>
|
|
|
|
# include <linux/rtnetlink.h>
|
2011-10-31 13:51:16 +08:00
|
|
|
#endif
|
2004-10-03 15:33:54 +08:00
|
|
|
#ifdef HAVE_EPOLL
|
|
|
|
# include <sys/epoll.h>
|
|
|
|
#endif
|
2008-06-13 06:40:27 +08:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
# include <sys/inotify.h>
|
|
|
|
#endif
|
2004-08-27 02:35:05 +08:00
|
|
|
#include <sys/mman.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <sys/param.h>
|
1998-06-23 01:08:51 +08:00
|
|
|
#include <sys/poll.h>
|
2005-11-20 01:22:39 +08:00
|
|
|
#ifdef HAVE_SENDFILE
|
|
|
|
# include <sys/sendfile.h>
|
|
|
|
#endif
|
1998-01-31 16:39:55 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include "nscd.h"
|
|
|
|
#include "dbg_log.h"
|
2004-09-17 08:04:18 +08:00
|
|
|
#include "selinux.h"
|
2008-06-14 00:31:07 +08:00
|
|
|
#include <resolv/resolv.h>
|
2012-05-11 05:39:53 +08:00
|
|
|
|
|
|
|
#include <kernel-features.h>
|
Narrowing the visibility of libc-internal.h even further.
posix/wordexp-test.c used libc-internal.h for PTR_ALIGN_DOWN; similar
to what was done with libc-diag.h, I have split the definitions of
cast_to_integer, ALIGN_UP, ALIGN_DOWN, PTR_ALIGN_UP, and PTR_ALIGN_DOWN
to a new header, libc-pointer-arith.h.
It then occurred to me that the remaining declarations in libc-internal.h
are mostly to do with early initialization, and probably most of the
files including it, even in the core code, don't need it anymore. Indeed,
only 19 files actually need what remains of libc-internal.h. 23 others
need libc-diag.h instead, and 12 need libc-pointer-arith.h instead.
No file needs more than one of them, and 16 don't need any of them!
So, with this patch, libc-internal.h stops including libc-diag.h as
well as losing the pointer arithmetic macros, and all including files
are adjusted.
* include/libc-pointer-arith.h: New file. Define
cast_to_integer, ALIGN_UP, ALIGN_DOWN, PTR_ALIGN_UP, and
PTR_ALIGN_DOWN here.
* include/libc-internal.h: Definitions of above macros
moved from here. Don't include libc-diag.h anymore either.
* posix/wordexp-test.c: Include stdint.h and libc-pointer-arith.h.
Don't include libc-internal.h.
* debug/pcprofile.c, elf/dl-tunables.c, elf/soinit.c, io/openat.c
* io/openat64.c, misc/ptrace.c, nptl/pthread_clock_gettime.c
* nptl/pthread_clock_settime.c, nptl/pthread_cond_common.c
* string/strcoll_l.c, sysdeps/nacl/brk.c
* sysdeps/unix/clock_settime.c
* sysdeps/unix/sysv/linux/i386/get_clockfreq.c
* sysdeps/unix/sysv/linux/ia64/get_clockfreq.c
* sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
* sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c:
Don't include libc-internal.h.
* elf/get-dynamic-info.h, iconv/loop.c
* iconvdata/iso-2022-cn-ext.c, locale/weight.h, locale/weightwc.h
* misc/reboot.c, nis/nis_table.c, nptl_db/thread_dbP.h
* nscd/connections.c, resolv/res_send.c, soft-fp/fmadf4.c
* soft-fp/fmasf4.c, soft-fp/fmatf4.c, stdio-common/vfscanf.c
* sysdeps/ieee754/dbl-64/e_lgamma_r.c
* sysdeps/ieee754/dbl-64/k_rem_pio2.c
* sysdeps/ieee754/flt-32/e_lgammaf_r.c
* sysdeps/ieee754/flt-32/k_rem_pio2f.c
* sysdeps/ieee754/ldbl-128/k_tanl.c
* sysdeps/ieee754/ldbl-128ibm/k_tanl.c
* sysdeps/ieee754/ldbl-96/e_lgammal_r.c
* sysdeps/ieee754/ldbl-96/k_tanl.c, sysdeps/nptl/futex-internal.h:
Include libc-diag.h instead of libc-internal.h.
* elf/dl-load.c, elf/dl-reloc.c, locale/programs/locarchive.c
* nptl/nptl-init.c, string/strcspn.c, string/strspn.c
* malloc/malloc.c, sysdeps/i386/nptl/tls.h
* sysdeps/nacl/dl-map-segments.h, sysdeps/x86_64/atomic-machine.h
* sysdeps/unix/sysv/linux/spawni.c
* sysdeps/x86_64/nptl/tls.h:
Include libc-pointer-arith.h instead of libc-internal.h.
* elf/get-dynamic-info.h, sysdeps/nacl/dl-map-segments.h
* sysdeps/x86_64/atomic-machine.h:
Add multiple include guard.
2017-02-27 09:17:52 +08:00
|
|
|
#include <libc-diag.h>
|
2004-12-23 04:10:10 +08:00
|
|
|
|
|
|
|
|
2002-01-18 10:10:41 +08:00
|
|
|
/* Support to run nscd as an unprivileged user */
|
|
|
|
const char *server_user;
|
|
|
|
static uid_t server_uid;
|
|
|
|
static gid_t server_gid;
|
2003-05-04 15:00:44 +08:00
|
|
|
const char *stat_user;
|
|
|
|
uid_t stat_uid;
|
2002-01-18 10:10:41 +08:00
|
|
|
static gid_t *server_groups;
|
|
|
|
#ifndef NGROUPS
|
|
|
|
# define NGROUPS 32
|
|
|
|
#endif
|
2004-08-27 02:35:05 +08:00
|
|
|
static int server_ngroups;
|
2002-01-18 10:10:41 +08:00
|
|
|
|
2004-10-04 05:11:37 +08:00
|
|
|
static pthread_attr_t attr;
|
|
|
|
|
2002-01-18 10:10:41 +08:00
|
|
|
static void begin_drop_privileges (void);
|
|
|
|
static void finish_drop_privileges (void);
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Map request type to a string. */
|
2007-01-14 13:26:04 +08:00
|
|
|
const char *const serv2str[LASTREQ] =
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
[GETPWBYNAME] = "GETPWBYNAME",
|
|
|
|
[GETPWBYUID] = "GETPWBYUID",
|
|
|
|
[GETGRBYNAME] = "GETGRBYNAME",
|
|
|
|
[GETGRBYGID] = "GETGRBYGID",
|
|
|
|
[GETHOSTBYNAME] = "GETHOSTBYNAME",
|
|
|
|
[GETHOSTBYNAMEv6] = "GETHOSTBYNAMEv6",
|
|
|
|
[GETHOSTBYADDR] = "GETHOSTBYADDR",
|
|
|
|
[GETHOSTBYADDRv6] = "GETHOSTBYADDRv6",
|
|
|
|
[SHUTDOWN] = "SHUTDOWN",
|
1999-09-27 08:22:04 +08:00
|
|
|
[GETSTAT] = "GETSTAT",
|
2004-09-08 23:46:42 +08:00
|
|
|
[INVALIDATE] = "INVALIDATE",
|
|
|
|
[GETFDPW] = "GETFDPW",
|
|
|
|
[GETFDGR] = "GETFDGR",
|
2004-09-15 16:25:49 +08:00
|
|
|
[GETFDHST] = "GETFDHST",
|
2004-09-30 10:24:51 +08:00
|
|
|
[GETAI] = "GETAI",
|
2007-01-14 13:26:04 +08:00
|
|
|
[INITGROUPS] = "INITGROUPS",
|
|
|
|
[GETSERVBYNAME] = "GETSERVBYNAME",
|
|
|
|
[GETSERVBYPORT] = "GETSERVBYPORT",
|
2011-10-07 22:06:31 +08:00
|
|
|
[GETFDSERV] = "GETFDSERV",
|
|
|
|
[GETNETGRENT] = "GETNETGRENT",
|
|
|
|
[INNETGR] = "INNETGR",
|
|
|
|
[GETFDNETGR] = "GETFDNETGR"
|
1998-10-18 23:16:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The control data structures for the services. */
|
2004-08-27 02:35:05 +08:00
|
|
|
struct database_dyn dbs[lastdb] =
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
[pwddb] = {
|
2002-12-16 07:23:24 +08:00
|
|
|
.lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
|
2007-11-06 08:45:40 +08:00
|
|
|
.prune_lock = PTHREAD_MUTEX_INITIALIZER,
|
2008-12-09 13:24:37 +08:00
|
|
|
.prune_run_lock = PTHREAD_MUTEX_INITIALIZER,
|
2002-12-16 07:23:24 +08:00
|
|
|
.enabled = 0,
|
|
|
|
.check_file = 1,
|
2004-08-27 02:35:05 +08:00
|
|
|
.persistent = 0,
|
2006-04-27 01:32:10 +08:00
|
|
|
.propagate = 1,
|
2004-09-08 23:46:42 +08:00
|
|
|
.shared = 0,
|
2005-08-24 07:21:53 +08:00
|
|
|
.max_db_size = DEFAULT_MAX_DB_SIZE,
|
2008-03-04 09:54:09 +08:00
|
|
|
.suggested_module = DEFAULT_SUGGESTED_MODULE,
|
2004-08-27 02:35:05 +08:00
|
|
|
.db_filename = _PATH_NSCD_PASSWD_DB,
|
2002-12-16 07:23:24 +08:00
|
|
|
.disabled_iov = &pwd_iov_disabled,
|
|
|
|
.postimeout = 3600,
|
2004-08-27 02:35:05 +08:00
|
|
|
.negtimeout = 20,
|
|
|
|
.wr_fd = -1,
|
|
|
|
.ro_fd = -1,
|
|
|
|
.mmap_used = false
|
1998-10-18 23:16:22 +08:00
|
|
|
},
|
|
|
|
[grpdb] = {
|
2002-12-16 07:23:24 +08:00
|
|
|
.lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
|
2007-11-06 08:45:40 +08:00
|
|
|
.prune_lock = PTHREAD_MUTEX_INITIALIZER,
|
2008-12-09 13:24:37 +08:00
|
|
|
.prune_run_lock = PTHREAD_MUTEX_INITIALIZER,
|
2002-12-16 07:23:24 +08:00
|
|
|
.enabled = 0,
|
|
|
|
.check_file = 1,
|
2004-08-27 02:35:05 +08:00
|
|
|
.persistent = 0,
|
2006-04-27 01:32:10 +08:00
|
|
|
.propagate = 1,
|
2004-09-08 23:46:42 +08:00
|
|
|
.shared = 0,
|
2005-08-24 07:21:53 +08:00
|
|
|
.max_db_size = DEFAULT_MAX_DB_SIZE,
|
2008-03-04 09:54:09 +08:00
|
|
|
.suggested_module = DEFAULT_SUGGESTED_MODULE,
|
2004-08-27 02:35:05 +08:00
|
|
|
.db_filename = _PATH_NSCD_GROUP_DB,
|
2002-12-16 07:23:24 +08:00
|
|
|
.disabled_iov = &grp_iov_disabled,
|
|
|
|
.postimeout = 3600,
|
2004-08-27 02:35:05 +08:00
|
|
|
.negtimeout = 60,
|
|
|
|
.wr_fd = -1,
|
|
|
|
.ro_fd = -1,
|
|
|
|
.mmap_used = false
|
1998-10-18 23:16:22 +08:00
|
|
|
},
|
|
|
|
[hstdb] = {
|
2002-12-16 07:23:24 +08:00
|
|
|
.lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
|
2007-11-06 08:45:40 +08:00
|
|
|
.prune_lock = PTHREAD_MUTEX_INITIALIZER,
|
2008-12-09 13:24:37 +08:00
|
|
|
.prune_run_lock = PTHREAD_MUTEX_INITIALIZER,
|
2002-12-16 07:23:24 +08:00
|
|
|
.enabled = 0,
|
|
|
|
.check_file = 1,
|
2004-08-27 02:35:05 +08:00
|
|
|
.persistent = 0,
|
2006-04-27 01:32:10 +08:00
|
|
|
.propagate = 0, /* Not used. */
|
2004-09-08 23:46:42 +08:00
|
|
|
.shared = 0,
|
2005-08-24 07:21:53 +08:00
|
|
|
.max_db_size = DEFAULT_MAX_DB_SIZE,
|
2008-03-04 09:54:09 +08:00
|
|
|
.suggested_module = DEFAULT_SUGGESTED_MODULE,
|
2004-08-27 02:35:05 +08:00
|
|
|
.db_filename = _PATH_NSCD_HOSTS_DB,
|
2002-12-16 07:23:24 +08:00
|
|
|
.disabled_iov = &hst_iov_disabled,
|
|
|
|
.postimeout = 3600,
|
2004-08-27 02:35:05 +08:00
|
|
|
.negtimeout = 20,
|
|
|
|
.wr_fd = -1,
|
|
|
|
.ro_fd = -1,
|
|
|
|
.mmap_used = false
|
2007-01-14 13:26:04 +08:00
|
|
|
},
|
|
|
|
[servdb] = {
|
|
|
|
.lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
|
2007-11-06 08:45:40 +08:00
|
|
|
.prune_lock = PTHREAD_MUTEX_INITIALIZER,
|
2008-12-09 13:24:37 +08:00
|
|
|
.prune_run_lock = PTHREAD_MUTEX_INITIALIZER,
|
2007-01-14 13:26:04 +08:00
|
|
|
.enabled = 0,
|
|
|
|
.check_file = 1,
|
|
|
|
.persistent = 0,
|
|
|
|
.propagate = 0, /* Not used. */
|
|
|
|
.shared = 0,
|
|
|
|
.max_db_size = DEFAULT_MAX_DB_SIZE,
|
2008-03-04 09:54:09 +08:00
|
|
|
.suggested_module = DEFAULT_SUGGESTED_MODULE,
|
2007-01-14 13:26:04 +08:00
|
|
|
.db_filename = _PATH_NSCD_SERVICES_DB,
|
|
|
|
.disabled_iov = &serv_iov_disabled,
|
|
|
|
.postimeout = 28800,
|
|
|
|
.negtimeout = 20,
|
|
|
|
.wr_fd = -1,
|
|
|
|
.ro_fd = -1,
|
|
|
|
.mmap_used = false
|
2011-10-07 22:06:31 +08:00
|
|
|
},
|
|
|
|
[netgrdb] = {
|
|
|
|
.lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP,
|
|
|
|
.prune_lock = PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
.prune_run_lock = PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
.enabled = 0,
|
|
|
|
.check_file = 1,
|
|
|
|
.persistent = 0,
|
|
|
|
.propagate = 0, /* Not used. */
|
|
|
|
.shared = 0,
|
|
|
|
.max_db_size = DEFAULT_MAX_DB_SIZE,
|
|
|
|
.suggested_module = DEFAULT_SUGGESTED_MODULE,
|
|
|
|
.db_filename = _PATH_NSCD_NETGROUP_DB,
|
|
|
|
.disabled_iov = &netgroup_iov_disabled,
|
|
|
|
.postimeout = 28800,
|
|
|
|
.negtimeout = 20,
|
|
|
|
.wr_fd = -1,
|
|
|
|
.ro_fd = -1,
|
|
|
|
.mmap_used = false
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
};
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* Mapping of request type to database. */
|
2007-01-16 06:31:14 +08:00
|
|
|
static struct
|
2004-08-27 02:35:05 +08:00
|
|
|
{
|
2007-01-16 06:31:14 +08:00
|
|
|
bool data_request;
|
|
|
|
struct database_dyn *db;
|
2007-01-16 09:27:56 +08:00
|
|
|
} const reqinfo[LASTREQ] =
|
2007-01-16 06:31:14 +08:00
|
|
|
{
|
|
|
|
[GETPWBYNAME] = { true, &dbs[pwddb] },
|
|
|
|
[GETPWBYUID] = { true, &dbs[pwddb] },
|
|
|
|
[GETGRBYNAME] = { true, &dbs[grpdb] },
|
|
|
|
[GETGRBYGID] = { true, &dbs[grpdb] },
|
|
|
|
[GETHOSTBYNAME] = { true, &dbs[hstdb] },
|
|
|
|
[GETHOSTBYNAMEv6] = { true, &dbs[hstdb] },
|
|
|
|
[GETHOSTBYADDR] = { true, &dbs[hstdb] },
|
|
|
|
[GETHOSTBYADDRv6] = { true, &dbs[hstdb] },
|
|
|
|
[SHUTDOWN] = { false, NULL },
|
|
|
|
[GETSTAT] = { false, NULL },
|
|
|
|
[SHUTDOWN] = { false, NULL },
|
|
|
|
[GETFDPW] = { false, &dbs[pwddb] },
|
|
|
|
[GETFDGR] = { false, &dbs[grpdb] },
|
|
|
|
[GETFDHST] = { false, &dbs[hstdb] },
|
|
|
|
[GETAI] = { true, &dbs[hstdb] },
|
|
|
|
[INITGROUPS] = { true, &dbs[grpdb] },
|
|
|
|
[GETSERVBYNAME] = { true, &dbs[servdb] },
|
|
|
|
[GETSERVBYPORT] = { true, &dbs[servdb] },
|
2011-10-07 22:06:31 +08:00
|
|
|
[GETFDSERV] = { false, &dbs[servdb] },
|
|
|
|
[GETNETGRENT] = { true, &dbs[netgrdb] },
|
|
|
|
[INNETGR] = { true, &dbs[netgrdb] },
|
|
|
|
[GETFDNETGR] = { false, &dbs[netgrdb] }
|
2004-08-27 02:35:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-10-04 05:11:37 +08:00
|
|
|
/* Initial number of threads to use. */
|
1998-10-18 23:16:22 +08:00
|
|
|
int nthreads = -1;
|
2004-10-04 05:11:37 +08:00
|
|
|
/* Maximum number of threads to use. */
|
|
|
|
int max_nthreads = 32;
|
1998-01-31 16:39:55 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Socket for incoming connections. */
|
|
|
|
static int sock;
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2008-06-13 06:40:27 +08:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
/* Inotify descriptor. */
|
2011-07-12 02:50:24 +08:00
|
|
|
int inotify_fd = -1;
|
2008-06-13 06:40:27 +08:00
|
|
|
#endif
|
|
|
|
|
2011-10-31 13:51:16 +08:00
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
/* Descriptor for netlink status updates. */
|
|
|
|
static int nl_status_fd = -1;
|
|
|
|
#endif
|
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
/* Number of times clients had to wait. */
|
|
|
|
unsigned long int client_queued;
|
|
|
|
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2005-02-23 06:58:32 +08:00
|
|
|
ssize_t
|
|
|
|
writeall (int fd, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t n = len;
|
|
|
|
ssize_t ret;
|
|
|
|
do
|
|
|
|
{
|
2005-08-24 07:21:53 +08:00
|
|
|
ret = TEMP_FAILURE_RETRY (send (fd, buf, n, MSG_NOSIGNAL));
|
2005-02-23 06:58:32 +08:00
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
buf = (const char *) buf + ret;
|
|
|
|
n -= ret;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return ret < 0 ? ret : len - n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-23 03:06:27 +08:00
|
|
|
#ifdef HAVE_SENDFILE
|
|
|
|
ssize_t
|
|
|
|
sendfileall (int tofd, int fromfd, off_t off, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t n = len;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = TEMP_FAILURE_RETRY (sendfile (tofd, fromfd, &off, n));
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
n -= ret;
|
|
|
|
}
|
|
|
|
while (n > 0);
|
|
|
|
return ret < 0 ? ret : len - n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
enum usekey
|
|
|
|
{
|
|
|
|
use_not = 0,
|
|
|
|
/* The following three are not really used, they are symbolic constants. */
|
|
|
|
use_first = 16,
|
|
|
|
use_begin = 32,
|
|
|
|
use_end = 64,
|
|
|
|
|
|
|
|
use_he = 1,
|
|
|
|
use_he_begin = use_he | use_begin,
|
|
|
|
use_he_end = use_he | use_end,
|
|
|
|
use_data = 3,
|
|
|
|
use_data_begin = use_data | use_begin,
|
|
|
|
use_data_end = use_data | use_end,
|
|
|
|
use_data_first = use_data_begin | use_first
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_use (const char *data, nscd_ssize_t first_free, uint8_t *usemap,
|
|
|
|
enum usekey use, ref_t start, size_t len)
|
|
|
|
{
|
|
|
|
assert (len >= 2);
|
|
|
|
|
|
|
|
if (start > first_free || start + len > first_free
|
|
|
|
|| (start & BLOCK_ALIGN_M1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (usemap[start] == use_not)
|
|
|
|
{
|
|
|
|
/* Add the start marker. */
|
|
|
|
usemap[start] = use | use_begin;
|
|
|
|
use &= ~use_first;
|
|
|
|
|
|
|
|
while (--len > 0)
|
|
|
|
if (usemap[++start] != use_not)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
usemap[start] = use;
|
|
|
|
|
|
|
|
/* Add the end marker. */
|
|
|
|
usemap[start] = use | use_end;
|
|
|
|
}
|
|
|
|
else if ((usemap[start] & ~use_first) == ((use | use_begin) & ~use_first))
|
|
|
|
{
|
|
|
|
/* Hash entries can't be shared. */
|
|
|
|
if (use == use_he)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
usemap[start] |= (use & use_first);
|
|
|
|
use &= ~use_first;
|
|
|
|
|
|
|
|
while (--len > 1)
|
|
|
|
if (usemap[++start] != use)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (usemap[++start] != (use | use_end))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Points to a wrong object or somewhere in the middle. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Verify data in persistent database. */
|
|
|
|
static int
|
|
|
|
verify_persistent_db (void *mem, struct database_pers_head *readhead, int dbnr)
|
|
|
|
{
|
2011-10-07 22:06:31 +08:00
|
|
|
assert (dbnr == pwddb || dbnr == grpdb || dbnr == hstdb || dbnr == servdb
|
|
|
|
|| dbnr == netgrdb);
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
|
|
|
|
time_t now = time (NULL);
|
|
|
|
|
|
|
|
struct database_pers_head *head = mem;
|
|
|
|
struct database_pers_head head_copy = *head;
|
|
|
|
|
|
|
|
/* Check that the header that was read matches the head in the database. */
|
2008-03-04 09:54:09 +08:00
|
|
|
if (memcmp (head, readhead, sizeof (*head)) != 0)
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* First some easy tests: make sure the database header is sane. */
|
|
|
|
if (head->version != DB_VERSION
|
|
|
|
|| head->header_size != sizeof (*head)
|
|
|
|
/* We allow a timestamp to be one hour ahead of the current time.
|
|
|
|
This should cover daylight saving time changes. */
|
|
|
|
|| head->timestamp > now + 60 * 60 + 60
|
|
|
|
|| (head->gc_cycle & 1)
|
2008-03-04 09:54:09 +08:00
|
|
|
|| head->module == 0
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
|| (size_t) head->module > INT32_MAX / sizeof (ref_t)
|
|
|
|
|| (size_t) head->data_size > INT32_MAX - head->module * sizeof (ref_t)
|
|
|
|
|| head->first_free < 0
|
|
|
|
|| head->first_free > head->data_size
|
|
|
|
|| (head->first_free & BLOCK_ALIGN_M1) != 0
|
|
|
|
|| head->maxnentries < 0
|
|
|
|
|| head->maxnsearched < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t *usemap = calloc (head->first_free, 1);
|
|
|
|
if (usemap == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const char *data = (char *) &head->array[roundup (head->module,
|
|
|
|
ALIGN / sizeof (ref_t))];
|
|
|
|
|
|
|
|
nscd_ssize_t he_cnt = 0;
|
|
|
|
for (nscd_ssize_t cnt = 0; cnt < head->module; ++cnt)
|
|
|
|
{
|
2007-10-07 13:31:00 +08:00
|
|
|
ref_t trail = head->array[cnt];
|
|
|
|
ref_t work = trail;
|
|
|
|
int tick = 0;
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
|
|
|
|
while (work != ENDREF)
|
|
|
|
{
|
|
|
|
if (! check_use (data, head->first_free, usemap, use_he, work,
|
|
|
|
sizeof (struct hashentry)))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Now we know we can dereference the record. */
|
|
|
|
struct hashentry *here = (struct hashentry *) (data + work);
|
|
|
|
|
|
|
|
++he_cnt;
|
|
|
|
|
|
|
|
/* Make sure the record is for this type of service. */
|
|
|
|
if (here->type >= LASTREQ
|
2007-01-16 09:27:56 +08:00
|
|
|
|| reqinfo[here->type].db != &dbs[dbnr])
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Validate boolean field value. */
|
|
|
|
if (here->first != false && here->first != true)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (here->len < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Now the data. */
|
|
|
|
if (here->packet < 0
|
|
|
|
|| here->packet > head->first_free
|
|
|
|
|| here->packet + sizeof (struct datahead) > head->first_free)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
struct datahead *dh = (struct datahead *) (data + here->packet);
|
|
|
|
|
|
|
|
if (! check_use (data, head->first_free, usemap,
|
|
|
|
use_data | (here->first ? use_first : 0),
|
|
|
|
here->packet, dh->allocsize))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (dh->allocsize < sizeof (struct datahead)
|
|
|
|
|| dh->recsize > dh->allocsize
|
|
|
|
|| (dh->notfound != false && dh->notfound != true)
|
|
|
|
|| (dh->usable != false && dh->usable != true))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (here->key < here->packet + sizeof (struct datahead)
|
|
|
|
|| here->key > here->packet + dh->allocsize
|
|
|
|
|| here->key + here->len > here->packet + dh->allocsize)
|
2014-06-26 16:40:07 +08:00
|
|
|
goto fail;
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
|
|
|
|
work = here->next;
|
2007-10-07 03:03:13 +08:00
|
|
|
|
2007-10-07 13:31:00 +08:00
|
|
|
if (work == trail)
|
2007-10-07 03:03:13 +08:00
|
|
|
/* A circular list, this must not happen. */
|
|
|
|
goto fail;
|
2007-10-07 13:31:00 +08:00
|
|
|
if (tick)
|
|
|
|
trail = ((struct hashentry *) (data + trail))->next;
|
|
|
|
tick = 1 - tick;
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (he_cnt != head->nentries)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* See if all data and keys had at least one reference from
|
|
|
|
he->first == true hashentry. */
|
|
|
|
for (ref_t idx = 0; idx < head->first_free; ++idx)
|
|
|
|
{
|
|
|
|
if (usemap[idx] == use_data_begin)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, make sure the database hasn't changed since the first test. */
|
|
|
|
if (memcmp (mem, &head_copy, sizeof (*head)) != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
free (usemap);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free (usemap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-23 04:10:10 +08:00
|
|
|
/* Initialize database information structures. */
|
|
|
|
void
|
|
|
|
nscd_init (void)
|
2007-07-13 02:26:36 +08:00
|
|
|
{
|
2002-01-18 10:10:41 +08:00
|
|
|
/* Look up unprivileged uid/gid/groups before we start listening on the
|
|
|
|
socket */
|
|
|
|
if (server_user != NULL)
|
|
|
|
begin_drop_privileges ();
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
if (nthreads == -1)
|
|
|
|
/* No configuration for this value, assume a default. */
|
2007-11-06 08:45:40 +08:00
|
|
|
nthreads = 4;
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2005-02-23 06:58:32 +08:00
|
|
|
for (size_t cnt = 0; cnt < lastdb; ++cnt)
|
1998-10-18 23:16:22 +08:00
|
|
|
if (dbs[cnt].enabled)
|
1998-06-28 01:16:24 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
pthread_rwlock_init (&dbs[cnt].lock, NULL);
|
2004-08-27 02:35:05 +08:00
|
|
|
pthread_mutex_init (&dbs[cnt].memlock, NULL);
|
1998-07-25 05:32:39 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
if (dbs[cnt].persistent)
|
2003-01-16 15:54:50 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
/* Try to open the appropriate file on disk. */
|
2017-04-18 20:56:51 +08:00
|
|
|
int fd = open (dbs[cnt].db_filename, O_RDWR | O_CLOEXEC);
|
2004-08-27 02:35:05 +08:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
2008-03-04 09:54:09 +08:00
|
|
|
char *msg = NULL;
|
2004-08-27 02:35:05 +08:00
|
|
|
struct stat64 st;
|
|
|
|
void *mem;
|
|
|
|
size_t total;
|
|
|
|
struct database_pers_head head;
|
|
|
|
ssize_t n = TEMP_FAILURE_RETRY (read (fd, &head,
|
|
|
|
sizeof (head)));
|
|
|
|
if (n != sizeof (head) || fstat64 (fd, &st) != 0)
|
|
|
|
{
|
2008-03-04 09:54:09 +08:00
|
|
|
fail_db_errno:
|
|
|
|
/* The code is single-threaded at this point so
|
|
|
|
using strerror is just fine. */
|
|
|
|
msg = strerror (errno);
|
2004-08-27 02:35:05 +08:00
|
|
|
fail_db:
|
|
|
|
dbg_log (_("invalid persistent database file \"%s\": %s"),
|
2008-03-04 09:54:09 +08:00
|
|
|
dbs[cnt].db_filename, msg);
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
unlink (dbs[cnt].db_filename);
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
else if (head.module == 0 && head.data_size == 0)
|
|
|
|
{
|
2008-03-04 09:54:09 +08:00
|
|
|
/* The file has been created, but the head has not
|
|
|
|
been initialized yet. */
|
|
|
|
msg = _("uninitialized header");
|
|
|
|
goto fail_db;
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
else if (head.header_size != (int) sizeof (head))
|
|
|
|
{
|
2008-03-04 09:54:09 +08:00
|
|
|
msg = _("header size does not match");
|
|
|
|
goto fail_db;
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
else if ((total = (sizeof (head)
|
2004-09-08 23:46:42 +08:00
|
|
|
+ roundup (head.module * sizeof (ref_t),
|
2004-08-27 02:35:05 +08:00
|
|
|
ALIGN)
|
|
|
|
+ head.data_size))
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
> st.st_size
|
|
|
|
|| total < sizeof (head))
|
2004-08-27 02:35:05 +08:00
|
|
|
{
|
2008-03-04 09:54:09 +08:00
|
|
|
msg = _("file size does not match");
|
|
|
|
goto fail_db;
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
2005-08-24 07:21:53 +08:00
|
|
|
/* Note we map with the maximum size allowed for the
|
|
|
|
database. This is likely much larger than the
|
|
|
|
actual file size. This is OK on most OSes since
|
|
|
|
extensions of the underlying file will
|
|
|
|
automatically translate more pages available for
|
|
|
|
memory access. */
|
|
|
|
else if ((mem = mmap (NULL, dbs[cnt].max_db_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0))
|
|
|
|
== MAP_FAILED)
|
2008-03-04 09:54:09 +08:00
|
|
|
goto fail_db_errno;
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
else if (!verify_persistent_db (mem, &head, cnt))
|
|
|
|
{
|
|
|
|
munmap (mem, total);
|
2008-03-04 09:54:09 +08:00
|
|
|
msg = _("verification failed");
|
|
|
|
goto fail_db;
|
* posix/bits/unistd.h (confstr, getgroups, ttyname_r, gethostname,
getdomainname): Add __NTH.
* stdlib/bits/stdlib.h (ptsname_r, wctomb, mbstowcs, wcstombs):
Likewise.
(realpath): Likewise. Use __const instead of const. Add __restrict
keywords.
* socket/bits/socket2.h (recvfrom): Add __restrict keyword to __buf.
* wcsmbs/bits/wchar2.h (wmemcpy, wmemmove, wmempcpy, wmemset,
wcscpy, wcpcpy, wcsncpy, wcpncpy, wcscat, wcsncat, vswprintf, wcrtomb,
mbsrtowcs, wcsrtombs, mbsnrtowcs, wcsnrtombs): Add __NTH.
* string/bits/string3.h (__memset_ichk): Likewise.
(__memcpy_ichk, __memmove_ichk, __mempcpy_ichk, __strcpy_ichk,
__stpcpy_ichk, __strncpy_ichk, stpncpy, __strcat_ichk,
__strncat_ichk): Likewise. Use __const instead of const.
(__stpncpy_chk): Use __const instead of const.
(__stpncpy_alias): Use __REDIRECT_NTH instead of __REDIRECT.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
Jakub Jelinek <jakub@redhat.com>
* nscd/mem.c (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): Move
definitions to...
* nscd/nscd.h (BLOCK_ALIGN_LOG, BLOCK_ALIGN, BLOCK_ALIGN_M1): ...here.
* nscd/connections.c (usekey): New enum.
(check_use, verify_persistent_db): New functions.
(nscd_init): If persistent database is corrupted, unlink it and
recreate rather than falling back to non-persistent database.
Call verify_persistent_db. Avoid overflows in total computation.
2005-08-08 Ulrich Drepper <drepper@redhat.com>
* iconvdata/utf-16.c (PREPARE_LOOP): Minor cleanups to make code
better readable. Avoid passing var to loop function, it's not
necessary at all.
2005-08-09 03:04:11 +08:00
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Success. We have the database. */
|
|
|
|
dbs[cnt].head = mem;
|
|
|
|
dbs[cnt].memsize = total;
|
|
|
|
dbs[cnt].data = (char *)
|
|
|
|
&dbs[cnt].head->array[roundup (dbs[cnt].head->module,
|
|
|
|
ALIGN / sizeof (ref_t))];
|
|
|
|
dbs[cnt].mmap_used = true;
|
|
|
|
|
|
|
|
if (dbs[cnt].suggested_module > head.module)
|
|
|
|
dbg_log (_("suggested size of table for database %s larger than the persistent database's table"),
|
|
|
|
dbnames[cnt]);
|
|
|
|
|
|
|
|
dbs[cnt].wr_fd = fd;
|
|
|
|
fd = -1;
|
|
|
|
/* We also need a read-only descriptor. */
|
2004-09-11 04:31:41 +08:00
|
|
|
if (dbs[cnt].shared)
|
|
|
|
{
|
2007-08-11 10:48:28 +08:00
|
|
|
dbs[cnt].ro_fd = open (dbs[cnt].db_filename,
|
2017-04-18 20:56:51 +08:00
|
|
|
O_RDONLY | O_CLOEXEC);
|
2004-09-11 04:31:41 +08:00
|
|
|
if (dbs[cnt].ro_fd == -1)
|
|
|
|
dbg_log (_("\
|
2004-08-27 02:35:05 +08:00
|
|
|
cannot create read-only descriptor for \"%s\"; no mmap"),
|
2004-09-11 04:31:41 +08:00
|
|
|
dbs[cnt].db_filename);
|
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
// XXX Shall we test whether the descriptors actually
|
|
|
|
// XXX point to the same file?
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the file descriptors in case something went
|
|
|
|
wrong in which case the variable have not been
|
|
|
|
assigned -1. */
|
|
|
|
if (fd != -1)
|
|
|
|
close (fd);
|
|
|
|
}
|
2009-01-29 05:00:13 +08:00
|
|
|
else if (errno == EACCES)
|
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-04 01:21:39 +08:00
|
|
|
do_exit (EXIT_FAILURE, 0, _("cannot access '%s'"),
|
|
|
|
dbs[cnt].db_filename);
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dbs[cnt].head == NULL)
|
|
|
|
{
|
|
|
|
/* No database loaded. Allocate the data structure,
|
|
|
|
possibly on disk. */
|
|
|
|
struct database_pers_head head;
|
|
|
|
size_t total = (sizeof (head)
|
|
|
|
+ roundup (dbs[cnt].suggested_module
|
|
|
|
* sizeof (ref_t), ALIGN)
|
|
|
|
+ (dbs[cnt].suggested_module
|
|
|
|
* DEFAULT_DATASIZE_PER_BUCKET));
|
|
|
|
|
|
|
|
/* Try to create the database. If we do not need a
|
|
|
|
persistent database create a temporary file. */
|
|
|
|
int fd;
|
|
|
|
int ro_fd = -1;
|
|
|
|
if (dbs[cnt].persistent)
|
|
|
|
{
|
|
|
|
fd = open (dbs[cnt].db_filename,
|
2017-04-18 20:56:51 +08:00
|
|
|
O_RDWR | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC,
|
2004-08-27 02:35:05 +08:00
|
|
|
S_IRUSR | S_IWUSR);
|
2004-09-11 04:31:41 +08:00
|
|
|
if (fd != -1 && dbs[cnt].shared)
|
2007-08-11 10:48:28 +08:00
|
|
|
ro_fd = open (dbs[cnt].db_filename,
|
2017-04-18 20:56:51 +08:00
|
|
|
O_RDONLY | O_CLOEXEC);
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-10-16 05:12:15 +08:00
|
|
|
char fname[] = _PATH_NSCD_XYZ_DB_TMP;
|
2017-04-18 20:56:51 +08:00
|
|
|
fd = mkostemp (fname, O_CLOEXEC);
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* We do not need the file name anymore after we
|
|
|
|
opened another file descriptor in read-only mode. */
|
2004-10-15 00:18:09 +08:00
|
|
|
if (fd != -1)
|
2004-08-27 02:35:05 +08:00
|
|
|
{
|
2004-10-15 00:18:09 +08:00
|
|
|
if (dbs[cnt].shared)
|
2017-04-18 20:56:51 +08:00
|
|
|
ro_fd = open (fname, O_RDONLY | O_CLOEXEC);
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
unlink (fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
|
|
|
dbg_log (_("database for %s corrupted or simultaneously used; remove %s manually if necessary and restart"),
|
|
|
|
dbnames[cnt], dbs[cnt].db_filename);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (1, 0, NULL);
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dbs[cnt].persistent)
|
|
|
|
dbg_log (_("cannot create %s; no persistent database used"),
|
|
|
|
dbs[cnt].db_filename);
|
|
|
|
else
|
|
|
|
dbg_log (_("cannot create %s; no sharing possible"),
|
|
|
|
dbs[cnt].db_filename);
|
|
|
|
|
|
|
|
dbs[cnt].persistent = 0;
|
|
|
|
// XXX remember: no mmap
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Tell the user if we could not create the read-only
|
|
|
|
descriptor. */
|
2004-09-11 04:31:41 +08:00
|
|
|
if (ro_fd == -1 && dbs[cnt].shared)
|
2004-08-27 02:35:05 +08:00
|
|
|
dbg_log (_("\
|
|
|
|
cannot create read-only descriptor for \"%s\"; no mmap"),
|
|
|
|
dbs[cnt].db_filename);
|
|
|
|
|
2013-06-24 20:37:37 +08:00
|
|
|
/* Before we create the header, initialize the hash
|
|
|
|
table. That way if we get interrupted while writing
|
2004-08-27 02:35:05 +08:00
|
|
|
the header we can recognize a partially initialized
|
|
|
|
database. */
|
|
|
|
size_t ps = sysconf (_SC_PAGESIZE);
|
|
|
|
char tmpbuf[ps];
|
|
|
|
assert (~ENDREF == 0);
|
|
|
|
memset (tmpbuf, '\xff', ps);
|
|
|
|
|
|
|
|
size_t remaining = dbs[cnt].suggested_module * sizeof (ref_t);
|
|
|
|
off_t offset = sizeof (head);
|
|
|
|
|
|
|
|
size_t towrite;
|
|
|
|
if (offset % ps != 0)
|
|
|
|
{
|
|
|
|
towrite = MIN (remaining, ps - (offset % ps));
|
2005-07-19 06:28:46 +08:00
|
|
|
if (pwrite (fd, tmpbuf, towrite, offset) != towrite)
|
|
|
|
goto write_fail;
|
2004-08-27 02:35:05 +08:00
|
|
|
offset += towrite;
|
|
|
|
remaining -= towrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (remaining > ps)
|
|
|
|
{
|
2005-07-19 06:28:46 +08:00
|
|
|
if (pwrite (fd, tmpbuf, ps, offset) == -1)
|
|
|
|
goto write_fail;
|
2004-08-27 02:35:05 +08:00
|
|
|
offset += ps;
|
|
|
|
remaining -= ps;
|
|
|
|
}
|
|
|
|
|
2005-07-19 06:28:46 +08:00
|
|
|
if (remaining > 0
|
|
|
|
&& pwrite (fd, tmpbuf, remaining, offset) != remaining)
|
|
|
|
goto write_fail;
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* Create the header of the file. */
|
|
|
|
struct database_pers_head head =
|
|
|
|
{
|
|
|
|
.version = DB_VERSION,
|
|
|
|
.header_size = sizeof (head),
|
|
|
|
.module = dbs[cnt].suggested_module,
|
|
|
|
.data_size = (dbs[cnt].suggested_module
|
|
|
|
* DEFAULT_DATASIZE_PER_BUCKET),
|
|
|
|
.first_free = 0
|
|
|
|
};
|
|
|
|
void *mem;
|
|
|
|
|
|
|
|
if ((TEMP_FAILURE_RETRY (write (fd, &head, sizeof (head)))
|
|
|
|
!= sizeof (head))
|
2005-08-24 07:21:53 +08:00
|
|
|
|| (TEMP_FAILURE_RETRY_VAL (posix_fallocate (fd, 0, total))
|
|
|
|
!= 0)
|
|
|
|
|| (mem = mmap (NULL, dbs[cnt].max_db_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
2004-08-27 02:35:05 +08:00
|
|
|
MAP_SHARED, fd, 0)) == MAP_FAILED)
|
|
|
|
{
|
2005-07-19 06:28:46 +08:00
|
|
|
write_fail:
|
2004-08-27 02:35:05 +08:00
|
|
|
unlink (dbs[cnt].db_filename);
|
|
|
|
dbg_log (_("cannot write to database file %s: %s"),
|
|
|
|
dbs[cnt].db_filename, strerror (errno));
|
|
|
|
dbs[cnt].persistent = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Success. */
|
|
|
|
dbs[cnt].head = mem;
|
|
|
|
dbs[cnt].data = (char *)
|
|
|
|
&dbs[cnt].head->array[roundup (dbs[cnt].head->module,
|
|
|
|
ALIGN / sizeof (ref_t))];
|
|
|
|
dbs[cnt].memsize = total;
|
|
|
|
dbs[cnt].mmap_used = true;
|
|
|
|
|
|
|
|
/* Remember the descriptors. */
|
|
|
|
dbs[cnt].wr_fd = fd;
|
|
|
|
dbs[cnt].ro_fd = ro_fd;
|
|
|
|
fd = -1;
|
|
|
|
ro_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd != -1)
|
|
|
|
close (fd);
|
|
|
|
if (ro_fd != -1)
|
|
|
|
close (ro_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbs[cnt].head == NULL)
|
|
|
|
{
|
|
|
|
/* We do not use the persistent database. Just
|
|
|
|
create an in-memory data structure. */
|
|
|
|
assert (! dbs[cnt].persistent);
|
|
|
|
|
|
|
|
dbs[cnt].head = xmalloc (sizeof (struct database_pers_head)
|
|
|
|
+ (dbs[cnt].suggested_module
|
|
|
|
* sizeof (ref_t)));
|
2006-05-03 00:45:25 +08:00
|
|
|
memset (dbs[cnt].head, '\0', sizeof (struct database_pers_head));
|
2004-08-27 02:35:05 +08:00
|
|
|
assert (~ENDREF == 0);
|
|
|
|
memset (dbs[cnt].head->array, '\xff',
|
|
|
|
dbs[cnt].suggested_module * sizeof (ref_t));
|
|
|
|
dbs[cnt].head->module = dbs[cnt].suggested_module;
|
|
|
|
dbs[cnt].head->data_size = (DEFAULT_DATASIZE_PER_BUCKET
|
|
|
|
* dbs[cnt].head->module);
|
|
|
|
dbs[cnt].data = xmalloc (dbs[cnt].head->data_size);
|
|
|
|
dbs[cnt].head->first_free = 0;
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
dbs[cnt].shared = 0;
|
|
|
|
assert (dbs[cnt].ro_fd == -1);
|
2003-01-16 15:54:50 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
|
|
|
|
/* Create the socket. */
|
2015-10-17 18:02:37 +08:00
|
|
|
sock = socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
1998-10-18 23:16:22 +08:00
|
|
|
if (sock < 0)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
dbg_log (_("cannot open socket: %s"), strerror (errno));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (errno == EACCES ? 4 : 1, 0, NULL);
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
|
|
|
/* Bind a name to the socket. */
|
2005-02-23 06:58:32 +08:00
|
|
|
struct sockaddr_un sock_addr;
|
1998-01-31 16:39:55 +08:00
|
|
|
sock_addr.sun_family = AF_UNIX;
|
|
|
|
strcpy (sock_addr.sun_path, _PATH_NSCDSOCKET);
|
1998-10-18 23:16:22 +08:00
|
|
|
if (bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr)) < 0)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
|
|
|
dbg_log ("%s: %s", _PATH_NSCDSOCKET, strerror (errno));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (errno == EACCES ? 4 : 1, 0, NULL);
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
1998-01-31 16:39:55 +08:00
|
|
|
/* Set permissions for the socket. */
|
2004-08-27 02:35:05 +08:00
|
|
|
chmod (_PATH_NSCDSOCKET, DEFFILEMODE);
|
1998-01-31 16:39:55 +08:00
|
|
|
|
|
|
|
/* Set the socket up to accept connections. */
|
1998-10-18 23:16:22 +08:00
|
|
|
if (listen (sock, SOMAXCONN) < 0)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
dbg_log (_("cannot enable socket to accept connections: %s"),
|
|
|
|
strerror (errno));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (1, 0, NULL);
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
2002-01-18 10:10:41 +08:00
|
|
|
|
2011-10-31 13:51:16 +08:00
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
if (dbs[hstdb].enabled)
|
|
|
|
{
|
|
|
|
/* Try to open netlink socket to monitor network setting changes. */
|
|
|
|
nl_status_fd = socket (AF_NETLINK,
|
|
|
|
SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
|
|
|
NETLINK_ROUTE);
|
|
|
|
if (nl_status_fd != -1)
|
|
|
|
{
|
|
|
|
struct sockaddr_nl snl;
|
|
|
|
memset (&snl, '\0', sizeof (snl));
|
|
|
|
snl.nl_family = AF_NETLINK;
|
|
|
|
/* XXX Is this the best set to use? */
|
|
|
|
snl.nl_groups = (RTMGRP_IPV4_IFADDR | RTMGRP_TC | RTMGRP_IPV4_MROUTE
|
|
|
|
| RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_RULE
|
|
|
|
| RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_MROUTE
|
|
|
|
| RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFINFO
|
|
|
|
| RTMGRP_IPV6_PREFIX);
|
|
|
|
|
|
|
|
if (bind (nl_status_fd, (struct sockaddr *) &snl, sizeof (snl)) != 0)
|
|
|
|
{
|
|
|
|
close (nl_status_fd);
|
|
|
|
nl_status_fd = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Start the timestamp process. */
|
|
|
|
dbs[hstdb].head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP]
|
|
|
|
= __bump_nl_timestamp ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-12 02:50:24 +08:00
|
|
|
/* Change to unprivileged uid/gid/groups if specified in config file */
|
2002-01-18 10:10:41 +08:00
|
|
|
if (server_user != NULL)
|
|
|
|
finish_drop_privileges ();
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 21:49:24 +08:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
#define TRACED_FILE_MASK (IN_DELETE_SELF | IN_CLOSE_WRITE | IN_MOVE_SELF)
|
|
|
|
#define TRACED_DIR_MASK (IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MOVE_SELF)
|
|
|
|
void
|
|
|
|
install_watches (struct traced_file *finfo)
|
|
|
|
{
|
|
|
|
/* Use inotify support if we have it. */
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] < 0)
|
|
|
|
finfo->inotify_descr[TRACED_FILE] = inotify_add_watch (inotify_fd,
|
|
|
|
finfo->fname,
|
|
|
|
TRACED_FILE_MASK);
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] < 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("disabled inotify-based monitoring for file `%s': %s"),
|
|
|
|
finfo->fname, strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbg_log (_("monitoring file `%s` (%d)"),
|
|
|
|
finfo->fname, finfo->inotify_descr[TRACED_FILE]);
|
|
|
|
/* Additionally listen for events in the file's parent directory.
|
|
|
|
We do this because the file to be watched might be
|
|
|
|
deleted and then added back again. When it is added back again
|
|
|
|
we must re-add the watch. We must also cover IN_MOVED_TO to
|
|
|
|
detect a file being moved into the directory. */
|
|
|
|
if (finfo->inotify_descr[TRACED_DIR] < 0)
|
|
|
|
finfo->inotify_descr[TRACED_DIR] = inotify_add_watch (inotify_fd,
|
|
|
|
finfo->dname,
|
|
|
|
TRACED_DIR_MASK);
|
|
|
|
if (finfo->inotify_descr[TRACED_DIR] < 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("disabled inotify-based monitoring for directory `%s': %s"),
|
|
|
|
finfo->fname, strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbg_log (_("monitoring directory `%s` (%d)"),
|
|
|
|
finfo->dname, finfo->inotify_descr[TRACED_DIR]);
|
|
|
|
}
|
|
|
|
#endif
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2013-02-08 03:28:53 +08:00
|
|
|
/* Register the file in FINFO as a traced file for the database DBS[DBIX].
|
|
|
|
|
|
|
|
We support registering multiple files per database. Each call to
|
|
|
|
register_traced_file adds to the list of registered files.
|
|
|
|
|
|
|
|
When we prune the database, either through timeout or a request to
|
|
|
|
invalidate, we will check to see if any of the registered files has changed.
|
|
|
|
When we accept new connections to handle a cache request we will also
|
|
|
|
check to see if any of the registered files has changed.
|
|
|
|
|
|
|
|
If we have inotify support then we install an inotify fd to notify us of
|
|
|
|
file deletion or modification, both of which will require we invalidate
|
|
|
|
the cache for the database. Without inotify support we stat the file and
|
|
|
|
store st_mtime to determine if the file has been modified. */
|
2011-07-12 02:50:24 +08:00
|
|
|
void
|
|
|
|
register_traced_file (size_t dbidx, struct traced_file *finfo)
|
|
|
|
{
|
2013-02-08 03:28:53 +08:00
|
|
|
/* If the database is disabled or file checking is disabled
|
|
|
|
then ignore the registration. */
|
2011-10-07 13:20:53 +08:00
|
|
|
if (! dbs[dbidx].enabled || ! dbs[dbidx].check_file)
|
2011-07-12 02:50:24 +08:00
|
|
|
return;
|
|
|
|
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (debug_level > 0))
|
2015-03-13 21:49:24 +08:00
|
|
|
dbg_log (_("monitoring file %s for database %s"),
|
2011-07-12 02:50:24 +08:00
|
|
|
finfo->fname, dbnames[dbidx]);
|
|
|
|
|
|
|
|
#ifdef HAVE_INOTIFY
|
2015-03-13 21:49:24 +08:00
|
|
|
install_watches (finfo);
|
2011-07-12 02:50:24 +08:00
|
|
|
#endif
|
2015-03-13 21:49:24 +08:00
|
|
|
struct stat64 st;
|
|
|
|
if (stat64 (finfo->fname, &st) < 0)
|
2011-07-12 02:50:24 +08:00
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
/* We cannot stat() the file. Set mtime to zero and try again later. */
|
|
|
|
dbg_log (_("stat failed for file `%s'; will try again later: %s"),
|
|
|
|
finfo->fname, strerror (errno));
|
|
|
|
finfo->mtime = 0;
|
2011-07-12 02:50:24 +08:00
|
|
|
}
|
2015-03-13 21:49:24 +08:00
|
|
|
else
|
|
|
|
finfo->mtime = st.st_mtime;
|
2011-07-12 02:50:24 +08:00
|
|
|
|
|
|
|
/* Queue up the file name. */
|
|
|
|
finfo->next = dbs[dbidx].traced_files;
|
|
|
|
dbs[dbidx].traced_files = finfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Close the connections. */
|
1998-01-31 16:39:55 +08:00
|
|
|
void
|
1998-10-18 23:16:22 +08:00
|
|
|
close_sockets (void)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
close (sock);
|
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2003-05-04 15:00:44 +08:00
|
|
|
|
1999-09-27 08:22:04 +08:00
|
|
|
static void
|
2006-05-31 01:32:08 +08:00
|
|
|
invalidate_cache (char *key, int fd)
|
1999-09-27 08:22:04 +08:00
|
|
|
{
|
|
|
|
dbtype number;
|
2006-05-31 01:32:08 +08:00
|
|
|
int32_t resp;
|
1999-09-27 08:22:04 +08:00
|
|
|
|
2007-01-14 13:26:04 +08:00
|
|
|
for (number = pwddb; number < lastdb; ++number)
|
|
|
|
if (strcmp (key, dbnames[number]) == 0)
|
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
struct traced_file *runp = dbs[number].traced_files;
|
|
|
|
while (runp != NULL)
|
2011-07-12 02:50:24 +08:00
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
/* Make sure we reload from file when checking mtime. */
|
|
|
|
runp->mtime = 0;
|
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
/* During an invalidation we try to reload the traced
|
|
|
|
file watches. This allows the user to re-sync if
|
|
|
|
inotify events were lost. Similar to what we do during
|
|
|
|
pruning. */
|
|
|
|
install_watches (runp);
|
|
|
|
#endif
|
|
|
|
if (runp->call_res_init)
|
|
|
|
{
|
|
|
|
res_init ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
runp = runp->next;
|
2011-07-12 02:50:24 +08:00
|
|
|
}
|
2007-01-14 13:26:04 +08:00
|
|
|
break;
|
2015-03-13 21:49:24 +08:00
|
|
|
}
|
2007-01-14 13:26:04 +08:00
|
|
|
|
|
|
|
if (number == lastdb)
|
2006-05-31 01:32:08 +08:00
|
|
|
{
|
|
|
|
resp = EINVAL;
|
|
|
|
writeall (fd, &resp, sizeof (resp));
|
|
|
|
return;
|
|
|
|
}
|
1999-09-27 08:22:04 +08:00
|
|
|
|
2000-10-24 01:11:35 +08:00
|
|
|
if (dbs[number].enabled)
|
2007-11-06 08:45:40 +08:00
|
|
|
{
|
2008-12-09 13:24:37 +08:00
|
|
|
pthread_mutex_lock (&dbs[number].prune_run_lock);
|
2007-11-06 08:45:40 +08:00
|
|
|
prune_cache (&dbs[number], LONG_MAX, fd);
|
2008-12-09 13:24:37 +08:00
|
|
|
pthread_mutex_unlock (&dbs[number].prune_run_lock);
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
2006-05-31 01:32:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
resp = 0;
|
|
|
|
writeall (fd, &resp, sizeof (resp));
|
|
|
|
}
|
1999-09-27 08:22:04 +08:00
|
|
|
}
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
#ifdef SCM_RIGHTS
|
|
|
|
static void
|
|
|
|
send_ro_fd (struct database_dyn *db, char *key, int fd)
|
|
|
|
{
|
|
|
|
/* If we do not have an read-only file descriptor do nothing. */
|
|
|
|
if (db->ro_fd == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* We need to send some data along with the descriptor. */
|
2007-08-29 14:10:07 +08:00
|
|
|
uint64_t mapsize = (db->head->data_size
|
|
|
|
+ roundup (db->head->module * sizeof (ref_t), ALIGN)
|
|
|
|
+ sizeof (struct database_pers_head));
|
|
|
|
struct iovec iov[2];
|
2004-09-08 23:46:42 +08:00
|
|
|
iov[0].iov_base = key;
|
|
|
|
iov[0].iov_len = strlen (key) + 1;
|
2007-08-29 14:10:07 +08:00
|
|
|
iov[1].iov_base = &mapsize;
|
|
|
|
iov[1].iov_len = sizeof (mapsize);
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
/* Prepare the control message to transfer the descriptor. */
|
2005-07-14 10:17:58 +08:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct cmsghdr hdr;
|
|
|
|
char bytes[CMSG_SPACE (sizeof (int))];
|
|
|
|
} buf;
|
2007-08-29 14:10:07 +08:00
|
|
|
struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 2,
|
2005-07-14 10:17:58 +08:00
|
|
|
.msg_control = buf.bytes,
|
|
|
|
.msg_controllen = sizeof (buf) };
|
2004-09-08 23:46:42 +08:00
|
|
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
|
|
|
|
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN (sizeof (int));
|
|
|
|
|
2009-04-27 06:31:19 +08:00
|
|
|
int *ip = (int *) CMSG_DATA (cmsg);
|
|
|
|
*ip = db->ro_fd;
|
2004-09-08 23:46:42 +08:00
|
|
|
|
|
|
|
msg.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
|
|
|
/* Send the control message. We repeat when we are interrupted but
|
|
|
|
everything else is ignored. */
|
2005-01-31 04:34:21 +08:00
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
# define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
(void) TEMP_FAILURE_RETRY (sendmsg (fd, &msg, MSG_NOSIGNAL));
|
2004-09-08 23:46:42 +08:00
|
|
|
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (debug_level > 0))
|
2004-09-08 23:46:42 +08:00
|
|
|
dbg_log (_("provide access to FD %d, for %s"), db->ro_fd, key);
|
|
|
|
}
|
|
|
|
#endif /* SCM_RIGHTS */
|
|
|
|
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Handle new request. */
|
|
|
|
static void
|
2008-04-20 00:42:41 +08:00
|
|
|
handle_request (int fd, request_header *req, void *key, uid_t uid, pid_t pid)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2001-07-17 10:38:34 +08:00
|
|
|
if (__builtin_expect (req->version, NSCD_VERSION) != NSCD_VERSION)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
2001-07-17 09:37:42 +08:00
|
|
|
if (debug_level > 0)
|
|
|
|
dbg_log (_("\
|
1998-10-18 23:16:22 +08:00
|
|
|
cannot handle old request version %d; current version is %d"),
|
2001-07-17 09:37:42 +08:00
|
|
|
req->version, NSCD_VERSION);
|
1998-01-31 16:39:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Perform the SELinux check before we go on to the standard checks. */
|
2007-01-16 09:27:56 +08:00
|
|
|
if (selinux_enabled && nscd_request_avc_has_perm (fd, req->type) != 0)
|
2007-11-06 08:45:40 +08:00
|
|
|
{
|
|
|
|
if (debug_level > 0)
|
2008-04-20 00:42:41 +08:00
|
|
|
{
|
|
|
|
#ifdef SO_PEERCRED
|
|
|
|
# ifdef PATH_MAX
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
# else
|
|
|
|
char buf[4096];
|
|
|
|
# endif
|
|
|
|
|
|
|
|
snprintf (buf, sizeof (buf), "/proc/%ld/exe", (long int) pid);
|
|
|
|
ssize_t n = readlink (buf, buf, sizeof (buf) - 1);
|
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
dbg_log (_("\
|
|
|
|
request from %ld not handled due to missing permission"), (long int) pid);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[n] = '\0';
|
|
|
|
dbg_log (_("\
|
|
|
|
request from '%s' [%ld] not handled due to missing permission"),
|
|
|
|
buf, (long int) pid);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
dbg_log (_("request not handled due to missing permission"));
|
|
|
|
#endif
|
|
|
|
}
|
2007-11-06 08:45:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2004-09-17 08:04:18 +08:00
|
|
|
|
2007-01-16 09:27:56 +08:00
|
|
|
struct database_dyn *db = reqinfo[req->type].db;
|
2004-08-27 02:35:05 +08:00
|
|
|
|
2007-01-16 06:31:14 +08:00
|
|
|
/* See whether we can service the request from the cache. */
|
2007-01-16 09:27:56 +08:00
|
|
|
if (__builtin_expect (reqinfo[req->type].data_request, true))
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
2001-07-17 10:38:34 +08:00
|
|
|
if (__builtin_expect (debug_level, 0) > 0)
|
1999-02-07 08:06:12 +08:00
|
|
|
{
|
|
|
|
if (req->type == GETHOSTBYADDR || req->type == GETHOSTBYADDRv6)
|
|
|
|
{
|
|
|
|
char buf[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
dbg_log ("\t%s (%s)", serv2str[req->type],
|
|
|
|
inet_ntop (req->type == GETHOSTBYADDR
|
|
|
|
? AF_INET : AF_INET6,
|
|
|
|
key, buf, sizeof (buf)));
|
|
|
|
}
|
|
|
|
else
|
2004-08-27 02:35:05 +08:00
|
|
|
dbg_log ("\t%s (%s)", serv2str[req->type], (char *) key);
|
1999-02-07 08:06:12 +08:00
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Is this service enabled? */
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (!db->enabled))
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
1998-10-19 23:22:14 +08:00
|
|
|
/* No, sent the prepared record. */
|
2005-08-24 07:21:53 +08:00
|
|
|
if (TEMP_FAILURE_RETRY (send (fd, db->disabled_iov->iov_base,
|
|
|
|
db->disabled_iov->iov_len,
|
|
|
|
MSG_NOSIGNAL))
|
2003-04-23 03:52:59 +08:00
|
|
|
!= (ssize_t) db->disabled_iov->iov_len
|
2001-07-17 10:38:34 +08:00
|
|
|
&& __builtin_expect (debug_level, 0) > 0)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
/* We have problems sending the result. */
|
|
|
|
char buf[256];
|
|
|
|
dbg_log (_("cannot write result: %s"),
|
|
|
|
strerror_r (errno, buf, sizeof (buf)));
|
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
return;
|
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Be sure we can read the data. */
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (pthread_rwlock_tryrdlock (&db->lock) != 0))
|
2003-04-26 12:15:50 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
++db->head->rdlockdelayed;
|
2003-04-26 12:15:50 +08:00
|
|
|
pthread_rwlock_rdlock (&db->lock);
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* See whether we can handle it from the cache. */
|
2004-08-27 02:35:05 +08:00
|
|
|
struct datahead *cached;
|
|
|
|
cached = (struct datahead *) cache_search (req->type, key, req->key_len,
|
|
|
|
db, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
if (cached != NULL)
|
|
|
|
{
|
|
|
|
/* Hurray it's in the cache. */
|
2005-11-20 01:22:39 +08:00
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
#ifdef HAVE_SENDFILE
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_likely (db->mmap_used))
|
2005-11-20 01:22:39 +08:00
|
|
|
{
|
|
|
|
assert (db->wr_fd != -1);
|
|
|
|
assert ((char *) cached->data > (char *) db->data);
|
|
|
|
assert ((char *) cached->data - (char *) db->head
|
|
|
|
+ cached->recsize
|
|
|
|
<= (sizeof (struct database_pers_head)
|
|
|
|
+ db->head->module * sizeof (ref_t)
|
|
|
|
+ db->head->data_size));
|
2005-11-23 03:06:27 +08:00
|
|
|
nwritten = sendfileall (fd, db->wr_fd,
|
|
|
|
(char *) cached->data
|
|
|
|
- (char *) db->head, cached->recsize);
|
2005-11-20 01:22:39 +08:00
|
|
|
# ifndef __ASSUME_SENDFILE
|
|
|
|
if (nwritten == -1 && errno == ENOSYS)
|
|
|
|
goto use_write;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
# ifndef __ASSUME_SENDFILE
|
|
|
|
use_write:
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
nwritten = writeall (fd, cached->data, cached->recsize);
|
|
|
|
|
|
|
|
if (nwritten != cached->recsize
|
2001-07-17 10:38:34 +08:00
|
|
|
&& __builtin_expect (debug_level, 0) > 0)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
/* We have problems sending the result. */
|
|
|
|
char buf[256];
|
|
|
|
dbg_log (_("cannot write result: %s"),
|
|
|
|
strerror_r (errno, buf, sizeof (buf)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_unlock (&db->lock);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_unlock (&db->lock);
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
2001-07-17 10:38:34 +08:00
|
|
|
else if (__builtin_expect (debug_level, 0) > 0)
|
1999-09-27 08:22:04 +08:00
|
|
|
{
|
|
|
|
if (req->type == INVALIDATE)
|
2004-09-08 23:46:42 +08:00
|
|
|
dbg_log ("\t%s (%s)", serv2str[req->type], (char *) key);
|
2004-08-04 14:25:42 +08:00
|
|
|
else
|
2004-08-27 02:35:05 +08:00
|
|
|
dbg_log ("\t%s", serv2str[req->type]);
|
1999-09-27 08:22:04 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* Handle the request. */
|
|
|
|
switch (req->type)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
1998-10-18 23:16:22 +08:00
|
|
|
case GETPWBYNAME:
|
2004-08-27 02:35:05 +08:00
|
|
|
addpwbyname (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETPWBYUID:
|
2004-08-27 02:35:05 +08:00
|
|
|
addpwbyuid (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETGRBYNAME:
|
2004-08-27 02:35:05 +08:00
|
|
|
addgrbyname (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETGRBYGID:
|
2004-08-27 02:35:05 +08:00
|
|
|
addgrbygid (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYNAME:
|
2004-08-27 02:35:05 +08:00
|
|
|
addhstbyname (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYNAMEv6:
|
2004-08-27 02:35:05 +08:00
|
|
|
addhstbynamev6 (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYADDR:
|
2004-08-27 02:35:05 +08:00
|
|
|
addhstbyaddr (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYADDRv6:
|
2004-08-27 02:35:05 +08:00
|
|
|
addhstbyaddrv6 (db, fd, req, key, uid);
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
2004-09-15 16:25:49 +08:00
|
|
|
case GETAI:
|
|
|
|
addhstai (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
2004-09-30 10:24:51 +08:00
|
|
|
case INITGROUPS:
|
|
|
|
addinitgroups (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
2007-01-14 13:26:04 +08:00
|
|
|
case GETSERVBYNAME:
|
|
|
|
addservbyname (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETSERVBYPORT:
|
|
|
|
addservbyport (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
2011-10-07 22:06:31 +08:00
|
|
|
case GETNETGRENT:
|
|
|
|
addgetnetgrent (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INNETGR:
|
|
|
|
addinnetgr (db, fd, req, key, uid);
|
|
|
|
break;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
case GETSTAT:
|
|
|
|
case SHUTDOWN:
|
1999-09-27 08:22:04 +08:00
|
|
|
case INVALIDATE:
|
2005-12-31 00:51:19 +08:00
|
|
|
{
|
|
|
|
/* Get the callers credentials. */
|
2000-01-08 18:13:33 +08:00
|
|
|
#ifdef SO_PEERCRED
|
2005-12-31 00:51:19 +08:00
|
|
|
struct ucred caller;
|
|
|
|
socklen_t optlen = sizeof (caller);
|
2002-09-06 02:52:18 +08:00
|
|
|
|
2005-12-31 00:51:19 +08:00
|
|
|
if (getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &caller, &optlen) < 0)
|
|
|
|
{
|
|
|
|
char buf[256];
|
1999-06-12 04:58:21 +08:00
|
|
|
|
2006-09-21 12:04:20 +08:00
|
|
|
dbg_log (_("error getting caller's id: %s"),
|
2005-12-31 00:51:19 +08:00
|
|
|
strerror_r (errno, buf, sizeof (buf)));
|
|
|
|
break;
|
|
|
|
}
|
2003-05-04 15:00:44 +08:00
|
|
|
|
2005-12-31 00:51:19 +08:00
|
|
|
uid = caller.uid;
|
2003-05-04 15:00:44 +08:00
|
|
|
#else
|
2005-12-31 00:51:19 +08:00
|
|
|
/* Some systems have no SO_PEERCRED implementation. They don't
|
|
|
|
care about security so we don't as well. */
|
|
|
|
uid = 0;
|
2000-01-08 18:13:33 +08:00
|
|
|
#endif
|
2005-12-31 00:51:19 +08:00
|
|
|
}
|
2003-05-04 15:00:44 +08:00
|
|
|
|
|
|
|
/* Accept shutdown, getstat and invalidate only from root. For
|
|
|
|
the stat call also allow the user specified in the config file. */
|
|
|
|
if (req->type == GETSTAT)
|
|
|
|
{
|
|
|
|
if (uid == 0 || uid == stat_uid)
|
|
|
|
send_stats (fd, dbs);
|
|
|
|
}
|
|
|
|
else if (uid == 0)
|
|
|
|
{
|
|
|
|
if (req->type == INVALIDATE)
|
2006-05-31 01:32:08 +08:00
|
|
|
invalidate_cache (key, fd);
|
2003-05-04 15:00:44 +08:00
|
|
|
else
|
|
|
|
termination_handler (0);
|
1999-06-12 04:58:21 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
2004-09-08 23:46:42 +08:00
|
|
|
case GETFDPW:
|
|
|
|
case GETFDGR:
|
|
|
|
case GETFDHST:
|
2007-01-14 13:26:04 +08:00
|
|
|
case GETFDSERV:
|
2011-10-07 22:06:31 +08:00
|
|
|
case GETFDNETGR:
|
2004-09-08 23:46:42 +08:00
|
|
|
#ifdef SCM_RIGHTS
|
2007-01-16 09:27:56 +08:00
|
|
|
send_ro_fd (reqinfo[req->type].db, key, fd);
|
2004-09-08 23:46:42 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
default:
|
2000-02-19 01:25:16 +08:00
|
|
|
/* Ignore the command, it's nothing we know. */
|
|
|
|
break;
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2004-10-04 03:33:48 +08:00
|
|
|
/* Restart the process. */
|
|
|
|
static void
|
|
|
|
restart (void)
|
|
|
|
{
|
|
|
|
/* First determine the parameters. We do not use the parameters
|
|
|
|
passed to main() since in case nscd is started by running the
|
|
|
|
dynamic linker this will not work. Yes, this is not the usual
|
|
|
|
case but nscd is part of glibc and we occasionally do this. */
|
|
|
|
size_t buflen = 1024;
|
|
|
|
char *buf = alloca (buflen);
|
|
|
|
size_t readlen = 0;
|
|
|
|
int fd = open ("/proc/self/cmdline", O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("\
|
|
|
|
cannot open /proc/self/cmdline: %s; disabling paranoia mode"),
|
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
paranoia = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ssize_t n = TEMP_FAILURE_RETRY (read (fd, buf + readlen,
|
|
|
|
buflen - readlen));
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("\
|
2007-01-14 13:26:04 +08:00
|
|
|
cannot read /proc/self/cmdline: %s; disabling paranoia mode"),
|
2004-10-04 03:33:48 +08:00
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
paranoia = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
readlen += n;
|
|
|
|
|
|
|
|
if (readlen < buflen)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We might have to extend the buffer. */
|
|
|
|
size_t old_buflen = buflen;
|
|
|
|
char *newp = extend_alloca (buf, buflen, 2 * buflen);
|
|
|
|
buf = memmove (newp, buf, old_buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
/* Parse the command line. Worst case scenario: every two
|
|
|
|
characters form one parameter (one character plus NUL). */
|
|
|
|
char **argv = alloca ((readlen / 2 + 1) * sizeof (argv[0]));
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
char *cp = buf;
|
|
|
|
while (cp < buf + readlen)
|
|
|
|
{
|
|
|
|
argv[argc++] = cp;
|
|
|
|
cp = (char *) rawmemchr (cp, '\0') + 1;
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
/* Second, change back to the old user if we changed it. */
|
|
|
|
if (server_user != NULL)
|
|
|
|
{
|
2006-04-28 04:20:52 +08:00
|
|
|
if (setresuid (old_uid, old_uid, old_uid) != 0)
|
2004-10-04 03:33:48 +08:00
|
|
|
{
|
|
|
|
dbg_log (_("\
|
|
|
|
cannot change to old UID: %s; disabling paranoia mode"),
|
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
paranoia = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-28 04:20:52 +08:00
|
|
|
if (setresgid (old_gid, old_gid, old_gid) != 0)
|
2004-10-04 03:33:48 +08:00
|
|
|
{
|
|
|
|
dbg_log (_("\
|
|
|
|
cannot change to old GID: %s; disabling paranoia mode"),
|
|
|
|
strerror (errno));
|
|
|
|
|
2014-11-27 04:54:16 +08:00
|
|
|
ignore_value (setuid (server_uid));
|
2004-10-04 03:33:48 +08:00
|
|
|
paranoia = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next change back to the old working directory. */
|
|
|
|
if (chdir (oldcwd) == -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("\
|
|
|
|
cannot change to old working directory: %s; disabling paranoia mode"),
|
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
if (server_user != NULL)
|
|
|
|
{
|
2014-11-27 04:54:16 +08:00
|
|
|
ignore_value (setuid (server_uid));
|
|
|
|
ignore_value (setgid (server_gid));
|
2004-10-04 03:33:48 +08:00
|
|
|
}
|
|
|
|
paranoia = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Synchronize memory. */
|
2008-05-19 05:54:43 +08:00
|
|
|
int32_t certainly[lastdb];
|
2004-10-04 03:33:48 +08:00
|
|
|
for (int cnt = 0; cnt < lastdb; ++cnt)
|
2008-01-05 17:08:03 +08:00
|
|
|
if (dbs[cnt].enabled)
|
2007-10-06 05:12:46 +08:00
|
|
|
{
|
|
|
|
/* Make sure nobody keeps using the database. */
|
|
|
|
dbs[cnt].head->timestamp = 0;
|
2008-05-19 05:54:43 +08:00
|
|
|
certainly[cnt] = dbs[cnt].head->nscd_certainly_running;
|
|
|
|
dbs[cnt].head->nscd_certainly_running = 0;
|
2004-10-04 03:33:48 +08:00
|
|
|
|
2007-10-06 05:12:46 +08:00
|
|
|
if (dbs[cnt].persistent)
|
|
|
|
// XXX async OK?
|
|
|
|
msync (dbs[cnt].head, dbs[cnt].memsize, MS_ASYNC);
|
|
|
|
}
|
2004-10-04 03:33:48 +08:00
|
|
|
|
|
|
|
/* The preparations are done. */
|
2009-03-17 06:33:01 +08:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
#else
|
|
|
|
char pathbuf[256];
|
|
|
|
#endif
|
|
|
|
/* Try to exec the real nscd program so the process name (as reported
|
|
|
|
in /proc/PID/status) will be 'nscd', but fall back to /proc/self/exe
|
2009-11-23 11:07:52 +08:00
|
|
|
if readlink or the exec with the result of the readlink call fails. */
|
2009-03-17 06:33:01 +08:00
|
|
|
ssize_t n = readlink ("/proc/self/exe", pathbuf, sizeof (pathbuf) - 1);
|
2009-11-23 11:07:52 +08:00
|
|
|
if (n != -1)
|
2009-03-17 06:33:01 +08:00
|
|
|
{
|
|
|
|
pathbuf[n] = '\0';
|
|
|
|
execv (pathbuf, argv);
|
|
|
|
}
|
2009-11-23 11:07:52 +08:00
|
|
|
execv ("/proc/self/exe", argv);
|
2004-10-04 03:33:48 +08:00
|
|
|
|
|
|
|
/* If we come here, we will never be able to re-exec. */
|
|
|
|
dbg_log (_("re-exec failed: %s; disabling paranoia mode"),
|
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
if (server_user != NULL)
|
|
|
|
{
|
2014-11-27 04:54:16 +08:00
|
|
|
ignore_value (setuid (server_uid));
|
|
|
|
ignore_value (setgid (server_gid));
|
2004-10-04 03:33:48 +08:00
|
|
|
}
|
2005-07-19 06:28:46 +08:00
|
|
|
if (chdir ("/") != 0)
|
|
|
|
dbg_log (_("cannot change current working directory to \"/\": %s"),
|
|
|
|
strerror (errno));
|
2004-10-04 03:33:48 +08:00
|
|
|
paranoia = 0;
|
2008-05-19 05:54:43 +08:00
|
|
|
|
|
|
|
/* Reenable the databases. */
|
|
|
|
time_t now = time (NULL);
|
|
|
|
for (int cnt = 0; cnt < lastdb; ++cnt)
|
|
|
|
if (dbs[cnt].enabled)
|
|
|
|
{
|
|
|
|
dbs[cnt].head->timestamp = now;
|
|
|
|
dbs[cnt].head->nscd_certainly_running = certainly[cnt];
|
|
|
|
}
|
2004-10-04 03:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* List of file descriptors. */
|
|
|
|
struct fdlist
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct fdlist *next;
|
|
|
|
};
|
|
|
|
/* Memory allocated for the list. */
|
|
|
|
static struct fdlist *fdlist;
|
|
|
|
/* List of currently ready-to-read file descriptors. */
|
|
|
|
static struct fdlist *readylist;
|
|
|
|
|
|
|
|
/* Conditional variable and mutex to signal availability of entries in
|
|
|
|
READYLIST. The condvar is initialized dynamically since we might
|
|
|
|
use a different clock depending on availability. */
|
2007-11-06 08:45:40 +08:00
|
|
|
static pthread_cond_t readylist_cond = PTHREAD_COND_INITIALIZER;
|
2004-10-03 09:21:47 +08:00
|
|
|
static pthread_mutex_t readylist_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
/* The clock to use with the condvar. */
|
|
|
|
static clockid_t timeout_clock = CLOCK_REALTIME;
|
|
|
|
|
|
|
|
/* Number of threads ready to handle the READYLIST. */
|
|
|
|
static unsigned long int nready;
|
|
|
|
|
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Function for the clean-up threads. */
|
1998-10-18 23:16:22 +08:00
|
|
|
static void *
|
|
|
|
__attribute__ ((__noreturn__))
|
2007-11-06 08:45:40 +08:00
|
|
|
nscd_run_prune (void *p)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
const long int my_number = (long int) p;
|
2007-11-06 08:45:40 +08:00
|
|
|
assert (dbs[my_number].enabled);
|
|
|
|
|
|
|
|
int dont_need_update = setup_thread (&dbs[my_number]);
|
|
|
|
|
2008-05-19 05:54:43 +08:00
|
|
|
time_t now = time (NULL);
|
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
/* We are running. */
|
2008-05-19 05:54:43 +08:00
|
|
|
dbs[my_number].head->timestamp = now;
|
2007-11-06 08:45:40 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
struct timespec prune_ts;
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (clock_gettime (timeout_clock, &prune_ts) == -1))
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Should never happen. */
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
/* Compute the initial timeout time. Prevent all the timers to go
|
|
|
|
off at the same time by adding a db-based value. */
|
|
|
|
prune_ts.tv_sec += CACHE_PRUNE_INTERVAL + my_number;
|
2008-05-19 05:54:43 +08:00
|
|
|
dbs[my_number].wakeup_time = now + CACHE_PRUNE_INTERVAL + my_number;
|
|
|
|
|
|
|
|
pthread_mutex_t *prune_lock = &dbs[my_number].prune_lock;
|
2008-12-09 13:24:37 +08:00
|
|
|
pthread_mutex_t *prune_run_lock = &dbs[my_number].prune_run_lock;
|
2008-05-19 05:54:43 +08:00
|
|
|
pthread_cond_t *prune_cond = &dbs[my_number].prune_cond;
|
1998-07-25 05:32:39 +08:00
|
|
|
|
2008-05-19 05:54:43 +08:00
|
|
|
pthread_mutex_lock (prune_lock);
|
2007-11-06 08:45:40 +08:00
|
|
|
while (1)
|
2004-10-03 09:21:47 +08:00
|
|
|
{
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Wait, but not forever. */
|
2008-06-13 06:40:27 +08:00
|
|
|
int e = 0;
|
|
|
|
if (! dbs[my_number].clear_cache)
|
|
|
|
e = pthread_cond_timedwait (prune_cond, prune_lock, &prune_ts);
|
2008-05-19 05:54:43 +08:00
|
|
|
assert (__builtin_expect (e == 0 || e == ETIMEDOUT, 1));
|
2004-09-09 01:56:46 +08:00
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
time_t next_wait;
|
2008-05-19 05:54:43 +08:00
|
|
|
now = time (NULL);
|
2008-06-13 06:40:27 +08:00
|
|
|
if (e == ETIMEDOUT || now >= dbs[my_number].wakeup_time
|
|
|
|
|| dbs[my_number].clear_cache)
|
2007-11-06 08:45:40 +08:00
|
|
|
{
|
2008-05-19 05:54:43 +08:00
|
|
|
/* We will determine the new timout values based on the
|
|
|
|
cache content. Should there be concurrent additions to
|
|
|
|
the cache which are not accounted for in the cache
|
|
|
|
pruning we want to know about it. Therefore set the
|
|
|
|
timeout to the maximum. It will be descreased when adding
|
|
|
|
new entries to the cache, if necessary. */
|
2011-02-06 09:07:27 +08:00
|
|
|
dbs[my_number].wakeup_time = MAX_TIMEOUT_VALUE;
|
2008-05-19 05:54:43 +08:00
|
|
|
|
2008-06-13 06:40:27 +08:00
|
|
|
/* Unconditionally reset the flag. */
|
|
|
|
time_t prune_now = dbs[my_number].clear_cache ? LONG_MAX : now;
|
|
|
|
dbs[my_number].clear_cache = 0;
|
|
|
|
|
2008-05-19 05:54:43 +08:00
|
|
|
pthread_mutex_unlock (prune_lock);
|
|
|
|
|
2008-12-09 13:24:37 +08:00
|
|
|
/* We use a separate lock for running the prune function (instead
|
|
|
|
of keeping prune_lock locked) because this enables concurrent
|
|
|
|
invocations of cache_add which might modify the timeout value. */
|
|
|
|
pthread_mutex_lock (prune_run_lock);
|
2008-06-13 06:40:27 +08:00
|
|
|
next_wait = prune_cache (&dbs[my_number], prune_now, -1);
|
2008-12-09 13:24:37 +08:00
|
|
|
pthread_mutex_unlock (prune_run_lock);
|
2008-05-19 05:54:43 +08:00
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
next_wait = MAX (next_wait, CACHE_PRUNE_INTERVAL);
|
|
|
|
/* If clients cannot determine for sure whether nscd is running
|
|
|
|
we need to wake up occasionally to update the timestamp.
|
|
|
|
Wait 90% of the update period. */
|
|
|
|
#define UPDATE_MAPPING_TIMEOUT (MAPPING_TIMEOUT * 9 / 10)
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (! dont_need_update))
|
2008-05-19 05:54:43 +08:00
|
|
|
{
|
|
|
|
next_wait = MIN (UPDATE_MAPPING_TIMEOUT, next_wait);
|
|
|
|
dbs[my_number].head->timestamp = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock (prune_lock);
|
2007-11-06 08:45:40 +08:00
|
|
|
|
|
|
|
/* Make it known when we will wake up again. */
|
2008-05-19 05:54:43 +08:00
|
|
|
if (now + next_wait < dbs[my_number].wakeup_time)
|
|
|
|
dbs[my_number].wakeup_time = now + next_wait;
|
|
|
|
else
|
|
|
|
next_wait = dbs[my_number].wakeup_time - now;
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* The cache was just pruned. Do not do it again now. Just
|
|
|
|
use the new timeout value. */
|
|
|
|
next_wait = dbs[my_number].wakeup_time - now;
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
if (clock_gettime (timeout_clock, &prune_ts) == -1)
|
|
|
|
/* Should never happen. */
|
|
|
|
abort ();
|
2003-04-28 10:43:30 +08:00
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Compute next timeout time. */
|
|
|
|
prune_ts.tv_sec += next_wait;
|
2004-10-03 09:21:47 +08:00
|
|
|
}
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the main loop. It is replicated in different threads but
|
2011-04-23 09:34:32 +08:00
|
|
|
the use of the ready list makes sure only one thread handles an
|
2007-11-06 08:45:40 +08:00
|
|
|
incoming connection. */
|
|
|
|
static void *
|
|
|
|
__attribute__ ((__noreturn__))
|
|
|
|
nscd_run_worker (void *p)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2004-10-03 09:21:47 +08:00
|
|
|
|
|
|
|
/* Initial locking. */
|
|
|
|
pthread_mutex_lock (&readylist_lock);
|
|
|
|
|
|
|
|
/* One more thread available. */
|
|
|
|
++nready;
|
2003-04-28 10:43:30 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
while (readylist == NULL)
|
2007-11-06 08:45:40 +08:00
|
|
|
pthread_cond_wait (&readylist_cond, &readylist_lock);
|
2003-04-28 10:43:30 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
struct fdlist *it = readylist->next;
|
|
|
|
if (readylist->next == readylist)
|
|
|
|
/* Just one entry on the list. */
|
|
|
|
readylist = NULL;
|
|
|
|
else
|
|
|
|
readylist->next = it->next;
|
2003-04-28 10:43:30 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Extract the information and mark the record ready to be used
|
|
|
|
again. */
|
|
|
|
int fd = it->fd;
|
|
|
|
it->next = NULL;
|
2003-04-28 10:43:30 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* One more thread available. */
|
|
|
|
--nready;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* We are done with the list. */
|
|
|
|
pthread_mutex_unlock (&readylist_lock);
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
/* Now read the request. */
|
2004-10-03 09:21:47 +08:00
|
|
|
request_header req;
|
2003-04-28 10:43:30 +08:00
|
|
|
if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd, &req, sizeof (req)))
|
|
|
|
!= sizeof (req), 0))
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
/* We failed to read data. Note that this also might mean we
|
|
|
|
failed because we would have blocked. */
|
2003-04-28 10:43:30 +08:00
|
|
|
if (debug_level > 0)
|
|
|
|
dbg_log (_("short read while reading request: %s"),
|
|
|
|
strerror_r (errno, buf, sizeof (buf)));
|
2004-10-03 09:21:47 +08:00
|
|
|
goto close_and_out;
|
2003-04-28 10:43:30 +08:00
|
|
|
}
|
|
|
|
|
2004-09-03 11:39:45 +08:00
|
|
|
/* Check whether this is a valid request type. */
|
|
|
|
if (req.type < GETPWBYNAME || req.type >= LASTREQ)
|
|
|
|
goto close_and_out;
|
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
/* Some systems have no SO_PEERCRED implementation. They don't
|
|
|
|
care about security so we don't as well. */
|
2004-10-03 09:21:47 +08:00
|
|
|
uid_t uid = -1;
|
2003-04-26 12:15:50 +08:00
|
|
|
#ifdef SO_PEERCRED
|
2004-10-03 09:21:47 +08:00
|
|
|
pid_t pid = 0;
|
|
|
|
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (debug_level > 0))
|
2003-04-28 10:43:30 +08:00
|
|
|
{
|
|
|
|
struct ucred caller;
|
|
|
|
socklen_t optlen = sizeof (caller);
|
2003-04-26 12:15:50 +08:00
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
if (getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &caller, &optlen) == 0)
|
|
|
|
pid = caller.pid;
|
|
|
|
}
|
2008-04-20 00:42:41 +08:00
|
|
|
#else
|
|
|
|
const pid_t pid = 0;
|
2000-01-08 18:13:33 +08:00
|
|
|
#endif
|
1999-06-12 04:58:21 +08:00
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
/* It should not be possible to crash the nscd with a silly
|
|
|
|
request (i.e., a terribly large key). We limit the size to 1kb. */
|
|
|
|
if (__builtin_expect (req.key_len, 1) < 0
|
2004-10-03 09:21:47 +08:00
|
|
|
|| __builtin_expect (req.key_len, 1) > MAXKEYLEN)
|
2003-04-28 10:43:30 +08:00
|
|
|
{
|
|
|
|
if (debug_level > 0)
|
|
|
|
dbg_log (_("key length in request too long: %d"), req.key_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the key. */
|
2013-06-10 20:39:09 +08:00
|
|
|
char keybuf[MAXKEYLEN + 1];
|
2003-04-28 10:43:30 +08:00
|
|
|
|
|
|
|
if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd, keybuf,
|
|
|
|
req.key_len))
|
|
|
|
!= req.key_len, 0))
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Again, this can also mean we would have blocked. */
|
2001-07-17 09:37:42 +08:00
|
|
|
if (debug_level > 0)
|
2003-04-28 10:43:30 +08:00
|
|
|
dbg_log (_("short read while reading request key: %s"),
|
|
|
|
strerror_r (errno, buf, sizeof (buf)));
|
2004-10-03 09:21:47 +08:00
|
|
|
goto close_and_out;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2013-06-10 20:39:09 +08:00
|
|
|
keybuf[req.key_len] = '\0';
|
2003-04-28 10:43:30 +08:00
|
|
|
|
|
|
|
if (__builtin_expect (debug_level, 0) > 0)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2003-04-26 12:15:50 +08:00
|
|
|
#ifdef SO_PEERCRED
|
2003-04-28 10:43:30 +08:00
|
|
|
if (pid != 0)
|
|
|
|
dbg_log (_("\
|
2003-04-26 12:15:50 +08:00
|
|
|
handle_request: request received (Version = %d) from PID %ld"),
|
2003-04-28 10:43:30 +08:00
|
|
|
req.version, (long int) pid);
|
|
|
|
else
|
2003-04-26 12:15:50 +08:00
|
|
|
#endif
|
2003-04-28 10:43:30 +08:00
|
|
|
dbg_log (_("\
|
2003-04-26 12:15:50 +08:00
|
|
|
handle_request: request received (Version = %d)"), req.version);
|
2003-04-28 10:43:30 +08:00
|
|
|
}
|
2003-04-26 12:15:50 +08:00
|
|
|
|
2003-04-28 10:43:30 +08:00
|
|
|
/* Phew, we got all the data, now process it. */
|
2008-04-20 00:42:41 +08:00
|
|
|
handle_request (fd, &req, keybuf, uid, pid);
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
1998-07-25 05:32:39 +08:00
|
|
|
|
2004-09-03 11:39:45 +08:00
|
|
|
close_and_out:
|
|
|
|
/* We are done. */
|
|
|
|
close (fd);
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Re-locking. */
|
|
|
|
pthread_mutex_lock (&readylist_lock);
|
|
|
|
|
|
|
|
/* One more thread available. */
|
|
|
|
++nready;
|
|
|
|
}
|
2009-04-27 06:31:19 +08:00
|
|
|
/* NOTREACHED */
|
2004-10-03 09:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
static unsigned int nconns;
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
static void
|
2004-10-03 15:33:54 +08:00
|
|
|
fd_ready (int fd)
|
2004-10-03 09:21:47 +08:00
|
|
|
{
|
2004-10-03 15:33:54 +08:00
|
|
|
pthread_mutex_lock (&readylist_lock);
|
|
|
|
|
|
|
|
/* Find an empty entry in FDLIST. */
|
|
|
|
size_t inner;
|
|
|
|
for (inner = 0; inner < nconns; ++inner)
|
|
|
|
if (fdlist[inner].next == NULL)
|
|
|
|
break;
|
|
|
|
assert (inner < nconns);
|
|
|
|
|
|
|
|
fdlist[inner].fd = fd;
|
|
|
|
|
|
|
|
if (readylist == NULL)
|
|
|
|
readylist = fdlist[inner].next = &fdlist[inner];
|
2004-10-03 09:21:47 +08:00
|
|
|
else
|
2004-10-03 15:33:54 +08:00
|
|
|
{
|
|
|
|
fdlist[inner].next = readylist->next;
|
|
|
|
readylist = readylist->next = &fdlist[inner];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool do_signal = true;
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely (nready == 0))
|
2004-10-03 15:33:54 +08:00
|
|
|
{
|
|
|
|
++client_queued;
|
|
|
|
do_signal = false;
|
2004-10-04 05:11:37 +08:00
|
|
|
|
|
|
|
/* Try to start another thread to help out. */
|
|
|
|
pthread_t th;
|
|
|
|
if (nthreads < max_nthreads
|
2007-11-06 08:45:40 +08:00
|
|
|
&& pthread_create (&th, &attr, nscd_run_worker,
|
2004-10-04 05:11:37 +08:00
|
|
|
(void *) (long int) nthreads) == 0)
|
|
|
|
{
|
|
|
|
/* We got another thread. */
|
|
|
|
++nthreads;
|
2005-08-09 14:16:47 +08:00
|
|
|
/* The new thread might need a kick. */
|
2004-10-04 05:11:37 +08:00
|
|
|
do_signal = true;
|
|
|
|
}
|
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock (&readylist_lock);
|
|
|
|
|
|
|
|
/* Tell one of the worker threads there is work to do. */
|
|
|
|
if (do_signal)
|
|
|
|
pthread_cond_signal (&readylist_cond);
|
|
|
|
}
|
2004-10-03 09:21:47 +08:00
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
|
2004-10-04 03:33:48 +08:00
|
|
|
/* Check whether restarting should happen. */
|
2013-02-08 06:44:18 +08:00
|
|
|
static bool
|
2004-10-04 03:33:48 +08:00
|
|
|
restart_p (time_t now)
|
|
|
|
{
|
|
|
|
return (paranoia && readylist == NULL && nready == nthreads
|
|
|
|
&& now >= restart_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Array for times a connection was accepted. */
|
2004-10-03 15:33:54 +08:00
|
|
|
static time_t *starttime;
|
|
|
|
|
2013-02-08 03:28:53 +08:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
/* Inotify event for changed file. */
|
|
|
|
union __inev
|
|
|
|
{
|
|
|
|
struct inotify_event i;
|
|
|
|
# ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 1024
|
|
|
|
# endif
|
|
|
|
char buf[sizeof (struct inotify_event) + PATH_MAX];
|
|
|
|
};
|
|
|
|
|
2015-03-13 21:49:24 +08:00
|
|
|
/* Returns 0 if the file is there otherwise -1. */
|
|
|
|
int
|
|
|
|
check_file (struct traced_file *finfo)
|
|
|
|
{
|
|
|
|
struct stat64 st;
|
|
|
|
/* We could check mtime and if different re-add
|
|
|
|
the watches, and invalidate the database, but we
|
|
|
|
don't because we are called from inotify_check_files
|
|
|
|
which should be doing that work. If sufficient inotify
|
|
|
|
events were lost then the next pruning or invalidation
|
|
|
|
will do the stat and mtime check. We don't do it here to
|
|
|
|
keep the logic simple. */
|
|
|
|
if (stat64 (finfo->fname, &st) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-08 03:28:53 +08:00
|
|
|
/* Process the inotify event in INEV. If the event matches any of the files
|
|
|
|
registered with a database then mark that database as requiring its cache
|
|
|
|
to be cleared. We indicate the cache needs clearing by setting
|
|
|
|
TO_CLEAR[DBCNT] to true for the matching database. */
|
2015-03-13 21:49:24 +08:00
|
|
|
static void
|
2013-02-08 03:28:53 +08:00
|
|
|
inotify_check_files (bool *to_clear, union __inev *inev)
|
|
|
|
{
|
|
|
|
/* Check which of the files changed. */
|
|
|
|
for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
|
|
|
|
{
|
|
|
|
struct traced_file *finfo = dbs[dbcnt].traced_files;
|
|
|
|
|
|
|
|
while (finfo != NULL)
|
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
/* The configuration file was moved or deleted.
|
|
|
|
We stop watching it at that point, and reinitialize. */
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] == inev->i.wd
|
|
|
|
&& ((inev->i.mask & IN_MOVE_SELF)
|
|
|
|
|| (inev->i.mask & IN_DELETE_SELF)
|
|
|
|
|| (inev->i.mask & IN_IGNORED)))
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
bool moved = (inev->i.mask & IN_MOVE_SELF) != 0;
|
|
|
|
|
|
|
|
if (check_file (finfo) == 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("ignored inotify event for `%s` (file exists)"),
|
|
|
|
finfo->fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg_log (_("monitored file `%s` was %s, removing watch"),
|
|
|
|
finfo->fname, moved ? "moved" : "deleted");
|
|
|
|
/* File was moved out, remove the watch. Watches are
|
|
|
|
automatically removed when the file is deleted. */
|
|
|
|
if (moved)
|
|
|
|
{
|
|
|
|
ret = inotify_rm_watch (inotify_fd, inev->i.wd);
|
|
|
|
if (ret < 0)
|
|
|
|
dbg_log (_("failed to remove file watch `%s`: %s"),
|
|
|
|
finfo->fname, strerror (errno));
|
|
|
|
}
|
|
|
|
finfo->inotify_descr[TRACED_FILE] = -1;
|
|
|
|
to_clear[dbcnt] = true;
|
|
|
|
if (finfo->call_res_init)
|
|
|
|
res_init ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* The configuration file was open for writing and has just closed.
|
|
|
|
We reset the cache and reinitialize. */
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] == inev->i.wd
|
|
|
|
&& inev->i.mask & IN_CLOSE_WRITE)
|
2013-02-08 03:28:53 +08:00
|
|
|
{
|
|
|
|
/* Mark cache as needing to be cleared and reinitialize. */
|
2015-03-13 21:49:24 +08:00
|
|
|
dbg_log (_("monitored file `%s` was written to"), finfo->fname);
|
2013-02-08 03:28:53 +08:00
|
|
|
to_clear[dbcnt] = true;
|
|
|
|
if (finfo->call_res_init)
|
|
|
|
res_init ();
|
|
|
|
return;
|
|
|
|
}
|
2015-03-13 21:49:24 +08:00
|
|
|
/* The parent directory was moved or deleted. We trigger one last
|
|
|
|
invalidation. At the next pruning or invalidation we may add
|
|
|
|
this watch back if the file is present again. */
|
|
|
|
if (finfo->inotify_descr[TRACED_DIR] == inev->i.wd
|
|
|
|
&& ((inev->i.mask & IN_DELETE_SELF)
|
|
|
|
|| (inev->i.mask & IN_MOVE_SELF)
|
|
|
|
|| (inev->i.mask & IN_IGNORED)))
|
|
|
|
{
|
|
|
|
bool moved = (inev->i.mask & IN_MOVE_SELF) != 0;
|
|
|
|
/* The directory watch may have already been removed
|
|
|
|
but we don't know so we just remove it again and
|
|
|
|
ignore the error. Then we remove the file watch.
|
|
|
|
Note: watches are automatically removed for deleted
|
|
|
|
files. */
|
|
|
|
if (moved)
|
|
|
|
inotify_rm_watch (inotify_fd, inev->i.wd);
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] != -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("monitored parent directory `%s` was %s, removing watch on `%s`"),
|
|
|
|
finfo->dname, moved ? "moved" : "deleted", finfo->fname);
|
|
|
|
if (inotify_rm_watch (inotify_fd, finfo->inotify_descr[TRACED_FILE]) < 0)
|
|
|
|
dbg_log (_("failed to remove file watch `%s`: %s"),
|
|
|
|
finfo->dname, strerror (errno));
|
|
|
|
}
|
|
|
|
finfo->inotify_descr[TRACED_FILE] = -1;
|
|
|
|
finfo->inotify_descr[TRACED_DIR] = -1;
|
|
|
|
to_clear[dbcnt] = true;
|
|
|
|
if (finfo->call_res_init)
|
|
|
|
res_init ();
|
|
|
|
/* Continue to the next entry since this might be the
|
|
|
|
parent directory for multiple registered files and
|
|
|
|
we want to remove watches for all registered files. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* The parent directory had a create or moved to event. */
|
|
|
|
if (finfo->inotify_descr[TRACED_DIR] == inev->i.wd
|
|
|
|
&& ((inev->i.mask & IN_MOVED_TO)
|
|
|
|
|| (inev->i.mask & IN_CREATE))
|
|
|
|
&& strcmp (inev->i.name, finfo->sfname) == 0)
|
|
|
|
{
|
|
|
|
/* We detected a directory change. We look for the creation
|
|
|
|
of the file we are tracking or the move of the same file
|
|
|
|
into the directory. */
|
|
|
|
int ret;
|
|
|
|
dbg_log (_("monitored file `%s` was %s, adding watch"),
|
|
|
|
finfo->fname,
|
|
|
|
inev->i.mask & IN_CREATE ? "created" : "moved into place");
|
|
|
|
/* File was moved in or created. Regenerate the watch. */
|
|
|
|
if (finfo->inotify_descr[TRACED_FILE] != -1)
|
|
|
|
inotify_rm_watch (inotify_fd,
|
|
|
|
finfo->inotify_descr[TRACED_FILE]);
|
|
|
|
|
|
|
|
ret = inotify_add_watch (inotify_fd,
|
|
|
|
finfo->fname,
|
|
|
|
TRACED_FILE_MASK);
|
|
|
|
if (ret < 0)
|
|
|
|
dbg_log (_("failed to add file watch `%s`: %s"),
|
|
|
|
finfo->fname, strerror (errno));
|
|
|
|
|
|
|
|
finfo->inotify_descr[TRACED_FILE] = ret;
|
|
|
|
|
|
|
|
/* The file is new or moved so mark cache as needing to
|
|
|
|
be cleared and reinitialize. */
|
|
|
|
to_clear[dbcnt] = true;
|
|
|
|
if (finfo->call_res_init)
|
|
|
|
res_init ();
|
2013-02-08 03:28:53 +08:00
|
|
|
|
2015-03-13 21:49:24 +08:00
|
|
|
/* Done re-adding the watch. Don't return, we may still
|
|
|
|
have other files in this same directory, same watch
|
|
|
|
descriptor, and need to process them. */
|
|
|
|
}
|
|
|
|
/* Other events are ignored, and we move on to the next file. */
|
2013-02-08 03:28:53 +08:00
|
|
|
finfo = finfo->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If an entry in the array of booleans TO_CLEAR is TRUE then clear the cache
|
|
|
|
for the associated database, otherwise do nothing. The TO_CLEAR array must
|
|
|
|
have LASTDB entries. */
|
|
|
|
static inline void
|
|
|
|
clear_db_cache (bool *to_clear)
|
|
|
|
{
|
|
|
|
for (size_t dbcnt = 0; dbcnt < lastdb; ++dbcnt)
|
|
|
|
if (to_clear[dbcnt])
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&dbs[dbcnt].prune_lock);
|
|
|
|
dbs[dbcnt].clear_cache = 1;
|
|
|
|
pthread_mutex_unlock (&dbs[dbcnt].prune_lock);
|
|
|
|
pthread_cond_signal (&dbs[dbcnt].prune_cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 21:49:24 +08:00
|
|
|
int
|
|
|
|
handle_inotify_events (void)
|
|
|
|
{
|
|
|
|
bool to_clear[lastdb] = { false, };
|
|
|
|
union __inev inev;
|
|
|
|
|
|
|
|
/* Read all inotify events for files registered via
|
|
|
|
register_traced_file(). */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* Potentially read multiple events into buf. */
|
|
|
|
ssize_t nb = TEMP_FAILURE_RETRY (read (inotify_fd,
|
|
|
|
&inev.buf,
|
|
|
|
sizeof (inev)));
|
|
|
|
if (nb < (ssize_t) sizeof (struct inotify_event))
|
|
|
|
{
|
|
|
|
/* Not even 1 event. */
|
|
|
|
if (__glibc_unlikely (nb == -1 && errno != EAGAIN))
|
|
|
|
return -1;
|
|
|
|
/* Done reading events that are ready. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Process all events. The normal inotify interface delivers
|
|
|
|
complete events on a read and never a partial event. */
|
|
|
|
char *eptr = &inev.buf[0];
|
|
|
|
ssize_t count;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* Check which of the files changed. */
|
|
|
|
inotify_check_files (to_clear, &inev);
|
|
|
|
count = sizeof (struct inotify_event) + inev.i.len;
|
|
|
|
eptr += count;
|
|
|
|
nb -= count;
|
|
|
|
if (nb >= (ssize_t) sizeof (struct inotify_event))
|
|
|
|
memcpy (&inev, eptr, nb);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Actually perform the cache clearing. */
|
|
|
|
clear_db_cache (to_clear);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-08 03:28:53 +08:00
|
|
|
#endif
|
2004-10-03 15:33:54 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
__attribute__ ((__noreturn__))
|
|
|
|
main_loop_poll (void)
|
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
struct pollfd *conns = (struct pollfd *) xmalloc (nconns
|
|
|
|
* sizeof (conns[0]));
|
|
|
|
|
|
|
|
conns[0].fd = sock;
|
|
|
|
conns[0].events = POLLRDNORM;
|
|
|
|
size_t nused = 1;
|
|
|
|
size_t firstfree = 1;
|
|
|
|
|
2008-06-13 06:40:27 +08:00
|
|
|
#ifdef HAVE_INOTIFY
|
|
|
|
if (inotify_fd != -1)
|
|
|
|
{
|
|
|
|
conns[1].fd = inotify_fd;
|
|
|
|
conns[1].events = POLLRDNORM;
|
|
|
|
nused = 2;
|
|
|
|
firstfree = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-31 13:51:16 +08:00
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
size_t idx_nl_status_fd = 0;
|
|
|
|
if (nl_status_fd != -1)
|
|
|
|
{
|
|
|
|
idx_nl_status_fd = nused;
|
|
|
|
conns[nused].fd = nl_status_fd;
|
|
|
|
conns[nused].events = POLLRDNORM;
|
|
|
|
++nused;
|
|
|
|
firstfree = nused;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* Wait for any event. We wait at most a couple of seconds so
|
|
|
|
that we can check whether we should close any of the accepted
|
|
|
|
connections since we have not received a request. */
|
|
|
|
#define MAX_ACCEPT_TIMEOUT 30
|
|
|
|
#define MIN_ACCEPT_TIMEOUT 5
|
|
|
|
#define MAIN_THREAD_TIMEOUT \
|
|
|
|
(MAX_ACCEPT_TIMEOUT * 1000 \
|
|
|
|
- ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * 1000 * nused) / (2 * nconns))
|
|
|
|
|
|
|
|
int n = poll (conns, nused, MAIN_THREAD_TIMEOUT);
|
|
|
|
|
|
|
|
time_t now = time (NULL);
|
|
|
|
|
|
|
|
/* If there is a descriptor ready for reading or there is a new
|
|
|
|
connection, process this now. */
|
|
|
|
if (n > 0)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
if (conns[0].revents != 0)
|
|
|
|
{
|
|
|
|
/* We have a new incoming connection. Accept the connection. */
|
2017-04-19 13:44:48 +08:00
|
|
|
int fd = TEMP_FAILURE_RETRY (accept4 (sock, NULL, NULL,
|
2008-07-25 13:30:10 +08:00
|
|
|
SOCK_NONBLOCK));
|
2004-10-03 09:21:47 +08:00
|
|
|
|
2006-04-02 02:51:15 +08:00
|
|
|
/* Use the descriptor if we have not reached the limit. */
|
|
|
|
if (fd >= 0)
|
2004-10-03 09:21:47 +08:00
|
|
|
{
|
2006-04-02 02:51:15 +08:00
|
|
|
if (firstfree < nconns)
|
|
|
|
{
|
|
|
|
conns[firstfree].fd = fd;
|
|
|
|
conns[firstfree].events = POLLRDNORM;
|
|
|
|
starttime[firstfree] = now;
|
|
|
|
if (firstfree >= nused)
|
|
|
|
nused = firstfree + 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
++firstfree;
|
|
|
|
while (firstfree < nused && conns[firstfree].fd != -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* We cannot use the connection so close it. */
|
|
|
|
close (fd);
|
2004-10-03 09:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
|
2008-06-13 06:40:27 +08:00
|
|
|
size_t first = 1;
|
|
|
|
#ifdef HAVE_INOTIFY
|
2008-07-13 01:40:49 +08:00
|
|
|
if (inotify_fd != -1 && conns[1].fd == inotify_fd)
|
2008-06-13 06:40:27 +08:00
|
|
|
{
|
|
|
|
if (conns[1].revents != 0)
|
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
int ret;
|
|
|
|
ret = handle_inotify_events ();
|
|
|
|
if (ret == -1)
|
2008-06-13 06:40:27 +08:00
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
/* Something went wrong when reading the inotify
|
|
|
|
data. Better disable inotify. */
|
|
|
|
dbg_log (_("disabled inotify-based monitoring after read error %d"), errno);
|
|
|
|
conns[1].fd = -1;
|
|
|
|
firstfree = 1;
|
|
|
|
if (nused == 2)
|
|
|
|
nused = 1;
|
|
|
|
close (inotify_fd);
|
|
|
|
inotify_fd = -1;
|
2008-06-13 06:40:27 +08:00
|
|
|
}
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-31 13:51:16 +08:00
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
if (idx_nl_status_fd != 0 && conns[idx_nl_status_fd].revents != 0)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
/* Read all the data. We do not interpret it here. */
|
|
|
|
while (TEMP_FAILURE_RETRY (read (nl_status_fd, buf,
|
|
|
|
sizeof (buf))) != -1)
|
|
|
|
;
|
|
|
|
|
|
|
|
dbs[hstdb].head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP]
|
|
|
|
= __bump_nl_timestamp ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-13 06:40:27 +08:00
|
|
|
for (size_t cnt = first; cnt < nused && n > 0; ++cnt)
|
2004-10-03 09:21:47 +08:00
|
|
|
if (conns[cnt].revents != 0)
|
|
|
|
{
|
2004-10-03 15:33:54 +08:00
|
|
|
fd_ready (conns[cnt].fd);
|
2004-10-03 09:21:47 +08:00
|
|
|
|
|
|
|
/* Clean up the CONNS array. */
|
|
|
|
conns[cnt].fd = -1;
|
|
|
|
if (cnt < firstfree)
|
|
|
|
firstfree = cnt;
|
|
|
|
if (cnt == nused - 1)
|
|
|
|
do
|
|
|
|
--nused;
|
|
|
|
while (conns[nused - 1].fd == -1);
|
|
|
|
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now find entries which have timed out. */
|
|
|
|
assert (nused > 0);
|
2004-10-03 15:33:54 +08:00
|
|
|
|
|
|
|
/* We make the timeout length depend on the number of file
|
|
|
|
descriptors currently used. */
|
2004-10-03 09:21:47 +08:00
|
|
|
#define ACCEPT_TIMEOUT \
|
|
|
|
(MAX_ACCEPT_TIMEOUT \
|
|
|
|
- ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * nused) / nconns)
|
2004-10-03 15:33:54 +08:00
|
|
|
time_t laststart = now - ACCEPT_TIMEOUT;
|
2004-10-03 09:21:47 +08:00
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
for (size_t cnt = nused - 1; cnt > 0; --cnt)
|
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
if (conns[cnt].fd != -1 && starttime[cnt] < laststart)
|
|
|
|
{
|
|
|
|
/* Remove the entry, it timed out. */
|
|
|
|
(void) close (conns[cnt].fd);
|
|
|
|
conns[cnt].fd = -1;
|
|
|
|
|
|
|
|
if (cnt < firstfree)
|
|
|
|
firstfree = cnt;
|
|
|
|
if (cnt == nused - 1)
|
|
|
|
do
|
|
|
|
--nused;
|
|
|
|
while (conns[nused - 1].fd == -1);
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2004-10-04 03:33:48 +08:00
|
|
|
|
|
|
|
if (restart_p (now))
|
|
|
|
restart ();
|
1998-07-26 02:02:26 +08:00
|
|
|
}
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
#ifdef HAVE_EPOLL
|
|
|
|
static void
|
|
|
|
main_loop_epoll (int efd)
|
|
|
|
{
|
|
|
|
struct epoll_event ev = { 0, };
|
|
|
|
int nused = 1;
|
|
|
|
size_t highest = 0;
|
|
|
|
|
|
|
|
/* Add the socket. */
|
|
|
|
ev.events = EPOLLRDNORM;
|
|
|
|
ev.data.fd = sock;
|
|
|
|
if (epoll_ctl (efd, EPOLL_CTL_ADD, sock, &ev) == -1)
|
|
|
|
/* We cannot use epoll. */
|
|
|
|
return;
|
|
|
|
|
2008-12-03 12:23:18 +08:00
|
|
|
# ifdef HAVE_INOTIFY
|
2008-06-13 06:40:27 +08:00
|
|
|
if (inotify_fd != -1)
|
|
|
|
{
|
|
|
|
ev.events = EPOLLRDNORM;
|
|
|
|
ev.data.fd = inotify_fd;
|
|
|
|
if (epoll_ctl (efd, EPOLL_CTL_ADD, inotify_fd, &ev) == -1)
|
|
|
|
/* We cannot use epoll. */
|
|
|
|
return;
|
|
|
|
nused = 2;
|
|
|
|
}
|
2008-12-03 12:23:18 +08:00
|
|
|
# endif
|
2008-06-13 06:40:27 +08:00
|
|
|
|
2011-10-31 13:51:16 +08:00
|
|
|
# ifdef HAVE_NETLINK
|
|
|
|
if (nl_status_fd != -1)
|
|
|
|
{
|
|
|
|
ev.events = EPOLLRDNORM;
|
|
|
|
ev.data.fd = nl_status_fd;
|
|
|
|
if (epoll_ctl (efd, EPOLL_CTL_ADD, nl_status_fd, &ev) == -1)
|
|
|
|
/* We cannot use epoll. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
struct epoll_event revs[100];
|
|
|
|
# define nrevs (sizeof (revs) / sizeof (revs[0]))
|
|
|
|
|
|
|
|
int n = epoll_wait (efd, revs, nrevs, MAIN_THREAD_TIMEOUT);
|
|
|
|
|
|
|
|
time_t now = time (NULL);
|
|
|
|
|
|
|
|
for (int cnt = 0; cnt < n; ++cnt)
|
|
|
|
if (revs[cnt].data.fd == sock)
|
|
|
|
{
|
|
|
|
/* A new connection. */
|
2017-04-19 13:44:48 +08:00
|
|
|
int fd = TEMP_FAILURE_RETRY (accept4 (sock, NULL, NULL,
|
2008-12-03 12:23:18 +08:00
|
|
|
SOCK_NONBLOCK));
|
2004-10-03 15:33:54 +08:00
|
|
|
|
2008-12-03 12:23:18 +08:00
|
|
|
/* Use the descriptor if we have not reached the limit. */
|
2004-10-03 15:33:54 +08:00
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
/* Try to add the new descriptor. */
|
|
|
|
ev.data.fd = fd;
|
|
|
|
if (fd >= nconns
|
|
|
|
|| epoll_ctl (efd, EPOLL_CTL_ADD, fd, &ev) == -1)
|
|
|
|
/* The descriptor is too large or something went
|
|
|
|
wrong. Close the descriptor. */
|
|
|
|
close (fd);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Remember when we accepted the connection. */
|
|
|
|
starttime[fd] = now;
|
|
|
|
|
|
|
|
if (fd > highest)
|
|
|
|
highest = fd;
|
|
|
|
|
|
|
|
++nused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-03 12:23:18 +08:00
|
|
|
# ifdef HAVE_INOTIFY
|
2008-06-13 06:40:27 +08:00
|
|
|
else if (revs[cnt].data.fd == inotify_fd)
|
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
int ret;
|
|
|
|
ret = handle_inotify_events ();
|
|
|
|
if (ret == -1)
|
2008-06-13 06:40:27 +08:00
|
|
|
{
|
2015-03-13 21:49:24 +08:00
|
|
|
/* Something went wrong when reading the inotify
|
|
|
|
data. Better disable inotify. */
|
|
|
|
dbg_log (_("disabled inotify-based monitoring after read error %d"), errno);
|
|
|
|
(void) epoll_ctl (efd, EPOLL_CTL_DEL, inotify_fd, NULL);
|
|
|
|
close (inotify_fd);
|
|
|
|
inotify_fd = -1;
|
|
|
|
break;
|
2008-06-13 06:40:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-10-31 13:51:16 +08:00
|
|
|
# endif
|
|
|
|
# ifdef HAVE_NETLINK
|
|
|
|
else if (revs[cnt].data.fd == nl_status_fd)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
/* Read all the data. We do not interpret it here. */
|
|
|
|
while (TEMP_FAILURE_RETRY (read (nl_status_fd, buf,
|
|
|
|
sizeof (buf))) != -1)
|
|
|
|
;
|
|
|
|
|
|
|
|
__bump_nl_timestamp ();
|
|
|
|
}
|
2008-12-03 12:23:18 +08:00
|
|
|
# endif
|
2004-10-03 15:33:54 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Remove the descriptor from the epoll descriptor. */
|
2005-08-09 14:16:47 +08:00
|
|
|
(void) epoll_ctl (efd, EPOLL_CTL_DEL, revs[cnt].data.fd, NULL);
|
2004-10-03 15:33:54 +08:00
|
|
|
|
2005-11-12 10:11:32 +08:00
|
|
|
/* Get a worker to handle the request. */
|
2004-10-03 15:33:54 +08:00
|
|
|
fd_ready (revs[cnt].data.fd);
|
|
|
|
|
|
|
|
/* Reset the time. */
|
|
|
|
starttime[revs[cnt].data.fd] = 0;
|
|
|
|
if (revs[cnt].data.fd == highest)
|
|
|
|
do
|
|
|
|
--highest;
|
|
|
|
while (highest > 0 && starttime[highest] == 0);
|
|
|
|
|
|
|
|
--nused;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now look for descriptors for accepted connections which have
|
|
|
|
no reply in too long of a time. */
|
|
|
|
time_t laststart = now - ACCEPT_TIMEOUT;
|
2008-07-13 01:40:49 +08:00
|
|
|
assert (starttime[sock] == 0);
|
2015-03-13 21:49:24 +08:00
|
|
|
# ifdef HAVE_INOTIFY
|
2008-07-13 01:40:49 +08:00
|
|
|
assert (inotify_fd == -1 || starttime[inotify_fd] == 0);
|
2015-03-13 21:49:24 +08:00
|
|
|
# endif
|
2011-10-31 13:51:16 +08:00
|
|
|
assert (nl_status_fd == -1 || starttime[nl_status_fd] == 0);
|
2004-10-03 15:33:54 +08:00
|
|
|
for (int cnt = highest; cnt > STDERR_FILENO; --cnt)
|
2008-07-13 01:40:49 +08:00
|
|
|
if (starttime[cnt] != 0 && starttime[cnt] < laststart)
|
2004-10-03 15:33:54 +08:00
|
|
|
{
|
|
|
|
/* We are waiting for this one for too long. Close it. */
|
2005-08-09 14:16:47 +08:00
|
|
|
(void) epoll_ctl (efd, EPOLL_CTL_DEL, cnt, NULL);
|
2004-10-03 15:33:54 +08:00
|
|
|
|
|
|
|
(void) close (cnt);
|
|
|
|
|
|
|
|
starttime[cnt] = 0;
|
|
|
|
if (cnt == highest)
|
|
|
|
--highest;
|
|
|
|
}
|
|
|
|
else if (cnt != sock && starttime[cnt] == 0 && cnt == highest)
|
|
|
|
--highest;
|
2004-10-04 03:33:48 +08:00
|
|
|
|
|
|
|
if (restart_p (now))
|
|
|
|
restart ();
|
2004-10-03 15:33:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Start all the threads we want. The initial process is thread no. 1. */
|
1998-01-31 16:39:55 +08:00
|
|
|
void
|
1998-10-18 23:16:22 +08:00
|
|
|
start_threads (void)
|
1998-01-31 16:39:55 +08:00
|
|
|
{
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Initialize the conditional variable we will use. The only
|
|
|
|
non-standard attribute we might use is the clock selection. */
|
|
|
|
pthread_condattr_t condattr;
|
|
|
|
pthread_condattr_init (&condattr);
|
|
|
|
|
2004-10-05 00:35:54 +08:00
|
|
|
#if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0 \
|
|
|
|
&& defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Determine whether the monotonous clock is available. */
|
|
|
|
struct timespec dummy;
|
2004-10-05 00:35:54 +08:00
|
|
|
# if _POSIX_MONOTONIC_CLOCK == 0
|
2004-10-05 03:28:07 +08:00
|
|
|
if (sysconf (_SC_MONOTONIC_CLOCK) > 0)
|
2004-10-05 00:35:54 +08:00
|
|
|
# endif
|
|
|
|
# if _POSIX_CLOCK_SELECTION == 0
|
2004-10-05 03:28:07 +08:00
|
|
|
if (sysconf (_SC_CLOCK_SELECTION) > 0)
|
2004-10-05 00:35:54 +08:00
|
|
|
# endif
|
2004-10-05 03:28:07 +08:00
|
|
|
if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0
|
|
|
|
&& pthread_condattr_setclock (&condattr, CLOCK_MONOTONIC) == 0)
|
|
|
|
timeout_clock = CLOCK_MONOTONIC;
|
2004-10-03 09:21:47 +08:00
|
|
|
#endif
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* Create the attribute for the threads. They are all created
|
|
|
|
detached. */
|
1998-10-18 23:16:22 +08:00
|
|
|
pthread_attr_init (&attr);
|
|
|
|
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
|
2004-10-04 05:11:37 +08:00
|
|
|
/* Use 1MB stacks, twice as much for 64-bit architectures. */
|
2007-11-23 14:37:58 +08:00
|
|
|
pthread_attr_setstacksize (&attr, NSCD_THREAD_STACKSIZE);
|
1998-01-31 16:39:55 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* We allow less than LASTDB threads only for debugging. */
|
|
|
|
if (debug_level == 0)
|
|
|
|
nthreads = MAX (nthreads, lastdb);
|
1998-01-31 16:39:55 +08:00
|
|
|
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Create the threads which prune the databases. */
|
|
|
|
// XXX Ideally this work would be done by some of the worker threads.
|
|
|
|
// XXX But this is problematic since we would need to be able to wake
|
|
|
|
// XXX them up explicitly as well as part of the group handling the
|
|
|
|
// XXX ready-list. This requires an operation where we can wait on
|
|
|
|
// XXX two conditional variables at the same time. This operation
|
|
|
|
// XXX does not exist (yet).
|
|
|
|
for (long int i = 0; i < lastdb; ++i)
|
2004-10-03 09:21:47 +08:00
|
|
|
{
|
2007-11-06 08:45:40 +08:00
|
|
|
/* Initialize the conditional variable. */
|
|
|
|
if (pthread_cond_init (&dbs[i].prune_cond, &condattr) != 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("could not initialize conditional variable"));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (1, 0, NULL);
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
pthread_t th;
|
2007-11-06 08:45:40 +08:00
|
|
|
if (dbs[i].enabled
|
|
|
|
&& pthread_create (&th, &attr, nscd_run_prune, (void *) i) != 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("could not start clean-up thread; terminating"));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (1, 0, NULL);
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
2004-10-04 05:11:37 +08:00
|
|
|
}
|
2007-11-06 08:45:40 +08:00
|
|
|
|
|
|
|
pthread_condattr_destroy (&condattr);
|
|
|
|
|
|
|
|
for (long int i = 0; i < nthreads; ++i)
|
2004-10-04 05:11:37 +08:00
|
|
|
{
|
2007-11-06 08:45:40 +08:00
|
|
|
pthread_t th;
|
|
|
|
if (pthread_create (&th, &attr, nscd_run_worker, NULL) != 0)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
dbg_log (_("could not start any worker thread; terminating"));
|
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-04 01:21:39 +08:00
|
|
|
do_exit (1, 0, NULL);
|
2007-11-06 08:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2004-10-03 09:21:47 +08:00
|
|
|
}
|
1998-01-31 16:39:55 +08: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-04 01:21:39 +08:00
|
|
|
/* Now it is safe to let the parent know that we're doing fine and it can
|
|
|
|
exit. */
|
|
|
|
notify_parent (0);
|
|
|
|
|
2004-10-03 15:33:54 +08:00
|
|
|
/* Determine how much room for descriptors we should initially
|
|
|
|
allocate. This might need to change later if we cap the number
|
|
|
|
with MAXCONN. */
|
|
|
|
const long int nfds = sysconf (_SC_OPEN_MAX);
|
|
|
|
#define MINCONN 32
|
|
|
|
#define MAXCONN 16384
|
|
|
|
if (nfds == -1 || nfds > MAXCONN)
|
|
|
|
nconns = MAXCONN;
|
|
|
|
else if (nfds < MINCONN)
|
|
|
|
nconns = MINCONN;
|
|
|
|
else
|
|
|
|
nconns = nfds;
|
|
|
|
|
|
|
|
/* We need memory to pass descriptors on to the worker threads. */
|
|
|
|
fdlist = (struct fdlist *) xcalloc (nconns, sizeof (fdlist[0]));
|
|
|
|
/* Array to keep track when connection was accepted. */
|
|
|
|
starttime = (time_t *) xcalloc (nconns, sizeof (starttime[0]));
|
|
|
|
|
2004-10-03 09:21:47 +08:00
|
|
|
/* In the main thread we execute the loop which handles incoming
|
|
|
|
connections. */
|
2004-10-03 15:33:54 +08:00
|
|
|
#ifdef HAVE_EPOLL
|
|
|
|
int efd = epoll_create (100);
|
|
|
|
if (efd != -1)
|
|
|
|
{
|
|
|
|
main_loop_epoll (efd);
|
|
|
|
close (efd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
main_loop_poll ();
|
1998-01-31 16:39:55 +08:00
|
|
|
}
|
2002-01-18 10:10:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Look up the uid, gid, and supplementary groups to run nscd as. When
|
|
|
|
this function is called, we are not listening on the nscd socket yet so
|
|
|
|
we can just use the ordinary lookup functions without causing a lockup */
|
|
|
|
static void
|
|
|
|
begin_drop_privileges (void)
|
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
struct passwd *pwd = getpwnam (server_user);
|
2002-01-18 10:10:41 +08:00
|
|
|
|
|
|
|
if (pwd == NULL)
|
|
|
|
{
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (EXIT_FAILURE, 0,
|
|
|
|
_("Failed to run nscd as user '%s'"), server_user);
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
server_uid = pwd->pw_uid;
|
|
|
|
server_gid = pwd->pw_gid;
|
|
|
|
|
2004-10-04 03:33:48 +08:00
|
|
|
/* Save the old UID/GID if we have to change back. */
|
|
|
|
if (paranoia)
|
|
|
|
{
|
|
|
|
old_uid = getuid ();
|
|
|
|
old_gid = getgid ();
|
|
|
|
}
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
if (getgrouplist (server_user, server_gid, NULL, &server_ngroups) == 0)
|
|
|
|
{
|
|
|
|
/* This really must never happen. */
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (EXIT_FAILURE, errno,
|
|
|
|
_("initial getgrouplist failed"));
|
2004-08-27 02:35:05 +08:00
|
|
|
}
|
2002-01-18 10:10:41 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
server_groups = (gid_t *) xmalloc (server_ngroups * sizeof (gid_t));
|
2002-01-18 10:10:41 +08:00
|
|
|
|
|
|
|
if (getgrouplist (server_user, server_gid, server_groups, &server_ngroups)
|
|
|
|
== -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (EXIT_FAILURE, errno, _("getgrouplist failed"));
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Call setgroups(), setgid(), and setuid() to drop root privileges and
|
|
|
|
run nscd as the user specified in the configuration file. */
|
|
|
|
static void
|
|
|
|
finish_drop_privileges (void)
|
|
|
|
{
|
2006-04-27 00:29:29 +08:00
|
|
|
#if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
|
|
|
|
/* We need to preserve the capabilities to connect to the audit daemon. */
|
|
|
|
cap_t new_caps = preserve_capabilities ();
|
|
|
|
#endif
|
|
|
|
|
2002-01-18 10:10:41 +08:00
|
|
|
if (setgroups (server_ngroups, server_groups) == -1)
|
|
|
|
{
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (EXIT_FAILURE, errno, _("setgroups failed"));
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2006-04-29 01:01:50 +08:00
|
|
|
int res;
|
|
|
|
if (paranoia)
|
|
|
|
res = setresgid (server_gid, server_gid, old_gid);
|
|
|
|
else
|
|
|
|
res = setgid (server_gid);
|
|
|
|
if (res == -1)
|
2002-01-18 10:10:41 +08:00
|
|
|
{
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (4, errno, "setgid");
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2006-04-29 01:01:50 +08:00
|
|
|
if (paranoia)
|
|
|
|
res = setresuid (server_uid, server_uid, old_uid);
|
|
|
|
else
|
|
|
|
res = setuid (server_uid);
|
|
|
|
if (res == -1)
|
2002-01-18 10:10:41 +08:00
|
|
|
{
|
|
|
|
dbg_log (_("Failed to run nscd as user '%s'"), server_user);
|
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-04 01:21:39 +08:00
|
|
|
do_exit (4, errno, "setuid");
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|
2006-04-27 00:29:29 +08:00
|
|
|
|
|
|
|
#if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
|
|
|
|
/* Remove the temporary capabilities. */
|
|
|
|
install_real_capabilities (new_caps);
|
|
|
|
#endif
|
2002-01-18 10:10:41 +08:00
|
|
|
}
|