Skip to content

Commit bb11cff

Browse files
committed
InitSystem: detect version
1 parent 90690ba commit bb11cff

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/detection/initsystem/initsystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
typedef struct FFInitSystemResult
66
{
7-
uint32_t pid;
87
FFstrbuf name;
98
FFstrbuf exe;
9+
FFstrbuf version;
10+
uint32_t pid;
1011
} FFInitSystemResult;
1112

1213
const char* ffDetectInitSystem(FFInitSystemResult* result);

src/detection/initsystem/initsystem_linux.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,51 @@ const char* ffDetectInitSystem(FFInitSystemResult* result)
1010

1111
ffProcessGetInfoLinux((int) result->pid, &result->name, &result->exe, &exeName, NULL);
1212

13+
if (ffStrbufEqualS(&result->name, "systemd"))
14+
{
15+
if (ffProcessAppendStdOut(&result->version, (char* const[]) {
16+
"systemctl",
17+
"--version",
18+
NULL,
19+
}) == NULL && result->version.length)
20+
{
21+
uint32_t iStart = ffStrbufFirstIndexC(&result->version, '(');
22+
if (iStart < result->version.length)
23+
{
24+
uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ')');
25+
ffStrbufSubstrBefore(&result->version, iEnd);
26+
ffStrbufSubstrAfter(&result->version, iStart);
27+
}
28+
else
29+
{
30+
iStart = ffStrbufFirstIndexC(&result->version, ' ');
31+
if (iStart < result->version.length)
32+
{
33+
uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ' ');
34+
ffStrbufSubstrBefore(&result->version, iEnd);
35+
ffStrbufSubstrAfter(&result->version, iStart);
36+
}
37+
}
38+
}
39+
}
40+
else if (ffStrbufEqualS(&result->name, "launchd"))
41+
{
42+
if (ffProcessAppendStdOut(&result->version, (char* const[]) {
43+
"launchctl",
44+
"version",
45+
NULL,
46+
}) == NULL && result->version.length)
47+
{
48+
uint32_t iStart = ffStrbufFirstIndexS(&result->version, "Version ");
49+
if (iStart < result->version.length)
50+
{
51+
iStart += strlen("Version");
52+
uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ':');
53+
ffStrbufSubstrBefore(&result->version, iEnd);
54+
ffStrbufSubstrAfter(&result->version, iStart);
55+
}
56+
}
57+
}
58+
1359
return NULL;
1460
}

src/modules/initsystem/initsystem.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
#include "modules/initsystem/initsystem.h"
55
#include "util/stringUtils.h"
66

7-
#define FF_INITSYSTEM_NUM_FORMAT_ARGS 3
7+
#define FF_INITSYSTEM_NUM_FORMAT_ARGS 4
88
#define FF_INITSYSTEM_DISPLAY_NAME "Init System"
99

1010
void ffPrintInitSystem(FFInitSystemOptions* options)
1111
{
1212
FFInitSystemResult result = {
13-
.exe = ffStrbufCreate(),
1413
.name = ffStrbufCreate(),
14+
.exe = ffStrbufCreate(),
15+
.version = ffStrbufCreate(),
1516
.pid = 1,
1617
};
1718

@@ -26,13 +27,18 @@ void ffPrintInitSystem(FFInitSystemOptions* options)
2627
if(options->moduleArgs.outputFormat.length == 0)
2728
{
2829
ffPrintLogoAndKey(FF_INITSYSTEM_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
29-
ffStrbufPutTo(&result.name, stdout);
30+
ffStrbufWriteTo(&result.name, stdout);
31+
if (result.version.length)
32+
printf(" (%s)\n", result.version.chars);
33+
else
34+
putchar('\n');
3035
}
3136
else
3237
{
3338
FF_PRINT_FORMAT_CHECKED(FF_INITSYSTEM_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_INITSYSTEM_NUM_FORMAT_ARGS, ((FFformatarg[]) {
3439
{FF_FORMAT_ARG_TYPE_STRBUF, &result.name, "name"},
3540
{FF_FORMAT_ARG_TYPE_STRBUF, &result.exe, "exe"},
41+
{FF_FORMAT_ARG_TYPE_STRBUF, &result.version, "version"},
3642
{FF_FORMAT_ARG_TYPE_UINT, &result.pid, "pid"},
3743
}));
3844
}
@@ -80,8 +86,9 @@ void ffGenerateInitSystemJsonConfig(FFInitSystemOptions* options, yyjson_mut_doc
8086
void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
8187
{
8288
FFInitSystemResult result = {
83-
.exe = ffStrbufCreate(),
8489
.name = ffStrbufCreate(),
90+
.exe = ffStrbufCreate(),
91+
.version = ffStrbufCreate(),
8592
.pid = 1,
8693
};
8794

@@ -96,6 +103,7 @@ void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options
96103
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
97104
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
98105
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
106+
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
99107
yyjson_mut_obj_add_uint(doc, obj, "pid", result.pid);
100108

101109
exit:
@@ -108,6 +116,7 @@ void ffPrintInitSystemHelpFormat(void)
108116
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_INITSYSTEM_DISPLAY_NAME, "{1}", FF_INITSYSTEM_NUM_FORMAT_ARGS, ((const char* []) {
109117
"init system name - name",
110118
"init system exe path - exe",
119+
"init system version path - version",
111120
"init system pid - pid",
112121
}));
113122
}
@@ -117,7 +126,7 @@ void ffInitInitSystemOptions(FFInitSystemOptions* options)
117126
ffOptionInitModuleBaseInfo(
118127
&options->moduleInfo,
119128
FF_INITSYSTEM_MODULE_NAME,
120-
"Print init system (pid 1) name",
129+
"Print init system (pid 1) name and version",
121130
ffParseInitSystemCommandOptions,
122131
ffParseInitSystemJsonObject,
123132
ffPrintInitSystem,

0 commit comments

Comments
 (0)