Skip to content

Commit 3fae29b

Browse files
committed
InitSystem (Linux): add fast path for systemd version detection
1 parent a9373fb commit 3fae29b

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/detection/initsystem/initsystem_linux.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
#include "initsystem.h"
22
#include "common/processing.h"
3+
#include "util/linux/elf.h"
34
#include <unistd.h>
45

6+
static bool elfExtractStringsCallBack(const char* str, uint32_t len, void* data)
7+
{
8+
if (len > strlen("systemd 0.0 running in ") && memcmp(str, "systemd ", strlen("systemd ")) == 0)
9+
{
10+
const char* pend = memmem(str + strlen("systemd "), len - strlen("systemd "), " running in ", strlen(" running in "));
11+
if (pend)
12+
{
13+
ffStrbufSetNS((FFstrbuf*) data, (uint32_t) (pend - str) - (uint32_t) strlen("systemd "), str + strlen("systemd "));
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
520
const char* ffDetectInitSystem(FFInitSystemResult* result)
621
{
722
const char* error = ffProcessGetBasicInfoLinux((int) result->pid, &result->name, NULL, NULL);
@@ -33,18 +48,22 @@ const char* ffDetectInitSystem(FFInitSystemResult* result)
3348
{
3449
if (ffStrbufEqualS(&result->name, "systemd"))
3550
{
36-
if (ffProcessAppendStdOut(&result->version, (char* const[]) {
37-
ffStrbufEndsWithS(&result->exe, "/systemd") ? result->exe.chars : "systemctl", // use exe path in case users have another systemd installed
38-
"--version",
39-
NULL,
40-
}) == NULL && result->version.length)
51+
ffElfExtractStrings(result->exe.chars, elfExtractStringsCallBack, &result->version);
52+
if (result->version.length == 0)
4153
{
42-
uint32_t iStart = ffStrbufFirstIndexC(&result->version, '(');
43-
if (iStart < result->version.length)
54+
if (ffProcessAppendStdOut(&result->version, (char* const[]) {
55+
ffStrbufEndsWithS(&result->exe, "/systemd") ? result->exe.chars : "systemctl", // use exe path in case users have another systemd installed
56+
"--version",
57+
NULL,
58+
}) == NULL && result->version.length)
4459
{
45-
uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ')');
46-
ffStrbufSubstrBefore(&result->version, iEnd);
47-
ffStrbufSubstrAfter(&result->version, iStart);
60+
uint32_t iStart = ffStrbufFirstIndexC(&result->version, '(');
61+
if (iStart < result->version.length)
62+
{
63+
uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ')');
64+
ffStrbufSubstrBefore(&result->version, iEnd);
65+
ffStrbufSubstrAfter(&result->version, iStart);
66+
}
4867
}
4968
}
5069
}

0 commit comments

Comments
 (0)