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> |
Dylan Reid | 0bb592b | 2016-11-09 13:34:11 -0800 | [diff] [blame] | 11 | #include <sys/types.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 12 | |
| 13 | #ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | struct container_config; |
| 18 | |
| 19 | /* Create a container config. */ |
| 20 | struct container_config *container_config_create(); |
| 21 | |
| 22 | /* Destroy a config create with container_config_create. */ |
| 23 | void container_config_destroy(struct container_config *c); |
| 24 | |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 25 | /* config_root - Path to the root of the container itself. */ |
| 26 | int container_config_config_root(struct container_config *c, |
| 27 | const char *config_root); |
| 28 | |
| 29 | /* Get the configured container root path. */ |
| 30 | const char *container_config_get_config_root(const struct container_config *c); |
| 31 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 32 | /* rootfs - Path to the root of the container's filesystem. */ |
| 33 | int container_config_rootfs(struct container_config *c, const char *rootfs); |
| 34 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 35 | /* Get the configured rootfs path. */ |
| 36 | const char *container_config_get_rootfs(const struct container_config *c); |
| 37 | |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 38 | /* rootfs_mount_flags - Flags that will be passed to the mount() call when |
| 39 | * mounting the root of the container's filesystem. */ |
| 40 | void container_config_rootfs_mount_flags(struct container_config *c, |
| 41 | unsigned long flags); |
| 42 | |
| 43 | /* Get the configured rootfs mount() flags. */ |
| 44 | unsigned long container_config_get_rootfs_mount_flags( |
| 45 | const struct container_config *c); |
| 46 | |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 47 | /* runfs - Path to where the container filesystem has been mounted. */ |
Luis Hector Chavez | c32a2e6 | 2016-09-23 09:19:16 -0700 | [diff] [blame] | 48 | int container_config_premounted_runfs(struct container_config *c, |
| 49 | const char *runfs); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 50 | |
| 51 | /* Get the pre-mounted runfs path. */ |
Luis Hector Chavez | c32a2e6 | 2016-09-23 09:19:16 -0700 | [diff] [blame] | 52 | const char *container_config_get_premounted_runfs( |
| 53 | const struct container_config *c); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 54 | |
| 55 | /* The pid of the program will be written here. */ |
| 56 | int container_config_pid_file(struct container_config *c, const char *path); |
| 57 | |
| 58 | /* Get the pid file path. */ |
| 59 | const char *container_config_get_pid_file(const struct container_config *c); |
| 60 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 61 | /* The program to run and args, e.g. "/sbin/init", "--second-stage". */ |
| 62 | int container_config_program_argv(struct container_config *c, |
Dylan Reid | 17fd53f | 2016-11-18 19:14:41 -0800 | [diff] [blame] | 63 | const char **argv, size_t num_args); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 64 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 65 | /* Get the number of command line args for the program to be run. */ |
| 66 | size_t container_config_get_num_program_args(const struct container_config *c); |
| 67 | |
| 68 | /* Get the program argument at the given index. */ |
| 69 | const char *container_config_get_program_arg(const struct container_config *c, |
| 70 | size_t index); |
| 71 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 72 | /* Sets/Gets the uid the container will run as. */ |
| 73 | void container_config_uid(struct container_config *c, uid_t uid); |
| 74 | uid_t container_config_get_uid(const struct container_config *c); |
| 75 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 76 | /* Mapping of UIDs in the container, e.g. "0 100000 1024" */ |
| 77 | int container_config_uid_map(struct container_config *c, const char *uid_map); |
| 78 | |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 79 | /* Sets/Gets the gid the container will run as. */ |
| 80 | void container_config_gid(struct container_config *c, gid_t gid); |
| 81 | gid_t container_config_get_gid(const struct container_config *c); |
| 82 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 83 | /* Mapping of GIDs in the container, e.g. "0 100000 1024" */ |
| 84 | int container_config_gid_map(struct container_config *c, const char *gid_map); |
| 85 | |
| 86 | /* Alt-Syscall table to use or NULL if none. */ |
| 87 | int container_config_alt_syscall_table(struct container_config *c, |
| 88 | const char *alt_syscall_table); |
| 89 | |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame^] | 90 | /* Add a runtime limit for the contained process. */ |
| 91 | int container_config_add_rlimit(struct container_config *c, int type, |
| 92 | uint32_t cur, uint32_t max); |
| 93 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 94 | /* |
| 95 | * Add a filesystem to mount in the new VFS namespace. |
| 96 | * |
| 97 | * c - The container config in which to add the mount. |
| 98 | * source - Mount source, e.g. "tmpfs" or "/data". |
| 99 | * destination - Mount point in the container, e.g. "/dev". |
| 100 | * type - Mount type, e.g. "tmpfs", "selinuxfs", or "devpts". |
| 101 | * data - Mount data for extra options, e.g. "newinstance" or "ptmxmode=0000". |
Mike Frysinger | 05e594e | 2017-01-10 02:11:08 -0500 | [diff] [blame] | 102 | * verity - dm-verity options (if used). |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 103 | * flags - Mount flags as defined in mount(2). |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 104 | * uid - uid to chown mount point to if created. |
| 105 | * gid - gid to chown mount point to if created. |
| 106 | * mode - Permissions of mount point if created. |
| 107 | * mount_in_ns - True if mount should happen in the process' vfs namespace. |
| 108 | * create - If true, create mount destination if it doesn't exist. |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 109 | * loopback - If true, set up a loopback device and mount that. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 110 | */ |
| 111 | int container_config_add_mount(struct container_config *c, |
| 112 | const char *name, |
| 113 | const char *source, |
| 114 | const char *destination, |
| 115 | const char *type, |
| 116 | const char *data, |
Mike Frysinger | 05e594e | 2017-01-10 02:11:08 -0500 | [diff] [blame] | 117 | const char *verity, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 118 | int flags, |
| 119 | int uid, |
| 120 | int gid, |
| 121 | int mode, |
| 122 | int mount_in_ns, |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 123 | int create, |
| 124 | int loopback); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 125 | |
| 126 | /* |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 127 | * Add a device cgroup permission. |
| 128 | * |
| 129 | * c - The container config in which to add the mount. |
| 130 | * allow - If true allow access to the specified r/w/m. |
| 131 | * type - 'c', 'b', or 'a' for char, block, or all respectively. |
| 132 | * major - Major device number. |
| 133 | * minor - Minor device number. |
| 134 | * read - If true set reading of device to |allow|. |
| 135 | * write - If true set writing of device to |allow|. |
| 136 | * modify - If true set modifying of device to |allow|. |
| 137 | */ |
| 138 | int container_config_add_cgroup_device(struct container_config *c, |
| 139 | int allow, |
| 140 | char type, |
| 141 | int major, |
| 142 | int minor, |
| 143 | int read, |
| 144 | int write, |
| 145 | int modify); |
| 146 | |
| 147 | /* |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 148 | * Add a device node to create. |
| 149 | * |
| 150 | * c - The container config in which to add the mount. |
| 151 | * type - 'c' or 'b' for char or block respectively. |
| 152 | * path - Where to mknod, "/dev/zero". |
| 153 | * fs_permissions - Permissions to set on the node. |
| 154 | * major - Major device number. |
| 155 | * minor - Minor device number. |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 156 | * copy_minor - Overwrite minor with the minor of the existing device node. If |
| 157 | * this is true minor will be copied from an existing node. The |minor| param |
| 158 | * should be set to -1 in this case. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 159 | * uid - User to own the device. |
| 160 | * gid - Group to own the device. |
| 161 | * read_allowed - If true allow reading from the device via "devices" cgroup. |
| 162 | * write_allowed - If true allow writing to the device via "devices" cgroup. |
| 163 | * modify_allowed - If true allow creation of the device via "devices" cgroup. |
| 164 | */ |
| 165 | int container_config_add_device(struct container_config *c, |
| 166 | char type, |
| 167 | const char *path, |
| 168 | int fs_permissions, |
| 169 | int major, |
| 170 | int minor, |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 171 | int copy_minor, |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 172 | int uid, |
| 173 | int gid, |
| 174 | int read_allowed, |
| 175 | int write_allowed, |
| 176 | int modify_allowed); |
| 177 | |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 178 | /* |
| 179 | * Set to cause the given setfiles command to be run whenever a mount is made |
| 180 | * in the parent mount namespace. |
| 181 | */ |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 182 | int container_config_run_setfiles(struct container_config *c, |
| 183 | const char *setfiles_cmd); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 184 | |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 185 | /* Get the setfiles command that is configured to be run. */ |
| 186 | const char *container_config_get_run_setfiles(const struct container_config *c); |
| 187 | |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 188 | /* Set the CPU shares cgroup param for container. */ |
| 189 | int container_config_set_cpu_shares(struct container_config *c, int shares); |
| 190 | |
| 191 | /* Set the CFS CPU cgroup params for container. */ |
| 192 | int container_config_set_cpu_cfs_params(struct container_config *c, |
| 193 | int quota, |
| 194 | int period); |
| 195 | |
| 196 | /* Set the RT CPU cgroup params for container. */ |
| 197 | int container_config_set_cpu_rt_params(struct container_config *c, |
| 198 | int rt_runtime, |
| 199 | int rt_period); |
| 200 | |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 201 | int container_config_get_cpu_shares(struct container_config *c); |
| 202 | int container_config_get_cpu_quota(struct container_config *c); |
| 203 | int container_config_get_cpu_period(struct container_config *c); |
| 204 | int container_config_get_cpu_rt_runtime(struct container_config *c); |
| 205 | int container_config_get_cpu_rt_period(struct container_config *c); |
| 206 | |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Configure the owner of cgroups created for the container. |
| 209 | * |
| 210 | * This is needed so the container's cgroup namespace rootdir is accessible |
| 211 | * inside the container. |
| 212 | * |
| 213 | * cgroup_parent - Parent directory under which to create the cgroup. |
| 214 | * cgroup_owner - The uid that should own the cgroups that are created. |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 215 | * cgroup_group - The gid that should own the cgroups that are created. |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 216 | */ |
| 217 | int container_config_set_cgroup_parent(struct container_config *c, |
| 218 | const char *parent, |
Dmitry Torokhov | 14eef72 | 2016-09-27 16:40:37 -0700 | [diff] [blame] | 219 | uid_t cgroup_owner, |
| 220 | gid_t cgroup_group); |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 221 | |
| 222 | /* Get the parent cgroup directory from the config. Here for UT only. */ |
| 223 | const char *container_config_get_cgroup_parent(struct container_config *c); |
| 224 | |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 225 | /* Enable sharing of the host's network namespace with the container */ |
| 226 | void container_config_share_host_netns(struct container_config *c); |
| 227 | int get_container_config_share_host_netns(struct container_config *c); |
| 228 | |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 229 | /* |
| 230 | * Configures the container so that any FDs open in the parent process are still |
| 231 | * visible to the child. Useful for apps that need stdin/stdout/stderr. Use |
| 232 | * with caution to avoid leaking other FDs into the namespaced app. |
| 233 | */ |
| 234 | void container_config_keep_fds_open(struct container_config *c); |
| 235 | |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Sets the capability mask of the container to |capmask|. If |ambient| is 1 it |
| 238 | * will additionally set the ambient capability set. |
| 239 | */ |
| 240 | void container_config_set_capmask(struct container_config *c, |
| 241 | uint64_t capmask, |
| 242 | int ambient); |
| 243 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 244 | /* Container manipulation. */ |
| 245 | struct container; |
| 246 | |
| 247 | /* |
| 248 | * Create a container based on the given config. |
| 249 | * |
| 250 | * name - Name of the directory holding the container config files. |
| 251 | * rundir - Where to build the temporary rootfs. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 252 | */ |
| 253 | struct container *container_new(const char *name, |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 254 | const char *rundir); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 255 | |
| 256 | /* Destroy a container created with container_new. */ |
| 257 | void container_destroy(struct container *c); |
| 258 | |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 259 | /* Start the container. Returns 0 on success. |
| 260 | * c - The container to run. |
| 261 | * config - Details of how the container should be run. |
| 262 | */ |
| 263 | int container_start(struct container *c, |
| 264 | const struct container_config *config); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 265 | |
| 266 | /* Get the path to the root of the container. */ |
| 267 | const char *container_root(struct container *c); |
| 268 | |
| 269 | /* Get the pid of the init process in the container. */ |
| 270 | int container_pid(struct container *c); |
| 271 | |
| 272 | /* Wait for the container to exit. Returns 0 on success. */ |
| 273 | int container_wait(struct container *c); |
| 274 | |
| 275 | /* Kill the container's init process, then wait for it to exit. */ |
| 276 | int container_kill(struct container *c); |
| 277 | |
| 278 | #ifdef __cplusplus |
| 279 | }; /* extern "C" */ |
| 280 | #endif |
| 281 | |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 282 | #endif /* LIBCONTAINER_LIBCONTAINER_H_ */ |