s3compat-only s3:winbindd Split event handlers from winbindd.c
authorAndrew Bartlett <abartlet@samba.org>
Wed, 26 May 2010 01:04:51 +0000 (11:04 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 21 Sep 2010 11:39:54 +0000 (21:39 +1000)
This allows the s3compat layer to register the handlers, without
having a conflicting main() function.

Andrew Bartlett

source3/Makefile.in
source3/winbindd/winbindd.c
source3/winbindd/winbindd_event_handlers.c [new file with mode: 0644]
source3/winbindd/winbindd_proto.h

index 4bcf6443a7d7d0f5f8321ecfa2146ce23b8c7926..20e0de9fb93801ef1d5cf96539855a77c497f028 100644 (file)
@@ -1293,6 +1293,7 @@ IDMAP_ADEX_OBJ = \
 WINBINDD_OBJ1 = \
                winbindd/winbindd.o       \
                winbindd/winbindd_event.o \
+               winbindd/winbindd_event_handlers.o \
                winbindd/winbindd_process.o \
                winbindd/winbindd_group.o \
                winbindd/winbindd_util.o  \
index 72aabdb8bd29541013c23f6e26332d0224304f69..7551f0b8c014e3f5f2f757c99df7b8c73cb8c1c1 100644 (file)
@@ -31,7 +31,6 @@
 #include "../librpc/gen_ndr/srv_lsa.h"
 #include "../librpc/gen_ndr/srv_samr.h"
 #include "secrets.h"
-#include "idmap.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
@@ -41,36 +40,6 @@ static bool interactive = False;
 
 extern bool override_logfile;
 
-/* Reload configuration */
-
-static bool reload_services_file(const char *lfile)
-{
-       bool ret;
-
-       if (lp_loaded()) {
-               const char *fname = lp_configfile();
-
-               if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
-                       set_dyn_CONFIGFILE(fname);
-               }
-       }
-
-       /* if this is a child, restore the logfile to the special
-          name - <domain>, idmap, etc. */
-       if (lfile && *lfile) {
-               lp_set_logfile(lfile);
-       }
-
-       reopen_logs();
-       ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
-
-       reopen_logs();
-       load_interfaces();
-
-       return(ret);
-}
-
-
 /**************************************************************************** **
  Handle a fault..
  **************************************************************************** */
@@ -80,337 +49,6 @@ static void fault_quit(void)
        dump_core();
 }
 
