winbind: Improve child selection
[samba.git] / source3 / winbindd / winbindd_dual.c
index d36eb0525c9b3a0a3ce58919bc4af26fc9847201..993166d82d91991646effe62e11a625b027eaefb 100644 (file)
@@ -248,33 +248,31 @@ static void wb_child_request_cleanup(struct tevent_req *req,
        DLIST_REMOVE(winbindd_children, state->child);
 }
 
-static bool winbindd_child_busy(struct winbindd_child *child)
-{
-       return tevent_queue_length(child->queue) > 0;
-}
-
-static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
+struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
 {
+       struct winbindd_child *shortest = &domain->children[0];
+       struct winbindd_child *current;
        int i;
 
        for (i=0; i<lp_winbind_max_domain_connections(); i++) {
-               if (!winbindd_child_busy(&domain->children[i])) {
-                       return &domain->children[i];
-               }
-       }
+               size_t shortest_len, current_len;
 
-       return NULL;
-}
+               current = &domain->children[i];
+               current_len = tevent_queue_length(current->queue);
 
-struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
-{
-       struct winbindd_child *result;
+               if (current_len == 0) {
+                       /* idle child */
+                       return current;
+               }
 
-       result = find_idle_child(domain);
-       if (result != NULL) {
-               return result;
+               shortest_len = tevent_queue_length(shortest->queue);
+
+               if (current_len < shortest_len) {
+                       shortest = current;
+               }
        }
-       return &domain->children[rand() % lp_winbind_max_domain_connections()];
+
+       return shortest;
 }
 
 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
@@ -1051,10 +1049,11 @@ static void machine_password_change_handler(struct tevent_context *ctx,
                                            struct timeval now,
                                            void *private_data)
 {
-       struct messaging_context *msg_ctx = winbind_messaging_context();
+       struct messaging_context *msg_ctx = server_messaging_context();
        struct winbindd_child *child =
                (struct winbindd_child *)private_data;
        struct rpc_pipe_client *netlogon_pipe = NULL;
+       struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
        NTSTATUS result;
        struct timeval next_change;
 
@@ -1083,7 +1082,9 @@ static void machine_password_change_handler(struct tevent_context *ctx,
                return;
        }
 
-       result = cm_connect_netlogon(child->domain, &netlogon_pipe);
+       result = cm_connect_netlogon_secure(child->domain,
+                                           &netlogon_pipe,
+                                           &netlogon_creds_ctx);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(10,("machine_password_change_handler: "
                        "failed to connect netlogon pipe: %s\n",
@@ -1091,7 +1092,7 @@ static void machine_password_change_handler(struct tevent_context *ctx,
                return;
        }
 
-       result = trust_pw_change(child->domain->conn.netlogon_creds_ctx,
+       result = trust_pw_change(netlogon_creds_ctx,
                                 msg_ctx,
                                 netlogon_pipe->binding_handle,
                                 child->domain->name,
@@ -1250,7 +1251,7 @@ NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
        NTSTATUS status;
 
        status = reinit_after_fork(
-               winbind_messaging_context(),
+               server_messaging_context(),
                server_event_context(),
                true, NULL);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1275,26 +1276,26 @@ NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
        CatchChild();
 
        /* Don't handle the same messages as our parent. */
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_SMB_CONF_UPDATED, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_SHUTDOWN, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_OFFLINE, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_ONLINE, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_ONLINESTATUS, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_DUMP_EVENT_LIST, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_DEBUG, NULL);
 
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_DOMAIN_OFFLINE, NULL);
-       messaging_deregister(winbind_messaging_context(),
+       messaging_deregister(server_messaging_context(),
                             MSG_WINBIND_DOMAIN_ONLINE, NULL);
 
        /* We have destroyed all events in the winbindd_event_context
@@ -1491,16 +1492,22 @@ static bool fork_domain_child(struct winbindd_child *child)
                _exit(0);
        }
 
+       if (child_domain != NULL) {
+               setproctitle("domain child [%s]", child_domain->name);
+       } else if (child == idmap_child()) {
+               setproctitle("idmap child");
+       }
+
        /* Handle online/offline messages. */
-       messaging_register(winbind_messaging_context(), NULL,
+       messaging_register(server_messaging_context(), NULL,
                           MSG_WINBIND_OFFLINE, child_msg_offline);
-       messaging_register(winbind_messaging_context(), NULL,
+       messaging_register(server_messaging_context(), NULL,
                           MSG_WINBIND_ONLINE, child_msg_online);
-       messaging_register(winbind_messaging_context(), NULL,
+       messaging_register(server_messaging_context(), NULL,
                           MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
-       messaging_register(winbind_messaging_context(), NULL,
+       messaging_register(server_messaging_context(), NULL,
                           MSG_DEBUG, debug_message);
-       messaging_register(winbind_messaging_context(), NULL,
+       messaging_register(server_messaging_context(), NULL,
                           MSG_WINBIND_IP_DROPPED,
                           winbind_msg_ip_dropped);