diff options
Diffstat (limited to 'nscd')
-rw-r--r-- | nscd/nscd-client.h | 22 | ||||
-rw-r--r-- | nscd/nscd.c | 58 | ||||
-rw-r--r-- | nscd/nscd_gethst_r.c | 21 | ||||
-rw-r--r-- | nscd/nscd_helper.c | 15 |
4 files changed, 82 insertions, 34 deletions
diff --git a/nscd/nscd-client.h b/nscd/nscd-client.h index e57a23c057..aef76e8fdd 100644 --- a/nscd/nscd-client.h +++ b/nscd/nscd-client.h @@ -1,5 +1,4 @@ -/* Copyright (c) 1998, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2009, 2011 - Free Software Foundation, Inc. +/* Copyright (c) 1998-2012 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998. @@ -322,6 +321,25 @@ struct locked_map_ptr }; #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name +/* Try acquiring lock for mapptr, returns true if it succeeds, false + if not. */ +static inline bool +__nscd_acquire_maplock (volatile struct locked_map_ptr *mapptr) +{ + int cnt = 0; + while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock, + 1, 0) != 0, 0)) + { + // XXX Best number of rounds? + if (__builtin_expect (++cnt > 5, 0)) + return false; + + atomic_delay (); + } + + return true; +} + /* Open socket connection to nscd server. */ extern int __nscd_open_socket (const char *key, size_t keylen, diff --git a/nscd/nscd.c b/nscd/nscd.c index 9cd7273bbf..79fb32f327 100644 --- a/nscd/nscd.c +++ b/nscd/nscd.c @@ -1,4 +1,4 @@ -/* Copyright (c) 1998-2011, 2012 Free Software Foundation, Inc. +/* Copyright (c) 1998-2012 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998. @@ -71,7 +71,20 @@ thread_info_t thread_info; int do_shutdown; int disabled_passwd; int disabled_group; -int go_background = 1; + +typedef enum +{ + /* Running in background as daemon. */ + RUN_DAEMONIZE, + /* Running in foreground but otherwise behave like a daemon, + i.e., detach from terminal and use syslog. This allows + better integration with services like systemd. */ + RUN_FOREGROUND, + /* Run in foreground in debug mode. */ + RUN_DEBUG +} run_modes; + +static run_modes run_mode = RUN_DAEMONIZE; static const char *conffile = _PATH_NSCDCONF; @@ -103,6 +116,8 @@ static const struct argp_option options[] = N_("Read configuration data from NAME") }, { "debug", 'd', NULL, 0, N_("Do not fork and display messages on the current tty") }, + { "foreground", 'F', NULL, 0, + N_("Do not fork, but otherwise behave like a daemon") }, { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") }, { "shutdown", 'K', NULL, 0, N_("Shut the server down") }, { "statistics", 'g', NULL, 0, N_("Print current configuration statistics") }, @@ -173,16 +188,20 @@ main (int argc, char **argv) /* Determine page size. */ pagesize_m1 = getpagesize () - 1; - /* Behave like a daemon. */ - if (go_background) + if (run_mode == RUN_DAEMONIZE || run_mode == RUN_FOREGROUND) { int i; + pid_t pid; - pid_t pid = fork (); - if (pid == -1) - error (EXIT_FAILURE, errno, _("cannot fork")); - if (pid != 0) - exit (0); + /* Behave like a daemon. */ + if (run_mode == RUN_DAEMONIZE) + { + pid = fork (); + if (pid == -1) + error (EXIT_FAILURE, errno, _("cannot fork")); + if (pid != 0) + exit (0); + } int nullfd = open (_PATH_DEVNULL, O_RDWR); if (nullfd != -1) @@ -233,11 +252,14 @@ main (int argc, char **argv) for (i = min_close_fd; i < getdtablesize (); i++) close (i); - pid = fork (); - if (pid == -1) - error (EXIT_FAILURE, errno, _("cannot fork")); - if (pid != 0) - exit (0); + if (run_mode == RUN_DAEMONIZE) + { + pid = fork (); + if (pid == -1) + error (EXIT_FAILURE, errno, _("cannot fork")); + if (pid != 0) + exit (0); + } setsid (); @@ -259,7 +281,7 @@ main (int argc, char **argv) signal (SIGTSTP, SIG_IGN); } else - /* In foreground mode we are not paranoid. */ + /* In debug mode we are not paranoid. */ paranoia = 0; signal (SIGINT, termination_handler); @@ -308,7 +330,11 @@ parse_opt (int key, char *arg, struct argp_state *state) { case 'd': ++debug_level; - go_background = 0; + run_mode = RUN_DEBUG; + break; + + case 'F': + run_mode = RUN_FOREGROUND; break; case 'f': diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c index c1661f86d4..d64ad2e7b6 100644 --- a/nscd/nscd_gethst_r.c +++ b/nscd/nscd_gethst_r.c @@ -1,5 +1,4 @@ -/* Copyright (C) 1998-2005, 2006, 2007, 2008, 2009, 2011 - Free Software Foundation, Inc. +/* Copyright (C) 1998-2012 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. @@ -100,9 +99,18 @@ libc_freeres_fn (hst_map_free) uint32_t __nscd_get_nl_timestamp (void) { + uint32_t retval; if (__nss_not_use_nscd_hosts != 0) return 0; + /* __nscd_get_mapping can change hst_map_handle.mapped to NO_MAPPING. + However, __nscd_get_mapping assumes the prior value was not NO_MAPPING. + Thus we have to acquire the lock to prevent this thread from changing + hst_map_handle.mapped to NO_MAPPING while another thread is inside + __nscd_get_mapping. */ + if (!__nscd_acquire_maplock (&__hst_map_handle)) + return 0; + struct mapped_database *map = __hst_map_handle.mapped; if (map == NULL @@ -112,9 +120,14 @@ __nscd_get_nl_timestamp (void) map = __nscd_get_mapping (GETFDHST, "hosts", &__hst_map_handle.mapped); if (map == NO_MAPPING) - return 0; + retval = 0; + else + retval = map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP]; + + /* Release the lock. */ + __hst_map_handle.lock = 0; - return map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP]; + return retval; } diff --git a/nscd/nscd_helper.c b/nscd/nscd_helper.c index 92558b6a51..96fb93db76 100644 --- a/nscd/nscd_helper.c +++ b/nscd/nscd_helper.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1998-2007, 2008, 2009 Free Software Foundation, Inc. +/* Copyright (C) 1998-2012 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. @@ -419,7 +419,6 @@ __nscd_get_mapping (request_type type, const char *key, return result; } - struct mapped_database * __nscd_get_map_ref (request_type type, const char *name, volatile struct locked_map_ptr *mapptr, int *gc_cyclep) @@ -428,16 +427,8 @@ __nscd_get_map_ref (request_type type, const char *name, if (cur == NO_MAPPING) return cur; - int cnt = 0; - while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock, - 1, 0) != 0, 0)) - { - // XXX Best number of rounds? - if (__builtin_expect (++cnt > 5, 0)) - return NO_MAPPING; - - atomic_delay (); - } + if (!__nscd_acquire_maplock (mapptr)) + return NO_MAPPING; cur = mapptr->mapped; |