OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <fcntl.h> | 5 #include <fcntl.h> |
6 #include <poll.h> | 6 #include <poll.h> |
7 #include <signal.h> | 7 #include <signal.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <sys/resource.h> | 9 #include <sys/resource.h> |
10 #include <sys/time.h> | 10 #include <sys/time.h> |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 143 |
144 // Disable core files. They are not very useful for our individual test | 144 // Disable core files. They are not very useful for our individual test |
145 // cases. | 145 // cases. |
146 struct rlimit no_core = {0}; | 146 struct rlimit no_core = {0}; |
147 setrlimit(RLIMIT_CORE, &no_core); | 147 setrlimit(RLIMIT_CORE, &no_core); |
148 | 148 |
149 test(arg); | 149 test(arg); |
150 _exit(kExpectedValue); | 150 _exit(kExpectedValue); |
151 } | 151 } |
152 | 152 |
153 (void)HANDLE_EINTR(close(fds[1])); | 153 close(fds[1]); |
154 std::vector<char> msg_buf; | 154 std::vector<char> msg_buf; |
155 ssize_t rc; | 155 ssize_t rc; |
156 | 156 |
157 // Make sure read() will never block as we'll use poll() to | 157 // Make sure read() will never block as we'll use poll() to |
158 // block with a timeout instead. | 158 // block with a timeout instead. |
159 const int fcntl_ret = fcntl(fds[0], F_SETFL, O_NONBLOCK); | 159 const int fcntl_ret = fcntl(fds[0], F_SETFL, O_NONBLOCK); |
160 ASSERT_EQ(fcntl_ret, 0); | 160 ASSERT_EQ(fcntl_ret, 0); |
161 struct pollfd poll_fd = {fds[0], POLLIN | POLLRDHUP, 0}; | 161 struct pollfd poll_fd = {fds[0], POLLIN | POLLRDHUP, 0}; |
162 | 162 |
163 int poll_ret; | 163 int poll_ret; |
164 // We prefer the SIGALRM timeout to trigger in the child than this timeout | 164 // We prefer the SIGALRM timeout to trigger in the child than this timeout |
165 // so we double the common value here. | 165 // so we double the common value here. |
166 int poll_timeout = GetSubProcessTimeoutTimeInSeconds() * 2 * 1000; | 166 int poll_timeout = GetSubProcessTimeoutTimeInSeconds() * 2 * 1000; |
167 while ((poll_ret = poll(&poll_fd, 1, poll_timeout) > 0)) { | 167 while ((poll_ret = poll(&poll_fd, 1, poll_timeout) > 0)) { |
168 const size_t kCapacity = 256; | 168 const size_t kCapacity = 256; |
169 const size_t len = msg_buf.size(); | 169 const size_t len = msg_buf.size(); |
170 msg_buf.resize(len + kCapacity); | 170 msg_buf.resize(len + kCapacity); |
171 rc = HANDLE_EINTR(read(fds[0], &msg_buf[len], kCapacity)); | 171 rc = HANDLE_EINTR(read(fds[0], &msg_buf[len], kCapacity)); |
172 msg_buf.resize(len + std::max(rc, static_cast<ssize_t>(0))); | 172 msg_buf.resize(len + std::max(rc, static_cast<ssize_t>(0))); |
173 if (rc <= 0) | 173 if (rc <= 0) |
174 break; | 174 break; |
175 } | 175 } |
176 ASSERT_NE(poll_ret, -1) << "poll() failed"; | 176 ASSERT_NE(poll_ret, -1) << "poll() failed"; |
177 ASSERT_NE(poll_ret, 0) << "Timeout while reading child state"; | 177 ASSERT_NE(poll_ret, 0) << "Timeout while reading child state"; |
178 (void)HANDLE_EINTR(close(fds[0])); | 178 close(fds[0]); |
179 std::string msg(msg_buf.begin(), msg_buf.end()); | 179 std::string msg(msg_buf.begin(), msg_buf.end()); |
180 | 180 |
181 int status = 0; | 181 int status = 0; |
182 int waitpid_returned = HANDLE_EINTR(waitpid(pid, &status, 0)); | 182 int waitpid_returned = HANDLE_EINTR(waitpid(pid, &status, 0)); |
183 ASSERT_EQ(pid, waitpid_returned) << TestFailedMessage(msg); | 183 ASSERT_EQ(pid, waitpid_returned) << TestFailedMessage(msg); |
184 | 184 |
185 // At run-time, we sometimes decide that a test shouldn't actually | 185 // At run-time, we sometimes decide that a test shouldn't actually |
186 // run (e.g. when testing sandbox features on a kernel that doesn't | 186 // run (e.g. when testing sandbox features on a kernel that doesn't |
187 // have sandboxing support). When that happens, don't attempt to | 187 // have sandboxing support). When that happens, don't attempt to |
188 // call the "death" function, as it might be looking for a | 188 // call the "death" function, as it might be looking for a |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 fflush(stderr); | 252 fflush(stderr); |
253 _exit(kExitWithAssertionFailure); | 253 _exit(kExitWithAssertionFailure); |
254 } | 254 } |
255 | 255 |
256 void UnitTests::IgnoreThisTest() { | 256 void UnitTests::IgnoreThisTest() { |
257 fflush(stderr); | 257 fflush(stderr); |
258 _exit(kIgnoreThisTest); | 258 _exit(kIgnoreThisTest); |
259 } | 259 } |
260 | 260 |
261 } // namespace | 261 } // namespace |
OLD | NEW |