s3:winbindd: move non event related code out of process_loop() in the the caller
[ira/wip.git] / source3 / winbindd / winbindd.c
index e881ab412ecafac1ca22fe1f3e950efd810e2c05..d090b6f994afd5c2dbba20d9c73cce370ef3f604 100644 (file)
@@ -28,7 +28,7 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-bool opt_nocache = False;
+static bool opt_nocache = False;
 static bool interactive = False;
 
 extern bool override_logfile;
@@ -173,36 +173,161 @@ static void terminate(bool is_parent)
        exit(0);
 }
 
-static SIG_ATOMIC_T do_sigterm = 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);
+}
 
-static void termination_handler(int signum)
+bool winbindd_setup_sig_term_handler(bool parent)
 {
-       do_sigterm = 1;
-       sys_select_signal(signum);
+       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 SIG_ATOMIC_T do_sigusr2 = 0;
+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;
 
-static void sigusr2_handler(int signum)
+       DEBUG(1,("Reloading services after SIGHUP\n"));
+       flush_caches();
+       reload_services_file(file);
+}
+
+bool winbindd_setup_sig_hup_handler(const char *lfile)
 {
-       do_sigusr2 = 1;
-       sys_select_signal(SIGUSR2);
+       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 SIG_ATOMIC_T do_sighup = 0;
+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 void sighup_handler(int signum)
+static bool winbindd_setup_sig_chld_handler(void)
 {
-       do_sighup = 1;
-       sys_select_signal(SIGHUP);
+       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 SIG_ATOMIC_T do_sigchld = 0;
+static void winbindd_sig_usr2_handler(struct tevent_context *ev,
+                                     struct tevent_signal *se,
+                                     int signum,
+                                     int count,
+                                     void *siginfo,
+                                     void *private_data)
+{
+       print_winbindd_status();
+}
 
-static void sigchld_handler(int signum)
+static bool winbindd_setup_sig_usr2_handler(void)
 {
-       do_sigchld = 1;
-       sys_select_signal(SIGCHLD);
+       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*/
@@ -224,7 +349,9 @@ static void msg_shutdown(struct messaging_context *msg,
                         struct server_id server_id,
                         DATA_BLOB *data)
 {
-       do_sigterm = 1;
+       /* only the parent waits for this message */
+       DEBUG(0,("Got shutdown message\n"));
+       terminate(true);
 }
 
 
@@ -259,7 +386,7 @@ static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
        if (child_pid != 0) {
                /* parent */
                DEBUG(5, ("winbind_msg_validate_cache: child created with "
-                         "pid %d.\n", child_pid));
+                         "pid %d.\n", (int)child_pid));
                return;
        }
 
@@ -293,6 +420,7 @@ static struct winbindd_dispatch_table {
 
        { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
        { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
+       { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
 
        { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
        { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
@@ -302,6 +430,8 @@ static struct winbindd_dispatch_table {
        { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
        { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
          "GETUSERDOMGROUPS" },
+       { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
+          "LOOKUPUSERALIASES" },
 
        /* Group functions */
 
@@ -415,16 +545,16 @@ static void process_request(struct winbindd_cli_state *state)
 
 /*
  * A list of file descriptors being monitored by select in the main processing
- * loop. fd_event->handler is called whenever the socket is readable/writable.
+ * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
  */
 
-static struct fd_event *fd_events = NULL;
+static struct winbindd_fd_event *fd_events = NULL;
 
-void add_fd_event(struct fd_event *ev)
+void add_fd_event(struct winbindd_fd_event *ev)
 {
-       struct fd_event *match;
+       struct winbindd_fd_event *match;
 
-       /* only add unique fd_event structs */
+       /* only add unique winbindd_fd_event structs */
 
        for (match=fd_events; match; match=match->next ) {
 #ifdef DEVELOPER
@@ -438,17 +568,17 @@ void add_fd_event(struct fd_event *ev)
        DLIST_ADD(fd_events, ev);
 }
 
-void remove_fd_event(struct fd_event *ev)
+void remove_fd_event(struct winbindd_fd_event *ev)
 {
        DLIST_REMOVE(fd_events, ev);
 }
 
 /*
- * Handler for fd_events to complete a read/write request, set up by
+ * Handler for winbindd_fd_events to complete a read/write request, set up by
  * setup_async_read/setup_async_write.
  */
 
-static void rw_callback(struct fd_event *event, int flags)
+static void rw_callback(struct winbindd_fd_event *event, int flags)
 {
        size_t todo;
        ssize_t done = 0;
@@ -489,11 +619,11 @@ static void rw_callback(struct fd_event *event, int flags)
 }
 
 /*
- * Request an async read/write on a fd_event structure. (*finished) is called
+ * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
  * when the request is completed or an error had occurred.
  */
 
-void setup_async_read(struct fd_event *event, void *data, size_t length,
+void setup_async_read(struct winbindd_fd_event *event, void *data, size_t length,
                      void (*finished)(void *private_data, bool success),
                      void *private_data)
 {
@@ -507,7 +637,7 @@ void setup_async_read(struct fd_event *event, void *data, size_t length,
        event->flags = EVENT_FD_READ;
 }
 
-void setup_async_write(struct fd_event *event, void *data, size_t length,
+void setup_async_write(struct winbindd_fd_event *event, void *data, size_t length,
                       void (*finished)(void *private_data, bool success),
                       void *private_data)
 {
@@ -797,27 +927,6 @@ static bool remove_idle_client(void)
        return False;
 }
 
-/* check if HUP has been received and reload files */
-void winbind_check_sighup(const char *lfile)
-{
-       if (do_sighup) {
-
-               DEBUG(3, ("got SIGHUP\n"));
-
-               flush_caches();
-               reload_services_file(lfile);
-
-               do_sighup = 0;
-       }
-}
-
-/* check if TERM has been received */
-void winbind_check_sigterm(bool is_parent)
-{
-       if (do_sigterm)
-               terminate(is_parent);
-}
-
 /* Process incoming clients on listen_sock.  We use a tricky non-blocking,
    non-forking, non-threaded model which allows us to handle many
    simultaneous connections while remaining impervious to many denial of
@@ -825,8 +934,7 @@ void winbind_check_sigterm(bool is_parent)
 
 static void process_loop(void)
 {
-       struct winbindd_cli_state *state;
-       struct fd_event *ev;
+       struct winbindd_fd_event *ev;
        fd_set r_fds, w_fds;
        int maxfd, listen_sock, listen_priv_sock, selret;
        struct timeval timeout, ev_timeout;
@@ -840,18 +948,8 @@ static void process_loop(void)
                exit(1);
        }
 
-       /* We'll be doing this a lot */
-
-       /* Handle messages */
-
-       message_dispatch(winbind_messaging_context());
-
        run_events(winbind_event_context(), 0, NULL, NULL);
 
-       /* refresh the trusted domain cache */
-
-       rescan_trusted_domains();
-
        /* Initialise fd lists for select() */
 
        maxfd = MAX(listen_sock, listen_priv_sock);
@@ -865,27 +963,17 @@ static void process_loop(void)
        timeout.tv_usec = 0;
 
        /* Check for any event timeouts. */
+       {
+               struct timeval now;
+               GetTimeOfDay(&now);
+
+               event_add_to_select_args(winbind_event_context(), &now,
+                                        &r_fds, &w_fds, &ev_timeout, &maxfd);
+       }
        if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
                timeout = timeval_min(&timeout, &ev_timeout);
        }
 
-       /* Set up client readers and writers */
-
-       state = winbindd_client_list();
-
-       while (state) {
-
-               struct winbindd_cli_state *next = state->next;
-
-               /* Dispose of client connection if it is marked as 
-                  finished */ 
-
-               if (state->finished)
-                       remove_client(state);
-
-               state = next;
-       }
-
        for (ev = fd_events; ev; ev = ev->next) {
                if (ev->flags & EVENT_FD_READ) {
                        FD_SET(ev->fd, &r_fds);
@@ -918,9 +1006,11 @@ static void process_loop(void)
 
        /* selret > 0 */
 
+       run_events(winbind_event_context(), selret, &r_fds, &w_fds);
+
        ev = fd_events;
        while (ev != NULL) {
-               struct fd_event *next = ev->next;
+               struct winbindd_fd_event *next = ev->next;
                int flags = 0;
                if (FD_ISSET(ev->fd, &r_fds))
                        flags |= EVENT_FD_READ;
@@ -971,29 +1061,21 @@ static void process_loop(void)
 
  no_fds_ready:
 
+       run_events(winbind_event_context(), selret, &r_fds, &w_fds);
+
 #if 0
        winbindd_check_cache_size(time(NULL));
 #endif
+}
 
-       /* Check signal handling things */
-
-       winbind_check_sigterm(true);
-       winbind_check_sighup(NULL);
-
-       if (do_sigusr2) {
-               print_winbindd_status();
-               do_sigusr2 = 0;
-       }
-
-       if (do_sigchld) {
-               pid_t pid;
-
-               do_sigchld = 0;
+bool winbindd_use_idmap_cache(void)
+{
+       return !opt_nocache;
+}
 
-               while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
-                       winbind_child_died(pid);
-               }
-       }
+bool winbindd_use_cache(void)
+{
+       return !opt_nocache;
 }
 
 /* Main function */
@@ -1112,7 +1194,7 @@ int main(int argc, char **argv, char **envp)
        setup_logging("winbindd", log_stdout);
        reopen_logs();
 
-       DEBUG(0,("winbindd version %s started.\n", SAMBA_VERSION_STRING));
+       DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
        DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
 
        if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
@@ -1163,18 +1245,6 @@ int main(int argc, char **argv, char **envp)
        BlockSignals(False, SIGHUP);
        BlockSignals(False, SIGCHLD);
 
-       /* Setup signal handlers */
-
-       CatchSignal(SIGINT, termination_handler);      /* Exit on these sigs */
-       CatchSignal(SIGQUIT, termination_handler);
-       CatchSignal(SIGTERM, termination_handler);
-       CatchSignal(SIGCHLD, sigchld_handler);
-
-       CatchSignal(SIGPIPE, SIG_IGN);                 /* Ignore sigpipe */
-
-       CatchSignal(SIGUSR2, sigusr2_handler);         /* Debugging sigs */
-       CatchSignal(SIGHUP, sighup_handler);
-
        if (!interactive)
                become_daemon(Fork, no_process_group);
 
@@ -1191,11 +1261,30 @@ int main(int argc, char **argv, char **envp)
 
        TimeInit();
 
-       if (!reinit_after_fork(winbind_messaging_context(), false)) {
+       /* Don't use winbindd_reinit_after_fork here as
+        * we're just starting up and haven't created any
+        * winbindd-specific resources we must free yet. JRA.
+        */
+
+       if (!reinit_after_fork(winbind_messaging_context(),
+                              winbind_event_context(), false)) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                exit(1);
        }
 
+       /* 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.
@@ -1259,8 +1348,29 @@ int main(int argc, char **argv, char **envp)
 
        TALLOC_FREE(frame);
        while (1) {
+               struct winbindd_cli_state *state;
+
                frame = talloc_stackframe();
+
+               /* refresh the trusted domain cache */
+
+               rescan_trusted_domains();
+
+               /* Dispose of client connection if it is marked as
+                  finished */
+               state = winbindd_client_list();
+               while (state) {
+                       struct winbindd_cli_state *next = state->next;
+
+                       if (state->finished) {
+                               remove_client(state);
+                       }
+
+                       state = next;
+               }
+
                process_loop();
+
                TALLOC_FREE(frame);
        }