~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/powerpc/ptrace/child.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php