Back to index

glibc  2.9
tst-mqueue.h
Go to the documentation of this file.
00001 /* Common code for message queue passing tests.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <mqueue.h>
00022 #include <search.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <sys/uio.h>
00027 #include <unistd.h>
00028 
00029 static int temp_mq_fd;
00030 
00031 /* Add temporary files in list.  */
00032 static void
00033 __attribute__ ((unused))
00034 add_temp_mq (const char *name)
00035 {
00036   struct iovec iov[2];
00037   iov[0].iov_base = (char *) name;
00038   iov[0].iov_len = strlen (name);
00039   iov[1].iov_base = (char *) "\n";
00040   iov[1].iov_len = 1;
00041   if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
00042     printf ("Could not record temp mq filename %s\n", name);
00043 }
00044 
00045 /* Delete all temporary message queues.  */
00046 static void
00047 do_cleanup (void)
00048 {
00049   if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
00050     return;
00051 
00052   FILE *f = fdopen (temp_mq_fd, "r");
00053   if (f == NULL)
00054     return;
00055 
00056   char *line = NULL;
00057   size_t n = 0;
00058   ssize_t rets;
00059   while ((rets = getline (&line, &n, f)) > 0)
00060     {
00061       if (line[rets - 1] != '\n')
00062         continue;
00063 
00064       line[rets - 1] = '\0';
00065       mq_unlink (line);
00066     }
00067   fclose (f);
00068 }
00069 
00070 static void
00071 do_prepare (void)
00072 {
00073   char name [] = "/tmp/tst-mqueueN.XXXXXX";
00074   temp_mq_fd = mkstemp (name);
00075   if (temp_mq_fd == -1)
00076     {
00077       printf ("Could not create temporary file %s: %m\n", name);
00078       exit (1);
00079     }
00080   unlink (name);
00081 }
00082 
00083 #define PREPARE(argc, argv) do_prepare ()
00084 #define CLEANUP_HANDLER     do_cleanup ()