blob: e0dd41f92a7c23c97c453123f6834c6be0411579 [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>
Luis Hector Chavez644d2042017-09-19 18:56:44 -07009#include <vector>
Luis Hector Chavez81efb332017-09-18 14:01:29 -070010
Luis Hector Chavez644d2042017-09-19 18:56:44 -070011#include <base/callback_forward.h>
Luis Hector Chavez81efb332017-09-18 14:01:29 -070012#include <base/files/file_path.h>
Luis Hector Chavez835d39e2017-09-19 15:16:31 -070013#include <base/logging.h>
14#include <base/macros.h>
Luis Hector Chavez644d2042017-09-19 18:56:44 -070015#include <libminijail.h>
16
17#include "libcontainer/config.h"
18#include "libcontainer/libcontainer.h"
Luis Hector Chavez81efb332017-09-18 14:01:29 -070019
20namespace libcontainer {
21
Luis Hector Chavez644d2042017-09-19 18:56:44 -070022// A hook that can be run at different stages of the container startup. The PID
23// parameter is the pid of the container's init process in the outer namespace.
24// The hook should return true on success.
25using HookCallback = base::Callback<bool(pid_t)>;
26
Luis Hector Chavez835d39e2017-09-19 15:16:31 -070027// Simple class that saves errno.
28class SaveErrno {
29 public:
30 SaveErrno();
31 ~SaveErrno();
32
33 private:
34 const int saved_errno_;
35
36 DISALLOW_COPY_AND_ASSIGN(SaveErrno);
37};
38
39// The comma operator will discard the SaveErrno instance, but will keep it
40// alive until after the whole expression has been evaluated.
41#define PLOG_PRESERVE(verbose_level) \
42 ::libcontainer::SaveErrno(), PLOG(verbose_level)
43
Luis Hector Chavez644d2042017-09-19 18:56:44 -070044// WaitablePipe provides a way for one process to wait on another. This only
45// uses the read(2) and close(2) syscalls, so it can work even in a restrictive
46// environment. Each process must call only one of Wait() and Signal() exactly
47// once.
48struct WaitablePipe {
49 WaitablePipe();
50 ~WaitablePipe();
51
52 WaitablePipe(WaitablePipe&&);
53
54 // Waits for Signal() to be called.
55 void Wait();
56
57 // Notifies the process that called Wait() to continue running.
58 void Signal();
59
60 int pipe_fds[2];
61
62 private:
63 DISALLOW_COPY_AND_ASSIGN(WaitablePipe);
64};
65
66// HookState holds two WaitablePipes so that the container can wait for its
67// parent to run prestart hooks just prior to calling execve(2).
68class HookState {
69 public:
70 HookState();
71 ~HookState();
72
73 HookState(HookState&& state);
74
75 // Initializes this HookState so that WaitForHookAndRun() can be invoked and
76 // waited upon when |j| reaches |event|. Returns true on success.
77 bool InstallHook(minijail* j, minijail_hook_event_t event);
78
79 // Waits for the event specified in InstallHook() and invokes |callbacks| in
80 // the caller process. Returns true if all callbacks succeeded.
81 bool WaitForHookAndRun(const std::vector<HookCallback>& callbacks,
82 pid_t container_pid);
83
84 private:
85 // A function that can be passed to minijail_add_hook() that blocks the
86 // process in the container until the parent has finished running whatever
87 // operations are needed outside the container. This is not expected to be
88 // called directly.
89 static int WaitHook(void* payload);
90
91 bool installed_ = false;
92 WaitablePipe reached_pipe_;
93 WaitablePipe ready_pipe_;
94
95 DISALLOW_COPY_AND_ASSIGN(HookState);
96};
97
Luis Hector Chavez81efb332017-09-18 14:01:29 -070098// Given a uid/gid map of "inside1 outside1 length1, ...", and an id inside of
99// the user namespace, return the equivalent outside id, or return < 0 on error.
100int GetUsernsOutsideId(const std::string& map, int id);
101
102int MakeDir(const base::FilePath& path, int uid, int gid, int mode);
103
104int TouchFile(const base::FilePath& path, int uid, int gid, int mode);
105
106// Find a free loop device and attach it.
107int LoopdevSetup(const base::FilePath& source,
108 base::FilePath* loopdev_path_out);
109
110// Detach the specified loop device.
111int LoopdevDetach(const base::FilePath& loopdev);
112
113// Create a new device mapper target for the source.
114int DeviceMapperSetup(const base::FilePath& source,
115 const std::string& verity_cmdline,
116 base::FilePath* dm_path_out,
117 std::string* dm_name_out);
118
119// Tear down the device mapper target.
120int DeviceMapperDetach(const std::string& dm_name);
121
122// Match mount_one in minijail, mount one mountpoint with
123// consideration for combination of MS_BIND/MS_RDONLY flag.
124int MountExternal(const std::string& src,
125 const std::string& dest,
126 const std::string& type,
127 unsigned long flags,
128 const std::string& data);
129
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700130// Wraps a callback to be run in a subset of the container's namespaces.
131HookCallback AdaptCallbackToRunInNamespaces(HookCallback callback,
132 std::vector<int> nstypes);
133
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700134} // namespace libcontainer
135
136#endif // LIBCONTAINER_LIBCONTAINER_UTIL_H_