Merge branch 'obsd-master' into master

This commit is contained in:
Thomas Adam 2021-09-15 10:01:10 +01:00
commit 2b9830f0e7
2 changed files with 61 additions and 47 deletions

View File

@ -20,6 +20,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
@ -52,8 +53,8 @@ const struct cmd_entry cmd_if_shell_entry = {
};
struct cmd_if_shell_data {
struct cmd_list *cmd_if;
struct cmd_list *cmd_else;
struct args_command_state *cmd_if;
struct args_command_state *cmd_else;
struct client *client;
struct cmdq_item *item;
@ -78,8 +79,9 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
char *shellcmd;
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
struct cmd_list *cmdlist = NULL;
struct cmd_list *cmdlist;
u_int count = args_count(args);
int wait = !args_has(args, 'b');
shellcmd = format_single_from_target(item, args_string(args, 0));
if (args_has(args, 'F')) {
@ -101,25 +103,21 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
cdata = xcalloc(1, sizeof *cdata);
cdata->cmd_if = args_make_commands_now(self, item, 1, 0);
if (cdata->cmd_if == NULL)
return (CMD_RETURN_ERROR);
cdata->cmd_if = args_make_commands_prepare(self, item, 1, NULL, wait,
0);
if (count == 3) {
cdata->cmd_else = args_make_commands_now(self, item, 2, 0);
if (cdata->cmd_else == NULL)
return (CMD_RETURN_ERROR);
cdata->cmd_else = args_make_commands_prepare(self, item, 2,
NULL, wait, 0);
}
if (!args_has(args, 'b'))
if (wait) {
cdata->client = cmdq_get_client(item);
else
cdata->item = item;
} else
cdata->client = tc;
if (cdata->client != NULL)
cdata->client->references++;
if (!args_has(args, 'b'))
cdata->item = item;
if (job_run(shellcmd, 0, NULL, s,
server_client_get_cwd(cmdq_get_client(item), s), NULL,
cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
@ -131,7 +129,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
}
free(shellcmd);
if (args_has(args, 'b'))
if (!wait)
return (CMD_RETURN_NORMAL);
return (CMD_RETURN_WAIT);
}
@ -142,18 +140,28 @@ cmd_if_shell_callback(struct job *job)
struct cmd_if_shell_data *cdata = job_get_data(job);
struct client *c = cdata->client;
struct cmdq_item *item = cdata->item, *new_item;
struct args_command_state *state;
struct cmd_list *cmdlist;
char *error;
int status;
status = job_get_status(job);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
cmdlist = cdata->cmd_else;
state = cdata->cmd_else;
else
cmdlist = cdata->cmd_if;
if (cmdlist == NULL)
state = cdata->cmd_if;
if (state == NULL)
goto out;
if (item == NULL) {
cmdlist = args_make_commands(state, 0, NULL, &error);
if (cmdlist == NULL) {
if (cdata->item == NULL) {
*error = toupper((u_char)*error);
status_message_set(c, -1, 1, 0, "%s", error);
} else
cmdq_error(cdata->item, "%s", error);
free(error);
} else if (item == NULL) {
new_item = cmdq_get_command(cmdlist, NULL);
cmdq_append(c, new_item);
} else {
@ -175,8 +183,8 @@ cmd_if_shell_free(void *data)
server_client_unref(cdata->client);
if (cdata->cmd_else != NULL)
cmd_list_free(cdata->cmd_else);
cmd_list_free(cdata->cmd_if);
args_make_commands_free(cdata->cmd_else);
args_make_commands_free(cdata->cmd_if);
free(cdata);
}

View File

@ -56,7 +56,7 @@ const struct cmd_entry cmd_run_shell_entry = {
struct cmd_run_shell_data {
struct client *client;
char *cmd;
struct cmd_list *cmdlist;
struct args_command_state *state;
char *cwd;
struct cmdq_item *item;
struct session *s;
@ -132,9 +132,8 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
if (cmd != NULL)
cdata->cmd = format_single_from_target(item, cmd);
} else {
cdata->cmdlist = args_make_commands_now(self, item, 0, 1);
if (cdata->cmdlist == NULL)
return (CMD_RETURN_ERROR);
cdata->state = args_make_commands_prepare(self, item, 0, NULL,
wait, 1);
}
if (args_has(args, 't') && wp != NULL)
@ -179,8 +178,10 @@ cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
struct client *c = cdata->client;
const char *cmd = cdata->cmd;
struct cmdq_item *item = cdata->item, *new_item;
struct cmd_list *cmdlist;
char *error;
if (cdata->cmdlist == NULL && cmd != NULL) {
if (cdata->state == NULL && cmd != NULL) {
if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL,
cmd_run_shell_callback, cmd_run_shell_free, cdata,
cdata->flags, -1, -1) == NULL)
@ -188,16 +189,21 @@ cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
return;
}
if (cdata->cmdlist != NULL) {
if (item == NULL) {
new_item = cmdq_get_command(cdata->cmdlist, NULL);
cmdlist = args_make_commands(cdata->state, 0, NULL, &error);
if (cmdlist == NULL) {
if (cdata->item == NULL) {
*error = toupper((u_char)*error);
status_message_set(c, -1, 1, 0, "%s", error);
} else
cmdq_error(cdata->item, "%s", error);
free(error);
} else if (item == NULL) {
new_item = cmdq_get_command(cmdlist, NULL);
cmdq_append(c, new_item);
} else {
new_item = cmdq_get_command(cdata->cmdlist,
cmdq_get_state(item));
new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
cmdq_insert_after(item, new_item);
}
}
if (cdata->item != NULL)
cmdq_continue(cdata->item);
@ -264,8 +270,8 @@ cmd_run_shell_free(void *data)
session_remove_ref(cdata->s, __func__);
if (cdata->client != NULL)
server_client_unref(cdata->client);
if (cdata->cmdlist != NULL)
cmd_list_free(cdata->cmdlist);
if (cdata->state != NULL)
args_make_commands_free(cdata->state);
free(cdata->cwd);
free(cdata->cmd);
free(cdata);