blob: 41c99bb661e952e2f41d545ef2ab645bd05f398b [file] [log] [blame]
Dylan Reid837c74a2016-01-22 17:25:21 -08001/* 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 Chavez479b95f2016-06-06 08:01:05 -07006#ifndef LIBCONTAINER_LIBCONTAINER_H_
7#define LIBCONTAINER_LIBCONTAINER_H_
Dylan Reid837c74a2016-01-22 17:25:21 -08008
Dylan Reid2bd9ea92016-04-07 20:57:47 -07009#include <stddef.h>
Dylan Reid0bb592b2016-11-09 13:34:11 -080010#include <sys/types.h>
Dylan Reid837c74a2016-01-22 17:25:21 -080011
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16struct container_config;
17
18/* Create a container config. */
19struct container_config *container_config_create();
20
21/* Destroy a config create with container_config_create. */
22void container_config_destroy(struct container_config *c);
23
Mike Frysingerb22acdf2017-01-08 02:02:35 -050024/* config_root - Path to the root of the container itself. */
25int container_config_config_root(struct container_config *c,
26 const char *config_root);
27
28/* Get the configured container root path. */
29const char *container_config_get_config_root(const struct container_config *c);
30
Dylan Reid837c74a2016-01-22 17:25:21 -080031/* rootfs - Path to the root of the container's filesystem. */
32int container_config_rootfs(struct container_config *c, const char *rootfs);
33
Dylan Reid11456722016-05-02 11:24:50 -070034/* Get the configured rootfs path. */
35const char *container_config_get_rootfs(const struct container_config *c);
36
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -070037/* rootfs_mount_flags - Flags that will be passed to the mount() call when
38 * mounting the root of the container's filesystem. */
39void container_config_rootfs_mount_flags(struct container_config *c,
40 unsigned long flags);
41
42/* Get the configured rootfs mount() flags. */
43unsigned long container_config_get_rootfs_mount_flags(
44 const struct container_config *c);
45
Keshav Santhanam0e4c3282016-07-14 10:25:16 -070046/* runfs - Path to where the container filesystem has been mounted. */
Luis Hector Chavezc32a2e62016-09-23 09:19:16 -070047int container_config_premounted_runfs(struct container_config *c,
48 const char *runfs);
Keshav Santhanam0e4c3282016-07-14 10:25:16 -070049
50/* Get the pre-mounted runfs path. */
Luis Hector Chavezc32a2e62016-09-23 09:19:16 -070051const char *container_config_get_premounted_runfs(
52 const struct container_config *c);
Keshav Santhanam0e4c3282016-07-14 10:25:16 -070053
54/* The pid of the program will be written here. */
55int container_config_pid_file(struct container_config *c, const char *path);
56
57/* Get the pid file path. */
58const char *container_config_get_pid_file(const struct container_config *c);
59
Dylan Reid837c74a2016-01-22 17:25:21 -080060/* The program to run and args, e.g. "/sbin/init", "--second-stage". */
61int container_config_program_argv(struct container_config *c,
Dylan Reid17fd53f2016-11-18 19:14:41 -080062 const char **argv, size_t num_args);
Dylan Reid837c74a2016-01-22 17:25:21 -080063
Dylan Reid11456722016-05-02 11:24:50 -070064/* Get the number of command line args for the program to be run. */
65size_t container_config_get_num_program_args(const struct container_config *c);
66
67/* Get the program argument at the given index. */
68const char *container_config_get_program_arg(const struct container_config *c,
69 size_t index);
70
Dylan Reid1874feb2016-06-22 17:53:50 -070071/* Sets/Gets the uid the container will run as. */
72void container_config_uid(struct container_config *c, uid_t uid);
73uid_t container_config_get_uid(const struct container_config *c);
74
Dylan Reid837c74a2016-01-22 17:25:21 -080075/* Mapping of UIDs in the container, e.g. "0 100000 1024" */
76int container_config_uid_map(struct container_config *c, const char *uid_map);
77
Dylan Reid1874feb2016-06-22 17:53:50 -070078/* Sets/Gets the gid the container will run as. */
79void container_config_gid(struct container_config *c, gid_t gid);
80gid_t container_config_get_gid(const struct container_config *c);
81
Dylan Reid837c74a2016-01-22 17:25:21 -080082/* Mapping of GIDs in the container, e.g. "0 100000 1024" */
83int container_config_gid_map(struct container_config *c, const char *gid_map);
84
85/* Alt-Syscall table to use or NULL if none. */
86int 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 Frysinger05e594e2017-01-10 02:11:08 -050097 * verity - dm-verity options (if used).
Mike Frysinger412dbd22017-01-06 01:50:34 -050098 * flags - Mount flags as defined in mount(2).
Dylan Reid837c74a2016-01-22 17:25:21 -080099 * 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 Frysinger412dbd22017-01-06 01:50:34 -0500104 * loopback - If true, set up a loopback device and mount that.
Dylan Reid837c74a2016-01-22 17:25:21 -0800105 */
106int 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 Frysinger05e594e2017-01-10 02:11:08 -0500112 const char *verity,
Dylan Reid837c74a2016-01-22 17:25:21 -0800113 int flags,
114 int uid,
115 int gid,
116 int mode,
117 int mount_in_ns,
Mike Frysinger412dbd22017-01-06 01:50:34 -0500118 int create,
119 int loopback);
Dylan Reid837c74a2016-01-22 17:25:21 -0800120
121/*
Dylan Reid4843d6b2017-03-31 18:14:30 -0700122 * Add a device cgroup permission.
123 *
124 * c - The container config in which to add the mount.
125 * allow - If true allow access to the specified r/w/m.
126 * type - 'c', 'b', or 'a' for char, block, or all respectively.
127 * major - Major device number.
128 * minor - Minor device number.
129 * read - If true set reading of device to |allow|.
130 * write - If true set writing of device to |allow|.
131 * modify - If true set modifying of device to |allow|.
132 */
133int container_config_add_cgroup_device(struct container_config *c,
134 int allow,
135 char type,
136 int major,
137 int minor,
138 int read,
139 int write,
140 int modify);
141
142/*
Dylan Reid837c74a2016-01-22 17:25:21 -0800143 * Add a device node to create.
144 *
145 * c - The container config in which to add the mount.
146 * type - 'c' or 'b' for char or block respectively.
147 * path - Where to mknod, "/dev/zero".
148 * fs_permissions - Permissions to set on the node.
149 * major - Major device number.
150 * minor - Minor device number.
Dylan Reid355d5e42016-04-29 16:53:31 -0700151 * copy_minor - Overwrite minor with the minor of the existing device node. If
152 * this is true minor will be copied from an existing node. The |minor| param
153 * should be set to -1 in this case.
Dylan Reid837c74a2016-01-22 17:25:21 -0800154 * uid - User to own the device.
155 * gid - Group to own the device.
156 * read_allowed - If true allow reading from the device via "devices" cgroup.
157 * write_allowed - If true allow writing to the device via "devices" cgroup.
158 * modify_allowed - If true allow creation of the device via "devices" cgroup.
159 */
160int container_config_add_device(struct container_config *c,
161 char type,
162 const char *path,
163 int fs_permissions,
164 int major,
165 int minor,
Dylan Reid355d5e42016-04-29 16:53:31 -0700166 int copy_minor,
Dylan Reid837c74a2016-01-22 17:25:21 -0800167 int uid,
168 int gid,
169 int read_allowed,
170 int write_allowed,
171 int modify_allowed);
172
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700173/*
174 * Set to cause the given setfiles command to be run whenever a mount is made
175 * in the parent mount namespace.
176 */
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700177int container_config_run_setfiles(struct container_config *c,
178 const char *setfiles_cmd);
Dylan Reid837c74a2016-01-22 17:25:21 -0800179
Dylan Reid11456722016-05-02 11:24:50 -0700180/* Get the setfiles command that is configured to be run. */
181const char *container_config_get_run_setfiles(const struct container_config *c);
182
Chinyue Chenfac909e2016-06-24 14:17:42 +0800183/* Set the CPU shares cgroup param for container. */
184int container_config_set_cpu_shares(struct container_config *c, int shares);
185
186/* Set the CFS CPU cgroup params for container. */
187int container_config_set_cpu_cfs_params(struct container_config *c,
188 int quota,
189 int period);
190
191/* Set the RT CPU cgroup params for container. */
192int container_config_set_cpu_rt_params(struct container_config *c,
193 int rt_runtime,
194 int rt_period);
195
Chinyue Chen4f3fd682016-07-01 14:11:42 +0800196int container_config_get_cpu_shares(struct container_config *c);
197int container_config_get_cpu_quota(struct container_config *c);
198int container_config_get_cpu_period(struct container_config *c);
199int container_config_get_cpu_rt_runtime(struct container_config *c);
200int container_config_get_cpu_rt_period(struct container_config *c);
201
Dylan Reid9e724af2016-07-21 09:58:07 -0700202/*
203 * Configure the owner of cgroups created for the container.
204 *
205 * This is needed so the container's cgroup namespace rootdir is accessible
206 * inside the container.
207 *
208 * cgroup_parent - Parent directory under which to create the cgroup.
209 * cgroup_owner - The uid that should own the cgroups that are created.
Dmitry Torokhov14eef722016-09-27 16:40:37 -0700210 * cgroup_group - The gid that should own the cgroups that are created.
Dylan Reid9e724af2016-07-21 09:58:07 -0700211 */
212int container_config_set_cgroup_parent(struct container_config *c,
213 const char *parent,
Dmitry Torokhov14eef722016-09-27 16:40:37 -0700214 uid_t cgroup_owner,
215 gid_t cgroup_group);
Dylan Reid9e724af2016-07-21 09:58:07 -0700216
217/* Get the parent cgroup directory from the config. Here for UT only. */
218const char *container_config_get_cgroup_parent(struct container_config *c);
219
Keshav Santhanam1b6bf672016-08-10 18:35:12 -0700220/* Enable sharing of the host's network namespace with the container */
221void container_config_share_host_netns(struct container_config *c);
222int get_container_config_share_host_netns(struct container_config *c);
223
Dylan Reidc4335842016-11-11 10:24:52 -0800224/*
225 * Configures the container so that any FDs open in the parent process are still
226 * visible to the child. Useful for apps that need stdin/stdout/stderr. Use
227 * with caution to avoid leaking other FDs into the namespaced app.
228 */
229void container_config_keep_fds_open(struct container_config *c);
230
Dylan Reid837c74a2016-01-22 17:25:21 -0800231/* Container manipulation. */
232struct container;
233
234/*
235 * Create a container based on the given config.
236 *
237 * name - Name of the directory holding the container config files.
238 * rundir - Where to build the temporary rootfs.
Dylan Reid837c74a2016-01-22 17:25:21 -0800239 */
240struct container *container_new(const char *name,
Dylan Reide040c6b2016-05-02 18:49:02 -0700241 const char *rundir);
Dylan Reid837c74a2016-01-22 17:25:21 -0800242
243/* Destroy a container created with container_new. */
244void container_destroy(struct container *c);
245
Dylan Reide040c6b2016-05-02 18:49:02 -0700246/* Start the container. Returns 0 on success.
247 * c - The container to run.
248 * config - Details of how the container should be run.
249 */
250int container_start(struct container *c,
251 const struct container_config *config);
Dylan Reid837c74a2016-01-22 17:25:21 -0800252
253/* Get the path to the root of the container. */
254const char *container_root(struct container *c);
255
256/* Get the pid of the init process in the container. */
257int container_pid(struct container *c);
258
259/* Wait for the container to exit. Returns 0 on success. */
260int container_wait(struct container *c);
261
262/* Kill the container's init process, then wait for it to exit. */
263int container_kill(struct container *c);
264
265#ifdef __cplusplus
266}; /* extern "C" */
267#endif
268
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700269#endif /* LIBCONTAINER_LIBCONTAINER_H_ */