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 #include <time.h> | 19 #include <time.h> |
20 | 20 |
21 #include <string> | 21 #include <string> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/mac/foundation_util.h" | 24 #include "base/mac/foundation_util.h" |
25 #include "base/mac/scoped_cftyperef.h" | 25 #include "base/mac/scoped_cftyperef.h" |
26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
27 #include "base/strings/sys_string_conversions.h" | 27 #include "base/strings/sys_string_conversions.h" |
28 #include "base/rand_util.h" | 28 #include "base/rand_util.h" |
29 #include "gtest/gtest.h" | 29 #include "gtest/gtest.h" |
30 #include "util/mac/mac_util.h" | |
31 #include "util/posix/process_util.h" | 30 #include "util/posix/process_util.h" |
32 #include "util/stdlib/objc.h" | 31 #include "util/stdlib/objc.h" |
33 | 32 |
34 namespace { | 33 namespace { |
35 | 34 |
36 using namespace crashpad; | 35 using namespace crashpad; |
37 | 36 |
38 // 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 |
39 // requiring that its argv[argc - 1] compare equal to last_arg. | 38 // requiring that its argv[argc - 1] compare equal to last_arg. |
40 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) { | 39 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) { |
41 // The process may not have called exec yet, so loop with a small delay while | 40 // The process may not have called exec yet, so loop with a small delay while |
42 // looking for the cookie. | 41 // looking for the cookie. |
43 int outer_tries = 10; | 42 int outer_tries = 10; |
44 std::vector<std::string> job_argv; | 43 std::vector<std::string> job_argv; |
45 while (outer_tries--) { | 44 while (outer_tries--) { |
46 // If the process is in the middle of calling exec, ProcessArgumentsForPID | 45 // If the process is in the middle of calling exec, ProcessArgumentsForPID |
47 // may fail. Loop with a small retry delay while waiting for the expected | 46 // may fail. Loop with a small retry delay while waiting for the expected |
48 // successful call. | 47 // successful call. |
49 int inner_tries = 10; | 48 int inner_tries = 10; |
50 bool success; | 49 bool success; |
51 do { | 50 do { |
52 success = ProcessArgumentsForPID(pid, &job_argv); | 51 success = ProcessArgumentsForPID(pid, &job_argv); |
53 if (success) { | 52 if (success) { |
54 break; | 53 break; |
55 } | 54 } |
56 if (inner_tries > 0) { | 55 if (inner_tries > 0) { |
57 timespec sleep_time; | 56 timespec sleep_time; |
58 sleep_time.tv_sec = 0; | 57 sleep_time.tv_sec = 0; |
59 sleep_time.tv_nsec = 1E5; // 100 microseconds | 58 sleep_time.tv_nsec = 1E6; // 1 millisecond |
60 nanosleep(&sleep_time, NULL); | 59 nanosleep(&sleep_time, NULL); |
61 } | 60 } |
62 } while (inner_tries--); | 61 } while (inner_tries--); |
63 ASSERT_TRUE(success); | 62 ASSERT_TRUE(success); |
64 | 63 |
65 ASSERT_TRUE(ProcessArgumentsForPID(pid, &job_argv)); | 64 ASSERT_TRUE(ProcessArgumentsForPID(pid, &job_argv)); |
66 ASSERT_FALSE(job_argv.empty()); | 65 ASSERT_FALSE(job_argv.empty()); |
67 if (job_argv.back() == last_arg) { | 66 if (job_argv.back() == last_arg) { |
68 break; | 67 break; |
69 } | 68 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 ASSERT_GT(job_pid, 0); | 150 ASSERT_GT(job_pid, 0); |
152 | 151 |
153 ExpectProcessIsRunning(job_pid, shell_script); | 152 ExpectProcessIsRunning(job_pid, shell_script); |
154 | 153 |
155 // Remove the job. | 154 // Remove the job. |
156 ASSERT_TRUE(ServiceManagementRemoveJob(kJobLabel, true)); | 155 ASSERT_TRUE(ServiceManagementRemoveJob(kJobLabel, true)); |
157 EXPECT_FALSE(ServiceManagementIsJobLoaded(kJobLabel)); | 156 EXPECT_FALSE(ServiceManagementIsJobLoaded(kJobLabel)); |
158 EXPECT_EQ(0, ServiceManagementIsJobRunning(kJobLabel)); | 157 EXPECT_EQ(0, ServiceManagementIsJobRunning(kJobLabel)); |
159 | 158 |
160 // Now that the job is unloaded, a subsequent attempt to unload it should be | 159 // Now that the job is unloaded, a subsequent attempt to unload it should be |
161 // an error. However, ServiceManagementRemoveJob does not properly report | 160 // an error. |
162 // this error case on Mac OS X 10.10. | 161 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false)); |
163 if (MacOSXMinorVersion() >= 10) { | |
164 // If this check starts failing because radar 18268941 is fixed, remove | |
165 // the OS version check here and revise the interface documentation for | |
166 // ServiceManagementRemoveJob(). | |
167 EXPECT_TRUE(ServiceManagementRemoveJob(kJobLabel, false)); | |
168 } else { | |
169 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false)); | |
170 } | |
171 | 162 |
172 ExpectProcessIsNotRunning(job_pid, shell_script); | 163 ExpectProcessIsNotRunning(job_pid, shell_script); |
173 } | 164 } |
174 } | 165 } |
175 | 166 |
176 } // namespace | 167 } // namespace |
OLD | NEW |