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