| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test message queue passing. | 
|  | 2 | Copyright (C) 2004-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2004. | 
|  | 5 |  | 
|  | 6 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 7 | modify it under the terms of the GNU Lesser General Public | 
|  | 8 | License as published by the Free Software Foundation; either | 
|  | 9 | version 2.1 of the License, or (at your option) any later version. | 
|  | 10 |  | 
|  | 11 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 14 | Lesser General Public License for more details. | 
|  | 15 |  | 
|  | 16 | You should have received a copy of the GNU Lesser General Public | 
|  | 17 | License along with the GNU C Library; if not, see | 
|  | 18 | <http://www.gnu.org/licenses/>.  */ | 
|  | 19 |  | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <fcntl.h> | 
|  | 22 | #include <mqueue.h> | 
|  | 23 | #include <signal.h> | 
|  | 24 | #include <stdio.h> | 
|  | 25 | #include <stdlib.h> | 
|  | 26 | #include <string.h> | 
|  | 27 | #include <sys/time.h> | 
|  | 28 | #include <sys/wait.h> | 
|  | 29 | #include <time.h> | 
|  | 30 | #include <unistd.h> | 
|  | 31 | #include "tst-mqueue.h" | 
|  | 32 |  | 
|  | 33 | static void | 
|  | 34 | alrm_handler (int sig) | 
|  | 35 | { | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | #define TIMEOUT 10 | 
|  | 39 | #define TEST_FUNCTION do_test () | 
|  | 40 | static int | 
|  | 41 | do_test (void) | 
|  | 42 | { | 
|  | 43 | int result = 0; | 
|  | 44 |  | 
|  | 45 | char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3]; | 
|  | 46 | snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ()); | 
|  | 47 |  | 
|  | 48 | struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 }; | 
|  | 49 | mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); | 
|  | 50 |  | 
|  | 51 | if (q == (mqd_t) -1) | 
|  | 52 | { | 
|  | 53 | printf ("mq_open failed with: %m\n"); | 
|  | 54 | return result; | 
|  | 55 | } | 
|  | 56 | else | 
|  | 57 | add_temp_mq (name); | 
|  | 58 |  | 
|  | 59 | mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); | 
|  | 60 | if (q2 != (mqd_t) -1) | 
|  | 61 | { | 
|  | 62 | puts ("mq_open with O_EXCL unexpectedly succeeded"); | 
|  | 63 | result = 1; | 
|  | 64 | } | 
|  | 65 | else if (errno != EEXIST) | 
|  | 66 | { | 
|  | 67 | printf ("mq_open did not fail with EEXIST: %m\n"); | 
|  | 68 | result = 1; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3]; | 
|  | 72 | snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ()); | 
|  | 73 |  | 
|  | 74 | attr.mq_maxmsg = -2; | 
|  | 75 | q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); | 
|  | 76 | if (q2 != (mqd_t) -1) | 
|  | 77 | { | 
|  | 78 | puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded"); | 
|  | 79 | add_temp_mq (name2); | 
|  | 80 | result = 1; | 
|  | 81 | } | 
|  | 82 | else if (errno != EINVAL) | 
|  | 83 | { | 
|  | 84 | printf ("mq_open with invalid mq_maxmsg did not fail with " | 
|  | 85 | "EINVAL: %m\n"); | 
|  | 86 | result = 1; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | attr.mq_maxmsg = 2; | 
|  | 90 | attr.mq_msgsize = -56; | 
|  | 91 | q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); | 
|  | 92 | if (q2 != (mqd_t) -1) | 
|  | 93 | { | 
|  | 94 | puts ("mq_open with invalid mq_msgsize unexpectedly succeeded"); | 
|  | 95 | add_temp_mq (name2); | 
|  | 96 | result = 1; | 
|  | 97 | } | 
|  | 98 | else if (errno != EINVAL) | 
|  | 99 | { | 
|  | 100 | printf ("mq_open with invalid mq_msgsize did not fail with " | 
|  | 101 | "EINVAL: %m\n"); | 
|  | 102 | result = 1; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | char buf[3]; | 
|  | 106 | struct timespec ts; | 
|  | 107 | if (clock_gettime (CLOCK_REALTIME, &ts) == 0) | 
|  | 108 | ts.tv_sec += 10; | 
|  | 109 | else | 
|  | 110 | { | 
|  | 111 | ts.tv_sec = time (NULL) + 10; | 
|  | 112 | ts.tv_nsec = 0; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0) | 
|  | 116 | { | 
|  | 117 | puts ("mq_timedreceive with too small msg_len did not fail"); | 
|  | 118 | result = 1; | 
|  | 119 | } | 
|  | 120 | else if (errno != EMSGSIZE) | 
|  | 121 | { | 
|  | 122 | printf ("mq_timedreceive with too small msg_len did not fail with " | 
|  | 123 | "EMSGSIZE: %m\n"); | 
|  | 124 | result = 1; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | ts.tv_nsec = -1; | 
|  | 128 | if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) | 
|  | 129 | { | 
|  | 130 | puts ("mq_timedreceive with negative tv_nsec did not fail"); | 
|  | 131 | result = 1; | 
|  | 132 | } | 
|  | 133 | else if (errno != EINVAL) | 
|  | 134 | { | 
|  | 135 | printf ("mq_timedreceive with negative tv_nsec did not fail with " | 
|  | 136 | "EINVAL: %m\n"); | 
|  | 137 | result = 1; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | ts.tv_nsec = 1000000000; | 
|  | 141 | if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) | 
|  | 142 | { | 
|  | 143 | puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail"); | 
|  | 144 | result = 1; | 
|  | 145 | } | 
|  | 146 | else if (errno != EINVAL) | 
|  | 147 | { | 
|  | 148 | printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with " | 
|  | 149 | "EINVAL: %m\n"); | 
|  | 150 | result = 1; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 }; | 
|  | 154 | sigemptyset (&sa.sa_mask); | 
|  | 155 | sigaction (SIGALRM, &sa, NULL); | 
|  | 156 |  | 
|  | 157 | struct itimerval it = { .it_value = { .tv_sec = 1 } }; | 
|  | 158 | setitimer (ITIMER_REAL, &it, NULL); | 
|  | 159 |  | 
|  | 160 | if (mq_receive (q, buf, 2, NULL) == 0) | 
|  | 161 | { | 
|  | 162 | puts ("mq_receive on empty queue did not block"); | 
|  | 163 | result = 1; | 
|  | 164 | } | 
|  | 165 | else if (errno != EINTR) | 
|  | 166 | { | 
|  | 167 | printf ("mq_receive on empty queue did not fail with EINTR: %m\n"); | 
|  | 168 | result = 1; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | setitimer (ITIMER_REAL, &it, NULL); | 
|  | 172 |  | 
|  | 173 | ts.tv_nsec = 0; | 
|  | 174 | if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) | 
|  | 175 | { | 
|  | 176 | puts ("mq_timedreceive on empty queue did not block"); | 
|  | 177 | result = 1; | 
|  | 178 | } | 
|  | 179 | else if (errno != EINTR) | 
|  | 180 | { | 
|  | 181 | printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n"); | 
|  | 182 | result = 1; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | buf[0] = '6'; | 
|  | 186 | buf[1] = '7'; | 
|  | 187 | if (mq_send (q, buf, 2, 3) != 0 | 
|  | 188 | || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0)) | 
|  | 189 | { | 
|  | 190 | printf ("mq_send failed: %m\n"); | 
|  | 191 | result = 1; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | memset (buf, ' ', sizeof (buf)); | 
|  | 195 |  | 
|  | 196 | unsigned int prio; | 
|  | 197 | ssize_t rets = mq_receive (q, buf, 3, &prio); | 
|  | 198 | if (rets != 1) | 
|  | 199 | { | 
|  | 200 | if (rets == -1) | 
|  | 201 | printf ("mq_receive failed: %m\n"); | 
|  | 202 | else | 
|  | 203 | printf ("mq_receive returned %zd != 1\n", rets); | 
|  | 204 | result = 1; | 
|  | 205 | } | 
|  | 206 | else if (prio != 4 || memcmp (buf, "8  ", 3) != 0) | 
|  | 207 | { | 
|  | 208 | printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8  \")\n", | 
|  | 209 | prio, buf[0], buf[1], buf[2]); | 
|  | 210 | result = 1; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | rets = mq_receive (q, buf, 2, NULL); | 
|  | 214 | if (rets != 2) | 
|  | 215 | { | 
|  | 216 | if (rets == -1) | 
|  | 217 | printf ("mq_receive failed: %m\n"); | 
|  | 218 | else | 
|  | 219 | printf ("mq_receive returned %zd != 2\n", rets); | 
|  | 220 | result = 1; | 
|  | 221 | } | 
|  | 222 | else if (memcmp (buf, "67 ", 3) != 0) | 
|  | 223 | { | 
|  | 224 | printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n", | 
|  | 225 | buf[0], buf[1], buf[2]); | 
|  | 226 | result = 1; | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | buf[0] = '2'; | 
|  | 230 | buf[1] = '1'; | 
|  | 231 | if (clock_gettime (CLOCK_REALTIME, &ts) != 0) | 
|  | 232 | ts.tv_sec = time (NULL); | 
|  | 233 | ts.tv_nsec = -1000000001; | 
|  | 234 | if ((mq_timedsend (q, buf, 2, 5, &ts) != 0 | 
|  | 235 | && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0)) | 
|  | 236 | || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec, | 
|  | 237 | (mq_timedsend (q, buf, 1, 4, &ts) != 0 | 
|  | 238 | && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0)))) | 
|  | 239 | { | 
|  | 240 | printf ("mq_timedsend failed: %m\n"); | 
|  | 241 | result = 1; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | buf[0] = '-'; | 
|  | 245 | ts.tv_nsec = 1000000001; | 
|  | 246 | if (mq_timedsend (q, buf, 1, 6, &ts) == 0) | 
|  | 247 | { | 
|  | 248 | puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail"); | 
|  | 249 | result = 1; | 
|  | 250 | } | 
|  | 251 | else if (errno != EINVAL) | 
|  | 252 | { | 
|  | 253 | printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with " | 
|  | 254 | "EINVAL: %m\n"); | 
|  | 255 | result = 1; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | ts.tv_nsec = -2; | 
|  | 259 | if (mq_timedsend (q, buf, 1, 6, &ts) == 0) | 
|  | 260 | { | 
|  | 261 | puts ("mq_timedsend with negative tv_nsec did not fail"); | 
|  | 262 | result = 1; | 
|  | 263 | } | 
|  | 264 | else if (errno != EINVAL) | 
|  | 265 | { | 
|  | 266 | printf ("mq_timedsend with megatove tv_nsec did not fail with " | 
|  | 267 | "EINVAL: %m\n"); | 
|  | 268 | result = 1; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | setitimer (ITIMER_REAL, &it, NULL); | 
|  | 272 |  | 
|  | 273 | if (mq_send (q, buf, 2, 8) == 0) | 
|  | 274 | { | 
|  | 275 | puts ("mq_send on full queue did not block"); | 
|  | 276 | result = 1; | 
|  | 277 | } | 
|  | 278 | else if (errno != EINTR) | 
|  | 279 | { | 
|  | 280 | printf ("mq_send on full queue did not fail with EINTR: %m\n"); | 
|  | 281 | result = 1; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | setitimer (ITIMER_REAL, &it, NULL); | 
|  | 285 |  | 
|  | 286 | ts.tv_sec += 10; | 
|  | 287 | ts.tv_nsec = 0; | 
|  | 288 | if (mq_timedsend (q, buf, 2, 7, &ts) == 0) | 
|  | 289 | { | 
|  | 290 | puts ("mq_timedsend on full queue did not block"); | 
|  | 291 | result = 1; | 
|  | 292 | } | 
|  | 293 | else if (errno != EINTR) | 
|  | 294 | { | 
|  | 295 | printf ("mq_timedsend on full queue did not fail with EINTR: %m\n"); | 
|  | 296 | result = 1; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | memset (buf, ' ', sizeof (buf)); | 
|  | 300 |  | 
|  | 301 | if (clock_gettime (CLOCK_REALTIME, &ts) != 0) | 
|  | 302 | ts.tv_sec = time (NULL); | 
|  | 303 | ts.tv_nsec = -1000000001; | 
|  | 304 | rets = mq_timedreceive (q, buf, 2, &prio, &ts); | 
|  | 305 | if (rets == -1 && errno == EINVAL) | 
|  | 306 | rets = mq_receive (q, buf, 2, &prio); | 
|  | 307 | if (rets != 2) | 
|  | 308 | { | 
|  | 309 | if (rets == -1) | 
|  | 310 | printf ("mq_timedreceive failed: %m\n"); | 
|  | 311 | else | 
|  | 312 | printf ("mq_timedreceive returned %zd != 2\n", rets); | 
|  | 313 | result = 1; | 
|  | 314 | } | 
|  | 315 | else if (prio != 5 || memcmp (buf, "21 ", 3) != 0) | 
|  | 316 | { | 
|  | 317 | printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n", | 
|  | 318 | prio, buf[0], buf[1], buf[2]); | 
|  | 319 | result = 1; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | if (mq_receive (q, buf, 1, NULL) == 0) | 
|  | 323 | { | 
|  | 324 | puts ("mq_receive with too small msg_len did not fail"); | 
|  | 325 | result = 1; | 
|  | 326 | } | 
|  | 327 | else if (errno != EMSGSIZE) | 
|  | 328 | { | 
|  | 329 | printf ("mq_receive with too small msg_len did not fail with " | 
|  | 330 | "EMSGSIZE: %m\n"); | 
|  | 331 | result = 1; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | ts.tv_nsec = -ts.tv_nsec; | 
|  | 335 | rets = mq_timedreceive (q, buf, 2, NULL, &ts); | 
|  | 336 | if (rets == -1 && errno == EINVAL) | 
|  | 337 | rets = mq_receive (q, buf, 2, NULL); | 
|  | 338 | if (rets != 1) | 
|  | 339 | { | 
|  | 340 | if (rets == -1) | 
|  | 341 | printf ("mq_timedreceive failed: %m\n"); | 
|  | 342 | else | 
|  | 343 | printf ("mq_timedreceive returned %zd != 1\n", rets); | 
|  | 344 | result = 1; | 
|  | 345 | } | 
|  | 346 | else if (memcmp (buf, "31 ", 3) != 0) | 
|  | 347 | { | 
|  | 348 | printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n", | 
|  | 349 | buf[0], buf[1], buf[2]); | 
|  | 350 | result = 1; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | if (mq_send (q, "", 0, 2) != 0) | 
|  | 354 | { | 
|  | 355 | printf ("mq_send with msg_len 0 failed: %m\n"); | 
|  | 356 | result = 1; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | rets = mq_receive (q, buf, 2, &prio); | 
|  | 360 | if (rets) | 
|  | 361 | { | 
|  | 362 | if (rets == -1) | 
|  | 363 | printf ("mq_receive failed: %m\n"); | 
|  | 364 | else | 
|  | 365 | printf ("mq_receive returned %zd != 0\n", rets); | 
|  | 366 | result = 1; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX); | 
|  | 370 | if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max) | 
|  | 371 | { | 
|  | 372 | if (mq_send (q, buf, 1, mq_prio_max) == 0) | 
|  | 373 | { | 
|  | 374 | puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded"); | 
|  | 375 | result = 1; | 
|  | 376 | } | 
|  | 377 | else if (errno != EINVAL) | 
|  | 378 | { | 
|  | 379 | printf ("mq_send with MQ_PRIO_MAX priority did not fail with " | 
|  | 380 | "EINVAL: %m\n"); | 
|  | 381 | result = 1; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | if (mq_send (q, buf, 1, mq_prio_max - 1) != 0) | 
|  | 385 | { | 
|  | 386 | printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n"); | 
|  | 387 | result = 1; | 
|  | 388 | } | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | if (mq_unlink (name) != 0) | 
|  | 392 | { | 
|  | 393 | printf ("mq_unlink failed: %m\n"); | 
|  | 394 | result = 1; | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | q2 = mq_open (name, O_RDWR); | 
|  | 398 | if (q2 != (mqd_t) -1) | 
|  | 399 | { | 
|  | 400 | printf ("mq_open of unlinked %s without O_CREAT unexpectedly" | 
|  | 401 | "succeeded\n", name); | 
|  | 402 | result = 1; | 
|  | 403 | } | 
|  | 404 | else if (errno != ENOENT) | 
|  | 405 | { | 
|  | 406 | printf ("mq_open of unlinked %s without O_CREAT did not fail with " | 
|  | 407 | "ENOENT: %m\n", name); | 
|  | 408 | result = 1; | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | if (mq_close (q) != 0) | 
|  | 412 | { | 
|  | 413 | printf ("mq_close in parent failed: %m\n"); | 
|  | 414 | result = 1; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | if (mq_receive (q, buf, 2, NULL) == 0) | 
|  | 418 | { | 
|  | 419 | puts ("mq_receive on invalid mqd_t did not fail"); | 
|  | 420 | result = 1; | 
|  | 421 | } | 
|  | 422 | else if (errno != EBADF) | 
|  | 423 | { | 
|  | 424 | printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n"); | 
|  | 425 | result = 1; | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | if (mq_send (q, buf, 1, 2) == 0) | 
|  | 429 | { | 
|  | 430 | puts ("mq_send on invalid mqd_t did not fail"); | 
|  | 431 | result = 1; | 
|  | 432 | } | 
|  | 433 | else if (errno != EBADF) | 
|  | 434 | { | 
|  | 435 | printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n"); | 
|  | 436 | result = 1; | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | if (mq_getattr (q, &attr) == 0) | 
|  | 440 | { | 
|  | 441 | puts ("mq_getattr on invalid mqd_t did not fail"); | 
|  | 442 | result = 1; | 
|  | 443 | } | 
|  | 444 | else if (errno != EBADF) | 
|  | 445 | { | 
|  | 446 | printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n"); | 
|  | 447 | result = 1; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | memset (&attr, 0, sizeof (attr)); | 
|  | 451 | if (mq_setattr (q, &attr, NULL) == 0) | 
|  | 452 | { | 
|  | 453 | puts ("mq_setattr on invalid mqd_t did not fail"); | 
|  | 454 | result = 1; | 
|  | 455 | } | 
|  | 456 | else if (errno != EBADF) | 
|  | 457 | { | 
|  | 458 | printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n"); | 
|  | 459 | result = 1; | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1) | 
|  | 463 | { | 
|  | 464 | puts ("mq_unlink of non-existant message queue unexpectedly succeeded"); | 
|  | 465 | result = 1; | 
|  | 466 | } | 
|  | 467 | else if (errno != ENOENT) | 
|  | 468 | { | 
|  | 469 | printf ("mq_unlink of non-existant message queue did not fail with " | 
|  | 470 | "ENOENT: %m\n"); | 
|  | 471 | result = 1; | 
|  | 472 | } | 
|  | 473 | return result; | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | #include "../test-skeleton.c" |