• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <assert.h>
18 #include <info/fatal_message.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <string.h>
23 #include <stdlib.h>
24 
25 #undef assert
26 #define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, __func__),0)))
27 
28 #define FILE_ABORT "abort.txt"
29 #define FILE_IGNORE "ignore.txt"
30 #define FILE_RETRY "retry.txt"
31 #define ASSERT_ABORT_INFO "ASSERT_ABORT"
32 #define ASSERT_IGNORE_INFO "ASSERT_IGNORE"
33 #define ASSERT_RETRY_INFO "ASSERT_RETRY"
34 #define MAX_FILE_SIZE 1024
35 
WriteToFile(const char * filename,const char * content)36 void WriteToFile(const char *filename, const char *content)
37 {
38     FILE *file = fopen(filename, "w");
39     if (file != NULL) {
40         fprintf(file, "%s\n", content);
41         fclose(file);
42     } else {
43         perror("fopen");
44     }
45 }
46 
ReadFile(const char * filename)47 char* ReadFile(const char *filename)
48 {
49     static char buffer[MAX_FILE_SIZE];
50     FILE *file;
51 
52     file = fopen(filename, "r");
53     if (file == NULL) {
54         perror("fopen");
55         return NULL;
56     }
57     if (fgets(buffer, sizeof(buffer), file) == NULL) {
58         perror("fgets");
59         fclose(file);
60         return NULL;
61     }
62 
63     fclose(file);
64     return buffer;
65 }
66 
CallbackFunctionAbort(AssertFailureInfo assert_fail)67 Assert_Status CallbackFunctionAbort(AssertFailureInfo assert_fail)
68 {
69     char content[MAX_FILE_SIZE] = ASSERT_ABORT_INFO;
70     WriteToFile(FILE_ABORT, content);
71     Assert_Status res = ASSERT_ABORT;
72     return res;
73 }
74 
CallbackFunctionIgnore(AssertFailureInfo assert_fail)75 Assert_Status CallbackFunctionIgnore(AssertFailureInfo assert_fail)
76 {
77     char content[MAX_FILE_SIZE] = ASSERT_IGNORE_INFO;
78     WriteToFile(FILE_IGNORE, content);
79     Assert_Status res = ASSERT_IGNORE;
80     return res;
81 }
82 
CallbackFunctionRetry(AssertFailureInfo assert_fail)83 Assert_Status CallbackFunctionRetry(AssertFailureInfo assert_fail)
84 {
85     char content[MAX_FILE_SIZE] = ASSERT_RETRY_INFO;
86     WriteToFile(FILE_RETRY, content);
87     Assert_Status res = ASSERT_RETRY;
88     return res;
89 }
90 
ProcessAbort()91 void ProcessAbort()
92 {
93     set_assert_callback(CallbackFunctionAbort);
94     assert(0);
95 }
96 
ProcessIgnore()97 void ProcessIgnore()
98 {
99     set_assert_callback(CallbackFunctionIgnore);
100     assert(0);
101 }
102 
ProcessRetry()103 void ProcessRetry()
104 {
105     set_assert_callback(CallbackFunctionRetry);
106     assert(0);
107 }
108 
main(void)109 int main(void)
110 {
111     signal(SIGUSR1, SIG_DFL);
112     pid_t pid1, pid2, pid3;
113     int status;
114 
115     // Create the first child process
116     pid1 = fork();
117     if (pid1 < 0) {
118         // Failed to create the process
119         fprintf(stderr, "Fork failed for Process One\n");
120         return 1;
121     } else if (pid1 == 0) {
122         // Child process one
123         ProcessAbort(); // Specific operations for Child Process One can be added here
124         return 0;
125     }
126 
127     // Create the second child process
128     pid2 = fork();
129     if (pid2 < 0) {
130         // Failed to create the process
131         fprintf(stderr, "Fork failed for Process Two\n");
132         return 1;
133     } else if (pid2 == 0) {
134         // Child process two
135         ProcessIgnore(); // Specific operations for Child Process Two can be added here
136         return 0;
137     }
138 
139     // Create the third child process
140     pid3 = fork();
141     if (pid3 < 0) {
142         // Failed to create the process
143         fprintf(stderr, "Fork failed for Process Three\n");
144         return 1;
145     } else if (pid3 == 0) {
146         // Child process three
147         ProcessRetry(); // Specific operations for Child Process Three can be added here
148         return 0;
149     }
150 
151     // Wait for all child processes to finish
152     waitpid(pid1, &status, 0);
153     waitpid(pid2, &status, 0);
154     waitpid(pid3, &status, 0);
155 
156     char *BufferAbort = strdup(ReadFile(FILE_ABORT));
157     char *BufferIgnore = strdup(ReadFile(FILE_IGNORE));
158     char *BufferRetry = strdup(ReadFile(FILE_RETRY));
159 
160     if (strcmp(BufferAbort, ASSERT_ABORT_INFO) == 0 && strcmp(BufferIgnore, ASSERT_IGNORE_INFO) == 0
161     && strcmp(BufferRetry, ASSERT_RETRY_INFO) == 0) {
162         printf("All processes finished.\n");
163     }
164 
165     free(BufferAbort);
166     free(BufferIgnore);
167     free(BufferRetry);
168 
169     return 0;
170 }