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