1/* Common code for message queue passing tests.
2 Copyright (C) 2004-2022 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 <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>
26
27static int temp_mq_fd;
28
29/* Add temporary files in list. */
30static void
31__attribute__ ((unused))
32add_temp_mq (const char *name)
33{
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 (fd: temp_mq_fd, iovec: iov, count: 2) != iov[0].iov_len + 1)
40 printf (format: "Could not record temp mq filename %s\n", name);
41}
42
43/* Delete all temporary message queues. */
44static void
45do_cleanup (void)
46{
47 if (lseek (fd: temp_mq_fd, offset: 0, SEEK_SET) != 0)
48 return;
49
50 FILE *f = fdopen (temp_mq_fd, "r");
51 if (f == NULL)
52 return;
53
54 char *line = NULL;
55 size_t n = 0;
56 ssize_t rets;
57 while ((rets = getline (lineptr: &line, n: &n, stream: f)) > 0)
58 {
59 if (line[rets - 1] != '\n')
60 continue;
61
62 line[rets - 1] = '\0';
63 mq_unlink (name: line);
64 }
65 fclose (f);
66}
67
68static void
69do_prepare (void)
70{
71 char name [] = "/tmp/tst-mqueueN.XXXXXX";
72 temp_mq_fd = mkstemp (template: name);
73 if (temp_mq_fd == -1)
74 {
75 printf (format: "Could not create temporary file %s: %m\n", name);
76 exit (1);
77 }
78 unlink (name: name);
79}
80
81#define PREPARE(argc, argv) do_prepare ()
82#define CLEANUP_HANDLER do_cleanup ()
83

source code of glibc/rt/tst-mqueue.h