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"
9 #include "base/containers/hash_tables.h"
10 #include "base/logging.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/process.h"
13 #include "testing/multiprocess_func_list.h"
17 // A very basic implementation for android. On Android tests can run in an APK
18 // and we don't have an executable to exec*. This implementation does the bare
19 // minimum to execute the method specified by procname (in the child process).
20 // - |debug_on_start| is ignored.
21 ProcessHandle
MultiProcessTest::SpawnChildImpl(
22 const std::string
& procname
,
23 const FileHandleMappingVector
& fds_to_remap
,
24 bool debug_on_start
) {
28 PLOG(ERROR
) << "fork";
29 return kNullProcessHandle
;
36 std::hash_set
<int> fds_to_keep_open
;
37 for (FileHandleMappingVector::const_iterator it
= fds_to_remap
.begin();
38 it
!= fds_to_remap
.end(); ++it
) {
39 fds_to_keep_open
.insert(it
->first
);
41 // Keep stdin, stdout and stderr open since this is not meant to spawn a
43 const int kFdForAndroidLogging
= 3; // FD used by __android_log_write().
44 for (int fd
= kFdForAndroidLogging
+ 1; fd
< getdtablesize(); ++fd
) {
45 if (fds_to_keep_open
.find(fd
) == fds_to_keep_open
.end()) {
46 HANDLE_EINTR(close(fd
));
49 for (FileHandleMappingVector::const_iterator it
= fds_to_remap
.begin();
50 it
!= fds_to_remap
.end(); ++it
) {
51 int old_fd
= it
->first
;
52 int new_fd
= it
->second
;
53 if (dup2(old_fd
, new_fd
) < 0) {
54 PLOG(FATAL
) << "dup2";
56 HANDLE_EINTR(close(old_fd
));
58 _exit(multi_process_function_list::InvokeChildProcessTest(procname
));