Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cros-disks/process.h" |
| 6 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 7 | #include <csignal> |
| 8 | #include <memory> |
| 9 | #include <ostream> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <fcntl.h> |
| 13 | #include <sys/time.h> |
| 14 | #include <unistd.h> |
| 15 | |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 16 | #include <base/files/file_path.h> |
| 17 | #include <base/files/file_util.h> |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 18 | #include <base/files/scoped_file.h> |
| 19 | #include <base/logging.h> |
| 20 | #include <base/strings/string_piece.h> |
| 21 | #include <base/strings/stringprintf.h> |
| 22 | #include <chromeos/libminijail.h> |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 23 | |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
François Degros | 4a76d70 | 2019-09-26 14:54:35 +1000 | [diff] [blame] | 27 | #include "cros-disks/sandboxed_init.h" |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 28 | #include "cros-disks/sandboxed_process.h" |
| 29 | |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 30 | namespace cros_disks { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 31 | namespace { |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 32 | |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 33 | using testing::_; |
| 34 | using testing::Contains; |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 35 | using testing::ElementsAre; |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 36 | using testing::IsEmpty; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 37 | using testing::PrintToStringParamName; |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 38 | using testing::Return; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 39 | using testing::SizeIs; |
| 40 | using testing::StartsWith; |
| 41 | using testing::UnorderedElementsAre; |
| 42 | using testing::Values; |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 43 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 44 | // Sets a signal handler for SIGALRM and an interval timer signaling SIGALRM at |
| 45 | // regular intervals. |
| 46 | class AlarmGuard { |
| 47 | public: |
| 48 | explicit AlarmGuard(const int timer_interval_ms) { |
| 49 | CHECK(!old_handler_); |
| 50 | count_ = 0; |
| 51 | old_handler_ = signal(SIGALRM, &Handler); |
| 52 | CHECK_NE(old_handler_, SIG_ERR); |
| 53 | SetIntervalTimer(timer_interval_ms * 1000 /* microseconds */); |
| 54 | } |
| 55 | |
| 56 | ~AlarmGuard() { |
| 57 | SetIntervalTimer(0); |
| 58 | CHECK_EQ(signal(SIGALRM, old_handler_), &Handler); |
| 59 | old_handler_ = nullptr; |
| 60 | } |
| 61 | |
| 62 | // Number of times SIGALRM has been received. |
| 63 | static int count() { return count_; } |
| 64 | |
| 65 | private: |
| 66 | static void Handler(int sig) { |
| 67 | CHECK_EQ(sig, SIGALRM); |
| 68 | ++count_; |
| 69 | } |
| 70 | |
| 71 | static void SetIntervalTimer(const int usec) { |
| 72 | const itimerval tv = {{0, usec}, {0, usec}}; |
| 73 | if (setitimer(ITIMER_REAL, &tv, nullptr) < 0) { |
| 74 | PLOG(FATAL) << "Cannot set timer"; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Number of times SIGALRM has been received. |
| 79 | static int count_; |
| 80 | |
| 81 | using SigHandler = void (*)(int); |
| 82 | static SigHandler old_handler_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(AlarmGuard); |
| 85 | }; |
| 86 | |
| 87 | int AlarmGuard::count_ = 0; |
| 88 | AlarmGuard::SigHandler AlarmGuard::old_handler_ = nullptr; |
| 89 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 90 | std::string Read(const int fd) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 91 | char buffer[PIPE_BUF]; |
| 92 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 93 | LOG(INFO) << "Reading up to " << PIPE_BUF << " bytes from fd " << fd << "..."; |
| 94 | const ssize_t bytes_read = HANDLE_EINTR(read(fd, buffer, PIPE_BUF)); |
| 95 | PLOG_IF(FATAL, bytes_read < 0) << "Cannot read from fd " << fd; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 96 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 97 | LOG(INFO) << "Read " << bytes_read << " bytes from fd " << fd; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 98 | return std::string(buffer, bytes_read); |
| 99 | } |
| 100 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 101 | void Write(const int fd, base::StringPiece s) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 102 | while (!s.empty()) { |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 103 | const ssize_t bytes_written = HANDLE_EINTR(write(fd, s.data(), s.size())); |
| 104 | PLOG_IF(FATAL, bytes_written < 0) << "Cannot write to fd " << fd; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 105 | |
| 106 | s.remove_prefix(bytes_written); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // A mock Process class for testing the Process base class. |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 111 | class ProcessUnderTest : public Process { |
| 112 | public: |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 113 | MOCK_METHOD(pid_t, |
| 114 | StartImpl, |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 115 | (base::ScopedFD, base::ScopedFD, base::ScopedFD), |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 116 | (override)); |
| 117 | MOCK_METHOD(int, WaitImpl, (), (override)); |
François Degros | 92bbea4 | 2019-09-13 10:42:52 +1000 | [diff] [blame] | 118 | MOCK_METHOD(int, WaitNonBlockingImpl, (), (override)); |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 121 | struct ProcessFactory { |
| 122 | base::StringPiece name; |
| 123 | std::unique_ptr<Process> (*make_process)(); |
| 124 | }; |
| 125 | |
| 126 | std::ostream& operator<<(std::ostream& out, const ProcessFactory& x) { |
| 127 | return out << x.name; |
| 128 | } |
| 129 | |
| 130 | } // namespace |
| 131 | |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 132 | class ProcessTest : public ::testing::Test { |
| 133 | protected: |
| 134 | ProcessUnderTest process_; |
| 135 | }; |
| 136 | |
| 137 | TEST_F(ProcessTest, GetArguments) { |
François Degros | 5593b8c | 2019-07-25 12:27:42 +1000 | [diff] [blame] | 138 | const char* const kTestArguments[] = {"/bin/ls", "-l", "", "."}; |
Ben Chan | 6057fe6 | 2016-12-02 10:08:59 -0800 | [diff] [blame] | 139 | for (const char* test_argument : kTestArguments) { |
| 140 | process_.AddArgument(test_argument); |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 143 | EXPECT_THAT(process_.arguments(), ElementsAre("/bin/ls", "-l", "", ".")); |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 144 | |
François Degros | 5593b8c | 2019-07-25 12:27:42 +1000 | [diff] [blame] | 145 | char* const* arguments = process_.GetArguments(); |
Ben Chan | 44e7ea6 | 2014-08-29 18:13:37 -0700 | [diff] [blame] | 146 | EXPECT_NE(nullptr, arguments); |
Ben Chan | 6057fe6 | 2016-12-02 10:08:59 -0800 | [diff] [blame] | 147 | for (const char* test_argument : kTestArguments) { |
| 148 | EXPECT_STREQ(test_argument, *arguments); |
| 149 | ++arguments; |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 150 | } |
Ben Chan | 6057fe6 | 2016-12-02 10:08:59 -0800 | [diff] [blame] | 151 | EXPECT_EQ(nullptr, *arguments); |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(ProcessTest, GetArgumentsWithNoArgumentsAdded) { |
François Degros | 2a4ec24 | 2019-10-15 16:28:54 +1100 | [diff] [blame] | 155 | char* const* arguments = process_.GetArguments(); |
| 156 | EXPECT_NE(nullptr, arguments); |
| 157 | EXPECT_EQ(nullptr, *arguments); |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 160 | TEST_F(ProcessTest, Run_Success) { |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 161 | process_.AddArgument("foo"); |
François Degros | 5593b8c | 2019-07-25 12:27:42 +1000 | [diff] [blame] | 162 | EXPECT_CALL(process_, StartImpl(_, _, _)).WillOnce(Return(123)); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 163 | EXPECT_CALL(process_, WaitImpl()).Times(0); |
| 164 | EXPECT_CALL(process_, WaitNonBlockingImpl()).WillOnce(Return(42)); |
| 165 | std::vector<std::string> output; |
| 166 | EXPECT_EQ(process_.Run(&output), 42); |
| 167 | EXPECT_THAT(output, IsEmpty()); |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(ProcessTest, Run_Fail) { |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 171 | process_.AddArgument("foo"); |
François Degros | 5593b8c | 2019-07-25 12:27:42 +1000 | [diff] [blame] | 172 | EXPECT_CALL(process_, StartImpl(_, _, _)).WillOnce(Return(-1)); |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 173 | EXPECT_CALL(process_, WaitImpl()).Times(0); |
François Degros | 92bbea4 | 2019-09-13 10:42:52 +1000 | [diff] [blame] | 174 | EXPECT_CALL(process_, WaitNonBlockingImpl()).Times(0); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 175 | std::vector<std::string> output; |
| 176 | EXPECT_EQ(process_.Run(&output), -1); |
| 177 | EXPECT_THAT(output, IsEmpty()); |
Sergei Datsenko | 9246e9c | 2019-03-22 10:26:47 +1100 | [diff] [blame] | 178 | } |
| 179 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 180 | class ProcessRunTest : public ::testing::TestWithParam<ProcessFactory> { |
| 181 | public: |
| 182 | ProcessRunTest() { |
| 183 | // Ensure that we get an error message if Minijail crashes. |
| 184 | // TODO(crbug.com/1007098) Remove the following line or this comment |
| 185 | // depending on how this bug is resolved. |
| 186 | minijail_log_to_fd(STDERR_FILENO, 0); |
| 187 | } |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 188 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 189 | const std::unique_ptr<Process> process_ = GetParam().make_process(); |
| 190 | }; |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 191 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 192 | TEST_P(ProcessRunTest, RunReturnsZero) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 193 | Process& process = *process_; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 194 | process.AddArgument("/bin/sh"); |
| 195 | process.AddArgument("-c"); |
| 196 | process.AddArgument("exit 0"); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 197 | std::vector<std::string> output; |
| 198 | EXPECT_EQ(process.Run(&output), 0); |
| 199 | EXPECT_THAT(output, IsEmpty()); |
Sergei Datsenko | cd676b7 | 2019-05-10 11:42:05 +1000 | [diff] [blame] | 200 | } |
| 201 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 202 | TEST_P(ProcessRunTest, WaitReturnsZero) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 203 | Process& process = *process_; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 204 | process.AddArgument("/bin/sh"); |
| 205 | process.AddArgument("-c"); |
| 206 | process.AddArgument("exit 0"); |
| 207 | EXPECT_TRUE(process.Start()); |
| 208 | EXPECT_EQ(process.Wait(), 0); |
| 209 | } |
| 210 | |
| 211 | TEST_P(ProcessRunTest, RunReturnsNonZero) { |
| 212 | Process& process = *process_; |
| 213 | process.AddArgument("/bin/sh"); |
| 214 | process.AddArgument("-c"); |
| 215 | process.AddArgument("exit 42"); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 216 | std::vector<std::string> output; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 217 | EXPECT_EQ(process.Run(&output), 42); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 218 | EXPECT_THAT(output, IsEmpty()); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 219 | } |
| 220 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 221 | TEST_P(ProcessRunTest, WaitReturnsNonZero) { |
| 222 | Process& process = *process_; |
| 223 | process.AddArgument("/bin/sh"); |
| 224 | process.AddArgument("-c"); |
| 225 | process.AddArgument("exit 42"); |
| 226 | EXPECT_TRUE(process.Start()); |
| 227 | EXPECT_EQ(process.Wait(), 42); |
| 228 | } |
| 229 | |
| 230 | TEST_P(ProcessRunTest, RunKilledBySigKill) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 231 | Process& process = *process_; |
| 232 | process.AddArgument("/bin/sh"); |
| 233 | process.AddArgument("-c"); |
| 234 | process.AddArgument("kill -KILL $$; sleep 1000"); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 235 | std::vector<std::string> output; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 236 | EXPECT_EQ(process.Run(&output), MINIJAIL_ERR_SIG_BASE + SIGKILL); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 237 | EXPECT_THAT(output, IsEmpty()); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 238 | } |
| 239 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 240 | TEST_P(ProcessRunTest, WaitKilledBySigKill) { |
| 241 | Process& process = *process_; |
| 242 | process.AddArgument("/bin/sh"); |
| 243 | process.AddArgument("-c"); |
| 244 | process.AddArgument("kill -KILL $$; sleep 1000"); |
| 245 | EXPECT_TRUE(process.Start()); |
| 246 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_SIG_BASE + SIGKILL); |
| 247 | } |
| 248 | |
| 249 | TEST_P(ProcessRunTest, RunKilledBySigSys) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 250 | Process& process = *process_; |
| 251 | process.AddArgument("/bin/sh"); |
| 252 | process.AddArgument("-c"); |
| 253 | process.AddArgument("kill -SYS $$; sleep 1000"); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 254 | std::vector<std::string> output; |
| 255 | EXPECT_EQ(process.Run(&output), MINIJAIL_ERR_JAIL); |
| 256 | EXPECT_THAT(output, IsEmpty()); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 257 | } |
| 258 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 259 | TEST_P(ProcessRunTest, WaitKilledBySigSys) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 260 | Process& process = *process_; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 261 | process.AddArgument("/bin/sh"); |
| 262 | process.AddArgument("-c"); |
| 263 | process.AddArgument("kill -SYS $$; sleep 1000"); |
| 264 | EXPECT_TRUE(process.Start()); |
| 265 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_JAIL); |
| 266 | } |
| 267 | |
François Degros | 21bc9bb | 2020-04-18 00:14:43 +1000 | [diff] [blame^] | 268 | TEST_P(ProcessRunTest, ExternallyKilledBySigKill) { |
| 269 | Process& process = *process_; |
| 270 | process.AddArgument("/bin/sh"); |
| 271 | process.AddArgument("-c"); |
| 272 | |
| 273 | // Pipe to block the child process. |
| 274 | SubprocessPipe to_block(SubprocessPipe::kParentToChild); |
| 275 | |
| 276 | // Pipe to monitor the child process. |
| 277 | SubprocessPipe to_wait(SubprocessPipe::kChildToParent); |
| 278 | |
| 279 | process.AddArgument(base::StringPrintf( |
| 280 | R"( |
| 281 | printf 'Begin\n' >&%d; |
| 282 | read line <&%d; |
| 283 | printf '%%s and End\n' "$line" >&%d; |
| 284 | exit 42; |
| 285 | )", |
| 286 | to_wait.child_fd.get(), to_block.child_fd.get(), to_wait.child_fd.get())); |
| 287 | |
| 288 | EXPECT_TRUE(process.Start()); |
| 289 | |
| 290 | // Close unused pipe ends. |
| 291 | to_block.child_fd.reset(); |
| 292 | to_wait.child_fd.reset(); |
| 293 | |
| 294 | // Wait for child process to start. |
| 295 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Begin\n"); |
| 296 | |
| 297 | // Send SIGKILL to child process. |
| 298 | const pid_t pid = process.pid(); |
| 299 | LOG(INFO) << "Sending signal to PID " << pid; |
| 300 | EXPECT_EQ(kill(pid, SIGKILL), 0); |
| 301 | |
| 302 | // Wait for child process to finish. |
| 303 | EXPECT_EQ(Read(to_wait.parent_fd.get()), ""); |
| 304 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_SIG_BASE + SIGKILL); |
| 305 | } |
| 306 | |
| 307 | TEST_P(ProcessRunTest, ExternallyKilledBySigTerm) { |
| 308 | Process& process = *process_; |
| 309 | process.AddArgument("/bin/sh"); |
| 310 | process.AddArgument("-c"); |
| 311 | |
| 312 | // Pipe to block the child process. |
| 313 | SubprocessPipe to_block(SubprocessPipe::kParentToChild); |
| 314 | |
| 315 | // Pipe to monitor the child process. |
| 316 | SubprocessPipe to_wait(SubprocessPipe::kChildToParent); |
| 317 | |
| 318 | process.AddArgument(base::StringPrintf( |
| 319 | R"( |
| 320 | printf 'Begin\n' >&%d; |
| 321 | read line <&%d; |
| 322 | printf '%%s and End\n' "$line" >&%d; |
| 323 | exit 42; |
| 324 | )", |
| 325 | to_wait.child_fd.get(), to_block.child_fd.get(), to_wait.child_fd.get())); |
| 326 | |
| 327 | EXPECT_TRUE(process.Start()); |
| 328 | |
| 329 | // Close unused pipe ends. |
| 330 | to_block.child_fd.reset(); |
| 331 | to_wait.child_fd.reset(); |
| 332 | |
| 333 | // Wait for child process to start. |
| 334 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Begin\n"); |
| 335 | |
| 336 | // Send SIGTERM to child process. |
| 337 | const pid_t pid = process.pid(); |
| 338 | LOG(INFO) << "Sending signal to PID " << pid; |
| 339 | EXPECT_EQ(kill(pid, SIGTERM), 0); |
| 340 | |
| 341 | // Wait for child process to finish. |
| 342 | EXPECT_EQ(Read(to_wait.parent_fd.get()), ""); |
| 343 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_SIG_BASE + SIGTERM); |
| 344 | } |
| 345 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 346 | TEST_P(ProcessRunTest, RunCannotFindCommand) { |
| 347 | Process& process = *process_; |
| 348 | process.AddArgument("non existing command"); |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 349 | std::vector<std::string> output; |
François Degros | a97ad49 | 2019-10-04 12:12:12 +1000 | [diff] [blame] | 350 | EXPECT_EQ(process.Run(&output), MINIJAIL_ERR_NO_COMMAND); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 351 | } |
| 352 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 353 | TEST_P(ProcessRunTest, WaitCannotFindCommand) { |
| 354 | Process& process = *process_; |
| 355 | process.AddArgument("non existing command"); |
| 356 | EXPECT_TRUE(process.Start()); |
François Degros | a97ad49 | 2019-10-04 12:12:12 +1000 | [diff] [blame] | 357 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_NO_COMMAND); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | TEST_P(ProcessRunTest, RunCannotRunCommand) { |
| 361 | Process& process = *process_; |
| 362 | process.AddArgument("/dev/null"); |
| 363 | std::vector<std::string> output; |
François Degros | a97ad49 | 2019-10-04 12:12:12 +1000 | [diff] [blame] | 364 | EXPECT_EQ(process.Run(&output), MINIJAIL_ERR_NO_ACCESS); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | TEST_P(ProcessRunTest, WaitCannotRunCommand) { |
| 368 | Process& process = *process_; |
| 369 | process.AddArgument("/dev/null"); |
| 370 | EXPECT_TRUE(process.Start()); |
François Degros | a97ad49 | 2019-10-04 12:12:12 +1000 | [diff] [blame] | 371 | EXPECT_EQ(process.Wait(), MINIJAIL_ERR_NO_ACCESS); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 372 | } |
| 373 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 374 | TEST_P(ProcessRunTest, CapturesInterleavedOutputs) { |
| 375 | Process& process = *process_; |
| 376 | process.AddArgument("/bin/sh"); |
| 377 | process.AddArgument("-c"); |
| 378 | process.AddArgument(R"( |
| 379 | printf 'Line 1\nLine ' >&1; |
| 380 | printf 'Line 2\nLine' >&2; |
| 381 | printf '3\nLine 4\n' >&1; |
| 382 | printf ' 5\nLine 6' >&2; |
| 383 | )"); |
| 384 | |
| 385 | std::vector<std::string> output; |
| 386 | EXPECT_EQ(process.Run(&output), 0); |
| 387 | EXPECT_THAT(output, UnorderedElementsAre("OUT: Line 1", "OUT: Line 3", |
| 388 | "OUT: Line 4", "ERR: Line 2", |
| 389 | "ERR: Line 5", "ERR: Line 6")); |
| 390 | } |
| 391 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 392 | TEST_P(ProcessRunTest, CapturesLotsOfOutputData) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 393 | Process& process = *process_; |
| 394 | process.AddArgument("/bin/sh"); |
| 395 | process.AddArgument("-c"); |
| 396 | process.AddArgument(R"( |
| 397 | for i in $(seq 1 1000); do |
| 398 | printf 'Message %i\n' $i >&1; |
| 399 | printf 'Error %i\n' $i >&2; |
| 400 | done; |
| 401 | )"); |
| 402 | |
| 403 | std::vector<std::string> output; |
| 404 | EXPECT_EQ(process.Run(&output), 0); |
| 405 | EXPECT_THAT(output, SizeIs(2000)); |
| 406 | } |
| 407 | |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 408 | TEST_P(ProcessRunTest, DoesNotBlockWhenNotCapturingOutput) { |
François Degros | 86fb180 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 409 | Process& process = *process_; |
| 410 | process.AddArgument("/bin/sh"); |
| 411 | process.AddArgument("-c"); |
| 412 | |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 413 | // Pipe to monitor the process and wait for it to finish without calling |
| 414 | // Process::Wait. |
| 415 | SubprocessPipe to_wait(SubprocessPipe::kChildToParent); |
| 416 | |
| 417 | process.AddArgument(base::StringPrintf(R"( |
| 418 | printf '%%01000i\n' $(seq 1 100) >&1; |
| 419 | printf '%%01000i\n' $(seq 1 100) >&2; |
| 420 | printf 'End' >&%d; |
François Degros | 86fb180 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 421 | exit 42; |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 422 | )", |
| 423 | to_wait.child_fd.get())); |
François Degros | 86fb180 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 424 | |
| 425 | // This process generates lots of output on stdout and stderr, ie more than |
| 426 | // what a pipe can hold without blocking. If the pipes connected to stdout and |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 427 | // stderr were not drained, they would fill, the process would stall and |
| 428 | // process.Wait() would block forever. If the pipes were closed, the process |
| 429 | // would be killed by a SIGPIPE. With drained pipes, the process finishes |
| 430 | // normally and its return code should be visible. |
| 431 | EXPECT_TRUE(process.Start()); |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 432 | |
| 433 | // The process should finish normally without the parent having to call |
| 434 | // Process::Wait() first. |
| 435 | to_wait.child_fd.reset(); |
| 436 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "End"); |
| 437 | EXPECT_EQ(Read(to_wait.parent_fd.get()), ""); |
| 438 | |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 439 | EXPECT_EQ(process.Wait(), 42); |
François Degros | 86fb180 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 440 | } |
| 441 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 442 | TEST_P(ProcessRunTest, RunDoesNotBlockWhenReadingFromStdIn) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 443 | Process& process = *process_; |
| 444 | process.AddArgument("/bin/cat"); |
| 445 | |
| 446 | // By default, /bin/cat reads from stdin. If the pipe connected to stdin was |
| 447 | // left open, the process would block indefinitely while reading from it. |
François Degros | edaefc5 | 2019-10-03 12:00:20 +1000 | [diff] [blame] | 448 | std::vector<std::string> output; |
| 449 | EXPECT_EQ(process.Run(&output), 0); |
| 450 | EXPECT_THAT(output, IsEmpty()); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 451 | } |
| 452 | |
François Degros | 1ef6994 | 2019-10-01 15:31:17 +1000 | [diff] [blame] | 453 | TEST_P(ProcessRunTest, WaitDoesNotBlockWhenReadingFromStdIn) { |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 454 | Process& process = *process_; |
| 455 | process.AddArgument("/bin/cat"); |
| 456 | |
| 457 | // By default, /bin/cat reads from stdin. If the pipe connected to stdin was |
| 458 | // left open, the process would block indefinitely while reading from it. |
| 459 | EXPECT_TRUE(process.Start()); |
| 460 | EXPECT_EQ(process.Wait(), 0); |
| 461 | } |
| 462 | |
| 463 | TEST_P(ProcessRunTest, RunDoesNotWaitForBackgroundProcessToFinish) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 464 | Process& process = *process_; |
| 465 | process.AddArgument("/bin/sh"); |
| 466 | process.AddArgument("-c"); |
| 467 | |
| 468 | // Pipe to unblock the background process and allow it to finish. |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 469 | SubprocessPipe to_continue(SubprocessPipe::kParentToChild); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 470 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 471 | // Pipe to monitor the background process and wait for it to finish. |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 472 | SubprocessPipe to_wait(SubprocessPipe::kChildToParent); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 473 | |
| 474 | process.AddArgument(base::StringPrintf(R"( |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 475 | ( |
| 476 | exec 0<&-; |
| 477 | exec 1>&-; |
| 478 | exec 2>&-; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 479 | printf 'Begin\n' >&%d; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 480 | read line <&%d; |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 481 | printf '%%s and End\n' "$line" >&%d; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 482 | exit 42; |
| 483 | )& |
| 484 | printf 'Started background process %%i\n' $! |
| 485 | exit 5; |
| 486 | )", |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 487 | to_wait.child_fd.get(), |
| 488 | to_continue.child_fd.get(), |
| 489 | to_wait.child_fd.get())); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 490 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 491 | LOG(INFO) << "Running launcher process"; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 492 | std::vector<std::string> output; |
| 493 | EXPECT_EQ(process.Run(&output), 5); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 494 | EXPECT_THAT(output, |
| 495 | ElementsAre(StartsWith("OUT: Started background process"))); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 496 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 497 | LOG(INFO) << "Closing unused fds"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 498 | to_continue.child_fd.reset(); |
| 499 | to_wait.child_fd.reset(); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 500 | |
| 501 | LOG(INFO) << "Waiting for background process to confirm starting"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 502 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Begin\n"); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 503 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 504 | LOG(INFO) << "Unblocking background process"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 505 | Write(to_continue.parent_fd.get(), "Continue\n"); |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 506 | |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 507 | LOG(INFO) << "Waiting for background process to finish"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 508 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Continue and End\n"); |
| 509 | EXPECT_EQ(Read(to_wait.parent_fd.get()), ""); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 510 | |
| 511 | LOG(INFO) << "Background process finished"; |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 512 | } |
| 513 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 514 | // TODO(crbug.com/1007613) Enable test once bug is fixed. |
| 515 | TEST_P(ProcessRunTest, DISABLED_WaitDoesNotWaitForBackgroundProcessToFinish) { |
| 516 | Process& process = *process_; |
| 517 | process.AddArgument("/bin/sh"); |
| 518 | process.AddArgument("-c"); |
| 519 | |
| 520 | // Pipe to unblock the background process and allow it to finish. |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 521 | SubprocessPipe to_continue(SubprocessPipe::kParentToChild); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 522 | |
| 523 | // Pipe to monitor the background process and wait for it to finish. |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 524 | SubprocessPipe to_wait(SubprocessPipe::kChildToParent); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 525 | |
| 526 | process.AddArgument(base::StringPrintf(R"( |
| 527 | ( |
| 528 | exec 0<&-; |
| 529 | exec 1>&-; |
| 530 | exec 2>&-; |
| 531 | printf 'Begin\n' >&%d; |
| 532 | read line <&%d; |
| 533 | printf '%%s and End\n' "$line" >&%d; |
| 534 | exit 42; |
| 535 | )& |
| 536 | exit 5; |
| 537 | )", |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 538 | to_wait.child_fd.get(), |
| 539 | to_continue.child_fd.get(), |
| 540 | to_wait.child_fd.get())); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 541 | |
| 542 | LOG(INFO) << "Starting launcher process"; |
| 543 | EXPECT_TRUE(process.Start()); |
| 544 | |
| 545 | LOG(INFO) << "Waiting for launcher process to finish"; |
| 546 | EXPECT_EQ(process.Wait(), 5); |
| 547 | |
| 548 | LOG(INFO) << "Closing unused fds"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 549 | to_continue.child_fd.reset(); |
| 550 | to_wait.child_fd.reset(); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 551 | |
| 552 | LOG(INFO) << "Waiting for background process to confirm starting"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 553 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Begin\n"); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 554 | |
| 555 | LOG(INFO) << "Unblocking background process"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 556 | Write(to_continue.parent_fd.get(), "Continue\n"); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 557 | |
| 558 | LOG(INFO) << "Waiting for background process to finish"; |
François Degros | c2bfe0e | 2019-10-14 13:08:49 +1100 | [diff] [blame] | 559 | EXPECT_EQ(Read(to_wait.parent_fd.get()), "Continue and End\n"); |
| 560 | EXPECT_EQ(Read(to_wait.parent_fd.get()), ""); |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 561 | |
| 562 | LOG(INFO) << "Background process finished"; |
| 563 | } |
| 564 | |
| 565 | TEST_P(ProcessRunTest, RunUndisturbedBySignals) { |
François Degros | 8c14d38 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 566 | Process& process = *process_; |
| 567 | process.AddArgument("/bin/sh"); |
| 568 | process.AddArgument("-c"); |
| 569 | process.AddArgument(R"( |
| 570 | for i in $(seq 1 100); do |
| 571 | printf 'Line %0100i\n' $i; |
| 572 | sleep 0.01; |
| 573 | done; |
| 574 | exit 42; |
| 575 | )"); |
| 576 | |
| 577 | std::vector<std::string> output; |
| 578 | |
| 579 | // Activate an interval timer. |
| 580 | const AlarmGuard guard(13 /* milliseconds */); |
| 581 | EXPECT_EQ(process.Run(&output), 42); |
| 582 | EXPECT_GT(AlarmGuard::count(), 0); |
| 583 | // This checks that crbug.com/1005590 is fixed. |
| 584 | EXPECT_THAT(output, SizeIs(100)); |
| 585 | } |
| 586 | |
François Degros | 5682914 | 2019-10-16 22:00:11 +1100 | [diff] [blame] | 587 | TEST_P(ProcessRunTest, WaitUndisturbedBySignals) { |
François Degros | c0b33b1 | 2019-09-12 14:50:43 +1000 | [diff] [blame] | 588 | Process& process = *process_; |
| 589 | process.AddArgument("/bin/sh"); |
| 590 | process.AddArgument("-c"); |
| 591 | process.AddArgument(R"( |
| 592 | sleep 1; |
| 593 | exit 42; |
| 594 | )"); |
| 595 | |
| 596 | // Activate an interval timer. |
| 597 | const AlarmGuard guard(13 /* milliseconds */); |
| 598 | EXPECT_TRUE(process.Start()); |
| 599 | EXPECT_EQ(process.Wait(), 42); |
| 600 | EXPECT_GT(AlarmGuard::count(), 0); |
| 601 | } |
| 602 | |
| 603 | INSTANTIATE_TEST_SUITE_P(ProcessRun, |
| 604 | ProcessRunTest, |
| 605 | Values(ProcessFactory{ |
| 606 | "SandboxedProcess", |
| 607 | []() -> std::unique_ptr<Process> { |
| 608 | return std::make_unique<SandboxedProcess>(); |
| 609 | }}), |
| 610 | PrintToStringParamName()); |
| 611 | |
François Degros | b497e42 | 2019-09-20 14:50:08 +1000 | [diff] [blame] | 612 | // TODO(crbug.com/1023727) Make it work on ARM and ARM64. |
| 613 | #if defined(__x86_64__) |
| 614 | INSTANTIATE_TEST_SUITE_P(ProcessRunAsRoot, |
| 615 | ProcessRunTest, |
| 616 | Values(ProcessFactory{ |
| 617 | "WithPidNamespace", |
| 618 | []() -> std::unique_ptr<Process> { |
| 619 | auto process = |
| 620 | std::make_unique<SandboxedProcess>(); |
| 621 | process->NewPidNamespace(); |
| 622 | // TODO(crbug.com/1008262) Remove this line. |
| 623 | process->SkipRemountPrivate(); |
| 624 | return process; |
| 625 | }}), |
| 626 | PrintToStringParamName()); |
| 627 | #endif |
| 628 | |
Ben Chan | 6f391cb | 2012-03-21 17:38:21 -0700 | [diff] [blame] | 629 | } // namespace cros_disks |