From 649f63d882d53533ae8b1c3b28967e772e738c45 Mon Sep 17 00:00:00 2001 From: Doug Freed Date: Sun, 17 Jan 2016 21:43:46 -0800 Subject: librc: move system detection code into rc_sys and use it This fixes an issue where librc code was calling code that only existed in the rc binary. This reverts commits 8addd79 and 9f6e056 This fixes #75. --- src/librc/librc-depend.c | 5 +-- src/librc/librc.c | 97 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 70 insertions(+), 32 deletions(-) (limited to 'src/librc') diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index 991a871f..d64b2b2a 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -740,7 +740,7 @@ rc_deptree_update(void) char *depend, *depends, *service, *type, *nosys, *onosys; size_t i, k, l; bool retval = true; - const char *sys = NULL; + const char *sys = rc_sys(); struct utsname uts; /* Some init scripts need RC_LIBEXECDIR to source stuff @@ -847,9 +847,6 @@ rc_deptree_update(void) /* Phase 2 - if we're a special system, remove services that don't * work for them. This doesn't stop them from being run directly. */ - sys = detect_container(); - if (!sys) - sys = detect_vm(); if (sys) { len = strlen(sys); nosys = xmalloc(len + 2); diff --git a/src/librc/librc.c b/src/librc/librc.c index 64498982..5cf4898f 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -198,13 +198,9 @@ found: #endif -const char * -rc_sys(void) +static const char * +get_systype(void) { -#ifdef PREFIX - return RC_SYS_PREFIX; -#endif - char *systype = rc_conf_value("rc_sys"); if (systype) { char *s = systype; @@ -215,7 +211,22 @@ rc_sys(void) s++; } } + return systype; +} + +static const char * +detect_prefix(const char *systype) +{ +#ifdef PREFIX + return RC_SYS_PREFIX; +#else + return NULL; +#endif +} +static const char * +detect_container(const char *systype) +{ #ifdef __FreeBSD__ if (systype && strcmp(systype, RC_SYS_JAIL) == 0) return RC_SYS_JAIL; @@ -227,25 +238,8 @@ rc_sys(void) return RC_SYS_JAIL; #endif -#ifdef __NetBSD__ - if (systype) { - if(strcmp(systype, RC_SYS_XEN0) == 0) - return RC_SYS_XEN0; - if (strcmp(systype, RC_SYS_XENU) == 0) - return RC_SYS_XENU; - } - if (exists("/kern/xen/privcmd")) - return RC_SYS_XEN0; - if (exists("/kern/xen")) - return RC_SYS_XENU; -#endif - #ifdef __linux__ if (systype) { - if (strcmp(systype, RC_SYS_XEN0) == 0) - return RC_SYS_XEN0; - if (strcmp(systype, RC_SYS_XENU) == 0) - return RC_SYS_XENU; if (strcmp(systype, RC_SYS_UML) == 0) return RC_SYS_UML; if (strcmp(systype, RC_SYS_VSERVER) == 0) @@ -261,11 +255,7 @@ rc_sys(void) if (strcmp(systype, RC_SYS_DOCKER) == 0) return RC_SYS_DOCKER; } - if (exists("/proc/xen")) { - if (file_regex("/proc/xen/capabilities", "control_d")) - return RC_SYS_XEN0; - return RC_SYS_XENU; - } else if (file_regex("/proc/cpuinfo", "UML")) + if (file_regex("/proc/cpuinfo", "UML")) return RC_SYS_UML; else if (file_regex("/proc/self/status", "(s_context|VxID):[[:space:]]*[1-9]")) @@ -287,6 +277,57 @@ rc_sys(void) return NULL; } + +static const char * +detect_vm(const char *systype) +{ +#ifdef __NetBSD__ + if (systype) { + if (strcmp(systype, RC_SYS_XEN0) == 0) + return RC_SYS_XEN0; + if (strcmp(systype, RC_SYS_XENU) == 0) + return RC_SYS_XENU; + } + if (exists("/kern/xen/privcmd")) + return RC_SYS_XEN0; + if (exists("/kern/xen")) + return RC_SYS_XENU; +#endif + +#ifdef __linux__ + if (systype) { + if (strcmp(systype, RC_SYS_XEN0) == 0) + return RC_SYS_XEN0; + if (strcmp(systype, RC_SYS_XENU) == 0) + return RC_SYS_XENU; + } + if (exists("/proc/xen")) { + if (file_regex("/proc/xen/capabilities", "control_d")) + return RC_SYS_XEN0; + return RC_SYS_XENU; + } +#endif + + return NULL; +} + +const char * +rc_sys(void) +{ + const char *systype; + const char *sys; + + systype = get_systype(); + sys = detect_prefix(systype); + if (!sys) { + sys = detect_container(systype); + if (!sys) { + sys = detect_vm(systype); + } + } + + return sys; +} librc_hidden_def(rc_sys) static const char * -- cgit v1.2.3