xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/powerpc/ptrace/child.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Helper functions to sync execution between parent and child processes.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2018, Thiago Jung Bauermann, IBM Corporation.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <stdio.h>
8*4882a593Smuzhiyun #include <stdbool.h>
9*4882a593Smuzhiyun #include <semaphore.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * Information in a shared memory location for synchronization between child and
13*4882a593Smuzhiyun  * parent.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun struct child_sync {
16*4882a593Smuzhiyun 	/* The parent waits on this semaphore. */
17*4882a593Smuzhiyun 	sem_t sem_parent;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 	/* If true, the child should give up as well. */
20*4882a593Smuzhiyun 	bool parent_gave_up;
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 	/* The child waits on this semaphore. */
23*4882a593Smuzhiyun 	sem_t sem_child;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	/* If true, the parent should give up as well. */
26*4882a593Smuzhiyun 	bool child_gave_up;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define CHILD_FAIL_IF(x, sync)						\
30*4882a593Smuzhiyun 	do {								\
31*4882a593Smuzhiyun 		if (x) {						\
32*4882a593Smuzhiyun 			fprintf(stderr,					\
33*4882a593Smuzhiyun 				"[FAIL] Test FAILED on line %d\n", __LINE__); \
34*4882a593Smuzhiyun 			(sync)->child_gave_up = true;			\
35*4882a593Smuzhiyun 			prod_parent(sync);				\
36*4882a593Smuzhiyun 			return 1;					\
37*4882a593Smuzhiyun 		}							\
38*4882a593Smuzhiyun 	} while (0)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define PARENT_FAIL_IF(x, sync)						\
41*4882a593Smuzhiyun 	do {								\
42*4882a593Smuzhiyun 		if (x) {						\
43*4882a593Smuzhiyun 			fprintf(stderr,					\
44*4882a593Smuzhiyun 				"[FAIL] Test FAILED on line %d\n", __LINE__); \
45*4882a593Smuzhiyun 			(sync)->parent_gave_up = true;			\
46*4882a593Smuzhiyun 			prod_child(sync);				\
47*4882a593Smuzhiyun 			return 1;					\
48*4882a593Smuzhiyun 		}							\
49*4882a593Smuzhiyun 	} while (0)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define PARENT_SKIP_IF_UNSUPPORTED(x, sync)				\
52*4882a593Smuzhiyun 	do {								\
53*4882a593Smuzhiyun 		if ((x) == -1 && (errno == ENODEV || errno == EINVAL)) { \
54*4882a593Smuzhiyun 			(sync)->parent_gave_up = true;			\
55*4882a593Smuzhiyun 			prod_child(sync);				\
56*4882a593Smuzhiyun 			SKIP_IF(1);					\
57*4882a593Smuzhiyun 		}							\
58*4882a593Smuzhiyun 	} while (0)
59*4882a593Smuzhiyun 
init_child_sync(struct child_sync * sync)60*4882a593Smuzhiyun int init_child_sync(struct child_sync *sync)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	int ret;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	ret = sem_init(&sync->sem_parent, 1, 0);
65*4882a593Smuzhiyun 	if (ret) {
66*4882a593Smuzhiyun 		perror("Semaphore initialization failed");
67*4882a593Smuzhiyun 		return 1;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	ret = sem_init(&sync->sem_child, 1, 0);
71*4882a593Smuzhiyun 	if (ret) {
72*4882a593Smuzhiyun 		perror("Semaphore initialization failed");
73*4882a593Smuzhiyun 		return 1;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return 0;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
destroy_child_sync(struct child_sync * sync)79*4882a593Smuzhiyun void destroy_child_sync(struct child_sync *sync)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	sem_destroy(&sync->sem_parent);
82*4882a593Smuzhiyun 	sem_destroy(&sync->sem_child);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
wait_child(struct child_sync * sync)85*4882a593Smuzhiyun int wait_child(struct child_sync *sync)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int ret;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Wait until the child prods us. */
90*4882a593Smuzhiyun 	ret = sem_wait(&sync->sem_parent);
91*4882a593Smuzhiyun 	if (ret) {
92*4882a593Smuzhiyun 		perror("Error waiting for child");
93*4882a593Smuzhiyun 		return 1;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return sync->child_gave_up;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
prod_child(struct child_sync * sync)99*4882a593Smuzhiyun int prod_child(struct child_sync *sync)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	int ret;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/* Unblock the child now. */
104*4882a593Smuzhiyun 	ret = sem_post(&sync->sem_child);
105*4882a593Smuzhiyun 	if (ret) {
106*4882a593Smuzhiyun 		perror("Error prodding child");
107*4882a593Smuzhiyun 		return 1;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
wait_parent(struct child_sync * sync)113*4882a593Smuzhiyun int wait_parent(struct child_sync *sync)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	int ret;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* Wait until the parent prods us. */
118*4882a593Smuzhiyun 	ret = sem_wait(&sync->sem_child);
119*4882a593Smuzhiyun 	if (ret) {
120*4882a593Smuzhiyun 		perror("Error waiting for parent");
121*4882a593Smuzhiyun 		return 1;
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return sync->parent_gave_up;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
prod_parent(struct child_sync * sync)127*4882a593Smuzhiyun int prod_parent(struct child_sync *sync)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	int ret;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* Unblock the parent now. */
132*4882a593Smuzhiyun 	ret = sem_post(&sync->sem_parent);
133*4882a593Smuzhiyun 	if (ret) {
134*4882a593Smuzhiyun 		perror("Error prodding parent");
135*4882a593Smuzhiyun 		return 1;
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return 0;
139*4882a593Smuzhiyun }
140