github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/util/mount_util.h (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  #ifndef GVISOR_TEST_UTIL_MOUNT_UTIL_H_
    16  #define GVISOR_TEST_UTIL_MOUNT_UTIL_H_
    17  
    18  #include <errno.h>
    19  #include <sys/mount.h>
    20  
    21  #include <functional>
    22  #include <string>
    23  
    24  #include "gmock/gmock.h"
    25  #include "absl/container/flat_hash_map.h"
    26  #include "test/util/cleanup.h"
    27  #include "test/util/posix_error.h"
    28  #include "test/util/test_util.h"
    29  
    30  namespace gvisor {
    31  namespace testing {
    32  
    33  // Mount mounts the filesystem, and unmounts when the returned reference is
    34  // destroyed.
    35  inline PosixErrorOr<Cleanup> Mount(const std::string& source,
    36                                     const std::string& target,
    37                                     const std::string& fstype,
    38                                     uint64_t mountflags, const std::string& data,
    39                                     uint64_t umountflags) {
    40    if (mount(source.c_str(), target.c_str(), fstype.c_str(), mountflags,
    41              data.c_str()) == -1) {
    42      return PosixError(errno, "mount failed");
    43    }
    44    return Cleanup([target, umountflags]() {
    45      EXPECT_THAT(umount2(target.c_str(), umountflags), SyscallSucceeds());
    46    });
    47  }
    48  
    49  struct ProcMountsEntry {
    50    std::string spec;
    51    std::string mount_point;
    52    std::string fstype;
    53    std::string mount_opts;
    54    uint32_t dump;
    55    uint32_t fsck;
    56  };
    57  
    58  // ProcSelfMountsEntries returns a parsed representation of /proc/self/mounts.
    59  PosixErrorOr<std::vector<ProcMountsEntry>> ProcSelfMountsEntries();
    60  
    61  // ProcSelfMountsEntries returns a parsed representation of mounts from the
    62  // provided content.
    63  PosixErrorOr<std::vector<ProcMountsEntry>> ProcSelfMountsEntriesFrom(
    64      const std::string& content);
    65  
    66  struct ProcMountInfoEntry {
    67    uint64_t id;
    68    uint64_t parent_id;
    69    dev_t major;
    70    dev_t minor;
    71    std::string root;
    72    std::string mount_point;
    73    std::string mount_opts;
    74    std::string optional;
    75    std::string fstype;
    76    std::string mount_source;
    77    std::string super_opts;
    78  };
    79  
    80  // ProcSelfMountInfoEntries returns a parsed representation of
    81  // /proc/self/mountinfo.
    82  PosixErrorOr<std::vector<ProcMountInfoEntry>> ProcSelfMountInfoEntries();
    83  
    84  // ProcSelfMountInfoEntriesFrom returns a parsed representation of
    85  // mountinfo from the provided content.
    86  PosixErrorOr<std::vector<ProcMountInfoEntry>> ProcSelfMountInfoEntriesFrom(
    87      const std::string&);
    88  
    89  // Interprets the input string mopts as a comma separated list of mount
    90  // options. A mount option can either be just a value, or a key=value pair. For
    91  // example, the string "rw,relatime,fd=7" will be parsed into a map like { "rw":
    92  // "", "relatime": "", "fd": "7" }.
    93  absl::flat_hash_map<std::string, std::string> ParseMountOptions(
    94      std::string mopts);
    95  
    96  }  // namespace testing
    97  }  // namespace gvisor
    98  
    99  #endif  // GVISOR_TEST_UTIL_MOUNT_UTIL_H_