blob: 3a259fa3afcd487df82afe5930dc62f005d601c5 [file] [log] [blame]
Luis Hector Chavez81efb332017-09-18 14:01:29 -07001// 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 Chavez835d39e2017-09-19 15:16:31 -070011#include <base/logging.h>
12#include <base/macros.h>
Luis Hector Chavez81efb332017-09-18 14:01:29 -070013
14namespace libcontainer {
15
Luis Hector Chavez835d39e2017-09-19 15:16:31 -070016// Simple class that saves errno.
17class 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 Chavez81efb332017-09-18 14:01:29 -070033// 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.
35int GetUsernsOutsideId(const std::string& map, int id);
36
37int MakeDir(const base::FilePath& path, int uid, int gid, int mode);
38
39int TouchFile(const base::FilePath& path, int uid, int gid, int mode);
40
41// Find a free loop device and attach it.
42int LoopdevSetup(const base::FilePath& source,
43 base::FilePath* loopdev_path_out);
44
45// Detach the specified loop device.
46int LoopdevDetach(const base::FilePath& loopdev);
47
48// Create a new device mapper target for the source.
49int 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.
55int 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.
59int 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_