All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Fix a readlink() typo in Psystem_daemon()
@ 2026-11-19  7:11 ` Eslam Almorshdy via B4 Relay
  0 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

Psystem_daemon() tests readlink() == 0, but readlink() returns the
number of bytes stored or -1, so the "regular file outside /var" rule
never applied: every root process without a terminal was a system
daemon wherever systemd doesn't decide.

Patch 1 is the one-character fix.  Patch 2 adds a test that hides
/run/systemd with unshare, so the rule is exercised on systemd hosts
too.  On systems without systemd Patch 1 also turns pid/tst.emptystack,
tst.float, tst.manypids and tst.probemod green.

Changes since v1:
- Patch 2: moved the /var guard up and used [[ ]] (Kris).

---
Eslam Almorshdy (2):
      libproc: fix readlink() typo in Psystem_daemon()
      test: add test for system daemons with regular files as streams

 libproc/Pcontrol.c                            |  2 +-
 test/unittest/proc/tst.system-daemon-files.r  |  2 +
 test/unittest/proc/tst.system-daemon-files.sh | 95 +++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 1 deletion(-)
---
base-commit: 61fea621160ea4b6302874c62a723da105b44883
change-id: 20261118-b4-bug008-daemon-readlink-67e33013fbc5

Best regards,
--  
Eslam Almorshdy <ealmorshdy@d-matrix.ai>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 0/2] Fix a readlink() typo in Psystem_daemon()
@ 2026-11-19  7:11 ` Eslam Almorshdy via B4 Relay
  0 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy via B4 Relay @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

Psystem_daemon() tests readlink() == 0, but readlink() returns the
number of bytes stored or -1, so the "regular file outside /var" rule
never applied: every root process without a terminal was a system
daemon wherever systemd doesn't decide.

Patch 1 is the one-character fix.  Patch 2 adds a test that hides
/run/systemd with unshare, so the rule is exercised on systemd hosts
too.  On systems without systemd Patch 1 also turns pid/tst.emptystack,
tst.float, tst.manypids and tst.probemod green.

Changes since v1:
- Patch 2: moved the /var guard up and used [[ ]] (Kris).

---
Eslam Almorshdy (2):
      libproc: fix readlink() typo in Psystem_daemon()
      test: add test for system daemons with regular files as streams

 libproc/Pcontrol.c                            |  2 +-
 test/unittest/proc/tst.system-daemon-files.r  |  2 +
 test/unittest/proc/tst.system-daemon-files.sh | 95 +++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 1 deletion(-)
---
base-commit: 61fea621160ea4b6302874c62a723da105b44883
change-id: 20261118-b4-bug008-daemon-readlink-67e33013fbc5

Best regards,
--  
Eslam Almorshdy <ealmorshdy@d-matrix.ai>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/2] libproc: fix readlink() typo in Psystem_daemon()
  2026-11-19  7:11 ` Eslam Almorshdy via B4 Relay
@ 2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  -1 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

readlink() returns the number of bytes it stored or -1, never 0, so
the check for a regular file outside /var never ran.

Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>
Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>
---
 libproc/Pcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libproc/Pcontrol.c b/libproc/Pcontrol.c
index f8440d5b..ece074e0 100644
--- a/libproc/Pcontrol.c
+++ b/libproc/Pcontrol.c
@@ -3045,7 +3045,7 @@ Psystem_daemon(pid_t pid, uid_t useruid, const char *sysslice)
 		 */
 		memset(buf, '\0', sizeof(buf));
 		if (S_ISREG(s.st_mode) &&
-		    (readlink(procname, buf, sizeof(buf)-1) == 0)) {
+		    (readlink(procname, buf, sizeof(buf)-1) > 0)) {
 			if((buf[0] == '/') && (strcmp(buf, "/var/") != 0))
 				return 0;
 		}

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 1/2] libproc: fix readlink() typo in Psystem_daemon()
@ 2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  0 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy via B4 Relay @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

From: Eslam Almorshdy <ealmorshdy@d-matrix.ai>

readlink() returns the number of bytes it stored or -1, never 0, so
the check for a regular file outside /var never ran.

Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>
Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>
---
 libproc/Pcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libproc/Pcontrol.c b/libproc/Pcontrol.c
index f8440d5b..ece074e0 100644
--- a/libproc/Pcontrol.c
+++ b/libproc/Pcontrol.c
@@ -3045,7 +3045,7 @@ Psystem_daemon(pid_t pid, uid_t useruid, const char *sysslice)
 		 */
 		memset(buf, '\0', sizeof(buf));
 		if (S_ISREG(s.st_mode) &&
-		    (readlink(procname, buf, sizeof(buf)-1) == 0)) {
+		    (readlink(procname, buf, sizeof(buf)-1) > 0)) {
 			if((buf[0] == '/') && (strcmp(buf, "/var/") != 0))
 				return 0;
 		}

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] test: add test for system daemons with regular files as streams
  2026-11-19  7:11 ` Eslam Almorshdy via B4 Relay
