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/base_switches.h" | 9 #include "base/base_switches.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/posix/global_descriptors.h" | 13 #include "base/posix/global_descriptors.h" |
14 #include "testing/multiprocess_func_list.h" | 14 #include "testing/multiprocess_func_list.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 // A very basic implementation for Android. On Android tests can run in an APK | 18 // A very basic implementation for Android. On Android tests can run in an APK |
19 // and we don't have an executable to exec*. This implementation does the bare | 19 // and we don't have an executable to exec*. This implementation does the bare |
20 // minimum to execute the method specified by procname (in the child process). | 20 // minimum to execute the method specified by procname (in the child process). |
21 // - All options except |fds_to_remap| are ignored. | 21 // - All options except |fds_to_remap| are ignored. |
22 ProcessHandle SpawnMultiProcessTestChild(const std::string& procname, | 22 Process SpawnMultiProcessTestChild(const std::string& procname, |
23 const CommandLine& base_command_line, | 23 const CommandLine& base_command_line, |
24 const LaunchOptions& options) { | 24 const LaunchOptions& options) { |
25 // TODO(viettrungluu): The FD-remapping done below is wrong in the presence of | 25 // TODO(viettrungluu): The FD-remapping done below is wrong in the presence of |
26 // cycles (e.g., fd1 -> fd2, fd2 -> fd1). crbug.com/326576 | 26 // cycles (e.g., fd1 -> fd2, fd2 -> fd1). crbug.com/326576 |
27 FileHandleMappingVector empty; | 27 FileHandleMappingVector empty; |
28 const FileHandleMappingVector* fds_to_remap = | 28 const FileHandleMappingVector* fds_to_remap = |
29 options.fds_to_remap ? options.fds_to_remap : ∅ | 29 options.fds_to_remap ? options.fds_to_remap : ∅ |
30 | 30 |
31 pid_t pid = fork(); | 31 pid_t pid = fork(); |
32 | 32 |
33 if (pid < 0) { | 33 if (pid < 0) { |
34 PLOG(ERROR) << "fork"; | 34 PLOG(ERROR) << "fork"; |
35 return kNullProcessHandle; | 35 return Process(); |
36 } | 36 } |
37 if (pid > 0) { | 37 if (pid > 0) { |
38 // Parent process. | 38 // Parent process. |
39 return pid; | 39 return Process(pid); |
40 } | 40 } |
41 // Child process. | 41 // Child process. |
42 base::hash_set<int> fds_to_keep_open; | 42 base::hash_set<int> fds_to_keep_open; |
43 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); | 43 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); |
44 it != fds_to_remap->end(); ++it) { | 44 it != fds_to_remap->end(); ++it) { |
45 fds_to_keep_open.insert(it->first); | 45 fds_to_keep_open.insert(it->first); |
46 } | 46 } |
47 // Keep standard FDs (stdin, stdout, stderr, etc.) open since this | 47 // Keep standard FDs (stdin, stdout, stderr, etc.) open since this |
48 // is not meant to spawn a daemon. | 48 // is not meant to spawn a daemon. |
49 int base = GlobalDescriptors::kBaseDescriptor; | 49 int base = GlobalDescriptors::kBaseDescriptor; |
(...skipping 12 matching lines...) Expand all Loading... |
62 close(old_fd); | 62 close(old_fd); |
63 } | 63 } |
64 CommandLine::Reset(); | 64 CommandLine::Reset(); |
65 CommandLine::Init(0, nullptr); | 65 CommandLine::Init(0, nullptr); |
66 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 66 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
67 command_line->InitFromArgv(base_command_line.argv()); | 67 command_line->InitFromArgv(base_command_line.argv()); |
68 if (!command_line->HasSwitch(switches::kTestChildProcess)) | 68 if (!command_line->HasSwitch(switches::kTestChildProcess)) |
69 command_line->AppendSwitchASCII(switches::kTestChildProcess, procname); | 69 command_line->AppendSwitchASCII(switches::kTestChildProcess, procname); |
70 | 70 |
71 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); | 71 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); |
72 return 0; | 72 return Process(); |
73 } | 73 } |
74 | 74 |
75 } // namespace base | 75 } // namespace base |
OLD | NEW |