mirror of
https://github.com/systemd/systemd.git
synced 2025-03-09 12:58:26 +03:00
Move systemd_installation_has_version() to src/nspawn/
This function implements a heuristic that is only used by nspawn. It doesn't belong in basic. I opted for a new file "nspawn-utils.c", because it seems likely that we'll need some other new utilities like that in the future. No functional change.
This commit is contained in:
parent
be6447b483
commit
c9394f4f93
@ -17,11 +17,8 @@
|
||||
#include "extract-word.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "glob-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "stat-util.h"
|
||||
#include "string-util.h"
|
||||
@ -1314,74 +1311,6 @@ bool valid_device_allow_pattern(const char *path) {
|
||||
return valid_device_node_path(path);
|
||||
}
|
||||
|
||||
int systemd_installation_has_version(const char *root, unsigned minimal_version) {
|
||||
const char *pattern;
|
||||
int r;
|
||||
|
||||
/* Try to guess if systemd installation is later than the specified version. This
|
||||
* is hacky and likely to yield false negatives, particularly if the installation
|
||||
* is non-standard. False positives should be relatively rare.
|
||||
*/
|
||||
|
||||
NULSTR_FOREACH(pattern,
|
||||
/* /lib works for systems without usr-merge, and for systems with a sane
|
||||
* usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
|
||||
* for Gentoo which does a merge without making /lib a symlink.
|
||||
*/
|
||||
"lib/systemd/libsystemd-shared-*.so\0"
|
||||
"lib64/systemd/libsystemd-shared-*.so\0"
|
||||
"usr/lib/systemd/libsystemd-shared-*.so\0"
|
||||
"usr/lib64/systemd/libsystemd-shared-*.so\0") {
|
||||
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
char *c;
|
||||
|
||||
path = path_join(root, pattern);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
r = glob_extend(&names, path, 0);
|
||||
if (r == -ENOENT)
|
||||
continue;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert_se(c = endswith(path, "*.so"));
|
||||
*c = '\0'; /* truncate the glob part */
|
||||
|
||||
STRV_FOREACH(name, names) {
|
||||
/* This is most likely to run only once, hence let's not optimize anything. */
|
||||
char *t, *t2;
|
||||
unsigned version;
|
||||
|
||||
t = startswith(*name, path);
|
||||
if (!t)
|
||||
continue;
|
||||
|
||||
t2 = endswith(t, ".so");
|
||||
if (!t2)
|
||||
continue;
|
||||
|
||||
t2[0] = '\0'; /* truncate the suffix */
|
||||
|
||||
r = safe_atou(t, &version);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name);
|
||||
continue;
|
||||
}
|
||||
|
||||
log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
|
||||
*name, version,
|
||||
version >= minimal_version ? "OK" : "too old");
|
||||
if (version >= minimal_version)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool dot_or_dot_dot(const char *path) {
|
||||
if (!path)
|
||||
return false;
|
||||
|
@ -181,8 +181,6 @@ bool is_device_path(const char *path);
|
||||
bool valid_device_node_path(const char *path);
|
||||
bool valid_device_allow_pattern(const char *path);
|
||||
|
||||
int systemd_installation_has_version(const char *root, unsigned minimal_version);
|
||||
|
||||
bool dot_or_dot_dot(const char *path);
|
||||
|
||||
static inline const char *skip_dev_prefix(const char *p) {
|
||||
|
@ -28,6 +28,8 @@ libnspawn_core_sources = files(
|
||||
'nspawn-setuid.h',
|
||||
'nspawn-stub-pid1.c',
|
||||
'nspawn-stub-pid1.h',
|
||||
'nspawn-util.c',
|
||||
'nspawn-util.h',
|
||||
'nspawn.h',
|
||||
)
|
||||
|
||||
@ -58,6 +60,11 @@ tests += [
|
||||
libshared],
|
||||
[libseccomp]],
|
||||
|
||||
[files('test-nspawn-util.c'),
|
||||
[libnspawn_core,
|
||||
libshared],
|
||||
[libseccomp]],
|
||||
|
||||
[files('test-patch-uid.c'),
|
||||
[libnspawn_core,
|
||||
libshared],
|
||||
|
78
src/nspawn/nspawn-util.c
Normal file
78
src/nspawn/nspawn-util.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "glob-util.h"
|
||||
#include "log.h"
|
||||
#include "nspawn-util.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "string-util.h"
|
||||
|
||||
int systemd_installation_has_version(const char *root, unsigned minimal_version) {
|
||||
const char *pattern;
|
||||
int r;
|
||||
|
||||
/* Try to guess if systemd installation is later than the specified version. This
|
||||
* is hacky and likely to yield false negatives, particularly if the installation
|
||||
* is non-standard. False positives should be relatively rare.
|
||||
*/
|
||||
|
||||
NULSTR_FOREACH(pattern,
|
||||
/* /lib works for systems without usr-merge, and for systems with a sane
|
||||
* usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
|
||||
* for Gentoo which does a merge without making /lib a symlink.
|
||||
*/
|
||||
"lib/systemd/libsystemd-shared-*.so\0"
|
||||
"lib64/systemd/libsystemd-shared-*.so\0"
|
||||
"usr/lib/systemd/libsystemd-shared-*.so\0"
|
||||
"usr/lib64/systemd/libsystemd-shared-*.so\0") {
|
||||
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
char *c;
|
||||
|
||||
path = path_join(root, pattern);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
r = glob_extend(&names, path, 0);
|
||||
if (r == -ENOENT)
|
||||
continue;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert_se(c = endswith(path, "*.so"));
|
||||
*c = '\0'; /* truncate the glob part */
|
||||
|
||||
STRV_FOREACH(name, names) {
|
||||
/* This is most likely to run only once, hence let's not optimize anything. */
|
||||
char *t, *t2;
|
||||
unsigned version;
|
||||
|
||||
t = startswith(*name, path);
|
||||
if (!t)
|
||||
continue;
|
||||
|
||||
t2 = endswith(t, ".so");
|
||||
if (!t2)
|
||||
continue;
|
||||
|
||||
t2[0] = '\0'; /* truncate the suffix */
|
||||
|
||||
r = safe_atou(t, &version);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name);
|
||||
continue;
|
||||
}
|
||||
|
||||
log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
|
||||
*name, version,
|
||||
version >= minimal_version ? "OK" : "too old");
|
||||
if (version >= minimal_version)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
4
src/nspawn/nspawn-util.h
Normal file
4
src/nspawn/nspawn-util.h
Normal file
@ -0,0 +1,4 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
int systemd_installation_has_version(const char *root, unsigned minimal_version);
|
@ -78,13 +78,13 @@
|
||||
#include "nspawn-settings.h"
|
||||
#include "nspawn-setuid.h"
|
||||
#include "nspawn-stub-pid1.h"
|
||||
#include "nspawn-util.h"
|
||||
#include "nspawn.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "os-util.h"
|
||||
#include "pager.h"
|
||||
#include "parse-argument.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "pretty-print.h"
|
||||
#include "process-util.h"
|
||||
#include "ptyfwd.h"
|
||||
|
19
src/nspawn/test-nspawn-util.c
Normal file
19
src/nspawn/test-nspawn-util.c
Normal file
@ -0,0 +1,19 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "nspawn-util.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST(systemd_installation_has_version) {
|
||||
static const unsigned versions[] = {0, 231, PROJECT_VERSION, 999};
|
||||
int r;
|
||||
|
||||
for (size_t i = 0; i < ELEMENTSOF(versions); i++) {
|
||||
r = systemd_installation_has_version(saved_argv[1], versions[i]);
|
||||
assert_se(r >= 0);
|
||||
log_info("%s has systemd >= %u: %s",
|
||||
saved_argv[1] ?: "Current installation", versions[i], yes_no(r));
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
@ -960,21 +960,6 @@ TEST(hidden_or_backup_file) {
|
||||
assert_se(!hidden_or_backup_file("test.dpkg-old.foo"));
|
||||
}
|
||||
|
||||
TEST(systemd_installation_has_version) {
|
||||
int r;
|
||||
const unsigned versions[] = {0, 231, PROJECT_VERSION, 999};
|
||||
unsigned i;
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(versions); i++) {
|
||||
r = systemd_installation_has_version(saved_argv[1], versions[i]);
|
||||
assert_se(r >= 0);
|
||||
log_info("%s has systemd >= %u: %s",
|
||||
saved_argv[1] ?: "Current installation", versions[i], yes_no(r));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(skip_dev_prefix) {
|
||||
assert_se(streq(skip_dev_prefix("/"), "/"));
|
||||
assert_se(streq(skip_dev_prefix("/dev"), ""));
|
||||
|
Loading…
x
Reference in New Issue
Block a user