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