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