xref: /freebsd/tests/sys/mqueue/mqtest5.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1e100f6a2SEnji Cooper 
2e100f6a2SEnji Cooper #include <sys/types.h>
3e100f6a2SEnji Cooper #include <sys/event.h>
4e100f6a2SEnji Cooper #include <sys/select.h>
5e100f6a2SEnji Cooper #include <sys/wait.h>
6e100f6a2SEnji Cooper #include <err.h>
7e100f6a2SEnji Cooper #include <fcntl.h>
8e100f6a2SEnji Cooper #include <mqueue.h>
9e100f6a2SEnji Cooper #include <signal.h>
10e100f6a2SEnji Cooper #include <stdio.h>
11e100f6a2SEnji Cooper #include <stdlib.h>
12e100f6a2SEnji Cooper #include <unistd.h>
13e100f6a2SEnji Cooper 
14*e8395c48SEnji Cooper #include "freebsd_test_suite/macros.h"
15*e8395c48SEnji Cooper 
16e100f6a2SEnji Cooper #define MQNAME	"/mytstqueue5"
17e100f6a2SEnji Cooper #define LOOPS	1000
18e100f6a2SEnji Cooper #define PRIO	10
19e100f6a2SEnji Cooper 
20e100f6a2SEnji Cooper static void
sighandler(int sig __unused)21e100f6a2SEnji Cooper sighandler(int sig __unused)
22e100f6a2SEnji Cooper {
23e100f6a2SEnji Cooper 	write(1, "timeout\n", 8);
24e100f6a2SEnji Cooper 	_exit(1);
25e100f6a2SEnji Cooper }
26e100f6a2SEnji Cooper 
27e100f6a2SEnji Cooper int
main(void)28e100f6a2SEnji Cooper main(void)
29e100f6a2SEnji Cooper {
30e100f6a2SEnji Cooper 	int status;
31e100f6a2SEnji Cooper 	struct mq_attr attr;
32e100f6a2SEnji Cooper 	struct sigaction sa;
33e100f6a2SEnji Cooper 	sigset_t set;
34e100f6a2SEnji Cooper 	siginfo_t info;
35e100f6a2SEnji Cooper 	mqd_t mq;
36e100f6a2SEnji Cooper 	pid_t pid;
37e100f6a2SEnji Cooper 
38*e8395c48SEnji Cooper 	PLAIN_REQUIRE_KERNEL_MODULE("mqueuefs", 0);
39*e8395c48SEnji Cooper 
40e100f6a2SEnji Cooper 	mq_unlink(MQNAME);
41e100f6a2SEnji Cooper 
42e100f6a2SEnji Cooper 	sigemptyset(&set);
43e100f6a2SEnji Cooper 	sigaddset(&set, SIGRTMIN);
44e100f6a2SEnji Cooper 	sigprocmask(SIG_BLOCK, &set, NULL);
45e100f6a2SEnji Cooper 	sigemptyset(&sa.sa_mask);
46e100f6a2SEnji Cooper 	sa.sa_flags = SA_SIGINFO;
47e100f6a2SEnji Cooper 	sa.sa_sigaction = (void *) SIG_DFL;
48e100f6a2SEnji Cooper 	sigaction(SIGRTMIN, &sa, NULL);
49e100f6a2SEnji Cooper 
50e100f6a2SEnji Cooper 	attr.mq_maxmsg  = 5;
51e100f6a2SEnji Cooper 	attr.mq_msgsize = 128;
52e100f6a2SEnji Cooper 	mq = mq_open(MQNAME, O_CREAT | O_RDWR | O_EXCL, 0666, &attr);
53e100f6a2SEnji Cooper 	if (mq == (mqd_t)-1)
54e100f6a2SEnji Cooper 		err(1, "mq_open()");
55e100f6a2SEnji Cooper 	status = mq_getattr(mq, &attr);
56e100f6a2SEnji Cooper 	if (status)
57e100f6a2SEnji Cooper 		err(1, "mq_getattr()");
58e100f6a2SEnji Cooper 	pid = fork();
59e100f6a2SEnji Cooper 	if (pid == 0) { /* child */
60e100f6a2SEnji Cooper 		int prio, j, i;
61e100f6a2SEnji Cooper 		char *buf;
62e100f6a2SEnji Cooper 		struct sigevent sigev;
63e100f6a2SEnji Cooper 
64e100f6a2SEnji Cooper 		signal(SIGALRM, sighandler);
65e100f6a2SEnji Cooper 
66e100f6a2SEnji Cooper 		sigev.sigev_notify = SIGEV_SIGNAL;
67e100f6a2SEnji Cooper 		sigev.sigev_signo = SIGRTMIN;
68e100f6a2SEnji Cooper 		sigev.sigev_value.sival_int = 2;
69e100f6a2SEnji Cooper 
70e100f6a2SEnji Cooper 		mq_close(mq);
71e100f6a2SEnji Cooper 		mq = mq_open(MQNAME, O_RDWR | O_NONBLOCK);
72e100f6a2SEnji Cooper 		if (mq == (mqd_t)-1)
73e100f6a2SEnji Cooper 			err(1, "child: mq_open");
74e100f6a2SEnji Cooper 		buf = malloc(attr.mq_msgsize);
75e100f6a2SEnji Cooper 		for (j = 0; j < LOOPS; ++j) {
76e100f6a2SEnji Cooper 			alarm(3);
77e100f6a2SEnji Cooper 			status = mq_notify(mq, &sigev);
78e100f6a2SEnji Cooper 			if (status)
79e100f6a2SEnji Cooper 				err(1, "child: mq_notify");
80e100f6a2SEnji Cooper 			status = sigwaitinfo(&set, &info);
81e100f6a2SEnji Cooper 			if (status == -1)
82e100f6a2SEnji Cooper 				err(1, "child: sigwaitinfo");
83e100f6a2SEnji Cooper 			if (info.si_value.sival_int != 2)
84e100f6a2SEnji Cooper 				err(1, "child: sival_int");
85e100f6a2SEnji Cooper 			status = mq_receive(mq, buf, attr.mq_msgsize, &prio);
86e100f6a2SEnji Cooper 			if (status == -1)
87e100f6a2SEnji Cooper 				err(2, "child: mq_receive");
88e100f6a2SEnji Cooper 			for (i = 0; i < attr.mq_msgsize; ++i)
89e100f6a2SEnji Cooper 				if (buf[i] != i)
90e100f6a2SEnji Cooper 					err(3, "child: message data corrupted");
91e100f6a2SEnji Cooper 			if (prio != PRIO)
92e100f6a2SEnji Cooper 				err(4, "child: priority is incorrect: %d",
93e100f6a2SEnji Cooper 					 prio);
94e100f6a2SEnji Cooper 		}
95e100f6a2SEnji Cooper 		alarm(0);
96e100f6a2SEnji Cooper 		free(buf);
97e100f6a2SEnji Cooper 		mq_close(mq);
98e100f6a2SEnji Cooper 		return (0);
99e100f6a2SEnji Cooper 	} else if (pid == -1) {
100e100f6a2SEnji Cooper 		err(1, "fork()");
101e100f6a2SEnji Cooper 	} else {
102e100f6a2SEnji Cooper 		char *buf;
103e100f6a2SEnji Cooper 		int i, j;
104e100f6a2SEnji Cooper 
105e100f6a2SEnji Cooper 		signal(SIGALRM, sighandler);
106e100f6a2SEnji Cooper 		buf = malloc(attr.mq_msgsize);
107e100f6a2SEnji Cooper 		for (j = 0; j < LOOPS; ++j) {
108e100f6a2SEnji Cooper 			for (i = 0; i < attr.mq_msgsize; ++i) {
109e100f6a2SEnji Cooper 				buf[i] = i;
110e100f6a2SEnji Cooper 			}
111e100f6a2SEnji Cooper 			alarm(3);
112e100f6a2SEnji Cooper 			status = mq_send(mq, buf, attr.mq_msgsize, PRIO);
113e100f6a2SEnji Cooper 			if (status) {
114e100f6a2SEnji Cooper 				kill(pid, SIGKILL);
115e100f6a2SEnji Cooper 				err(2, "mq_send()");
116e100f6a2SEnji Cooper 			}
117e100f6a2SEnji Cooper 		}
118e100f6a2SEnji Cooper 		alarm(3);
119e100f6a2SEnji Cooper 		wait(&status);
120e100f6a2SEnji Cooper 		alarm(0);
121e100f6a2SEnji Cooper 	}
122e100f6a2SEnji Cooper 	status = mq_close(mq);
123e100f6a2SEnji Cooper 	if (status)
124e100f6a2SEnji Cooper 		err(1, "mq_close");
125e100f6a2SEnji Cooper 	mq_unlink(MQNAME);
126e100f6a2SEnji Cooper 	return (0);
127e100f6a2SEnji Cooper }
128