1 /*
2  * Copyright (C) 2017 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 #ifndef _LIBUNWINDSTACK_TESTS_TEST_UTILS_H
18 #define _LIBUNWINDSTACK_TESTS_TEST_UTILS_H
19 
20 #include <signal.h>
21 #include <sys/ptrace.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 
25 namespace unwindstack {
26 
27 class TestScopedPidReaper {
28  public:
29   TestScopedPidReaper(pid_t pid) : pid_(pid) {}
30   ~TestScopedPidReaper() {
31     kill(pid_, SIGKILL);
32     waitpid(pid_, nullptr, 0);
33   }
34 
35  private:
36   pid_t pid_;
37 };
38 
39 inline bool TestQuiescePid(pid_t pid) {
40   siginfo_t si;
41   bool ready = false;
42   // Wait for up to 5 seconds.
43   for (size_t i = 0; i < 5000; i++) {
44     if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
45       ready = true;
46       break;
47     }
48     usleep(1000);
49   }
50   return ready;
51 }
52 
53 }  // namespace unwindstack
54 
55 #endif  // _LIBUNWINDSTACK_TESTS_TEST_UTILS_H
56