Skip to content

Commit

Permalink
qga: Refactor guest-exec capture-output to take enum
Browse files Browse the repository at this point in the history
Previously capture-output was an optional boolean flag that either
captured all output or captured none. While this is OK in most cases, it
lacks flexibility for more advanced capture cases, such as wanting to
only capture stdout.

This commits refactors guest-exec qapi to take an enum for capture mode
instead while preserving backwards compatibility.

Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
  • Loading branch information
danobi authored and kostyanf14 committed May 4, 2023
1 parent 5a954e0 commit 9c5ccc5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
37 changes: 34 additions & 3 deletions qga/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,23 @@ static gboolean guest_exec_output_watch(GIOChannel *ch,
return false;
}

static GuestExecCaptureOutputMode ga_parse_capture_output(
GuestExecCaptureOutput *capture_output)
{
if (!capture_output)
return GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
else if (capture_output->type == QTYPE_QBOOL)
return capture_output->u.flag ? GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED
: GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE;
else
return capture_output->u.mode;
}

GuestExec *qmp_guest_exec(const char *path,
bool has_arg, strList *arg,
bool has_env, strList *env,
const char *input_data,
bool has_capture_output, bool capture_output,
GuestExecCaptureOutput *capture_output,
Error **errp)
{
GPid pid;
Expand All @@ -396,7 +408,8 @@ GuestExec *qmp_guest_exec(const char *path,
gint in_fd, out_fd, err_fd;
GIOChannel *in_ch, *out_ch, *err_ch;
GSpawnFlags flags;
bool has_output = (has_capture_output && capture_output);
bool has_output = false;
GuestExecCaptureOutputMode output_mode;
g_autofree uint8_t *input = NULL;
size_t ninput = 0;

Expand All @@ -415,8 +428,26 @@ GuestExec *qmp_guest_exec(const char *path,

flags = G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |
G_SPAWN_SEARCH_PATH_FROM_ENVP;
if (!has_output) {

output_mode = ga_parse_capture_output(capture_output);
switch (output_mode) {
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_NONE:
flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL;
break;
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDOUT:
has_output = true;
flags |= G_SPAWN_STDERR_TO_DEV_NULL;
break;
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_STDERR:
has_output = true;
flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
break;
case GUEST_EXEC_CAPTURE_OUTPUT_MODE_SEPARATED:
has_output = true;
break;
case GUEST_EXEC_CAPTURE_OUTPUT_MODE__MAX:
/* Silence warning; impossible branch */
break;
}

ret = g_spawn_async_with_pipes(NULL, argv, envp, flags,
Expand Down
33 changes: 32 additions & 1 deletion qga/qapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,37 @@
{ 'struct': 'GuestExec',
'data': { 'pid': 'int'} }

##
# @GuestExecCaptureOutputMode:
#
# An enumeration of guest-exec capture modes.
#
# @none: do not capture any output
# @stdout: only capture stdout
# @stderr: only capture stderr
# @separated: capture both stdout and stderr, but separated into
# GuestExecStatus out-data and err-data, respectively
#
# Since: 8.0
##
{ 'enum': 'GuestExecCaptureOutputMode',
'data': [ 'none', 'stdout', 'stderr', 'separated' ] }

##
# @GuestExecCaptureOutput:
#
# Controls what guest-exec output gets captures.
#
# @flag: captures both stdout and stderr if true. Equivalent
# to GuestExecCaptureOutputMode::all. (since 2.5)
# @mode: capture mode; preferred interface
#
# Since: 8.0
##
{ 'alternate': 'GuestExecCaptureOutput',
'data': { 'flag': 'bool',
'mode': 'GuestExecCaptureOutputMode'} }

##
# @guest-exec:
#
Expand All @@ -1218,7 +1249,7 @@
##
{ 'command': 'guest-exec',
'data': { 'path': 'str', '*arg': ['str'], '*env': ['str'],
'*input-data': 'str', '*capture-output': 'bool' },
'*input-data': 'str', '*capture-output': 'GuestExecCaptureOutput' },
'returns': 'GuestExec' }


Expand Down

0 comments on commit 9c5ccc5

Please sign in to comment.