Merge pull request #33790 from YHNdnzj/run-exec-flags

run: clean up ExecCommandFlags serialization
This commit is contained in:
Luca Boccassi
2024-07-21 11:44:20 +01:00
committed by GitHub
4 changed files with 44 additions and 49 deletions
+12 -6
View File
@@ -21,6 +21,7 @@
#include "chase.h"
#include "env-util.h"
#include "escape.h"
#include "exec-util.h"
#include "exit-status.h"
#include "fd-util.h"
#include "format-util.h"
@@ -1246,12 +1247,17 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
if (r < 0)
return bus_log_create_error(r);
if (use_ex_prop)
r = sd_bus_message_append_strv(
m,
STRV_MAKE(arg_expand_environment > 0 ? NULL : "no-env-expand",
arg_ignore_failure ? "ignore-failure" : NULL));
else
if (use_ex_prop) {
_cleanup_strv_free_ char **opts = NULL;
r = exec_command_flags_to_strv(
(arg_expand_environment > 0 ? 0 : EXEC_COMMAND_NO_ENV_EXPAND)|(arg_ignore_failure ? EXEC_COMMAND_IGNORE_FAILURE : 0),
&opts);
if (r < 0)
return log_error_errno(r, "Failed to format execute flags: %m");
r = sd_bus_message_append_strv(m, opts);
} else
r = sd_bus_message_append(m, "b", arg_ignore_failure);
if (r < 0)
return bus_log_create_error(r);
+23 -26
View File
@@ -8,6 +8,7 @@
#include <stdio.h>
#include "alloc-util.h"
#include "bitfield.h"
#include "conf-files.h"
#include "env-file.h"
#include "env-util.h"
@@ -425,46 +426,42 @@ static int gather_environment_consume(int fd, void *arg) {
return r;
}
int exec_command_flags_from_strv(char **ex_opts, ExecCommandFlags *flags) {
ExecCommandFlags ex_flag, ret_flags = 0;
int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret) {
ExecCommandFlags flags = 0;
assert(flags);
assert(ret);
STRV_FOREACH(opt, ex_opts) {
ex_flag = exec_command_flags_from_string(*opt);
if (ex_flag < 0)
return ex_flag;
ret_flags |= ex_flag;
ExecCommandFlags fl = exec_command_flags_from_string(*opt);
if (fl < 0)
return fl;
flags |= fl;
}
*flags = ret_flags;
*ret = flags;
return 0;
}
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ex_opts) {
_cleanup_strv_free_ char **ret_opts = NULL;
ExecCommandFlags it = flags;
const char *str;
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret) {
_cleanup_strv_free_ char **opts = NULL;
int r;
assert(ex_opts);
assert(flags >= 0);
assert(ret);
if (flags < 0)
return flags;
BIT_FOREACH(i, flags) {
const char *s = exec_command_flags_to_string(1 << i);
if (!s)
return -EINVAL;
for (unsigned i = 0; it != 0; it &= ~(1 << i), i++)
if (FLAGS_SET(flags, (1 << i))) {
str = exec_command_flags_to_string(1 << i);
if (!str)
return -EINVAL;
r = strv_extend(&opts, s);
if (r < 0)
return r;
}
r = strv_extend(&ret_opts, str);
if (r < 0)
return r;
}
*ex_opts = TAKE_PTR(ret_opts);
*ret = TAKE_PTR(opts);
return 0;
}
+2 -2
View File
@@ -51,8 +51,8 @@ int execute_directories(
char *envp[],
ExecDirFlags flags);
int exec_command_flags_from_strv(char **ex_opts, ExecCommandFlags *flags);
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ex_opts);
int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret);
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret);
extern const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX];
+7 -15
View File
@@ -429,27 +429,19 @@ TEST(exec_command_flags_from_strv) {
}
TEST(exec_command_flags_to_strv) {
_cleanup_strv_free_ char **opts = NULL, **empty_opts = NULL, **invalid_opts = NULL;
ExecCommandFlags flags = 0;
int r;
_cleanup_strv_free_ char **opts = NULL;
flags |= (EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE);
r = exec_command_flags_to_strv(flags, &opts);
assert_se(r == 0);
ASSERT_OK(exec_command_flags_to_strv(EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE, &opts));
assert_se(strv_equal(opts, STRV_MAKE("ignore-failure", "ambient", "no-env-expand")));
r = exec_command_flags_to_strv(0, &empty_opts);
opts = strv_free(opts);
assert_se(r == 0);
assert_se(strv_equal(empty_opts, STRV_MAKE_EMPTY));
ASSERT_OK(exec_command_flags_to_strv(0, &opts));
assert_se(strv_isempty(opts));
flags = _EXEC_COMMAND_FLAGS_INVALID;
opts = strv_free(opts);
r = exec_command_flags_to_strv(flags, &invalid_opts);
assert_se(r == -EINVAL);
ASSERT_ERROR(exec_command_flags_to_strv(1U << 20, &opts), EINVAL);
}
DEFINE_TEST_MAIN(LOG_DEBUG);