8a984d
Backport only the test case:
8a984d
 * sysdeps/unix/sysv/linux/tst-sysvmsg-linux.c
8a984d
8a984d
This improves coverage for IPC_INFO and MSG_INFO.
8a984d
8a984d
We don't need the actual fix in the bug because we don't have the 64-bit
8a984d
time_t handling backported.
8a984d
8a984d
commit 20a00dbefca5695cccaa44846a482db8ccdd85ab
8a984d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
8a984d
Date:   Tue Sep 29 14:39:56 2020 -0300
8a984d
8a984d
    sysvipc: Fix IPC_INFO and MSG_INFO handling [BZ #26639]
8a984d
    
8a984d
    Both commands are Linux extensions where the third argument is a
8a984d
    'struct msginfo' instead of 'struct msqid_ds' and its information
8a984d
    does not contain any time related fields (so there is no need to
8a984d
    extra conversion for __IPC_TIME64.
8a984d
    
8a984d
    The regression testcase checks for Linux specifix SysV ipc message
8a984d
    control extension.  For IPC_INFO/MSG_INFO it tries to match the values
8a984d
    against the tunable /proc values and for MSG_STAT/MSG_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
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
8a984d
index 7d04e3313c56c15d..688cf9fa9dea23a6 100644
8a984d
--- a/sysdeps/unix/sysv/linux/Makefile
8a984d
+++ b/sysdeps/unix/sysv/linux/Makefile
8a984d
@@ -46,7 +46,7 @@ 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-sysvsem-linux
8a984d
+	 tst-sysvsem-linux tst-sysvmsg-linux
8a984d
 tests-internal += tst-ofdlocks-compat
8a984d
 
8a984d
 
8a984d
diff --git a/sysdeps/unix/sysv/linux/tst-sysvmsg-linux.c b/sysdeps/unix/sysv/linux/tst-sysvmsg-linux.c
8a984d
new file mode 100644
8a984d
index 0000000000000000..1857fab8c1fdf041
8a984d
--- /dev/null
8a984d
+++ b/sysdeps/unix/sysv/linux/tst-sysvmsg-linux.c
8a984d
@@ -0,0 +1,177 @@
8a984d
+/* Basic tests for Linux SYSV message queue extensions.
8a984d
+   Copyright (C) 2020-2021 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/msg.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
+#define MSGQ_MODE 0644
8a984d
+
8a984d
+/* These are for the temporary file we generate.  */
8a984d
+static char *name;
8a984d
+static int msqid;
8a984d
+
8a984d
+static void
8a984d
+remove_msq (void)
8a984d
+{
8a984d
+  /* Enforce message queue removal in case of early test failure.
8a984d
+     Ignore error since the msg may already have being removed.  */
8a984d
+  msgctl (msqid, IPC_RMID, NULL);
8a984d
+}
8a984d
+
8a984d
+static void
8a984d
+do_prepare (int argc, char *argv[])
8a984d
+{
8a984d
+  TEST_VERIFY_EXIT (create_temp_file ("tst-sysvmsg.", &name) != -1);
8a984d
+}
8a984d
+
8a984d
+#define PREPARE do_prepare
8a984d
+
8a984d
+struct test_msginfo
8a984d
+{
8a984d
+  int msgmax;
8a984d
+  int msgmnb;
8a984d
+  int msgmni;
8a984d
+};
8a984d
+
8a984d
+/* It tries to obtain some system-wide SysV messsage queue information from
8a984d
+   /proc to check against IPC_INFO/MSG_INFO.  The /proc only returns the
8a984d
+   tunables value of MSGMAX, MSGMNB, and MSGMNI.
8a984d
+
8a984d
+   The kernel also returns constant value for MSGSSZ, MSGSEG and also MSGMAP,
8a984d
+   MSGPOOL, and MSGTQL (for IPC_INFO).  The issue to check them is they might
8a984d
+   change over kernel releases.  */
8a984d
+
8a984d
+static int
8a984d
+read_proc_file (const char *file)
8a984d
+{
8a984d
+  FILE *f = fopen (file, "r");
8a984d
+  if (f == NULL)
8a984d
+    FAIL_UNSUPPORTED ("/proc is not mounted or %s is not available", file);
8a984d
+
8a984d
+  int v;
8a984d
+  int r = fscanf (f, "%d", & v);
8a984d
+  TEST_VERIFY_EXIT (r == 1);
8a984d
+
8a984d
+  fclose (f);
8a984d
+  return v;
8a984d
+}
8a984d
+
8a984d
+
8a984d
+/* Check if the message queue with IDX (index into the kernel's internal
8a984d
+   array) matches the one with KEY.  The CMD is either MSG_STAT or
8a984d
+   MSG_STAT_ANY.  */
8a984d
+
8a984d
+static bool
8a984d
+check_msginfo (int idx, key_t key, int cmd)
8a984d
+{
8a984d
+  struct msqid_ds msginfo;
8a984d
+  int mid = msgctl (idx, cmd, &msginfo);
8a984d
+  /* Ignore unused array slot returned by the kernel or information from
8a984d
+     unknown message queue.  */
8a984d
+  if ((mid == -1 && errno == EINVAL) || mid != msqid)
8a984d
+    return false;
8a984d
+
8a984d
+  if (mid == -1)
8a984d
+    FAIL_EXIT1 ("msgctl with %s failed: %m",
8a984d
+		cmd == MSG_STAT ? "MSG_STAT" : "MSG_STAT_ANY");
8a984d
+
8a984d
+  TEST_COMPARE (msginfo.msg_perm.__key, key);
8a984d
+  TEST_COMPARE (msginfo.msg_perm.mode, MSGQ_MODE);
8a984d
+  TEST_COMPARE (msginfo.msg_qnum, 0);
8a984d
+
8a984d
+  return true;
8a984d
+}
8a984d
+
8a984d
+static int
8a984d
+do_test (void)
8a984d
+{
8a984d
+  atexit (remove_msq);
8a984d
+
8a984d
+  key_t key = ftok (name, 'G');
8a984d
+  if (key == -1)
8a984d
+    FAIL_EXIT1 ("ftok failed: %m");
8a984d
+
8a984d
+  msqid = msgget (key, MSGQ_MODE | IPC_CREAT);
8a984d
+  if (msqid == -1)
8a984d
+    FAIL_EXIT1 ("msgget failed: %m");
8a984d
+
8a984d
+  struct test_msginfo tipcinfo;
8a984d
+  tipcinfo.msgmax = read_proc_file ("/proc/sys/kernel/msgmax");
8a984d
+  tipcinfo.msgmnb = read_proc_file ("/proc/sys/kernel/msgmnb");
8a984d
+  tipcinfo.msgmni = read_proc_file ("/proc/sys/kernel/msgmni");
8a984d
+
8a984d
+  int msqidx;
8a984d
+
8a984d
+  {
8a984d
+    struct msginfo ipcinfo;
8a984d
+    msqidx = msgctl (msqid, IPC_INFO, (struct msqid_ds *) &ipcinfo);
8a984d
+    if (msqidx == -1)
8a984d
+      FAIL_EXIT1 ("msgctl with IPC_INFO failed: %m");
8a984d
+
8a984d
+    TEST_COMPARE (ipcinfo.msgmax, tipcinfo.msgmax);
8a984d
+    TEST_COMPARE (ipcinfo.msgmnb, tipcinfo.msgmnb);
8a984d
+    TEST_COMPARE (ipcinfo.msgmni, tipcinfo.msgmni);
8a984d
+  }
8a984d
+
8a984d
+  /* Same as before but with MSG_INFO.  */
8a984d
+  {
8a984d
+    struct msginfo ipcinfo;
8a984d
+    msqidx = msgctl (msqid, MSG_INFO, (struct msqid_ds *) &ipcinfo);
8a984d
+    if (msqidx == -1)
8a984d
+      FAIL_EXIT1 ("msgctl with IPC_INFO failed: %m");
8a984d
+
8a984d
+    TEST_COMPARE (ipcinfo.msgmax, tipcinfo.msgmax);
8a984d
+    TEST_COMPARE (ipcinfo.msgmnb, tipcinfo.msgmnb);
8a984d
+    TEST_COMPARE (ipcinfo.msgmni, tipcinfo.msgmni);
8a984d
+  }
8a984d
+
8a984d
+  /* We check if the created message queue shows in global list.  */
8a984d
+  bool found = false;
8a984d
+  for (int i = 0; i <= msqidx; i++)
8a984d
+    {
8a984d
+      /* We can't tell apart if MSG_STAT_ANY is not supported (kernel older
8a984d
+	 than 4.17) or if the index used is invalid.  So it just check if the
8a984d
+	 value returned from a valid call matches the created message
8a984d
+	 queue.  */
8a984d
+      check_msginfo (i, key, MSG_STAT_ANY);
8a984d
+
8a984d
+      if (check_msginfo (i, key, MSG_STAT))
8a984d
+	{
8a984d
+	  found = true;
8a984d
+	  break;
8a984d
+	}
8a984d
+    }
8a984d
+
8a984d
+  if (!found)
8a984d
+    FAIL_EXIT1 ("msgctl with MSG_STAT/MSG_STAT_ANY could not find the "
8a984d
+		"created message queue");
8a984d
+
8a984d
+  if (msgctl (msqid, IPC_RMID, NULL) == -1)
8a984d
+    FAIL_EXIT1 ("msgctl failed");
8a984d
+
8a984d
+  return 0;
8a984d
+}
8a984d
+
8a984d
+#include <support/test-driver.c>