gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/proc_isolated.cc (about)

     1  // Copyright 2021 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 <linux/msg.h>
    16  #include <linux/sem.h>
    17  #include <linux/shm.h>
    18  
    19  #include "gtest/gtest.h"
    20  #include "absl/strings/numbers.h"
    21  #include "absl/strings/str_split.h"
    22  #include "test/util/fs_util.h"
    23  #include "test/util/test_util.h"
    24  
    25  namespace gvisor {
    26  namespace testing {
    27  namespace {
    28  
    29  TEST(ProcDefaults, PresenceOfShmMaxMniAll) {
    30    uint64_t shmmax = 0;
    31    uint64_t shmall = 0;
    32    uint64_t shmmni = 0;
    33    std::string proc_file;
    34    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/shmmax"));
    35    ASSERT_FALSE(proc_file.empty());
    36    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &shmmax));
    37    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/shmall"));
    38    ASSERT_FALSE(proc_file.empty());
    39    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &shmall));
    40    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/shmmni"));
    41    ASSERT_FALSE(proc_file.empty());
    42    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &shmmni));
    43  
    44    ASSERT_EQ(shmmax, SHMMAX);
    45    ASSERT_EQ(shmall, SHMALL);
    46    ASSERT_EQ(shmmni, SHMMNI);
    47    ASSERT_LE(shmall, shmmax);
    48  
    49    // These values should never be higher than this by default, for more
    50    // information see uapi/linux/shm.h
    51    ASSERT_LE(shmmax, ULONG_MAX - (1UL << 24));
    52    ASSERT_LE(shmall, ULONG_MAX - (1UL << 24));
    53  }
    54  
    55  TEST(ProcDefaults, PresenceOfSem) {
    56    uint32_t semmsl = 0;
    57    uint32_t semmns = 0;
    58    uint32_t semopm = 0;
    59    uint32_t semmni = 0;
    60    std::string proc_file;
    61    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/sem"));
    62    ASSERT_FALSE(proc_file.empty());
    63    std::vector<absl::string_view> sem_limits =
    64        absl::StrSplit(proc_file, absl::ByAnyChar("\t"), absl::SkipWhitespace());
    65    ASSERT_EQ(sem_limits.size(), 4);
    66    ASSERT_TRUE(absl::SimpleAtoi(sem_limits[0], &semmsl));
    67    ASSERT_TRUE(absl::SimpleAtoi(sem_limits[1], &semmns));
    68    ASSERT_TRUE(absl::SimpleAtoi(sem_limits[2], &semopm));
    69    ASSERT_TRUE(absl::SimpleAtoi(sem_limits[3], &semmni));
    70  
    71    ASSERT_EQ(semmsl, SEMMSL);
    72    ASSERT_EQ(semmns, SEMMNS);
    73    ASSERT_EQ(semopm, SEMOPM);
    74    ASSERT_EQ(semmni, SEMMNI);
    75  }
    76  
    77  TEST(ProcDefaults, PresenceOfMsgMniMaxMnb) {
    78    uint64_t msgmni = 0;
    79    uint64_t msgmax = 0;
    80    uint64_t msgmnb = 0;
    81  
    82    std::string proc_file;
    83    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/msgmni"));
    84    ASSERT_FALSE(proc_file.empty());
    85    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &msgmni));
    86    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/msgmax"));
    87    ASSERT_FALSE(proc_file.empty());
    88    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &msgmax));
    89    proc_file = ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/sys/kernel/msgmnb"));
    90    ASSERT_FALSE(proc_file.empty());
    91    ASSERT_TRUE(absl::SimpleAtoi(proc_file, &msgmnb));
    92  
    93    ASSERT_EQ(msgmni, MSGMNI);
    94    ASSERT_EQ(msgmax, MSGMAX);
    95    ASSERT_EQ(msgmnb, MSGMNB);
    96  }
    97  
    98  }  // namespace
    99  }  // namespace testing
   100  }  // namespace gvisor