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