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 "util/test/multiprocess_exec.h" |
| 16 |
| 17 #include <fcntl.h> |
| 18 #include <stdio.h> |
| 19 #include <stdlib.h> |
| 20 |
| 21 #include "base/posix/eintr_wrapper.h" |
| 22 #include "gtest/gtest.h" |
| 23 #include "util/misc/scoped_forbid_return.h" |
| 24 #include "util/test/errors.h" |
| 25 #include "util/test/posix/close_multiple.h" |
| 26 |
| 27 namespace crashpad { |
| 28 namespace test { |
| 29 |
| 30 using namespace testing; |
| 31 |
| 32 MultiprocessExec::MultiprocessExec() |
| 33 : Multiprocess(), |
| 34 command_(), |
| 35 arguments_(), |
| 36 argv_() { |
| 37 } |
| 38 |
| 39 void MultiprocessExec::SetChildCommand( |
| 40 const std::string& command, const std::vector<std::string>* arguments) { |
| 41 command_ = command; |
| 42 if (arguments) { |
| 43 arguments_ = *arguments; |
| 44 } else { |
| 45 arguments_.clear(); |
| 46 } |
| 47 } |
| 48 |
| 49 MultiprocessExec::~MultiprocessExec() { |
| 50 } |
| 51 |
| 52 void MultiprocessExec::PreFork() { |
| 53 Multiprocess::PreFork(); |
| 54 if (testing::Test::HasFatalFailure()) { |
| 55 return; |
| 56 } |
| 57 |
| 58 ASSERT_FALSE(command_.empty()); |
| 59 |
| 60 // Build up the argv vector. This is done in PreFork() instead of |
| 61 // MultiprocessChild() because although the result is only needed in the child |
| 62 // process, building it is a hazardous operation in that process. |
| 63 ASSERT_TRUE(argv_.empty()); |
| 64 |
| 65 argv_.push_back(command_.c_str()); |
| 66 for (const std::string& argument : arguments_) { |
| 67 argv_.push_back(argument.c_str()); |
| 68 } |
| 69 argv_.push_back(NULL); |
| 70 } |
| 71 |
| 72 void MultiprocessExec::MultiprocessChild() { |
| 73 // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively. |
| 74 // All FDs above this will be closed. |
| 75 COMPILE_ASSERT(STDIN_FILENO == 0, stdin_must_be_fd_0); |
| 76 COMPILE_ASSERT(STDOUT_FILENO == 1, stdout_must_be_fd_1); |
| 77 COMPILE_ASSERT(STDERR_FILENO == 2, stderr_must_be_fd_2); |
| 78 |
| 79 // Move the read pipe to stdin. |
| 80 int read_fd = ReadPipeFD(); |
| 81 ASSERT_NE(read_fd, STDIN_FILENO); |
| 82 ASSERT_NE(read_fd, STDOUT_FILENO); |
| 83 ASSERT_EQ(STDIN_FILENO, fileno(stdin)); |
| 84 |
| 85 int rv = fpurge(stdin); |
| 86 ASSERT_EQ(0, rv) << ErrnoMessage("fpurge"); |
| 87 |
| 88 rv = HANDLE_EINTR(dup2(read_fd, STDIN_FILENO)); |
| 89 ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2"); |
| 90 |
| 91 // Move the write pipe to stdout. |
| 92 int write_fd = WritePipeFD(); |
| 93 ASSERT_NE(write_fd, STDIN_FILENO); |
| 94 ASSERT_NE(write_fd, STDOUT_FILENO); |
| 95 ASSERT_EQ(STDOUT_FILENO, fileno(stdout)); |
| 96 |
| 97 // Make a copy of the original stdout file descriptor so that in case there’s |
| 98 // an execv() failure, the original stdout can be restored so that gtest |
| 99 // messages directed to stdout go to the right place. Mark it as |
| 100 // close-on-exec, so that the child won’t see it after a successful exec(), |
| 101 // but it will still be available in this process after an unsuccessful |
| 102 // exec(). |
| 103 int dup_orig_stdout_fd = dup(STDOUT_FILENO); |
| 104 ASSERT_GE(dup_orig_stdout_fd, 0) << ErrnoMessage("dup"); |
| 105 |
| 106 rv = fcntl(dup_orig_stdout_fd, F_SETFD, FD_CLOEXEC); |
| 107 ASSERT_NE(rv, -1) << ErrnoMessage("fcntl"); |
| 108 |
| 109 rv = HANDLE_EINTR(fflush(stdout)); |
| 110 ASSERT_EQ(0, rv) << ErrnoMessage("fflush"); |
| 111 |
| 112 rv = HANDLE_EINTR(dup2(write_fd, STDOUT_FILENO)); |
| 113 ASSERT_EQ(STDOUT_FILENO, rv) << ErrnoMessage("dup2"); |
| 114 |
| 115 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, dup_orig_stdout_fd); |
| 116 |
| 117 // Start the new program, replacing this one. execv() has a weird declaration |
| 118 // where its argv argument is declared as char* const*. In reality, the |
| 119 // implementation behaves as if the argument were const char* const*, and this |
| 120 // behavior is required by the standard. See |
| 121 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html |
| 122 // (search for “constant”). |
| 123 execv(argv_[0], const_cast<char* const*>(&argv_[0])); |
| 124 |
| 125 // This should not normally be reached. Getting here means that execv() |
| 126 // failed. |
| 127 |
| 128 // Be sure not to return until FAIL() is reached. |
| 129 ScopedForbidReturn forbid_return; |
| 130 |
| 131 // Put the original stdout back. Close the copy of the write pipe FD that’s |
| 132 // currently on stdout first, so that in case the dup2() that restores the |
| 133 // original stdout fails, stdout isn’t left attached to the pipe when the |
| 134 // FAIL() statement executes. |
| 135 HANDLE_EINTR(fflush(stdout)); |
| 136 IGNORE_EINTR(close(STDOUT_FILENO)); |
| 137 HANDLE_EINTR(dup2(dup_orig_stdout_fd, STDOUT_FILENO)); |
| 138 IGNORE_EINTR(close(dup_orig_stdout_fd)); |
| 139 |
| 140 forbid_return.Disarm(); |
| 141 FAIL() << ErrnoMessage("execv") << ": " << argv_[0]; |
| 142 } |
| 143 |
| 144 } // namespace test |
| 145 } // namespace crashpad |
OLD | NEW |