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