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 "base/test/multiprocess_test.h" | 5 #include "base/test/multiprocess_test.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "base/hash_tables.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/posix/eintr_wrapper.h" | |
10 #include "base/process.h" | 12 #include "base/process.h" |
11 #include "testing/multiprocess_func_list.h" | 13 #include "testing/multiprocess_func_list.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 | 16 |
15 // A very basic implementation for android. On Android tests can run in an APK | 17 // A very basic implementation for android. On Android tests can run in an APK |
16 // and we don't have an executable to exec*. This implementation does the bare | 18 // and we don't have an executable to exec*. This implementation does the bare |
17 // minimum to execute the method specified by procname (in the child process). | 19 // minimum to execute the method specified by procname (in the child process). |
18 // - File descriptors are not closed and hence |fds_to_map| is ignored. | |
19 // - |debug_on_start| is ignored. | 20 // - |debug_on_start| is ignored. |
20 ProcessHandle MultiProcessTest::SpawnChildImpl( | 21 ProcessHandle MultiProcessTest::SpawnChildImpl( |
21 const std::string& procname, | 22 const std::string& procname, |
22 const FileHandleMappingVector& fds_to_map, | 23 const FileHandleMappingVector& fds_to_remap, |
23 bool debug_on_start) { | 24 bool debug_on_start) { |
24 pid_t pid = fork(); | 25 pid_t pid = fork(); |
25 | 26 |
26 if (pid < 0) { | 27 if (pid < 0) { |
27 DPLOG(ERROR) << "fork"; | 28 PLOG(ERROR) << "fork"; |
28 return kNullProcessHandle; | 29 return kNullProcessHandle; |
29 } else if (pid == 0) { | |
30 // Child process. | |
31 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); | |
32 } | 30 } |
33 | 31 if (pid > 0) |
34 // Parent process. | 32 // Parent process. |
Paweł Hajdan Jr.
2012/11/26 18:18:41
nit: If you lay it out like that use braces {} for
Philippe
2012/11/26 18:30:44
Done.
| |
35 return pid; | 33 return pid; |
34 // Child process. | |
35 std::hash_set<int> fds_to_keep_open; | |
36 for (FileHandleMappingVector::const_iterator it = fds_to_remap.begin(); | |
37 it != fds_to_remap.end(); ++it) { | |
38 fds_to_keep_open.insert(it->first); | |
39 } | |
40 // Keep stdin, stdout and stderr open since this is not meant to spawn a | |
41 // daemon. | |
42 const int kFdForAndroidLogging = 3; // FD used by __android_log_write(). | |
43 for (int fd = kFdForAndroidLogging + 1; fd < getdtablesize(); ++fd) | |
44 if (fds_to_keep_open.find(fd) == fds_to_keep_open.end()) | |
45 HANDLE_EINTR(close(fd)); | |
Paweł Hajdan Jr.
2012/11/26 18:18:41
nit: This is a macro, so use {} around it.
Philippe
2012/11/26 18:30:44
I think that such macros should be written in a sa
| |
46 for (FileHandleMappingVector::const_iterator it = fds_to_remap.begin(); | |
47 it != fds_to_remap.end(); ++it) { | |
48 const int old_fd = it->first; | |
Paweł Hajdan Jr.
2012/11/26 18:18:41
nit: No need for "const int" if this is not really
Philippe
2012/11/26 18:30:44
Done.
| |
49 const int new_fd = it->second; | |
50 if (dup2(old_fd, new_fd) < 0) { | |
51 PLOG(ERROR) << "dup2"; | |
Paweł Hajdan Jr.
2012/11/26 18:18:41
Why do you make that non-fatal?
Philippe
2012/11/26 18:30:44
Done.
| |
52 continue; | |
53 } | |
54 HANDLE_EINTR(close(old_fd)); | |
55 } | |
56 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); | |
57 return 0; | |
36 } | 58 } |
37 | 59 |
38 } // namespace base | 60 } // namespace base |
OLD | NEW |