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/test/multiprocess_exec.h" | 15 #include "util/test/multiprocess_exec.h" |
16 | 16 |
17 #include <fcntl.h> | 17 #include <fcntl.h> |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
Mark Mentovai
2015/01/28 22:32:54
This needs <unistd.h> now too.
scottmg
2015/01/28 22:48:25
Done.
| |
20 | 20 |
21 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
22 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
23 #include "util/misc/scoped_forbid_return.h" | 23 #include "util/misc/scoped_forbid_return.h" |
24 #include "util/posix/close_multiple.h" | 24 #include "util/posix/close_multiple.h" |
25 #include "util/test/errors.h" | 25 #include "util/test/errors.h" |
26 | 26 |
27 namespace crashpad { | 27 namespace crashpad { |
28 namespace test { | 28 namespace test { |
29 | 29 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 } | 65 } |
66 | 66 |
67 void MultiprocessExec::MultiprocessChild() { | 67 void MultiprocessExec::MultiprocessChild() { |
68 // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively. | 68 // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively. |
69 // All FDs above this will be closed. | 69 // All FDs above this will be closed. |
70 static_assert(STDIN_FILENO == 0, "stdin must be fd 0"); | 70 static_assert(STDIN_FILENO == 0, "stdin must be fd 0"); |
71 static_assert(STDOUT_FILENO == 1, "stdout must be fd 1"); | 71 static_assert(STDOUT_FILENO == 1, "stdout must be fd 1"); |
72 static_assert(STDERR_FILENO == 2, "stderr must be fd 2"); | 72 static_assert(STDERR_FILENO == 2, "stderr must be fd 2"); |
73 | 73 |
74 // Move the read pipe to stdin. | 74 // Move the read pipe to stdin. |
75 int read_fd = ReadPipeFD(); | 75 FileHandle read_handle = ReadPipeHandle(); |
76 ASSERT_NE(read_fd, STDIN_FILENO); | 76 ASSERT_NE(read_handle, STDIN_FILENO); |
77 ASSERT_NE(read_fd, STDOUT_FILENO); | 77 ASSERT_NE(read_handle, STDOUT_FILENO); |
78 ASSERT_EQ(STDIN_FILENO, fileno(stdin)); | 78 ASSERT_EQ(STDIN_FILENO, fileno(stdin)); |
79 | 79 |
80 int rv = fpurge(stdin); | 80 int rv = fpurge(stdin); |
81 ASSERT_EQ(0, rv) << ErrnoMessage("fpurge"); | 81 ASSERT_EQ(0, rv) << ErrnoMessage("fpurge"); |
82 | 82 |
83 rv = HANDLE_EINTR(dup2(read_fd, STDIN_FILENO)); | 83 rv = HANDLE_EINTR(dup2(read_handle, STDIN_FILENO)); |
84 ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2"); | 84 ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2"); |
85 | 85 |
86 // Move the write pipe to stdout. | 86 // Move the write pipe to stdout. |
87 int write_fd = WritePipeFD(); | 87 FileHandle write_handle = WritePipeHandle(); |
88 ASSERT_NE(write_fd, STDIN_FILENO); | 88 ASSERT_NE(write_handle, STDIN_FILENO); |
89 ASSERT_NE(write_fd, STDOUT_FILENO); | 89 ASSERT_NE(write_handle, STDOUT_FILENO); |
90 ASSERT_EQ(STDOUT_FILENO, fileno(stdout)); | 90 ASSERT_EQ(STDOUT_FILENO, fileno(stdout)); |
91 | 91 |
92 // Make a copy of the original stdout file descriptor so that in case there’s | 92 // Make a copy of the original stdout file descriptor so that in case there’s |
93 // an execv() failure, the original stdout can be restored so that gtest | 93 // an execv() failure, the original stdout can be restored so that gtest |
94 // messages directed to stdout go to the right place. Mark it as | 94 // messages directed to stdout go to the right place. Mark it as |
95 // close-on-exec, so that the child won’t see it after a successful exec(), | 95 // close-on-exec, so that the child won’t see it after a successful exec(), |
96 // but it will still be available in this process after an unsuccessful | 96 // but it will still be available in this process after an unsuccessful |
97 // exec(). | 97 // exec(). |
98 int dup_orig_stdout_fd = dup(STDOUT_FILENO); | 98 int dup_orig_stdout_fd = dup(STDOUT_FILENO); |
99 ASSERT_GE(dup_orig_stdout_fd, 0) << ErrnoMessage("dup"); | 99 ASSERT_GE(dup_orig_stdout_fd, 0) << ErrnoMessage("dup"); |
100 | 100 |
101 rv = fcntl(dup_orig_stdout_fd, F_SETFD, FD_CLOEXEC); | 101 rv = fcntl(dup_orig_stdout_fd, F_SETFD, FD_CLOEXEC); |
102 ASSERT_NE(rv, -1) << ErrnoMessage("fcntl"); | 102 ASSERT_NE(rv, -1) << ErrnoMessage("fcntl"); |
103 | 103 |
104 rv = HANDLE_EINTR(fflush(stdout)); | 104 rv = HANDLE_EINTR(fflush(stdout)); |
105 ASSERT_EQ(0, rv) << ErrnoMessage("fflush"); | 105 ASSERT_EQ(0, rv) << ErrnoMessage("fflush"); |
106 | 106 |
107 rv = HANDLE_EINTR(dup2(write_fd, STDOUT_FILENO)); | 107 rv = HANDLE_EINTR(dup2(write_handle, STDOUT_FILENO)); |
108 ASSERT_EQ(STDOUT_FILENO, rv) << ErrnoMessage("dup2"); | 108 ASSERT_EQ(STDOUT_FILENO, rv) << ErrnoMessage("dup2"); |
109 | 109 |
110 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, dup_orig_stdout_fd); | 110 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, dup_orig_stdout_fd); |
111 | 111 |
112 // Start the new program, replacing this one. execv() has a weird declaration | 112 // Start the new program, replacing this one. execv() has a weird declaration |
113 // where its argv argument is declared as char* const*. In reality, the | 113 // where its argv argument is declared as char* const*. In reality, the |
114 // implementation behaves as if the argument were const char* const*, and this | 114 // implementation behaves as if the argument were const char* const*, and this |
115 // behavior is required by the standard. See | 115 // behavior is required by the standard. See |
116 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html | 116 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html |
117 // (search for “constant”). | 117 // (search for “constant”). |
(...skipping 13 matching lines...) Expand all Loading... | |
131 IGNORE_EINTR(close(STDOUT_FILENO)); | 131 IGNORE_EINTR(close(STDOUT_FILENO)); |
132 HANDLE_EINTR(dup2(dup_orig_stdout_fd, STDOUT_FILENO)); | 132 HANDLE_EINTR(dup2(dup_orig_stdout_fd, STDOUT_FILENO)); |
133 IGNORE_EINTR(close(dup_orig_stdout_fd)); | 133 IGNORE_EINTR(close(dup_orig_stdout_fd)); |
134 | 134 |
135 forbid_return.Disarm(); | 135 forbid_return.Disarm(); |
136 FAIL() << ErrnoMessage("execv") << ": " << argv_[0]; | 136 FAIL() << ErrnoMessage("execv") << ": " << argv_[0]; |
137 } | 137 } |
138 | 138 |
139 } // namespace test | 139 } // namespace test |
140 } // namespace crashpad | 140 } // namespace crashpad |
OLD | NEW |