msgque.c 5.3 KB

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