github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/exit.cc (about) 1 // Copyright 2018 The gVisor Authors. 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 <sys/wait.h> 16 #include <unistd.h> 17 18 #include "gtest/gtest.h" 19 #include "absl/time/time.h" 20 #include "test/util/file_descriptor.h" 21 #include "test/util/test_util.h" 22 #include "test/util/time_util.h" 23 24 namespace gvisor { 25 namespace testing { 26 27 namespace { 28 29 void TestExit(int code) { 30 pid_t pid = fork(); 31 if (pid == 0) { 32 _exit(code); 33 } 34 35 ASSERT_THAT(pid, SyscallSucceeds()); 36 37 int status; 38 EXPECT_THAT(RetryEINTR(waitpid)(pid, &status, 0), SyscallSucceeds()); 39 EXPECT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == code) << status; 40 } 41 42 TEST(ExitTest, Success) { TestExit(0); } 43 44 TEST(ExitTest, Failure) { TestExit(1); } 45 46 // This test ensures that a process's file descriptors are closed when it calls 47 // exit(). In order to test this, the parent tries to read from a pipe whose 48 // write end is held by the child. While the read is blocking, the child exits, 49 // which should cause the parent to read 0 bytes due to EOF. 50 TEST(ExitTest, CloseFds) { 51 int pipe_fds[2]; 52 ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds()); 53 54 FileDescriptor read_fd(pipe_fds[0]); 55 FileDescriptor write_fd(pipe_fds[1]); 56 57 pid_t pid = fork(); 58 if (pid == 0) { 59 read_fd.reset(); 60 61 SleepSafe(absl::Seconds(10)); 62 63 _exit(0); 64 } 65 66 EXPECT_THAT(pid, SyscallSucceeds()); 67 68 write_fd.reset(); 69 70 char buf[10]; 71 EXPECT_THAT(ReadFd(read_fd.get(), buf, sizeof(buf)), 72 SyscallSucceedsWithValue(0)); 73 } 74 75 } // namespace 76 77 } // namespace testing 78 } // namespace gvisor