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