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, "Did not quiesce in 10 seconds\n");
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
RunWhenQuiesced(pid_t pid,bool leave_attached,std::function<PidRunEnum ()> fn)105 bool RunWhenQuiesced(pid_t pid, bool leave_attached, std::function<PidRunEnum()> fn) {
106 // Wait up to 120 seconds to run the fn.
107 PidRunEnum status = PID_RUN_KEEP_GOING;
108 for (time_t start_time = time(nullptr);
109 time(nullptr) - start_time < 120 && status == PID_RUN_KEEP_GOING;) {
110 if (Attach(pid)) {
111 status = fn();
112 if (status == PID_RUN_PASS && leave_attached) {
113 return true;
114 }
115
116 if (!Detach(pid)) {
117 return false;
118 }
119 } else if (Exited(pid)) {
120 return false;
121 }
122 usleep(5000);
123 }
124 if (status == PID_RUN_KEEP_GOING) {
125 fprintf(stderr, "Timed out waiting for pid %d to be ready\n", pid);
126 }
127 return status == PID_RUN_PASS;
128 }
129
130 } // namespace unwindstack
131