blob: 5932add44ec64fc7c05e63b777c7f251f474fa7b [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 Reid837c74a2016-01-22 17:25:21 -080010
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15struct container_config;
16
17/* Create a container config. */
18struct container_config *container_config_create();
19
20/* Destroy a config create with container_config_create. */
21void container_config_destroy(struct container_config *c);
22
23/* rootfs - Path to the root of the container's filesystem. */
24int container_config_rootfs(struct container_config *c, const char *rootfs);
25
Dylan Reid11456722016-05-02 11:24:50 -070026/* Get the configured rootfs path. */
27const char *container_config_get_rootfs(const struct container_config *c);
28
Keshav Santhanam0e4c3282016-07-14 10:25:16 -070029/* runfs - Path to where the container filesystem has been mounted. */
30int container_config_premounted_runfs(struct container_config *c, const char *runfs);
31
32/* Get the pre-mounted runfs path. */
33const char *container_config_get_premounted_runfs(const struct container_config *c);
34
35/* The pid of the program will be written here. */
36int container_config_pid_file(struct container_config *c, const char *path);
37
38/* Get the pid file path. */
39const char *container_config_get_pid_file(const struct container_config *c);
40
Dylan Reid837c74a2016-01-22 17:25:21 -080041/* The program to run and args, e.g. "/sbin/init", "--second-stage". */
42int container_config_program_argv(struct container_config *c,
43 char **argv, size_t num_args);
44
Dylan Reid11456722016-05-02 11:24:50 -070045/* Get the number of command line args for the program to be run. */
46size_t container_config_get_num_program_args(const struct container_config *c);
47
48/* Get the program argument at the given index. */
49const char *container_config_get_program_arg(const struct container_config *c,
50 size_t index);
51
Dylan Reid1874feb2016-06-22 17:53:50 -070052/* Sets/Gets the uid the container will run as. */
53void container_config_uid(struct container_config *c, uid_t uid);
54uid_t container_config_get_uid(const struct container_config *c);
55
Dylan Reid837c74a2016-01-22 17:25:21 -080056/* Mapping of UIDs in the container, e.g. "0 100000 1024" */
57int container_config_uid_map(struct container_config *c, const char *uid_map);
58
Dylan Reid1874feb2016-06-22 17:53:50 -070059/* Sets/Gets the gid the container will run as. */
60void container_config_gid(struct container_config *c, gid_t gid);
61gid_t container_config_get_gid(const struct container_config *c);
62
Dylan Reid837c74a2016-01-22 17:25:21 -080063/* Mapping of GIDs in the container, e.g. "0 100000 1024" */
64int container_config_gid_map(struct container_config *c, const char *gid_map);
65
66/* Alt-Syscall table to use or NULL if none. */
67int 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 */
85int 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 Reid355d5e42016-04-29 16:53:31 -0700107 * 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 Reid837c74a2016-01-22 17:25:21 -0800110 * 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 */
116int 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 Reid355d5e42016-04-29 16:53:31 -0700122 int copy_minor,
Dylan Reid837c74a2016-01-22 17:25:21 -0800123 int uid,
124 int gid,
125 int read_allowed,
126 int write_allowed,
127 int modify_allowed);
128
Dylan Reid2bd9ea92016-04-07 20:57:47 -0700129/*
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 Chavez479b95f2016-06-06 08:01:05 -0700133int container_config_run_setfiles(struct container_config *c,
134 const char *setfiles_cmd);
Dylan Reid837c74a2016-01-22 17:25:21 -0800135
Dylan Reid11456722016-05-02 11:24:50 -0700136/* Get the setfiles command that is configured to be run. */
137const char *container_config_get_run_setfiles(const struct container_config *c);
138
Chinyue Chenfac909e2016-06-24 14:17:42 +0800139/* Set the CPU shares cgroup param for container. */
140int container_config_set_cpu_shares(struct container_config *c, int shares);
141
142/* Set the CFS CPU cgroup params for container. */
143int 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. */
148int container_config_set_cpu_rt_params(struct container_config *c,
149 int rt_runtime,
150 int rt_period);
151
Chinyue Chen4f3fd682016-07-01 14:11:42 +0800152int container_config_get_cpu_shares(struct container_config *c);
153int container_config_get_cpu_quota(struct container_config *c);
154int container_config_get_cpu_period(struct container_config *c);
155int container_config_get_cpu_rt_runtime(struct container_config *c);
156int container_config_get_cpu_rt_period(struct container_config *c);
157
Dylan Reid9e724af2016-07-21 09:58:07 -0700158/*
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 */
167int 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. */
172const char *container_config_get_cgroup_parent(struct container_config *c);
173
Keshav Santhanam1b6bf672016-08-10 18:35:12 -0700174/* Enable sharing of the host's network namespace with the container */
175void container_config_share_host_netns(struct container_config *c);
176int get_container_config_share_host_netns(struct container_config *c);
177
Dylan Reid837c74a2016-01-22 17:25:21 -0800178/* Container manipulation. */
179struct 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 Reid837c74a2016-01-22 17:25:21 -0800186 */
187struct container *container_new(const char *name,
Dylan Reide040c6b2016-05-02 18:49:02 -0700188 const char *rundir);
Dylan Reid837c74a2016-01-22 17:25:21 -0800189
Keshav Santhanam998fd7d2016-07-12 13:33:00 -0700190/*
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 */
197struct container *container_new_with_cgroup_parent(const char *name,
198 const char *rundir,
199 const char *cgroup_parent);
200
Dylan Reid837c74a2016-01-22 17:25:21 -0800201/* Destroy a container created with container_new. */
202void container_destroy(struct container *c);
203
Dylan Reide040c6b2016-05-02 18:49:02 -0700204/* Start the container. Returns 0 on success.
205 * c - The container to run.
206 * config - Details of how the container should be run.
207 */
208int container_start(struct container *c,
209 const struct container_config *config);
Dylan Reid837c74a2016-01-22 17:25:21 -0800210
211/* Get the path to the root of the container. */
212const char *container_root(struct container *c);
213
214/* Get the pid of the init process in the container. */
215int container_pid(struct container *c);
216
217/* Wait for the container to exit. Returns 0 on success. */
218int container_wait(struct container *c);
219
220/* Kill the container's init process, then wait for it to exit. */
221int container_kill(struct container *c);
222
223#ifdef __cplusplus
224}; /* extern "C" */
225#endif
226
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700227#endif /* LIBCONTAINER_LIBCONTAINER_H_ */