| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/mac/service_management.h" | 15 #include "util/mac/service_management.h" |
| 16 | 16 |
| 17 #import <Foundation/Foundation.h> | 17 #import <Foundation/Foundation.h> |
| 18 #include <launch.h> | 18 #include <launch.h> |
| 19 | 19 |
| 20 #include <string> | 20 #include <string> |
| 21 #include <vector> | 21 #include <vector> |
| 22 | 22 |
| 23 #include "base/mac/foundation_util.h" | 23 #include "base/mac/foundation_util.h" |
| 24 #include "base/mac/scoped_cftyperef.h" | 24 #include "base/mac/scoped_cftyperef.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/strings/sys_string_conversions.h" | 26 #include "base/strings/sys_string_conversions.h" |
| 27 #include "base/rand_util.h" | 27 #include "base/rand_util.h" |
| 28 #include "gtest/gtest.h" | 28 #include "gtest/gtest.h" |
| 29 #include "util/misc/clock.h" | 29 #include "util/misc/clock.h" |
| 30 #include "util/posix/process_util.h" | 30 #include "util/posix/process_info.h" |
| 31 #include "util/stdlib/objc.h" | 31 #include "util/stdlib/objc.h" |
| 32 | 32 |
| 33 namespace crashpad { | 33 namespace crashpad { |
| 34 namespace test { | 34 namespace test { |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Ensures that the process with the specified PID is running, identifying it by | 37 // Ensures that the process with the specified PID is running, identifying it by |
| 38 // requiring that its argv[argc - 1] compare equal to last_arg. | 38 // requiring that its argv[argc - 1] compare equal to last_arg. |
| 39 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) { | 39 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) { |
| 40 ProcessInfo process_info; |
| 41 ASSERT_TRUE(process_info.Initialize(pid)); |
| 42 |
| 40 // The process may not have called exec yet, so loop with a small delay while | 43 // The process may not have called exec yet, so loop with a small delay while |
| 41 // looking for the cookie. | 44 // looking for the cookie. |
| 42 int outer_tries = 10; | 45 int outer_tries = 10; |
| 43 std::vector<std::string> job_argv; | 46 std::vector<std::string> job_argv; |
| 44 while (outer_tries--) { | 47 while (outer_tries--) { |
| 45 // If the process is in the middle of calling exec, ProcessArgumentsForPID | 48 // If the process is in the middle of calling exec, process_info.Arguments() |
| 46 // may fail. Loop with a small retry delay while waiting for the expected | 49 // may fail. Loop with a small retry delay while waiting for the expected |
| 47 // successful call. | 50 // successful call. |
| 48 int inner_tries = 10; | 51 int inner_tries = 10; |
| 49 bool success; | 52 bool success; |
| 50 do { | 53 do { |
| 51 success = ProcessArgumentsForPID(pid, &job_argv); | 54 success = process_info.Arguments(&job_argv); |
| 52 if (success) { | 55 if (success) { |
| 53 break; | 56 break; |
| 54 } | 57 } |
| 55 if (inner_tries > 0) { | 58 if (inner_tries > 0) { |
| 56 SleepNanoseconds(1E6); // 1 millisecond | 59 SleepNanoseconds(1E6); // 1 millisecond |
| 57 } | 60 } |
| 58 } while (inner_tries--); | 61 } while (inner_tries--); |
| 59 ASSERT_TRUE(success); | 62 ASSERT_TRUE(success); |
| 60 | 63 |
| 61 ASSERT_FALSE(job_argv.empty()); | 64 ASSERT_FALSE(job_argv.empty()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 74 | 77 |
| 75 // Ensures that the process with the specified PID is not running. Because the | 78 // Ensures that the process with the specified PID is not running. Because the |
| 76 // PID may be reused for another process, a process is only treated as running | 79 // PID may be reused for another process, a process is only treated as running |
| 77 // if its argv[argc - 1] compares equal to last_arg. | 80 // if its argv[argc - 1] compares equal to last_arg. |
| 78 void ExpectProcessIsNotRunning(pid_t pid, std::string& last_arg) { | 81 void ExpectProcessIsNotRunning(pid_t pid, std::string& last_arg) { |
| 79 // The process may not have exited yet, so loop with a small delay while | 82 // The process may not have exited yet, so loop with a small delay while |
| 80 // checking that it has exited. | 83 // checking that it has exited. |
| 81 int tries = 10; | 84 int tries = 10; |
| 82 std::vector<std::string> job_argv; | 85 std::vector<std::string> job_argv; |
| 83 while (tries--) { | 86 while (tries--) { |
| 84 if (!ProcessArgumentsForPID(pid, &job_argv)) { | 87 ProcessInfo process_info; |
| 88 if (!process_info.Initialize(pid) || !process_info.Arguments(&job_argv)) { |
| 85 // The PID was not found. | 89 // The PID was not found. |
| 86 return; | 90 return; |
| 87 } | 91 } |
| 88 | 92 |
| 89 // The PID was found. It may have been recycled for another process. Make | 93 // The PID was found. It may have been recycled for another process. Make |
| 90 // sure that the cookie isn’t found. | 94 // sure that the cookie isn’t found. |
| 91 ASSERT_FALSE(job_argv.empty()); | 95 ASSERT_FALSE(job_argv.empty()); |
| 92 if (job_argv.back() != last_arg) { | 96 if (job_argv.back() != last_arg) { |
| 93 break; | 97 break; |
| 94 } | 98 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // an error. | 154 // an error. |
| 151 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false)); | 155 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false)); |
| 152 | 156 |
| 153 ExpectProcessIsNotRunning(job_pid, shell_script); | 157 ExpectProcessIsNotRunning(job_pid, shell_script); |
| 154 } | 158 } |
| 155 } | 159 } |
| 156 | 160 |
| 157 } // namespace | 161 } // namespace |
| 158 } // namespace test | 162 } // namespace test |
| 159 } // namespace crashpad | 163 } // namespace crashpad |
| OLD | NEW |