s3-printing: remove pcap_cache_loaded asserts
[samba.git] / source3 / smbd / server.c
index 346e8973de3948aecddaa59d66eab2badf222c96..9c2709caf6ba49cc89862d77515b2daa641b0474 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);
 }
 
@@ -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;
 
@@ -374,10 +376,15 @@ static void smbd_accept_connection(struct tevent_context *ev,
                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");
                }
@@ -386,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) {
@@ -490,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.
 ****************************************************************************/
@@ -617,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,
@@ -643,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;
@@ -654,61 +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;
+               ret = tevent_loop_once(smbd_event_context());
+               if (ret != 0) {
+                       exit_server_cleanly("tevent_loop_once() error");
                }
 
-               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;
-               }
-
-               /* socket error */
-               if (num < 0)
-                       exit_server_cleanly("socket 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;
@@ -716,9 +715,7 @@ void reload_printers(void)
        int pnum = lp_servicenumber(PRINTERS_NAME);
        const char *pname;
 
-       pcap_cache_reload();
-
-       /* 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) &&
@@ -730,7 +727,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);
                }
@@ -765,7 +762,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)
@@ -860,6 +857,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
@@ -1063,6 +1063,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);
@@ -1147,8 +1152,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);
        }
@@ -1229,6 +1234,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);