[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[jerry/samba.git] / source / winbindd / winbindd.c
index 17915fb01b1613db9630d2f2691494949c3a2aa3..81f07c4c8c5a0aad2acf03f6fba44cf132f2c1a6 100644 (file)
@@ -7,7 +7,6 @@
    Copyright (C) Andrew Tridgell 2002
    Copyright (C) Jelmer Vernooij 2003
    Copyright (C) Volker Lendecke 2004
-   Copyright (C) James Peach 2007
    
    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
@@ -30,6 +29,7 @@
 #define DBGC_CLASS DBGC_WINBIND
 
 BOOL opt_nocache = False;
+static BOOL interactive = False;
 
 extern BOOL override_logfile;
 
@@ -129,8 +129,13 @@ static void flush_caches(void)
 
 static void terminate(void)
 {
+       pstring path;
+
+       /* Remove socket file */
+       pstr_sprintf(path, "%s/%s", 
+                get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME);
+       unlink(path);
 
-       winbindd_release_sockets();
        idmap_close();
        
        trustdom_cache_shutdown();
@@ -791,14 +796,23 @@ static BOOL remove_idle_client(void)
    simultaneous connections while remaining impervious to many denial of
    service attacks. */
 
-static int process_loop(int listen_sock, int listen_priv_sock)
+static void process_loop(void)
 {
        struct winbindd_cli_state *state;
        struct fd_event *ev;
        fd_set r_fds, w_fds;
-       int maxfd, selret;
+       int maxfd, listen_sock, listen_priv_sock, selret;
        struct timeval timeout, ev_timeout;
 
+       /* Open Sockets here to get stuff going ASAP */
+       listen_sock = open_winbindd_socket();
+       listen_priv_sock = open_winbindd_priv_socket();
+
+       if (listen_sock == -1 || listen_priv_sock == -1) {
+               perror("open_winbind_socket");
+               exit(1);
+       }
+
        /* We'll be doing this a lot */
 
        /* Handle messages */
@@ -963,58 +977,6 @@ static int process_loop(int listen_sock, int listen_priv_sock)
                        winbind_child_died(pid);
                }
        }
-
-
-       return winbindd_num_clients();
-}
-
-static void winbindd_process_loop(enum smb_server_mode server_mode)
-{
-       int idle_timeout_sec;
-       struct timeval starttime;
-       int listen_public, listen_priv;
-
-       errno = 0;
-       if (!winbindd_init_sockets(&listen_public, &listen_priv,
-                                   &idle_timeout_sec)) {
-               terminate();
-       }
-
-       starttime = timeval_current();
-
-       if (listen_public == -1 || listen_priv == -1) {
-               DEBUG(0, ("failed to open winbindd pipes: %s\n",
-                           errno ? strerror(errno) : "unknown error"));
-               terminate();
-       }
-
-       for (;;) {
-               TALLOC_CTX *frame = talloc_stackframe();
-               int clients = process_loop(listen_public, listen_priv);
-
-               /* Don't bother figuring out the idle time if we won't be
-                * timing out anyway.
-                */
-               if (idle_timeout_sec < 0) {
-                       TALLOC_FREE(frame);
-                       continue;
-               }
-
-               if (clients == 0 && server_mode == SERVER_MODE_FOREGROUND) {
-                       struct timeval now;
-
-                       now = timeval_current();
-                       if (timeval_elapsed2(&starttime, &now) >
-                               (double)idle_timeout_sec) {
-                               DEBUG(0, ("idle for %d secs, exitting\n",
-                                           idle_timeout_sec));
-                               terminate();
-                       }
-               } else {
-                       starttime = timeval_current();
-               }
-               TALLOC_FREE(frame);
-       }
 }
 
 /* Main function */
@@ -1022,18 +984,17 @@ static void winbindd_process_loop(enum smb_server_mode server_mode)
 int main(int argc, char **argv, char **envp)
 {
        pstring logfile;
-       BOOL log_stdout = False;
-       BOOL no_process_group = False;
-
-       enum smb_server_mode server_mode = SERVER_MODE_DAEMON;
-
+       static BOOL is_daemon = False;
+       static BOOL Fork = True;
+       static BOOL log_stdout = False;
+       static BOOL no_process_group = False;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
-               { "foreground", 'F', POPT_ARG_VAL, &server_mode, SERVER_MODE_FOREGROUND, "Daemon in foreground mode" },
+               { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
                { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
-               { "daemon", 'D', POPT_ARG_VAL, &server_mode, SERVER_MODE_DAEMON, "Become a daemon (default)" },
-               { "interactive", 'i', POPT_ARG_VAL, &server_mode, SERVER_MODE_INTERACTIVE, "Interactive mode" },
+               { "daemon", 'D', POPT_ARG_NONE, NULL, 'D', "Become a daemon (default)" },
+               { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
                { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
                POPT_COMMON_SAMBA
                POPT_TABLEEND
@@ -1072,6 +1033,15 @@ int main(int argc, char **argv, char **envp)
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
+                       /* Don't become a daemon */
+               case 'D':
+                       is_daemon = True;
+                       break;
+               case 'i':
+                       interactive = True;
+                       log_stdout = True;
+                       Fork = False;
+                       break;
                default:
                        d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
                                  poptBadOption(pc, 0), poptStrerror(opt));
@@ -1080,15 +1050,16 @@ int main(int argc, char **argv, char **envp)
                }
        }
 
-       if (server_mode == SERVER_MODE_INTERACTIVE) {
-               log_stdout = True;
-               if (DEBUGLEVEL >= 9) {
-                       talloc_enable_leak_report();
-               }
+       if (is_daemon && interactive) {
+               d_fprintf(stderr,"\nERROR: "
+                         "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
+               poptPrintUsage(pc, stderr, 0);
+               exit(1);
        }
 
-       if (log_stdout && server_mode == SERVER_MODE_DAEMON) {
-               printf("Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n");
+       if (log_stdout && Fork) {
+               d_fprintf(stderr, "\nERROR: "
+                         "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
                poptPrintUsage(pc, stderr, 0);
                exit(1);
        }
@@ -1161,12 +1132,8 @@ int main(int argc, char **argv, char **envp)
        CatchSignal(SIGUSR2, sigusr2_handler);         /* Debugging sigs */
        CatchSignal(SIGHUP, sighup_handler);
 
-       if (server_mode == SERVER_MODE_DAEMON) {
-               DEBUG( 3, ( "Becoming a daemon.\n" ) );
-               become_daemon(True, no_process_group);
-       } else if (server_mode == SERVER_MODE_FOREGROUND) {
-               become_daemon(False, no_process_group);
-       }
+       if (!interactive)
+               become_daemon(Fork, no_process_group);
 
        pidfile_create("winbindd");
 
@@ -1189,9 +1156,8 @@ int main(int argc, char **argv, char **envp)
         * If we're interactive we want to set our own process group for
         * signal management.
         */
-       if (server_mode == SERVER_MODE_INTERACTIVE && !no_process_group) {
+       if (interactive && !no_process_group)
                setpgid( (pid_t)0, (pid_t)0);
-       }
 #endif
 
        TimeInit();
@@ -1248,7 +1214,12 @@ int main(int argc, char **argv, char **envp)
        smb_nscd_flush_group_cache();
 
        /* Loop waiting for requests */
-       winbindd_process_loop(server_mode);
+
+       while (1) {
+               TALLOC_CTX *frame = talloc_stackframe();
+               process_loop();
+               TALLOC_FREE(frame);
+       }
 
        return 0;
 }