OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/posix/close_multiple.h" | 15 #include "util/posix/close_multiple.h" |
16 | 16 |
17 #include <dirent.h> | 17 #include <dirent.h> |
18 #include <errno.h> | 18 #include <errno.h> |
19 #include <fcntl.h> | 19 #include <fcntl.h> |
20 #include <limits.h> | 20 #include <limits.h> |
21 #include <stdio.h> | 21 #include <stdio.h> |
22 #include <stdlib.h> | 22 #include <stdlib.h> |
23 #include <string.h> | 23 #include <string.h> |
24 #include <unistd.h> | 24 #include <unistd.h> |
25 | 25 |
26 #include <algorithm> | 26 #include <algorithm> |
27 #include <memory> | |
28 | 27 |
29 #include "base/files/scoped_file.h" | 28 #include "base/files/scoped_file.h" |
30 #include "base/logging.h" | 29 #include "base/logging.h" |
31 #include "base/posix/eintr_wrapper.h" | 30 #include "base/posix/eintr_wrapper.h" |
32 #include "build/build_config.h" | 31 #include "build/build_config.h" |
33 #include "util/misc/implicit_cast.h" | 32 #include "util/misc/implicit_cast.h" |
34 #include "util/numeric/safe_assignment.h" | 33 #include "util/numeric/safe_assignment.h" |
| 34 #include "util/posix/scoped_dir.h" |
35 | 35 |
36 #if defined(OS_MACOSX) | 36 #if defined(OS_MACOSX) |
37 #include <sys/sysctl.h> | 37 #include <sys/sysctl.h> |
38 #endif | 38 #endif |
39 | 39 |
40 // Everything in this file is expected to execute between fork() and exec(), | 40 // Everything in this file is expected to execute between fork() and exec(), |
41 // so everything called here must be acceptable in this context. However, | 41 // so everything called here must be acceptable in this context. However, |
42 // logging code that is not expected to execute under normal circumstances is | 42 // logging code that is not expected to execute under normal circumstances is |
43 // currently permitted. | 43 // currently permitted. |
44 | 44 |
(...skipping 17 matching lines...) Expand all Loading... |
62 } | 62 } |
63 PLOG(WARNING) << "fcntl"; | 63 PLOG(WARNING) << "fcntl"; |
64 #endif | 64 #endif |
65 | 65 |
66 rv = IGNORE_EINTR(close(fd)); | 66 rv = IGNORE_EINTR(close(fd)); |
67 if (rv != 0 && !(ebadf_ok && errno == EBADF)) { | 67 if (rv != 0 && !(ebadf_ok && errno == EBADF)) { |
68 PLOG(WARNING) << "close"; | 68 PLOG(WARNING) << "close"; |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 struct ScopedDIRCloser { | |
73 void operator()(DIR* dir) const { | |
74 if (dir) { | |
75 if (closedir(dir) < 0) { | |
76 PLOG(ERROR) << "closedir"; | |
77 } | |
78 } | |
79 } | |
80 }; | |
81 | |
82 using ScopedDIR = std::unique_ptr<DIR, ScopedDIRCloser>; | |
83 | |
84 // This function implements CloseMultipleNowOrOnExec() using an operating | 72 // This function implements CloseMultipleNowOrOnExec() using an operating |
85 // system-specific FD directory to determine which file descriptors are open. | 73 // system-specific FD directory to determine which file descriptors are open. |
86 // This is an advantage over looping over all possible file descriptors, because | 74 // This is an advantage over looping over all possible file descriptors, because |
87 // no attempt needs to be made to close file descriptors that are not open. | 75 // no attempt needs to be made to close file descriptors that are not open. |
88 bool CloseMultipleNowOrOnExecUsingFDDir(int fd, int preserve_fd) { | 76 bool CloseMultipleNowOrOnExecUsingFDDir(int fd, int preserve_fd) { |
89 #if defined(OS_MACOSX) | 77 #if defined(OS_MACOSX) |
90 const char kFDDir[] = "/dev/fd"; | 78 const char kFDDir[] = "/dev/fd"; |
91 #elif defined(OS_LINUX) || defined(OS_ANDROID) | 79 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
92 const char kFDDir[] = "/proc/self/fd"; | 80 const char kFDDir[] = "/proc/self/fd"; |
93 #endif | 81 #endif |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 #endif | 213 #endif |
226 | 214 |
227 for (int entry_fd = fd; entry_fd < max_fd; ++entry_fd) { | 215 for (int entry_fd = fd; entry_fd < max_fd; ++entry_fd) { |
228 if (entry_fd != preserve_fd) { | 216 if (entry_fd != preserve_fd) { |
229 CloseNowOrOnExec(entry_fd, true); | 217 CloseNowOrOnExec(entry_fd, true); |
230 } | 218 } |
231 } | 219 } |
232 } | 220 } |
233 | 221 |
234 } // namespace crashpad | 222 } // namespace crashpad |
OLD | NEW |