Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: base/process/process_unittest.cc

Issue 831363002: Add the ability to run a callback between fork and exec. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve pre_exec_delegate comment. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/process/launch_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/process/process.h" 5 #include "base/process/process.h"
6 6
7 #include "base/files/scoped_file.h"
8 #include "base/posix/eintr_wrapper.h"
7 #include "base/process/kill.h" 9 #include "base/process/kill.h"
8 #include "base/test/multiprocess_test.h" 10 #include "base/test/multiprocess_test.h"
9 #include "base/test/test_timeouts.h" 11 #include "base/test/test_timeouts.h"
10 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/multiprocess_func_list.h" 14 #include "testing/multiprocess_func_list.h"
13 15
14 16
15 namespace { 17 namespace {
16 18
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 EXPECT_TRUE(process.SetProcessBackgrounded(false)); 196 EXPECT_TRUE(process.SetProcessBackgrounded(false));
195 EXPECT_FALSE(process.IsProcessBackgrounded()); 197 EXPECT_FALSE(process.IsProcessBackgrounded());
196 #else 198 #else
197 process.SetProcessBackgrounded(true); 199 process.SetProcessBackgrounded(true);
198 process.SetProcessBackgrounded(false); 200 process.SetProcessBackgrounded(false);
199 #endif 201 #endif
200 int new_priority = process.GetPriority(); 202 int new_priority = process.GetPriority();
201 EXPECT_EQ(old_priority, new_priority); 203 EXPECT_EQ(old_priority, new_priority);
202 } 204 }
203 205
206 #if defined(OS_POSIX)
207 const char kPipeValue = '\xcc';
208
209 class ReadFromPipeDelegate : public LaunchOptions::PreExecDelegate {
210 public:
211 explicit ReadFromPipeDelegate(int fd) : fd_(fd) {}
212 ~ReadFromPipeDelegate() override {}
213 void RunAsyncSafe() override {
214 char c;
215 RAW_CHECK(HANDLE_EINTR(read(fd_, &c, 1)) == 1);
216 RAW_CHECK(IGNORE_EINTR(close(fd_)) == 0);
217 RAW_CHECK(c == kPipeValue);
218 }
219
220 private:
221 int fd_;
222 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate);
223 };
224
225 TEST_F(ProcessTest, PreExecHook) {
226 int pipe_fds[2];
227 ASSERT_EQ(0, pipe(pipe_fds));
228
229 ScopedFD read_fd(pipe_fds[0]);
230 ScopedFD write_fd(pipe_fds[1]);
231 base::FileHandleMappingVector fds_to_remap;
232 fds_to_remap.push_back(std::make_pair(read_fd.get(), read_fd.get()));
233
234 ReadFromPipeDelegate read_from_pipe_delegate(read_fd.get());
235 LaunchOptions options;
236 options.fds_to_remap = &fds_to_remap;
237 options.pre_exec_delegate = &read_from_pipe_delegate;
238 Process process(SpawnChildWithOptions("SimpleChildProcess", options));
239 ASSERT_TRUE(process.IsValid());
240
241 read_fd.reset();
242 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd.get(), &kPipeValue, 1)));
243 write_fd.reset();
jln (very slow on Chromium) 2015/01/15 19:42:56 I don't think that's needed, right?
rickyz (no longer on Chrome) 2015/01/15 22:03:03 Yeah, neither of the resets are technically needed
jln (very slow on Chromium) 2015/01/15 22:06:48 The first one is needed so that you get SIGPIPE-ed
rickyz (no longer on Chrome) 2015/01/15 22:21:41 Ahh, I actually had not realized that subtle detai
244
245 int exit_code = 42;
246 EXPECT_TRUE(process.WaitForExit(&exit_code));
247 EXPECT_EQ(0, exit_code);
248 }
249 #endif // defined(OS_POSIX)
250
204 } // namespace base 251 } // namespace base
OLDNEW
« no previous file with comments | « base/process/launch_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698