Subject: Re: bug#57730: [PATCH] syscalls: Adjust for glibc 2.34 and later.

From: Ludovic Courtès <[email-masked]>
Date: Mon, 12 Sep 2022 23:45:47 +0200

Hi,

Marius Bakke <[email-masked]> skribis:

> This is a re-implementation of 3c8b6fd94ceb1e898216929e8768fb518dbf1de9 that
> works with new and old libc's.
>
> * guix/build/syscalls.scm (openpty, login-tty): Wrap in exception handlers and
> retry with libutil if the first call is unsuccessful.

[...]

>  (define openpty
> -  (let ((proc (syscall->procedure int "openpty" '(* * * * *)
> -                                  #:library "libutil")))
> -    (lambda ()
> -      "Return two file descriptors: one for the pseudo-terminal control side,
> +  (lambda* (#:optional library)
> +    "Return two file descriptors: one for the pseudo-terminal control side,
>  and one for the controlled side."
> +    (let ((proc (syscall->procedure int "openpty" '(* * * * *)
> +                                    #:library library)))

In general, we must ensure that ‘syscall->procedure’ is called only once
per procedure, because it’s expensive compared to the function we’re
wrapping (it’s doing dlopen, dlsym, and all that).

Anyway, I think this should work:

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 7842b0a9fc..e081aaca44 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -445,9 +445,14 @@ (define* (syscall->procedure return-type name argument-types
 the returned procedure is called."
   (catch #t
     (lambda ()
+      ;; Note: When #:library is set, try it first and fall back to libc
+      ;; proper.  This is because libraries like libutil.so have been subsumed
+      ;; by libc.so with glibc >= 2.34.
       (let ((ptr (dynamic-func name
                                (if library
-                                   (dynamic-link library)
+                                   (or (false-if-exception
+                                        (dynamic-link library))
+                                       (dynamic-link))
                                    (dynamic-link)))))
         ;; The #:return-errno? facility was introduced in Guile 2.0.12.
         (pointer->procedure return-type ptr argument-types

WDYT?

Thanks,
Ludo’.