test-sysvmsg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Basic tests for SYSV message queue functions.
  2. Copyright (C) 2016-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/ipc.h>
  21. #include <sys/msg.h>
  22. #include <test-sysvipc.h>
  23. #include <support/support.h>
  24. #include <support/check.h>
  25. #include <support/temp_file.h>
  26. #define TEXTSIZE 32
  27. struct msgbuf_t
  28. {
  29. #ifdef _GNU_SOURCE
  30. __syscall_slong_t type;
  31. #else
  32. long type;
  33. #endif
  34. char buf[TEXTSIZE];
  35. };
  36. #define MSGTYPE 0x01020304
  37. #define MSGDATA "0123456789"
  38. /* These are for the temporary file we generate. */
  39. static char *name;
  40. static int msqid;
  41. static void
  42. remove_msq (void)
  43. {
  44. /* Enforce message queue removal in case of early test failure.
  45. Ignore error since the msgq may already have being removed. */
  46. msgctl (msqid, IPC_RMID, NULL);
  47. }
  48. static void
  49. do_prepare (int argc, char *argv[])
  50. {
  51. int fd = create_temp_file ("tst-sysvmsg.", &name);
  52. if (fd == -1)
  53. FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
  54. }
  55. #define PREPARE do_prepare
  56. /* It is not an extensive test, but rather a functional one aimed to check
  57. correct parameter passing on kernel. */
  58. #define MSGQ_MODE 0644
  59. static int
  60. do_test (void)
  61. {
  62. atexit (remove_msq);
  63. key_t key = ftok (name, 'G');
  64. if (key == -1)
  65. FAIL_EXIT1 ("ftok failed");
  66. msqid = msgget (key, MSGQ_MODE | IPC_CREAT);
  67. if (msqid == -1)
  68. {
  69. if (errno == ENOSYS)
  70. FAIL_UNSUPPORTED ("msgget not supported");
  71. FAIL_EXIT1 ("msgget failed (errno=%d)", errno);
  72. }
  73. TEST_COMPARE (msgctl (msqid, first_msg_invalid_cmd (), NULL), -1);
  74. TEST_COMPARE (errno, EINVAL);
  75. /* Get message queue kernel information and do some sanity checks. */
  76. struct msqid_ds msginfo;
  77. if (msgctl (msqid, IPC_STAT, &msginfo) == -1)
  78. FAIL_EXIT1 ("msgctl with IPC_STAT failed (errno=%d)", errno);
  79. if (msginfo.msg_perm.__key != key)
  80. FAIL_EXIT1 ("msgid_ds::msg_perm::key (%d) != %d",
  81. (int) msginfo.msg_perm.__key, (int) key);
  82. if (msginfo.msg_perm.mode != MSGQ_MODE)
  83. FAIL_EXIT1 ("msgid_ds::msg_perm::mode (%o) != %o",
  84. msginfo.msg_perm.mode, MSGQ_MODE);
  85. if (msginfo.msg_qnum != 0)
  86. FAIL_EXIT1 ("msgid_ds::msg_qnum (%lu) != 0",
  87. (long unsigned) msginfo.msg_qnum);
  88. /* Check if last argument (IPC_NOWAIT) is correctly handled. */
  89. struct msgbuf_t msg2rcv = { 0 };
  90. if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE,
  91. IPC_NOWAIT) == -1
  92. && errno != ENOMSG)
  93. FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
  94. struct msgbuf_t msg2snd = { MSGTYPE, MSGDATA };
  95. if (msgsnd (msqid, &msg2snd, sizeof (msg2snd.buf), 0) == -1)
  96. FAIL_EXIT1 ("msgsnd failed (errno=%d)", errno);
  97. if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE, 0) == -1)
  98. FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
  99. int ret = 0;
  100. if (strncmp (msg2snd.buf, msg2rcv.buf, TEXTSIZE) != 0)
  101. ret = 1;
  102. if (msgctl (msqid, IPC_RMID, NULL) == -1)
  103. FAIL_EXIT1 ("msgctl failed");
  104. return ret;
  105. }
  106. #include <support/test-driver.c>