s3-nmbd: Remove obsolete signal type cast.
[ira/wip.git] / source3 / nmbd / nmbd.c
index 00d252940afc33bcf34e9d98dca13c89b15b0408..148361f9f93484748d2031af7892b75c6a0ff20b 100644 (file)
@@ -52,9 +52,12 @@ struct messaging_context *nmbd_messaging_context(void)
 {
        static struct messaging_context *ctx;
 
-       if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
-                                          nmbd_event_context()))) {
-               smb_panic("Could not init nmbd messaging context");
+       if (ctx == NULL) {
+               ctx = messaging_init(NULL, server_id_self(),
+                                    nmbd_event_context());
+       }
+       if (ctx == NULL) {
+               DEBUG(0, ("Could not init nmbd messaging context.\n"));
        }
        return ctx;
 }
@@ -79,44 +82,87 @@ static void terminate(void)
        /* If there was an async dns child - kill it. */
        kill_async_dns_child();
 
+       gencache_stabilize();
+
+       pidfile_unlink();
+
        exit(0);
 }
 
-/**************************************************************************** **
- Handle a SHUTDOWN message from smbcontrol.
- **************************************************************************** */
-
-static void nmbd_terminate(struct messaging_context *msg,
-                          void *private_data,
-                          uint32_t msg_type,
-                          struct server_id server_id,
-                          DATA_BLOB *data)
+static void nmbd_sig_term_handler(struct tevent_context *ev,
+                                 struct tevent_signal *se,
+                                 int signum,
+                                 int count,
+                                 void *siginfo,
+                                 void *private_data)
 {
        terminate();
 }
 
-/**************************************************************************** **
- Catch a SIGTERM signal.
- **************************************************************************** */
+static bool nmbd_setup_sig_term_handler(void)
+{
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(nmbd_event_context(),
+                              nmbd_event_context(),
+                              SIGTERM, 0,
+                              nmbd_sig_term_handler,
+                              NULL);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGTERM handler"));
+               return false;
+       }
 
-static SIG_ATOMIC_T got_sig_term;
+       return true;
+}
 
-static void sig_term(int sig)
+static void msg_reload_nmbd_services(struct messaging_context *msg,
+                                    void *private_data,
+                                    uint32_t msg_type,
+                                    struct server_id server_id,
+                                    DATA_BLOB *data);
+
+static void nmbd_sig_hup_handler(struct tevent_context *ev,
+                                struct tevent_signal *se,
+                                int signum,
+                                int count,
+                                void *siginfo,
+                                void *private_data)
+{
+       DEBUG(0,("Got SIGHUP dumping debug info.\n"));
+       msg_reload_nmbd_services(nmbd_messaging_context(),
+                                NULL, MSG_SMB_CONF_UPDATED,
+                                procid_self(), NULL);
+}
+
+static bool nmbd_setup_sig_hup_handler(void)
 {
-       got_sig_term = 1;
-       sys_select_signal(SIGTERM);
+       struct tevent_signal *se;
+
+       se = tevent_add_signal(nmbd_event_context(),
+                              nmbd_event_context(),
+                              SIGHUP, 0,
+                              nmbd_sig_hup_handler,
+                              NULL);
+       if (!se) {
+               DEBUG(0,("failed to setup SIGHUP handler"));
+               return false;
+       }
+
+       return true;
 }
 
 /**************************************************************************** **
Catch a SIGHUP signal.
Handle a SHUTDOWN message from smbcontrol.
  **************************************************************************** */
 
-static SIG_ATOMIC_T reload_after_sighup;
-
-static void sig_hup(int sig)
+static void nmbd_terminate(struct messaging_context *msg,
+                          void *private_data,
+                          uint32_t msg_type,
+                          struct server_id server_id,
+                          DATA_BLOB *data)
 {
-       reload_after_sighup = 1;
-       sys_select_signal(SIGHUP);
+       terminate();
 }
 
 /**************************************************************************** **
@@ -170,6 +216,7 @@ static void reload_interfaces(time_t t)
 {
        static time_t lastt;
        int n;
+       bool print_waiting_msg = true;
        struct subnet_record *subrec;
 
        if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) {
@@ -182,12 +229,12 @@ static void reload_interfaces(time_t t)
                return;
        }
 
+  try_again:
+
        /* the list of probed interfaces has changed, we may need to add/remove
           some subnets */
        load_interfaces();
 
