doveadm_http_server_command_execute(struct client_request_http *req)
{
struct client_connection_http *conn = req->conn;
- struct doveadm_cmd_context cctx;
struct istream *is;
const char *user;
struct ioloop *ioloop, *prev_ioloop;
}
prev_ioloop = current_ioloop;
- i_zero(&cctx);
- cctx.pool = pool_alloconly_create("doveadm cmd", 256);
- cctx.conn_type = conn->conn.type;
- cctx.input = req->input;
- cctx.output = req->output;
+
+ struct doveadm_cmd_context *cctx = doveadm_cmd_context_create(
+ conn->conn.type, doveadm_verbose || doveadm_debug);
+
+ cctx->input = req->input;
+ cctx->output = req->output;
// create iostream
doveadm_print_ostream = iostream_temp_create("/tmp/doveadm.", 0);
- cctx.cmd = req->cmd;
+ cctx->cmd = req->cmd;
- if ((cctx.cmd->flags & CMD_FLAG_NO_PRINT) == 0)
+ if ((cctx->cmd->flags & CMD_FLAG_NO_PRINT) == 0)
doveadm_print_init(DOVEADM_PRINT_TYPE_JSON);
/* then call it */
doveadm_cmd_params_null_terminate_arrays(&req->pargv);
- cctx.argv = array_get(&req->pargv, (unsigned int*)&cctx.argc);
+ cctx->argv = array_get(&req->pargv, (unsigned int*)&cctx->argc);
ioloop = io_loop_create();
doveadm_exit_code = 0;
- cctx.local_ip = conn->conn.local_ip;
- cctx.local_port = conn->conn.local_port;
- cctx.remote_ip = conn->conn.remote_ip;
- cctx.remote_port = conn->conn.remote_port;
+ cctx->local_ip = conn->conn.local_ip;
+ cctx->local_port = conn->conn.local_port;
+ cctx->remote_ip = conn->conn.remote_ip;
+ cctx->remote_port = conn->conn.remote_port;
- if (doveadm_cmd_param_str(&cctx, "user", &user))
- i_info("Executing command '%s' as '%s'", cctx.cmd->name, user);
+ if (doveadm_cmd_param_str(cctx, "user", &user))
+ i_info("Executing command '%s' as '%s'", cctx->cmd->name, user);
else
- i_info("Executing command '%s'", cctx.cmd->name);
- client_connection_set_proctitle(&conn->conn, cctx.cmd->name);
- cctx.cmd->cmd(&cctx);
+ i_info("Executing command '%s'", cctx->cmd->name);
+ client_connection_set_proctitle(&conn->conn, cctx->cmd->name);
+ cctx->cmd->cmd(cctx);
client_connection_set_proctitle(&conn->conn, "");
o_stream_switch_ioloop_to(req->output, prev_ioloop);
io_loop_destroy(&ioloop);
- if ((cctx.cmd->flags & CMD_FLAG_NO_PRINT) == 0)
+ if ((cctx->cmd->flags & CMD_FLAG_NO_PRINT) == 0)
doveadm_print_deinit();
if (o_stream_finish(doveadm_print_ostream) < 0) {
i_info("Error writing output in command %s: %s",
else
o_stream_nsend_str(req->output,",");
- if (cctx.referral != NULL) {
- i_error("Command requested referral: %s", cctx.referral);
+ if (cctx->referral != NULL) {
+ i_error("Command requested referral: %s", cctx->referral);
doveadm_http_server_json_error(req, "internalError");
} else if (doveadm_exit_code != 0) {
if (doveadm_exit_code == 0 || doveadm_exit_code == EX_TEMPFAIL)
doveadm_http_server_json_success(req, is);
}
i_stream_unref(&is);
- pool_unref(&cctx.pool);
+ doveadm_cmd_context_unref(&cctx);
}
static int
static bool client_handle_command(struct client_connection_tcp *conn,
const char *const *args)
{
- struct doveadm_cmd_context cctx;
-
- i_zero(&cctx);
- cctx.pool = pool_alloconly_create("doveadm cmd", 256);
- cctx.conn_type = conn->conn.type;
- cctx.input = conn->input;
- cctx.output = conn->output;
- cctx.local_ip = conn->conn.local_ip;
- cctx.remote_ip = conn->conn.remote_ip;
- cctx.local_port = conn->conn.local_port;
- cctx.remote_port = conn->conn.remote_port;
- bool ret = client_handle_command_ctx(conn, &cctx, args);
- pool_unref(&cctx.pool);
+ struct doveadm_cmd_context *cctx = doveadm_cmd_context_create(
+ conn->conn.type, doveadm_verbose || doveadm_debug);
+ cctx->input = conn->input;
+ cctx->output = conn->output;
+ cctx->local_ip = conn->conn.local_ip;
+ cctx->remote_ip = conn->conn.remote_ip;
+ cctx->local_port = conn->conn.local_port;
+ cctx->remote_port = conn->conn.remote_port;
+ bool ret = client_handle_command_ctx(conn, cctx, args);
+ doveadm_cmd_context_unref(&cctx);
return ret;
}
ARRAY_DEFINE_TYPE(getopt_option_array, struct option);
+static struct event_category event_category_doveadm = {
+ .name = "doveadm",
+};
+
+struct doveadm_cmd_context*
+doveadm_cmd_context_create(enum doveadm_client_type conn_type, bool forced_debug)
+{
+ pool_t pool = pool_alloconly_create("doveadm cmd", 256);
+ struct event *event = event_create(NULL);
+ event_set_append_log_prefix(event, "doveadm: ");
+ event_set_forced_debug(event, forced_debug);
+ event_add_category(event, &event_category_doveadm);
+
+ struct doveadm_cmd_context *cctx = p_new(pool, struct doveadm_cmd_context, 1);
+ cctx->pool = pool;
+ cctx->event = event;
+ cctx->conn_type = conn_type;
+ return cctx;
+}
+
+void doveadm_cmd_context_unref(struct doveadm_cmd_context **_cctx)
+{
+ struct doveadm_cmd_context *cctx = *_cctx;
+ *_cctx = NULL;
+
+ event_unref(&cctx->event);
+ pool_unref(&cctx->pool);
+}
+
static const struct doveadm_cmd_param *
doveadm_cmd_param_get(const struct doveadm_cmd_context *cctx,
const char *name)
enum doveadm_client_type conn_type;
struct istream *input;
struct ostream *output;
+ struct event *event;
/* non-NULL if doveadm-server should return referral to another
server instead. */
bool proxy_redirect_reauth;
};
+struct doveadm_cmd_context*
+doveadm_cmd_context_create(enum doveadm_client_type conn_type, bool forced_debug);
+void doveadm_cmd_context_unref(struct doveadm_cmd_context **cctx);
+
/* Returns 0 if success, -1 if parameters were invalid. */
int doveadm_cmdline_run(int argc, const char *const argv[],
struct doveadm_cmd_context *cctx);
MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN |
MASTER_SERVICE_FLAG_NO_SSL_INIT |
MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME;
- struct doveadm_cmd_context cctx;
const char *cmd_name;
unsigned int i;
bool quick_init = FALSE;
int c;
- i_zero(&cctx);
- cctx.pool = pool_alloconly_create("doveadm cmd", 256);
- cctx.conn_type = DOVEADM_CONNECTION_TYPE_CLI;
-
- i_set_failure_exit_callback(failure_exit_callback);
-
/* "+" is GNU extension to stop at the first non-option.
others just accept -+ option. */
master_service = master_service_init("doveadm", service_flags,
&argc, &argv, "+Df:hv");
+ struct doveadm_cmd_context *cctx = doveadm_cmd_context_create(
+ DOVEADM_CONNECTION_TYPE_CLI, doveadm_verbose || doveadm_debug);
+
+ i_set_failure_exit_callback(failure_exit_callback);
+
while ((c = master_getopt(master_service)) > 0) {
switch (c) {
case 'D':
/* this has to be done here because proctitle hack can break
the env pointer */
- cctx.username = getenv("USER");
+ cctx->username = getenv("USER");
- if (!doveadm_cmdline_try_run(cmd_name, argc, (const char**)argv, &cctx)) {
+ if (!doveadm_cmdline_try_run(cmd_name, argc, (const char**)argv, cctx)) {
if (doveadm_has_subcommands(cmd_name))
usage_prefix(cmd_name);
if (doveadm_has_unloaded_plugin(cmd_name)) {
}
usage();
}
- if (cctx.referral != NULL)
- i_fatal("Command requested referral: %s", cctx.referral);
- pool_unref(&cctx.pool);
+ if (cctx->referral != NULL)
+ i_fatal("Command requested referral: %s", cctx->referral);
+ doveadm_cmd_context_unref(&cctx);
if (!quick_init) {
doveadm_mail_deinit();
doveadm_dump_deinit();