1 2 #include <sys/types.h> 3 #include <sys/event.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 "/mytstqueue5" 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 int status; 31 struct mq_attr attr; 32 struct sigaction sa; 33 sigset_t set; 34 siginfo_t info; 35 mqd_t mq; 36 pid_t pid; 37 38 PLAIN_REQUIRE_KERNEL_MODULE("mqueuefs", 0); 39 40 mq_unlink(MQNAME); 41 42 sigemptyset(&set); 43 sigaddset(&set, SIGRTMIN); 44 sigprocmask(SIG_BLOCK, &set, NULL); 45 sigemptyset(&sa.sa_mask); 46 sa.sa_flags = SA_SIGINFO; 47 sa.sa_sigaction = (void *) SIG_DFL; 48 sigaction(SIGRTMIN, &sa, NULL); 49 50 attr.mq_maxmsg = 5; 51 attr.mq_msgsize = 128; 52 mq = mq_open(MQNAME, O_CREAT | O_RDWR | O_EXCL, 0666, &attr); 53 if (mq == (mqd_t)-1) 54 err(1, "mq_open()"); 55 status = mq_getattr(mq, &attr); 56 if (status) 57 err(1, "mq_getattr()"); 58 pid = fork(); 59 if (pid == 0) { /* child */ 60 int prio, j, i; 61 char *buf; 62 struct sigevent sigev; 63 64 signal(SIGALRM, sighandler); 65 66 sigev.sigev_notify = SIGEV_SIGNAL; 67 sigev.sigev_signo = SIGRTMIN; 68 sigev.sigev_value.sival_int = 2; 69 70 mq_close(mq); 71 mq = mq_open(MQNAME, O_RDWR | O_NONBLOCK); 72 if (mq == (mqd_t)-1) 73 err(1, "child: mq_open"); 74 buf = malloc(attr.mq_msgsize); 75 for (j = 0; j < LOOPS; ++j) { 76 alarm(3); 77 status = mq_notify(mq, &sigev); 78 if (status) 79 err(1, "child: mq_notify"); 80 status = sigwaitinfo(&set, &info); 81 if (status == -1) 82 err(1, "child: sigwaitinfo"); 83 if (info.si_value.sival_int != 2) 84 err(1, "child: sival_int"); 85 status = mq_receive(mq, buf, attr.mq_msgsize, &prio); 86 if (status == -1) 87 err(2, "child: mq_receive"); 88 for (i = 0; i < attr.mq_msgsize; ++i) 89 if (buf[i] != i) 90 err(3, "child: message data corrupted"); 91 if (prio != PRIO) 92 err(4, "child: priority is incorrect: %d", 93 prio); 94 } 95 alarm(0); 96 free(buf); 97 mq_close(mq); 98 return (0); 99 } else if (pid == -1) { 100 err(1, "fork()"); 101 } else { 102 char *buf; 103 int i, j; 104 105 signal(SIGALRM, sighandler); 106 buf = malloc(attr.mq_msgsize); 107 for (j = 0; j < LOOPS; ++j) { 108 for (i = 0; i < attr.mq_msgsize; ++i) { 109 buf[i] = i; 110 } 111 alarm(3); 112 status = mq_send(mq, buf, attr.mq_msgsize, PRIO); 113 if (status) { 114 kill(pid, SIGKILL); 115 err(2, "mq_send()"); 116 } 117 } 118 alarm(3); 119 wait(&status); 120 alarm(0); 121 } 122 status = mq_close(mq); 123 if (status) 124 err(1, "mq_close"); 125 mq_unlink(MQNAME); 126 return (0); 127 } 128