unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
From: Tomas Volf <~@wolfsden.cz>
To: 68800@debbugs.gnu.org
Subject: bug#68800: Guix waits for termination of a kernel thread
Date: Mon, 29 Jan 2024 18:44:00 +0100	[thread overview]
Message-ID: <Zbfj4EjZHIqIcUyJ@ws> (raw)
In-Reply-To: <ZbfhqeMUdS63NB93@ws>

[-- Attachment #1: Type: text/plain, Size: 3587 bytes --]

> Date: Mon, 29 Jan 2024 17:31:33 +0100
> From: Ludovic Courtès <ludo@gnu.org>
> To: guix-devel@gnu.org
> Subject: Re: GNU Shepherd 0.10.3 released
>
> Hi,
>
> Tomas Volf <~@wolfsden.cz> skribis:
>
> > Ah, that code indeed returns #f for the pid in question:
> >
> >     scheme@(guix-user)> ((@@ (guix build syscalls) kernel?) 688)
> >     $1 = #f
> >
> > The stat file:
> >
> >     $ cat /proc/688/stat
> >     688 (mt76-tx phy0) S 2 0 0 0 -1 2129984 0 0 0 0 0 0 0 0 -2 0 1 0 964 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 5 1 1 0 0 0 0 0 0 0 0 0 0 0
> >
> > So the start_code is not zero (I would guess it is -1).  I have no idea what
> > that means though.
>
> What about this method (from shepherd)?
>
> --8<---------------cut here---------------start------------->8---
> (define (linux-process-flags pid)
>   "Return the process flags of @var{pid} (or'd @code{PF_} constants), assuming
> the Linux /proc file system is mounted; raise a @code{system-error} exception
> otherwise."
>   (call-with-input-file (string-append "/proc/" (number->string pid)
>                                        "/stat")
>     (lambda (port)
>       (define line
>         (get-string-all port))
>
>       ;; Parse like systemd's 'is_kernel_thread' function.
>       (let ((offset (string-index line #\))))     ;offset past 'tcomm' field
>         (match (and offset
>                     (string-tokenize (string-drop line (+ offset 1))))
>           ((state ppid pgrp sid tty-nr tty-pgrp flags . _)
>            (or (string->number flags) 0))
>           (_
>            0))))))
>
> ;; Per-process flag defined in <linux/sched.h>.
> (define PF_KTHREAD #x00200000)                    ;I am a kernel thread
>
> (define (linux-kernel-thread? pid)
>   "Return true if @var{pid} is a Linux kernel thread."
>   (= PF_KTHREAD (logand (linux-process-flags pid) PF_KTHREAD)))
> --8<---------------cut here---------------end--------------->8---
>
> If it works better, we can use that in syscalls.scm as well.

Yes, that does seem to work:

    scheme@(guile-user)> ,use (ice-9 match)
    scheme@(guile-user)> ,use (ice-9 textual-ports)
    scheme@(guile-user)> (define (linux-process-flags pid)
      "Return the process flags of @var{pid} (or'd @code{PF_} constants), assuming
    the Linux /proc file system is mounted; raise a @code{system-error} exception
    otherwise."
      (call-with-input-file (string-append "/proc/" (number->string pid)
                                           "/stat")
        (lambda (port)
          (define line
            (get-string-all port))

          ;; Parse like systemd's 'is_kernel_thread' function.
          (let ((offset (string-index line #\))))     ;offset past 'tcomm' field
            (match (and offset
                        (string-tokenize (string-drop line (+ offset 1))))
              ((state ppid pgrp sid tty-nr tty-pgrp flags . _)
               (or (string->number flags) 0))
              (_
               0))))))
    scheme@(guile-user)> ;; Per-process flag defined in <linux/sched.h>.
    (define PF_KTHREAD #x00200000)                    ;I am a kernel thread
    scheme@(guile-user)> (define (linux-kernel-thread? pid)
      "Return true if @var{pid} is a Linux kernel thread."
      (= PF_KTHREAD (logand (linux-process-flags pid) PF_KTHREAD)))
    scheme@(guile-user)> (linux-kernel-thread? 685)
    $5 = #t

Tomas

--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2024-01-29 17:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29 17:34 bug#68800: Guix waits for termination of a kernel thread Tomas Volf
2024-01-29 17:44 ` Tomas Volf [this message]
2024-02-20 10:08   ` Ludovic Courtès

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

  List information: https://guix.gnu.org/

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

  git send-email \
    --in-reply-to=Zbfj4EjZHIqIcUyJ@ws \
    --to=~@wolfsden.cz \
    --cc=68800@debbugs.gnu.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).