|
Packit |
6c4009 |
/* Test all open message queues descriptors are closed during exec*.
|
|
Packit |
6c4009 |
Copyright (C) 2004-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <mqueue.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
#include <time.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define OPT_AFTEREXEC 20000
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static mqd_t after_exec = (mqd_t) -1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define CMDLINE_OPTIONS \
|
|
Packit |
6c4009 |
{ "after-exec", required_argument, NULL, OPT_AFTEREXEC },
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define CMDLINE_PROCESS \
|
|
Packit |
6c4009 |
case OPT_AFTEREXEC: \
|
|
Packit |
6c4009 |
after_exec = (mqd_t) strtoul (optarg, NULL, 0); \
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_after_exec (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int result = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct mq_attr attr;
|
|
Packit |
6c4009 |
if (mq_getattr (after_exec, &attr) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("mq_getattr after exec unexpectedly succeeded");
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (errno != EBADF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("mq_getattr after exec did not fail with EBADF: %m\n");
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (int argc, char **argv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (after_exec != (mqd_t) -1)
|
|
Packit |
6c4009 |
return do_after_exec ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char name[sizeof "/tst-mqueue7-" + sizeof (pid_t) * 3];
|
|
Packit |
6c4009 |
snprintf (name, sizeof (name), "/tst-mqueue7-%u", getpid ());
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
|
|
Packit |
6c4009 |
mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (q == (mqd_t) -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("mq_open failed with: %m\n");
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (mq_unlink (name) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("mq_unlink failed with: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (mq_getattr (q, &attr) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("mq_getattr failed: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char after_exec_arg[sizeof "--after-exec=0x" + sizeof (long) * 3];
|
|
Packit |
6c4009 |
snprintf (after_exec_arg, sizeof (after_exec_arg),
|
|
Packit |
6c4009 |
"--after-exec=0x%lx", (long) q);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
const char *newargv[argc + 2];
|
|
Packit |
6c4009 |
for (int i = 1; i < argc; ++i)
|
|
Packit |
6c4009 |
newargv[i - 1] = argv[i];
|
|
Packit |
6c4009 |
newargv[argc - 1] = "--direct";
|
|
Packit |
6c4009 |
newargv[argc] = after_exec_arg;
|
|
Packit |
6c4009 |
newargv[argc + 1] = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Verify that exec* has the effect of mq_close (q). */
|
|
Packit |
6c4009 |
execv (newargv[0], (char * const *) newargv);
|
|
Packit |
6c4009 |
printf ("execv failed: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|