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