-static void winbindd_status(void)
-{
-       struct winbindd_cli_state *tmp;
-
-       DEBUG(0, ("winbindd status:\n"));
-
-       /* Print client state information */
-
-       DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
-
-       if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
-               DEBUG(2, ("\tclient list:\n"));
-               for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
-                       DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
-                                    (unsigned long)tmp->pid, tmp->sock,
-                                    client_is_idle(tmp) ? "idle" : "active"));
-               }
-       }
-}
-
-/* Flush client cache */
-
-static void flush_caches(void)
-{
-       /* We need to invalidate cached user list entries on a SIGHUP 
-           otherwise cached access denied errors due to restrict anonymous
-           hang around until the sequence number changes. */
-
-       if (!wcache_invalidate_cache()) {
-               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
-               if (!winbindd_cache_validate_and_initialize()) {
-                       exit(1);
-               }
-       }
-}
-
-static void flush_caches_noinit(void)
-{
-       /*
-        * We need to invalidate cached user list entries on a SIGHUP
-         * otherwise cached access denied errors due to restrict anonymous
-         * hang around until the sequence number changes.
-        * NB
-        * Skip uninitialized domains when flush cache.
-        * If domain is not initialized, it means it is never
-        * used or never become online. look, wcache_invalidate_cache()
-        * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
-        * for unused domains and large traffic for primay domain's DC if there
-        * are many domains..
-        */
-
-       if (!wcache_invalidate_cache_noinit()) {
-               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
-               if (!winbindd_cache_validate_and_initialize()) {
-                       exit(1);
-               }
-       }
-}
-
-/* Handle the signal by unlinking socket and exiting */
-
-static void terminate(bool is_parent)
-{
-       if (is_parent) {
-               /* When parent goes away we should
-                * remove the socket file. Not so
-                * when children terminate.
-                */ 
-               char *path = NULL;
-
-               if (asprintf(&path, "%s/%s",
-                       get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
-                       unlink(path);
-                       SAFE_FREE(path);
-               }
-       }
-
-       idmap_close();
-
-       trustdom_cache_shutdown();
-
-       gencache_stabilize();
-
-#if 0
-       if (interactive) {
-               TALLOC_CTX *mem_ctx = talloc_init("end_description");
-               char *description = talloc_describe_all(mem_ctx);
-
-               DEBUG(3, ("tallocs left:\n%s\n", description));
-               talloc_destroy(mem_ctx);
-       }
-#endif
-
-       if (is_parent) {
-               serverid_deregister(procid_self());
-               pidfile_unlink();
-       }
-
-       exit(0);
-}
-
-static void winbindd_sig_term_handler(struct tevent_context *ev,
-                                     struct tevent_signal *se,
-                                     int signum,
-                                     int count,
-                                     void *siginfo,
-                                     void *private_data)
-{
-       bool *is_parent = talloc_get_type_abort(private_data, bool);
-
-       DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
-                signum, (int)*is_parent));
-       terminate(*is_parent);
-}
-
-bool winbindd_setup_sig_term_handler(bool parent)
-{
-       struct tevent_signal *se;
-       bool *is_parent;
-
-       is_parent = talloc(winbind_event_context(), bool);
-       if (!is_parent) {
-               return false;
-       }
-
-       *is_parent = parent;
-
-       se = tevent_add_signal(winbind_event_context(),
-                              is_parent,
-                              SIGTERM, 0,
-                              winbindd_sig_term_handler,
-                              is_parent);
-       if (!se) {
-               DEBUG(0,("failed to setup SIGTERM handler"));
-               talloc_free(is_parent);
-               return false;
-       }
-
-       se = tevent_add_signal(winbind_event_context(),
-                              is_parent,
-                              SIGINT, 0,
-                              winbindd_sig_term_handler,
-                              is_parent);
-       if (!se) {
-               DEBUG(0,("failed to setup SIGINT handler"));
-               talloc_free(is_parent);
-               return false;
-       }
-
-       se = tevent_add_signal(winbind_event_context(),
-                              is_parent,
-                              SIGQUIT, 0,
-                              winbindd_sig_term_handler,
-                              is_parent);
-       if (!se) {
-               DEBUG(0,("failed to setup SIGINT handler"));
-               talloc_free(is_parent);
-               return false;
-       }
-
-       return true;
-}
-
-static void winbindd_sig_hup_handler(struct tevent_context *ev,
-                                    struct tevent_signal *se,
-                                    int signum,
-                                    int count,
-                                    void *siginfo,
-                                    void *private_data)
-{
-       const char *file = (const char *)private_data;
-
-       DEBUG(1,("Reloading services after SIGHUP\n"));
-       flush_caches_noinit();
-       reload_services_file(file);
-}
-
-bool winbindd_setup_sig_hup_handler(const char *lfile)
-{
-       struct tevent_signal *se;
-       char *file = NULL;
-
-       if (lfile) {
-               file = talloc_strdup(winbind_event_context(),
-                                    lfile);
-               if (!file) {
-                       return false;
-               }
-       }
-
-       se = tevent_add_signal(winbind_event_context(),
-                              winbind_event_context(),
-                              SIGHUP, 0,
-                              winbindd_sig_hup_handler,
-                              file);
-       if (!se) {
-               return false;
-       }
-
-       return true;
-}
-
-static void winbindd_sig_chld_handler(struct tevent_context *ev,
-                                     struct tevent_signal *se,
-                                     int signum,
-                                     int count,
-                                     void *siginfo,
-                                     void *private_data)
-{
-       pid_t pid;
-
-       while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
-               winbind_child_died(pid);
-       }
-}
-
-static bool winbindd_setup_sig_chld_handler(void)
-{
-       struct tevent_signal *se;
-
-       se = tevent_add_signal(winbind_event_context(),
-                              winbind_event_context(),
-                              SIGCHLD, 0,
-                              winbindd_sig_chld_handler,
-                              NULL);
-       if (!se) {
-               return false;
-       }
-
-       return true;
-}
-
-static void winbindd_sig_usr2_handler(struct tevent_context *ev,
-                                     struct tevent_signal *se,
-                                     int signum,
-                                     int count,
-                                     void *siginfo,
-                                     void *private_data)
-{
-       winbindd_status();
-}
-
-static bool winbindd_setup_sig_usr2_handler(void)
-{
-       struct tevent_signal *se;
-
-       se = tevent_add_signal(winbind_event_context(),
-                              winbind_event_context(),
-                              SIGUSR2, 0,
-                              winbindd_sig_usr2_handler,
-                              NULL);
-       if (!se) {
-               return false;
-       }
-
-       return true;
-}
-
-/* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
-static void msg_reload_services(struct messaging_context *msg,
-                               void *private_data,
-                               uint32_t msg_type,
-                               struct server_id server_id,
-                               DATA_BLOB *data)
-{
-        /* Flush various caches */
-       flush_caches();
-       reload_services_file((const char *) private_data);
-}
-
-/* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
-static void msg_shutdown(struct messaging_context *msg,
-                        void *private_data,
-                        uint32_t msg_type,
-                        struct server_id server_id,
-                        DATA_BLOB *data)
-{
-       /* only the parent waits for this message */
-       DEBUG(0,("Got shutdown message\n"));
-       terminate(true);
-}
-
-
-static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
-                                      void *private_data,
-                                      uint32_t msg_type,
-                                      struct server_id server_id,
-                                      DATA_BLOB *data)
-{
-       uint8 ret;
-       pid_t child_pid;
-
-       DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
-                  "message.\n"));
-
-       /*
-        * call the validation code from a child:
-        * so we don't block the main winbindd and the validation
-        * code can safely use fork/waitpid...
-        */
-       child_pid = sys_fork();
-
-       if (child_pid == -1) {
-               DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
-                         strerror(errno)));
-               return;
-       }
-
-       if (child_pid != 0) {
-               /* parent */
-               DEBUG(5, ("winbind_msg_validate_cache: child created with "
-                         "pid %d.\n", (int)child_pid));
-               return;
-       }
-
-       /* child */
-
-       if (!winbindd_reinit_after_fork(NULL)) {
-               _exit(0);
-       }
-
-       /* install default SIGCHLD handler: validation code uses fork/waitpid */
-       CatchSignal(SIGCHLD, SIG_DFL);
-
-       ret = (uint8)winbindd_validate_cache_nobackup();
-       DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
-       messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
-                          (size_t)1);
-       _exit(0);
-}
-
 bool winbindd_use_idmap_cache(void)
 {
        return !opt_nocache;
@@ -421,94 +59,6 @@ bool winbindd_use_cache(void)
        return !opt_nocache;
 }
 
