s4-kcc: add a very simple KCC
[kai/samba.git] / source4 / smbd / server.c
index a62846b091100002c9b7982f38bd79dbf2d99ae2..73dbec01200832db3d17f10df9fe2710c19c39ff 100644 (file)
@@ -28,7 +28,6 @@
 #include "lib/cmdline/popt_common.h"
 #include "system/dir.h"
 #include "system/filesys.h"
-#include "build.h"
 #include "ldb/include/ldb.h"
 #include "registry/registry.h"
 #include "ntvfs/ntvfs.h"
@@ -38,8 +37,9 @@
 #include "smbd/service.h"
 #include "param/secrets.h"
 #include "smbd/pidfile.h"
-#include "cluster/ctdb/ctdb_cluster.h"
 #include "param/param.h"
+#include "dsdb/samdb/samdb.h"
+#include "auth/session.h"
 
 /*
   recursively delete a directory tree
@@ -114,6 +114,7 @@ static void sig_term(int sig)
                kill(-getpgrp(), SIGTERM);
        }
 #endif
+       DEBUG(0,("Exiting pid %d on SIGTERM\n", (int)getpid()));
        exit(0);
 }
 
@@ -150,15 +151,16 @@ static void setup_signals(void)
 /*
   handle io on stdin
 */
-static void server_stdin_handler(struct event_context *event_ctx, struct fd_event *fde, 
-                                uint16_t flags, void *private)
+static void server_stdin_handler(struct tevent_context *event_ctx, struct tevent_fd *fde, 
+                                uint16_t flags, void *private_data)
 {
-       const char *binary_name = (const char *)private;
+       const char *binary_name = (const char *)private_data;
        uint8_t c;
        if (read(0, &c, 1) == 0) {
                DEBUG(0,("%s: EOF on stdin - terminating\n", binary_name));
 #if HAVE_GETPGRP
                if (getpgrp() == getpid()) {
+                       DEBUG(0,("Sending SIGTERM from pid %d\n", (int)getpid()));
                        kill(-getpgrp(), SIGTERM);
                }
 #endif
@@ -169,15 +171,27 @@ static void server_stdin_handler(struct event_context *event_ctx, struct fd_even
 /*
   die if the user selected maximum runtime is exceeded
 */
-_NORETURN_ static void max_runtime_handler(struct event_context *ev, 
-                                          struct timed_event *te, 
-                                          struct timeval t, void *private)
+_NORETURN_ static void max_runtime_handler(struct tevent_context *ev, 
+                                          struct tevent_timer *te, 
+                                          struct timeval t, void *private_data)
 {
-       const char *binary_name = (const char *)private;
+       const char *binary_name = (const char *)private_data;
        DEBUG(0,("%s: maximum runtime exceeded - terminating\n", binary_name));
        exit(0);
 }
 
+/*
+  pre-open the sam ldb to ensure the schema has been loaded. This
+  saves a lot of time in child processes  
+ */
+static void prime_samdb_schema(struct tevent_context *event_ctx)
+{
+       TALLOC_CTX *samdb_context;
+       samdb_context = talloc_new(event_ctx);
+       samdb_connect(samdb_context, event_ctx, cmdline_lp_ctx, system_session(samdb_context, cmdline_lp_ctx));
+       talloc_free(samdb_context);
+}
+
 /*
  main server.
 */
@@ -187,9 +201,25 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        bool opt_interactive = false;
        int opt;
        poptContext pc;
-       init_module_fn static_init[] = STATIC_service_MODULES;
+       extern NTSTATUS server_service_wrepl_init(void);
+       extern NTSTATUS server_service_kdc_init(void);
+       extern NTSTATUS server_service_ldap_init(void);
+       extern NTSTATUS server_service_web_init(void);
+       extern NTSTATUS server_service_ldap_init(void);
+       extern NTSTATUS server_service_winbind_init(void);
+       extern NTSTATUS server_service_nbtd_init(void);
+       extern NTSTATUS server_service_auth_init(void);
+       extern NTSTATUS server_service_cldapd_init(void);
+       extern NTSTATUS server_service_smb_init(void);
+       extern NTSTATUS server_service_drepl_init(void);
+       extern NTSTATUS server_service_kcc_init(void);
+       extern NTSTATUS server_service_rpc_init(void);
+       extern NTSTATUS server_service_ntp_signd_init(void);
+       extern NTSTATUS server_service_samba3_smb_init(void);
+       init_module_fn static_init[] = { STATIC_service_MODULES };
        init_module_fn *shared_init;
-       struct event_context *event_ctx;
+       struct tevent_context *event_ctx;
+       uint16_t stdin_event_flags;
        NTSTATUS status;
        const char *model = "standard";
        int max_runtime = 0;
@@ -226,7 +256,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
                        model = poptGetOptArg(pc);
                        break;
                default:
-                       d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                       fprintf(stderr, "\nInvalid option %s: %s\n\n",
                                  poptBadOption(pc, 0), poptStrerror(opt));
                        poptPrintUsage(pc, stderr, 0);
                        exit(1);
@@ -234,7 +264,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        }
 
        if (opt_daemon && opt_interactive) {
-               d_fprintf(stderr,"\nERROR: "
+               fprintf(stderr,"\nERROR: "
                          "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
                poptPrintUsage(pc, stderr, 0);
                exit(1);
@@ -253,7 +283,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        umask(0);
 
        DEBUG(0,("%s version %s started.\n", binary_name, SAMBA_VERSION_STRING));
-       DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2007\n"));
+       DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2009\n"));
 
        if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4 || sizeof(uint64_t) < 8) {
                DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
@@ -264,39 +294,35 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
 
        if (opt_daemon) {
                DEBUG(3,("Becoming a daemon.\n"));
-               become_daemon(true);
+               become_daemon(true, false);
        }
 
-       cleanup_tmp_files(global_loadparm);
+       cleanup_tmp_files(cmdline_lp_ctx);
 
-       if (!directory_exist(lp_lockdir(global_loadparm))) {
-               mkdir(lp_lockdir(global_loadparm), 0755);
+       if (!directory_exist(lp_lockdir(cmdline_lp_ctx))) {
+               mkdir(lp_lockdir(cmdline_lp_ctx), 0755);
        }
 
-       pidfile_create(lp_piddir(global_loadparm), binary_name);
+       pidfile_create(lp_piddir(cmdline_lp_ctx), binary_name);
 
        /* Do *not* remove this, until you have removed
         * passdb/secrets.c, and proved that Samba still builds... */
        /* Setup the SECRETS subsystem */
-       if (!secrets_init(global_loadparm)) {
+       if (secrets_init(talloc_autofree_context(), cmdline_lp_ctx) == NULL) {
                exit(1);
        }
 
-       ldb_global_init(); /* FIXME: */
-
-       share_init(global_loadparm);
-
-       gensec_init(global_loadparm); /* FIXME: */
+       gensec_init(cmdline_lp_ctx); /* FIXME: */
 
-       ntptr_init(global_loadparm);    /* FIXME: maybe run this in the initialization function 
+       ntptr_init(cmdline_lp_ctx);     /* FIXME: maybe run this in the initialization function 
                                                of the spoolss RPC server instead? */
 
-       ntvfs_init();   /* FIXME: maybe run this in the initialization functions 
+       ntvfs_init(cmdline_lp_ctx);     /* FIXME: maybe run this in the initialization functions 
                                                of the SMB[,2] server instead? */
 
-       process_model_init(global_loadparm); 
+       process_model_init(cmdline_lp_ctx); 
 
-       shared_init = load_samba_modules(NULL, global_loadparm, "service");
+       shared_init = load_samba_modules(NULL, cmdline_lp_ctx, "service");
 
        run_init_functions(static_init);
        run_init_functions(shared_init);
@@ -305,36 +331,41 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        
        /* the event context is the top level structure in smbd. Everything else
           should hang off that */
-       event_ctx = event_context_init(talloc_autofree_context());
+       event_ctx = s4_event_context_init(talloc_autofree_context());
 
        if (event_ctx == NULL) {
                DEBUG(0,("Initializing event context failed\n"));
                return 1;
        }
 
-       /* initialise clustering if needed */
-       cluster_ctdb_init(global_loadparm, event_ctx, model);
-
        if (opt_interactive) {
-               /* catch EOF on stdin */
-#ifdef SIGTTIN
-               signal(SIGTTIN, SIG_IGN);
-#endif
-               event_add_fd(event_ctx, event_ctx, 0, EVENT_FD_READ, 
-                            server_stdin_handler,
-                            discard_const(binary_name));
+               /* terminate when stdin goes away */
+               stdin_event_flags = TEVENT_FD_READ;
+       } else {
+               /* stay alive forever */
+               stdin_event_flags = 0;
        }
 
+       /* catch EOF on stdin */
+#ifdef SIGTTIN
+       signal(SIGTTIN, SIG_IGN);
+#endif
+       tevent_add_fd(event_ctx, event_ctx, 0, stdin_event_flags,
+                     server_stdin_handler,
+                     discard_const(binary_name));
 
        if (max_runtime) {
-               event_add_timed(event_ctx, event_ctx, 
-                               timeval_current_ofs(max_runtime, 0), 
-                               max_runtime_handler,
-                               discard_const(binary_name));
+               tevent_add_timer(event_ctx, event_ctx,
+                                timeval_current_ofs(max_runtime, 0),
+                                max_runtime_handler,
+                                discard_const(binary_name));
        }
 
+       prime_samdb_schema(event_ctx);
+
        DEBUG(0,("%s: using '%s' process model\n", binary_name, model));
-       status = server_service_startup(event_ctx, global_loadparm, model, lp_server_services(global_loadparm));
+       status = server_service_startup(event_ctx, cmdline_lp_ctx, model, 
+                                       lp_server_services(cmdline_lp_ctx));
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Starting Services failed - %s\n", nt_errstr(status)));
                return 1;
@@ -342,7 +373,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
 
        /* wait for events - this is where smbd sits for most of its
           life */
-       event_loop_wait(event_ctx);
+       tevent_loop_wait(event_ctx);
 
        /* as everything hangs off this event context, freeing it
           should initiate a clean shutdown of all services */
@@ -353,5 +384,5 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
 
  int main(int argc, const char *argv[])
 {
-       return binary_smbd_main("smbd", argc, argv);
+       return binary_smbd_main("samba", argc, argv);
 }