msgque.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/msg.h>
  8. #include <fcntl.h>
  9. #include "../kselftest.h"
  10. #define MAX_MSG_SIZE 32
  11. struct msg1 {
  12. int msize;
  13. long mtype;
  14. char mtext[MAX_MSG_SIZE];
  15. };
  16. #define TEST_STRING "Test sysv5 msg"
  17. #define MSG_TYPE 1
  18. #define ANOTHER_TEST_STRING "Yet another test sysv5 msg"
  19. #define ANOTHER_MSG_TYPE 26538
  20. struct msgque_data {
  21. key_t key;
  22. int msq_id;
  23. int qbytes;
  24. int qnum;
  25. int mode;
  26. struct msg1 *messages;
  27. };
  28. int restore_queue(struct msgque_data *msgque)
  29. {
  30. int fd, ret, id, i;
  31. char buf[32];
  32. fd = open("/proc/sys/kernel/msg_next_id", O_WRONLY);
  33. if (fd == -1) {
  34. printf("Failed to open /proc/sys/kernel/msg_next_id\n");
  35. return -errno;
  36. }
  37. sprintf(buf, "%d", msgque->msq_id);
  38. ret = write(fd, buf, strlen(buf));
  39. if (ret != strlen(buf)) {
  40. printf("Failed to write to /proc/sys/kernel/msg_next_id\n");
  41. return -errno;
  42. }
  43. id = msgget(msgque->key, msgque->mode | IPC_CREAT | IPC_EXCL);
  44. if (id == -1) {
  45. printf("Failed to create queue\n");
  46. return -errno;
  47. }
  48. if (id != msgque->msq_id) {
  49. printf("Restored queue has wrong id (%d instead of %d)\n",
  50. id, msgque->msq_id);
  51. ret = -EFAULT;
  52. goto destroy;
  53. }
  54. for (i = 0; i < msgque->qnum; i++) {
  55. if (msgsnd(msgque->msq_id, &msgque->messages[i].mtype,
  56. msgque->messages[i].msize, IPC_NOWAIT) != 0) {
  57. printf("msgsnd failed (%m)\n");
  58. ret = -errno;
  59. goto destroy;
  60. }
  61. }
  62. return 0;
  63. destroy:
  64. if (msgctl(id, IPC_RMID, NULL))
  65. printf("Failed to destroy queue: %d\n", -errno);
  66. return ret;
  67. }
  68. int check_and_destroy_queue(struct msgque_data *msgque)
  69. {
  70. struct msg1 message;
  71. int cnt = 0, ret;
  72. while (1) {
  73. ret = msgrcv(msgque->msq_id, &message.mtype, MAX_MSG_SIZE,
  74. 0, IPC_NOWAIT);
  75. if (ret < 0) {
  76. if (errno == ENOMSG)
  77. break;
  78. printf("Failed to read IPC message: %m\n");
  79. ret = -errno;
  80. goto err;
  81. }
  82. if (ret != msgque->messages[cnt].msize) {
  83. printf("Wrong message size: %d (expected %d)\n", ret,
  84. msgque->messages[cnt].msize);
  85. ret = -EINVAL;
  86. goto err;
  87. }
  88. if (message.mtype != msgque->messages[cnt].mtype) {
  89. printf("Wrong message type\n");
  90. ret = -EINVAL;
  91. goto err;
  92. }
  93. if (memcmp(message.mtext, msgque->messages[cnt].mtext, ret)) {
  94. printf("Wrong message content\n");
  95. ret = -EINVAL;
  96. goto err;
  97. }
  98. cnt++;
  99. }
  100. if (cnt != msgque->qnum) {
  101. printf("Wrong message number\n");
  102. ret = -EINVAL;
  103. goto err;
  104. }
  105. ret = 0;
  106. err:
  107. if (msgctl(msgque->msq_id, IPC_RMID, NULL)) {
  108. printf("Failed to destroy queue: %d\n", -errno);
  109. return -errno;
  110. }
  111. return ret;
  112. }
  113. int dump_queue(struct msgque_data *msgque)
  114. {
  115. struct msqid_ds ds;
  116. int kern_id;
  117. int i, ret;
  118. for (kern_id = 0; kern_id < 256; kern_id++) {
  119. ret = msgctl(kern_id, MSG_STAT, &ds);
  120. if (ret < 0) {
  121. if (errno == EINVAL)
  122. continue;
  123. printf("Failed to get stats for IPC queue with id %d\n",
  124. kern_id);
  125. return -errno;
  126. }
  127. if (ret == msgque->msq_id)
  128. break;
  129. }
  130. msgque->messages = malloc(sizeof(struct msg1) * ds.msg_qnum);
  131. if (msgque->messages == NULL) {
  132. printf("Failed to get stats for IPC queue\n");
  133. return -ENOMEM;
  134. }
  135. msgque->qnum = ds.msg_qnum;
  136. msgque->mode = ds.msg_perm.mode;
  137. msgque->qbytes = ds.msg_qbytes;
  138. for (i = 0; i < msgque->qnum; i++) {
  139. ret = msgrcv(msgque->msq_id, &msgque->messages[i].mtype,
  140. MAX_MSG_SIZE, i, IPC_NOWAIT | MSG_COPY);
  141. if (ret < 0) {
  142. printf("Failed to copy IPC message: %m (%d)\n", errno);
  143. return -errno;
  144. }
  145. msgque->messages[i].msize = ret;
  146. }
  147. return 0;
  148. }
  149. int fill_msgque(struct msgque_data *msgque)
  150. {
  151. struct msg1 msgbuf;
  152. msgbuf.mtype = MSG_TYPE;
  153. memcpy(msgbuf.mtext, TEST_STRING, sizeof(TEST_STRING));
  154. if (msgsnd(msgque->msq_id, &msgbuf.mtype, sizeof(TEST_STRING),
  155. IPC_NOWAIT) != 0) {
  156. printf("First message send failed (%m)\n");
  157. return -errno;
  158. }
  159. msgbuf.mtype = ANOTHER_MSG_TYPE;
  160. memcpy(msgbuf.mtext, ANOTHER_TEST_STRING, sizeof(ANOTHER_TEST_STRING));
  161. if (msgsnd(msgque->msq_id, &msgbuf.mtype, sizeof(ANOTHER_TEST_STRING),
  162. IPC_NOWAIT) != 0) {
  163. printf("Second message send failed (%m)\n");
  164. return -errno;
  165. }
  166. return 0;
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. int msg, pid, err;
  171. struct msgque_data msgque;
  172. if (getuid() != 0)
  173. return ksft_exit_skip(
  174. "Please run the test as root - Exiting.\n");
  175. msgque.key = ftok(argv[0], 822155650);
  176. if (msgque.key == -1) {
  177. printf("Can't make key: %d\n", -errno);
  178. return ksft_exit_fail();
  179. }
  180. msgque.msq_id = msgget(msgque.key, IPC_CREAT | IPC_EXCL | 0666);
  181. if (msgque.msq_id == -1) {
  182. err = -errno;
  183. printf("Can't create queue: %d\n", err);
  184. goto err_out;
  185. }
  186. err = fill_msgque(&msgque);
  187. if (err) {
  188. printf("Failed to fill queue: %d\n", err);
  189. goto err_destroy;
  190. }
  191. err = dump_queue(&msgque);
  192. if (err) {
  193. printf("Failed to dump queue: %d\n", err);
  194. goto err_destroy;
  195. }
  196. err = check_and_destroy_queue(&msgque);
  197. if (err) {
  198. printf("Failed to check and destroy queue: %d\n", err);
  199. goto err_out;
  200. }
  201. err = restore_queue(&msgque);
  202. if (err) {
  203. printf("Failed to restore queue: %d\n", err);
  204. goto err_destroy;
  205. }
  206. err = check_and_destroy_queue(&msgque);
  207. if (err) {
  208. printf("Failed to test queue: %d\n", err);
  209. goto err_out;
  210. }
  211. return ksft_exit_pass();
  212. err_destroy:
  213. if (msgctl(msgque.msq_id, IPC_RMID, NULL)) {
  214. printf("Failed to destroy queue: %d\n", -errno);
  215. return ksft_exit_fail();
  216. }
  217. err_out:
  218. return ksft_exit_fail();
  219. }