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/posix/global_descriptors.h" | 5 #include "base/posix/global_descriptors.h" |
6 #include "base/test/multiprocess_test.h" | 6 #include "base/test/multiprocess_test.h" |
7 | 7 |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 if (pid < 0) { | 32 if (pid < 0) { |
33 PLOG(ERROR) << "fork"; | 33 PLOG(ERROR) << "fork"; |
34 return kNullProcessHandle; | 34 return kNullProcessHandle; |
35 } | 35 } |
36 if (pid > 0) { | 36 if (pid > 0) { |
37 // Parent process. | 37 // Parent process. |
38 return pid; | 38 return pid; |
39 } | 39 } |
40 // Child process. | 40 // Child process. |
41 std::hash_set<int> fds_to_keep_open; | 41 base::hash_set<int> fds_to_keep_open; |
42 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); | 42 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); |
43 it != fds_to_remap->end(); ++it) { | 43 it != fds_to_remap->end(); ++it) { |
44 fds_to_keep_open.insert(it->first); | 44 fds_to_keep_open.insert(it->first); |
45 } | 45 } |
46 // Keep standard FDs (stdin, stdout, stderr, etc.) open since this | 46 // Keep standard FDs (stdin, stdout, stderr, etc.) open since this |
47 // is not meant to spawn a daemon. | 47 // is not meant to spawn a daemon. |
48 int base = GlobalDescriptors::kBaseDescriptor; | 48 int base = GlobalDescriptors::kBaseDescriptor; |
49 for (int fd = base; fd < sysconf(_SC_OPEN_MAX); ++fd) { | 49 for (int fd = base; fd < sysconf(_SC_OPEN_MAX); ++fd) { |
50 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()) { |
51 close(fd); | 51 close(fd); |
52 } | 52 } |
53 } | 53 } |
54 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); | 54 for (FileHandleMappingVector::const_iterator it = fds_to_remap->begin(); |
55 it != fds_to_remap->end(); ++it) { | 55 it != fds_to_remap->end(); ++it) { |
56 int old_fd = it->first; | 56 int old_fd = it->first; |
57 int new_fd = it->second; | 57 int new_fd = it->second; |
58 if (dup2(old_fd, new_fd) < 0) { | 58 if (dup2(old_fd, new_fd) < 0) { |
59 PLOG(FATAL) << "dup2"; | 59 PLOG(FATAL) << "dup2"; |
60 } | 60 } |
61 close(old_fd); | 61 close(old_fd); |
62 } | 62 } |
63 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); | 63 _exit(multi_process_function_list::InvokeChildProcessTest(procname)); |
64 return 0; | 64 return 0; |
65 } | 65 } |
66 | 66 |
67 } // namespace base | 67 } // namespace base |
OLD | NEW |