s3:winbindd: use tevent_loop_wait() in the main loop
[metze/samba/wip.git] / source3 / winbindd / winbindd.c
index f5da15c259571b91127bda14b5c1ea3d58bca685..ffd2d9bce11e735399dd8081081202ae5bc53dca 100644 (file)
@@ -1284,6 +1284,34 @@ static void winbindd_addr_changed(struct tevent_req *req)
        tevent_req_set_callback(req, winbindd_addr_changed, state);
 }
 
+
+struct winbindd_tevent_trace_state {
+       TALLOC_CTX *frame;
+};
+
+static void winbindd_tevent_trace_callback(enum tevent_trace_point point,
+                                          void *private_data)
+{
+       struct winbindd_tevent_trace_state *state =
+               talloc_get_type_abort(private_data,
+                                     struct winbindd_tevent_trace_state);
+
+       switch (point) {
+       case TEVENT_TRACE_BEFORE_WAIT:
+       case TEVENT_TRACE_AFTER_WAIT:
+               break;
+       case TEVENT_TRACE_BEFORE_LOOP_ONCE:
+               TALLOC_FREE(state->frame);
+               state->frame = talloc_stackframe();
+               errno = 0;
+               break;
+       case TEVENT_TRACE_AFTER_LOOP_ONCE:
+               TALLOC_FREE(state->frame);
+               break;
+       }
+}
+
+
 /* Main function */
 
 int main(int argc, char **argv, char **envp)
@@ -1314,6 +1342,9 @@ int main(int argc, char **argv, char **envp)
        TALLOC_CTX *frame;
        NTSTATUS status;
        bool ok;
+       int ret = 0;
+       struct tevent_context *ev_ctx = NULL;
+       struct winbindd_tevent_trace_state *trace_state = NULL;
 
        /*
         * Do this before any other talloc operation
@@ -1472,7 +1503,7 @@ int main(int argc, char **argv, char **envp)
        if (!init_names())
                exit(1);
 
-       load_interfaces();
+       load_interfaces();
 
        if (!secrets_init()) {
 
@@ -1553,19 +1584,35 @@ int main(int argc, char **argv, char **envp)
                exit(1);
        }
 
-       TALLOC_FREE(frame);
        /* Loop waiting for requests */
-       while (1) {
-               frame = talloc_stackframe();
 
-               if (tevent_loop_once(winbind_event_context()) == -1) {
-                       DEBUG(1, ("tevent_loop_once() failed: %s\n",
-                                 strerror(errno)));
-                       return 1;
-               }
+       ev_ctx = winbind_event_context();
+       if (ev_ctx == NULL) {
+               DEBUG(0, ("%s: winbind_event_context() failed\n",
+                         __location__));
+               exit(1);
+       }
+
+       trace_state = talloc_zero(ev_ctx, struct winbindd_tevent_trace_state);
+       if (trace_state == NULL) {
+               DEBUG(0, ("talloc_zero(winbindd_tevent_trace_state).\n"));
+               exit(1);
+       }
+
+       trace_state->frame = frame;
+       frame = NULL;
+
+       tevent_set_trace_callback(ev_ctx, winbindd_tevent_trace_callback,
+                                 trace_state);
 
-               TALLOC_FREE(frame);
+       ret = tevent_loop_wait(ev_ctx);
+       if (ret != 0) {
+               DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n",
+                         ret, strerror(errno)));
+               exit(1);
        }
 
+       TALLOC_FREE(trace_state->frame);
+
        return 0;
 }