]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
cgroups: flatten hierarchy
[mirror_lxc.git] / src / lxc / utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_UTILS_H
4 #define __LXC_UTILS_H
5
6 /* Properly support loop devices on 32bit systems. */
7 #define _FILE_OFFSET_BITS 64
8
9 #ifndef MAX_GRBUF_SIZE
10 #define MAX_GRBUF_SIZE 65536
11 #endif
12
13 #include <errno.h>
14 #include <linux/loop.h>
15 #include <linux/types.h>
16 #include <stdarg.h>
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <sys/vfs.h>
22 #include <unistd.h>
23
24 #include "file_utils.h"
25 #include "initutils.h"
26 #include "macro.h"
27 #include "raw_syscalls.h"
28 #include "string_utils.h"
29
30 /* returns 1 on success, 0 if there were any failures */
31 extern int lxc_rmdir_onedev(const char *path, const char *exclude);
32 extern int get_u16(unsigned short *val, const char *arg, int base);
33 extern int mkdir_p(const char *dir, mode_t mode);
34 extern char *get_rundir(void);
35
36 /* Define getline() if missing from the C library */
37 #ifndef HAVE_GETLINE
38 #ifdef HAVE_FGETLN
39 #include <../include/getline.h>
40 #endif
41 #endif
42
43 static inline int lxc_set_cloexec(int fd)
44 {
45 return fcntl(fd, F_SETFD, FD_CLOEXEC);
46 }
47
48 /*
49 * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
50 * struct to allow direct access to the underlying FILE without additional
51 * wrappers.
52 */
53 struct lxc_popen_FILE {
54 int pipe;
55 FILE *f;
56 pid_t child_pid;
57 };
58
59 /* popen(command, "re") replacement that restores default signal mask
60 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
61 * In short, popen(command, "re") does pipe() + fork() + exec()
62 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
63 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
64 * On error returns NULL.
65 */
66 extern struct lxc_popen_FILE *lxc_popen(const char *command);
67
68 /* pclose() replacement to be used on struct lxc_popen_FILE *,
69 * returned by lxc_popen().
70 * Waits for associated process to terminate, returns its exit status and
71 * frees resources, pointed to by struct lxc_popen_FILE *.
72 */
73 extern int lxc_pclose(struct lxc_popen_FILE *fp);
74
75 /*
76 * wait on a child we forked
77 */
78 extern int wait_for_pid(pid_t pid);
79 extern int lxc_wait_for_pid_status(pid_t pid);
80
81 #if HAVE_OPENSSL
82 extern int sha1sum_file(char *fnam, unsigned char *md_value, unsigned int *md_len);
83 #endif
84
85 /* initialize rand with urandom */
86 extern int randseed(bool);
87
88 /* are we unprivileged with respect to our namespaces */
89 inline static bool am_guest_unpriv(void) {
90 return geteuid() != 0;
91 }
92
93 /* are we unprivileged with respect to init_user_ns */
94 inline static bool am_host_unpriv(void)
95 {
96 FILE *f;
97 uid_t user, host, count;
98 int ret;
99
100 if (geteuid() != 0)
101 return true;
102
103 /* Now: are we in a user namespace? Because then we're also
104 * unprivileged.
105 */
106 f = fopen("/proc/self/uid_map", "r");
107 if (!f) {
108 return false;
109 }
110
111 ret = fscanf(f, "%u %u %u", &user, &host, &count);
112 fclose(f);
113 if (ret != 3) {
114 return false;
115 }
116
117 if (user != 0 || host != 0 || count != UINT32_MAX)
118 return true;
119 return false;
120 }
121
122 /*
123 * parse /proc/self/uid_map to find what @orig maps to
124 */
125 extern uid_t get_ns_uid(uid_t orig);
126 /*
127 * parse /proc/self/gid_map to find what @orig maps to
128 */
129 extern gid_t get_ns_gid(gid_t orig);
130
131 extern bool dir_exists(const char *path);
132
133 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
134 extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
135
136 extern bool is_shared_mountpoint(const char *path);
137 extern int detect_shared_rootfs(void);
138 extern bool detect_ramfs_rootfs(void);
139 extern char *on_path(const char *cmd, const char *rootfs);
140 extern bool cgns_supported(void);
141 extern char *choose_init(const char *rootfs);
142 extern bool switch_to_ns(pid_t pid, const char *ns);
143 extern char *get_template_path(const char *t);
144 extern int safe_mount(const char *src, const char *dest, const char *fstype,
145 unsigned long flags, const void *data,
146 const char *rootfs);
147 extern int lxc_mount_proc_if_needed(const char *rootfs);
148 extern int open_devnull(void);
149 extern int set_stdfds(int fd);
150 extern int null_stdfds(void);
151 extern int lxc_preserve_ns(const int pid, const char *ns);
152
153 /* Check whether a signal is blocked by a process. */
154 extern bool task_blocks_signal(pid_t pid, int signal);
155
156 /* Switch to a new uid and gid.
157 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
158 */
159 extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
160 extern bool lxc_setgroups(int size, gid_t list[]);
161
162 /* Find an unused loop device and associate it with source. */
163 extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
164
165 /* Clear all mounts on a given node.
166 * >= 0 successfully cleared. The number returned is the number of umounts
167 * performed.
168 * < 0 error umounting. Return -errno.
169 */
170 extern int lxc_unstack_mountpoint(const char *path, bool lazy);
171
172 /*
173 * run_command runs a command and collect it's std{err,out} output in buf.
174 *
175 * @param[out] buf The buffer where the commands std{err,out] output will be
176 * read into. If no output was produced, buf will be memset
177 * to 0.
178 * @param[in] buf_size The size of buf. This function will reserve one byte for
179 * \0-termination.
180 * @param[in] child_fn The function to be run in the child process. This
181 * function must exec.
182 * @param[in] args Arguments to be passed to child_fn.
183 */
184 extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
185 void *args);
186
187 /*
188 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
189 *
190 * @param[out] buf The buffer where the commands std{err,out] output will be
191 * read into. If no output was produced, buf will be memset
192 * to 0.
193 * @param[in] buf_size The size of buf. This function will reserve one byte for
194 * \0-termination.
195 * @param[in] child_fn The function to be run in the child process. This
196 * function must exec.
197 * @param[in] args Arguments to be passed to child_fn.
198 */
199 extern int run_command_status(char *buf, size_t buf_size, int (*child_fn)(void *),
200 void *args);
201
202 /* return copy of string @entry; do not fail. */
203 extern char *must_copy_string(const char *entry);
204
205 /* Re-allocate a pointer, do not fail */
206 extern void *must_realloc(void *orig, size_t sz);
207
208 extern bool lxc_nic_exists(char *nic);
209
210 static inline uint64_t lxc_getpagesize(void)
211 {
212 int64_t pgsz;
213
214 pgsz = sysconf(_SC_PAGESIZE);
215 if (pgsz <= 0)
216 pgsz = 1 << 12;
217
218 return pgsz;
219 }
220
221 /* If n is not a power of 2 this function will return the next power of 2
222 * greater than that number. Note that this function always returns the *next*
223 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
224 * as argument this function will return 2048 although the closest power of 2
225 * would be 1024.
226 * If the caller passes in 0 they will receive 0 in return since this is invalid
227 * input and 0 is not a power of 2.
228 */
229 extern uint64_t lxc_find_next_power2(uint64_t n);
230
231 /* Set a signal the child process will receive after the parent has died. */
232 extern int lxc_set_death_signal(int signal, pid_t parent, int parent_status_fd);
233 extern int fd_cloexec(int fd, bool cloexec);
234 extern int recursive_destroy(char *dirname);
235 extern int lxc_setup_keyring(void);
236
237 #endif /* __LXC_UTILS_H */