plugin_services: split the POSIX file reader from the Linux parser region
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-12 08:40:35
Commit33f0b80a
Parentsb5c52c51
Changed files
src/plugin_services.c+55−54■■■■■
1 file+55−54
Diff
src/plugin_services.c+55−54■■■■■
index 7451e97..0c8be54 100644
--- a/src/plugin_services.c
+++ b/src/plugin_services.c
@@ -388,10 +388,9 @@ static svc_t *svc_find(const char *name)
388388 /* ------------------------------------------------------------------ */
389389 /* Minimal JSON field extraction (for systemctl --output=json output) */
390390 /* ------------------------------------------------------------------ */
391-/* The JSON readers and the text parsers are POSIX-only: the Windows slice
392- * talks to the SCM directly and never parses text. The systemd half is Linux
393- * only, because macOS has launchd and needs none of it. */
394-#if !defined(_WIN32)
391+/* The JSON readers and the systemd text parsers are Linux-only: macOS has
392+ * launchd and the Windows slice talks to the SCM, so neither parses text. */
393+#if defined(__linux__)
395394
396395 static int json_read_string_at(const char **pp, char *out, size_t outsz)
397396 {
@@ -486,52 +485,6 @@ static int json_field(const char *obj, const char *end, const char *key,
486485 /* Collection: systemd (Linux) */
487486 /* ------------------------------------------------------------------ */
488487
489-static char *read_file_all(const char *path, size_t *len)
490-{
491- FILE *f;
492- char *buf;
493- long sz;
494- size_t n;
495-
496- f = fopen(path, "rb");
497- if (!f) return NULL;
498- sz = 0;
499- /* /proc files report st_size 0 and are not seekable, so only trust the
500- * size when the seek actually works; otherwise read until EOF. */
501- if (fseek(f, 0, SEEK_END) == 0) {
502- sz = ftell(f);
503- if (sz < 0) sz = 0;
504- if (sz > (long)(8 * 1024 * 1024)) { fclose(f); return NULL; }
505- rewind(f);
506- }
507- buf = (char *)malloc((size_t)(sz > 0 ? sz : 4096) + 1);
508- if (!buf) { fclose(f); return NULL; }
509- if (sz > 0) {
510- n = fread(buf, 1, (size_t)sz, f);
511- } else {
512- size_t cap = 4096;
513- n = 0;
514- for (;;) {
515- size_t got;
516- if (n == cap) {
517- char *np;
518- cap *= 2;
519- if (cap > 8 * 1024 * 1024) break;
520- np = (char *)realloc(buf, cap + 1);
521- if (!np) break;
522- buf = np;
523- }
524- got = fread(buf + n, 1, cap - n, f);
525- if (got == 0) break;
526- n += got;
527- }
528- }
529- buf[n] = '\0';
530- fclose(f);
531- if (len) *len = n;
532- return buf;
533-}
534-
535488 /* Parse the systemctl --output=json array into g_svc. */
536489 static void parse_json_units(const char *json)
537490 {
@@ -679,8 +632,57 @@ static void apply_show(const char *text)
679632 }
680633 }
681634
635+#endif /* __linux__ */
636+
637+#if !defined(_WIN32)
638+static char *read_file_all(const char *path, size_t *len)
639+{
640+ FILE *f;
641+ char *buf;
642+ long sz;
643+ size_t n;
644+
645+ f = fopen(path, "rb");
646+ if (!f) return NULL;
647+ sz = 0;
648+ /* /proc files report st_size 0 and are not seekable, so only trust the
649+ * size when the seek actually works; otherwise read until EOF. */
650+ if (fseek(f, 0, SEEK_END) == 0) {
651+ sz = ftell(f);
652+ if (sz < 0) sz = 0;
653+ if (sz > (long)(8 * 1024 * 1024)) { fclose(f); return NULL; }
654+ rewind(f);
655+ }
656+ buf = (char *)malloc((size_t)(sz > 0 ? sz : 4096) + 1);
657+ if (!buf) { fclose(f); return NULL; }
658+ if (sz > 0) {
659+ n = fread(buf, 1, (size_t)sz, f);
660+ } else {
661+ size_t cap = 4096;
662+ n = 0;
663+ for (;;) {
664+ size_t got;
665+ if (n == cap) {
666+ char *np;
667+ cap *= 2;
668+ if (cap > 8 * 1024 * 1024) break;
669+ np = (char *)realloc(buf, cap + 1);
670+ if (!np) break;
671+ buf = np;
672+ }
673+ got = fread(buf + n, 1, cap - n, f);
674+ if (got == 0) break;
675+ n += got;
676+ }
677+ }
678+ buf[n] = '\0';
679+ fclose(f);
680+ if (len) *len = n;
681+ return buf;
682+}
683+
682684 /* Containers with no init system: list /proc/<pid> long-running daemons. */
683-#if !defined(__APPLE__)
685+#if defined(__linux__)
684686 static int collect_proc_daemons(void)
685687 {
686688 const char *root = getenv("SERVICES_MOCK_PROC_DIR");
@@ -827,9 +829,8 @@ static void collect_systemd(void)
827829 }
828830 }
829831
830-#endif /* !_WIN32 || !__APPLE__ */
831-
832-#endif /* !__APPLE__ */
832+#endif /* __linux__ */
833+#endif /* !_WIN32 */
833834
834835 /* ------------------------------------------------------------------ */
835836 /* Collection: launchd (macOS) */
836837
src/plugin_services.c+55−54■■■■■
index 7451e97..0c8be54 100644index 7451e97..0c8be54 100644
--- a/src/plugin_services.c--- a/src/plugin_services.c
+++ b/src/plugin_services.c+++ b/src/plugin_services.c
@@ -388,10 +388,9 @@ static svc_t *svc_find(const char *name)@@ -388,10 +388,9 @@ static svc_t *svc_find(const char *name)
388 /* ------------------------------------------------------------------ */388 /* ------------------------------------------------------------------ */
389 /* Minimal JSON field extraction (for systemctl --output=json output) */389 /* Minimal JSON field extraction (for systemctl --output=json output) */
390 /* ------------------------------------------------------------------ */390 /* ------------------------------------------------------------------ */
391-/* The JSON readers and the text parsers are POSIX-only: the Windows slice391+/* The JSON readers and the systemd text parsers are Linux-only: macOS has
392- * talks to the SCM directly and never parses text. The systemd half is Linux392+ * launchd and the Windows slice talks to the SCM, so neither parses text. */
393- * only, because macOS has launchd and needs none of it. */393+#if defined(__linux__)
394-#if !defined(_WIN32)
395 394
396 static int json_read_string_at(const char **pp, char *out, size_t outsz)395 static int json_read_string_at(const char **pp, char *out, size_t outsz)
397 {396 {
@@ -486,52 +485,6 @@ static int json_field(const char *obj, const char *end, const char *key,@@ -486,52 +485,6 @@ static int json_field(const char *obj, const char *end, const char *key,
486 /* Collection: systemd (Linux) */485 /* Collection: systemd (Linux) */
487 /* ------------------------------------------------------------------ */486 /* ------------------------------------------------------------------ */
488 487
489-static char *read_file_all(const char *path, size_t *len)
490-{
491- FILE *f;
492- char *buf;
493- long sz;
494- size_t n;
495-
496- f = fopen(path, "rb");
497- if (!f) return NULL;
498- sz = 0;
499- /* /proc files report st_size 0 and are not seekable, so only trust the
500- * size when the seek actually works; otherwise read until EOF. */
501- if (fseek(f, 0, SEEK_END) == 0) {
502- sz = ftell(f);
503- if (sz < 0) sz = 0;
504- if (sz > (long)(8 * 1024 * 1024)) { fclose(f); return NULL; }
505- rewind(f);
506- }
507- buf = (char *)malloc((size_t)(sz > 0 ? sz : 4096) + 1);
508- if (!buf) { fclose(f); return NULL; }
509- if (sz > 0) {
510- n = fread(buf, 1, (size_t)sz, f);
511- } else {
512- size_t cap = 4096;
513- n = 0;
514- for (;;) {
515- size_t got;
516- if (n == cap) {
517- char *np;
518- cap *= 2;
519- if (cap > 8 * 1024 * 1024) break;
520- np = (char *)realloc(buf, cap + 1);
521- if (!np) break;
522- buf = np;
523- }
524- got = fread(buf + n, 1, cap - n, f);
525- if (got == 0) break;
526- n += got;
527- }
528- }
529- buf[n] = '\0';
530- fclose(f);
531- if (len) *len = n;
532- return buf;
533-}
534-
535 /* Parse the systemctl --output=json array into g_svc. */488 /* Parse the systemctl --output=json array into g_svc. */
536 static void parse_json_units(const char *json)489 static void parse_json_units(const char *json)
537 {490 {
@@ -679,8 +632,57 @@ static void apply_show(const char *text)@@ -679,8 +632,57 @@ static void apply_show(const char *text)
679 }632 }
680 }633 }
681 634
635+#endif /* __linux__ */
636+
637+#if !defined(_WIN32)
638+static char *read_file_all(const char *path, size_t *len)
639+{
640+ FILE *f;
641+ char *buf;
642+ long sz;
643+ size_t n;
644+
645+ f = fopen(path, "rb");
646+ if (!f) return NULL;
647+ sz = 0;
648+ /* /proc files report st_size 0 and are not seekable, so only trust the
649+ * size when the seek actually works; otherwise read until EOF. */
650+ if (fseek(f, 0, SEEK_END) == 0) {
651+ sz = ftell(f);
652+ if (sz < 0) sz = 0;
653+ if (sz > (long)(8 * 1024 * 1024)) { fclose(f); return NULL; }
654+ rewind(f);
655+ }
656+ buf = (char *)malloc((size_t)(sz > 0 ? sz : 4096) + 1);
657+ if (!buf) { fclose(f); return NULL; }
658+ if (sz > 0) {
659+ n = fread(buf, 1, (size_t)sz, f);
660+ } else {
661+ size_t cap = 4096;
662+ n = 0;
663+ for (;;) {
664+ size_t got;
665+ if (n == cap) {
666+ char *np;
667+ cap *= 2;
668+ if (cap > 8 * 1024 * 1024) break;
669+ np = (char *)realloc(buf, cap + 1);
670+ if (!np) break;
671+ buf = np;
672+ }
673+ got = fread(buf + n, 1, cap - n, f);
674+ if (got == 0) break;
675+ n += got;
676+ }
677+ }
678+ buf[n] = '\0';
679+ fclose(f);
680+ if (len) *len = n;
681+ return buf;
682+}
683+
682 /* Containers with no init system: list /proc/<pid> long-running daemons. */684 /* Containers with no init system: list /proc/<pid> long-running daemons. */
683-#if !defined(__APPLE__)685+#if defined(__linux__)
684 static int collect_proc_daemons(void)686 static int collect_proc_daemons(void)
685 {687 {
686 const char *root = getenv("SERVICES_MOCK_PROC_DIR");688 const char *root = getenv("SERVICES_MOCK_PROC_DIR");
@@ -827,9 +829,8 @@ static void collect_systemd(void)@@ -827,9 +829,8 @@ static void collect_systemd(void)
827 }829 }
828 }830 }
829 831
830-#endif /* !_WIN32 || !__APPLE__ */832+#endif /* __linux__ */
831-833+#endif /* !_WIN32 */
832-#endif /* !__APPLE__ */
833 834
834 /* ------------------------------------------------------------------ */835 /* ------------------------------------------------------------------ */
835 /* Collection: launchd (macOS) */836 /* Collection: launchd (macOS) */
836837