Subject: [bug#63527] [PATCH v4 1/8] gnu: glibc: Update time patches for the Hurd.

From: Janneke Nieuwenhuizen <[email-masked]>
Date: Thu, 18 May 2023 11:38:56 +0200

The previous set of time patches were adapted from the Debian Salsa glibc
package git and were needed only to get Python to build.  The rumpkernel needs
the full versions of those patches, with the current set causes a rumpkernel
panic.

As discussed/suggested on IRC

    https://logs.guix.gnu.org/hurd/2023-05-17.log#150916

* gnu/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch,
gnu/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch: New files.
* gnu/packages/cross-base.scm (cross-libc*)[native-inputs]: Add them, together
with their incorrect/incomplete already applied versions.
[arguments]: Use them in a new patch-libc/hurd phase: revert the incorrect
ones and apply the new patches.
---
 gnu/local.mk                                  |   2 +
 gnu/packages/cross-base.scm                   |  34 ++++-
 ...bc-2.35-hurd-clock_gettime_monotonic.patch | 137 ++++++++++++++++++
 ...glibc-2.35-hurd-clock_t_centiseconds.patch |  63 ++++++++
 4 files changed, 233 insertions(+), 3 deletions(-)
 create mode 100644 gnu/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch
 create mode 100644 gnu/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 42514ded8e..c5b3568742 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1233,6 +1233,8 @@ dist_patch_DATA =						\
   %D%/packages/patches/glib-appinfo-watch.patch			\
   %D%/packages/patches/glib-networking-gnutls-binding.patch	\
   %D%/packages/patches/glib-skip-failing-test.patch		\
+  %D%/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch	\
+  %D%/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch	\
   %D%/packages/patches/glibc-CVE-2019-7309.patch		\
   %D%/packages/patches/glibc-CVE-2019-9169.patch		\
   %D%/packages/patches/glibc-CVE-2019-19126.patch		\
diff --git a/gnu/packages/cross-base.scm b/gnu/packages/cross-base.scm
index 8d0b41180b..c8d1aef814 100644
--- a/gnu/packages/cross-base.scm
+++ b/gnu/packages/cross-base.scm
@@ -1,7 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2013-2018, 2020, 2023 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2014, 2015, 2018 Mark H Weaver <[email-masked]>
-;;; Copyright © 2016, 2019 Jan (janneke) Nieuwenhuizen <[email-masked]>
+;;; Copyright © 2016, 2019, 2023 Janneke Nieuwenhuizen <[email-masked]>
 ;;; Copyright © 2016 Manolis Fragkiskos Ragkousis <[email-masked]>
 ;;; Copyright © 2018 Tobias Geerinckx-Rice <[email-masked]>
 ;;; Copyright © 2019, 2020, 2021 Marius Bakke <[email-masked]>
@@ -624,7 +624,22 @@ (define* (cross-libc* target
                                (("/[^ ]+/lib/libc.so.0.3")
                                 (string-append out "/lib/libc.so.0.3"
                                                " libmachuser.so libhurduser.so"))))
-                           #t)))
+                           #t))
+                       ;; TODO: move to glibc in the next rebuild cycle
+                       (add-after 'unpack 'patch-libc/hurd
+                         (lambda* (#:key inputs #:allow-other-keys)
+                           (for-each
+                            (lambda (name)
+                              (let ((patch (assoc-ref inputs name)))
+                                (invoke "patch" "-p1" "--force" "-R" "-i" patch)))
+                            '("hurd-monotonic.patch"
+                              "hurd-centiseconds.patch"))
+                           (for-each
+                            (lambda (name)
+                              (let ((patch (assoc-ref inputs name)))
+                                (invoke "patch" "-p1" "--force" "-i" patch)))
+                            '("2.35-hurd-centiseconds.patch"
+                              "2.35-hurd-monotonic.patch")))))
                      '())))))
 
         ;; Shadow the native "kernel-headers" because glibc's recipe expects the