-void winbindd_register_handlers(void)
-{
-       struct tevent_timer *te;
-       /* Setup signal handlers */
-
-       if (!winbindd_setup_sig_term_handler(true))
-               exit(1);
-       if (!winbindd_setup_sig_hup_handler(NULL))
-               exit(1);
-       if (!winbindd_setup_sig_chld_handler())
-               exit(1);
-       if (!winbindd_setup_sig_usr2_handler())
-               exit(1);
-
-       CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
-
-       /*
-        * Ensure all cache and idmap caches are consistent
-        * and initialized before we startup.
-        */
-       if (!winbindd_cache_validate_and_initialize()) {
-               exit(1);
-       }
-
-       /* get broadcast messages */
-
-       if (!serverid_register(procid_self(),
-                              FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
-               DEBUG(1, ("Could not register myself in serverid.tdb\n"));
-               exit(1);
-       }
-
-       /* React on 'smbcontrol winbindd reload-config' in the same way
-          as to SIGHUP signal */
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_SMB_CONF_UPDATED, msg_reload_services);
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_SHUTDOWN, msg_shutdown);
-
-       /* Handle online/offline messages. */
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_WINBIND_OFFLINE, winbind_msg_offline);
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_WINBIND_ONLINE, winbind_msg_online);
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
-
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
-
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_WINBIND_VALIDATE_CACHE,
-                          winbind_msg_validate_cache);
-
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_WINBIND_DUMP_DOMAIN_LIST,
-                          winbind_msg_dump_domain_list);
-
-       /* Register handler for MSG_DEBUG. */
-       messaging_register(winbind_messaging_context(), NULL,
-                          MSG_DEBUG,
-                          winbind_msg_debug);
-
-       netsamlogon_cache_init(); /* Non-critical */
-
-       /* clear the cached list of trusted domains */
-
-       wcache_tdc_clear();
-
-       if (!init_domain_list()) {
-               DEBUG(0,("unable to initialize domain list\n"));
-               exit(1);
-       }
-
-       init_idmap_child();
-       init_locator_child();
-
-       smb_nscd_flush_user_cache();
-       smb_nscd_flush_group_cache();
-
-       te = tevent_add_timer(winbind_event_context(), NULL, timeval_zero(),
-                             rescan_trusted_domains, NULL);
-       if (te == NULL) {
-               DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
-               exit(1);
-       }
-
-}
 int main(int argc, char **argv, char **envp)
 {
        static bool is_daemon = False;
@@ -638,7 +188,7 @@ int main(int argc, char **argv, char **envp)
                exit(1);
        }
 
