Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1 | /* Copyright 2016 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 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 6 | #ifndef LIBCONTAINER_LIBCONTAINER_H_ |
| 7 | #define LIBCONTAINER_LIBCONTAINER_H_ |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 8 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 9 | #include <stddef.h> |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 10 | #include <stdint.h> |
Luis Hector Chavez | da35246 | 2018-01-30 09:10:00 -0800 | [diff] [blame^] | 11 | #include <sys/resource.h> |
Dylan Reid | 0bb592b | 2016-11-09 13:34:11 -0800 | [diff] [blame] | 12 | #include <sys/types.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 13 | |
Luis Hector Chavez | dac0f66 | 2017-09-14 14:41:27 -0700 | [diff] [blame] | 14 | #include <brillo/brillo_export.h> |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 15 | #include <libminijail.h> |
Luis Hector Chavez | dac0f66 | 2017-09-14 14:41:27 -0700 | [diff] [blame] | 16 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
| 21 | struct container_config; |
| 22 | |
| 23 | /* Create a container config. */ |
Luis Hector Chavez | 1e1fd9d | 2017-09-15 08:45:33 -0700 | [diff] [blame] | 24 | BRILLO_EXPORT struct container_config* container_config_create(void); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 25 | |
| 26 | /* Destroy a config create with container_config_create. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 27 | BRILLO_EXPORT void container_config_destroy(struct container_config* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 28 | |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 29 | /* config_root - Path to the root of the container itself. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 30 | BRILLO_EXPORT int container_config_config_root(struct container_config* c, |
| 31 | const char* config_root); |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 32 | |
| 33 | /* Get the configured container root path. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 34 | BRILLO_EXPORT const char* container_config_get_config_root( |
| 35 | const struct container_config* c); |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 36 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 37 | /* rootfs - Path to the root of the container's filesystem. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 38 | BRILLO_EXPORT int container_config_rootfs(struct container_config* c, |
| 39 | const char* rootfs); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 40 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 41 | /* Get the configured rootfs path. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 42 | BRILLO_EXPORT const char* container_config_get_rootfs( |
| 43 | const struct container_config* c); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 44 | |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 45 | /* rootfs_mount_flags - Flags that will be passed to the mount() call when |
| 46 | * mounting the root of the container's filesystem. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 47 | BRILLO_EXPORT void container_config_rootfs_mount_flags( |
| 48 | struct container_config* c, unsigned long flags); |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 49 | |
| 50 | /* Get the configured rootfs mount() flags. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 51 | BRILLO_EXPORT unsigned long container_config_get_rootfs_mount_flags( |
| 52 | const struct container_config* c); |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 53 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 54 | /* runfs - Path to where the container filesystem has been mounted. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 55 | BRILLO_EXPORT int container_config_premounted_runfs(struct container_config* c, |
| 56 | const char* runfs); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 57 | |
| 58 | /* Get the pre-mounted runfs path. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 59 | BRILLO_EXPORT const char* container_config_get_premounted_runfs( |
| 60 | const struct container_config* c); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 61 | |
| 62 | /* The pid of the program will be written here. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 63 | BRILLO_EXPORT int container_config_pid_file(struct container_config* c, |
| 64 | const char* path); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 65 | |
| 66 | /* Get the pid file path. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 67 | BRILLO_EXPORT const char* container_config_get_pid_file( |
| 68 | const struct container_config* c); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 69 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 70 | /* The program to run and args, e.g. "/sbin/init", "--second-stage". */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 71 | BRILLO_EXPORT int container_config_program_argv(struct container_config* c, |
| 72 | const char** argv, |
| 73 | size_t num_args); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 74 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 75 | /* Get the number of command line args for the program to be run. */ |
Luis Hector Chavez | dac0f66 | 2017-09-14 14:41:27 -0700 | [diff] [blame] | 76 | BRILLO_EXPORT size_t |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 77 | container_config_get_num_program_args(const struct container_config* c); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 78 | |
| 79 | /* Get the program argument at the given index. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 80 | BRILLO_EXPORT const char* container_config_get_program_arg( |
| 81 | const struct container_config* c, size_t index); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 82 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 83 | /* Sets/Gets the uid the container will run as. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 84 | BRILLO_EXPORT void container_config_uid(struct container_config* c, uid_t uid); |
| 85 | BRILLO_EXPORT uid_t container_config_get_uid(const struct container_config* c); |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 86 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 87 | /* Mapping of UIDs in the container, e.g. "0 100000 1024" */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 88 | BRILLO_EXPORT int container_config_uid_map(struct container_config* c, |
| 89 | const char* uid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 90 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 91 | /* Sets/Gets the gid the container will run as. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 92 | BRILLO_EXPORT void container_config_gid(struct container_config* c, gid_t gid); |
| 93 | BRILLO_EXPORT gid_t container_config_get_gid(const struct container_config* c); |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 94 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 95 | /* Mapping of GIDs in the container, e.g. "0 100000 1024" */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 96 | BRILLO_EXPORT int container_config_gid_map(struct container_config* c, |
| 97 | const char* gid_map); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 98 | |
| 99 | /* Alt-Syscall table to use or NULL if none. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 100 | BRILLO_EXPORT int container_config_alt_syscall_table( |
| 101 | struct container_config* c, const char* alt_syscall_table); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 102 | |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 103 | /* Add a runtime limit for the contained process. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 104 | BRILLO_EXPORT int container_config_add_rlimit(struct container_config* c, |
| 105 | int type, |
Luis Hector Chavez | da35246 | 2018-01-30 09:10:00 -0800 | [diff] [blame^] | 106 | rlim_t cur, |
| 107 | rlim_t max); |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 108 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 109 | /* |
| 110 | * Add a filesystem to mount in the new VFS namespace. |
| 111 | * |
| 112 | * c - The container config in which to add the mount. |
| 113 | * source - Mount source, e.g. "tmpfs" or "/data". |
| 114 | * destination - Mount point in the container, e.g. "/dev". |
| 115 | * type - Mount type, e.g. "tmpfs", "selinuxfs", or "devpts". |
| 116 | * data - Mount data for extra options, e.g. "newinstance" or "ptmxmode=0000". |
Mike Frysinger | 05e594e | 2017-01-10 02:11:08 -0500 | [diff] [blame] | 117 | * verity - dm-verity options (if used). |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 118 | * flags - Mount flags as defined in mount(2). |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 119 | * uid - uid to chown mount point to if created. |
| 120 | * gid - gid to chown mount point to if created. |
| 121 | * mode - Permissions of mount point if created. |
| 122 | * mount_in_ns - True if mount should happen in the process' vfs namespace. |
| 123 | * create - If true, create mount destination if it doesn't exist. |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 124 | * loopback - If true, set up a loopback device and mount that. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 125 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 126 | BRILLO_EXPORT int container_config_add_mount(struct container_config* c, |
| 127 | const char* name, |
| 128 | const char* source, |
| 129 | const char* destination, |
| 130 | const char* type, |
| 131 | const char* data, |
| 132 | const char* verity, |
| 133 | int flags, |
| 134 | int uid, |
| 135 | int gid, |
| 136 | int mode, |
| 137 | int mount_in_ns, |
| 138 | int create, |
| 139 | int loopback); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 140 | |
| 141 | /* |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 142 | * Add a device cgroup permission. |
| 143 | * |
| 144 | * c - The container config in which to add the mount. |
| 145 | * allow - If true allow access to the specified r/w/m. |
| 146 | * type - 'c', 'b', or 'a' for char, block, or all respectively. |
| 147 | * major - Major device number. |
| 148 | * minor - Minor device number. |
| 149 | * read - If true set reading of device to |allow|. |
| 150 | * write - If true set writing of device to |allow|. |
| 151 | * modify - If true set modifying of device to |allow|. |
| 152 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 153 | BRILLO_EXPORT int container_config_add_cgroup_device(struct container_config* c, |
| 154 | int allow, |
| 155 | char type, |
| 156 | int major, |
| 157 | int minor, |
| 158 | int read, |
| 159 | int write, |
| 160 | int modify); |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 163 | * Add a device node to create. |
| 164 | * |
| 165 | * c - The container config in which to add the mount. |
| 166 | * type - 'c' or 'b' for char or block respectively. |
| 167 | * path - Where to mknod, "/dev/zero". |
| 168 | * fs_permissions - Permissions to set on the node. |
| 169 | * major - Major device number. |
| 170 | * minor - Minor device number. |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 171 | * copy_major - Overwrite major with the major of the existing device node. If |
| 172 | * this is true major will be copied from an existing node. The |major| param |
| 173 | * should be set to -1 in this case. |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 174 | * copy_minor - Overwrite minor with the minor of the existing device node. If |
| 175 | * this is true minor will be copied from an existing node. The |minor| param |
| 176 | * should be set to -1 in this case. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 177 | * uid - User to own the device. |
| 178 | * gid - Group to own the device. |
| 179 | * read_allowed - If true allow reading from the device via "devices" cgroup. |
| 180 | * write_allowed - If true allow writing to the device via "devices" cgroup. |
| 181 | * modify_allowed - If true allow creation of the device via "devices" cgroup. |
| 182 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 183 | BRILLO_EXPORT int container_config_add_device(struct container_config* c, |
| 184 | char type, |
| 185 | const char* path, |
| 186 | int fs_permissions, |
| 187 | int major, |
| 188 | int minor, |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 189 | int copy_major, |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 190 | int copy_minor, |
| 191 | int uid, |
| 192 | int gid, |
| 193 | int read_allowed, |
| 194 | int write_allowed, |
| 195 | int modify_allowed); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 196 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 197 | /* |
| 198 | * Set to cause the given setfiles command to be run whenever a mount is made |
| 199 | * in the parent mount namespace. |
| 200 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 201 | BRILLO_EXPORT int container_config_run_setfiles(struct container_config* c, |
| 202 | const char* setfiles_cmd); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 203 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 204 | /* Get the setfiles command that is configured to be run. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 205 | BRILLO_EXPORT const char* container_config_get_run_setfiles( |
| 206 | const struct container_config* c); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 207 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 208 | /* Set the CPU shares cgroup param for container. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 209 | BRILLO_EXPORT int container_config_set_cpu_shares(struct container_config* c, |
| 210 | int shares); |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 211 | |
| 212 | /* Set the CFS CPU cgroup params for container. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 213 | BRILLO_EXPORT int container_config_set_cpu_cfs_params( |
| 214 | struct container_config* c, int quota, int period); |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 215 | |
| 216 | /* Set the RT CPU cgroup params for container. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 217 | BRILLO_EXPORT int container_config_set_cpu_rt_params(struct container_config* c, |
| 218 | int rt_runtime, |
| 219 | int rt_period); |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 220 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 221 | BRILLO_EXPORT int container_config_get_cpu_shares(struct container_config* c); |
| 222 | BRILLO_EXPORT int container_config_get_cpu_quota(struct container_config* c); |
| 223 | BRILLO_EXPORT int container_config_get_cpu_period(struct container_config* c); |
| 224 | BRILLO_EXPORT int container_config_get_cpu_rt_runtime( |
| 225 | struct container_config* c); |
| 226 | BRILLO_EXPORT int container_config_get_cpu_rt_period( |
| 227 | struct container_config* c); |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 228 | |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 229 | /* |
| 230 | * Configure the owner of cgroups created for the container. |
| 231 | * |
| 232 | * This is needed so the container's cgroup namespace rootdir is accessible |
| 233 | * inside the container. |
| 234 | * |
| 235 | * cgroup_parent - Parent directory under which to create the cgroup. |
| 236 | * cgroup_owner - The uid that should own the cgroups that are created. |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 237 | * cgroup_group - The gid that should own the cgroups that are created. |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 238 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 239 | BRILLO_EXPORT int container_config_set_cgroup_parent(struct container_config* c, |
| 240 | const char* parent, |
| 241 | uid_t cgroup_owner, |
| 242 | gid_t cgroup_group); |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 243 | |
| 244 | /* Get the parent cgroup directory from the config. Here for UT only. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 245 | BRILLO_EXPORT const char* container_config_get_cgroup_parent( |
| 246 | struct container_config* c); |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 247 | |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 248 | /* Set namespaces to be used by the container. */ |
| 249 | BRILLO_EXPORT int container_config_namespaces( |
| 250 | struct container_config* c, const char** namespaces, size_t num_ns); |
| 251 | |
| 252 | /* Get the number of namespaces to enter. */ |
| 253 | BRILLO_EXPORT size_t |
| 254 | container_config_get_num_namespaces(const struct container_config* c); |
| 255 | |
| 256 | /* Get the namespace at the given index. */ |
| 257 | BRILLO_EXPORT bool container_config_has_namespace( |
| 258 | const struct container_config* c, const char* ns); |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 259 | |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 260 | /* |
| 261 | * Configures the container so that any FDs open in the parent process are still |
| 262 | * visible to the child. Useful for apps that need stdin/stdout/stderr. Use |
| 263 | * with caution to avoid leaking other FDs into the namespaced app. |
| 264 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 265 | BRILLO_EXPORT void container_config_keep_fds_open(struct container_config* c); |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 266 | |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Sets the capability mask of the container to |capmask|. If |ambient| is 1 it |
| 269 | * will additionally set the ambient capability set. |
| 270 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 271 | BRILLO_EXPORT void container_config_set_capmask(struct container_config* c, |
| 272 | uint64_t capmask, |
| 273 | int ambient); |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 274 | |
Luis Hector Chavez | cd44ba7 | 2017-06-30 13:01:38 -0700 | [diff] [blame] | 275 | /* |
| 276 | * Skips settings the securebits in |securebits_skip_mask| when restricting |
| 277 | * capabilities. This is only used when container_config_set_capmask() is used. |
| 278 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 279 | BRILLO_EXPORT void container_config_set_securebits_skip_mask( |
| 280 | struct container_config* c, uint64_t securebits_skip_mask); |
Luis Hector Chavez | cd44ba7 | 2017-06-30 13:01:38 -0700 | [diff] [blame] | 281 | |
Luis Hector Chavez | dac65c3 | 2017-07-21 10:30:23 -0700 | [diff] [blame] | 282 | /* |
| 283 | * Sets whether the container's entry point should run as init. An init process |
| 284 | * is responsible for setting up certain paths within the container (such as |
| 285 | * /proc) and performing explicit reaping of zombie processes. The container |
| 286 | * will also be torn down if the init process is killed. |
| 287 | * The default is true. |
| 288 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 289 | BRILLO_EXPORT void container_config_set_run_as_init(struct container_config* c, |
| 290 | int run_as_init); |
Luis Hector Chavez | dac65c3 | 2017-07-21 10:30:23 -0700 | [diff] [blame] | 291 | |
Luis Hector Chavez | 15e8e67 | 2017-07-20 15:13:27 -0700 | [diff] [blame] | 292 | /* |
| 293 | * Sets the SELinux context under which the container will run. |
| 294 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 295 | BRILLO_EXPORT int container_config_set_selinux_context( |
| 296 | struct container_config* c, const char* context); |
Luis Hector Chavez | 15e8e67 | 2017-07-20 15:13:27 -0700 | [diff] [blame] | 297 | |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Sets a pre-execve hook that is run in the child process just before the |
| 300 | * container invokes execve(2). If this is used to run a pre-start hook which |
| 301 | * should run in the caller's context, a synchronization mechanism (such as a |
| 302 | * pair of pipes or sending messages through a unix domain pipe) should be used |
| 303 | * to ensure this hook blocks until the pre-start hook finishes running. The |
| 304 | * file descriptors used to synchronize this can be passed using |
| 305 | * container_config_inherit_fds(). |
| 306 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 307 | BRILLO_EXPORT void container_config_set_pre_execve_hook( |
| 308 | struct container_config* c, int (*hook)(void*), void* payload); |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 309 | |
| 310 | /* |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 311 | * Adds a hook that will be run, execve(2)-style. This new process will be run |
| 312 | * outside the container in the original namespace. Any parameters that are |
| 313 | * equal to the magic value "$PID" will be replaced with the container's PID. If |
| 314 | * |pstdin_fd|, |pstdout_fd|, or |pstderr_fd| are set to non-null values, they |
| 315 | * will contain valid file descriptors that can be used to communicate with the |
| 316 | * process. |
| 317 | */ |
| 318 | BRILLO_EXPORT int container_config_add_hook(struct container_config* c, |
| 319 | minijail_hook_event_t event, |
| 320 | const char* filename, |
| 321 | const char** argv, |
| 322 | size_t num_args, |
| 323 | int* pstdin_fd, |
| 324 | int* pstdtout_fd, |
| 325 | int* pstderr_fd); |
| 326 | |
| 327 | /* |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 328 | * Sets the set of file descriptors to inherit. |
| 329 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 330 | BRILLO_EXPORT int container_config_inherit_fds(struct container_config* c, |
| 331 | int* inherited_fds, |
| 332 | size_t inherited_fd_count); |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 333 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 334 | /* Container manipulation. */ |
| 335 | struct container; |
| 336 | |
| 337 | /* |
| 338 | * Create a container based on the given config. |
| 339 | * |
| 340 | * name - Name of the directory holding the container config files. |
| 341 | * rundir - Where to build the temporary rootfs. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 342 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 343 | BRILLO_EXPORT struct container* container_new(const char* name, |
| 344 | const char* rundir); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 345 | |
| 346 | /* Destroy a container created with container_new. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 347 | BRILLO_EXPORT void container_destroy(struct container* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 348 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 349 | /* Start the container. Returns 0 on success. |
| 350 | * c - The container to run. |
| 351 | * config - Details of how the container should be run. |
| 352 | */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 353 | BRILLO_EXPORT int container_start(struct container* c, |
| 354 | const struct container_config* config); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 355 | |
| 356 | /* Get the path to the root of the container. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 357 | BRILLO_EXPORT const char* container_root(struct container* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 358 | |
| 359 | /* Get the pid of the init process in the container. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 360 | BRILLO_EXPORT int container_pid(struct container* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 361 | |
| 362 | /* Wait for the container to exit. Returns 0 on success. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 363 | BRILLO_EXPORT int container_wait(struct container* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 364 | |
| 365 | /* Kill the container's init process, then wait for it to exit. */ |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 366 | BRILLO_EXPORT int container_kill(struct container* c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 367 | |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 368 | /* Dumps the container config. The returned string has to be passed to free() |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame] | 369 | when it is no longer needed. |
| 370 | c - The config to dump. |
| 371 | sort_vectors - When not 0, the function sorts the list of mount points, |
| 372 | devices, and cgroups before dumping to make it easier to |
| 373 | compare two dumps side by side. |
| 374 | */ |
| 375 | BRILLO_EXPORT char* container_config_dump(struct container_config* c, |
| 376 | int sort_vectors); |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 377 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 378 | #ifdef __cplusplus |
| 379 | }; /* extern "C" */ |
| 380 | #endif |
| 381 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 382 | #endif /* LIBCONTAINER_LIBCONTAINER_H_ */ |