Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: util/test/multiprocess_exec_posix.cc

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

Powered by Google App Engine
This is Rietveld 408576698