1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

Merge pull request #30363 from yuwata/analyze-find-template

analyze: also find template unit when an instance is specified
This commit is contained in:
Luca Boccassi 2023-12-11 15:56:21 +00:00 committed by GitHub
commit bf8726d1ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 11 deletions

View File

@ -72,6 +72,54 @@ int verify_prepare_filename(const char *filename, char **ret) {
return 0;
}
static int find_unit_directory(const char *p, char **ret) {
_cleanup_free_ char *a = NULL, *u = NULL, *t = NULL, *d = NULL;
int r;
assert(p);
assert(ret);
r = path_make_absolute_cwd(p, &a);
if (r < 0)
return r;
if (access(a, F_OK) >= 0) {
r = path_extract_directory(a, &d);
if (r < 0)
return r;
*ret = TAKE_PTR(d);
return 0;
}
r = path_extract_filename(a, &u);
if (r < 0)
return r;
if (!unit_name_is_valid(u, UNIT_NAME_INSTANCE))
return -ENOENT;
/* If the specified unit is an instance of a template unit, then let's try to find the template unit. */
r = unit_name_template(u, &t);
if (r < 0)
return r;
r = path_extract_directory(a, &d);
if (r < 0)
return r;
free(a);
a = path_join(d, t);
if (!a)
return -ENOMEM;
if (access(a, F_OK) < 0)
return -errno;
*ret = TAKE_PTR(d);
return 0;
}
int verify_set_unit_path(char **filenames) {
_cleanup_strv_free_ char **ans = NULL;
_cleanup_free_ char *joined = NULL;
@ -79,21 +127,15 @@ int verify_set_unit_path(char **filenames) {
int r;
STRV_FOREACH(filename, filenames) {
_cleanup_free_ char *a = NULL;
char *t;
_cleanup_free_ char *t = NULL;
r = path_make_absolute_cwd(*filename, &a);
if (r < 0)
r = find_unit_directory(*filename, &t);
if (r == -ENOMEM)
return r;
if (access(a, F_OK) < 0)
if (r < 0)
continue;
r = path_extract_directory(a, &t);
if (r < 0)
return r;
r = strv_consume(&ans, t);
r = strv_consume(&ans, TAKE_PTR(t));
if (r < 0)
return r;
}

View File

@ -296,6 +296,44 @@ EOF
# Verifies that the --offline= option works with --root=
systemd-analyze security --threshold=90 --offline=true --root=/tmp/img/ testfile.service
cat <<EOF >/tmp/foo@.service
[Service]
ExecStart=ls
EOF
cat <<EOF >/tmp/hoge@test.service
[Service]
ExecStart=ls
EOF
# issue #30357
pushd /tmp
systemd-analyze verify foo@bar.service
systemd-analyze verify foo@.service
systemd-analyze verify hoge@test.service
(! systemd-analyze verify hoge@nonexist.service)
(! systemd-analyze verify hoge@.service)
popd
pushd /
systemd-analyze verify tmp/foo@bar.service
systemd-analyze verify tmp/foo@.service
systemd-analyze verify tmp/hoge@test.service
(! systemd-analyze verify tmp/hoge@nonexist.service)
(! systemd-analyze verify tmp/hoge@.service)
popd
pushd /usr
systemd-analyze verify ../tmp/foo@bar.service
systemd-analyze verify ../tmp/foo@.service
systemd-analyze verify ../tmp/hoge@test.service
(! systemd-analyze verify ../tmp/hoge@nonexist.service)
(! systemd-analyze verify ../tmp/hoge@.service)
popd
systemd-analyze verify /tmp/foo@bar.service
systemd-analyze verify /tmp/foo@.service
systemd-analyze verify /tmp/hoge@test.service
(! systemd-analyze verify /tmp/hoge@nonexist.service)
(! systemd-analyze verify /tmp/hoge@.service)
# Added an additional "INVALID_ID" id to the .json to verify that nothing breaks when input is malformed
# The PrivateNetwork id description and weight was changed to verify that 'security' is actually reading in
# values from the .json file when required. The default weight for "PrivateNetwork" is 2500, and the new weight