8a984d
Conflicts in sysdeps/unix/sysv/linux/semctl.c were due to 64-bit time_t
8a984d
and RHEL8 has a simpler implementation.
8a984d
8a984d
Conflicts in sysdeps/unix/sysv/linux/Makefile were due to the usual test
8a984d
case conflicts.
8a984d
8a984d
commit 574500a108be1d2a6a0dc97a075c9e0a98371aba
8a984d
Author: Dmitry V. Levin <ldv@altlinux.org>
8a984d
Date:   Tue Sep 29 14:10:20 2020 -0300
8a984d
8a984d
    sysvipc: Fix SEM_STAT_ANY kernel argument pass [BZ #26637]
8a984d
    
8a984d
    Handle SEM_STAT_ANY the same way as SEM_STAT so that the buffer argument
8a984d
    of SEM_STAT_ANY is properly passed to the kernel and back.
8a984d
    
8a984d
    The regression testcase checks for Linux specifix SysV ipc message
8a984d
    control extension.  For IPC_INFO/SEM_INFO it tries to match the values
8a984d
    against the tunable /proc values and for SEM_STAT/SEM_STAT_ANY it
8a984d
    check if the create message queue is within the global list returned
8a984d
    by the kernel.
8a984d
    
8a984d
    Checked on x86_64-linux-gnu and on i686-linux-gnu (Linux v5.4 and on
8a984d
    Linux v4.15).
8a984d
    
8a984d
    Co-authored-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
8a984d
8a984d
# Conflicts:
8a984d
#	sysdeps/unix/sysv/linux/Makefile
8a984d
#	sysdeps/unix/sysv/linux/semctl.c
8a984d
8a984d
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
8a984d
index fb4ccd63ddec7eca..c6907796152eb09d 100644
8a984d
--- a/sysdeps/unix/sysv/linux/Makefile
8a984d
+++ b/sysdeps/unix/sysv/linux/Makefile
8a984d
@@ -45,7 +45,8 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
8a984d
 tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
8a984d
 	 tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
8a984d
 	 test-errno-linux tst-memfd_create tst-mlock2 tst-pkey \
8a984d
-	 tst-rlimit-infinity tst-ofdlocks
8a984d
+	 tst-rlimit-infinity tst-ofdlocks \
8a984d
+	 tst-sysvsem-linux
8a984d
 tests-internal += tst-ofdlocks-compat
8a984d
 
8a984d
 
8a984d
diff --git a/sysdeps/unix/sysv/linux/semctl.c b/sysdeps/unix/sysv/linux/semctl.c
8a984d
index e2925447eba2ee94..bdf31ca7747fe5a4 100644
8a984d
--- a/sysdeps/unix/sysv/linux/semctl.c
8a984d
+++ b/sysdeps/unix/sysv/linux/semctl.c
8a984d
@@ -51,6 +51,7 @@ __new_semctl (int semid, int semnum, int cmd, ...)
8a984d
     case IPC_STAT:      /* arg.buf */
8a984d
     case IPC_SET:
8a984d
     case SEM_STAT:
8a984d
+    case SEM_STAT_ANY:
8a984d
     case IPC_INFO:      /* arg.__buf */
8a984d
     case SEM_INFO:
8a984d
       va_start (ap, cmd);
8a984d
@@ -90,6 +91,7 @@ __old_semctl (int semid, int semnum, int cmd, ...)
8a984d
     case IPC_STAT:      /* arg.buf */
8a984d
     case IPC_SET:
8a984d
     case SEM_STAT:
8a984d
+    case SEM_STAT_ANY:
8a984d
     case IPC_INFO:      /* arg.__buf */
8a984d
     case SEM_INFO:
8a984d
       va_start (ap, cmd);
8a984d
diff --git a/sysdeps/unix/sysv/linux/tst-sysvsem-linux.c b/sysdeps/unix/sysv/linux/tst-sysvsem-linux.c
8a984d
new file mode 100644
8a984d
index 0000000000000000..45f19e2d37ed194a
8a984d
--- /dev/null
8a984d
+++ b/sysdeps/unix/sysv/linux/tst-sysvsem-linux.c
8a984d
@@ -0,0 +1,184 @@
8a984d
+/* Basic tests for Linux SYSV semaphore extensions.
8a984d
+   Copyright (C) 2020 Free Software Foundation, Inc.
8a984d
+   This file is part of the GNU C Library.
8a984d
+
8a984d
+   The GNU C Library is free software; you can redistribute it and/or
8a984d
+   modify it under the terms of the GNU Lesser General Public
8a984d
+   License as published by the Free Software Foundation; either
8a984d
+   version 2.1 of the License, or (at your option) any later version.
8a984d
+
8a984d
+   The GNU C Library is distributed in the hope that it will be useful,
8a984d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8a984d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8a984d
+   Lesser General Public License for more details.
8a984d
+
8a984d
+   You should have received a copy of the GNU Lesser General Public
8a984d
+   License along with the GNU C Library; if not, see
8a984d
+   <https://www.gnu.org/licenses/>.  */
8a984d
+
8a984d
+#include <sys/ipc.h>
8a984d
+#include <sys/sem.h>
8a984d
+#include <errno.h>
8a984d
+#include <stdlib.h>
8a984d
+#include <stdbool.h>
8a984d
+#include <stdio.h>
8a984d
+
8a984d
+#include <support/check.h>
8a984d
+#include <support/temp_file.h>
8a984d
+
8a984d
+/* These are for the temporary file we generate.  */
8a984d
+static char *name;
8a984d
+static int semid;
8a984d
+
8a984d
+static void
8a984d
+remove_sem (void)
8a984d
+{
8a984d
+  /* Enforce message queue removal in case of early test failure.
8a984d
+     Ignore error since the sem may already have being removed.  */
8a984d
+  semctl (semid, 0, IPC_RMID, 0);
8a984d
+}
8a984d
+
8a984d
+static void
8a984d
+do_prepare (int argc, char *argv[])
8a984d
+{
8a984d
+  TEST_VERIFY_EXIT (create_temp_file ("tst-sysvsem.", &name) != -1);
8a984d
+}
8a984d
+
8a984d
+#define PREPARE do_prepare
8a984d
+
8a984d
+#define SEM_MODE 0644
8a984d
+
8a984d
+union semun
8a984d
+{
8a984d
+  int val;
8a984d
+  struct semid_ds *buf;
8a984d
+  unsigned short  *array;
8a984d
+  struct seminfo *__buf;
8a984d
+};
8a984d
+
8a984d
+struct test_seminfo
8a984d
+{
8a984d
+  int semmsl;
8a984d
+  int semmns;
8a984d
+  int semopm;
8a984d
+  int semmni;
8a984d
+};
8a984d
+
8a984d
+/* It tries to obtain some system-wide SysV semaphore information from /proc
8a984d
+   to check against IPC_INFO/SEM_INFO.  The /proc only returns the tunables
8a984d
+   value of SEMMSL, SEMMNS, SEMOPM, and SEMMNI.
8a984d
+
8a984d
+   The kernel also returns constant value for SEMVMX, SEMMNU, SEMMAP, SEMUME,
8a984d
+   and also SEMUSZ and SEMAEM (for IPC_INFO).  The issue to check them is they
8a984d
+   might change over kernel releases.  */
8a984d
+
8a984d
+static void
8a984d
+read_sem_stat (struct test_seminfo *tseminfo)
8a984d
+{
8a984d
+  FILE *f = fopen ("/proc/sys/kernel/sem", "r");
8a984d
+  if (f == NULL)
8a984d
+    FAIL_UNSUPPORTED ("/proc is not mounted or /proc/sys/kernel/sem is not "
8a984d
+		      "available");
8a984d
+
8a984d
+  int r = fscanf (f, "%d %d %d %d",
8a984d
+		  &tseminfo->semmsl, &tseminfo->semmns, &tseminfo->semopm,
8a984d
+		  &tseminfo->semmni);
8a984d
+  TEST_VERIFY_EXIT (r == 4);
8a984d
+
8a984d
+  fclose (f);
8a984d
+}
8a984d
+
8a984d
+
8a984d
+/* Check if the semaphore with IDX (index into the kernel's internal array)
8a984d
+   matches the one with KEY.  The CMD is either SEM_STAT or SEM_STAT_ANY.  */
8a984d
+
8a984d
+static bool
8a984d
+check_seminfo (int idx, key_t key, int cmd)
8a984d
+{
8a984d
+  struct semid_ds seminfo;
8a984d
+  int sid = semctl (idx, 0, cmd, (union semun) { .buf = &seminfo });
8a984d
+  /* Ignore unused array slot returned by the kernel or information from
8a984d
+     unknown semaphores.  */
8a984d
+  if ((sid == -1 && errno == EINVAL) || sid != semid)
8a984d
+    return false;
8a984d
+
8a984d
+  if (sid == -1)
8a984d
+    FAIL_EXIT1 ("semctl with SEM_STAT failed (errno=%d)", errno);
8a984d
+
8a984d
+  TEST_COMPARE (seminfo.sem_perm.__key, key);
8a984d
+  TEST_COMPARE (seminfo.sem_perm.mode, SEM_MODE);
8a984d
+  TEST_COMPARE (seminfo.sem_nsems, 1);
8a984d
+
8a984d
+  return true;
8a984d
+}
8a984d
+
8a984d
+static int
8a984d
+do_test (void)
8a984d
+{
8a984d
+  atexit (remove_sem);
8a984d
+
8a984d
+  key_t key = ftok (name, 'G');
8a984d
+  if (key == -1)
8a984d
+    FAIL_EXIT1 ("ftok failed: %m");
8a984d
+
8a984d
+  semid = semget (key, 1, IPC_CREAT | IPC_EXCL | SEM_MODE);
8a984d
+  if (semid == -1)
8a984d
+    FAIL_EXIT1 ("semget failed: %m");
8a984d
+
8a984d
+  struct test_seminfo tipcinfo;
8a984d
+  read_sem_stat (&tipcinfo);
8a984d
+
8a984d
+  int semidx;
8a984d
+
8a984d
+  {
8a984d
+    struct seminfo ipcinfo;
8a984d
+    semidx = semctl (semid, 0, IPC_INFO, (union semun) { .__buf = &ipcinfo });
8a984d
+    if (semidx == -1)
8a984d
+      FAIL_EXIT1 ("semctl with IPC_INFO failed: %m");
8a984d
+
8a984d
+    TEST_COMPARE (ipcinfo.semmsl, tipcinfo.semmsl);
8a984d
+    TEST_COMPARE (ipcinfo.semmns, tipcinfo.semmns);
8a984d
+    TEST_COMPARE (ipcinfo.semopm, tipcinfo.semopm);
8a984d
+    TEST_COMPARE (ipcinfo.semmni, tipcinfo.semmni);
8a984d
+  }
8a984d
+
8a984d
+  /* Same as before but with SEM_INFO.  */
8a984d
+  {
8a984d
+    struct seminfo ipcinfo;
8a984d
+    semidx = semctl (semid, 0, SEM_INFO, (union semun) { .__buf = &ipcinfo });
8a984d
+    if (semidx == -1)
8a984d
+      FAIL_EXIT1 ("semctl with IPC_INFO failed: %m");
8a984d
+
8a984d
+    TEST_COMPARE (ipcinfo.semmsl, tipcinfo.semmsl);
8a984d
+    TEST_COMPARE (ipcinfo.semmns, tipcinfo.semmns);
8a984d
+    TEST_COMPARE (ipcinfo.semopm, tipcinfo.semopm);
8a984d
+    TEST_COMPARE (ipcinfo.semmni, tipcinfo.semmni);
8a984d
+  }
8a984d
+
8a984d
+  /* We check if the created semaphore shows in the system-wide status.  */
8a984d
+  bool found = false;
8a984d
+  for (int i = 0; i <= semidx; i++)
8a984d
+    {
8a984d
+      /* We can't tell apart if SEM_STAT_ANY is not supported (kernel older
8a984d
+	 than 4.17) or if the index used is invalid.  So it just check if
8a984d
+	 value returned from a valid call matches the created semaphore.  */
8a984d
+      check_seminfo (i, key, SEM_STAT_ANY);
8a984d
+
8a984d
+      if (check_seminfo (i, key, SEM_STAT))
8a984d
+	{
8a984d
+	  found = true;
8a984d
+	  break;
8a984d
+	}
8a984d
+    }
8a984d
+
8a984d
+  if (!found)
8a984d
+    FAIL_EXIT1 ("semctl with SEM_STAT/SEM_STAT_ANY could not find the "
8a984d
+		"created  semaphore");
8a984d
+
8a984d
+  if (semctl (semid, 0, IPC_RMID, 0) == -1)
8a984d
+    FAIL_EXIT1 ("semctl failed: %m");
8a984d
+
8a984d
+  return 0;
8a984d
+}
8a984d
+
8a984d
+#include <support/test-driver.c>
8a984d
diff --git a/sysvipc/test-sysvsem.c b/sysvipc/test-sysvsem.c
8a984d
index a8e9bff000949ff8..d197772917a7579d 100644
8a984d
--- a/sysvipc/test-sysvsem.c
8a984d
+++ b/sysvipc/test-sysvsem.c
8a984d
@@ -20,6 +20,7 @@
8a984d
 #include <stdlib.h>
8a984d
 #include <errno.h>
8a984d
 #include <string.h>
8a984d
+#include <stdbool.h>
8a984d
 #include <sys/types.h>
8a984d
 #include <sys/ipc.h>
8a984d
 #include <sys/sem.h>