Revert "Revert "s3-printing: update parent smbd pcap cache""
[samba.git] / source3 / smbd / server.c
index 5f6783e05c125252741e4301566a4d5c5684ef6d..6147526172ce700d5d71f1f8758098e4cab6de16 100644 (file)
@@ -94,6 +94,7 @@ static void smb_conf_updated(struct messaging_context *msg,
 {
        DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
                  "updated. Reloading.\n"));
+       change_to_root_user();
        reload_services(False);
 }
 
@@ -212,7 +213,7 @@ static void remove_child_pid(pid_t pid, bool unclean_shutdown)
                /* a child terminated uncleanly so tickle all processes to see 
                   if they can grab any of the pending locks
                */
-               DEBUG(3,(__location__ " Unclean shutdown of pid %u\n", pid));
+               DEBUG(3,(__location__ " Unclean shutdown of pid %u\n", (unsigned int)pid));
                messaging_send_buf(smbd_messaging_context(), procid_self(), 
                                   MSG_SMB_BRL_VALIDATE, NULL, 0);
                message_send_all(smbd_messaging_context(), 
@@ -356,6 +357,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
 
        pid = sys_fork();
        if (pid == 0) {
+               NTSTATUS status = NT_STATUS_OK;
                /* Child code ... */
                am_parent = 0;
 
@@ -367,13 +369,22 @@ static void smbd_accept_connection(struct tevent_context *ev,
                   descriptors */
                close_low_fds(False);
 
-               TALLOC_FREE(s->parent);
+               /*
+                * Can't use TALLOC_FREE here. Nulling out the argument to it
+                * would overwrite memory we've just freed.
+                */
+               talloc_free(s->parent);
                s = NULL;
 
-               if (!reinit_after_fork(
-                           smbd_messaging_context(),
-                           smbd_event_context(),
-                           true)) {
+               status = reinit_after_fork(smbd_messaging_context(),
+                                          smbd_event_context(), true);
+               if (!NT_STATUS_IS_OK(status)) {
+                       if (NT_STATUS_EQUAL(status,
+                                           NT_STATUS_TOO_MANY_OPENED_FILES)) {
+                               DEBUG(0,("child process cannot initialize "
+                                        "because too many files are open\n"));
+                               goto exit;
+                       }
                        DEBUG(0,("reinit_after_fork() failed\n"));
                        smb_panic("reinit_after_fork() failed");
                }
@@ -382,6 +393,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
                smbd_setup_sig_hup_handler();
 
                smbd_process();
+        exit:
                exit_server_cleanly("end of child");
                return;
        } else if (pid < 0) {
@@ -443,7 +455,6 @@ static bool smbd_open_one_socket(struct smbd_parent_context *parent,
        if (s->fd == -1) {
                DEBUG(0,("smbd_open_once_socket: open_socket_in: "
                        "%s\n", strerror(errno)));
-                       close(s->fd);
                TALLOC_FREE(s);
                /*
                 * We ignore an error here, as we've done before
@@ -487,6 +498,14 @@ static bool smbd_open_one_socket(struct smbd_parent_context *parent,
        return true;
 }
 
+static bool parent_housekeeping_fn(const struct timeval *now, void *private_data)
+{
+       DEBUG(5, ("houskeeping\n"));
+       /* check if we need to reload services */
+       check_reload(time(NULL));
+       return true;
+}
+
 /****************************************************************************
  Open the socket communication.
 ****************************************************************************/
@@ -614,6 +633,14 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
        claim_connection(NULL,"",
                         FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_DBWRAP);
 
+       if (!(event_add_idle(smbd_event_context(), NULL,
+                            timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
+                            "parent_housekeeping", parent_housekeeping_fn,
+                            parent))) {
+               DEBUG(0, ("Could not add housekeeping event\n"));
+               exit(1);
+       }
+
         /* Listen to messages */
 
        messaging_register(smbd_messaging_context(), NULL,
@@ -640,8 +667,19 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
 #endif
 
        if (dns_port != 0) {
+#ifdef WITH_DNSSD_SUPPORT
                smbd_setup_mdns_registration(smbd_event_context(),
                                             parent, dns_port);
+#endif
+#ifdef WITH_AVAHI_SUPPORT
+               void *avahi_conn;
+
+               avahi_conn = avahi_start_register(
+                       smbd_event_context(), smbd_event_context(), dns_port);
+               if (avahi_conn == NULL) {
+                       DEBUG(10, ("avahi_start_register failed\n"));
+               }
+#endif
        }
 
        return true;
@@ -651,57 +689,25 @@ static void smbd_parent_loop(struct smbd_parent_context *parent)
 {
        /* now accept incoming connections - forking a new process
           for each incoming connection */
-       DEBUG(2,("waiting for a connection\n"));
+       DEBUG(2,("waiting for connections\n"));
        while (1) {
-               struct timeval now, idle_timeout;
-               fd_set r_fds, w_fds;
-               int maxfd = 0;
-               int num;
+               int ret;
                TALLOC_CTX *frame = talloc_stackframe();
 
-               if (run_events(smbd_event_context(), 0, NULL, NULL)) {
-                       TALLOC_FREE(frame);
-                       continue;
-               }
-
-               idle_timeout = timeval_zero();
-
-               FD_ZERO(&w_fds);
-               FD_ZERO(&r_fds);
-               GetTimeOfDay(&now);
-
-               event_add_to_select_args(smbd_event_context(), &now,
-                                        &r_fds, &w_fds, &idle_timeout,
-                                        &maxfd);
-
-               num = sys_select(maxfd+1,&r_fds,&w_fds,NULL,
-                                timeval_is_zero(&idle_timeout) ?
-                                NULL : &idle_timeout);
-
-               /* check if we need to reload services */
-               check_reload(time(NULL));
-
-               if (run_events(smbd_event_context(), num, &r_fds, &w_fds)) {
-                       TALLOC_FREE(frame);
-                       continue;
+               ret = tevent_loop_once(smbd_event_context());
+               if (ret != 0) {
+                       exit_server_cleanly("tevent_loop_once() error");
                }
 
-               /* If the idle timeout fired and we don't have any connected
-                * users, exit gracefully. We should be running under a process
-                * controller that will restart us if necessry.
-                */
-               if (num == 0 && count_all_current_connections() == 0) {
-                       exit_server_cleanly("idle timeout");
-               }
                TALLOC_FREE(frame);
        } /* end while 1 */
 
 /* NOTREACHED  return True; */
 }
 
-/****************************************************************************
- Reload printers
-**************************************************************************/
+/***************************************************************************
+ purge stale printers and reload from pre-populated pcap cache
+***************************************************************************/
 void reload_printers(void)
 {
        int snum;
@@ -709,9 +715,9 @@ void reload_printers(void)
        int pnum = lp_servicenumber(PRINTERS_NAME);
        const char *pname;
 
-       pcap_cache_reload();
+       SMB_ASSERT(pcap_cache_loaded());
 
-       /* remove stale printers */
+       DEBUG(10, ("reloading printer services from pcap cache\n"));
        for (snum = 0; snum < n_services; snum++) {
                /* avoid removing PRINTERS_NAME or non-autoloaded printers */
                if (snum == pnum || !(lp_snum_ok(snum) && lp_print_ok(snum) &&
@@ -723,7 +729,7 @@ void reload_printers(void)
                        DEBUG(3, ("removing stale printer %s\n", pname));
 
                        if (is_printer_published(NULL, snum, NULL))
-                               nt_printer_publish(NULL, snum, SPOOL_DS_UNPUBLISH);
+                               nt_printer_publish(NULL, snum, DSPRINT_UNPUBLISH);
                        del_a_printer(pname);
                        lp_killservice(snum);
                }
@@ -758,7 +764,7 @@ bool reload_services(bool test)
 
        ret = lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
 
-       reload_printers();
+       pcap_cache_reload(&reload_printers);
 
        /* perhaps the config filename is now set */
        if (!test)
@@ -853,6 +859,9 @@ static void exit_server_common(enum server_exit_reason how,
        } else {    
                DEBUG(3,("Server exit (%s)\n",
                        (reason ? reason : "normal exit")));
+               if (am_parent) {
+                       pidfile_unlink();
+               }
        }
 
        /* if we had any open SMB connections when we exited then we
@@ -1056,6 +1065,11 @@ extern void build_options(bool screen);
        BlockSignals(False, SIGUSR1);
        BlockSignals(False, SIGTERM);
 
+       /* Ensure we leave no zombies until we
+        * correctly set up child handling below. */
+
+       CatchChild();
+
        /* we want total control over the permissions on created files,
           so set our umask to 0 */
        umask(0);
@@ -1140,8 +1154,8 @@ extern void build_options(bool screen);
        if (is_daemon)
                pidfile_create("smbd");
 
-       if (!reinit_after_fork(smbd_messaging_context(),
-                              smbd_event_context(), false)) {
+       if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
+                            smbd_event_context(), false))) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                exit(1);
        }
@@ -1222,6 +1236,13 @@ extern void build_options(bool screen);
                /* close our standard file descriptors */
                close_low_fds(False); /* Don't close stderr */
 
+#ifdef HAVE_ATEXIT
+               atexit(killkids);
+#endif
+
+               /* Stop zombies */
+               smbd_setup_sig_chld_handler();
+
                smbd_process();
 
                exit_server_cleanly(NULL);