diff options
Diffstat (limited to 'source3')
-rw-r--r-- | source3/lib/events.c | 47 | ||||
-rw-r--r-- | source3/lib/messages.c | 7 | ||||
-rw-r--r-- | source3/utils/smbcontrol.c | 58 |
3 files changed, 87 insertions, 25 deletions
diff --git a/source3/lib/events.c b/source3/lib/events.c index a00db77b6b..77d18ec13d 100644 --- a/source3/lib/events.c +++ b/source3/lib/events.c @@ -185,20 +185,27 @@ void event_fd_set_not_readable(struct fd_event *fde) fde->flags &= ~EVENT_FD_READ; } -void event_add_to_select_args(struct event_context *event_ctx, +/* + * Return if there's something in the queue + */ + +BOOL event_add_to_select_args(struct event_context *event_ctx, const struct timeval *now, fd_set *read_fds, fd_set *write_fds, struct timeval *timeout, int *maxfd) { struct fd_event *fde; struct timeval diff; + BOOL ret = False; for (fde = event_ctx->fd_events; fde; fde = fde->next) { if (fde->flags & EVENT_FD_READ) { FD_SET(fde->fd, read_fds); + ret = True; } if (fde->flags & EVENT_FD_WRITE) { FD_SET(fde->fd, write_fds); + ret = True; } if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE)) @@ -208,11 +215,13 @@ void event_add_to_select_args(struct event_context *event_ctx, } if (event_ctx->timed_events == NULL) { - return; + return ret; } diff = timeval_until(now, &event_ctx->timed_events->when); *timeout = timeval_min(timeout, &diff); + + return True; } BOOL run_events(struct event_context *event_ctx, @@ -297,6 +306,40 @@ struct timeval *get_timed_events_timeout(struct event_context *event_ctx, return to_ret; } +int event_loop_once(struct event_context *ev) +{ + struct timeval now, to; + fd_set r_fds, w_fds; + int maxfd = 0; + int ret; + + FD_ZERO(&r_fds); + FD_ZERO(&w_fds); + + to.tv_sec = 9999; /* Max timeout */ + to.tv_usec = 0; + + GetTimeOfDay(&now); + + if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) { + return -1; + } + + if (timeval_is_zero(&to)) { + run_events(ev, 0, NULL, NULL); + return 0; + } + + ret = sys_select(maxfd, &r_fds, &w_fds, NULL, &to); + + if (ret == -1 && errno != EINTR) { + return -1; + } + + run_events(ev, ret, &r_fds, &w_fds); + return 0; +} + struct event_context *event_context_init(TALLOC_CTX *mem_ctx) { return TALLOC_ZERO_P(NULL, struct event_context); diff --git a/source3/lib/messages.c b/source3/lib/messages.c index 9c1eba7b1c..4cbdda61ee 100644 --- a/source3/lib/messages.c +++ b/source3/lib/messages.c @@ -723,6 +723,7 @@ struct messaging_callback { struct messaging_context { struct server_id id; + struct event_context *event_ctx; struct messaging_callback *callbacks; }; @@ -740,6 +741,11 @@ static int messaging_context_destructor(struct messaging_context *ctx) return 0; } +struct event_context *messaging_event_context(struct messaging_context *msg_ctx) +{ + return msg_ctx->event_ctx; +} + struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, struct server_id server_id, struct event_context *ev) @@ -751,6 +757,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, } ctx->id = server_id; + ctx->event_ctx = ev; talloc_set_destructor(ctx, messaging_context_destructor); if (!message_init(ctx)) { diff --git a/source3/utils/smbcontrol.c b/source3/utils/smbcontrol.c index f9e39329f1..5d06576825 100644 --- a/source3/utils/smbcontrol.c +++ b/source3/utils/smbcontrol.c @@ -70,7 +70,8 @@ static BOOL send_message(struct messaging_context *msg_ctx, /* Wait for one or more reply messages */ -static void wait_replies(BOOL multiple_replies) +static void wait_replies(struct messaging_context *msg_ctx, + BOOL multiple_replies) { time_t start_time = time(NULL); @@ -79,6 +80,7 @@ static void wait_replies(BOOL multiple_replies) do { message_dispatch(); + event_loop_once(messaging_event_context(msg_ctx)); if (num_replies > 0 && !multiple_replies) break; sleep(1); @@ -87,20 +89,26 @@ static void wait_replies(BOOL multiple_replies) /* Message handler callback that displays the PID and a string on stdout */ -static void print_pid_string_cb(int msg_type, struct server_id pid, void *buf, - size_t len, void *private_data) +static void print_pid_string_cb(struct messaging_context *msg, + void *private_data, + uint32_t msg_type, + struct server_id pid, + DATA_BLOB *data) { printf("PID %u: %.*s", (unsigned int)procid_to_pid(&pid), - (int)len, (const char *)buf); + (int)data->length, (const char *)data->data); num_replies++; } /* Message handler callback that displays a string on stdout */ -static void print_string_cb(int msg_type, struct server_id pid, - void *buf, size_t len, void *private_data) +static void print_string_cb(struct messaging_context *msg, + void *private_data, + uint32_t msg_type, + struct server_id pid, + DATA_BLOB *data) { - printf("%.*s", (int)len, (const char *)buf); + printf("%.*s", (int)data->length, (const char *)data->data); num_replies++; } @@ -350,8 +358,11 @@ static BOOL do_election(struct messaging_context *msg_ctx, /* Ping a samba daemon process */ -static void pong_cb(int msg_type, struct server_id pid, void *buf, - size_t len, void *private_data) +static void pong_cb(struct messaging_context *msg, + void *private_data, + uint32_t msg_type, + struct server_id pid, + DATA_BLOB *data) { char *src_string = procid_str(NULL, &pid); printf("PONG from pid %s\n", src_string); @@ -373,16 +384,16 @@ static BOOL do_ping(struct messaging_context *msg_ctx, if (!send_message(msg_ctx, pid, MSG_PING, NULL, 0, False)) return False; - message_register(MSG_PONG, pong_cb, NULL); + messaging_register(msg_ctx, NULL, MSG_PONG, pong_cb); - wait_replies(procid_to_pid(&pid) == 0); + wait_replies(msg_ctx, procid_to_pid(&pid) == 0); /* No replies were received within the timeout period */ if (num_replies == 0) printf("No replies received\n"); - message_deregister(MSG_PONG); + messaging_deregister(msg_ctx, MSG_PONG, NULL); return num_replies; } @@ -490,14 +501,14 @@ static BOOL do_profilelevel(struct messaging_context *msg_ctx, messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL, profilelevel_rqst); - wait_replies(procid_to_pid(&pid) == 0); + wait_replies(msg_ctx, procid_to_pid(&pid) == 0); /* No replies were received within the timeout period */ if (num_replies == 0) printf("No replies received\n"); - message_deregister(MSG_PROFILE); + messaging_deregister(msg_ctx, MSG_PROFILE, NULL); return num_replies; } @@ -518,16 +529,16 @@ static BOOL do_debuglevel(struct messaging_context *msg_ctx, if (!send_message(msg_ctx, pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False)) return False; - message_register(MSG_DEBUGLEVEL, print_pid_string_cb, NULL); + messaging_register(msg_ctx, NULL, MSG_DEBUGLEVEL, print_pid_string_cb); - wait_replies(procid_to_pid(&pid) == 0); + wait_replies(msg_ctx, procid_to_pid(&pid) == 0); /* No replies were received within the timeout period */ if (num_replies == 0) printf("No replies received\n"); - message_deregister(MSG_DEBUGLEVEL); + messaging_deregister(msg_ctx, MSG_DEBUGLEVEL, NULL); return num_replies; } @@ -730,21 +741,21 @@ static BOOL do_poolusage(struct messaging_context *msg_ctx, return False; } - message_register(MSG_POOL_USAGE, print_string_cb, NULL); + messaging_register(msg_ctx, NULL, MSG_POOL_USAGE, print_string_cb); /* Send a message and register our interest in a reply */ if (!send_message(msg_ctx, pid, MSG_REQ_POOL_USAGE, NULL, 0, False)) return False; - wait_replies(procid_to_pid(&pid) == 0); + wait_replies(msg_ctx, procid_to_pid(&pid) == 0); /* No replies were received within the timeout period */ if (num_replies == 0) printf("No replies received\n"); - message_deregister(MSG_POOL_USAGE); + messaging_deregister(msg_ctx, MSG_POOL_USAGE, NULL); return num_replies; } @@ -929,20 +940,21 @@ static BOOL do_winbind_onlinestatus(struct messaging_context *msg_ctx, return False; } - message_register(MSG_WINBIND_ONLINESTATUS, print_pid_string_cb, NULL); + messaging_register(msg_ctx, NULL, MSG_WINBIND_ONLINESTATUS, + print_pid_string_cb); if (!send_message(msg_ctx, pid, MSG_WINBIND_ONLINESTATUS, &myid, sizeof(myid), False)) return False; - wait_replies(procid_to_pid(&pid) == 0); + wait_replies(msg_ctx, procid_to_pid(&pid) == 0); /* No replies were received within the timeout period */ if (num_replies == 0) printf("No replies received\n"); - message_deregister(MSG_WINBIND_ONLINESTATUS); + messaging_deregister(msg_ctx, MSG_WINBIND_ONLINESTATUS, NULL); return num_replies; } |