Serenity Operating System
at master 76 lines 2.8 kB view raw
1/* 2 * Copyright (c) 2021, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8#include <cstring> 9#include <sys/stat.h> 10#include <unistd.h> 11 12TEST_CASE(test_process_fd_readlink) 13{ 14 // Make sure that stdin, stdout and stderr are actually symlinks that point somewhere interesting 15 // Sadly we can't assume that they all point to the same file. 16 struct stat stat_buf = {}; 17 struct stat lstat_buf = {}; 18 auto rc = stat("/proc/self/fd/0", &stat_buf); 19 EXPECT_EQ(rc, 0); 20 rc = lstat("/proc/self/fd/0", &lstat_buf); 21 EXPECT_EQ(rc, 0); 22 EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat))); 23 24 stat_buf = {}; 25 lstat_buf = {}; 26 rc = stat("/proc/self/fd/1", &stat_buf); 27 EXPECT_EQ(rc, 0); 28 rc = lstat("/proc/self/fd/1", &lstat_buf); 29 EXPECT_EQ(rc, 0); 30 EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat))); 31 32 stat_buf = {}; 33 lstat_buf = {}; 34 rc = stat("/proc/self/fd/2", &stat_buf); 35 EXPECT_EQ(rc, 0); 36 rc = lstat("/proc/self/fd/2", &lstat_buf); 37 EXPECT_EQ(rc, 0); 38 EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat))); 39 40 // Create a new file descriptor that is a dup of 0 with various big values in order to reproduce issue #7820. 41 // We should get the same link value for each fd that was duplicated. 42 char expected_link[MAXPATHLEN]; 43 char buf[MAXPATHLEN]; 44 45 // Read the symlink for stdin, stdout and stderr 46 auto link_length = readlink("/proc/self/fd/0", expected_link, sizeof(expected_link)); 47 expected_link[link_length] = '\0'; 48 49 // 255 is the first broken file descriptor that was discovered and might be used by other software (e.g. bash) 50 auto new_fd = dup2(0, 255); 51 EXPECT_EQ(new_fd, 255); 52 link_length = readlink("/proc/self/fd/255", buf, sizeof(buf)); 53 buf[link_length] = '\0'; 54 EXPECT_EQ(0, strcmp(buf, expected_link)); 55 56 // 215 is the last fd before we have to encode the fd using more than one byte (due to the offset by FI_MaxStaticFileIndex) 57 new_fd = dup2(0, 215); 58 EXPECT_EQ(new_fd, 215); 59 link_length = readlink("/proc/self/fd/215", buf, sizeof(buf)); 60 buf[link_length] = '\0'; 61 EXPECT_EQ(0, strcmp(buf, expected_link)); 62 63 // 216 is the first fd that is encoded using more than one byte 64 new_fd = dup2(0, 216); 65 EXPECT_EQ(new_fd, 216); 66 link_length = readlink("/proc/self/fd/216", buf, sizeof(buf)); 67 buf[link_length] = '\0'; 68 EXPECT_EQ(0, strcmp(buf, expected_link)); 69 70 // 1023 is the largest possible file descriptor 71 new_fd = dup2(0, 1023); 72 EXPECT_EQ(new_fd, 1023); 73 link_length = readlink("/proc/self/fd/1023", buf, sizeof(buf)); 74 buf[link_length] = '\0'; 75 EXPECT_EQ(0, strcmp(buf, expected_link)); 76}