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 16 /* 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 + 3)
74
75#define CGRP_FILE_PREFIX "cgroup"
76
77/* cgroup v2 files */
78#define CGV2_CONTROLLERS_FILE "cgroup.controllers"
79#define CGV2_SUBTREE_CTRL_FILE "cgroup.subtree_control"
80
81/* maximum line length when reading the cgroup.controllers file */
82#define CGV2_CONTROLLERS_LL_MAX 100
83
84#define cgroup_err(x...) cgroup_log(CGROUP_LOG_ERROR, "Error: " x)
85#define cgroup_warn(x...) cgroup_log(CGROUP_LOG_WARNING, "Warning: " x)
86#define cgroup_info(x...) cgroup_log(CGROUP_LOG_INFO, "Info: " x)
87#define cgroup_dbg(x...) cgroup_log(CGROUP_LOG_DEBUG, x)
88#define cgroup_cont(x...) cgroup_log(CGROUP_LOG_CONT, x)
89
90#define CGRP_DEFAULT_LOGLEVEL CGROUP_LOG_ERROR
91
92#define max(x, y) ((y) < (x)?(x):(y))
93#define min(x, y) ((y) > (x)?(x):(y))
94
95#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
96
98 char name[FILENAME_MAX];
99 char value[CG_CONTROL_VALUE_MAX];
100
101 /* cgget uses this field for values that span multiple lines */
102 char *multiline_value;
103
104 /*
105 * The abstraction layer uses prev_name when there's an
106 * N->1 or 1->N relationship between cgroup v1 and v2 settings.
107 */
108 char *prev_name;
109
110 bool dirty;
111};
112
114 char name[CONTROL_NAMELEN_MAX];
115 struct control_value *values[CG_NV_MAX];
116 struct cgroup *cgroup;
117 int index;
118 enum cg_version_t version;
119};
120
121struct cgroup {
122 char name[FILENAME_MAX];
123 struct cgroup_controller *controller[CG_CONTROLLER_MAX];
124 int index;
125 uid_t tasks_uid;
126 gid_t tasks_gid;
127 mode_t task_fperm;
128 uid_t control_uid;
129 gid_t control_gid;
130 mode_t control_fperm;
131 mode_t control_dperm;
132};
133
135 char path[FILENAME_MAX];
136 struct cg_mount_point *next;
137};
138
141 char name[CONTROL_NAMELEN_MAX];
146 int index;
147 int shared_mnt;
148 enum cg_version_t version;
149};
150
152 pid_t pid; /* pid of the process which needs to change group */
153
154 /* Details of user under consideration for destination cgroup */
155 struct passwd *pw;
156 /* gid of the process */
157 gid_t gid;
158};
159
160/* A rule that maps UID/GID to a cgroup */
162 uid_t uid;
163 gid_t gid;
164 int is_ignore;
165 char *procname;
166 char username[LOGIN_NAME_MAX];
167 char destination[FILENAME_MAX];
168 char *controllers[MAX_MNT_ELEMENTS];
169 struct cgroup_rule *next;
170};
171
172/* Container for a list of rules */
174 struct cgroup_rule *head;
175 struct cgroup_rule *tail;
176 int len;
177};
178
179/* The walk_tree handle */
181 FTS *fts;
182 int flags;
183};
184
193 const char *name;
194 const char *value;
195 struct cgroup_dictionary_item *next;
196};
197
198/* Flags for cgroup_dictionary_create */
204#define CG_DICT_DONT_FREE_ITEMS 1
205
215 struct cgroup_dictionary_item *head;
216 struct cgroup_dictionary_item *tail;
217 int flags;
218};
219
222 struct cgroup_dictionary_item *item;
223};
224
228extern __thread int last_errno;
229
233extern jmp_buf parser_error_env;
234
235/* Internal API */
236char *cg_build_path(const char *name, char *path, const char *type);
237int cgroup_get_uid_gid_from_procfs(pid_t pid, uid_t *euid, gid_t *egid);
238int cgroup_get_procname_from_procfs(pid_t pid, char **procname);
239int cg_mkdir_p(const char *path);
240struct cgroup *create_cgroup_from_name_value_pairs(const char *name,
241 struct control_value *name_value, int nv_number);
242void init_cgroup_table(struct cgroup *cgrps, size_t count);
243
244/*
245 * Main mounting structures
246 *
247 * cg_mount_table_lock must be held to access:
248 * cg_mount_table
249 * cg_cgroup_v2_mount_path
250 */
251extern struct cg_mount_table_s cg_mount_table[CG_CONTROLLER_MAX];
252extern char cg_cgroup_v2_mount_path[FILENAME_MAX];
253extern pthread_rwlock_t cg_mount_table_lock;
254
255/*
256 * config related structures
257 */
258extern __thread char *cg_namespace_table[CG_CONTROLLER_MAX];
259
260/*
261 * Default systemd cgroup used by the cg_build_path_locked() and tools
262 * setting the default cgroup path.
263 */
264extern char systemd_default_cgroup[FILENAME_MAX * 2 + 1];
265
266/*
267 * config related API
268 */
269int cgroup_config_insert_cgroup(char *cg_name);
270int cgroup_config_parse_controller_options(char *controller, struct cgroup_dictionary *values);
271int template_config_insert_cgroup(char *cg_name);
272int template_config_parse_controller_options(char *controller, struct cgroup_dictionary *values);
273int template_config_group_task_perm(char *perm_type, char *value);
274int template_config_group_admin_perm(char *perm_type, char *value);
275int cgroup_config_group_task_perm(char *perm_type, char *value);
276int cgroup_config_group_admin_perm(char *perm_type, char *value);
277int cgroup_config_insert_into_mount_table(char *name, char *mount_point);
278int cgroup_config_insert_into_namespace_table(char *name, char *mount_point);
279void cgroup_config_cleanup_mount_table(void);
280void cgroup_config_cleanup_namespace_table(void);
281int cgroup_config_define_default(void);
282
286extern int cgroup_dictionary_create(struct cgroup_dictionary **dict, int flags);
287
291extern int cgroup_dictionary_add(struct cgroup_dictionary *dict, const char *name,
292 const char *value);
297extern int cgroup_dictionary_free(struct cgroup_dictionary *dict);
298
303extern int cgroup_dictionary_iterator_begin(struct cgroup_dictionary *dict, void **handle,
304 const char **name, const char **value);
308extern int cgroup_dictionary_iterator_next(void **handle, const char **name, const char **value);
309
313extern void cgroup_dictionary_iterator_end(void **handle);
314
325int cg_chmod_path(const char *path, mode_t mode, int owner_is_umask);
326
336int cgroup_build_tasks_procs_path(char * const path, size_t path_sz, const char * const cg_name,
337 const char * const ctrl_name);
338
350char *cg_build_path_locked(const char *setting, char *path, const char *controller);
351
362int cgroup_fill_cgc(struct dirent *ctrl_dir, struct cgroup *cgrp, struct cgroup_controller *cgc,
363 int cg_index);
364
371int cgroup_test_subsys_mounted(const char *ctrl_name);
372
381int cgroup_copy_controller_values(struct cgroup_controller * const dst,
382 const struct cgroup_controller * const src);
383
391int cgroup_remove_value(struct cgroup_controller * const controller, const char * const name);
392
399void cgroup_free_controller(struct cgroup_controller *ctrl);
400
406#ifdef UNIT_TEST
407
408#define TEST_PROC_PID_CGROUP_FILE "test-procpidcgroup"
409
410int cgroup_parse_rules_options(char *options, struct cgroup_rule * const rule);
411int cg_get_cgroups_from_proc_cgroups(pid_t pid, char *cgrp_list[], char *controller_list[],
412 int list_len);
413bool cgroup_compare_ignore_rule(const struct cgroup_rule * const rule, pid_t pid,
414 const char * const procname);
415bool cgroup_compare_wildcard_procname(const char * const rule_procname,
416 const char * const procname);
417int cgroup_process_v1_mnt(char *controllers[], struct mntent *ent, int *mnt_tbl_idx);
418int cgroup_process_v2_mnt(struct mntent *ent, int *mnt_tbl_idx);
419int cgroup_set_values_recursive(const char * const base,
420 const struct cgroup_controller * const controller,
421 bool ignore_non_dirty_failures);
422int cgroup_chown_chmod_tasks(const char * const cg_path, uid_t uid, gid_t gid, mode_t fperm);
423int cgroupv2_subtree_control(const char *path, const char *ctrl_name, bool enable);
424int cgroupv2_get_subtree_control(const char *path, const char *ctrl_name, bool * const enabled);
425int cgroupv2_controller_enabled(const char * const cg_name, const char * const ctrl_name);
426int get_next_rule_field(char *rule, char *field, size_t field_len, bool expect_quotes);
427
428#endif /* UNIT_TEST */
429
430#ifdef __cplusplus
431} /* extern "C" */
432#endif
433
434#endif
Definition libcgroup-internal.h:134
Definition libcgroup-internal.h:139
struct cg_mount_point mount
Definition libcgroup-internal.h:145
char name[CONTROL_NAMELEN_MAX]
Definition libcgroup-internal.h:141
Definition libcgroup-internal.h:113
Definition libcgroup-internal.h:192
Definition libcgroup-internal.h:221
Definition libcgroup-internal.h:214
Definition libcgroup-internal.h:173
Definition libcgroup-internal.h:161
Definition libcgroup-internal.h:151
Definition libcgroup-internal.h:180
Definition libcgroup-internal.h:121
Definition libcgroup-internal.h:97