test-install-root.c revision 0ec0deaa30d0e68430f03fa6f32affa576481d18
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt/***
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt This file is part of systemd.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt Copyright 2011 Lennart Poettering
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemd is free software; you can redistribute it and/or modify it
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt under the terms of the GNU Lesser General Public License as published by
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt the Free Software Foundation; either version 2.1 of the License, or
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt (at your option) any later version.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemd is distributed in the hope that it will be useful, but
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt WITHOUT ANY WARRANTY; without even the implied warranty of
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt Lesser General Public License for more details.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt You should have received a copy of the GNU Lesser General Public License
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt along with systemd; If not, see <http://www.gnu.org/licenses/>.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt***/
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#include "alloc-util.h"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#include "fileio.h"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt#include "install.h"
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams#include "mkdir.h"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#include "rm-rf.h"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#include "string-util.h"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktstatic void test_basic_mask_and_enable(const char *root) {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt const char *p;
3df3e884ae1237ef0d4d23b0e80f4ffda95ac135Ronny Chevalier UnitFileState state;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt UnitFileChange *changes = NULL;
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt unsigned n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", NULL) == -ENOENT);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", NULL) == -ENOENT);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", NULL) == -ENOENT);
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", NULL) == -ENOENT);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams p = strjoina(root, "/usr/lib/systemd/system/a.service");
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(write_string_file(p,
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt "[Install]\n"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", NULL) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/b.service");
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(symlink("a.service", p) >= 0);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", NULL) >= 0);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/c.service");
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(symlink("/usr/lib/systemd/system/a.service", p) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", NULL) >= 0);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/d.service");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(symlink("c.service", p) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt /* This one is interesting, as d follows a relative, then an absolute symlink */
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", NULL) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_mask(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), false, &changes, &n_changes) >= 0);
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen assert_se(n_changes == 1);
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams assert_se(changes[0].type == UNIT_FILE_SYMLINK);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(streq(changes[0].source, "/dev/null"));
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/a.service");
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(streq(changes[0].path, p));
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt unit_file_changes_free(changes, n_changes);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt changes = NULL; n_changes = 0;
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_MASKED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_MASKED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_MASKED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_MASKED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt /* Enabling a masked unit should fail! */
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), false, &changes, &n_changes) == -ESHUTDOWN);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt unit_file_changes_free(changes, n_changes);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt changes = NULL; n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_unmask(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), &changes, &n_changes) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(n_changes == 1);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/a.service");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(streq(changes[0].path, p));
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt unit_file_changes_free(changes, n_changes);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt changes = NULL; n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), false, &changes, &n_changes) == 1);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(n_changes == 1);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/a.service"));
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/a.service");
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(streq(changes[0].path, p));
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt unit_file_changes_free(changes, n_changes);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt changes = NULL; n_changes = 0;
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt /* Enabling it again should succeed but be a NOP */
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), false, &changes, &n_changes) == 1);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt assert_se(n_changes == 0);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt unit_file_changes_free(changes, n_changes);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt changes = NULL; n_changes = 0;
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), &changes, &n_changes) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(n_changes == 1);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/a.service");
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(streq(changes[0].path, p));
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt unit_file_changes_free(changes, n_changes);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt changes = NULL; n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt /* Disabling a disabled unit must suceed but be a NOP */
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("a.service"), &changes, &n_changes) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(n_changes == 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt unit_file_changes_free(changes, n_changes);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt changes = NULL; n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt /* Let's enable this indirectly via a symlink */
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("d.service"), false, &changes, &n_changes) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(n_changes == 1);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(changes[0].type == UNIT_FILE_SYMLINK);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(streq(changes[0].source, "/usr/lib/systemd/system/a.service"));
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/a.service");
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(streq(changes[0].path, p));
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams unit_file_changes_free(changes, n_changes);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams changes = NULL; n_changes = 0;
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt /* Let's try to reenable */
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(unit_file_reenable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("b.service"), false, &changes, &n_changes) >= 0);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(n_changes == 2);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(changes[0].type == UNIT_FILE_UNLINK);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/a.service");
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(streq(changes[0].path, p));
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(changes[1].type == UNIT_FILE_SYMLINK);
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(streq(changes[1].source, "/usr/lib/systemd/system/a.service"));
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_se(streq(changes[1].path, p));
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt unit_file_changes_free(changes, n_changes);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt changes = NULL; n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "d.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller}
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Hallerstatic void test_linked_units(const char *root) {
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller const char *p, *q;
ebe207d4acf38165adbc45298662982eecdb9e9fTom Gundersen UnitFileState state;
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams UnitFileChange *changes = NULL;
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams unsigned n_changes = 0, i;
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams /*
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams * We'll test three cases here:
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams *
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * a) a unit file in /opt, that we use "systemctl link" and
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * "systemctl enable" on to make it available to the system
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen *
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * b) a unit file in /opt, that is statically linked into
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * /usr/lib/systemd/system, that "enable" should work on
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * correctly.
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen *
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * c) a unit file in /opt, that is linked into
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * /etc/systemd/system, and where "enable" should result in
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen * -ELOOP, since using information from /etc to generate
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen * information in /etc should not be allowed.
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen */
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen p = strjoina(root, "/opt/linked.service");
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen assert_se(write_string_file(p,
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen "[Install]\n"
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen p = strjoina(root, "/opt/linked2.service");
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen assert_se(write_string_file(p,
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen "[Install]\n"
fe4b2156256c5bdf52341576571ce9f095d9f085Tom Gundersen "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen
ebe207d4acf38165adbc45298662982eecdb9e9fTom Gundersen p = strjoina(root, "/opt/linked3.service");
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen assert_se(write_string_file(p,
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams "[Install]\n"
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", NULL) == -ENOENT);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked2.service", NULL) == -ENOENT);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked3.service", NULL) == -ENOENT);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/linked2.service");
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(symlink("/opt/linked2.service", p) >= 0);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked3.service");
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(symlink("/opt/linked3.service", p) >= 0);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", &state) == -ENOENT);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked2.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked3.service", &state) >= 0 && state == UNIT_FILE_LINKED);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt /* First, let's link the unit into the search path */
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_link(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("/opt/linked.service"), false, &changes, &n_changes) >= 0);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(n_changes == 1);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(streq(changes[0].source, "/opt/linked.service"));
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked.service");
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(streq(changes[0].path, p));
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt unit_file_changes_free(changes, n_changes);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt changes = NULL; n_changes = 0;
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", &state) >= 0 && state == UNIT_FILE_LINKED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt /* Let's unlink it from the search path again */
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("linked.service"), &changes, &n_changes) >= 0);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(n_changes == 1);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked.service");
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(streq(changes[0].path, p));
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt unit_file_changes_free(changes, n_changes);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt changes = NULL; n_changes = 0;
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", NULL) == -ENOENT);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt /* Now, let's not just link it, but also enable it */
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("/opt/linked.service"), false, &changes, &n_changes) >= 0);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(n_changes == 2);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/linked.service");
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt q = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked.service");
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt for (i = 0 ; i < n_changes; i++) {
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(changes[i].type == UNIT_FILE_SYMLINK);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(streq(changes[i].source, "/opt/linked.service"));
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt if (p && streq(changes[i].path, p))
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt p = NULL;
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt else if (q && streq(changes[i].path, q))
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt q = NULL;
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt else
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_not_reached("wut?");
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt }
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert(!p && !q);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt unit_file_changes_free(changes, n_changes);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt changes = NULL; n_changes = 0;
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt /* And let's unlink it again */
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("linked.service"), &changes, &n_changes) >= 0);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt assert_se(n_changes == 2);
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/linked.service");
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt q = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked.service");
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt for (i = 0; i < n_changes; i++) {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(changes[i].type == UNIT_FILE_UNLINK);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if (p && streq(changes[i].path, p))
c806ffb9592fa9a2b13a1f9f9be4c77cd5b211aaZbigniew Jędrzejewski-Szmek p = NULL;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt else if (q && streq(changes[i].path, q))
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt q = NULL;
4e3e6679e8f73b83d38e4b20d8b025e12991d1cbPatrik Flykt else
4e3e6679e8f73b83d38e4b20d8b025e12991d1cbPatrik Flykt assert_not_reached("wut?");
4e3e6679e8f73b83d38e4b20d8b025e12991d1cbPatrik Flykt }
4e3e6679e8f73b83d38e4b20d8b025e12991d1cbPatrik Flykt assert(!p && !q);
4e3e6679e8f73b83d38e4b20d8b025e12991d1cbPatrik Flykt unit_file_changes_free(changes, n_changes);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt changes = NULL; n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "linked.service", NULL) == -ENOENT);
c806ffb9592fa9a2b13a1f9f9be4c77cd5b211aaZbigniew Jędrzejewski-Szmek
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("linked2.service"), false, &changes, &n_changes) >= 0);
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt assert_se(n_changes == 2);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/linked2.service");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt q = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/linked2.service");
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt for (i = 0 ; i < n_changes; i++) {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(changes[i].type == UNIT_FILE_SYMLINK);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(streq(changes[i].source, "/opt/linked2.service"));
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt if (p && streq(changes[i].path, p))
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = NULL;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt else if (q && streq(changes[i].path, q))
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt q = NULL;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt else
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_not_reached("wut?");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt }
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert(!p && !q);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt unit_file_changes_free(changes, n_changes);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt changes = NULL; n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("linked3.service"), false, &changes, &n_changes) == -ELOOP);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt unit_file_changes_free(changes, n_changes);
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt changes = NULL; n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktstatic void test_default(const char *root) {
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt _cleanup_free_ char *def = NULL;
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt UnitFileChange *changes = NULL;
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt unsigned n_changes = 0;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt const char *p;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/test-default-real.target");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(write_string_file(p, "# pretty much empty", WRITE_STRING_FILE_CREATE) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/test-default.target");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(symlink("test-default-real.target", p) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_default(UNIT_FILE_SYSTEM, root, &def) == -ENOENT);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_set_default(UNIT_FILE_SYSTEM, root, "idontexist.target", false, &changes, &n_changes) == -ENOENT);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(n_changes == 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt unit_file_changes_free(changes, n_changes);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt changes = NULL; n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_default(UNIT_FILE_SYSTEM, root, &def) == -ENOENT);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_set_default(UNIT_FILE_SYSTEM, root, "test-default.target", false, &changes, &n_changes) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(n_changes == 1);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/test-default-real.target"));
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/default.target");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(streq(changes[0].path, p));
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt unit_file_changes_free(changes, n_changes);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt changes = NULL; n_changes = 0;
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_get_default(UNIT_FILE_SYSTEM, root, &def) >= 0);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(streq_ptr(def, "test-default-real.target"));
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktstatic void test_add_dependency(const char *root) {
ed6ee21953dac9c78383da00bc4514ece6b75ab5Patrik Flykt UnitFileChange *changes = NULL;
ed6ee21953dac9c78383da00bc4514ece6b75ab5Patrik Flykt unsigned n_changes = 0;
926695f1b5f9395eeb416cc2f478a9cf75fdbeb4Thomas Hindoe Paaboel Andersen const char *p;
926695f1b5f9395eeb416cc2f478a9cf75fdbeb4Thomas Hindoe Paaboel Andersen
ed6ee21953dac9c78383da00bc4514ece6b75ab5Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/real-add-dependency-test-target.target");
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(write_string_file(p, "# pretty much empty", WRITE_STRING_FILE_CREATE) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/add-dependency-test-target.target");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(symlink("real-add-dependency-test-target.target", p) >= 0);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/real-add-dependency-test-service.service");
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(write_string_file(p, "# pretty much empty", WRITE_STRING_FILE_CREATE) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/add-dependency-test-service.service");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(symlink("real-add-dependency-test-service.service", p) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_add_dependency(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("add-dependency-test-service.service"), "add-dependency-test-target.target", UNIT_WANTS, false, &changes, &n_changes) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(n_changes == 1);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/real-add-dependency-test-service.service"));
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/real-add-dependency-test-target.target.wants/real-add-dependency-test-service.service");
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(streq(changes[0].path, p));
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt unit_file_changes_free(changes, n_changes);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt changes = NULL; n_changes = 0;
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktstatic void test_template_enable(const char *root) {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt UnitFileChange *changes = NULL;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt unsigned n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt UnitFileState state;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt const char *p;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/template@.service");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(write_string_file(p,
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "[Install]\n"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt "DefaultInstance=def\n"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/template-symlink@.service");
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(symlink("template@.service", p) >= 0);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("template@.service"), false, &changes, &n_changes) >= 0);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(n_changes == 1);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/template@.service"));
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/template@def.service");
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(streq(changes[0].path, p));
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt unit_file_changes_free(changes, n_changes);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt changes = NULL; n_changes = 0;
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("template@.service"), &changes, &n_changes) >= 0);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(n_changes == 1);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(streq(changes[0].path, p));
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt unit_file_changes_free(changes, n_changes);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt changes = NULL; n_changes = 0;
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("template@foo.service"), false, &changes, &n_changes) >= 0);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/template@.service"));
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/template@foo.service");
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(streq(changes[0].path, p));
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt unit_file_changes_free(changes, n_changes);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt changes = NULL; n_changes = 0;
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("template@foo.service"), &changes, &n_changes) >= 0);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(n_changes == 1);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(streq(changes[0].path, p));
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt unit_file_changes_free(changes, n_changes);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt changes = NULL; n_changes = 0;
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@quux.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@quux.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("template-symlink@quux.service"), false, &changes, &n_changes) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/template@.service"));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/template@quux.service");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(streq(changes[0].path, p));
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt unit_file_changes_free(changes, n_changes);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt changes = NULL; n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template@quux.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@foo.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@quux.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt}
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flyktstatic void test_indirect(const char *root) {
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt UnitFileChange *changes = NULL;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unsigned n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt UnitFileState state;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt const char *p;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirecta.service", &state) == -ENOENT);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectb.service", &state) == -ENOENT);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectc.service", &state) == -ENOENT);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/indirecta.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(write_string_file(p,
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt "[Install]\n"
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt "Also=indirectb.service\n", WRITE_STRING_FILE_CREATE) >= 0);
4b4923e65423e60d755841b5b264730e8f3deab3Tom Gundersen
5e91345094a9e983e7abb2313334e7808bcd2cc2Tom Gundersen p = strjoina(root, "/usr/lib/systemd/system/indirectb.service");
513a6fa8679510ea1b55967bdb482dd5f8a39f21Ronny Chevalier assert_se(write_string_file(p,
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt "[Install]\n"
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/indirectc.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(symlink("indirecta.service", p) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirecta.service", &state) >= 0 && state == UNIT_FILE_INDIRECT);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectb.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectc.service", &state) >= 0 && state == UNIT_FILE_INDIRECT);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(unit_file_enable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("indirectc.service"), false, &changes, &n_changes) >= 0);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(n_changes == 1);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/indirectb.service"));
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/indirectb.service");
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt assert_se(streq(changes[0].path, p));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unit_file_changes_free(changes, n_changes);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt changes = NULL; n_changes = 0;
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirecta.service", &state) >= 0 && state == UNIT_FILE_INDIRECT);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectb.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "indirectc.service", &state) >= 0 && state == UNIT_FILE_INDIRECT);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("indirectc.service"), &changes, &n_changes) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(n_changes == 1);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/indirectb.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[0].path, p));
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt unit_file_changes_free(changes, n_changes);
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt changes = NULL; n_changes = 0;
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flyktstatic void test_preset_and_list(const char *root) {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt UnitFileChange *changes = NULL;
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt unsigned n_changes = 0, i;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt const char *p, *q;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt UnitFileState state;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt bool got_yes = false, got_no = false;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt Iterator j;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt UnitFileList *fl;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt Hashmap *h;
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) == -ENOENT);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/preset-yes.service");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(write_string_file(p,
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "[Install]\n"
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system/preset-no.service");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(write_string_file(p,
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "[Install]\n"
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt p = strjoina(root, "/usr/lib/systemd/system-preset/test.preset");
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(write_string_file(p,
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "enable *-yes.*\n"
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt "disable *\n", WRITE_STRING_FILE_CREATE) >= 0);
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt
3dc34fcc97b41f8b7b019027225b121dfbb9871dPatrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_preset(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("preset-yes.service"), UNIT_FILE_PRESET_FULL, false, &changes, &n_changes) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(n_changes == 1);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(changes[0].type == UNIT_FILE_SYMLINK);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[0].source, "/usr/lib/systemd/system/preset-yes.service"));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/preset-yes.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[0].path, p));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unit_file_changes_free(changes, n_changes);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt changes = NULL; n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_disable(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("preset-yes.service"), &changes, &n_changes) >= 0);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(n_changes == 1);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(changes[0].type == UNIT_FILE_UNLINK);
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/preset-yes.service");
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt assert_se(streq(changes[0].path, p));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unit_file_changes_free(changes, n_changes);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt changes = NULL; n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt assert_se(unit_file_preset(UNIT_FILE_SYSTEM, false, root, STRV_MAKE("preset-no.service"), UNIT_FILE_PRESET_FULL, false, &changes, &n_changes) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(n_changes == 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unit_file_changes_free(changes, n_changes);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt changes = NULL; n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_preset_all(UNIT_FILE_SYSTEM, false, root, UNIT_FILE_PRESET_FULL, false, &changes, &n_changes) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(n_changes > 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/preset-yes.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen for (i = 0; i < n_changes; i++) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if (changes[i].type == UNIT_FILE_SYMLINK) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[i].source, "/usr/lib/systemd/system/preset-yes.service"));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(streq(changes[i].path, p));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt } else
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(changes[i].type == UNIT_FILE_UNLINK);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt }
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt unit_file_changes_free(changes, n_changes);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt changes = NULL; n_changes = 0;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
356779df90a2ecab5da2cb310ad0f8ebc9ca9f46Lennart Poettering assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-yes.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "preset-no.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen assert_se(h = hashmap_new(&string_hash_ops));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_list(UNIT_FILE_SYSTEM, root, h) >= 0);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/preset-yes.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt q = strjoina(root, "/usr/lib/systemd/system/preset-no.service");
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt HASHMAP_FOREACH(fl, h, j) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, basename(fl->path), &state) >= 0);
fa94c34b083b5b4019975624453e53d0cbad2a5dTom Gundersen assert_se(fl->state == state);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if (streq(fl->path, p)) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt got_yes = true;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(fl->state == UNIT_FILE_ENABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt } else if (streq(fl->path, q)) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt got_no = true;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(fl->state == UNIT_FILE_DISABLED);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt } else
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(IN_SET(fl->state, UNIT_FILE_DISABLED, UNIT_FILE_STATIC, UNIT_FILE_INDIRECT));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt }
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen
356779df90a2ecab5da2cb310ad0f8ebc9ca9f46Lennart Poettering unit_file_list_free(h);
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen
9021bb9f935c93b516b10c88db2a212a9e3a8140Tom Gundersen assert_se(got_yes && got_no);
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktint main(int argc, char *argv[]) {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt char root[] = "/tmp/rootXXXXXX";
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt const char *p;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt assert_se(mkdtemp(root));
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system/");
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(mkdir_p(p, 0755) >= 0);
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/");
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(mkdir_p(p, 0755) >= 0);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt p = strjoina(root, "/run/systemd/system/");
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(mkdir_p(p, 0755) >= 0);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen p = strjoina(root, "/opt/");
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen assert_se(mkdir_p(p, 0755) >= 0);
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt p = strjoina(root, "/usr/lib/systemd/system-preset/");
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt assert_se(mkdir_p(p, 0755) >= 0);
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt test_basic_mask_and_enable(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt test_linked_units(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt test_default(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt test_add_dependency(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt test_template_enable(root);
44481a8b537839cd9ffead4d261491641f5b5260Zbigniew Jędrzejewski-Szmek test_indirect(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt test_preset_and_list(root);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt assert_se(rm_rf(root, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt return 0;
44481a8b537839cd9ffead4d261491641f5b5260Zbigniew Jędrzejewski-Szmek}
44481a8b537839cd9ffead4d261491641f5b5260Zbigniew Jędrzejewski-Szmek