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