-  try_again:
-
        /* find any interfaces that need adding */
        for (n=iface_count() - 1; n >= 0; n--) {
                char str[INET6_ADDRSTRLEN];
@@ -215,7 +262,7 @@ static void reload_interfaces(time_t t)
                 * ignore it here. JRA.
                 */
 
-               if (is_loopback_addr(&iface->ip)) {
+               if (is_loopback_addr((struct sockaddr *)&iface->ip)) {
                        DEBUG(2,("reload_interfaces: Ignoring loopback "
                                "interface %s\n",
                                print_sockaddr(str, sizeof(str), &iface->ip) ));
@@ -280,27 +327,30 @@ static void reload_interfaces(time_t t)
        if (FIRST_SUBNET == NULL) {
                void (*saved_handler)(int);
 
-               DEBUG(0,("reload_interfaces: "
-                       "No subnets to listen to. Waiting..\n"));
+               if (print_waiting_msg) {
+                       DEBUG(0,("reload_interfaces: "
+                               "No subnets to listen to. Waiting..\n"));
+                       print_waiting_msg = false;
+               }
 
                /*
                 * Whilst we're waiting for an interface, allow SIGTERM to
                 * cause us to exit.
                 */
+               saved_handler = CatchSignal(SIGTERM, SIG_DFL);
 
-               saved_handler = CatchSignal( SIGTERM, SIGNAL_CAST SIG_DFL );
-
-               /* We only count IPv4 interfaces here. */
-               while (iface_count_v4() == 0) {
+               /* We only count IPv4, non-loopback interfaces here. */
+               while (iface_count_v4_nl() == 0) {
                        sleep(5);
                        load_interfaces();
                }
 
+               CatchSignal(SIGTERM, saved_handler);
+
                /*
-                * We got an interface, restore our normal term handler.
+                * We got an interface, go back to blocking term.
                 */
 
-               CatchSignal( SIGTERM, SIGNAL_CAST saved_handler );
                goto try_again;
        }
 }
@@ -317,7 +367,7 @@ static bool reload_nmbd_services(bool test)
 
        if ( lp_loaded() ) {
                const char *fname = lp_configfile();
-               if (file_exist(fname,NULL) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
+               if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
                        set_dyn_CONFIGFILE(fname);
                        test = False;
                }
@@ -366,40 +416,41 @@ static void msg_nmbd_send_packet(struct messaging_context *msg,
        const struct sockaddr_storage *pss;
        const struct in_addr *local_ip;
 
-       DEBUG(10, ("Received send_packet from %d\n", procid_to_pid(&src)));
+       DEBUG(10, ("Received send_packet from %u\n", (unsigned int)procid_to_pid(&src)));
 
        if (data->length != sizeof(struct packet_struct)) {
-               DEBUG(2, ("Discarding invalid packet length from %d\n",
-                         procid_to_pid(&src)));
+               DEBUG(2, ("Discarding invalid packet length from %u\n",
+                         (unsigned int)procid_to_pid(&src)));
                return;
        }
 
        if ((p->packet_type != NMB_PACKET) &&
            (p->packet_type != DGRAM_PACKET)) {
-               DEBUG(2, ("Discarding invalid packet type from %d: %d\n",
-                         procid_to_pid(&src), p->packet_type));
+               DEBUG(2, ("Discarding invalid packet type from %u: %d\n",
+                         (unsigned int)procid_to_pid(&src), p->packet_type));
                return;
        }
 
        in_addr_to_sockaddr_storage(&ss, p->ip);
-       pss = iface_ip(&ss);
+       pss = iface_ip((struct sockaddr *)&ss);
 
        if (pss == NULL) {
-               DEBUG(2, ("Could not find ip for packet from %d\n",
-                         procid_to_pid(&src)));
+               DEBUG(2, ("Could not find ip for packet from %u\n",
+                         (unsigned int)procid_to_pid(&src)));
                return;
        }
 
        local_ip = &((const struct sockaddr_in *)pss)->sin_addr;
        subrec = FIRST_SUBNET;
 
-       p->fd = (p->packet_type == NMB_PACKET) ?
+       p->recv_fd = -1;
+       p->send_fd = (p->packet_type == NMB_PACKET) ?
                subrec->nmb_sock : subrec->dgram_sock;
 
        for (subrec = FIRST_SUBNET; subrec != NULL;
             subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
                if (ip_equal_v4(*local_ip, subrec->myip)) {
-                       p->fd = (p->packet_type == NMB_PACKET) ?
+                       p->send_fd = (p->packet_type == NMB_PACKET) ?
                                subrec->nmb_sock : subrec->dgram_sock;
                        break;
                }
@@ -426,10 +477,6 @@ static void process(void)
                time_t t = time(NULL);
                TALLOC_CTX *frame = talloc_stackframe();
 
-               /* Check for internal messages */
-
-               message_dispatch(nmbd_messaging_context());
-
                /*
                 * Check all broadcast subnets to see if
                 * we need to run an election on any of them.
@@ -448,15 +495,6 @@ static void process(void)
                        return;
                }
 
-               /*
-                * Handle termination inband.
-                */
-
-               if (got_sig_term) {
-                       got_sig_term = 0;
-                       terminate();
-               }
-
                /*
                 * Process all incoming packets
                 * read above. This calls the success and
@@ -626,19 +664,6 @@ static void process(void)
 
                clear_unexpected(t);
 
-               /*
-                * Reload the services file if we got a sighup.
-                */
-
-               if(reload_after_sighup) {
-                       DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
-                       msg_reload_nmbd_services(nmbd_messaging_context(),
-                                                NULL, MSG_SMB_CONF_UPDATED,
-                                                procid_self(), NULL);
-
-                       reload_after_sighup = 0;
-               }
-
                /* check for new network interfaces */
 
                reload_interfaces(t);
@@ -788,12 +813,12 @@ static bool open_sockets(bool isdaemon, int port)
        sys_srandom(time(NULL) ^ sys_getpid());
        
        if (!override_logfile) {
-               char *logfile = NULL;
-               if (asprintf(&logfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
+               char *lfile = NULL;
+               if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
                        exit(1);
                }
-               lp_set_logfile(logfile);
-               SAFE_FREE(logfile);
+               lp_set_logfile(lfile);
+               SAFE_FREE(lfile);
        }
        
        fault_setup((void (*)(void *))fault_continue );
@@ -804,10 +829,7 @@ static bool open_sockets(bool isdaemon, int port)
        BlockSignals(False, SIGHUP);
        BlockSignals(False, SIGUSR1);
        BlockSignals(False, SIGTERM);
-       
-       CatchSignal( SIGHUP,  SIGNAL_CAST sig_hup );
-       CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
-       
+
 #if defined(SIGFPE)
        /* we are never interested in SIGFPE */
        BlockSignals(True,SIGFPE);
@@ -832,9 +854,18 @@ static bool open_sockets(bool isdaemon, int port)
 
        reopen_logs();
 
-       DEBUG(0,("nmbd version %s started.\n", SAMBA_VERSION_STRING));
+       DEBUG(0,("nmbd version %s started.\n", samba_version_string()));
        DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
 
+       if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
+               DEBUG(0, ("error opening config file\n"));
+               exit(1);
+       }
+
+       if (nmbd_messaging_context() == NULL) {
+               return 1;
+       }
+
        if ( !reload_nmbd_services(False) )
                return(-1);
 
@@ -881,11 +912,26 @@ static bool open_sockets(bool isdaemon, int port)
        }
 #endif
 
-       if (!directory_exist(lp_lockdir(), NULL)) {
+       if (!directory_exist(lp_lockdir())) {
                mkdir(lp_lockdir(), 0755);
        }
 
        pidfile_create("nmbd");
+
+       if (!NT_STATUS_IS_OK(reinit_after_fork(nmbd_messaging_context(),
+                                              nmbd_event_context(), false))) {
+               DEBUG(0,("reinit_after_fork() failed\n"));
+               exit(1);
+       }
+
+       if (!nmbd_setup_sig_term_handler())
+               exit(1);
+       if (!nmbd_setup_sig_hup_handler())
+               exit(1);
+
+       /* get broadcast messages */
+       claim_connection(NULL,"",FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP);
+
        messaging_register(nmbd_messaging_context(), NULL,
                           MSG_FORCE_ELECTION, nmbd_message_election);
 #if 0
@@ -947,8 +993,11 @@ static bool open_sockets(bool isdaemon, int port)
                exit(1);
        }
 
-       /* We can only take signals in the select. */
-       BlockSignals( True, SIGTERM );
+       if (!initialize_nmbd_proxy_logon()) {
+               DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
+               kill_async_dns_child();
+               exit(1);
+       }
 
        TALLOC_FREE(frame);
        process();