libcgroup
Loading...
Searching...
No Matches
libcgroup-internal.h
1/* SPDX-License-Identifier: LGPL-2.1-only */
8#ifndef __LIBCG_INTERNAL
9
10#define __LIBCG_INTERNAL
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#include "config.h"
17
18#include <libcgroup.h>
19
20#include <pthread.h>
21#include <limits.h>
22#include <setjmp.h>
23#include <fts.h>
24
25#include <sys/stat.h>
26#include <sys/types.h>
27
28struct dirent;
29struct mntent;
30
31#define MAX_MNT_ELEMENTS 17 /* Maximum number of mount points/controllers */
32#define MAX_GROUP_ELEMENTS 128 /* Estimated number of groups created */
33
34#define CG_CONTROL_VALUE_MAX 4096 /* Maximum length of a value */
35
36#define CG_NV_MAX 100
37#define CG_CONTROLLER_MAX 100
38#define CG_OPTIONS_MAX 100
39
40/*
41 * Max number of mounted hierarchies. Event if one controller is mounted
42 * per hierarchy, it can not exceed CG_CONTROLLER_MAX
43 */
44#define CG_HIER_MAX CG_CONTROLLER_MAX
45
46#define CONTROL_NAMELEN_MAX 32 /* Maximum length of a controller's name */
47
48/* Definitions for the uid and gid members of a cgroup_rules */
49#define CGRULE_INVALID ((uid_t) -1)
50#define CGRULE_WILD ((uid_t) -2)
51
52#define CGRULE_SUCCESS_STORE_PID "SUCCESS_STORE_PID"
53/* Definitions for cgrules options field */
54#define CGRULE_OPTION_IGNORE "ignore"
55#define CGRULE_OPTION_IGNORE_RT "ignore_rt"
56#define CGRULE_OPT_IGNORE 1
57#define CGRULE_OPT_IGNORE_RT 2
58
59#define CGCONFIG_CONF_FILE "/etc/cgconfig.conf"
60/* Minimum number of file in template file list for cgrulesengd */
61#define CGCONFIG_CONF_FILES_LIST_MINIMUM_SIZE 4
62#define CGCONFIG_CONF_DIR "/etc/cgconfig.d"
63
64#define CGRULES_CONF_FILE "/etc/cgrules.conf"
65#define CGRULES_CONF_DIR "/etc/cgrules.d"
66#define CGRULES_MAX_FIELDS_PER_LINE 3
67
68#define CGRP_BUFFER_LEN (5 * FILENAME_MAX)
69
70/* Maximum length of a key(<user>:<process name>) in the daemon config file */
71#define CGRP_RULE_MAXKEY (LOGIN_NAME_MAX + FILENAME_MAX + 1)
72
73/* Maximum length of a line in the daemon config file */
74#define CGRP_RULE_MAXLINE (FILENAME_MAX + CGRP_RULE_MAXKEY + CG_CONTROLLER_MAX + \
75 CG_OPTIONS_MAX + 4)
76
77#define CGRP_FILE_PREFIX "cgroup"
78
79/* cgroup v2 files */
80#define CGV2_CONTROLLERS_FILE "cgroup.controllers"
81#define CGV2_SUBTREE_CTRL_FILE "cgroup.subtree_control"
82
83/* maximum line length when reading the cgroup.controllers file */
84#define CGV2_CONTROLLERS_LL_MAX 100
85
86#define cgroup_err(x...) cgroup_log(CGROUP_LOG_ERROR, "Error: " x)
87#define cgroup_warn(x...) cgroup_log(CGROUP_LOG_WARNING, "Warning: " x)
88#define cgroup_info(x...) cgroup_log(CGROUP_LOG_INFO, "Info: " x)
89#define cgroup_dbg(x...) cgroup_log(CGROUP_LOG_DEBUG, x)
90#define cgroup_cont(x...) cgroup_log(CGROUP_LOG_CONT, x)
91
92#define CGRP_DEFAULT_LOGLEVEL CGROUP_LOG_ERROR
93
94#define max(x, y) ((y) < (x)?(x):(y))
95#define min(x, y) ((y) > (x)?(x):(y))
96
97#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
98
100 char name[FILENAME_MAX];
101 char value[CG_CONTROL_VALUE_MAX];
102
103 /* cgget uses this field for values that span multiple lines */
104 char *multiline_value;
105
106 /*
107 * The abstraction layer uses prev_name when there's an
108 * N->1 or 1->N relationship between cgroup v1 and v2 settings.
109 */
110 char *prev_name;
111
112 bool dirty;
113};
114
116 char name[CONTROL_NAMELEN_MAX];
117 struct control_value *values[CG_NV_MAX];
118 struct cgroup *cgroup;
119 int index;
120 enum cg_version_t version;
121};
122
123struct cgroup {
124 char name[FILENAME_MAX];
125 struct cgroup_controller *controller[CG_CONTROLLER_MAX];
126 int index;
127 uid_t tasks_uid;
128 gid_t tasks_gid;
129 mode_t task_fperm;
130 uid_t control_uid;
131 gid_t control_gid;
132 mode_t control_fperm;
133 mode_t control_dperm;
134};
135
137 char path[FILENAME_MAX];
138 struct cg_mount_point *next;
139};
140
143 char name[CONTROL_NAMELEN_MAX];
148 int index;
149 int shared_mnt;
150 enum cg_version_t version;
151};
152
154 pid_t pid; /* pid of the process which needs to change group */
155
156 /* Details of user under consideration for destination cgroup */
157 struct passwd *pw;
158 /* gid of the process */
159 gid_t gid;
160};
161
162/* A rule that maps UID/GID to a cgroup */
164 uid_t uid;
165 gid_t gid;
166 int is_ignore;
167 char *procname;
168 char username[LOGIN_NAME_MAX];
169 char destination[FILENAME_MAX];
170 char *controllers[MAX_MNT_ELEMENTS];
171 struct cgroup_rule *next;
172};
173
174/* Container for a list of rules */
176 struct cgroup_rule *head;
177 struct cgroup_rule *tail;
178 int len;
179};
180
181/* The walk_tree handle */
183 FTS *fts;
184 int flags;
185};
186
195 const char *name;
196 const char *value;
197 struct cgroup_dictionary_item *next;
198};
199
200/* Flags for cgroup_dictionary_create */
206#define CG_DICT_DONT_FREE_ITEMS 1
207
217 struct cgroup_dictionary_item *head;
218 struct cgroup_dictionary_item *tail;
219 int flags;
220};
221
224 struct cgroup_dictionary_item *item;
225};
226
230extern __thread int last_errno;
231
235extern jmp_buf parser_error_env;
236
237/* Internal API */
238char *cg_build_path(const char *name, char *path, const char *type);
239int cgroup_get_uid_gid_from_procfs(pid_t pid, uid_t *euid, gid_t *egid);
240int cgroup_get_procname_from_procfs(pid_t pid, char **procname);
241int cg_mkdir_p(const char *path);
242struct cgroup *create_cgroup_from_name_value_pairs(const char *name,
243 struct control_value *name_value, int nv_number);
244void init_cgroup_table(struct cgroup *cgrps, size_t count);
245
246/*
247 * Main mounting structures
248 *
249 * cg_mount_table_lock must be held to access:
250 * cg_mount_table
251 * cg_cgroup_v2_mount_path
252 */
253extern struct cg_mount_table_s cg_mount_table[CG_CONTROLLER_MAX];
254extern char cg_cgroup_v2_mount_path[FILENAME_MAX];
255extern pthread_rwlock_t cg_mount_table_lock;
256
257/*
258 * config related structures
259 */
260extern __thread char *cg_namespace_table[CG_CONTROLLER_MAX];
261
262/*
263 * Default systemd cgroup used by the cg_build_path_locked() and tools
264 * setting the default cgroup path.
265 */
266extern char systemd_default_cgroup[FILENAME_MAX * 2 + 1];
267
268/*
269 * config related API
270 */
271int cgroup_config_insert_cgroup(char *cg_name);
272int cgroup_config_parse_controller_options(char *controller, struct cgroup_dictionary *values);
273int template_config_insert_cgroup(char *cg_name);
274int template_config_parse_controller_options(char *controller, struct cgroup_dictionary *values);
275int template_config_group_task_perm(char *perm_type, char *value);
276int template_config_group_admin_perm(char *perm_type, char *value);
277int cgroup_config_group_task_perm(char *perm_type, char *value);
278int cgroup_config_group_admin_perm(char *perm_type, char *value);
279int cgroup_config_insert_into_mount_table(char *name, char *mount_point);
280int cgroup_config_insert_into_namespace_table(char *name, char *mount_point);
281void cgroup_config_cleanup_mount_table(void);
282void cgroup_config_cleanup_namespace_table(void);
283int cgroup_config_define_default(void);
284
288extern int cgroup_dictionary_create(struct cgroup_dictionary **dict, int flags);
289
293extern int cgroup_dictionary_add(struct cgroup_dictionary *dict, const char *name,
294 const char *value);
299extern int cgroup_dictionary_free(struct cgroup_dictionary *dict);
300
305extern int cgroup_dictionary_iterator_begin(struct cgroup_dictionary *dict, void **handle,
306 const char **name, const char **value);
310extern int cgroup_dictionary_iterator_next(void **handle, const char **name, const char **value);
311
315extern void cgroup_dictionary_iterator_end(void **handle);
316
327int cg_chmod_path(const char *path, mode_t mode, int owner_is_umask);
328
338int cgroup_build_tasks_procs_path(char * const path, size_t path_sz, const char * const cg_name,
339 const char * const ctrl_name);
340
352char *cg_build_path_locked(const char *setting, char *path, const char *controller);
353
364int cgroup_fill_cgc(struct dirent *ctrl_dir, struct cgroup *cgrp, struct cgroup_controller *cgc,
365 int cg_index);
366
373int cgroup_test_subsys_mounted(const char *ctrl_name);
374
383int cgroup_copy_controller_values(struct cgroup_controller * const dst,
384 const struct cgroup_controller * const src);
385
393int cgroup_remove_value(struct cgroup_controller * const controller, const char * const name);
394
401void cgroup_free_controller(struct cgroup_controller *ctrl);
402
408#ifdef UNIT_TEST
409
410#define TEST_PROC_PID_CGROUP_FILE "test-procpidcgroup"
411
412int cgroup_parse_rules_options(char *options, struct cgroup_rule * const rule);
413int cg_get_cgroups_from_proc_cgroups(pid_t pid, char *cgrp_list[], char *controller_list[],
414 int list_len);
415bool cgroup_compare_ignore_rule(const struct cgroup_rule * const rule, pid_t pid,
416 const char * const procname);
417bool cgroup_compare_wildcard_procname(const char * const rule_procname,
418 const char * const procname);
419int cgroup_process_v1_mnt(char *controllers[], struct mntent *ent, int *mnt_tbl_idx);
420int cgroup_process_v2_mnt(struct mntent *ent, int *mnt_tbl_idx);
421int cgroup_set_values_recursive(const char * const base,
422 const struct cgroup_controller * const controller,
423 bool ignore_non_dirty_failures);
424int cgroup_chown_chmod_tasks(const char * const cg_path, uid_t uid, gid_t gid, mode_t fperm);
425int cgroupv2_subtree_control(const char *path, const char *ctrl_name, bool enable);
426int cgroupv2_get_subtree_control(const char *path, const char *ctrl_name, bool * const enabled);
427int cgroupv2_controller_enabled(const char * const cg_name, const char * const ctrl_name);
428int get_next_rule_field(char *rule, char *field, size_t field_len, bool expect_quotes);
429
430#endif /* UNIT_TEST */
431
432#ifdef __cplusplus
433} /* extern "C" */
434#endif
435
436#endif
Definition libcgroup-internal.h:136
Definition libcgroup-internal.h:141
struct cg_mount_point mount
Definition libcgroup-internal.h:147
char name[CONTROL_NAMELEN_MAX]
Definition libcgroup-internal.h:143
Definition libcgroup-internal.h:115
Definition libcgroup-internal.h:194
Definition libcgroup-internal.h:223
Definition libcgroup-internal.h:216
Definition libcgroup-internal.h:175
Definition libcgroup-internal.h:163
Definition libcgroup-internal.h:153
Definition libcgroup-internal.h:182
Definition libcgroup-internal.h:123
Definition libcgroup-internal.h:99