sysdeps: sem_open: Clear O_CREAT when semaphore file is expected to exist [BZ #30789]
[glibc.git] / rt / tst-mqueue.h
blob1475f45e6edd7e8884baf7900ed113b90ac3b482
1 /* Common code for message queue passing tests.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <mqueue.h>
20 #include <search.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
27 static int temp_mq_fd;
29 /* Add temporary files in list. */
30 static void
31 __attribute__ ((unused))
32 add_temp_mq (const char *name)
34 struct iovec iov[2];
35 iov[0].iov_base = (char *) name;
36 iov[0].iov_len = strlen (name);
37 iov[1].iov_base = (char *) "\n";
38 iov[1].iov_len = 1;
39 if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
40 printf ("Could not record temp mq filename %s\n", name);
43 /* Delete all temporary message queues. */
44 static void
45 do_cleanup (void)
47 if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
48 return;
50 FILE *f = fdopen (temp_mq_fd, "r");
51 if (f == NULL)
52 return;
54 char *line = NULL;
55 size_t n = 0;
56 ssize_t rets;
57 while ((rets = getline (&line, &n, f)) > 0)
59 if (line[rets - 1] != '\n')
60 continue;
62 line[rets - 1] = '\0';
63 mq_unlink (line);
65 fclose (f);
68 static void
69 do_prepare (void)
71 char name [] = "/tmp/tst-mqueueN.XXXXXX";
72 temp_mq_fd = mkstemp (name);
73 if (temp_mq_fd == -1)
75 printf ("Could not create temporary file %s: %m\n", name);
76 exit (1);
78 unlink (name);
81 #define PREPARE(argc, argv) do_prepare ()
82 #define CLEANUP_HANDLER do_cleanup ()