Linux DTrace development list
 help / color / mirror / Atom feed
From: Kris Van Hees <kris.van.hees@oracle.com>
To: ealmorshdy@d-matrix.ai
Cc: dtrace@lists.linux.dev, Kris Van Hees <kris.van.hees@oracle.com>,
	Nick Alcock <nix@esperi.org.uk>
Subject: Re: [PATCH 2/2] test: add test for system daemons with regular files as streams
Date: Wed, 18 Nov 2026 23:42:03 -0500	[thread overview]
Message-ID: <av5-G0XAipqPbQZ4@oracle.com> (raw)
In-Reply-To: <20261118-b4-bug008-daemon-readlink-v1-2-187c9eb22d11@d-matrix.ai>

On Wed, Nov 18, 2026 at 11:25:05PM +0000, Eslam Almorshdy via B4 Relay wrote:
> 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 | 94 +++++++++++++++++++++++++++
>  2 files changed, 96 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..a24b5eb1
> --- /dev/null
> +++ b/test/unittest/proc/tst.system-daemon-files.sh
> @@ -0,0 +1,94 @@
> +#!/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
> +mkdir -p $DIRNAME
> +cd $DIRNAME
> +
> +case $DIRNAME in
> +/var/*)        echo "$DIRNAME is under /var"; exit 1;;
> +esac

I would rewrite this using if [[ $DIRNAME == /var/* ]]; then .. fi and do it
immediately after setting DIRNAME.  Might be a personal taste thing, so if you
really prefer case .. esac, I'm OK to leave it.  But moving it up makes sense
either way.

> +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
> 
> 
> 
> 

      reply	other threads:[~2026-11-19  4:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 23:25 [PATCH 0/2] Fix a readlink() typo in Psystem_daemon() Eslam Almorshdy via B4 Relay
2026-11-18 23:25 ` [PATCH 1/2] libproc: fix " Eslam Almorshdy via B4 Relay
2026-11-19  4:42   ` Kris Van Hees
2026-11-18 23:25 ` [PATCH 2/2] test: add test for system daemons with regular files as streams Eslam Almorshdy via B4 Relay
2026-11-19  4:42   ` Kris Van Hees [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=av5-G0XAipqPbQZ4@oracle.com \
    --to=kris.van.hees@oracle.com \
    --cc=dtrace@lists.linux.dev \
    --cc=ealmorshdy@d-matrix.ai \
    --cc=nix@esperi.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox