1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <sys/ptrace.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <time.h>
24 #include <unistd.h>
25 
26 #include "PidUtils.h"
27 
28 namespace unwindstack {
29 
Exited(pid_t pid)30 static bool Exited(pid_t pid) {
31   int status;
32   pid_t wait_pid = waitpid(pid, &status, WNOHANG);
33   if (wait_pid != pid) {
34     return false;
35   }
36 
37   if (WIFEXITED(status)) {
38     fprintf(stderr, "%d died: Process exited with code %d\n", pid, WEXITSTATUS(status));
39   } else if (WIFSIGNALED(status)) {
40     fprintf(stderr, "%d died: Process exited due to signal %d\n", pid, WTERMSIG(status));
41   } else {
42     fprintf(stderr, "%d died: Process finished for unknown reason\n", pid);
43   }
44   return true;
45 }
46 
Quiesce(pid_t pid)47 bool Quiesce(pid_t pid) {
48   siginfo_t si;
49   // Wait for up to 10 seconds.
50   for (time_t start_time = time(nullptr); time(nullptr) - start_time < 10;) {
51     if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
52       return true;
53     }
54     if (errno != ESRCH) {
55       if (errno == EINVAL) {
56         // The process is in group-stop state, so try and kick the
57         // process out of that state.
58         if (ptrace(PTRACE_LISTEN, pid, 0, 0) == -1) {
59           // Cannot recover from this, so just pretend it worked and see
60           // if we can unwind.
61           return true;
62         }
63       } else {
64         perror("ptrace getsiginfo failed");
65         return false;
66       }
67     }
68     usleep(5000);
69   }
70   fprintf(stderr, "%d: Did not quiesce in 10 seconds\n", pid);
71   return false;
72 }
73 
Attach(pid_t pid)74 bool Attach(pid_t pid) {
75   // Wait up to 45 seconds to attach.
76   for (time_t start_time = time(nullptr); time(nullptr) - start_time < 45;) {
77     if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
78       break;
79     }
80     if (errno != ESRCH) {
81       perror("Failed to attach");
82       return false;
83     }
84     usleep(5000);
85   }
86 
87   if (Quiesce(pid)) {
88     return true;
89   }
90 
91   if (ptrace(PTRACE_DETACH, pid, 0, 0) == -1) {
92     perror("Failed to detach");
93   }
94   return false;
95 }
96 
Detach(pid_t pid)97 bool Detach(pid_t pid) {
98   if (ptrace(PTRACE_DETACH, pid, 0, 0) == -1) {
99     perror("ptrace detach failed");
100     return false;
101   }
102   return true;
103 }
104 
105 static constexpr time_t kMaxWaitTimeSeconds = 30;
106 
WaitForPidState(pid_t pid,const std::function<PidRunEnum ()> & state_check_func)107 bool WaitForPidState(pid_t pid, const std::function<PidRunEnum()>& state_check_func) {
108   PidRunEnum status = PID_RUN_KEEP_GOING;
109   for (time_t start_time = time(nullptr);
110        time(nullptr) - start_time < kMaxWaitTimeSeconds && status == PID_RUN_KEEP_GOING;) {
111     if (Attach(pid)) {
112       status = state_check_func();
113       if (status == PID_RUN_PASS) {
114         return true;
115       }
116 
117       if (!Detach(pid)) {
118         return false;
119       }
120     } else if (Exited(pid)) {
121       return false;
122     }
123     usleep(5000);
124   }
125   if (status == PID_RUN_KEEP_GOING) {
126     fprintf(stderr, "Timed out waiting for pid %d to be ready\n", pid);
127   }
128   return status == PID_RUN_PASS;
129 }
130 
WaitForPidStateAfterAttach(pid_t pid,const std::function<PidRunEnum ()> & state_check_func)131 bool WaitForPidStateAfterAttach(pid_t pid, const std::function<PidRunEnum()>& state_check_func) {
132   PidRunEnum status;
133   time_t start_time = time(nullptr);
134   do {
135     status = state_check_func();
136     if (status == PID_RUN_PASS) {
137       return true;
138     }
139     if (!Detach(pid)) {
140       return false;
141     }
142     usleep(5000);
143   } while (time(nullptr) - start_time < kMaxWaitTimeSeconds && status == PID_RUN_KEEP_GOING &&
144            Attach(pid));
145   if (status == PID_RUN_KEEP_GOING) {
146     fprintf(stderr, "Timed out waiting for pid %d to be ready\n", pid);
147   }
148   return status == PID_RUN_PASS;
149 }
150 
151 }  // namespace unwindstack
152