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