blob: 76e6253c8c51e251489d2cd778287a9f810cf327 [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// WaitablePipe provides a way for one process to wait on another. This only
23// uses the read(2) and close(2) syscalls, so it can work even in a restrictive
24// environment. Each process must call only one of Wait() and Signal() exactly
25// once.
26struct WaitablePipe {
27 WaitablePipe();
28 ~WaitablePipe();
29
30 WaitablePipe(WaitablePipe&&);
31
32 // Waits for Signal() to be called.
33 void Wait();
34
35 // Notifies the process that called Wait() to continue running.
36 void Signal();
37
38 int pipe_fds[2];
39
40 private:
41 DISALLOW_COPY_AND_ASSIGN(WaitablePipe);
42};
43
44// HookState holds two WaitablePipes so that the container can wait for its
45// parent to run prestart hooks just prior to calling execve(2).
46class HookState {
47 public:
48 HookState();
49 ~HookState();
50
51 HookState(HookState&& state);
52
53 // Initializes this HookState so that WaitForHookAndRun() can be invoked and
54 // waited upon when |j| reaches |event|. Returns true on success.
55 bool InstallHook(minijail* j, minijail_hook_event_t event);
56
57 // Waits for the event specified in InstallHook() and invokes |callbacks| in
58 // the caller process. Returns true if all callbacks succeeded.
59 bool WaitForHookAndRun(const std::vector<HookCallback>& callbacks,
60 pid_t container_pid);
61
62 private:
63 // A function that can be passed to minijail_add_hook() that blocks the
64 // process in the container until the parent has finished running whatever
65 // operations are needed outside the container. This is not expected to be
66 // called directly.
67 static int WaitHook(void* payload);
68
69 bool installed_ = false;
70 WaitablePipe reached_pipe_;
71 WaitablePipe ready_pipe_;
72
73 DISALLOW_COPY_AND_ASSIGN(HookState);
74};
75
Luis Hector Chavez81efb332017-09-18 14:01:29 -070076// Given a uid/gid map of "inside1 outside1 length1, ...", and an id inside of
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070077// the user namespace, populate |id_out|. Returns true on success.
78bool GetUsernsOutsideId(const std::string& map, int id, int* id_out);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070079
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070080bool MakeDir(const base::FilePath& path, int uid, int gid, int mode);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070081
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070082bool TouchFile(const base::FilePath& path, int uid, int gid, int mode);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070083
84// Find a free loop device and attach it.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070085bool LoopdevSetup(const base::FilePath& source,
86 base::FilePath* loopdev_path_out);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070087
88// Detach the specified loop device.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070089bool LoopdevDetach(const base::FilePath& loopdev);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070090
91// Create a new device mapper target for the source.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070092bool DeviceMapperSetup(const base::FilePath& source,
93 const std::string& verity_cmdline,
94 base::FilePath* dm_path_out,
95 std::string* dm_name_out);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070096
97// Tear down the device mapper target.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -070098bool DeviceMapperDetach(const std::string& dm_name);
Luis Hector Chavez81efb332017-09-18 14:01:29 -070099
100// Match mount_one in minijail, mount one mountpoint with
101// consideration for combination of MS_BIND/MS_RDONLY flag.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700102bool MountExternal(const std::string& src,
103 const std::string& dest,
104 const std::string& type,
105 unsigned long flags,
106 const std::string& data);
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700107
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700108// Wraps a callback to be run in a subset of the container's namespaces.
109HookCallback AdaptCallbackToRunInNamespaces(HookCallback callback,
110 std::vector<int> nstypes);
111
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700112} // namespace libcontainer
113
114#endif // LIBCONTAINER_LIBCONTAINER_UTIL_H_