1### **PTHREAD_ATFORK_FOR_GWPASAN OHOS Programmer's Manual ** 2 3 4 5#### **NAME** 6 7 pthread_atfork_for_gwpasan - pthread_atfork specified for GWP sanitizer, 8<200b> register fork handler for GWP sanitizer to make sure prepare handler of 9<200b> GWP sanitizer will be executed last after all the other registed prepare handler. 10<200b> parent and child handler for GWP sanitizer will be executed first before 11<200b> before all the other registed parent and child handler. 12 13#### **SYNOPSIS** 14 15 #include <pthread.h> 16 17 int pthread_atfork_for_gwpasan(typeof(void (void)) *prepare, 18 typeof(void (void)) *parent, 19 typeof(void (void)) *child); 20 21#### **DESCRIPTION** 22 23 The pthread_atfork_for_gwpasan() function registers fork handlers that are 24 to be executed when fork() is called by any thread in a process. 25 The handlers are executed in the context of thread that calls fork(). 26 27<200b> Three kinds of handler can be registered: 28 29<200b> 1. prepare specifies a handler that is executed in the parent 30<200b> process before fork() processing starts. 31 32<200b> 2. parent specifies a handler that is executed in the parent process 33<200b> after fork() processing completes. 34 35<200b> 3. child specifies a handler that is executed in the child process 36<200b> after fork() processing completes. 37 38<200b> Any of the three arguments may be NULL is no handler is needed in<200b> the corresponding phase of fork() processing. 39 40<200b> pthread_atfork() may be called multiple times 41<200b> by a process to register additional handlers. The handlers for each 42<200b> phase are called in a specified order: the prepare handlers are 43called 44<200b> in reverse order of registration; the parent and child handlers 45are 46<200b> called in the order of registration. GWP sanitizer will call 47<200b> pthread_atfork_gwpasan to make sure prepare handler of GWP sanitizer 48<200b> will be executed after other prepare handlers, parent and child 49<200b> handlers of GWP sanitizer will be executed before others. 50 51#### **RETURN VALUE** 52 53 On success, pthread_atfork_for_gwpasan() returns zero. On error, it returns: 54 an error number. 55 56#### **ERRORS** 57 58 ENOMEM Could not allocate memory to record the fork handler list 59entry. 60 61#### ATTRIBUTES 62 63| Interface | Attribute | Value | 64| ---------------------------- | ------------- | ---------- | 65| pthread_atfork_for_gwpasan() | Thread safety | MT-unsafe | 66| | Signal safety | Not Safe | 67 68#### HISTORY 69 70 -- 2025 71 72#### NOTES 73 74 The pthread_atfork_for_gwpasan() is specified for GWP sanitizer. 75<200b> The behavior will not be guranteed under other situations. 76<200b> pthread_atfork() should be used to register fork handler. 77 78#### EXAMPLES 79 80```c 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <pthread.h> 84 #include <unistd.h> 85 86 void prepare() { 87 printf("Prepare handler called\n"); 88 } 89 90 void parent() { 91 printf("Parent handler called\n"); 92 } 93 94 void child() { 95 printf("Child handler called\n"); 96 } 97 98 int main(void) { 99 if (pthread_atfork_for_gwpasan(prepare, parent, child) != 0) { 100 perror("pthread_atfork_for_gwp_asan"); 101 exit(EXIT_FAILURE); 102 } 103 104 105 106 pid_t pid = fork(); 107 if (pid == -1) { 108 perror("fork"); 109 exit(EXIT_FAILURE); 110 } else if (pid == 0) { 111 printf("Child process\n"); 112 exit(EXIT_SUCCESS); 113 } else { 114 printf("Parent process\n"); 115 wait(NULL); 116 } 117 118 return 0; 119 } 120``` 121 122 123#### COLOPHTON 124 125 this page is part of the C library user-space interface documentation. 126 Information about the project can be found at (https://gitee.com/openharmony/third_party_musl/blob/master/docs/) 127