s3:smbd: make mdns registration event driven.
[metze/samba/wip.git] / source3 / smbd / server.c
index 7bc96ff5fbf04e771c02fcba1242f3f532d751cd..d71a04ebc17cff7910c904580119d9abade7d522 100644 (file)
@@ -136,24 +136,6 @@ static void msg_sam_sync(struct messaging_context *msg,
         DEBUG(10, ("** sam sync message received, ignoring\n"));
 }
 
-
-/****************************************************************************
- Open the socket communication - inetd.
-****************************************************************************/
-
-static bool open_sockets_inetd(void)
-{
-       /* Started from inetd. fd 0 is the socket. */
-       /* We will abort gracefully when the client or remote system 
-          goes away */
-       smbd_set_server_fd(dup(0));
-       
-       /* close our standard file descriptors */
-       close_low_fds(False); /* Don't close stderr */
-
-       return True;
-}
-
 static void msg_exit_server(struct messaging_context *msg,
                            void *private_data,
                            uint32_t msg_type,
@@ -317,7 +299,7 @@ static void smbd_setup_sig_chld_handler(void)
  Open the socket communication.
 ****************************************************************************/
 
-static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_ports)
+static bool open_sockets_smbd(bool interactive, const char *smb_ports)
 {
        int num_interfaces = iface_count();
        int num_sockets = 0;
@@ -327,17 +309,15 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
        int maxfd = 0;
        int i;
        char *ports;
-       struct dns_reg_state * dns_reg = NULL;
+       TALLOC_CTX *dns_ctx = NULL;
        unsigned dns_port = 0;
 
-       if (!is_daemon) {
-               return open_sockets_inetd();
-       }
-
 #ifdef HAVE_ATEXIT
        atexit(killkids);
 #endif
 
+       dns_ctx = talloc_new(NULL);
+
        /* Stop zombies */
        smbd_setup_sig_chld_handler();
 
@@ -557,6 +537,11 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
                           MSG_SMB_INJECT_FAULT, msg_inject_fault);
 #endif
 
+       if (dns_port != 0) {
+               smbd_setup_mdns_registration(smbd_event_context(),
+                                            dns_ctx, dns_port);
+       }
+
        /* now accept incoming connections - forking a new process
           for each incoming connection */
        DEBUG(2,("waiting for a connection\n"));
@@ -576,12 +561,6 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
                FD_ZERO(&w_fds);
                GetTimeOfDay(&now);
 
-               /* Kick off our mDNS registration. */
-               if (dns_port != 0) {
-                       dns_register_smbd(&dns_reg, dns_port, &maxfd,
-                                       &r_fds, &idle_timeout);
-               }
-
                event_add_to_select_args(smbd_event_context(), &now,
                                         &r_fds, &w_fds, &idle_timeout,
                                         &maxfd);
@@ -602,11 +581,6 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
                        exit_server_cleanly("idle timeout");
                }
 
-               /* process pending nDNS responses */
-               if (dns_register_smbd_reply(dns_reg, &r_fds, &idle_timeout)) {
-                       --num;
-               }
-
                /* check if we need to reload services */
                check_reload(time(NULL));
 
@@ -646,6 +620,8 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
                            ((child = sys_fork())==0)) {
                                /* Child code ... */
 
+                               TALLOC_FREE(dns_ctx);
+
                                /* Stop zombies, the parent explicitly handles
                                 * them, counting worker smbds. */
                                CatchChild();
@@ -654,9 +630,6 @@ static bool open_sockets_smbd(bool is_daemon, bool interactive, const char *smb_
                                for(i = 0; i < num_sockets; i++)
                                        close(fd_listenset[i]);
 
-                               /* close our mDNS daemon handle */
-                               dns_register_close(&dns_reg);
-
                                /* close our standard file
                                   descriptors */
                                close_low_fds(False);
@@ -1224,7 +1197,25 @@ extern void build_options(bool screen);
                start_background_queue();
        }
 
-       if (!open_sockets_smbd(is_daemon, interactive, ports))
+       if (!is_daemon) {
+               /* inetd mode */
+               TALLOC_FREE(frame);
+
+               /* Started from inetd. fd 0 is the socket. */
+               /* We will abort gracefully when the client or remote system
+                  goes away */
+               smbd_set_server_fd(dup(0));
+
+               /* close our standard file descriptors */
+               close_low_fds(False); /* Don't close stderr */
+
+               smbd_process();
+
+               exit_server_cleanly(NULL);
+               return(0);
+       }
+
+       if (!open_sockets_smbd(interactive, ports))
                exit(1);
 
        TALLOC_FREE(frame);