|
1 | 1 | #include "initsystem.h" |
2 | 2 | #include "common/processing.h" |
| 3 | +#include "util/linux/elf.h" |
3 | 4 | #include <unistd.h> |
4 | 5 |
|
| 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 | + |
5 | 20 | const char* ffDetectInitSystem(FFInitSystemResult* result) |
6 | 21 | { |
7 | 22 | const char* error = ffProcessGetBasicInfoLinux((int) result->pid, &result->name, NULL, NULL); |
@@ -33,18 +48,22 @@ const char* ffDetectInitSystem(FFInitSystemResult* result) |
33 | 48 | { |
34 | 49 | if (ffStrbufEqualS(&result->name, "systemd")) |
35 | 50 | { |
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) |
41 | 53 | { |
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) |
44 | 59 | { |
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 | + } |
48 | 67 | } |
49 | 68 | } |
50 | 69 | } |
|
0 commit comments