1 /* Test all open message queues descriptors are closed during exec*.
2    Copyright (C) 2004-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <mqueue.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <time.h>
27 #include <unistd.h>
28 
29 #define OPT_AFTEREXEC 20000
30 
31 static mqd_t after_exec = (mqd_t) -1;
32 
33 #define CMDLINE_OPTIONS \
34   { "after-exec", required_argument, NULL, OPT_AFTEREXEC },
35 
36 #define CMDLINE_PROCESS \
37   case OPT_AFTEREXEC:					\
38     after_exec = (mqd_t) strtoul (optarg, NULL, 0);	\
39     break;
40 
41 static int
do_after_exec(void)42 do_after_exec (void)
43 {
44   int result = 0;
45 
46   struct mq_attr attr;
47   if (mq_getattr (after_exec, &attr) == 0)
48     {
49       puts ("mq_getattr after exec unexpectedly succeeded");
50       result = 1;
51     }
52   else if (errno != EBADF)
53     {
54       printf ("mq_getattr after exec did not fail with EBADF: %m\n");
55       result = 1;
56     }
57 
58   return result;
59 }
60 
61 static int
do_test(int argc,char ** argv)62 do_test (int argc, char **argv)
63 {
64   if (after_exec != (mqd_t) -1)
65     return do_after_exec ();
66 
67   char name[sizeof "/tst-mqueue7-" + sizeof (pid_t) * 3];
68   snprintf (name, sizeof (name), "/tst-mqueue7-%u", getpid ());
69 
70   struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
71   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
72 
73   if (q == (mqd_t) -1)
74     {
75       printf ("mq_open failed with: %m\n");
76       return 0;
77     }
78   else if (mq_unlink (name) != 0)
79     {
80       printf ("mq_unlink failed with: %m\n");
81       return 1;
82     }
83 
84   if (mq_getattr (q, &attr) != 0)
85     {
86       printf ("mq_getattr failed: %m\n");
87       return 1;
88     }
89 
90   char after_exec_arg[sizeof "--after-exec=0x" + sizeof (long) * 3];
91   snprintf (after_exec_arg, sizeof (after_exec_arg),
92 	    "--after-exec=0x%lx", (long) q);
93 
94   const char *newargv[argc + 2];
95   for (int i = 1; i < argc; ++i)
96     newargv[i - 1] = argv[i];
97   newargv[argc - 1] = "--direct";
98   newargv[argc] = after_exec_arg;
99   newargv[argc + 1] = NULL;
100 
101   /* Verify that exec* has the effect of mq_close (q).  */
102   execv (newargv[0], (char * const *) newargv);
103   printf ("execv failed: %m\n");
104   return 1;
105 }
106 
107 #include "../test-skeleton.c"
108