xref: /aosp_15_r20/external/google-breakpad/src/common/mac/launch_reporter.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2014 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>  // Must come first
31 #endif
32 
33 #include <stdio.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 
37 namespace google_breakpad {
38 
LaunchReporter(const char * reporter_executable_path,const char * config_file_path)39 void LaunchReporter(const char *reporter_executable_path,
40                     const char *config_file_path) {
41   const char* argv[] = { reporter_executable_path, config_file_path, NULL };
42 
43   // Launch the reporter
44   pid_t pid = fork();
45 
46   if (pid == -1) {
47     perror("fork");
48     fprintf(stderr, "Failed to fork reporter process\n");
49     return;
50   }
51 
52   // If we're in the child, load in our new executable and run.
53   // The parent will not wait for the child to complete.
54   if (pid == 0) {
55     execv(argv[0], (char* const*)argv);
56     perror("exec");
57     fprintf(stderr,
58             "Failed to launch reporter process from path %s\n",
59             reporter_executable_path);
60     unlink(config_file_path);  // launch failed - get rid of config file
61     _exit(1);
62   }
63 
64   // Wait until the Reporter child process exits.
65   //
66 
67   // We'll use a timeout of one minute.
68   int timeout_count = 60;   // 60 seconds
69 
70   while (timeout_count-- > 0) {
71     int status;
72     pid_t result = waitpid(pid, &status, WNOHANG);
73 
74     if (result == 0) {
75       // The child has not yet finished.
76       sleep(1);
77     } else if (result == -1) {
78       // error occurred.
79       break;
80     } else {
81       // child has finished
82       break;
83     }
84   }
85 }
86 
87 }  // namespace google_breakpad
88