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
main(int argc,char ** argv)15 int 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 !defined(VGO_solaris)
68 /* On Solaris, there is no existing notification registration. */
69 if (mq_notify(mqdr, NULL) < 0)
70 {
71 perror("mq_notify");
72 mq_close(mqdr);
73 mq_close(mqdw);
74 exit(1);
75 }
76 #endif /* !VGO_solaris */
77
78 if (len != 4 || memcmp(buffer, "PING", 4) != 0)
79 {
80 fprintf(stderr, "Message corrupt!");
81 }
82
83 if (mq_getattr(mqdr, &mqa) < 0)
84 {
85 perror("mq_getattr");
86 mq_close(mqdr);
87 mq_close(mqdw);
88 exit(1);
89 }
90
91 if (mq_setattr(mqdw, &mqa, &mqa) < 0)
92 {
93 perror("mq_setattr");
94 mq_close(mqdr);
95 mq_close(mqdw);
96 exit(1);
97 }
98
99 if (mq_close(mqdr) < 0)
100 {
101 perror("mq_close");
102 mq_close(mqdw);
103 exit(1);
104 }
105
106 if (mq_close(mqdw) < 0)
107 {
108 perror("mq_close");
109 exit(1);
110 }
111
112 exit(0);
113 }
114
115 #else
116
main(int argc,char ** argv)117 int main(int argc, char **argv)
118 {
119 exit(0);
120 }
121
122 #endif
123