mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-01 10:06:57 +03:00
posix: Adapt tst-spawn{2,3} to use libsupport.
Checked on x86_64-linux-gnu. * posix/tst-spawn2.c (do_test): Use libsupport. * posix/tst-spawn3.c (do_test): Likewise.
This commit is contained in:
@ -1,3 +1,8 @@
|
|||||||
|
2017-06-28 Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
||||||
|
|
||||||
|
* posix/tst-spawn2.c (do_test): Use libsupport.
|
||||||
|
* posix/tst-spawn3.c (do_test): Likewise.
|
||||||
|
|
||||||
2017-06-28 Joseph Myers <joseph@codesourcery.com>
|
2017-06-28 Joseph Myers <joseph@codesourcery.com>
|
||||||
|
|
||||||
* math/gen-tgmath-tests.py (Tests.__init__): Define
|
* math/gen-tgmath-tests.py (Tests.__init__): Define
|
||||||
|
@ -23,11 +23,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <support/check.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
posix_spawn_test (void)
|
do_test (void)
|
||||||
{
|
{
|
||||||
/* Check if posix_spawn correctly returns an error and an invalid pid
|
/* Check if posix_spawn correctly returns an error and an invalid pid
|
||||||
by trying to spawn an invalid binary. */
|
by trying to spawn an invalid binary. */
|
||||||
@ -38,35 +39,40 @@ posix_spawn_test (void)
|
|||||||
|
|
||||||
int ret = posix_spawn (&pid, program, 0, 0, args, environ);
|
int ret = posix_spawn (&pid, program, 0, 0, args, environ);
|
||||||
if (ret != ENOENT)
|
if (ret != ENOENT)
|
||||||
error (EXIT_FAILURE, errno, "posix_spawn");
|
{
|
||||||
|
errno = ret;
|
||||||
|
FAIL_EXIT1 ("posix_spawn: %m");
|
||||||
|
}
|
||||||
|
|
||||||
/* POSIX states the value returned on pid variable in case of an error
|
/* POSIX states the value returned on pid variable in case of an error
|
||||||
is not specified. GLIBC will update the value iff the child
|
is not specified. GLIBC will update the value iff the child
|
||||||
execution is successful. */
|
execution is successful. */
|
||||||
if (pid != -1)
|
if (pid != -1)
|
||||||
error (EXIT_FAILURE, errno, "posix_spawn returned pid != -1");
|
FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid);
|
||||||
|
|
||||||
/* Check if no child is actually created. */
|
/* Check if no child is actually created. */
|
||||||
ret = waitpid (-1, NULL, 0);
|
ret = waitpid (-1, NULL, 0);
|
||||||
if (ret != -1 || errno != ECHILD)
|
if (ret != -1 || errno != ECHILD)
|
||||||
error (EXIT_FAILURE, errno, "waitpid");
|
FAIL_EXIT1 ("waitpid: %m)");
|
||||||
|
|
||||||
/* Same as before, but with posix_spawnp. */
|
/* Same as before, but with posix_spawnp. */
|
||||||
char *args2[] = { (char*) program, 0 };
|
char *args2[] = { (char*) program, 0 };
|
||||||
|
|
||||||
ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
|
ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
|
||||||
if (ret != ENOENT)
|
if (ret != ENOENT)
|
||||||
error (EXIT_FAILURE, errno, "posix_spawnp");
|
{
|
||||||
|
errno = ret;
|
||||||
|
FAIL_EXIT1 ("posix_spawnp: %m");
|
||||||
|
}
|
||||||
|
|
||||||
if (pid != -1)
|
if (pid != -1)
|
||||||
error (EXIT_FAILURE, errno, "posix_spawnp returned pid != -1");
|
FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid);
|
||||||
|
|
||||||
ret = waitpid (-1, NULL, 0);
|
ret = waitpid (-1, NULL, 0);
|
||||||
if (ret != -1 || errno != ECHILD)
|
if (ret != -1 || errno != ECHILD)
|
||||||
error (EXIT_FAILURE, errno, "waitpid");
|
FAIL_EXIT1 ("waitpid: %m)");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TEST_FUNCTION posix_spawn_test ()
|
#include <support/test-driver.c>
|
||||||
#include "../test-skeleton.c"
|
|
||||||
|
@ -25,10 +25,11 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <paths.h>
|
||||||
|
|
||||||
static int do_test (void);
|
#include <support/check.h>
|
||||||
#define TEST_FUNCTION do_test ()
|
#include <support/temp_file.h>
|
||||||
#include <test-skeleton.c>
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
do_test (void)
|
do_test (void)
|
||||||
@ -47,25 +48,20 @@ do_test (void)
|
|||||||
|
|
||||||
struct rlimit rl;
|
struct rlimit rl;
|
||||||
int max_fd = 24;
|
int max_fd = 24;
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* Set maximum number of file descriptor to a low value to avoid open
|
/* Set maximum number of file descriptor to a low value to avoid open
|
||||||
too many files in environments where RLIMIT_NOFILE is large and to
|
too many files in environments where RLIMIT_NOFILE is large and to
|
||||||
limit the array size to track the opened file descriptors. */
|
limit the array size to track the opened file descriptors. */
|
||||||
|
|
||||||
if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
|
if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
|
||||||
{
|
FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
|
||||||
printf ("error: getrlimit RLIMIT_NOFILE failed");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);
|
max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);
|
||||||
rl.rlim_cur = max_fd;
|
rl.rlim_cur = max_fd;
|
||||||
|
|
||||||
if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
|
if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
|
||||||
{
|
FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
|
||||||
printf ("error: setrlimit RLIMIT_NOFILE to %u failed", max_fd);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Exhauste the file descriptor limit with temporary files. */
|
/* Exhauste the file descriptor limit with temporary files. */
|
||||||
int files[max_fd];
|
int files[max_fd];
|
||||||
@ -76,11 +72,7 @@ do_test (void)
|
|||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
{
|
{
|
||||||
if (errno != EMFILE)
|
if (errno != EMFILE)
|
||||||
{
|
FAIL_EXIT1 ("create_temp_file: %m");
|
||||||
printf ("error: create_temp_file returned -1 with "
|
|
||||||
"errno != EMFILE\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
files[nfiles++] = fd;
|
files[nfiles++] = fd;
|
||||||
@ -88,25 +80,16 @@ do_test (void)
|
|||||||
|
|
||||||
posix_spawn_file_actions_t a;
|
posix_spawn_file_actions_t a;
|
||||||
if (posix_spawn_file_actions_init (&a) != 0)
|
if (posix_spawn_file_actions_init (&a) != 0)
|
||||||
{
|
FAIL_EXIT1 ("posix_spawn_file_actions_init");
|
||||||
puts ("error: spawn_file_actions_init failed");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Executes a /bin/sh echo $$ 2>&1 > /tmp/tst-spawn3.pid . */
|
/* Executes a /bin/sh echo $$ 2>&1 > /tmp/tst-spawn3.pid . */
|
||||||
const char pidfile[] = "/tmp/tst-spawn3.pid";
|
const char pidfile[] = "/tmp/tst-spawn3.pid";
|
||||||
if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY |
|
if (posix_spawn_file_actions_addopen (&a, STDOUT_FILENO, pidfile, O_WRONLY |
|
||||||
O_CREAT | O_TRUNC, 0644) != 0)
|
O_CREAT | O_TRUNC, 0644) != 0)
|
||||||
{
|
FAIL_EXIT1 ("posix_spawn_file_actions_addopen");
|
||||||
puts ("error: spawn_file_actions_addopen failed");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)
|
if (posix_spawn_file_actions_adddup2 (&a, STDOUT_FILENO, STDERR_FILENO) != 0)
|
||||||
{
|
FAIL_EXIT1 ("posix_spawn_file_actions_adddup2");
|
||||||
puts ("error: spawn_file_actions_addclose");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Since execve (called by posix_spawn) might require to open files to
|
/* Since execve (called by posix_spawn) might require to open files to
|
||||||
actually execute the shell script, setup to close the temporary file
|
actually execute the shell script, setup to close the temporary file
|
||||||
@ -114,54 +97,40 @@ do_test (void)
|
|||||||
for (int i=0; i<nfiles; i++)
|
for (int i=0; i<nfiles; i++)
|
||||||
{
|
{
|
||||||
if (posix_spawn_file_actions_addclose (&a, files[i]))
|
if (posix_spawn_file_actions_addclose (&a, files[i]))
|
||||||
{
|
FAIL_EXIT1 ("posix_spawn_file_actions_addclose");
|
||||||
printf ("error: posix_spawn_file_actions_addclose failed");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",
|
char *spawn_argv[] = { (char *) _PATH_BSHELL, (char *) "-c",
|
||||||
(char *) "echo $$", NULL };
|
(char *) "echo $$", NULL };
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
if (posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL) != 0)
|
if ((ret = posix_spawn (&pid, _PATH_BSHELL, &a, NULL, spawn_argv, NULL))
|
||||||
|
!= 0)
|
||||||
{
|
{
|
||||||
puts ("error: posix_spawn failed");
|
errno = ret;
|
||||||
exit (EXIT_FAILURE);
|
FAIL_EXIT1 ("posix_spawn: %m");
|
||||||
}
|
}
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
int err = waitpid (pid, &status, 0);
|
int err = waitpid (pid, &status, 0);
|
||||||
if (err != pid)
|
if (err != pid)
|
||||||
{
|
FAIL_EXIT1 ("waitpid: %m");
|
||||||
puts ("error: waitpid failed");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Close the temporary files descriptor so it can check posix_spawn
|
/* Close the temporary files descriptor so it can check posix_spawn
|
||||||
output. */
|
output. */
|
||||||
for (int i=0; i<nfiles; i++)
|
for (int i=0; i<nfiles; i++)
|
||||||
{
|
{
|
||||||
if (close (files[i]))
|
if (close (files[i]))
|
||||||
{
|
FAIL_EXIT1 ("close: %m");
|
||||||
printf ("error: close failed\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int pidfd = open (pidfile, O_RDONLY);
|
int pidfd = open (pidfile, O_RDONLY);
|
||||||
if (pidfd == -1)
|
if (pidfd == -1)
|
||||||
{
|
FAIL_EXIT1 ("open: %m");
|
||||||
printf ("error: open pidfile failed\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[64];
|
char buf[64];
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
if ((n = read (pidfd, buf, sizeof (buf))) < 0)
|
if ((n = read (pidfd, buf, sizeof (buf))) < 0)
|
||||||
{
|
FAIL_EXIT1 ("read: %m");
|
||||||
printf ("error: read pidfile failed\n");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
unlink (pidfile);
|
unlink (pidfile);
|
||||||
|
|
||||||
@ -169,21 +138,14 @@ do_test (void)
|
|||||||
char *endp;
|
char *endp;
|
||||||
long int rpid = strtol (buf, &endp, 10);
|
long int rpid = strtol (buf, &endp, 10);
|
||||||
if (*endp != '\n')
|
if (*endp != '\n')
|
||||||
{
|
FAIL_EXIT1 ("*endp != \'n\'");
|
||||||
printf ("error: didn't parse whole line: \"%s\"\n", buf);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (endp == buf)
|
if (endp == buf)
|
||||||
{
|
FAIL_EXIT1 ("read empty line");
|
||||||
puts ("error: read empty line");
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rpid != pid)
|
if (rpid != pid)
|
||||||
{
|
FAIL_EXIT1 ("found \"%s\", expected pid %ld\n", buf, (long int) pid);
|
||||||
printf ("error: found \"%s\", expected PID %ld\n", buf, (long int) pid);
|
|
||||||
exit (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <support/test-driver.c>
|
||||||
|
Reference in New Issue
Block a user