@@ -637,7 +652,20 @@ (define* (cross-libc* target
                                `(("cross-mig"
                                   ,(cross-mig target
                                               #:xgcc xgcc
-                                              #:xbinutils xbinutils)))
+                                              #:xbinutils xbinutils))
+                                 ;; TODO: move to glibc in the next rebuild cycle
+                                 ("hurd-monotonic.patch"
+                                  ,@(search-patches
+                                     "glibc-hurd-clock_gettime_monotonic.patch"))
+                                 ("hurd-centiseconds.patch"
+                                  ,@(search-patches
+                                     "glibc-hurd-clock_t_centiseconds.patch"))
+                                 ("2.35-hurd-monotonic.patch"
+                                  ,@(search-patches
+                                     "glibc-2.35-hurd-clock_gettime_monotonic.patch"))
+                                 ("2.35-hurd-centiseconds.patch"
+                                  ,@(search-patches
+                                     "glibc-2.35-hurd-clock_t_centiseconds.patch")))
                                '())
                          ,@(package-inputs libc) ;FIXME: static-bash
                          ,@(package-native-inputs libc))))))
diff --git a/gnu/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch b/gnu/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch
new file mode 100644
index 0000000000..05769d61b9
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.35-hurd-clock_gettime_monotonic.patch
@@ -0,0 +1,137 @@
+Upstream status: taken from upstream:
+
+    https://salsa.debian.org/glibc-team/glibc/-/blob/sid/debian/patches/hurd-i386/local-clock_gettime_MONOTONIC.diff
+
+Use the realtime clock for the monotonic clock. This is of course not a proper
+implementation (which is being done in Mach), but will permit to fix at least
+the iceweasel stack.
+
+vlc however doesn't build when _POSIX_CLOCK_SELECTION is enabled but
+_POSIX_TIMERS is not, and they refuse to fix that (see #765578), so disable the
+former.
+
+---
+ sysdeps/mach/hurd/bits/posix_opt.h |    2 +-
+ sysdeps/unix/clock_gettime.c       |    1 +
+ 2 files changed, 2 insertions(+), 1 deletion(-)
+Index: glibc-2.27/sysdeps/mach/clock_gettime.c
+===================================================================
+--- glibc-2.27.orig/sysdeps/mach/clock_gettime.c
++++ glibc-2.27/sysdeps/mach/clock_gettime.c
+@@ -31,6 +31,10 @@ __clock_gettime (clockid_t clock_id, str
+   switch (clock_id) {
+ 
+     case CLOCK_REALTIME:
++    case CLOCK_MONOTONIC:
++    case CLOCK_MONOTONIC_RAW:
++    case CLOCK_REALTIME_COARSE:
++    case CLOCK_MONOTONIC_COARSE:
+       {
+ 	/* __host_get_time can only fail if passed an invalid host_t.
+ 	   __mach_host_self could theoretically fail (producing an
+Index: glibc-2.27/rt/timer_create.c
+===================================================================
+--- glibc-2.27.orig/rt/timer_create.c
++++ glibc-2.27/rt/timer_create.c
+@@ -48,7 +48,7 @@ timer_create (clockid_t clock_id, struct
+       return -1;
+     }
+ 
+-  if (clock_id != CLOCK_REALTIME)
++  if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_MONOTONIC_RAW && clock_id != CLOCK_REALTIME_COARSE && clock_id != CLOCK_MONOTONIC_COARSE)
+     {
+       __set_errno (EINVAL);
+       return -1;
+Index: glibc-2.27/sysdeps/mach/hurd/bits/posix_opt.h
+===================================================================
+--- glibc-2.27.orig/sysdeps/mach/hurd/bits/posix_opt.h
++++ glibc-2.27/sysdeps/mach/hurd/bits/posix_opt.h
+@@ -163,10 +163,10 @@
+ #define _POSIX_THREAD_PROCESS_SHARED	-1
+ 
+ /* The monotonic clock might be available.  */
+-#define _POSIX_MONOTONIC_CLOCK	0
++#define _POSIX_MONOTONIC_CLOCK	200809L
+ 
+-/* The clock selection interfaces are available.  */
+-#define _POSIX_CLOCK_SELECTION	200809L
++/* The clock selection interfaces are not really available yet.  */
++#define _POSIX_CLOCK_SELECTION	-1
+ 
+ /* Advisory information interfaces could be available in future.  */
+ #define _POSIX_ADVISORY_INFO	0
+Index: glibc-upstream/sysdeps/posix/clock_getres.c
+===================================================================
+--- glibc-upstream.orig/sysdeps/posix/clock_getres.c
++++ glibc-upstream/sysdeps/posix/clock_getres.c
+@@ -52,6 +52,10 @@ __clock_getres (clockid_t clock_id, stru
+   switch (clock_id)
+     {
+     case CLOCK_REALTIME:
++    case CLOCK_MONOTONIC:
++    case CLOCK_MONOTONIC_RAW:
++    case CLOCK_REALTIME_COARSE:
++    case CLOCK_MONOTONIC_COARSE:
+       retval = realtime_getres (res);
+       break;
+ 
+--- ./sysdeps/mach/clock_nanosleep.c.original	2020-07-21 00:31:35.226113142 +0200
++++ ./sysdeps/mach/clock_nanosleep.c	2020-07-21 00:31:49.026185761 +0200
+@@ -62,7 +62,7 @@
+ __clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
+ 		   struct timespec *rem)
+ {
+-  if (clock_id != CLOCK_REALTIME
++  if ((clock_id != CLOCK_REALTIME && clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_MONOTONIC_RAW && clock_id != CLOCK_REALTIME_COARSE && clock_id != CLOCK_MONOTONIC_COARSE)
+       || !valid_nanoseconds (req->tv_nsec)
+       || (flags != 0 && flags != TIMER_ABSTIME))
+     return EINVAL;
+Index: glibc-2.32/hurd/hurdlock.c
+===================================================================
+--- glibc-2.32.orig/hurd/hurdlock.c
++++ glibc-2.32/hurd/hurdlock.c
+@@ -47,7 +47,7 @@ int
+ __lll_abstimed_wait (void *ptr, int val,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -59,7 +59,7 @@ int
+ __lll_abstimed_wait_intr (void *ptr, int val,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -79,7 +79,7 @@ int
+ __lll_abstimed_xwait (void *ptr, int lo, int hi,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   int mlsec = compute_reltime (tsp, clk);
+@@ -91,7 +91,7 @@ int
+ __lll_abstimed_lock (void *ptr,
+   const struct timespec *tsp, int flags, int clk)
+ {
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   if (__lll_trylock (ptr) == 0)
+@@ -177,7 +177,7 @@ __lll_robust_abstimed_lock (void *ptr,
+   int wait_time = 25;
+   unsigned int val;
+ 
+-  if (clk != CLOCK_REALTIME)
++  if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
+     return EINVAL;
+ 
+   while (1)
diff --git a/gnu/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch b/gnu/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch
new file mode 100644
index 0000000000..c4e9fd5105
--- /dev/null
+++ b/gnu/packages/patches/glibc-2.35-hurd-clock_t_centiseconds.patch
@@ -0,0 +1,63 @@
+Upstream status: taken from upstream:
+
+    https://salsa.debian.org/glibc-team/glibc/-/blob/sid/debian/patches/hurd-i386/unsubmitted-clock_t_centiseconds.diff
+
+Some applications assume centisecond precision, or at most millisecond precision
+(e.g. guile).  This is a work-around for them.
+
+---
+ getclktck.c |    5 ++---
+ setitimer.c |    2 +-
+ times.c     |    2 +-
+ 4 files changed, 8 insertions(+), 9 deletions(-)
+commit d57f2f9b4bd007846af2fb4217486ea572579010
+Author: Richard Braun <[email-masked]>
+Date:   Tue Aug 27 11:35:31 2013 +0200
+
+    Express CPU time (clock_t of times(2)) in centiseconds
+
+diff --git a/sysdeps/mach/hurd/getclktck.c b/sysdeps/mach/hurd/getclktck.c
+index 69be2cc..5f7d946 100644
+--- a/sysdeps/mach/hurd/getclktck.c
++++ b/sysdeps/mach/hurd/getclktck.c
+@@ -18,12 +18,11 @@
+ 
+ #include <time.h>
+ 
+-/* Return frequency of `times'.
+-   Since Mach reports CPU times in microseconds, we always use 1 million.  */
++/* Return frequency of `times'.  */
+ int
+ __getclktck (void)
+ {
+-  return 1000000;
++  return 100;
+ }
+ 
+ /* Before glibc 2.2, the Hurd actually did this differently, so we
+diff --git a/sysdeps/mach/hurd/setitimer.c b/sysdeps/mach/hurd/setitimer.c
+index 39b6b16..4992c89 100644
+--- a/sysdeps/mach/hurd/setitimer.c
++++ b/sysdeps/mach/hurd/setitimer.c
+@@ -42,7 +42,7 @@ quantize_timeval (struct timeval *tv)
+   static time_t quantum = -1;
+ 
+   if (quantum == -1)
+-    quantum = 1000000 / __getclktck ();
++    quantum = 100 / __getclktck ();
+ 
+   tv->tv_usec = ((tv->tv_usec + (quantum - 1)) / quantum) * quantum;
+   if (tv->tv_usec >= 1000000)
+diff --git a/sysdeps/mach/hurd/times.c b/sysdeps/mach/hurd/times.c
+index 9e13a75..593c33a 100644
+--- a/sysdeps/mach/hurd/times.c
++++ b/sysdeps/mach/hurd/times.c
+@@ -29,7 +29,7 @@
+ static inline clock_t
+ clock_from_time_value (const time_value_t *t)
+ {
+-  return t->seconds * 1000000 + t->microseconds;
++  return t->seconds * 100 + t->microseconds / 10000;
+ }
+ 
+ /* Store the CPU time used by this process and all its
-- 
2.39.2