-       if (!reload_services_file(NULL)) {
+       if (!winbindd_reload_services_file(NULL)) {
                DEBUG(0, ("error opening config file\n"));
                exit(1);
        }
diff --git a/source3/winbindd/winbindd_event_handlers.c b/source3/winbindd/winbindd_event_handlers.c
new file mode 100644 (file)
index 0000000..9781b6d
--- /dev/null
@@ -0,0 +1,483 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Winbind daemon for ntdom nss module
+
+   Copyright (C) by Tim Potter 2000-2002
+   Copyright (C) Andrew Tridgell 2002
+   Copyright (C) Jelmer Vernooij 2003
+   Copyright (C) Volker Lendecke 2004
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "winbindd.h"
+#include "../../nsswitch/libwbclient/wbc_async.h"
+#include "librpc/gen_ndr/messaging.h"
+#include "idmap.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_WINBIND
+
+/* Main function */
+
+/* Reload configuration */
+
+bool winbindd_reload_services_file(const char *lfile)
+{
+       bool ret;
+
+       if (lp_loaded()) {
+               const char *fname = lp_configfile();
+
+               if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
+                       set_dyn_CONFIGFILE(fname);
+               }
+       }
+
+       /* if this is a child, restore the logfile to the special
+          name - <domain>, idmap, etc. */
+       if (lfile && *lfile) {
+               lp_set_logfile(lfile);
+       }
+
+       reopen_logs();
+       ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
+
+       reopen_logs();
+       load_interfaces();
+
+       return(ret);
+}
+
+static void winbindd_status(void)
+{
+       struct winbindd_cli_state *tmp;
+
+       DEBUG(0, ("winbindd status:\n"));
+
+       /* Print client state information */
+
+       DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
+
+       if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
+               DEBUG(2, ("\tclient list:\n"));
+               for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
+                       DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
+                                    (unsigned long)tmp->pid, tmp->sock,
+                                    client_is_idle(tmp) ? "idle" : "active"));
+               }
+       }
+}
+
+/* Flush client cache */
+
+static void flush_caches(void)
+{
+       /* We need to invalidate cached user list entries on a SIGHUP 
+           otherwise cached access denied errors due to restrict anonymous
+           hang around until the sequence number changes. */
+
+       if (!wcache_invalidate_cache()) {
+               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
+               if (!winbindd_cache_validate_and_initialize()) {
+                       exit(1);
+               }
+       }
+}
+
+static void flush_caches_noinit(void)
+{
+       /*
+        * We need to invalidate cached user list entries on a SIGHUP
+         * otherwise cached access denied errors due to restrict anonymous
+         * hang around until the sequence number changes.
+        * NB
+        * Skip uninitialized domains when flush cache.
+        * If domain is not initialized, it means it is never
+        * used or never become online. look, wcache_invalidate_cache()
+        * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
+        * for unused domains and large traffic for primay domain's DC if there
+        * are many domains..
+        */
+
+       if (!wcache_invalidate_cache_noinit()) {
+               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
+               if (!winbindd_cache_validate_and_initialize()) {
+                       exit(1);
+               }
+       }
+}
+
+/* Handle the signal by unlinking socket and exiting */
+
+static void terminate(bool is_parent)
+{
+       if (is_parent) {
+               /* When parent goes away we should
+                * remove the socket file. Not so
+                * when children terminate.
+                */ 
+               char *path = NULL;
+
+               if (asprintf(&path, "%s/%s",
+                       get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
+                       unlink(path);
+                       SAFE_FREE(path);
+               }
+       }
+
+       idmap_close();
+
+       trustdom_cache_shutdown();
+
+       gencache_stabilize();
+
+#if 0
+       if (interactive) {
+               TALLOC_CTX *mem_ctx = talloc_init("end_description");
+               char *description = talloc_describe_all(mem_ctx);
+
+               DEBUG(3, ("tallocs left:\n%s\n", description));
+               talloc_destroy(mem_ctx);
+       }
+#endif
+
+       if (is_parent) {
+               serverid_deregister(procid_self());
+               pidfile_unlink();
+       }
+
+       exit(0);
+}
+
+static void winbindd_sig_term_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       bool *is_parent = talloc_get_type_abort(private_data, bool);
+
+       DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
+                signum, (int)*is_parent));
+       terminate(*is_parent);
+}
+
+bool winbindd_setup_sig_term_handler(bool parent)
+{
+       struct tevent_signal *se;
+       bool *is_parent;
+
+       is_parent = talloc(winbind_event_context(), bool);
+       if (!is_parent) {
+               return false;
+       }
+
+       *is_parent = parent;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGTERM, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGTERM handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGINT, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGINT handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              is_parent,
+                              SIGQUIT, 0,
+                              winbindd_sig_term_handler,
+                              is_parent);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGINT handler"));
+               talloc_free(is_parent);
+               return false;
+       }
+
+       return true;
+}
+
+static void winbindd_sig_hup_handler(struct tevent_context *ev,
+                                    struct tevent_signal *se,
+                                    int signum,
+                                    int count,
+                                    void *siginfo,
+                                    void *private_data)
+{
+       const char *file = (const char *)private_data;
+
+       DEBUG(1,("Reloading services after SIGHUP\n"));
+       flush_caches_noinit();
+       winbindd_reload_services_file(file);
+}
+
+bool winbindd_setup_sig_hup_handler(const char *lfile)
+{
+       struct tevent_signal *se;
+       char *file = NULL;
+
+       if (lfile) {
+               file = talloc_strdup(winbind_event_context(),
+                                    lfile);
+               if (!file) {
+                       return false;
+               }
+       }
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGHUP, 0,
+                              winbindd_sig_hup_handler,
+                              file);
+       if (!se) {
+               return false;
+       }
+
+       return true;
+}
+
+static void winbindd_sig_chld_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       pid_t pid;
+
+       while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
+               winbind_child_died(pid);
+       }
+}
+
+static bool winbindd_setup_sig_chld_handler(void)
+{
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGCHLD, 0,
+                              winbindd_sig_chld_handler,
+                              NULL);
+       if (!se) {
+               return false;
+       }
+
+       return true;
+}
+
+static void winbindd_sig_usr2_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       winbindd_status();
+}
+
+static bool winbindd_setup_sig_usr2_handler(void)
+{
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(winbind_event_context(),
+                              winbind_event_context(),
+                              SIGUSR2, 0,
+                              winbindd_sig_usr2_handler,
+                              NULL);
+       if (!se) {
+               return false;
+       }
+
+       return true;
+}
+
+/* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
+static void msg_reload_services(struct messaging_context *msg,
+                               void *private_data,
+                               uint32_t msg_type,
+                               struct server_id server_id,
+                               DATA_BLOB *data)
+{
+        /* Flush various caches */
+       flush_caches();
+       winbindd_reload_services_file((const char *) private_data);
+}
+
+/* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
+static void msg_shutdown(struct messaging_context *msg,
+                        void *private_data,
+                        uint32_t msg_type,
+                        struct server_id server_id,
+                        DATA_BLOB *data)
+{
+       /* only the parent waits for this message */
+       DEBUG(0,("Got shutdown message\n"));
+       terminate(true);
+}
+
+
+static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
+                                      void *private_data,
+                                      uint32_t msg_type,
+                                      struct server_id server_id,
+                                      DATA_BLOB *data)
+{
+       uint8 ret;
+       pid_t child_pid;
+
+       DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
+                  "message.\n"));
+
+       /*
+        * call the validation code from a child:
+        * so we don't block the main winbindd and the validation
+        * code can safely use fork/waitpid...
+        */
+       child_pid = sys_fork();
+
+       if (child_pid == -1) {
+               DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
+                         strerror(errno)));
+               return;
+       }
+
+       if (child_pid != 0) {
+               /* parent */
+               DEBUG(5, ("winbind_msg_validate_cache: child created with "
+                         "pid %d.\n", (int)child_pid));
+               return;
+       }
+
+       /* child */
+
+       if (!winbindd_reinit_after_fork(NULL)) {
+               _exit(0);
+       }
+
+       /* install default SIGCHLD handler: validation code uses fork/waitpid */
+       CatchSignal(SIGCHLD, SIG_DFL);
+
+       ret = (uint8)winbindd_validate_cache_nobackup();
+       DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
+       messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
+                          (size_t)1);
+       _exit(0);
+}
+
+void winbindd_register_handlers(void)
+{
+       struct tevent_timer *te;
+       /* Setup signal handlers */
+
+       if (!winbindd_setup_sig_term_handler(true))
+               exit(1);
+       if (!winbindd_setup_sig_hup_handler(NULL))
+               exit(1);
+       if (!winbindd_setup_sig_chld_handler())
+               exit(1);
+       if (!winbindd_setup_sig_usr2_handler())
+               exit(1);
+
+       CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
+
+       /*
+        * Ensure all cache and idmap caches are consistent
+        * and initialized before we startup.
+        */
+       if (!winbindd_cache_validate_and_initialize()) {
+               exit(1);
+       }
+
+       /* get broadcast messages */
+
+       if (!serverid_register(procid_self(),
+                              FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
+               DEBUG(1, ("Could not register myself in serverid.tdb\n"));
+               exit(1);
+       }
+
+       /* React on 'smbcontrol winbindd reload-config' in the same way
+          as to SIGHUP signal */
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_SMB_CONF_UPDATED, msg_reload_services);
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_SHUTDOWN, msg_shutdown);
+
+       /* Handle online/offline messages. */
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_WINBIND_OFFLINE, winbind_msg_offline);
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_WINBIND_ONLINE, winbind_msg_online);
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
+
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
+
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_WINBIND_VALIDATE_CACHE,
+                          winbind_msg_validate_cache);
+
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_WINBIND_DUMP_DOMAIN_LIST,
+                          winbind_msg_dump_domain_list);
+
+       /* Register handler for MSG_DEBUG. */
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_DEBUG,
+                          winbind_msg_debug);
+
+       netsamlogon_cache_init(); /* Non-critical */
+
+       /* clear the cached list of trusted domains */
+
+       wcache_tdc_clear();
+
+       if (!init_domain_list()) {
+               DEBUG(0,("unable to initialize domain list\n"));
+               exit(1);
+       }
+
+       init_idmap_child();
+       init_locator_child();
+
+       smb_nscd_flush_user_cache();
+       smb_nscd_flush_group_cache();
+
+       te = tevent_add_timer(winbind_event_context(), NULL, timeval_zero(),
+                             rescan_trusted_domains, NULL);
+       if (te == NULL) {
+               DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
+               exit(1);
+       }
+
+}
index 5b89db4a0c749cfa50d2930b53c4023dfbecd4eb..18c2d85a62be36e547acb6ff1ffc956642e90a80 100644 (file)
@@ -48,8 +48,10 @@ void request_finished(struct winbindd_cli_state *state);
 void winbindd_remove_client(struct winbindd_cli_state *state);
 bool client_is_idle(struct winbindd_cli_state *state);
 
-/* The following definitions come from winbindd/winbindd.c  */
+/* The following definitions come from winbindd/winbindd_event_handlers.c  */
 struct messaging_context *winbind_messaging_context(void);
+bool winbindd_reload_services_file(const char *lfile);
+void winbindd_register_handlers(void);
 
 /* The following definitions come from winbindd/winbindd_process.c  */
 void wb_process_request(struct winbindd_cli_state *state);
@@ -61,8 +63,6 @@ bool winbindd_setup_sig_term_handler(bool parent);
 bool winbindd_setup_sig_hup_handler(const char *lfile);
 bool winbindd_use_idmap_cache(void);
 bool winbindd_use_cache(void);
-void winbindd_register_handlers(void);
-int main(int argc, char **argv, char **envp);
 
 /* The following definitions come from winbindd/winbindd_ads.c  */