@ 2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  -1 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

Start a root process without a terminal with its stdout to a regular
file outside /var, then to one under /var, and check how libproc
classifies it from DTRACE_DEBUG: not a system daemon, then a system
daemon.

libproc asks systemd whenever /run/systemd/system exists, and for a
process in a systemd slice the slice decides.  So on a systemd system
the test runs dtrace under unshare -m, in a mount namespace of its own
with a tmpfs over /run/systemd: libproc does not find systemd and uses
the rules under test.

The first case fails without the previous patch.

Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>
---
 test/unittest/proc/tst.system-daemon-files.r  |  2 +
 test/unittest/proc/tst.system-daemon-files.sh | 95 +++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)

diff --git a/test/unittest/proc/tst.system-daemon-files.r b/test/unittest/proc/tst.system-daemon-files.r
new file mode 100644
index 00000000..0a2b3945
--- /dev/null
+++ b/test/unittest/proc/tst.system-daemon-files.r
@@ -0,0 +1,2 @@
+outside /var: not a system daemon
+under /var: system daemon
diff --git a/test/unittest/proc/tst.system-daemon-files.sh b/test/unittest/proc/tst.system-daemon-files.sh
new file mode 100755
index 00000000..b71ebeb4
--- /dev/null
+++ b/test/unittest/proc/tst.system-daemon-files.sh
@@ -0,0 +1,95 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2026, d-Matrix Corporation. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+# Check how libproc classifies a root process without a terminal when it
+# does not ask systemd: a standard stream to a regular file outside /var
+# means it is not a system daemon, one under /var does not.
+#
+# libproc asks systemd whenever /run/systemd/system exists, and for a
+# process in a systemd slice, as this one is, the slice decides and these
+# rules are never reached.  So on a systemd system, dtrace runs under
+# unshare -m, in a mount namespace of its own, with a tmpfs mounted over
+# /run/systemd: libproc does not find systemd and uses these rules.  The
+# namespace is made private first, so the rest of the system still sees
+# /run/systemd.
+#
+
+dtrace=$1
+
+DIRNAME=$tmpdir/system-daemon-files.$$.$RANDOM
+if [[ $DIRNAME == /var/* ]]; then
+	echo "$DIRNAME is under /var"
+	exit 1
+fi
+mkdir -p $DIRNAME
+cd $DIRNAME
+
+VARFILE=$(mktemp /var/tmp/system-daemon-files.XXXXXX) || exit 1
+trap "rm -f $VARFILE" EXIT
+
+# main PIDFILE: leave the terminal's session, write the pid to PIDFILE and
+# sleep for a minute
+cat > main.c <<EOT
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	FILE *f;
+	int i;
+
+	if (setsid() < 0 || (f = fopen(argv[1], "w")) == NULL)
+		return 1;
+	fprintf(f, "%d\n", getpid());
+	fclose(f);
+	for (i = 0; i < 600; i++)
+		usleep(100000);
+	return 0;
+}
+EOT
+$CC -o main main.c || exit 1
+
+# run_dtrace DTRACE ARGS: run dtrace, with /run/systemd hidden if it exists
+run_dtrace() {
+	if [ -d /run/systemd/system ]; then
+		unshare -m sh -c 'mount --make-rprivate / &&
+		    mount -t tmpfs none /run/systemd && exec "$@"' sh "$@"
+	else
+		"$@"
+	fi
+}
+
+# verdict LABEL FILE: run main with stdout to FILE and print LABEL and
+# libproc's verdict
+verdict() {
+	local pid out
+
+	rm -f pid
+	./main pid < /dev/null > $2 2> /dev/null &
+	for i in $(seq 50); do
+		[ -s pid ] && break
+		sleep 0.1
+	done
+	pid=$(cat pid)
+
+	out=$(DTRACE_DEBUG=t run_dtrace $dtrace $dt_flags \
+	    -ln "pid$pid:a.out:main:entry" 2>&1)
+	if grep -q " $pid is a system daemon process" <<< "$out"; then
+		echo "$1: system daemon"
+	elif grep -q "^ *[0-9]* *pid$pid " <<< "$out"; then
+		echo "$1: not a system daemon"
+	else
+		echo "$1: no verdict"
+		grep -v DEBUG <<< "$out" | tail -2
+	fi
+	kill $pid
+}
+
+verdict "outside /var" $DIRNAME/out.txt
+verdict "under /var" $VARFILE
+
+exit 0

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] test: add test for system daemons with regular files as streams
@ 2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  0 siblings, 0 replies; 8+ messages in thread
From: Eslam Almorshdy via B4 Relay @ 2026-11-19  7:11 UTC (permalink / raw)
  To: dtrace; +Cc: Nick Alcock, Kris Van Hees, Eslam Almorshdy

From: Eslam Almorshdy <ealmorshdy@d-matrix.ai>

Start a root process without a terminal with its stdout to a regular
file outside /var, then to one under /var, and check how libproc
classifies it from DTRACE_DEBUG: not a system daemon, then a system
daemon.

libproc asks systemd whenever /run/systemd/system exists, and for a
process in a systemd slice the slice decides.  So on a systemd system
the test runs dtrace under unshare -m, in a mount namespace of its own
with a tmpfs over /run/systemd: libproc does not find systemd and uses
the rules under test.

The first case fails without the previous patch.

Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>
---
 test/unittest/proc/tst.system-daemon-files.r  |  2 +
 test/unittest/proc/tst.system-daemon-files.sh | 95 +++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)

diff --git a/test/unittest/proc/tst.system-daemon-files.r b/test/unittest/proc/tst.system-daemon-files.r
new file mode 100644
index 00000000..0a2b3945
--- /dev/null
+++ b/test/unittest/proc/tst.system-daemon-files.r
@@ -0,0 +1,2 @@
+outside /var: not a system daemon
+under /var: system daemon
diff --git a/test/unittest/proc/tst.system-daemon-files.sh b/test/unittest/proc/tst.system-daemon-files.sh
new file mode 100755
index 00000000..b71ebeb4
--- /dev/null
+++ b/test/unittest/proc/tst.system-daemon-files.sh
@@ -0,0 +1,95 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2026, d-Matrix Corporation. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+# Check how libproc classifies a root process without a terminal when it
+# does not ask systemd: a standard stream to a regular file outside /var
+# means it is not a system daemon, one under /var does not.
+#
+# libproc asks systemd whenever /run/systemd/system exists, and for a
+# process in a systemd slice, as this one is, the slice decides and these
+# rules are never reached.  So on a systemd system, dtrace runs under
+# unshare -m, in a mount namespace of its own, with a tmpfs mounted over
+# /run/systemd: libproc does not find systemd and uses these rules.  The
+# namespace is made private first, so the rest of the system still sees
+# /run/systemd.
+#
+
+dtrace=$1
+
+DIRNAME=$tmpdir/system-daemon-files.$$.$RANDOM
+if [[ $DIRNAME == /var/* ]]; then
+	echo "$DIRNAME is under /var"
+	exit 1
+fi
+mkdir -p $DIRNAME
+cd $DIRNAME
+
+VARFILE=$(mktemp /var/tmp/system-daemon-files.XXXXXX) || exit 1
+trap "rm -f $VARFILE" EXIT
+
+# main PIDFILE: leave the terminal's session, write the pid to PIDFILE and
+# sleep for a minute
+cat > main.c <<EOT
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	FILE *f;
+	int i;
+
+	if (setsid() < 0 || (f = fopen(argv[1], "w")) == NULL)
+		return 1;
+	fprintf(f, "%d\n", getpid());
+	fclose(f);
+	for (i = 0; i < 600; i++)
+		usleep(100000);
+	return 0;
+}
+EOT
+$CC -o main main.c || exit 1
+
+# run_dtrace DTRACE ARGS: run dtrace, with /run/systemd hidden if it exists
+run_dtrace() {
+	if [ -d /run/systemd/system ]; then
+		unshare -m sh -c 'mount --make-rprivate / &&
+		    mount -t tmpfs none /run/systemd && exec "$@"' sh "$@"
+	else
+		"$@"
+	fi
+}
+
+# verdict LABEL FILE: run main with stdout to FILE and print LABEL and
+# libproc's verdict
+verdict() {
+	local pid out
+
+	rm -f pid
+	./main pid < /dev/null > $2 2> /dev/null &
+	for i in $(seq 50); do
+		[ -s pid ] && break
+		sleep 0.1
+	done
+	pid=$(cat pid)
+
+	out=$(DTRACE_DEBUG=t run_dtrace $dtrace $dt_flags \
+	    -ln "pid$pid:a.out:main:entry" 2>&1)
+	if grep -q " $pid is a system daemon process" <<< "$out"; then
+		echo "$1: system daemon"
+	elif grep -q "^ *[0-9]* *pid$pid " <<< "$out"; then
+		echo "$1: not a system daemon"
+	else
+		echo "$1: no verdict"
+		grep -v DEBUG <<< "$out" | tail -2
+	fi
+	kill $pid
+}
+
+verdict "outside /var" $DIRNAME/out.txt
+verdict "under /var" $VARFILE
+
+exit 0

-- 
2.55.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] libproc: fix readlink() typo in Psystem_daemon()
  2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  (?)
@ 2026-11-19 12:48   ` Nick Alcock
  -1 siblings, 0 replies; 8+ messages in thread
From: Nick Alcock @ 2026-11-19 12:48 UTC (permalink / raw)
  To: ealmorshdy; +Cc: dtrace, Kris Van Hees

On 19 Nov 2026, Eslam Almorshdy via uttered the following:

> From: Eslam Almorshdy <ealmorshdy@d-matrix.ai>
>
> readlink() returns the number of bytes it stored or -1, never 0, so
> the check for a regular file outside /var never ran.
>
> Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>
> Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>

Reviewed-by: Nick Alcock <nix@esperi.org.uk>

(who is to blame. How did I miss this?!)

-- 
NULL && (void)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] test: add test for system daemons with regular files as streams
  2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
  (?)
@ 2026-11-19 12:51   ` Nick Alcock
  -1 siblings, 0 replies; 8+ messages in thread
From: Nick Alcock @ 2026-11-19 12:51 UTC (permalink / raw)
  To: ealmorshdy; +Cc: dtrace, Kris Van Hees

On 19 Nov 2026, Eslam Almorshdy via stated:

> Start a root process without a terminal with its stdout to a regular
> file outside /var, then to one under /var, and check how libproc
> classifies it from DTRACE_DEBUG: not a system daemon, then a system
> daemon.
>
> libproc asks systemd whenever /run/systemd/system exists, and for a
> process in a systemd slice the slice decides.  So on a systemd system
> the test runs dtrace under unshare -m, in a mount namespace of its own
> with a tmpfs over /run/systemd: libproc does not find systemd and uses
> the rules under test.

Oh that's cunning. Finally we can routinely test non-systemd codepaths
everywhere!

It might be easier just to always run it in such a mount namespace, but
other than that...

> Signed-off-by: Eslam Almorshdy <ealmorshdy@d-matrix.ai>

Test LGTM.

Reviewed-by: Nick Alcock <nix@esperi.org.uk>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-19 12:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  7:11 [PATCH v2 0/2] Fix a readlink() typo in Psystem_daemon() Eslam Almorshdy
2026-11-19  7:11 ` Eslam Almorshdy via B4 Relay
2026-11-19  7:11 ` [PATCH v2 1/2] libproc: fix " Eslam Almorshdy
2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
2026-11-19 12:48   ` Nick Alcock
2026-11-19  7:11 ` [PATCH v2 2/2] test: add test for system daemons with regular files as streams Eslam Almorshdy
2026-11-19  7:11   ` Eslam Almorshdy via B4 Relay
2026-11-19 12:51   ` Nick Alcock

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.