summaryrefslogtreecommitdiffstats
path: root/tests/sys/mqueue/mqtest3.c
blob: 4ee812c6f47357eaa638b9df08c700b0648cf736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* $FreeBSD$ */

#include <sys/types.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <err.h>
#include <fcntl.h>
#include <mqueue.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MQNAME	"/mytstqueue3"
#define LOOPS	1000
#define PRIO	10

static void
sighandler(int sig __unused)
{
	write(1, "timeout\n", 8);
	_exit(1);
}

int
main(void)
{
	fd_set set;
	struct mq_attr attr;
	int status;
	mqd_t mq;
	pid_t pid;

	mq_unlink(MQNAME);

	attr.mq_maxmsg  = 5;
	attr.mq_msgsize = 128;
	mq = mq_open(MQNAME, O_CREAT | O_RDWR | O_EXCL, 0666, &attr);
	if (mq == (mqd_t)-1)
		err(1, "mq_open()");
	status = mq_getattr(mq, &attr);
	if (status)
		err(1, "mq_getattr()");
	
	pid = fork();
	if (pid == 0) { /* child */
		char *buf;
		int j, i;
		unsigned int prio;

		mq_close(mq);

		signal(SIGALRM, sighandler);

		mq = mq_open(MQNAME, O_RDWR);
		if (mq == (mqd_t)-1)
			err(1, "child process: mq_open");
		buf = malloc(attr.mq_msgsize);
		for (j = 0; j < LOOPS; ++j) {
			FD_ZERO(&set);
			FD_SET(__mq_oshandle(mq), &set);
			alarm(3);
			status = select(__mq_oshandle(mq)+1, &set, NULL, NULL, NULL);
			if (status != 1)
				err(1, "child process: select()");
			status = mq_receive(mq, buf, attr.mq_msgsize, &prio);
			if (status == -1)
				err(2, "child process: mq_receive");
			for (i = 0; i < attr.mq_msgsize; ++i)
				if (buf[i] != i)
					err(3, "message data corrupted");
			if (prio != PRIO)
				err(4, "priority is incorrect: %d", prio);
		}
		alarm(0);
		free(buf);
		mq_close(mq);
		return (0);
	} else if (pid == -1) {
		err(1, "fork()");
	} else {
		char *buf;
		int i, j;

		signal(SIGALRM, sighandler);
		buf = malloc(attr.mq_msgsize);
		for (j = 0; j < LOOPS; ++j) {
			for (i = 0; i < attr.mq_msgsize; ++i) {
				buf[i] = i;
			}
			alarm(3);
			FD_ZERO(&set);
			FD_SET(__mq_oshandle(mq), &set);
			status = select(__mq_oshandle(mq)+1, NULL, &set, NULL, NULL);
			if (status != 1)
				err(1, "select()");
			status = mq_send(mq, buf, attr.mq_msgsize, PRIO);
			if (status) {
				kill(pid, SIGKILL);
				err(2, "mq_send()");
			}
		}
		alarm(3);
		wait(&status);
		alarm(0);
	}
	status = mq_close(mq);
	if (status)
		err(1, "mq_close");
	mq_unlink(MQNAME);
	return (0);
}
OpenPOWER on IntegriCloud