test-sysvmsg.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Basic tests for SYSV message queue functions.
  2. Copyright (C) 2016-2019 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. <http://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 <support/support.h>
  23. #include <support/check.h>
  24. #include <support/temp_file.h>
  25. #define TEXTSIZE 32
  26. struct msgbuf_t
  27. {
  28. #ifdef _GNU_SOURCE
  29. __syscall_slong_t type;
  30. #else
  31. long type;
  32. #endif
  33. char buf[TEXTSIZE];
  34. };
  35. #define MSGTYPE 0x01020304
  36. #define MSGDATA "0123456789"
  37. /* These are for the temporary file we generate. */
  38. static char *name;
  39. static int msqid;
  40. static void
  41. remove_msq (void)
  42. {
  43. /* Enforce message queue removal in case of early test failure.
  44. Ignore error since the msgq may already have being removed. */
  45. msgctl (msqid, IPC_RMID, NULL);
  46. }
  47. static void
  48. do_prepare (int argc, char *argv[])
  49. {
  50. int fd = create_temp_file ("tst-sysvmsg.", &name);
  51. if (fd == -1)
  52. FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
  53. }
  54. #define PREPARE do_prepare
  55. /* It is not an extensive test, but rather a functional one aimed to check
  56. correct parameter passing on kernel. */
  57. #define MSGQ_MODE 0644
  58. static int
  59. do_test (void)
  60. {
  61. atexit (remove_msq);
  62. key_t key = ftok (name, 'G');
  63. if (key == -1)
  64. FAIL_EXIT1 ("ftok failed");
  65. msqid = msgget (key, MSGQ_MODE | IPC_CREAT);
  66. if (msqid == -1)
  67. {
  68. if (errno == ENOSYS)
  69. FAIL_UNSUPPORTED ("msgget not supported");
  70. FAIL_EXIT1 ("msgget failed (errno=%d)", errno);
  71. }
  72. /* Get message queue kernel information and do some sanity checks. */
  73. struct msqid_ds msginfo;
  74. if (msgctl (msqid, IPC_STAT, &msginfo) == -1)
  75. FAIL_EXIT1 ("msgctl with IPC_STAT failed (errno=%d)", errno);
  76. if (msginfo.msg_perm.__key != key)
  77. FAIL_EXIT1 ("msgid_ds::msg_perm::key (%d) != %d",
  78. (int) msginfo.msg_perm.__key, (int) key);
  79. if (msginfo.msg_perm.mode != MSGQ_MODE)
  80. FAIL_EXIT1 ("msgid_ds::msg_perm::mode (%o) != %o",
  81. msginfo.msg_perm.mode, MSGQ_MODE);
  82. if (msginfo.msg_qnum != 0)
  83. FAIL_EXIT1 ("msgid_ds::msg_qnum (%lu) != 0",
  84. (long unsigned) msginfo.msg_qnum);
  85. /* Check if last argument (IPC_NOWAIT) is correctly handled. */
  86. struct msgbuf_t msg2rcv = { 0 };
  87. if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE,
  88. IPC_NOWAIT) == -1
  89. && errno != ENOMSG)
  90. FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
  91. struct msgbuf_t msg2snd = { MSGTYPE, MSGDATA };
  92. if (msgsnd (msqid, &msg2snd, sizeof (msg2snd.buf), 0) == -1)
  93. FAIL_EXIT1 ("msgsnd failed (errno=%d)", errno);
  94. if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE, 0) == -1)
  95. FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
  96. int ret = 0;
  97. if (strncmp (msg2snd.buf, msg2rcv.buf, TEXTSIZE) != 0)
  98. ret = 1;
  99. if (msgctl (msqid, IPC_RMID, NULL) == -1)
  100. FAIL_EXIT1 ("msgctl failed");
  101. return ret;
  102. }
  103. #include <support/test-driver.c>