Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | #ifndef LIBCONTAINER_LIBCONTAINER_UTIL_H_ |
| 6 | #define LIBCONTAINER_LIBCONTAINER_UTIL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/files/file_path.h> |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
| 12 | #include <base/macros.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 13 | |
| 14 | namespace libcontainer { |
| 15 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 16 | // Simple class that saves errno. |
| 17 | class SaveErrno { |
| 18 | public: |
| 19 | SaveErrno(); |
| 20 | ~SaveErrno(); |
| 21 | |
| 22 | private: |
| 23 | const int saved_errno_; |
| 24 | |
| 25 | DISALLOW_COPY_AND_ASSIGN(SaveErrno); |
| 26 | }; |
| 27 | |
| 28 | // The comma operator will discard the SaveErrno instance, but will keep it |
| 29 | // alive until after the whole expression has been evaluated. |
| 30 | #define PLOG_PRESERVE(verbose_level) \ |
| 31 | ::libcontainer::SaveErrno(), PLOG(verbose_level) |
| 32 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 33 | // Given a uid/gid map of "inside1 outside1 length1, ...", and an id inside of |
| 34 | // the user namespace, return the equivalent outside id, or return < 0 on error. |
| 35 | int GetUsernsOutsideId(const std::string& map, int id); |
| 36 | |
| 37 | int MakeDir(const base::FilePath& path, int uid, int gid, int mode); |
| 38 | |
| 39 | int TouchFile(const base::FilePath& path, int uid, int gid, int mode); |
| 40 | |
| 41 | // Find a free loop device and attach it. |
| 42 | int LoopdevSetup(const base::FilePath& source, |
| 43 | base::FilePath* loopdev_path_out); |
| 44 | |
| 45 | // Detach the specified loop device. |
| 46 | int LoopdevDetach(const base::FilePath& loopdev); |
| 47 | |
| 48 | // Create a new device mapper target for the source. |
| 49 | int DeviceMapperSetup(const base::FilePath& source, |
| 50 | const std::string& verity_cmdline, |
| 51 | base::FilePath* dm_path_out, |
| 52 | std::string* dm_name_out); |
| 53 | |
| 54 | // Tear down the device mapper target. |
| 55 | int DeviceMapperDetach(const std::string& dm_name); |
| 56 | |
| 57 | // Match mount_one in minijail, mount one mountpoint with |
| 58 | // consideration for combination of MS_BIND/MS_RDONLY flag. |
| 59 | int MountExternal(const std::string& src, |
| 60 | const std::string& dest, |
| 61 | const std::string& type, |
| 62 | unsigned long flags, |
| 63 | const std::string& data); |
| 64 | |
| 65 | } // namespace libcontainer |
| 66 | |
| 67 | #endif // LIBCONTAINER_LIBCONTAINER_UTIL_H_ |