From: Yu Watanabe Date: Thu, 7 Dec 2023 10:29:29 +0000 (+0900) Subject: analyze: also find template unit when a template instance is specified X-Git-Tag: v256-rc1~1520^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d9d55657946385916fa4db7149a9b389645ee73;p=thirdparty%2Fsystemd.git analyze: also find template unit when a template instance is specified Fixes a regression caused by 2f6181ad4d6c126e3ebf6880ba30b3b0059c6fc8. Fixes #30357. Co-authored-by: Jeff King --- diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index 26d11304771..6fbd6fa54c3 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -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) - continue; - - r = path_extract_directory(a, &t); if (r < 0) - return r; + continue; - r = strv_consume(&ans, t); + r = strv_consume(&ans, TAKE_PTR(t)); if (r < 0) return r; }