mq.c revision 663860b1408516d02ebfcb3a9999a134e6cfb223
1#include <config.h>
2#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <fcntl.h>
7
8#ifdef HAVE_MQUEUE_H
9
10#include <mqueue.h>
11
12#define MSGMAX 10
13#define MSGSIZEMAX 1024
14
15int main(int argc, char **argv)
16{
17  struct mq_attr mqa;
18  mqd_t mqdw;
19  mqd_t mqdr;
20  char buffer[MSGSIZEMAX];
21  unsigned int priority;
22  int len;
23
24  mqa.mq_maxmsg = MSGMAX;
25  mqa.mq_msgsize = MSGSIZEMAX;
26
27  if ((mqdw = mq_open("/valgrind-mqueue", O_CREAT|O_EXCL|O_WRONLY, 0600, &mqa)) < 0)
28    {
29      if (errno == ENOSYS)
30        exit(0);
31      perror("mq_open");
32      exit(1);
33    }
34
35  if ((mqdr = mq_open("/valgrind-mqueue", O_RDONLY)) < 0)
36    {
37      perror("mq_open");
38      mq_unlink("/valgrind-mqueue");
39      mq_close(mqdw);
40      exit(1);
41    }
42
43  if (mq_unlink("/valgrind-mqueue") < 0)
44    {
45      perror("mq_unlink");
46      mq_close(mqdw);
47      mq_close(mqdr);
48      exit(1);
49    }
50
51  if (mq_send(mqdw, "PING", 4, 0) < 0)
52    {
53      perror("mq_send");
54      mq_close(mqdr);
55      mq_close(mqdw);
56      exit(1);
57    }
58
59  if ((len = mq_receive(mqdr, buffer, sizeof(buffer), &priority)) < 0)
60    {
61      perror("mq_receive");
62      mq_close(mqdr);
63      mq_close(mqdw);
64      exit(1);
65    }
66
67  if (len != 4 || memcmp(buffer, "PING", 4) != 0)
68    {
69      fprintf(stderr, "Message corrupt!");
70    }
71
72  if (mq_notify(mqdr, NULL) < 0)
73    {
74      perror("mq_notify");
75      mq_close(mqdr);
76      mq_close(mqdw);
77      exit(1);
78    }
79
80  if (mq_getattr(mqdr, &mqa) < 0)
81    {
82      perror("mq_getattr");
83      mq_close(mqdr);
84      mq_close(mqdw);
85      exit(1);
86    }
87
88  if (mq_setattr(mqdw, &mqa, &mqa) < 0)
89    {
90      perror("mq_setattr");
91      mq_close(mqdr);
92      mq_close(mqdw);
93      exit(1);
94    }
95
96  if (mq_close(mqdr) < 0)
97    {
98      perror("mq_close");
99      mq_close(mqdw);
100      exit(1);
101    }
102
103  if (mq_close(mqdw) < 0)
104    {
105      perror("mq_close");
106      exit(1);
107    }
108
109  exit(0);
110}
111
112#else
113
114int main(int argc, char **argv)
115{
116  exit(0);
117}
118
119#endif
120