|
Packit |
6c4009 |
/* Test that spawn file action functions work without file limit.
|
|
Packit |
6c4009 |
Copyright (C) 2016-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
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 <spawn.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <sys/resource.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* _SC_OPEN_MAX value. */
|
|
Packit |
6c4009 |
static long maxfd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* A positive but unused file descriptor, used for testing
|
|
Packit |
6c4009 |
purposes. */
|
|
Packit |
6c4009 |
static int invalid_fd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Indicate that errors have been encountered. */
|
|
Packit |
6c4009 |
static bool errors;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static posix_spawn_file_actions_t actions;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
one_test (const char *name, int (*func) (int), int fd,
|
|
Packit |
6c4009 |
bool expect_success)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int ret = func (fd);
|
|
Packit |
6c4009 |
if (expect_success)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (ret != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = ret;
|
|
Packit |
6c4009 |
printf ("error: posix_spawn_file_actions_%s (%d): %m\n", name, fd);
|
|
Packit |
6c4009 |
errors = true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (ret != EBADF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (ret == 0)
|
|
Packit |
6c4009 |
printf ("error: posix_spawn_file_actions_%s (%d):"
|
|
Packit |
6c4009 |
" unexpected success\n", name, fd);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = ret;
|
|
Packit |
6c4009 |
printf ("error: posix_spawn_file_actions_%s (%d): %m\n", name, fd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
errors = true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
all_tests (const char *name, int (*func) (int))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
one_test (name, func, 0, true);
|
|
Packit |
6c4009 |
one_test (name, func, invalid_fd, true);
|
|
Packit |
6c4009 |
one_test (name, func, -1, false);
|
|
Packit |
6c4009 |
one_test (name, func, -2, false);
|
|
Packit |
6c4009 |
if (maxfd >= 0)
|
|
Packit |
6c4009 |
one_test (name, func, maxfd, false);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
addopen (int fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return posix_spawn_file_actions_addopen
|
|
Packit |
6c4009 |
(&actions, fd, "/dev/null", O_RDONLY, 0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
adddup2 (int fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return posix_spawn_file_actions_adddup2 (&actions, fd, 1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
adddup2_reverse (int fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return posix_spawn_file_actions_adddup2 (&actions, 1, fd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
addclose (int fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return posix_spawn_file_actions_addclose (&actions, fd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
all_functions (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
all_tests ("addopen", addopen);
|
|
Packit |
6c4009 |
all_tests ("adddup2", adddup2);
|
|
Packit |
6c4009 |
all_tests ("adddup2", adddup2_reverse);
|
|
Packit |
6c4009 |
all_tests ("adddup2", addclose);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Try to eliminate the file descriptor limit. */
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct rlimit limit;
|
|
Packit |
6c4009 |
if (getrlimit (RLIMIT_NOFILE, &limit) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: getrlimit: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
limit.rlim_cur = RLIM_INFINITY;
|
|
Packit |
6c4009 |
if (setrlimit (RLIMIT_NOFILE, &limit) < 0)
|
|
Packit |
6c4009 |
printf ("warning: setrlimit: %m\n");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
maxfd = sysconf (_SC_OPEN_MAX);
|
|
Packit |
6c4009 |
printf ("info: _SC_OPEN_MAX: %ld\n", maxfd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
invalid_fd = dup (0);
|
|
Packit |
6c4009 |
if (invalid_fd < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: dup: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (close (invalid_fd) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: close: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int ret = posix_spawn_file_actions_init (&actions);
|
|
Packit |
6c4009 |
if (ret != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = ret;
|
|
Packit |
6c4009 |
printf ("error: posix_spawn_file_actions_init: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
all_functions ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ret = posix_spawn_file_actions_destroy (&actions);
|
|
Packit |
6c4009 |
if (ret != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = ret;
|
|
Packit |
6c4009 |
printf ("error: posix_spawn_file_actions_destroy: %m\n");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return errors;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|