s3: piddir creation fix part 2.
[ira/wip.git] / source3 / nmbd / nmbd.c
index ceec2fdf9d9eb2149b94255e19238c4989e56725..52d7ed9e4e7d4a948711214f9c02efc1d86501b0 100644 (file)
@@ -47,30 +47,20 @@ struct event_context *nmbd_event_context(void)
        return server_event_context();
 }
 
-struct messaging_context *nmbd_messaging_context(void)
-{
-       struct messaging_context *msg_ctx = server_messaging_context();
-       if (likely(msg_ctx != NULL)) {
-               return msg_ctx;
-       }
-       smb_panic("Could not init nmbd's messaging context.\n");
-       return NULL;
-}
-
 /**************************************************************************** **
  Handle a SIGTERM in band.
  **************************************************************************** */
 
-static void terminate(void)
+static void terminate(struct messaging_context *msg)
 {
        DEBUG(0,("Got SIGTERM: going down...\n"));
-  
+
        /* Write out wins.dat file if samba is a WINS server */
        wins_write_database(0,False);
-  
+
        /* Remove all SELF registered names from WINS */
        release_wins_names();
-  
+
        /* Announce all server entries as 0 time-to-live, 0 type. */
        announce_my_servers_removed();
 
@@ -78,7 +68,7 @@ static void terminate(void)
        kill_async_dns_child();
 
        gencache_stabilize();
-       serverid_deregister(procid_self());
+       serverid_deregister(messaging_server_id(msg));
 
        pidfile_unlink();
 
@@ -92,10 +82,31 @@ static void nmbd_sig_term_handler(struct tevent_context *ev,
                                  void *siginfo,
                                  void *private_data)
 {
-       terminate();
+       struct messaging_context *msg = talloc_get_type_abort(
+               private_data, struct messaging_context);
+
+       terminate(msg);
 }
 
-static bool nmbd_setup_sig_term_handler(void)
+/*
+  handle stdin becoming readable when we are in --foreground mode
+ */
+static void nmbd_stdin_handler(struct tevent_context *ev,
+                              struct tevent_fd *fde,
+                              uint16_t flags,
+                              void *private_data)
+{
+       char c;
+       if (read(0, &c, 1) != 1) {
+               struct messaging_context *msg = talloc_get_type_abort(
+                       private_data, struct messaging_context);
+               
+               DEBUG(0,("EOF on stdin\n"));
+               terminate(msg);
+       }
+}
+
+static bool nmbd_setup_sig_term_handler(struct messaging_context *msg)
 {
        struct tevent_signal *se;
 
@@ -103,7 +114,7 @@ static bool nmbd_setup_sig_term_handler(void)
                               nmbd_event_context(),
                               SIGTERM, 0,
                               nmbd_sig_term_handler,
-                              NULL);
+                              msg);
        if (!se) {
                DEBUG(0,("failed to setup SIGTERM handler"));
                return false;
@@ -112,6 +123,19 @@ static bool nmbd_setup_sig_term_handler(void)
        return true;
 }
 
