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

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

Issue 531203002: Add the MultiprocessExec test and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 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
21 #include "base/posix/eintr_wrapper.h"
22 #include "gtest/gtest.h"
23 #include "util/test/errors.h"
24 #include "util/test/posix/close_multiple.h"
25
26 namespace crashpad {
27 namespace test {
28
29 using namespace testing;
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 Multiprocess::PreFork();
53 if (testing::Test::HasFatalFailure()) {
54 return;
55 }
56
57 // Build up the argv vector. This is done in PreFork() instead of
58 // MultiprocessChild() because although the result is only needed in the child
59 // process, building it is a hazardous operation in that process.
60 ASSERT_TRUE(argv_.empty());
61
62 argv_.push_back(command_.c_str());
63 for (const std::string& argument : arguments_) {
64 argv_.push_back(argument.c_str());
65 }
66 argv_.push_back(NULL);
67 }
68
69 void MultiprocessExec::MultiprocessChild() {
70 // Make sure that stdin, stdout, and stderr are FDs 0, 1, and 2, respectively.
71 // All FDs above this will be closed.
72 COMPILE_ASSERT(STDIN_FILENO == 0, stdin_must_be_fd_0);
73 COMPILE_ASSERT(STDOUT_FILENO == 1, stdout_must_be_fd_1);
74 COMPILE_ASSERT(STDERR_FILENO == 2, stderr_must_be_fd_2);
75
76 // Move the read pipe to stdin.
77 int read_fd = ReadPipeFD();
78 ASSERT_NE(read_fd, STDIN_FILENO);
79 ASSERT_NE(read_fd, STDOUT_FILENO);
80 ASSERT_EQ(STDIN_FILENO, fileno(stdin));
81
82 int rv = fpurge(stdin);
83 ASSERT_EQ(0, rv) << ErrnoMessage("fpurge");
84
85 rv = HANDLE_EINTR(dup2(read_fd, STDIN_FILENO));
86 ASSERT_EQ(STDIN_FILENO, rv) << ErrnoMessage("dup2");
87
88 // Move the write pipe to stdout.
89 int write_fd = WritePipeFD();
90 ASSERT_NE(write_fd, STDIN_FILENO);
91 ASSERT_NE(write_fd, STDOUT_FILENO);
92 ASSERT_EQ(STDOUT_FILENO, fileno(stdout));
93
94 // Make a copy of the original stdout file descriptor so that in case there’s
95 // an execv() failure, the original stdout can be restored so that test
96 // messages directed to stdout go to the right place. Since stdin, stdout, and
Robert Sesek 2014/09/02 21:45:14 Don't test messages go to stderr?
Mark Mentovai 2014/09/02 22:47:47 rsesek wrote:
97 // stderr are known to be 0, 1, and 2, and no other file descriptors are
98 // needed in the child, use the next available file descriptor, 3, for this
Robert Sesek 2014/09/02 21:45:14 Are you really guaranteed that no other fd occupie
99 // purpose. Mark it as close-on-exec, so that the child won’t see it after a
100 // successful exec(), but it will still be available in this process after an
101 // unsuccessful exec().
102 const int kDupOrigStdoutFd = 3;
103 rv = HANDLE_EINTR(dup2(STDOUT_FILENO, kDupOrigStdoutFd));
104 ASSERT_EQ(kDupOrigStdoutFd, rv) << ErrnoMessage("dup2");
105
106 rv = fcntl(kDupOrigStdoutFd, 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(kDupOrigStdoutFd + 1);
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 // Put the original stdout back. Close the copy of the write pipe FD that’s
129 // currently on stdout first, so that in case the dup2() that restores the
130 // original stdout fails, stdout isn’t left attached to the pipe when the
131 // FAIL() statement executes.
132 HANDLE_EINTR(fflush(stdout));
133 IGNORE_EINTR(close(STDOUT_FILENO));
134 HANDLE_EINTR(dup2(kDupOrigStdoutFd, STDOUT_FILENO));
135 IGNORE_EINTR(close(kDupOrigStdoutFd));
136
137 FAIL() << ErrnoMessage("execv") << ": " << argv_[0];
Robert Sesek 2014/09/02 21:45:14 Seems like a case for ScopedNoReturn.
Mark Mentovai 2014/09/02 22:47:47 rsesek wrote:
138 }
139
140 } // namespace test
141 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698