Merge pull request #19383 from keszybz/test58-fixes

Fixes for TEST-58-REPART and ExecStart deserialization logic
This commit is contained in:
Frantisek Sumsal
2021-04-26 19:16:44 +02:00
committed by GitHub
17 changed files with 200 additions and 106 deletions
+2 -2
View File
@@ -1198,8 +1198,8 @@ static void bump_file_max_and_nr_open(void) {
#endif
#if BUMP_PROC_SYS_FS_FILE_MAX
/* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously thing where
* different but the operation would fail silently.) */
/* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously things were
* different, but the operation would fail silently.) */
r = sysctl_writef("fs/file-max", "%li\n", LONG_MAX);
if (r < 0)
log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.file-max, ignoring: %m");
+4 -6
View File
@@ -2725,7 +2725,7 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
return 0;
}
static int service_deserialize_exec_command(
int service_deserialize_exec_command(
Unit *u,
const char *key,
const char *value) {
@@ -2783,9 +2783,6 @@ static int service_deserialize_exec_command(
case STATE_EXEC_COMMAND_PATH:
path = TAKE_PTR(arg);
state = STATE_EXEC_COMMAND_ARGS;
if (!path_is_absolute(path))
return -EINVAL;
break;
case STATE_EXEC_COMMAND_ARGS:
r = strv_extend(&argv, arg);
@@ -2793,13 +2790,14 @@ static int service_deserialize_exec_command(
return -ENOMEM;
break;
default:
assert_not_reached("Unknown error at deserialization of exec command");
break;
assert_not_reached("Logic error in exec command deserialization");
}
}
if (state != STATE_EXEC_COMMAND_ARGS)
return -EINVAL;
if (strv_isempty(argv))
return -EINVAL; /* At least argv[0] must be always present. */
/* Let's check whether exec command on given offset matches data that we just deserialized */
for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) {
+3
View File
@@ -255,3 +255,6 @@ ServiceTimeoutFailureMode service_timeout_failure_mode_from_string(const char *s
DEFINE_CAST(SERVICE, Service);
#define STATUS_TEXT_MAX (16U*1024U)
/* Only exported for unit tests */
int service_deserialize_exec_command(Unit *u, const char *key, const char *value);
+1 -1
View File
@@ -774,7 +774,7 @@ static int run(int argc, char *argv[]) {
FLAGS_SET(arg_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
&d);
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device: %m");
return log_error_errno(r, "Failed to set up loopback device for %s: %m", arg_image);
r = dissect_image_and_warn(
d->fd,
+1 -1
View File
@@ -376,7 +376,7 @@ static int portable_extract_by_path(
return r;
} else if (r < 0)
return log_debug_errno(r, "Failed to set up loopback device: %m");
return log_debug_errno(r, "Failed to set up loopback device for %s: %m", path);
else {
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
_cleanup_(rmdir_and_freep) char *tmpdir = NULL;
+1 -1
View File
@@ -2741,7 +2741,7 @@ int mount_image_privately_interactively(
FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
&d);
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device: %m");
return log_error_errno(r, "Failed to set up loopback device for %s: %m", image);
r = dissect_image_and_warn(d->fd, image, &verity, NULL, d->uevent_seqnum_not_before, d->timestamp_not_before, flags, &dissected_image);
if (r < 0)
+1 -1
View File
@@ -525,7 +525,7 @@ static int merge_subprocess(Hashmap *images, const char *workspace) {
r = loop_device_make_by_path(img->path, O_RDONLY, 0, &d);
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device: %m");
return log_error_errno(r, "Failed to set up loopback device for %s: %m", img->path);
r = dissect_image_and_warn(
d->fd,
+11
View File
@@ -133,6 +133,17 @@ tests += [
[['src/test/test-serialize.c']],
[['src/test/test-unit-serialize.c'],
[libcore,
libshared],
[threads,
librt,
libseccomp,
libselinux,
libmount,
libblkid],
core_includes],
[['src/test/test-utf8.c']],
[['src/test/test-dev-setup.c']],
+68
View File
@@ -0,0 +1,68 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "rm-rf.h"
#include "service.h"
#include "tests.h"
#define EXEC_START_ABSOLUTE \
"ExecStart 0 /bin/sh \"sh\" \"-e\" \"-x\" \"-c\" \"systemctl --state=failed --no-legend --no-pager >/failed ; systemctl daemon-reload ; echo OK >/testok\""
#define EXEC_START_RELATIVE \
"ExecStart 0 sh \"sh\" \"-e\" \"-x\" \"-c\" \"systemctl --state=failed --no-legend --no-pager >/failed ; systemctl daemon-reload ; echo OK >/testok\""
static void test_deserialize_exec_command_one(Manager *m, const char *key, const char *line, int expected) {
_cleanup_(unit_freep) Unit *u = NULL;
int r;
assert_se(unit_new_for_name(m, sizeof(Service), "test.service", &u) >= 0);
r = service_deserialize_exec_command(u, key, line);
log_debug("[%s] → %d (expected: %d)", line, r, expected);
assert(r == expected);
/* Note that the command doesn't match any command in the empty list of commands in 's', so it is
* always rejected with "Current command vanished from the unit file", and we don't leak anything. */
}
static void test_deserialize_exec_command(void) {
_cleanup_(manager_freep) Manager *m = NULL;
int r;
log_info("/* %s */", __func__);
r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
if (manager_errno_skip_test(r)) {
log_notice_errno(r, "Skipping test: manager_new: %m");
return;
}
assert_se(r >= 0);
test_deserialize_exec_command_one(m, "main-command", EXEC_START_ABSOLUTE, 0);
test_deserialize_exec_command_one(m, "main-command", EXEC_START_RELATIVE, 0);
test_deserialize_exec_command_one(m, "control-command", EXEC_START_ABSOLUTE, 0);
test_deserialize_exec_command_one(m, "control-command", EXEC_START_RELATIVE, 0);
test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /bin/sh \"sh\"", 0);
test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /no/command ", -EINVAL);
test_deserialize_exec_command_one(m, "control-command", "ExecStart 0 /bad/quote \"", -EINVAL);
test_deserialize_exec_command_one(m, "control-command", "ExecStart s /bad/id x y z", -EINVAL);
test_deserialize_exec_command_one(m, "control-command", "ExecStart 11", -EINVAL);
test_deserialize_exec_command_one(m, "control-command", "ExecWhat 11 /a/b c d e", -EINVAL);
}
int main(int argc, char *argv[]) {
_cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
int r;
test_setup_logging(LOG_DEBUG);
r = enter_cgroup_subroot(NULL);
if (r == -ENOMEDIUM)
return log_tests_skipped("cgroupfs not available");
assert_se(runtime_dir = setup_fake_runtime_dir());
test_deserialize_exec_command();
return EXIT_SUCCESS;
}