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

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

Issue 880763002: Reorganize Multiprocess and implement for Windows (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: fixes 2 Created 5 years, 10 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
« no previous file with comments | « util/test/multiprocess_exec.h ('k') | util/test/multiprocess_exec_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
21 #include "base/posix/eintr_wrapper.h"
22 #include "gtest/gtest.h"
23 #include "util/misc/scoped_forbid_return.h"
24 #include "util/posix/close_multiple.h"
25 #include "util/test/errors.h"
26
27 namespace crashpad {
28 namespace test {
29
30 MultiprocessExec::MultiprocessExec()
31 : Multiprocess(),
32 command_(),
33 arguments_(),
34 argv_() {
35 }
36
37 void MultiprocessExec::SetChildCommand(
38 const std::string& command, const std::vector<std::string>* arguments) {
39 command_ = command;
40 if (arguments) {
41 arguments_ = *arguments;
42 } else {
43 arguments_.clear();
44 }
45 }
46
47 MultiprocessExec::~MultiprocessExec() {
48 }
49
50 void MultiprocessExec::PreFork() {
51 ASSERT_NO_FATAL_FAILURE(Multiprocess::PreFork());
52
53 ASSERT_FALSE(command_.empty());
54
55 // Build up the argv vector. This is done in PreFork() instead of
56 // MultiprocessChild() because although the result is only needed in the child
57 // process, building it is a hazardous operation in that process.
58 ASSERT_TRUE(argv_.empty());
59
60 argv_.push_back(command_.c_str());
61 for (const std::string& argument : arguments_) {
62 argv_.push_back(argument.c_str());
63 }
64 argv_.push_back(nullptr);
65 }
66
67 void MultiprocessExec::MultiprocessChild() {
68 // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively.
69 // All FDs above this will be closed.
70 static_assert(STDIN_FILENO == 0, "stdin must be fd 0");
71 static_assert(STDOUT_FILENO == 1, "stdout must be fd 1");
72 static_assert(STDERR_FILENO == 2, "stderr must be fd 2");
73
74 // Move the read pipe to stdin.
75 int read_fd = ReadPipeFD();
76 ASSERT_NE(read_fd, STDIN_FILENO);
77 ASSERT_NE(read_fd, STDOUT_FILENO);
78 ASSERT_EQ(STDIN_FILENO, fileno(stdin));
79
80 int rv = fpurge(stdin);
81 ASSERT_EQ(0, rv) << ErrnoMessage("fpurge");
82
83 rv = HANDLE_EINTR(dup2(read_fd, STDIN_FILENO));
84 ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2");
85
86 // Move the write pipe to stdout.
87 int write_fd = WritePipeFD();
88 ASSERT_NE(write_fd, STDIN_FILENO);
89 ASSERT_NE(write_fd, STDOUT_FILENO);
90 ASSERT_EQ(STDOUT_FILENO, fileno(stdout));
91
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
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(),
96 // but it will still be available in this process after an unsuccessful
97 // exec().
98 int dup_orig_stdout_fd = dup(STDOUT_FILENO);
99 ASSERT_GE(dup_orig_stdout_fd, 0) << ErrnoMessage("dup");
100
101 rv = fcntl(dup_orig_stdout_fd, F_SETFD, FD_CLOEXEC);
102 ASSERT_NE(rv, -1) << ErrnoMessage("fcntl");
103
104 rv = HANDLE_EINTR(fflush(stdout));
105 ASSERT_EQ(0, rv) << ErrnoMessage("fflush");
106
107 rv = HANDLE_EINTR(dup2(write_fd, STDOUT_FILENO));
108 ASSERT_EQ(STDOUT_FILENO, rv) << ErrnoMessage("dup2");
109
110 CloseMultipleNowOrOnExec(STDERR_FILENO + 1, dup_orig_stdout_fd);
111
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
114 // implementation behaves as if the argument were const char* const*, and this
115 // behavior is required by the standard. See
116 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
117 // (search for “constant”).
118 execv(argv_[0], const_cast<char* const*>(&argv_[0]));
119
120 // This should not normally be reached. Getting here means that execv()
121 // failed.
122
123 // Be sure not to return until FAIL() is reached.
124 ScopedForbidReturn forbid_return;
125
126 // Put the original stdout back. Close the copy of the write pipe FD that’s
127 // currently on stdout first, so that in case the dup2() that restores the
128 // original stdout fails, stdout isn’t left attached to the pipe when the
129 // FAIL() statement executes.
130 HANDLE_EINTR(fflush(stdout));
131 IGNORE_EINTR(close(STDOUT_FILENO));
132 HANDLE_EINTR(dup2(dup_orig_stdout_fd, STDOUT_FILENO));
133 IGNORE_EINTR(close(dup_orig_stdout_fd));
134
135 forbid_return.Disarm();
136 FAIL() << ErrnoMessage("execv") << ": " << argv_[0];
137 }
138
139 } // namespace test
140 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/test/multiprocess_exec.h ('k') | util/test/multiprocess_exec_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698