Merge pull request #13436 from systemd/hidden-units-are-good-units

Hidden units are good units
This commit is contained in:
Yu Watanabe 2019-08-31 02:53:54 +09:00 committed by GitHub
commit 6bc536fc94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 11 deletions

View file

@ -104,16 +104,24 @@
<citerefentry><refentrytitle>systemd.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
<para>Unit files are loaded from a set of paths determined during
compilation, described in the next section.</para>
<para>Unit files are loaded from a set of paths determined during compilation, described in the next
section.</para>
<para>Unit files can be parameterized by a single argument called the "instance name". The unit
is then constructed based on a "template file" which serves as the definition of multiple
services or other units. A template unit must have a single <literal>@</literal> at the end of
the name (right before the type suffix). The name of the full unit is formed by inserting the
instance name between <literal>@</literal> and the unit type suffix. In the unit file itself,
the instance parameter may be referred to using <literal>%i</literal> and other specifiers, see
below.</para>
<para>Valid unit names consist of a "name prefix" and a dot and a suffix specifying the unit type. The
"unit prefix" must consist of one or more valid characters (ASCII letters, digits, <literal>:</literal>,
<literal>-</literal>, <literal>_</literal>, <literal>.</literal>, and <literal>\</literal>). The total
length of the unit name including the suffix must not exceed 256 characters. The type suffix must be one
of <literal>.service</literal>, <literal>.socket</literal>, <literal>.device</literal>,
<literal>.mount</literal>, <literal>.automount</literal>, <literal>.swap</literal>,
<literal>.target</literal>, <literal>.path</literal>, <literal>.timer</literal>,
<literal>.slice</literal>, or <literal>.scope</literal>.</para>
<para>Units names can be parameterized by a single argument called the "instance name". The unit is then
constructed based on a "template file" which serves as the definition of multiple services or other
units. A template unit must have a single <literal>@</literal> at the end of the name (right before the
type suffix). The name of the full unit is formed by inserting the instance name between
<literal>@</literal> and the unit type suffix. In the unit file itself, the instance parameter may be
referred to using <literal>%i</literal> and other specifiers, see below.</para>
<para>Unit files may contain additional options on top of those
listed here. If systemd encounters an unknown option, it will

View file

@ -157,6 +157,18 @@ void strv_print(char **l);
_found; \
})
#define ENDSWITH_SET(p, ...) \
({ \
const char *_p = (p); \
char *_found = NULL, **_i; \
STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \
_found = endswith(_p, *_i); \
if (_found) \
break; \
} \
_found; \
})
#define FOREACH_STRING(x, y, ...) \
for (char **_l = STRV_MAKE(({ x = y; }), ##__VA_ARGS__); \
x; \

View file

@ -242,10 +242,19 @@ int unit_file_build_name_map(
if (!lookup_paths_mtime_exclude(lp, *dir))
mtime = MAX(mtime, timespec_load(&st.st_mtim));
FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to read \"%s\", ignoring: %m", *dir)) {
FOREACH_DIRENT_ALL(de, d, log_warning_errno(errno, "Failed to read \"%s\", ignoring: %m", *dir)) {
char *filename;
_cleanup_free_ char *_filename_free = NULL, *simplified = NULL;
const char *suffix, *dst = NULL;
bool valid_unit_name;
valid_unit_name = unit_name_is_valid(de->d_name, UNIT_NAME_ANY);
/* We only care about valid units and dirs with certain suffixes, let's ignore the
* rest. */
if (!valid_unit_name &&
!ENDSWITH_SET(de->d_name, ".wants", ".requires", ".d"))
continue;
filename = path_join(*dir, de->d_name);
if (!filename)
@ -260,7 +269,7 @@ int unit_file_build_name_map(
} else
_filename_free = filename; /* Make sure we free the filename. */
if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
if (!valid_unit_name)
continue;
assert_se(suffix = strrchr(de->d_name, '.'));

View file

@ -50,6 +50,9 @@ static void test_unit_name_is_valid(void) {
assert_se(!unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE));
assert_se( unit_name_is_valid("foo@.service", UNIT_NAME_TEMPLATE));
assert_se( unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE));
assert_se( unit_name_is_valid(".test.service", UNIT_NAME_PLAIN));
assert_se( unit_name_is_valid(".test@.service", UNIT_NAME_TEMPLATE));
assert_se( unit_name_is_valid("_strange::::.service", UNIT_NAME_ANY));
assert_se(!unit_name_is_valid(".service", UNIT_NAME_ANY));
assert_se(!unit_name_is_valid("", UNIT_NAME_ANY));