util-lib: move runlevel_to_target() to shared/unit-file

It if of course related to /proc/cmdline parsing, but is higher-level
functionality built on top of it. It should be in shared/ because it
is something to be used by pid1 and related utilities, not something for
level-level libraries.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2019-09-16 18:08:00 +02:00
parent fdb3decaa7
commit da33cba0f8
8 changed files with 69 additions and 64 deletions
+45
View File
@@ -6,6 +6,7 @@
#include "macro.h"
#include "path-lookup.h"
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
@@ -511,3 +512,47 @@ int unit_file_find_fragment(
// FIXME: if instance, consider any unit names with different template name
return 0;
}
static const char * const rlmap[] = {
"emergency", SPECIAL_EMERGENCY_TARGET,
"-b", SPECIAL_EMERGENCY_TARGET,
"rescue", SPECIAL_RESCUE_TARGET,
"single", SPECIAL_RESCUE_TARGET,
"-s", SPECIAL_RESCUE_TARGET,
"s", SPECIAL_RESCUE_TARGET,
"S", SPECIAL_RESCUE_TARGET,
"1", SPECIAL_RESCUE_TARGET,
"2", SPECIAL_MULTI_USER_TARGET,
"3", SPECIAL_MULTI_USER_TARGET,
"4", SPECIAL_MULTI_USER_TARGET,
"5", SPECIAL_GRAPHICAL_TARGET,
NULL
};
static const char * const rlmap_initrd[] = {
"emergency", SPECIAL_EMERGENCY_TARGET,
"rescue", SPECIAL_RESCUE_TARGET,
NULL
};
const char* runlevel_to_target(const char *word) {
const char * const *rlmap_ptr;
size_t i;
if (!word)
return NULL;
if (in_initrd()) {
word = startswith(word, "rd.");
if (!word)
return NULL;
}
rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
for (i = 0; rlmap_ptr[i]; i += 2)
if (streq(word, rlmap_ptr[i]))
return rlmap_ptr[i+1];
return NULL;
}
+2
View File
@@ -54,3 +54,5 @@ int unit_file_find_fragment(
const char *unit_name,
const char **ret_fragment_path,
Set **names);
const char* runlevel_to_target(const char *rl);