Back to index

glibc  2.9
tst-mqueue7.c
Go to the documentation of this file.
00001 /* Test all open message queues descriptors are closed during exec*.
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 <errno.h>
00022 #include <fcntl.h>
00023 #include <mqueue.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/wait.h>
00028 #include <time.h>
00029 #include <unistd.h>
00030 
00031 #define OPT_AFTEREXEC 20000
00032 
00033 static mqd_t after_exec = (mqd_t) -1;
00034 
00035 #define CMDLINE_OPTIONS \
00036   { "after-exec", required_argument, NULL, OPT_AFTEREXEC },
00037 
00038 #define CMDLINE_PROCESS \
00039   case OPT_AFTEREXEC:                                   \
00040     after_exec = (mqd_t) strtoul (optarg, NULL, 0);     \
00041     break;
00042 
00043 static int
00044 do_after_exec (void)
00045 {
00046   int result = 0;
00047 
00048   struct mq_attr attr;
00049   if (mq_getattr (after_exec, &attr) == 0)
00050     {
00051       puts ("mq_getattr after exec unexpectedly succeeded");
00052       result = 1;
00053     }
00054   else if (errno != EBADF)
00055     {
00056       printf ("mq_getattr after exec did not fail with EBADF: %m\n");
00057       result = 1;
00058     }
00059 
00060   return result;
00061 }
00062 
00063 static int
00064 do_test (int argc, char **argv)
00065 {
00066   if (after_exec != (mqd_t) -1)
00067     return do_after_exec ();
00068 
00069   char name[sizeof "/tst-mqueue7-" + sizeof (pid_t) * 3];
00070   snprintf (name, sizeof (name), "/tst-mqueue7-%u", getpid ());
00071 
00072   struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
00073   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
00074 
00075   if (q == (mqd_t) -1)
00076     {
00077       printf ("mq_open failed with: %m\n");
00078       return 0;
00079     }
00080   else if (mq_unlink (name) != 0)
00081     {
00082       printf ("mq_unlink failed with: %m\n");
00083       return 1;
00084     }
00085 
00086   if (mq_getattr (q, &attr) != 0)
00087     {
00088       printf ("mq_getattr failed: %m\n");
00089       return 1;
00090     }
00091 
00092   char after_exec_arg[sizeof "--after-exec=0x" + sizeof (long) * 3];
00093   snprintf (after_exec_arg, sizeof (after_exec_arg),
00094            "--after-exec=0x%lx", (long) q);
00095 
00096   const char *newargv[argc + 2];
00097   for (int i = 1; i < argc; ++i)
00098     newargv[i - 1] = argv[i];
00099   newargv[argc - 1] = "--direct";
00100   newargv[argc] = after_exec_arg;
00101   newargv[argc + 1] = NULL;
00102 
00103   /* Verify that exec* has the effect of mq_close (q).  */
00104   execv (newargv[0], (char * const *) newargv);
00105   printf ("execv failed: %m\n");
00106   return 1;
00107 }
00108 
00109 #include "../test-skeleton.c"