plugin_services: a launchd SIGKILL/SIGTERM reap is inactive, not failed (73 false failures on a real Mac)
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-12 08:41:26
Commit4e910c9a
Parents33f0b80a
Changed files
src/plugin_services.c+19−5■■■■■
tests/run.sh+12−0■■■■■
2 files+31−5
Diff
src/plugin_services.c+19−5■■■■■
index 0c8be54..73749cd 100644
--- a/src/plugin_services.c
+++ b/src/plugin_services.c
@@ -877,12 +877,26 @@ static void collect_launchd(void)
877877 str_trim(label);
878878 if (!label[0]) continue;
879879 if (strcmp(pid_s, "-") != 0) pid = atoi(pid_s);
880- /* A job with no pid whose last exit status is non-zero failed. */
881- if (pid <= 0 && strcmp(st_s, "0") != 0)
880+ /* launchd reports a job with no pid as "<last status>". A positive
881+ * value is a real exit code and means the job failed. A NEGATIVE
882+ * value is a signal: -9 (SIGKILL) and -15 (SIGTERM) are how launchd
883+ * reaps idle on-demand agents on memory pressure, so they are
884+ * "inactive", not failures - reporting 73 of those as failed would
885+ * make the board useless on a real Mac. Any other signal is a
886+ * crash and stays failed. */
887+ if (pid > 0) {
888+ svc_add(label, "launchd job", "running", "running", pid);
889+ } else if (st_s[0] == '-') {
890+ int sig = atoi(st_s + 1);
891+ if (sig == 9 || sig == 15)
892+ svc_add(label, "launchd job", "inactive", "exited", 0);
893+ else
894+ svc_add(label, "launchd job", "failed", "failed", 0);
895+ } else if (st_s[0] && strcmp(st_s, "0") != 0) {
882896 svc_add(label, "launchd job", "failed", "failed", 0);
883- else
884- svc_add(label, "launchd job", pid > 0 ? "running" : "inactive",
885- pid > 0 ? "running" : "exited", pid);
897+ } else {
898+ svc_add(label, "launchd job", "inactive", "exited", 0);
899+ }
886900 {
887901 svc_t *s = &g_svc[g_count - 1];
888902 str_copy(s->enabled, sizeof s->enabled, "launchd");
tests/run.sh+12−0■■■■■
index 98de868..66d2045 100644
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -249,12 +249,24 @@ PID Status Label
249249 - 0 com.apple.periodic-daily
250250 4242 0 com.example.agent
251251 - 78 com.example.broken
252+- -9 com.apple.idleagent
253+- -15 com.apple.termagent
254+- -11 com.example.segv
252255 LAUNCH
253256
254257 LL=$(body "$(run 'action=list' SERVICES_MOCK_LAUNCHCTL="$WORK/launchctl.txt" SERVICES_FORCE_INIT=launchd)")
255258 json_ok "launchctl output parses" "$LL" "d['ok'] is True"
256259 json_ok "launchd reports init_system=launchd when mocked through the platform path" "$LL" "d['init_system'] in ('launchd','none','systemd')"
257260 json_ok "the header line is not a service" "$LL" "all('Label' not in s['name'] for s in d['services'])"
261+json_ok "launchctl output parses every job" "$LL" "d['count']==7"
262+json_ok "a launchd job with a pid is running" "$LL" "[s for s in d['services'] if s['name']=='com.apple.securityd'][0]['active']=='running'"
263+json_ok "a launchd job with status 0 and no pid is inactive" "$LL" "[s for s in d['services'] if s['name']=='com.apple.periodic-daily'][0]['active']=='inactive'"
264+json_ok "a positive exit status is a failure" "$LL" "[s for s in d['services'] if s['name']=='com.example.broken'][0]['failed'] is True"
265+json_ok "the failure keeps its exit code in since" "$LL" "'78' in [s for s in d['services'] if s['name']=='com.example.broken'][0]['since']"
266+json_ok "SIGKILL (reaped idle agent) is inactive, not failed" "$LL" "[s for s in d['services'] if s['name']=='com.apple.idleagent'][0]['active']=='inactive'"
267+json_ok "SIGTERM (reaped idle agent) is inactive, not failed" "$LL" "[s for s in d['services'] if s['name']=='com.apple.termagent'][0]['active']=='inactive'"
268+json_ok "a real crash signal stays failed" "$LL" "[s for s in d['services'] if s['name']=='com.example.segv'][0]['failed'] is True"
269+json_ok "the launchd summary counts one real failure" "$LL" "d['summary']['failed']==2"
258270
259271 # ---------------------------------------------------------------- show
260272 section "mode 2e - action=show"
261273
src/plugin_services.c+19−5■■■■■
index 0c8be54..73749cd 100644index 0c8be54..73749cd 100644
--- a/src/plugin_services.c--- a/src/plugin_services.c
+++ b/src/plugin_services.c+++ b/src/plugin_services.c
@@ -877,12 +877,26 @@ static void collect_launchd(void)@@ -877,12 +877,26 @@ static void collect_launchd(void)
877 str_trim(label);877 str_trim(label);
878 if (!label[0]) continue;878 if (!label[0]) continue;
879 if (strcmp(pid_s, "-") != 0) pid = atoi(pid_s);879 if (strcmp(pid_s, "-") != 0) pid = atoi(pid_s);
880- /* A job with no pid whose last exit status is non-zero failed. */880+ /* launchd reports a job with no pid as "<last status>". A positive
881- if (pid <= 0 && strcmp(st_s, "0") != 0)881+ * value is a real exit code and means the job failed. A NEGATIVE
882+ * value is a signal: -9 (SIGKILL) and -15 (SIGTERM) are how launchd
883+ * reaps idle on-demand agents on memory pressure, so they are
884+ * "inactive", not failures - reporting 73 of those as failed would
885+ * make the board useless on a real Mac. Any other signal is a
886+ * crash and stays failed. */
887+ if (pid > 0) {
888+ svc_add(label, "launchd job", "running", "running", pid);
889+ } else if (st_s[0] == '-') {
890+ int sig = atoi(st_s + 1);
891+ if (sig == 9 || sig == 15)
892+ svc_add(label, "launchd job", "inactive", "exited", 0);
893+ else
894+ svc_add(label, "launchd job", "failed", "failed", 0);
895+ } else if (st_s[0] && strcmp(st_s, "0") != 0) {
882 svc_add(label, "launchd job", "failed", "failed", 0);896 svc_add(label, "launchd job", "failed", "failed", 0);
883- else897+ } else {
884- svc_add(label, "launchd job", pid > 0 ? "running" : "inactive",898+ svc_add(label, "launchd job", "inactive", "exited", 0);
885- pid > 0 ? "running" : "exited", pid);899+ }
886 {900 {
887 svc_t *s = &g_svc[g_count - 1];901 svc_t *s = &g_svc[g_count - 1];
888 str_copy(s->enabled, sizeof s->enabled, "launchd");902 str_copy(s->enabled, sizeof s->enabled, "launchd");
tests/run.sh+12−0■■■■■
index 98de868..66d2045 100644index 98de868..66d2045 100644
--- a/tests/run.sh--- a/tests/run.sh
+++ b/tests/run.sh+++ b/tests/run.sh
@@ -249,12 +249,24 @@ PID Status Label@@ -249,12 +249,24 @@ PID Status Label
249 - 0 com.apple.periodic-daily249 - 0 com.apple.periodic-daily
250 4242 0 com.example.agent250 4242 0 com.example.agent
251 - 78 com.example.broken251 - 78 com.example.broken
252+- -9 com.apple.idleagent
253+- -15 com.apple.termagent
254+- -11 com.example.segv
252 LAUNCH255 LAUNCH
253 256
254 LL=$(body "$(run 'action=list' SERVICES_MOCK_LAUNCHCTL="$WORK/launchctl.txt" SERVICES_FORCE_INIT=launchd)")257 LL=$(body "$(run 'action=list' SERVICES_MOCK_LAUNCHCTL="$WORK/launchctl.txt" SERVICES_FORCE_INIT=launchd)")
255 json_ok "launchctl output parses" "$LL" "d['ok'] is True"258 json_ok "launchctl output parses" "$LL" "d['ok'] is True"
256 json_ok "launchd reports init_system=launchd when mocked through the platform path" "$LL" "d['init_system'] in ('launchd','none','systemd')"259 json_ok "launchd reports init_system=launchd when mocked through the platform path" "$LL" "d['init_system'] in ('launchd','none','systemd')"
257 json_ok "the header line is not a service" "$LL" "all('Label' not in s['name'] for s in d['services'])"260 json_ok "the header line is not a service" "$LL" "all('Label' not in s['name'] for s in d['services'])"
261+json_ok "launchctl output parses every job" "$LL" "d['count']==7"
262+json_ok "a launchd job with a pid is running" "$LL" "[s for s in d['services'] if s['name']=='com.apple.securityd'][0]['active']=='running'"
263+json_ok "a launchd job with status 0 and no pid is inactive" "$LL" "[s for s in d['services'] if s['name']=='com.apple.periodic-daily'][0]['active']=='inactive'"
264+json_ok "a positive exit status is a failure" "$LL" "[s for s in d['services'] if s['name']=='com.example.broken'][0]['failed'] is True"
265+json_ok "the failure keeps its exit code in since" "$LL" "'78' in [s for s in d['services'] if s['name']=='com.example.broken'][0]['since']"
266+json_ok "SIGKILL (reaped idle agent) is inactive, not failed" "$LL" "[s for s in d['services'] if s['name']=='com.apple.idleagent'][0]['active']=='inactive'"
267+json_ok "SIGTERM (reaped idle agent) is inactive, not failed" "$LL" "[s for s in d['services'] if s['name']=='com.apple.termagent'][0]['active']=='inactive'"
268+json_ok "a real crash signal stays failed" "$LL" "[s for s in d['services'] if s['name']=='com.example.segv'][0]['failed'] is True"
269+json_ok "the launchd summary counts one real failure" "$LL" "d['summary']['failed']==2"
258 270
259 # ---------------------------------------------------------------- show271 # ---------------------------------------------------------------- show
260 section "mode 2e - action=show"272 section "mode 2e - action=show"
261273