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