1 2 #include <sys/types.h> 3 #include <sys/wait.h> 4 #include <err.h> 5 #include <fcntl.h> 6 #include <mqueue.h> 7 #include <signal.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <unistd.h> 11 12 #include "freebsd_test_suite/macros.h" 13 14 #define MQNAME "/mytstqueue2" 15 #define LOOPS 1000 16 #define PRIO 10 17 18 static void 19 alarmhandler(int sig __unused) 20 { 21 write(1, "timeout\n", 8); 22 _exit(1); 23 } 24 25 int 26 main(void) 27 { 28 struct mq_attr attr; 29 mqd_t mq; 30 int status; 31 pid_t pid; 32 33 PLAIN_REQUIRE_KERNEL_MODULE("mqueuefs", 0); 34 35 mq_unlink(MQNAME); 36 37 attr.mq_maxmsg = 5; 38 attr.mq_msgsize = 128; 39 mq = mq_open(MQNAME, O_CREAT | O_RDWR | O_EXCL, 0666, &attr); 40 if (mq == (mqd_t)-1) 41 err(1, "mq_open"); 42 status = mq_getattr(mq, &attr); 43 if (status) 44 err(1, "mq_getattr"); 45 pid = fork(); 46 if (pid == 0) { /* child */ 47 char *buf; 48 int j, i; 49 unsigned int prio; 50 51 mq_close(mq); 52 53 signal(SIGALRM, alarmhandler); 54 55 mq = mq_open(MQNAME, O_RDWR); 56 if (mq == (mqd_t)-1) 57 err(1, "child: mq_open"); 58 buf = malloc(attr.mq_msgsize); 59 for (j = 0; j < LOOPS; ++j) { 60 alarm(3); 61 status = mq_receive(mq, buf, attr.mq_msgsize, &prio); 62 if (status == -1) 63 err(2, "child: mq_receive"); 64 for (i = 0; i < attr.mq_msgsize; ++i) 65 if (buf[i] != i) 66 err(3, "child: message data corrupted"); 67 if (prio != PRIO) 68 err(4, "child: priority is incorrect: %d", 69 prio); 70 } 71 alarm(0); 72 free(buf); 73 mq_close(mq); 74 return (0); 75 } else if (pid == -1) { 76 err(1, "fork()"); 77 } else { 78 char *buf; 79 int i, j; 80 81 signal(SIGALRM, alarmhandler); 82 buf = malloc(attr.mq_msgsize); 83 for (j = 0; j < LOOPS; ++j) { 84 for (i = 0; i < attr.mq_msgsize; ++i) 85 buf[i] = i; 86 alarm(3); 87 status = mq_send(mq, buf, attr.mq_msgsize, PRIO); 88 if (status) 89 err(1, "mq_send"); 90 } 91 alarm(3); 92 wait(&status); 93 alarm(0); 94 } 95 status = mq_close(mq); 96 if (status) 97 err(1, "mq_close"); 98 mq_unlink(MQNAME); 99 return (0); 100 } 101