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));
|
core: stop removing non-existent and duplicate lookup paths
When we would iterate over the lookup paths for each unit, making the list as
short as possible was important for performance. With the current cache, it
doesn't matter much. Two classes of paths were being removed:
- paths which don't exist in the filesystem
- paths which symlink to a path earlier in the search list
Both of those points cause problems with the caching code:
- if a user creates a directory that didn't exist before and puts units there,
now we will notice the new mtime an properly load the unit. When the path
was removed from list, we wouldn't.
- we now properly detect whether a unit path is on the path or not.
Before, if e.g. /lib/systemd/system, /usr/lib/systemd/systemd were both on
the path, and /lib was a symlink to /usr/lib, the second directory would be
pruned from the path. Then, the code would think that a symlink
/etc/systemd/system/foo.service→/lib/systemd/system/foo.service is an alias,
but /etc/systemd/system/foo.service→/usr/lib/systemd/system/foo.service would
be considered a link (in the systemctl link sense).
Removing the pruning has a slight negative performance impact in case of
usr-merge systems which have systemd compiled with non-usr-merge paths.
Non-usr-merge systems are deprecated, and this impact should be very small, so
I think it's OK. If it turns out to be an issue, the loop in function that
builds the cache could be improved to skip over "duplicate" directories with
same logic that the cache pruning did before. I didn't want to add this,
becuase it complicates the code to improve a corner case.
Fixes #13272.
2019-08-26 08:58:41 +02:00
|
|
|
lookup_paths_log(&lp_without_env);
|
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));
|
core: stop removing non-existent and duplicate lookup paths
When we would iterate over the lookup paths for each unit, making the list as
short as possible was important for performance. With the current cache, it
doesn't matter much. Two classes of paths were being removed:
- paths which don't exist in the filesystem
- paths which symlink to a path earlier in the search list
Both of those points cause problems with the caching code:
- if a user creates a directory that didn't exist before and puts units there,
now we will notice the new mtime an properly load the unit. When the path
was removed from list, we wouldn't.
- we now properly detect whether a unit path is on the path or not.
Before, if e.g. /lib/systemd/system, /usr/lib/systemd/systemd were both on
the path, and /lib was a symlink to /usr/lib, the second directory would be
pruned from the path. Then, the code would think that a symlink
/etc/systemd/system/foo.service→/lib/systemd/system/foo.service is an alias,
but /etc/systemd/system/foo.service→/usr/lib/systemd/system/foo.service would
be considered a link (in the systemctl link sense).
Removing the pruning has a slight negative performance impact in case of
usr-merge systems which have systemd compiled with non-usr-merge paths.
Non-usr-merge systems are deprecated, and this impact should be very small, so
I think it's OK. If it turns out to be an issue, the loop in function that
builds the cache could be improved to skip over "duplicate" directories with
same logic that the cache pruning did before. I didn't want to add this,
becuase it complicates the code to improve a corner case.
Fixes #13272.
2019-08-26 08:58:41 +02:00
|
|
|
lookup_paths_log(&lp_with_env);
|
|
|
|
assert_se(strv_equal(lp_with_env.search_path, STRV_MAKE(systemd_unit_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);
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:30:26 +01:00
|
|
|
static void test_generator_binary_paths(UnitFileScope scope) {
|
|
|
|
char template[] = "/tmp/test-path-lookup.XXXXXXX";
|
|
|
|
|
|
|
|
_cleanup_strv_free_ char **gp_without_env = NULL;
|
|
|
|
_cleanup_strv_free_ char **env_gp_without_env = NULL;
|
|
|
|
_cleanup_strv_free_ char **gp_with_env = NULL;
|
|
|
|
_cleanup_strv_free_ char **env_gp_with_env = NULL;
|
|
|
|
char *systemd_generator_path = NULL;
|
|
|
|
char *systemd_env_generator_path = NULL;
|
2015-01-09 05:29:33 +01:00
|
|
|
char **dir;
|
|
|
|
|
2020-02-17 13:30:26 +01:00
|
|
|
assert_se(mkdtemp(template));
|
|
|
|
|
|
|
|
assert_se(unsetenv("SYSTEMD_GENERATOR_PATH") == 0);
|
|
|
|
assert_se(unsetenv("SYSTEMD_ENVIRONMENT_GENERATOR_PATH") == 0);
|
|
|
|
|
|
|
|
gp_without_env = generator_binary_paths(scope);
|
|
|
|
env_gp_without_env = env_generator_binary_paths(scope == UNIT_FILE_SYSTEM ? true : false);
|
|
|
|
|
|
|
|
log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
|
|
|
|
STRV_FOREACH(dir, gp_without_env)
|
|
|
|
log_info(" %s", *dir);
|
|
|
|
|
|
|
|
log_info("Environment generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
|
|
|
|
STRV_FOREACH(dir, env_gp_without_env)
|
|
|
|
log_info(" %s", *dir);
|
|
|
|
|
|
|
|
assert_se(!strv_isempty(gp_without_env));
|
|
|
|
assert_se(!strv_isempty(env_gp_without_env));
|
|
|
|
|
|
|
|
systemd_generator_path = strjoina(template, "/systemd-generator-path");
|
|
|
|
systemd_env_generator_path = strjoina(template, "/systemd-environment-generator-path");
|
|
|
|
assert_se(setenv("SYSTEMD_GENERATOR_PATH", systemd_generator_path, 1) == 0);
|
|
|
|
assert_se(setenv("SYSTEMD_ENVIRONMENT_GENERATOR_PATH", systemd_env_generator_path, 1) == 0);
|
|
|
|
|
|
|
|
gp_with_env = generator_binary_paths(scope);
|
|
|
|
env_gp_with_env = env_generator_binary_paths(scope == UNIT_FILE_SYSTEM ? true : false);
|
|
|
|
|
2016-02-24 21:24:23 +01:00
|
|
|
log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
|
2020-02-17 13:30:26 +01:00
|
|
|
STRV_FOREACH(dir, gp_with_env)
|
|
|
|
log_info(" %s", *dir);
|
2015-01-09 05:29:33 +01:00
|
|
|
|
2020-02-17 13:30:26 +01:00
|
|
|
log_info("Environment generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
|
|
|
|
STRV_FOREACH(dir, env_gp_with_env)
|
2015-01-09 05:29:33 +01:00
|
|
|
log_info(" %s", *dir);
|
2020-02-17 13:30:26 +01:00
|
|
|
|
|
|
|
assert_se(strv_equal(gp_with_env, STRV_MAKE(systemd_generator_path)));
|
|
|
|
assert_se(strv_equal(env_gp_with_env, STRV_MAKE(systemd_env_generator_path)));
|
2015-01-09 05:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-02-17 13:30:26 +01:00
|
|
|
test_generator_binary_paths(UNIT_FILE_SYSTEM);
|
|
|
|
test_generator_binary_paths(UNIT_FILE_USER);
|
2015-01-09 05:29:33 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|