|
Packit |
6c4009 |
/* Test for sched_getaffinity and sched_setaffinity, PID version.
|
|
Packit |
6c4009 |
Copyright (C) 2015-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 |
/* Function definitions for the benefit of tst-skeleton-affinity.c.
|
|
Packit |
6c4009 |
This variant forks a child process which then invokes
|
|
Packit |
6c4009 |
sched_getaffinity and sched_setaffinity on the parent PID. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <sched.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
write_fully (int fd, const void *buffer, size_t length)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const void *end = buffer + length;
|
|
Packit |
6c4009 |
while (buffer < end)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ssize_t bytes_written = TEMP_FAILURE_RETRY
|
|
Packit |
6c4009 |
(write (fd, buffer, end - buffer));
|
|
Packit |
6c4009 |
if (bytes_written < 0)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
if (bytes_written == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = ENOSPC;
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
buffer += bytes_written;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static ssize_t
|
|
Packit |
6c4009 |
read_fully (int fd, void *buffer, size_t length)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const void *start = buffer;
|
|
Packit |
6c4009 |
const void *end = buffer + length;
|
|
Packit |
6c4009 |
while (buffer < end)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ssize_t bytes_read = TEMP_FAILURE_RETRY
|
|
Packit |
6c4009 |
(read (fd, buffer, end - buffer));
|
|
Packit |
6c4009 |
if (bytes_read < 0)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
if (bytes_read == 0)
|
|
Packit |
6c4009 |
return buffer - start;
|
|
Packit |
6c4009 |
buffer += bytes_read;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return length;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
process_child_response (int *pipes, pid_t child,
|
|
Packit |
6c4009 |
cpu_set_t *set, size_t size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
close (pipes[1]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int value_from_child;
|
|
Packit |
6c4009 |
ssize_t bytes_read = read_fully
|
|
Packit |
6c4009 |
(pipes[0], &value_from_child, sizeof (value_from_child));
|
|
Packit |
6c4009 |
if (bytes_read < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: read from child: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (bytes_read != sizeof (value_from_child))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: not enough bytes from child: %zd\n", bytes_read);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (value_from_child == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
bytes_read = read_fully (pipes[0], set, size);
|
|
Packit |
6c4009 |
if (bytes_read < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: read: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (bytes_read != size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: not enough bytes from child: %zd\n", bytes_read);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
if (waitpid (child, &status, 0) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: waitpid: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: invalid status from : %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (pipes[0]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (value_from_child != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
errno = value_from_child;
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
getaffinity (size_t size, cpu_set_t *set)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int pipes[2];
|
|
Packit |
6c4009 |
if (pipe (pipes) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: pipe: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int ret = fork ();
|
|
Packit |
6c4009 |
if (ret < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: fork: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (ret == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Child. */
|
|
Packit |
6c4009 |
int ret = sched_getaffinity (getppid (), size, set);
|
|
Packit |
6c4009 |
if (ret < 0)
|
|
Packit |
6c4009 |
ret = errno;
|
|
Packit |
6c4009 |
if (write_fully (pipes[1], &ret, sizeof (ret)) < 0
|
|
Packit |
6c4009 |
|| write_fully (pipes[1], set, size) < 0
|
|
Packit |
6c4009 |
|| (ret == 0 && write_fully (pipes[1], set, size) < 0))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: write: %m\n");
|
|
Packit |
6c4009 |
_exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
_exit (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Parent. */
|
|
Packit |
6c4009 |
return process_child_response (pipes, ret, set, size);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
setaffinity (size_t size, const cpu_set_t *set)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int pipes[2];
|
|
Packit |
6c4009 |
if (pipe (pipes) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: pipe: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int ret = fork ();
|
|
Packit |
6c4009 |
if (ret < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: fork: %m\n");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (ret == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Child. */
|
|
Packit |
6c4009 |
int ret = sched_setaffinity (getppid (), size, set);
|
|
Packit |
6c4009 |
if (write_fully (pipes[1], &ret, sizeof (ret)) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("error: write: %m\n");
|
|
Packit |
6c4009 |
_exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
_exit (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Parent. There is no affinity mask to read from the child, so the
|
|
Packit |
6c4009 |
size is 0. */
|
|
Packit |
6c4009 |
return process_child_response (pipes, ret, NULL, 0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct conf;
|
|
Packit |
6c4009 |
static bool early_test (struct conf *unused)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include "tst-skeleton-affinity.c"
|