test-sysvshm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Basic tests for SYSV shared memory 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 <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/ipc.h>
  22. #include <sys/shm.h>
  23. #include <support/support.h>
  24. #include <support/check.h>
  25. #include <support/temp_file.h>
  26. /* These are for the temporary file we generate. */
  27. static char *name;
  28. static int shmid;
  29. static void
  30. remove_shm (void)
  31. {
  32. /* Enforce message queue removal in case of early test failure.
  33. Ignore error since the shm may already have being removed. */
  34. shmctl (shmid, IPC_RMID, 0);
  35. }
  36. static void
  37. do_prepare (int argc, char *argv[])
  38. {
  39. int fd = create_temp_file ("tst-sysvshm.", &name);
  40. if (fd == -1)
  41. FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
  42. }
  43. #define PREPARE do_prepare
  44. /* It is not an extensive test, but rather a functional one aimed to check
  45. correct parameter passing on kernel. */
  46. #define CHECK_EQ(v, k) \
  47. if ((v) != (k)) \
  48. FAIL_EXIT1("%d != %d", v, k)
  49. #define SHM_MODE 0666
  50. static int
  51. do_test (void)
  52. {
  53. atexit (remove_shm);
  54. key_t key = ftok (name, 'G');
  55. if (key == -1)
  56. FAIL_EXIT1 ("ftok failed");
  57. long int pgsz = sysconf (_SC_PAGESIZE);
  58. if (pgsz == -1)
  59. FAIL_EXIT1 ("sysconf (_SC_PAGESIZE) failed (errno = %d)", errno);
  60. shmid = shmget(key, pgsz, IPC_CREAT | IPC_EXCL | SHM_MODE);
  61. if (shmid == -1)
  62. {
  63. if (errno == ENOSYS)
  64. FAIL_UNSUPPORTED ("shmget not supported");
  65. FAIL_EXIT1 ("shmget failed (errno=%d)", errno);
  66. }
  67. /* Get shared memory kernel information and do some sanity checks. */
  68. struct shmid_ds shminfo;
  69. if (shmctl (shmid, IPC_STAT, &shminfo) == -1)
  70. FAIL_EXIT1 ("shmctl with IPC_STAT failed (errno=%d)", errno);
  71. if (shminfo.shm_perm.__key != key)
  72. FAIL_EXIT1 ("shmid_ds::shm_perm::key (%d) != %d",
  73. (int) shminfo.shm_perm.__key, (int) key);
  74. if (shminfo.shm_perm.mode != SHM_MODE)
  75. FAIL_EXIT1 ("shmid_ds::shm_perm::mode (%o) != %o",
  76. shminfo.shm_perm.mode, SHM_MODE);
  77. if (shminfo.shm_segsz != pgsz)
  78. FAIL_EXIT1 ("shmid_ds::shm_segsz (%lu) != %lu",
  79. (long unsigned) shminfo.shm_segsz, pgsz);
  80. /* Attach on shared memory and realize some operations. */
  81. int *shmem = shmat (shmid, NULL, 0);
  82. if (shmem == (void*) -1)
  83. FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
  84. shmem[0] = 0x55555555;
  85. shmem[32] = 0x44444444;
  86. shmem[64] = 0x33333333;
  87. shmem[128] = 0x22222222;
  88. if (shmdt (shmem) == -1)
  89. FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
  90. shmem = shmat (shmid, NULL, SHM_RDONLY);
  91. if (shmem == (void*) -1)
  92. FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
  93. CHECK_EQ (shmem[0], 0x55555555);
  94. CHECK_EQ (shmem[32], 0x44444444);
  95. CHECK_EQ (shmem[64], 0x33333333);
  96. CHECK_EQ (shmem[128], 0x22222222);
  97. if (shmdt (shmem) == -1)
  98. FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
  99. /* Finally free up the semnaphore resource. */
  100. if (shmctl (shmid, IPC_RMID, 0) == -1)
  101. FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
  102. return 0;
  103. }
  104. #include <support/test-driver.c>