Update V8 to version 4.5.86.
[chromium-blink-merge.git] / base / test / multiprocess_test_android.cc
blobdc489d134d10a3e6c4a48f7335a0d5c08069477d
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
3 // found in the LICENSE file.
5 #include "base/test/multiprocess_test.h"
7 #include <unistd.h>
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/logging.h"
13 #include "base/posix/global_descriptors.h"
14 #include "testing/multiprocess_func_list.h"
16 namespace base {
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
20 // minimum to execute the method specified by procname (in the child process).
21 // - All options except |fds_to_remap| are ignored.
22 Process SpawnMultiProcessTestChild(const std::string& procname,
23 const CommandLine& base_command_line,
24 const LaunchOptions& options) {
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
27 FileHandleMappingVector empty;
28 const FileHandleMappingVector* fds_to_remap =
29 options.fds_to_remap ? options.fds_to_remap : &empty;
31 pid_t pid = fork();
33 if (pid < 0) {
34 PLOG(ERROR) << "fork";
35 return Process();
37 if (pid > 0) {
38 // Parent process.
39 return Process(pid);
41 // Child process.
42 base::hash_set<int> fds_to_keep_open;
43 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin();
44 it != fds_to_remap->end(); ++it) {
45 fds_to_keep_open.insert(it->first);
47 // Keep standard FDs (stdin, stdout, stderr, etc.) open since this
48 // is not meant to spawn a daemon.
49 int base = GlobalDescriptors::kBaseDescriptor;
50 for (int fd = base; fd < sysconf(_SC_OPEN_MAX); ++fd) {
51 if (fds_to_keep_open.find(fd) == fds_to_keep_open.end()) {
52 close(fd);
55 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin();
56 it != fds_to_remap->end(); ++it) {
57 int old_fd = it->first;
58 int new_fd = it->second;
59 if (dup2(old_fd, new_fd) < 0) {
60 PLOG(FATAL) << "dup2";
62 close(old_fd);
64 CommandLine::Reset();
65 CommandLine::Init(0, nullptr);
66 CommandLine* command_line = CommandLine::ForCurrentProcess();
67 command_line->InitFromArgv(base_command_line.argv());
68 if (!command_line->HasSwitch(switches::kTestChildProcess))
69 command_line->AppendSwitchASCII(switches::kTestChildProcess, procname);
71 _exit(multi_process_function_list::InvokeChildProcessTest(procname));
72 return Process();
75 } // namespace base