From: Vsevolod Stakhov Date: Mon, 4 Apr 2016 12:42:36 +0000 (+0100) Subject: [Feature] Allow to attach file descriptors to control commands X-Git-Tag: 1.2.3~75 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4bd75d888a4110b3cfa89c8fb33610e6ad95d899;p=rspamd.git [Feature] Allow to attach file descriptors to control commands --- diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index 151a0ba64..7a603ff58 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -813,6 +813,7 @@ sync_callback (gint fd, short what, void *arg) static gboolean rspamd_fuzzy_storage_sync (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud) { @@ -852,6 +853,7 @@ rspamd_fuzzy_storage_sync (struct rspamd_main *rspamd_main, static gboolean rspamd_fuzzy_storage_reload (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud) { @@ -1037,6 +1039,7 @@ rspamd_fuzzy_stat_to_ucl (struct rspamd_fuzzy_storage_ctx *ctx, gboolean ip_stat static gboolean rspamd_fuzzy_storage_stat (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud) { diff --git a/src/hs_helper.c b/src/hs_helper.c index 7e8f7f1c5..e0c56f4d2 100644 --- a/src/hs_helper.c +++ b/src/hs_helper.c @@ -203,6 +203,7 @@ rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker, static gboolean rspamd_hs_helper_reload (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud) { diff --git a/src/libserver/rspamd_control.c b/src/libserver/rspamd_control.c index 138809a84..84f68de02 100644 --- a/src/libserver/rspamd_control.c +++ b/src/libserver/rspamd_control.c @@ -491,6 +491,7 @@ struct rspamd_worker_control_data { static void rspamd_control_default_cmd_handler (gint fd, + gint attached_fd, struct rspamd_worker_control_data *cd, struct rspamd_control_command *cmd) { @@ -557,10 +558,21 @@ rspamd_control_default_worker_handler (gint fd, short what, gpointer ud) { struct rspamd_worker_control_data *cd = ud; struct rspamd_control_command cmd; - + struct msghdr msg; + struct iovec iov; + guchar fdspace[CMSG_SPACE(sizeof (int))]; + gint rfd = -1; gssize r; - r = read (fd, &cmd, sizeof (cmd)); + iov.iov_base = &cmd; + iov.iov_len = sizeof (cmd); + memset (&msg, 0, sizeof (msg)); + msg.msg_control = fdspace; + msg.msg_controllen = sizeof (fdspace); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + + r = recvmsg (fd, &msg, 0); if (r == -1) { msg_err ("cannot read request from the control socket: %s", @@ -582,12 +594,20 @@ rspamd_control_default_worker_handler (gint fd, short what, gpointer ud) } else if ((gint)cmd.type >= 0 && cmd.type < RSPAMD_CONTROL_MAX) { + if (msg.msg_controllen >= CMSG_SPACE(sizeof (int))) { + rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR (&msg)); + } + if (cd->handlers[cmd.type].handler) { - cd->handlers[cmd.type].handler (cd->worker->srv, cd->worker, - fd, &cmd, cd->handlers[cmd.type].ud); + cd->handlers[cmd.type].handler (cd->worker->srv, + cd->worker, + fd, + rfd, + &cmd, + cd->handlers[cmd.type].ud); } else { - rspamd_control_default_cmd_handler (fd, cd, &cmd); + rspamd_control_default_cmd_handler (fd, rfd, cd, &cmd); } } else { diff --git a/src/libserver/rspamd_control.h b/src/libserver/rspamd_control.h index a0d5fba1b..751b916eb 100644 --- a/src/libserver/rspamd_control.h +++ b/src/libserver/rspamd_control.h @@ -126,11 +126,16 @@ struct rspamd_srv_reply { struct { gint forced; } hs_loaded; + struct { + enum rspamd_log_pipe_type type; + } log_pipe; } reply; }; typedef gboolean (*rspamd_worker_control_handler) (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, + struct rspamd_worker *worker, + gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud); diff --git a/src/worker.c b/src/worker.c index 0e3db7d10..67c262e40 100644 --- a/src/worker.c +++ b/src/worker.c @@ -71,6 +71,12 @@ worker_t normal_worker = { G_STRFUNC, \ __VA_ARGS__) +struct rspamd_worker_log_pipe { + gint fd; + enum rspamd_log_pipe_type type; + struct rspamd_worker_log_pipe *prev, *next; +}; + /* * Worker's context */ @@ -358,6 +364,7 @@ accept_socket (gint fd, short what, void *arg) static gboolean rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, + gint attached_fd, struct rspamd_control_command *cmd, gpointer ud) {