1 | /* Test SIGEV_THREAD handling for POSIX message queues.
|
---|
2 | Copyright (C) 2004 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
|
---|
5 |
|
---|
6 | The GNU C Library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with the GNU C Library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <errno.h>
|
---|
22 | #include <mqueue.h>
|
---|
23 | #include <signal.h>
|
---|
24 | #include <stddef.h>
|
---|
25 | #include <stdint.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <sys/mman.h>
|
---|
30 | #include <sys/wait.h>
|
---|
31 | #include <unistd.h>
|
---|
32 |
|
---|
33 | #if _POSIX_THREADS
|
---|
34 | # include <pthread.h>
|
---|
35 |
|
---|
36 | static pid_t pid;
|
---|
37 | static mqd_t m;
|
---|
38 | static const char message[] = "hello";
|
---|
39 |
|
---|
40 | # define MAXMSG 10
|
---|
41 | # define MSGSIZE 10
|
---|
42 | # define UNIQUE 42
|
---|
43 |
|
---|
44 |
|
---|
45 | static void
|
---|
46 | fct (union sigval s)
|
---|
47 | {
|
---|
48 | /* Put the mq in non-blocking mode. */
|
---|
49 | struct mq_attr attr;
|
---|
50 | if (mq_getattr (m, &attr) != 0)
|
---|
51 | {
|
---|
52 | printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
|
---|
53 | exit (1);
|
---|
54 | }
|
---|
55 | attr.mq_flags |= O_NONBLOCK;
|
---|
56 | if (mq_setattr (m, &attr, NULL) != 0)
|
---|
57 | {
|
---|
58 | printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
|
---|
59 | exit (1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /* Check the values. */
|
---|
63 | if (attr.mq_maxmsg != MAXMSG)
|
---|
64 | {
|
---|
65 | printf ("%s: mq_maxmsg wrong: is %ld, expecte %d\n",
|
---|
66 | __FUNCTION__, attr.mq_maxmsg, MAXMSG);
|
---|
67 | exit (1);
|
---|
68 | }
|
---|
69 | if (attr.mq_msgsize != MAXMSG)
|
---|
70 | {
|
---|
71 | printf ("%s: mq_msgsize wrong: is %ld, expecte %d\n",
|
---|
72 | __FUNCTION__, attr.mq_msgsize, MSGSIZE);
|
---|
73 | exit (1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /* Read the message. */
|
---|
77 | char buf[attr.mq_msgsize];
|
---|
78 | ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
|
---|
79 | if (n != sizeof (message))
|
---|
80 | {
|
---|
81 | printf ("%s: length of message wrong: is %zd, expected %zu\n",
|
---|
82 | __FUNCTION__, n, sizeof (message));
|
---|
83 | exit (1);
|
---|
84 | }
|
---|
85 | if (memcmp (buf, message, sizeof (message)) != 0)
|
---|
86 | {
|
---|
87 | printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
|
---|
88 | __FUNCTION__, buf, message);
|
---|
89 | exit (1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | exit (UNIQUE);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | int
|
---|
97 | do_test (void)
|
---|
98 | {
|
---|
99 | char tmpfname[] = "/tmp/tst-mqueue3-barrier.XXXXXX";
|
---|
100 | int fd = mkstemp (tmpfname);
|
---|
101 | if (fd == -1)
|
---|
102 | {
|
---|
103 | printf ("cannot open temporary file: %m\n");
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* Make sure it is always removed. */
|
---|
108 | unlink (tmpfname);
|
---|
109 |
|
---|
110 | /* Create one page of data. */
|
---|
111 | size_t ps = sysconf (_SC_PAGESIZE);
|
---|
112 | char data[ps];
|
---|
113 | memset (data, '\0', ps);
|
---|
114 |
|
---|
115 | /* Write the data to the file. */
|
---|
116 | if (write (fd, data, ps) != (ssize_t) ps)
|
---|
117 | {
|
---|
118 | puts ("short write");
|
---|
119 | return 1;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
---|
123 | if (mem == MAP_FAILED)
|
---|
124 | {
|
---|
125 | printf ("mmap failed: %m\n");
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | pthread_barrier_t *b;
|
---|
130 | b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
|
---|
131 | & ~(__alignof (pthread_barrier_t) - 1));
|
---|
132 |
|
---|
133 | pthread_barrierattr_t a;
|
---|
134 | if (pthread_barrierattr_init (&a) != 0)
|
---|
135 | {
|
---|
136 | puts ("barrierattr_init failed");
|
---|
137 | return 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
|
---|
141 | {
|
---|
142 | puts ("barrierattr_setpshared failed, could not test");
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (pthread_barrier_init (b, &a, 2) != 0)
|
---|
147 | {
|
---|
148 | puts ("barrier_init failed");
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (pthread_barrierattr_destroy (&a) != 0)
|
---|
153 | {
|
---|
154 | puts ("barrierattr_destroy failed");
|
---|
155 | return 1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* Name for the message queue. */
|
---|
159 | char mqname[sizeof ("/tst-mqueue3-") + 3 * sizeof (pid_t)];
|
---|
160 | snprintf (mqname, sizeof (mqname) - 1, "/tst-mqueue3-%ld",
|
---|
161 | (long int) getpid ());
|
---|
162 |
|
---|
163 | /* Create the message queue. */
|
---|
164 | struct mq_attr attr = { .mq_maxmsg = MAXMSG, .mq_msgsize = MSGSIZE };
|
---|
165 | m = mq_open (mqname, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
|
---|
166 | if (m == -1)
|
---|
167 | {
|
---|
168 | if (errno == ENOSYS)
|
---|
169 | {
|
---|
170 | puts ("not implemented");
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | puts ("mq_open failed");
|
---|
175 | return 1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Unlink the message queue right away. */
|
---|
179 | if (mq_unlink (mqname) != 0)
|
---|
180 | {
|
---|
181 | puts ("mq_unlink failed");
|
---|
182 | return 1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | pid = fork ();
|
---|
186 | if (pid == -1)
|
---|
187 | {
|
---|
188 | puts ("fork failed");
|
---|
189 | return 1;
|
---|
190 | }
|
---|
191 | if (pid == 0)
|
---|
192 | {
|
---|
193 | /* Request notification via thread. */
|
---|
194 | struct sigevent ev;
|
---|
195 | ev.sigev_notify = SIGEV_THREAD;
|
---|
196 | ev.sigev_notify_function = fct;
|
---|
197 | ev.sigev_value.sival_ptr = NULL;
|
---|
198 | ev.sigev_notify_attributes = NULL;
|
---|
199 |
|
---|
200 | /* Tell the kernel. */
|
---|
201 | if (mq_notify (m,&ev) != 0)
|
---|
202 | {
|
---|
203 | puts ("mq_notify failed");
|
---|
204 | exit (1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* Tell the parent we are ready. */
|
---|
208 | (void) pthread_barrier_wait (b);
|
---|
209 |
|
---|
210 | /* Make sure the process goes away eventually. */
|
---|
211 | alarm (10);
|
---|
212 |
|
---|
213 | /* Do nothing forever. */
|
---|
214 | while (1)
|
---|
215 | pause ();
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* Wait for the child process to register to notification method. */
|
---|
219 | (void) pthread_barrier_wait (b);
|
---|
220 |
|
---|
221 | /* Send the message. */
|
---|
222 | if (mq_send (m, message, sizeof (message), 1) != 0)
|
---|
223 | {
|
---|
224 | kill (pid, SIGKILL);
|
---|
225 | puts ("mq_send failed");
|
---|
226 | return 1;
|
---|
227 | }
|
---|
228 |
|
---|
229 | int r;
|
---|
230 | if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
|
---|
231 | {
|
---|
232 | kill (pid, SIGKILL);
|
---|
233 | puts ("waitpid failed");
|
---|
234 | return 1;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
|
---|
238 | }
|
---|
239 | # define TEST_FUNCTION do_test ()
|
---|
240 | #else
|
---|
241 | # define TEST_FUNCTION 0
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | #include "../test-skeleton.c"
|
---|