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