+static bool nmbd_setup_stdin_handler(struct messaging_context *msg, bool foreground)
+{
+       if (foreground) {
+               /* if we are running in the foreground then look for
+                  EOF on stdin, and exit if it happens. This allows
+                  us to die if the parent process dies
+               */
+               tevent_add_fd(nmbd_event_context(), nmbd_event_context(), 0, TEVENT_FD_READ, nmbd_stdin_handler, msg);
+       }
+
+       return true;
+}
+
 static void msg_reload_nmbd_services(struct messaging_context *msg,
                                     void *private_data,
                                     uint32_t msg_type,
@@ -125,13 +149,15 @@ static void nmbd_sig_hup_handler(struct tevent_context *ev,
                                 void *siginfo,
                                 void *private_data)
 {
+       struct messaging_context *msg = talloc_get_type_abort(
+               private_data, struct messaging_context);
+
        DEBUG(0,("Got SIGHUP dumping debug info.\n"));
-       msg_reload_nmbd_services(nmbd_messaging_context(),
-                                NULL, MSG_SMB_CONF_UPDATED,
-                                procid_self(), NULL);
+       msg_reload_nmbd_services(msg, NULL, MSG_SMB_CONF_UPDATED,
+                                messaging_server_id(msg), NULL);
 }
 
-static bool nmbd_setup_sig_hup_handler(void)
+static bool nmbd_setup_sig_hup_handler(struct messaging_context *msg)
 {
        struct tevent_signal *se;
 
@@ -139,7 +165,7 @@ static bool nmbd_setup_sig_hup_handler(void)
                               nmbd_event_context(),
                               SIGHUP, 0,
                               nmbd_sig_hup_handler,
-                              NULL);
+                              msg);
        if (!se) {
                DEBUG(0,("failed to setup SIGHUP handler"));
                return false;
@@ -158,7 +184,7 @@ static void nmbd_terminate(struct messaging_context *msg,
                           struct server_id server_id,
                           DATA_BLOB *data)
 {
-       terminate();
+       terminate(msg);
 }
 
 /**************************************************************************** **
@@ -168,7 +194,7 @@ static void nmbd_terminate(struct messaging_context *msg,
 static void expire_names_and_servers(time_t t)
 {
        static time_t lastrun = 0;
-  
+
        if ( !lastrun )
                lastrun = t;
        if ( t < (lastrun + 5) )
@@ -459,7 +485,7 @@ static void msg_nmbd_send_packet(struct messaging_context *msg,
  The main select loop.
  **************************************************************************** */
 
-static void process(void)
+static void process(struct messaging_context *msg)
 {
        bool run_election;
 
@@ -480,7 +506,7 @@ static void process(void)
                 * (nmbd_packets.c)
                 */
 
-               if(listen_for_packets(run_election)) {
+               if (listen_for_packets(msg, run_election)) {
                        TALLOC_FREE(frame);
                        return;
                }
@@ -730,14 +756,15 @@ static bool open_sockets(bool isdaemon, int port)
 
  int main(int argc, const char *argv[])
 {
-       static bool is_daemon;
-       static bool opt_interactive;
-       static bool Fork = true;
-       static bool no_process_group;
-       static bool log_stdout;
+       bool is_daemon = false;
+       bool opt_interactive = false;
+       bool Fork = true;
+       bool no_process_group = false;
+       bool log_stdout = false;
        poptContext pc;
        char *p_lmhosts = NULL;
        int opt;
+       struct messaging_context *msg;
        enum {
                OPT_DAEMON = 1000,
                OPT_INTERACTIVE,
@@ -800,11 +827,11 @@ static bool open_sockets(bool isdaemon, int port)
        poptFreeContext(pc);
 
        global_in_nmbd = true;
-       
+
        StartupTime = time(NULL);
-       
+
        sys_srandom(time(NULL) ^ sys_getpid());
-       
+
        if (!override_logfile) {
                char *lfile = NULL;
                if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
@@ -813,10 +840,10 @@ static bool open_sockets(bool isdaemon, int port)
                lp_set_logfile(lfile);
                SAFE_FREE(lfile);
        }
-       
+
        fault_setup();
        dump_core_setup("nmbd", lp_logfile());
-       
+
        /* POSIX demands that signals are inherited. If the invoking process has
         * these signals masked, we will have problems, as we won't receive them. */
        BlockSignals(False, SIGHUP);
@@ -859,7 +886,8 @@ static bool open_sockets(bool isdaemon, int port)
                exit(1);
        }
 
-       if (nmbd_messaging_context() == NULL) {
+       msg = messaging_init(NULL, server_event_context());
+       if (msg == NULL) {
                return 1;
        }
 
@@ -882,7 +910,7 @@ static bool open_sockets(bool isdaemon, int port)
                DEBUG(0,("standard input is not a socket, assuming -D option\n"));
                is_daemon = True;
        }
-  
+
        if (is_daemon && !opt_interactive) {
                DEBUG( 2, ( "Becoming a daemon.\n" ) );
                become_daemon(Fork, no_process_group, log_stdout);
@@ -897,15 +925,11 @@ static bool open_sockets(bool isdaemon, int port)
                setpgid( (pid_t)0, (pid_t)0 );
 #endif
 
-       if (nmbd_messaging_context() == NULL) {
-               return 1;
-       }
-
 #ifndef SYNC_DNS
        /* Setup the async dns. We do it here so it doesn't have all the other
                stuff initialised and thus chewing memory and sockets */
        if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
-               start_async_dns();
+               start_async_dns(msg);
        }
 #endif
 
@@ -913,43 +937,50 @@ static bool open_sockets(bool isdaemon, int port)
                mkdir(lp_lockdir(), 0755);
        }
 
+       if (!directory_exist(lp_piddir())) {
+               mkdir(lp_piddir(), 0755);
+       }
+
        pidfile_create("nmbd");
 
-       status = reinit_after_fork(nmbd_messaging_context(),
-                                  nmbd_event_context(),
-                                  procid_self(), false);
+       status = reinit_after_fork(msg, nmbd_event_context(),
+                                  false);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                exit(1);
        }
 
-       if (!nmbd_setup_sig_term_handler())
+       if (!nmbd_setup_sig_term_handler(msg))
+               exit(1);
+       if (!nmbd_setup_stdin_handler(msg, !Fork))
                exit(1);
-       if (!nmbd_setup_sig_hup_handler())
+       if (!nmbd_setup_sig_hup_handler(msg))
                exit(1);
 
        /* get broadcast messages */
 
-       if (!serverid_register(procid_self(),
-                              FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
+       if (!serverid_register(messaging_server_id(msg),
+                               FLAG_MSG_GENERAL |
+                               FLAG_MSG_NMBD |
+                               FLAG_MSG_DBWRAP)) {
                DEBUG(1, ("Could not register myself in serverid.tdb\n"));
                exit(1);
        }
 
-       messaging_register(nmbd_messaging_context(), NULL,
-                          MSG_FORCE_ELECTION, nmbd_message_election);
+       messaging_register(msg, NULL, MSG_FORCE_ELECTION,
+                          nmbd_message_election);
 #if 0
        /* Until winsrepl is done. */
-       messaging_register(nmbd_messaging_context(), NULL,
-                          MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
+       messaging_register(msg, NULL, MSG_WINS_NEW_ENTRY,
+                          nmbd_wins_new_entry);
 #endif
-       messaging_register(nmbd_messaging_context(), NULL,
-                          MSG_SHUTDOWN, nmbd_terminate);
-       messaging_register(nmbd_messaging_context(), NULL,
-                          MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
-       messaging_register(nmbd_messaging_context(), NULL,
-                          MSG_SEND_PACKET, msg_nmbd_send_packet);
+       messaging_register(msg, NULL, MSG_SHUTDOWN,
+                          nmbd_terminate);
+       messaging_register(msg, NULL, MSG_SMB_CONF_UPDATED,
+                          msg_reload_nmbd_services);
+       messaging_register(msg, NULL, MSG_SEND_PACKET,
+                          msg_nmbd_send_packet);
 
        TimeInit();
 
@@ -1010,7 +1041,7 @@ static bool open_sockets(bool isdaemon, int port)
         }
 
        TALLOC_FREE(frame);
-       process();
+       process(msg);
 
        kill_async_dns_child();
        return(0);