]>
Commit | Line | Data |
---|---|---|
1b82c6c7 | 1 | /* Common code for message queue passing tests. |
2b778ceb | 2 | Copyright (C) 2004-2021 Free Software Foundation, Inc. |
1b82c6c7 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Jakub Jelinek <jakub@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 | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
1b82c6c7 UD |
19 | |
20 | #include <mqueue.h> | |
21 | #include <search.h> | |
22 | #include <stdlib.h> | |
efa8adf5 | 23 | #include <stdio.h> |
1b82c6c7 | 24 | #include <string.h> |
efa8adf5 UD |
25 | #include <sys/uio.h> |
26 | #include <unistd.h> | |
1b82c6c7 | 27 | |
efa8adf5 | 28 | static int temp_mq_fd; |
1b82c6c7 UD |
29 | |
30 | /* Add temporary files in list. */ | |
31 | static void | |
32 | __attribute__ ((unused)) | |
33 | add_temp_mq (const char *name) | |
34 | { | |
efa8adf5 UD |
35 | struct iovec iov[2]; |
36 | iov[0].iov_base = (char *) name; | |
37 | iov[0].iov_len = strlen (name); | |
38 | iov[1].iov_base = (char *) "\n"; | |
39 | iov[1].iov_len = 1; | |
40 | if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1) | |
41 | printf ("Could not record temp mq filename %s\n", name); | |
42 | } | |
43 | ||
44 | /* Delete all temporary message queues. */ | |
45 | static void | |
46 | do_cleanup (void) | |
47 | { | |
48 | if (lseek (temp_mq_fd, 0, SEEK_SET) != 0) | |
49 | return; | |
50 | ||
51 | FILE *f = fdopen (temp_mq_fd, "r"); | |
52 | if (f == NULL) | |
53 | return; | |
54 | ||
55 | char *line = NULL; | |
56 | size_t n = 0; | |
57 | ssize_t rets; | |
58 | while ((rets = getline (&line, &n, f)) > 0) | |
1b82c6c7 | 59 | { |
efa8adf5 UD |
60 | if (line[rets - 1] != '\n') |
61 | continue; | |
62 | ||
63 | line[rets - 1] = '\0'; | |
64 | mq_unlink (line); | |
1b82c6c7 | 65 | } |
efa8adf5 | 66 | fclose (f); |
1b82c6c7 UD |
67 | } |
68 | ||
1b82c6c7 | 69 | static void |
efa8adf5 | 70 | do_prepare (void) |
1b82c6c7 | 71 | { |
efa8adf5 UD |
72 | char name [] = "/tmp/tst-mqueueN.XXXXXX"; |
73 | temp_mq_fd = mkstemp (name); | |
74 | if (temp_mq_fd == -1) | |
1b82c6c7 | 75 | { |
efa8adf5 UD |
76 | printf ("Could not create temporary file %s: %m\n", name); |
77 | exit (1); | |
1b82c6c7 | 78 | } |
efa8adf5 | 79 | unlink (name); |
1b82c6c7 UD |
80 | } |
81 | ||
efa8adf5 UD |
82 | #define PREPARE(argc, argv) do_prepare () |
83 | #define CLEANUP_HANDLER do_cleanup () |