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