linux-stable/tools/testing/selftests/powerpc/ptrace/child.h
Benjamin Gray 68877ff20a selftests/powerpc/ptrace: Explain why tests are skipped
Many tests require specific hardware features/configurations that a
typical machine might not have. As a result, it's common to see a test
is skipped. But it is tedious to find out why a test is skipped
when all it gives is the file location of the skip macro.

Convert SKIP_IF() to SKIP_IF_MSG(), with appropriate descriptions of why
the test is being skipped. This gives a general idea of why a test is
skipped, which can be looked into further if it doesn't make sense.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20230725005841.28854-3-bgray@linux.ibm.com
2023-08-02 22:22:19 +10:00

139 lines
2.6 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* Helper functions to sync execution between parent and child processes.
*
* Copyright 2018, Thiago Jung Bauermann, IBM Corporation.
*/
#include <stdio.h>
#include <stdbool.h>
#include <semaphore.h>
/*
* Information in a shared memory location for synchronization between child and
* parent.
*/
struct child_sync {
/* The parent waits on this semaphore. */
sem_t sem_parent;
/* If true, the child should give up as well. */
bool parent_gave_up;
/* The child waits on this semaphore. */
sem_t sem_child;
/* If true, the parent should give up as well. */
bool child_gave_up;
};
#define CHILD_FAIL_IF(x, sync) \
do { \
if (x) { \
fprintf(stderr, \
"[FAIL] Test FAILED on line %d\n", __LINE__); \
(sync)->child_gave_up = true; \
prod_parent(sync); \
return 1; \
} \
} while (0)
#define PARENT_FAIL_IF(x, sync) \
do { \
if (x) { \
fprintf(stderr, \
"[FAIL] Test FAILED on line %d\n", __LINE__); \
(sync)->parent_gave_up = true; \
prod_child(sync); \
return 1; \
} \
} while (0)
#define PARENT_SKIP_IF_UNSUPPORTED(x, sync, msg) \
do { \
if ((x) == -1 && (errno == ENODEV || errno == EINVAL)) { \
(sync)->parent_gave_up = true; \
prod_child(sync); \
SKIP_IF_MSG(1, msg); \
} \
} while (0)
int init_child_sync(struct child_sync *sync)
{
int ret;
ret = sem_init(&sync->sem_parent, 1, 0);
if (ret) {
perror("Semaphore initialization failed");
return 1;
}
ret = sem_init(&sync->sem_child, 1, 0);
if (ret) {
perror("Semaphore initialization failed");
return 1;
}
return 0;
}
void destroy_child_sync(struct child_sync *sync)
{
sem_destroy(&sync->sem_parent);
sem_destroy(&sync->sem_child);
}
int wait_child(struct child_sync *sync)
{
int ret;
/* Wait until the child prods us. */
ret = sem_wait(&sync->sem_parent);
if (ret) {
perror("Error waiting for child");
return 1;
}
return sync->child_gave_up;
}
int prod_child(struct child_sync *sync)
{
int ret;
/* Unblock the child now. */
ret = sem_post(&sync->sem_child);
if (ret) {
perror("Error prodding child");
return 1;
}
return 0;
}
int wait_parent(struct child_sync *sync)
{
int ret;
/* Wait until the parent prods us. */
ret = sem_wait(&sync->sem_child);
if (ret) {
perror("Error waiting for parent");
return 1;
}
return sync->parent_gave_up;
}
int prod_parent(struct child_sync *sync)
{
int ret;
/* Unblock the parent now. */
ret = sem_post(&sync->sem_parent);
if (ret) {
perror("Error prodding parent");
return 1;
}
return 0;
}