msgque.c 5.3 KB

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