chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / rt / tst-mqueue.h
1 /* Common code for message queue passing tests.
2    Copyright (C) 2004 Free Software Foundation, Inc.
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
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 <mqueue.h>
22 #include <search.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/uio.h>
27 #include <unistd.h>
28
29 static int temp_mq_fd;
30
31 /* Add temporary files in list.  */
32 static void
33 __attribute__ ((unused))
34 add_temp_mq (const char *name)
35 {
36   struct iovec iov[2];
37   iov[0].iov_base = (char *) name;
38   iov[0].iov_len = strlen (name);
39   iov[1].iov_base = (char *) "\n";
40   iov[1].iov_len = 1;
41   if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
42     printf ("Could not record temp mq filename %s\n", name);
43 }
44
45 /* Delete all temporary message queues.  */
46 static void
47 do_cleanup (void)
48 {
49   if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
50     return;
51
52   FILE *f = fdopen (temp_mq_fd, "r");
53   if (f == NULL)
54     return;
55
56   char *line = NULL;
57   size_t n = 0;
58   ssize_t rets;
59   while ((rets = getline (&line, &n, f)) > 0)
60     {
61       if (line[rets - 1] != '\n')
62         continue;
63
64       line[rets - 1] = '\0';
65       mq_unlink (line);
66     }
67   fclose (f);
68 }
69
70 static void
71 do_prepare (void)
72 {
73   char name [] = "/tmp/tst-mqueueN.XXXXXX";
74   temp_mq_fd = mkstemp (name);
75   if (temp_mq_fd == -1)
76     {
77       printf ("Could not create temporary file %s: %m\n", name);
78       exit (1);
79     }
80   unlink (name);
81 }
82
83 #define PREPARE(argc, argv) do_prepare ()
84 #define CLEANUP_HANDLER do_cleanup ()