* malloc/mcheck.c (reallochook): If size==0, free the block.
[glibc/pb-stable.git] / rt / tst-mqueue7.c
blob34222f8345a07a99b356e59e21f1e548a1f6d172
1 /* Test all open message queues descriptors are closed during exec*.
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.
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.
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.
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. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mqueue.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <time.h>
29 #include <unistd.h>
31 #define OPT_AFTEREXEC 20000
33 static mqd_t after_exec = (mqd_t) -1;
35 #define CMDLINE_OPTIONS \
36 { "after-exec", required_argument, NULL, OPT_AFTEREXEC },
38 #define CMDLINE_PROCESS \
39 case OPT_AFTEREXEC: \
40 after_exec = (mqd_t) strtoul (optarg, NULL, 0); \
41 break;
43 static int
44 do_after_exec (void)
46 int result = 0;
48 struct mq_attr attr;
49 if (mq_getattr (after_exec, &attr) == 0)
51 puts ("mq_getattr after exec unexpectedly succeeded");
52 result = 1;
54 else if (errno != EBADF)
56 printf ("mq_getattr after exec did not fail with EBADF: %m\n");
57 result = 1;
60 return result;
63 static int
64 do_test (int argc, char **argv)
66 if (after_exec != (mqd_t) -1)
67 return do_after_exec ();
69 char name[sizeof "/tst-mqueue7-" + sizeof (pid_t) * 3];
70 snprintf (name, sizeof (name), "/tst-mqueue7-%u", getpid ());
72 struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
73 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
75 if (q == (mqd_t) -1)
77 printf ("mq_open failed with: %m\n");
78 return 0;
80 else if (mq_unlink (name) != 0)
82 printf ("mq_unlink failed with: %m\n");
83 return 1;
86 if (mq_getattr (q, &attr) != 0)
88 printf ("mq_getattr failed: %m\n");
89 return 1;
92 char after_exec_arg[sizeof "--after-exec=0x" + sizeof (long) * 3];
93 snprintf (after_exec_arg, sizeof (after_exec_arg),
94 "--after-exec=0x%lx", (long) q);
96 const char *newargv[argc + 2];
97 for (int i = 1; i < argc; ++i)
98 newargv[i - 1] = argv[i];
99 newargv[argc - 1] = "--direct";
100 newargv[argc] = after_exec_arg;
101 newargv[argc + 1] = NULL;
103 /* Verify that exec* has the effect of mq_close (q). */
104 execv (newargv[0], (char * const *) newargv);
105 printf ("execv failed: %m\n");
106 return 1;
109 #include "../test-skeleton.c"