]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/cgroups/cgroup_utils.h
8576e5e4ec54be506871087a23f8356a6b469007
[mirror_lxcfs.git] / src / cgroups / cgroup_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_CGROUP_UTILS_H
4 #define __LXC_CGROUP_UTILS_H
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9
10 #ifndef FUSE_USE_VERSION
11 #define FUSE_USE_VERSION 26
12 #endif
13
14 #define _FILE_OFFSET_BITS 64
15
16 #include <fcntl.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mount.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/vfs.h>
26 #include <unistd.h>
27
28 #include "../config.h"
29 #include "../macro.h"
30 #include "../memory_utils.h"
31
32 /* Retrieve the cgroup version of a given entry from /proc/<pid>/mountinfo. */
33 extern int get_cgroup_version(char *line);
34
35 /* Check if given entry from /proc/<pid>/mountinfo is a cgroupfs v1 mount. */
36 extern bool is_cgroupfs_v1(char *line);
37
38 /* Check if given entry from /proc/<pid>/mountinfo is a cgroupfs v2 mount. */
39 extern bool is_cgroupfs_v2(char *line);
40
41 /* Given a v1 hierarchy @mountpoint and base @path, verify that we can create
42 * directories underneath it.
43 */
44 extern bool test_writeable_v1(char *mountpoint, char *path);
45
46 /* Given a v2 hierarchy @mountpoint and base @path, verify that we can create
47 * directories underneath it and that we have write access to the cgroup's
48 * "cgroup.procs" file.
49 */
50 extern bool test_writeable_v2(char *mountpoint, char *path);
51
52 extern int unified_cgroup_hierarchy(void);
53
54 extern void *must_realloc(void *orig, size_t sz);
55
56 extern char *must_make_path(const char *first, ...);
57
58 extern char *must_copy_string(const char *entry);
59
60 /* __typeof__ should be safe to use with all compilers. */
61 typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic;
62 extern bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val);
63
64 extern char *lxc_string_join(const char *sep, const char **parts,
65 bool use_as_prefix);
66 extern int lxc_count_file_lines(const char *fn);
67
68 extern bool dir_exists(const char *path);
69
70 extern int safe_mount(const char *src, const char *dest, const char *fstype,
71 unsigned long flags, const void *data, const char *rootfs);
72
73 #if !HAVE_STRLCPY
74 extern size_t strlcpy(char *, const char *, size_t);
75 #endif
76
77 #if !HAVE_STRLCAT
78 extern size_t strlcat(char *d, const char *s, size_t n);
79 #endif
80
81 extern FILE *fopen_cloexec(const char *path, const char *mode);
82 extern void append_line(char **dest, size_t oldlen, char *new, size_t newlen);
83 extern char *read_file(const char *fnam);
84 extern char *readat_file(int fd, const char *path);
85 extern char *read_file_strip_newline(const char *fnam);
86 extern char *cg_unified_get_current_cgroup(pid_t pid);
87 extern char *cg_hybrid_get_current_cgroup(char *basecginfo,
88 const char *controller, int type);
89 extern char *cg_legacy_get_current_cgroup(pid_t pid, const char *controller);
90 extern bool mkdir_p(const char *dir, mode_t mode);
91 extern bool is_cgroup_fd(int fd);
92
93 static inline int openat_safe(int fd, const char *path)
94 {
95 return openat(fd, path, O_DIRECTORY | O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
96 }
97
98 extern int cgroup_walkup_to_root(int cgroup2_root_fd, int hierarchy_fd,
99 const char *cgroup, const char *file,
100 char **value);
101
102 #define must_make_path_relative(__first__, ...) \
103 ({ \
104 char *__ptr__; \
105 if (*__first__ == '/') \
106 __ptr__ = must_make_path(".", __first__, __VA_ARGS__); \
107 else \
108 __ptr__ = must_make_path(__first__, __VA_ARGS__); \
109 __ptr__; \
110 })
111
112 static inline bool is_empty_string(const char *s)
113 {
114 return !s || strcmp(s, "") == 0;
115 }
116
117 #endif /* __LXC_CGROUP_UTILS_H */