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 <errno.h> | 15 #include <errno.h> |
16 #include <limits.h> | 16 #include <limits.h> |
17 #include <stdio.h> | 17 #include <stdio.h> |
18 #include <stdlib.h> | 18 #include <stdlib.h> |
19 #include <sys/types.h> | 19 #include <sys/types.h> |
20 #include <unistd.h> | |
21 | 20 |
22 #include <algorithm> | 21 #include <algorithm> |
23 | 22 |
23 #include "build/build_config.h" | |
24 | |
25 #if defined(OS_POSIX) | |
26 #include <unistd.h> | |
27 #elif defined(OS_WIN) | |
28 #include <windows.h> | |
29 #endif | |
30 | |
24 int main(int argc, char* argv[]) { | 31 int main(int argc, char* argv[]) { |
32 #if defined(OS_POSIX) | |
25 // Make sure that there’s nothing open at any FD higher than 3. All FDs other | 33 // Make sure that there’s nothing open at any FD higher than 3. All FDs other |
26 // than stdin, stdout, and stderr should have been closed prior to or at | 34 // than stdin, stdout, and stderr should have been closed prior to or at |
27 // exec(). | 35 // exec(). |
28 int max_fd = std::max(static_cast<int>(sysconf(_SC_OPEN_MAX)), OPEN_MAX); | 36 int max_fd = std::max(static_cast<int>(sysconf(_SC_OPEN_MAX)), OPEN_MAX); |
29 max_fd = std::max(max_fd, getdtablesize()); | 37 max_fd = std::max(max_fd, getdtablesize()); |
30 for (int fd = STDERR_FILENO + 1; fd < max_fd; ++fd) { | 38 for (int fd = STDERR_FILENO + 1; fd < max_fd; ++fd) { |
31 if (close(fd) == 0 || errno != EBADF) { | 39 if (close(fd) == 0 || errno != EBADF) { |
32 abort(); | 40 abort(); |
33 } | 41 } |
34 } | 42 } |
35 | 43 |
36 // Read a byte from stdin, expecting it to be a specific value. | 44 // Read a byte from stdin, expecting it to be a specific value. |
37 char c; | 45 char c; |
38 ssize_t rv = read(STDIN_FILENO, &c, 1); | 46 ssize_t rv = read(STDIN_FILENO, &c, 1); |
39 if (rv != 1 || c != 'z') { | 47 if (rv != 1 || c != 'z') { |
40 abort(); | 48 abort(); |
41 } | 49 } |
42 | 50 |
43 // Write a byte to stdout. | 51 // Write a byte to stdout. |
44 c = 'Z'; | 52 c = 'Z'; |
45 rv = write(STDOUT_FILENO, &c, 1); | 53 rv = write(STDOUT_FILENO, &c, 1); |
46 if (rv != 1) { | 54 if (rv != 1) { |
47 abort(); | 55 abort(); |
48 } | 56 } |
49 | 57 |
58 #elif defined(OS_WIN) | |
59 | |
60 // Make sure there's nothing open other than stdin, stdout, and stderr. | |
61 HANDLE handle_stdin = GetStdHandle(STD_INPUT_HANDLE); | |
62 HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
63 HANDLE handle_stderr = GetStdHandle(STD_ERROR_HANDLE); | |
64 // Handles can theoretically go as high as 2^32-4, but in practice they're | |
Mark Mentovai
2015/01/08 17:20:20
I thought Windows had an API to do this a little m
Mark Mentovai
2015/01/08 17:20:20
Blank line before the comment, to make it easier t
scottmg
2015/01/08 18:15:53
Sure, I actually used that at first based on https
| |
65 // never that large, so we check only up to 2^16. | |
66 for (uintptr_t handle_int = 4; handle_int <= 0xffff; handle_int += 4) { | |
67 HANDLE handle = reinterpret_cast<HANDLE>(handle_int); | |
68 if (handle == handle_stdin || handle == handle_stdout || | |
69 handle == handle_stderr) | |
70 continue; | |
Mark Mentovai
2015/01/08 17:20:20
{} me (the condition takes up more than one line,
scottmg
2015/01/08 18:15:53
Removed.
| |
71 if (CloseHandle(handle)) { | |
Mark Mentovai
2015/01/08 17:20:20
Can CloseHandle() fail for a reason other than an
scottmg
2015/01/08 18:15:53
I don't know of any reason, but there are so many
| |
72 abort(); | |
73 } | |
74 } | |
75 | |
76 // Read a byte from stdin, expecting it to be a specific value. | |
77 char c; | |
78 DWORD bytes_read; | |
79 if (!ReadFile(handle_stdin, &c, 1, &bytes_read, nullptr) || bytes_read != 1 || | |
80 c != 'z') { | |
81 abort(); | |
82 } | |
83 | |
84 // Write a byte to stdout. | |
85 c = 'Z'; | |
86 DWORD bytes_written; | |
87 if (!WriteFile(handle_stdout, &c, 1, &bytes_written, nullptr) || | |
88 bytes_written != 1) { | |
89 abort(); | |
90 } | |
91 | |
92 #endif // OS_POSIX | |
93 | |
50 return 0; | 94 return 0; |
51 } | 95 } |
OLD | NEW |