Guix waits for termination of a kernel thread

  • Done
  • quality assurance status badge
Details
2 participants
  • Ludovic Courtès
  • Tomas Volf
Owner
unassigned
Submitted by
Tomas Volf
Severity
important
T
T
Tomas Volf wrote on 29 Jan 18:34 +0100
(address . bug-guix@gnu.org)
ZbfhqeMUdS63NB93@ws
Attachment: file
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEt4NJs4wUfTYpiGikL7/ufbZ/wakFAmW34akACgkQL7/ufbZ/
walrvxAAtplW7nd59Y3C5bPa2M8hb/ykYPFDE7gOKIvrpb7rtd3+CV72oEKceM5g
YRvmhITrW98eZA22xm43qZYHKSo8JzpmTiU+Pq/ern4nnUyzLphzO6SzaP/qzfsL
KmPCqwnVrzlfrw6katYuu36fgpLSIejmEUdrOWPbq7tdO+cIMAL5+YMN8nhJVlVV
hUBLQogP+K0NzOKtImhQliZ5sK6Ggr5MbK/OdjZwwEThV0mCnzHli2fK+tnP8tD8
PAptDlEdf6OGmSYLInI/rAFaH/w9ZtPz4UxF1wx8+eRPlaklGIg37Q0dNDrIdaA/
QMAk0A8Y+NljTMuCdEoNSZX19Kh9FHPTbuB4jc8unEGh3fwEz/C5twvLEBIGZIFC
v0HEw5VgvbK6oNzvLAvpNEyVe6TwIuycGFYjJD0wMI7M43NNSsclsQQGYd8IPKt/
OFpIkYrtfzKffDFztXAWYEm8HgcAS0en8ovXhqRkpVolioXjMhDCMuwK3A+FWGNl
/yXkkN4Nxx/80Bk1gUnJG2R3GL4ud5n16IWB455EDdigMK5Lpl37moN5If9/R0dY
4jk4TI3AKP/tmGdUo4EENEuCJMW44qrE6dKqwxkqQn/ZOY7TiRoQaKat+JbYAeU8
tH/mKZ/J492AU3fbyxBE3SaWt+Y8l44StIPjuvj8yMvKd70O/MI=
=RIKL
-----END PGP SIGNATURE-----


T
T
Tomas Volf wrote on 29 Jan 18:44 +0100
(address . 68800@debbugs.gnu.org)
Zbfj4EjZHIqIcUyJ@ws
Toggle quote (54 lines)
> 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.
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEt4NJs4wUfTYpiGikL7/ufbZ/wakFAmW34+AACgkQL7/ufbZ/
walllQ/+MMP8zMtfyQq/pP6AgKUs+m5cs4OsVhHEhoZjoVPPsewzN8UhMtsnycm0
WelkCJhCmI0JiM/mlNUEaJdgZUWI7jX4V5QbJM/k3DmH05E37UXtqMSShFjkyIJs
YdBgNwanxz31A9XhaPutizoWAN3M6V2Tj4wDQlsjI39fkQ0d2N8iuq2Lo7JOs81c
7S/xZqH5gcE1u2wfyw23equFOD88KGpvvdr/xBwM7pBZTP52FY7vYgE9/mLAUyQz
B5NrSERCVrbOHZHAorh50JFWKRDHHawK9oekac6+v2jPPTnBUtOg0LPZ+ms34QIe
Ln8wxNoUrPZmt47hXj6xw6Q/5sKHW9HQsBoo+4P/gpkGMNJbOAnms1L4vSv1NCq1
UtlvQOQiY3cW5rzU3iyoAuoctVmoPFn2yODAjNgCEs7pp8IxpdJ+pA+p4stVykyf
jDN5FV8G0ZFUc7um+gHIx8/vBTinoV/yE6JNF8l0jdWhJhnNO+BaHjGtJRDPZesd
ZPGywofauv07y+4sGOoKZ2N4FqvVTn2o0CXpGom74ZMYjpWwFam6hhs2fiygavmI
xjTS3M8Gv804vnQyPHmLb+Rbkf5ENRnTyoHCZgirK/PNk1n/Hlnls7VpIJRdfcxX
QdDMGCgfClZoZTEd2HVl0TyIF4Gj8igTwmvogKMlsdJM+atXHfM=
=cLVC
-----END PGP SIGNATURE-----


L
L
Ludovic Courtès wrote on 20 Feb 10:38 +0100
control message for bug #68800
(address . control@debbugs.gnu.org)
87le7fmq64.fsf@gnu.org
severity 68800 important
quit
L
L
Ludovic Courtès wrote on 20 Feb 11:08 +0100
Re: bug#68800: Guix waits for termination of a kernel thread
(name . Tomas Volf)(address . ~@wolfsden.cz)(address . 68800-done@debbugs.gnu.org)
877cizmos0.fsf@gnu.org
Hi Tomas,

Tomas Volf <~@wolfsden.cz> skribis:

Toggle quote (34 lines)
>> 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:

Pushed as 34c79c6ae8103ebae9ce08c81a9220a6b82b05f6.

Thank you!

Ludo’.
Closed
?
Your comment

This issue is archived.

To comment on this conversation send an email to 68800@debbugs.gnu.org

To respond to this issue using the mumi CLI, first switch to it
mumi current 68800
Then, you may apply the latest patchset in this issue (with sign off)
mumi am -- -s
Or, compose a reply to this issue
mumi compose
Or, send patches to this issue
mumi send-email *.patch