OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (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 | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include <errno.h> | |
16 #include <limits.h> | |
17 #include <stdio.h> | |
18 #include <stdlib.h> | |
19 #include <sys/types.h> | |
20 | |
21 #include <algorithm> | |
22 | |
23 #if defined(__APPLE__) || defined(__linux__) | |
24 #define OS_POSIX 1 | |
25 #elif defined(_WIN32) | |
26 #define OS_WIN 1 | |
27 #endif | |
28 | |
29 #if defined(OS_POSIX) | |
30 #include <unistd.h> | |
31 #elif defined(OS_WIN) | |
32 #include <windows.h> | |
33 #endif | |
34 | |
35 int main(int argc, char* argv[]) { | |
36 #if defined(OS_POSIX) | |
37 // Make sure that there’s nothing open at any FD higher than 3. All FDs other | |
38 // than stdin, stdout, and stderr should have been closed prior to or at | |
39 // exec(). | |
40 int max_fd = std::max(static_cast<int>(sysconf(_SC_OPEN_MAX)), OPEN_MAX); | |
41 max_fd = std::max(max_fd, getdtablesize()); | |
42 for (int fd = STDERR_FILENO + 1; fd < max_fd; ++fd) { | |
43 if (close(fd) == 0 || errno != EBADF) { | |
44 abort(); | |
45 } | |
46 } | |
47 | |
48 // Read a byte from stdin, expecting it to be a specific value. | |
49 char c; | |
50 ssize_t rv = read(STDIN_FILENO, &c, 1); | |
51 if (rv != 1 || c != 'z') { | |
52 abort(); | |
53 } | |
54 | |
55 // Write a byte to stdout. | |
56 c = 'Z'; | |
57 rv = write(STDOUT_FILENO, &c, 1); | |
58 if (rv != 1) { | |
59 abort(); | |
60 } | |
61 #elif defined(OS_WIN) | |
62 // TODO(scottmg): Verify that only the handles we expect to be open, are. | |
63 | |
64 // Read a byte from stdin, expecting it to be a specific value. | |
65 char c; | |
66 DWORD bytes_read; | |
67 HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE); | |
68 if (!ReadFile(stdin_handle, &c, 1, &bytes_read, nullptr) || | |
69 bytes_read != 1 || c != 'z') { | |
70 abort(); | |
71 } | |
72 | |
73 // Write a byte to stdout. | |
74 c = 'Z'; | |
75 DWORD bytes_written; | |
76 if (!WriteFile( | |
77 GetStdHandle(STD_OUTPUT_HANDLE), &c, 1, &bytes_written, nullptr) || | |
78 bytes_written != 1) { | |
79 abort(); | |
80 } | |
81 #endif // OS_POSIX | |
82 | |
83 return 0; | |
84 } | |
OLD | NEW |