For the moment, restore if-shell and run-shell to parsing at the last

moment (when the shell command completes) rather than when first
invoked, GitHub issue 2872.
pull/2899/head
nicm 2021-09-15 07:38:30 +00:00
parent e6b40cb339
commit a19cac5c46
2 changed files with 61 additions and 47 deletions

View File

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

View File

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