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/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "testing/multiprocess_func_list.h" | 11 #include "testing/multiprocess_func_list.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 // A very basic implementation for android. On Android tests can run in an APK | 15 // 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 | 16 // 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). | 17 // minimum to execute the method specified by procname (in the child process). |
| 18 // - All options except |fds_to_remap| are ignored. |
18 // - |debug_on_start| is ignored. | 19 // - |debug_on_start| is ignored. |
19 ProcessHandle MultiProcessTest::SpawnChildImpl( | 20 ProcessHandle MultiProcessTest::SpawnChildWithOptions( |
20 const std::string& procname, | 21 const std::string& procname, |
21 const FileHandleMappingVector& fds_to_remap, | 22 const LaunchOptions& options, |
22 bool debug_on_start) { | 23 bool debug_on_start) { |
| 24 // TODO(vtl): The FD-remapping done below is wrong in the presence of cycles |
| 25 // (e.g., fd1 -> fd2, fd2 -> fd1). crbug.com/326576 |
| 26 FileHandleMappingVector empty; |
| 27 const FileHandleMappingVector* fds_to_remap = |
| 28 options.fds_to_remap ? options.fds_to_remap : ∅ |
| 29 |
23 pid_t pid = fork(); | 30 pid_t pid = fork(); |
24 | 31 |
25 if (pid < 0) { | 32 if (pid < 0) { |
26 PLOG(ERROR) << "fork"; | 33 PLOG(ERROR) << "fork"; |
27 return kNullProcessHandle; | 34 return kNullProcessHandle; |
28 } | 35 } |
29 if (pid > 0) { | 36 if (pid > 0) { |
30 // Parent process. | 37 // Parent process. |
31 return pid; | 38 return pid; |
32 } | 39 } |
33 // Child process. | 40 // Child process. |
34 std::hash_set<int> fds_to_keep_open; | 41 std::hash_set<int> fds_to_keep_open; |
35 for (FileHandleMappingVector::const_iterator it = fds_to_remap.begin(); | 42 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); |
36 it != fds_to_remap.end(); ++it) { | 43 it != fds_to_remap->end(); ++it) { |
37 fds_to_keep_open.insert(it->first); | 44 fds_to_keep_open.insert(it->first); |
38 } | 45 } |
39 // Keep stdin, stdout and stderr open since this is not meant to spawn a | 46 // Keep stdin, stdout and stderr open since this is not meant to spawn a |
40 // daemon. | 47 // daemon. |
41 const int kFdForAndroidLogging = 3; // FD used by __android_log_write(). | 48 const int kFdForAndroidLogging = 3; // FD used by __android_log_write(). |
42 for (int fd = kFdForAndroidLogging + 1; fd < getdtablesize(); ++fd) { | 49 for (int fd = kFdForAndroidLogging + 1; fd < getdtablesize(); ++fd) { |
43 if (fds_to_keep_open.find(fd) == fds_to_keep_open.end()) { | 50 if (fds_to_keep_open.find(fd) == fds_to_keep_open.end()) { |
44 close(fd); | 51 close(fd); |
45 } | 52 } |
46 } | 53 } |
47 for (FileHandleMappingVector::const_iterator it = fds_to_remap.begin(); | 54 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); |
48 it != fds_to_remap.end(); ++it) { | 55 it != fds_to_remap->end(); ++it) { |
49 int old_fd = it->first; | 56 int old_fd = it->first; |
50 int new_fd = it->second; | 57 int new_fd = it->second; |
51 if (dup2(old_fd, new_fd) < 0) { | 58 if (dup2(old_fd, new_fd) < 0) { |
52 PLOG(FATAL) << "dup2"; | 59 PLOG(FATAL) << "dup2"; |
53 } | 60 } |
54 close(old_fd); | 61 close(old_fd); |
55 } | 62 } |
56 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); | 63 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); |
57 return 0; | 64 return 0; |
58 } | 65 } |
59 | 66 |
60 } // namespace base | 67 } // namespace base |
OLD | NEW |