From bb11cff700ead85e724a58be86be495cd4107473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 29 May 2024 14:22:57 +0800 Subject: [PATCH] InitSystem: detect version --- src/detection/initsystem/initsystem.h | 3 +- src/detection/initsystem/initsystem_linux.c | 46 +++++++++++++++++++++ src/modules/initsystem/initsystem.c | 19 ++++++--- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/detection/initsystem/initsystem.h b/src/detection/initsystem/initsystem.h index 7c96fa123..07f79ff74 100644 --- a/src/detection/initsystem/initsystem.h +++ b/src/detection/initsystem/initsystem.h @@ -4,9 +4,10 @@ typedef struct FFInitSystemResult { - uint32_t pid; FFstrbuf name; FFstrbuf exe; + FFstrbuf version; + uint32_t pid; } FFInitSystemResult; const char* ffDetectInitSystem(FFInitSystemResult* result); diff --git a/src/detection/initsystem/initsystem_linux.c b/src/detection/initsystem/initsystem_linux.c index 661f8c6f8..ed1f80e2f 100644 --- a/src/detection/initsystem/initsystem_linux.c +++ b/src/detection/initsystem/initsystem_linux.c @@ -10,5 +10,51 @@ const char* ffDetectInitSystem(FFInitSystemResult* result) ffProcessGetInfoLinux((int) result->pid, &result->name, &result->exe, &exeName, NULL); + if (ffStrbufEqualS(&result->name, "systemd")) + { + if (ffProcessAppendStdOut(&result->version, (char* const[]) { + "systemctl", + "--version", + NULL, + }) == NULL && result->version.length) + { + uint32_t iStart = ffStrbufFirstIndexC(&result->version, '('); + if (iStart < result->version.length) + { + uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ')'); + ffStrbufSubstrBefore(&result->version, iEnd); + ffStrbufSubstrAfter(&result->version, iStart); + } + else + { + iStart = ffStrbufFirstIndexC(&result->version, ' '); + if (iStart < result->version.length) + { + uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ' '); + ffStrbufSubstrBefore(&result->version, iEnd); + ffStrbufSubstrAfter(&result->version, iStart); + } + } + } + } + else if (ffStrbufEqualS(&result->name, "launchd")) + { + if (ffProcessAppendStdOut(&result->version, (char* const[]) { + "launchctl", + "version", + NULL, + }) == NULL && result->version.length) + { + uint32_t iStart = ffStrbufFirstIndexS(&result->version, "Version "); + if (iStart < result->version.length) + { + iStart += strlen("Version"); + uint32_t iEnd = ffStrbufNextIndexC(&result->version, iStart + 1, ':'); + ffStrbufSubstrBefore(&result->version, iEnd); + ffStrbufSubstrAfter(&result->version, iStart); + } + } + } + return NULL; } diff --git a/src/modules/initsystem/initsystem.c b/src/modules/initsystem/initsystem.c index 7f378e2fd..f7e1101cb 100644 --- a/src/modules/initsystem/initsystem.c +++ b/src/modules/initsystem/initsystem.c @@ -4,14 +4,15 @@ #include "modules/initsystem/initsystem.h" #include "util/stringUtils.h" -#define FF_INITSYSTEM_NUM_FORMAT_ARGS 3 +#define FF_INITSYSTEM_NUM_FORMAT_ARGS 4 #define FF_INITSYSTEM_DISPLAY_NAME "Init System" void ffPrintInitSystem(FFInitSystemOptions* options) { FFInitSystemResult result = { - .exe = ffStrbufCreate(), .name = ffStrbufCreate(), + .exe = ffStrbufCreate(), + .version = ffStrbufCreate(), .pid = 1, }; @@ -26,13 +27,18 @@ void ffPrintInitSystem(FFInitSystemOptions* options) if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_INITSYSTEM_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - ffStrbufPutTo(&result.name, stdout); + ffStrbufWriteTo(&result.name, stdout); + if (result.version.length) + printf(" (%s)\n", result.version.chars); + else + putchar('\n'); } else { FF_PRINT_FORMAT_CHECKED(FF_INITSYSTEM_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_INITSYSTEM_NUM_FORMAT_ARGS, ((FFformatarg[]) { {FF_FORMAT_ARG_TYPE_STRBUF, &result.name, "name"}, {FF_FORMAT_ARG_TYPE_STRBUF, &result.exe, "exe"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.version, "version"}, {FF_FORMAT_ARG_TYPE_UINT, &result.pid, "pid"}, })); } @@ -80,8 +86,9 @@ void ffGenerateInitSystemJsonConfig(FFInitSystemOptions* options, yyjson_mut_doc void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFInitSystemResult result = { - .exe = ffStrbufCreate(), .name = ffStrbufCreate(), + .exe = ffStrbufCreate(), + .version = ffStrbufCreate(), .pid = 1, }; @@ -96,6 +103,7 @@ void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name); yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); yyjson_mut_obj_add_uint(doc, obj, "pid", result.pid); exit: @@ -108,6 +116,7 @@ void ffPrintInitSystemHelpFormat(void) FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_INITSYSTEM_DISPLAY_NAME, "{1}", FF_INITSYSTEM_NUM_FORMAT_ARGS, ((const char* []) { "init system name - name", "init system exe path - exe", + "init system version path - version", "init system pid - pid", })); } @@ -117,7 +126,7 @@ void ffInitInitSystemOptions(FFInitSystemOptions* options) ffOptionInitModuleBaseInfo( &options->moduleInfo, FF_INITSYSTEM_MODULE_NAME, - "Print init system (pid 1) name", + "Print init system (pid 1) name and version", ffParseInitSystemCommandOptions, ffParseInitSystemJsonObject, ffPrintInitSystem,