2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2015-10-31 20:00:32 +01:00
|
|
|
#include <stdlib.h>
|
2015-01-09 05:29:33 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "path-lookup.h"
|
2015-04-04 11:52:57 +02:00
|
|
|
#include "rm-rf.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
2018-09-13 14:31:13 +02:00
|
|
|
#include "tests.h"
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
static void test_paths(UnitFileScope scope) {
|
2015-01-09 05:29:33 +01:00
|
|
|
char template[] = "/tmp/test-path-lookup.XXXXXXX";
|
|
|
|
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(lookup_paths_free) LookupPaths lp_without_env = {};
|
|
|
|
_cleanup_(lookup_paths_free) LookupPaths lp_with_env = {};
|
2016-02-24 15:31:33 +01:00
|
|
|
char *systemd_unit_path;
|
2015-01-09 05:29:33 +01:00
|
|
|
|
|
|
|
assert_se(mkdtemp(template));
|
|
|
|
|
2015-10-31 20:00:32 +01:00
|
|
|
assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
|
2016-04-07 18:48:01 +02:00
|
|
|
assert_se(lookup_paths_init(&lp_without_env, scope, 0, NULL) >= 0);
|
2016-02-24 15:31:33 +01:00
|
|
|
assert_se(!strv_isempty(lp_without_env.search_path));
|
2016-04-06 19:15:12 +02:00
|
|
|
assert_se(lookup_paths_reduce(&lp_without_env) >= 0);
|
2015-10-31 20:00:32 +01:00
|
|
|
|
|
|
|
systemd_unit_path = strjoina(template, "/systemd-unit-path");
|
|
|
|
assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
|
2016-04-07 18:48:01 +02:00
|
|
|
assert_se(lookup_paths_init(&lp_with_env, scope, 0, NULL) == 0);
|
2016-02-24 15:31:33 +01:00
|
|
|
assert_se(strv_length(lp_with_env.search_path) == 1);
|
|
|
|
assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
|
2016-02-25 02:32:19 +01:00
|
|
|
assert_se(lookup_paths_reduce(&lp_with_env) >= 0);
|
2017-11-29 12:40:22 +01:00
|
|
|
assert_se(strv_isempty(lp_with_env.search_path));
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2015-04-04 11:52:57 +02:00
|
|
|
assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
|
2015-01-09 05:29:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:12:13 +01:00
|
|
|
static void test_user_and_global_paths(void) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(lookup_paths_free) LookupPaths lp_global = {}, lp_user = {};
|
2018-02-08 14:12:13 +01:00
|
|
|
char **u, **g, **p;
|
|
|
|
unsigned k = 0;
|
|
|
|
|
|
|
|
assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
|
2018-09-13 09:37:33 +02:00
|
|
|
assert_se(unsetenv("XDG_DATA_DIRS") == 0);
|
|
|
|
assert_se(unsetenv("XDG_CONFIG_DIRS") == 0);
|
2018-02-08 14:12:13 +01:00
|
|
|
|
|
|
|
assert_se(lookup_paths_init(&lp_global, UNIT_FILE_GLOBAL, 0, NULL) == 0);
|
|
|
|
assert_se(lookup_paths_init(&lp_user, UNIT_FILE_USER, 0, NULL) == 0);
|
|
|
|
g = lp_global.search_path;
|
|
|
|
u = lp_user.search_path;
|
|
|
|
|
|
|
|
/* Go over all entries in global search path, and verify
|
|
|
|
* that they also exist in the user search path. Skip any
|
|
|
|
* entries in user search path which don't exist in the global
|
|
|
|
* one, but not vice versa. */
|
|
|
|
log_info("/* %s */", __func__);
|
|
|
|
STRV_FOREACH(p, g) {
|
|
|
|
while (u[k] && !streq(*p, u[k])) {
|
|
|
|
log_info("+ %s", u[k]);
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
log_info(" %s", *p);
|
|
|
|
assert(u[k]); /* If NULL, we didn't find a matching entry */
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
STRV_FOREACH(p, u + k)
|
|
|
|
log_info("+ %s", *p);
|
|
|
|
}
|
|
|
|
|
2016-04-06 20:48:58 +02:00
|
|
|
static void print_generator_binary_paths(UnitFileScope scope) {
|
2015-01-09 05:29:33 +01:00
|
|
|
_cleanup_strv_free_ char **paths;
|
|
|
|
char **dir;
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2016-04-06 20:48:58 +02:00
|
|
|
paths = generator_binary_paths(scope);
|
2015-01-09 05:29:33 +01:00
|
|
|
STRV_FOREACH(dir, paths)
|
|
|
|
log_info(" %s", *dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
test_paths(UNIT_FILE_SYSTEM);
|
|
|
|
test_paths(UNIT_FILE_USER);
|
|
|
|
test_paths(UNIT_FILE_GLOBAL);
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2018-02-08 14:12:13 +01:00
|
|
|
test_user_and_global_paths();
|
|
|
|
|
2016-04-06 20:48:58 +02:00
|
|
|
print_generator_binary_paths(UNIT_FILE_SYSTEM);
|
|
|
|
print_generator_binary_paths(UNIT_FILE_USER);
|
2015-01-09 05:29:33 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|