]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <linux/loop.h>
8 #include <linux/types.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
19 #include "file_utils.h"
20 #include "initutils.h"
22 #include "memory_utils.h"
23 #include "process_utils.h"
24 #include "string_utils.h"
26 /* returns 1 on success, 0 if there were any failures */
27 __hidden
extern int lxc_rmdir_onedev(const char *path
, const char *exclude
);
28 __hidden
extern int get_u16(unsigned short *val
, const char *arg
, int base
);
29 __hidden
extern int lxc_mkdir_p(const char *dir
, mode_t mode
);
30 __hidden
extern char *get_rundir(void);
32 /* Define getline() if missing from the C library */
39 static inline int lxc_set_cloexec(int fd
)
41 return fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
45 * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
46 * struct to allow direct access to the underlying FILE without additional
49 struct lxc_popen_FILE
{
55 /* popen(command, "re") replacement that restores default signal mask
56 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
57 * In short, popen(command, "re") does pipe() + fork() + exec()
58 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
59 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
60 * On error returns NULL.
62 __hidden
extern struct lxc_popen_FILE
*lxc_popen(const char *command
);
64 /* pclose() replacement to be used on struct lxc_popen_FILE *,
65 * returned by lxc_popen().
66 * Waits for associated process to terminate, returns its exit status and
67 * frees resources, pointed to by struct lxc_popen_FILE *.
69 __hidden
extern int lxc_pclose(struct lxc_popen_FILE
*fp
);
71 static inline void __auto_lxc_pclose__(struct lxc_popen_FILE
**f
)
76 #define __do_lxc_pclose __attribute__((__cleanup__(__auto_lxc_pclose__)))
79 * wait on a child we forked
81 __hidden
extern int wait_for_pid(pid_t pid
);
82 __hidden
extern int lxc_wait_for_pid_status(pid_t pid
);
83 __hidden
extern int wait_for_pidfd(int pidfd
);
84 __hidden
extern bool wait_exited(pid_t pid
);
87 __hidden
extern int sha1sum_file(char *fnam
, unsigned char *md_value
, unsigned int *md_len
);
90 /* initialize rand with urandom */
91 __hidden
extern int randseed(bool);
93 /* are we unprivileged with respect to our namespaces */
94 inline static bool am_guest_unpriv(void) {
95 return geteuid() != 0;
98 /* are we unprivileged with respect to init_user_ns */
99 inline static bool am_host_unpriv(void)
101 __do_fclose
FILE *f
= NULL
;
102 uid_t user
, host
, count
;
108 /* Now: are we in a user namespace? Because then we're also
111 f
= fopen("/proc/self/uid_map", "re");
115 ret
= fscanf(f
, "%u %u %u", &user
, &host
, &count
);
119 return user
!= 0 || host
!= 0 || count
!= UINT32_MAX
;
123 * parse /proc/self/uid_map to find what @orig maps to
125 __hidden
extern uid_t
get_ns_uid(uid_t orig
);
127 * parse /proc/self/gid_map to find what @orig maps to
129 __hidden
extern gid_t
get_ns_gid(gid_t orig
);
131 __hidden
extern bool dir_exists(const char *path
);
133 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
134 __hidden
extern uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
);
136 __hidden
extern bool is_shared_mountpoint(const char *path
);
137 __hidden
extern int detect_shared_rootfs(void);
138 __hidden
extern bool detect_ramfs_rootfs(void);
139 __hidden
extern char *on_path(const char *cmd
, const char *rootfs
);
140 __hidden
extern char *choose_init(const char *rootfs
);
141 __hidden
extern bool switch_to_ns(pid_t pid
, const char *ns
);
142 __hidden
extern char *get_template_path(const char *t
);
143 __hidden
extern int safe_mount(const char *src
, const char *dest
, const char *fstype
,
144 unsigned long flags
, const void *data
, const char *rootfs
);
145 __hidden
extern int open_devnull(void);
146 __hidden
extern int set_stdfds(int fd
);
147 __hidden
extern int null_stdfds(void);
148 __hidden
extern int lxc_preserve_ns(const int pid
, const char *ns
);
150 /* Check whether a signal is blocked by a process. */
151 __hidden
extern bool task_blocks_signal(pid_t pid
, int signal
);
153 /* Switch to a new uid and gid.
154 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
156 __hidden
extern bool lxc_switch_uid_gid(uid_t uid
, gid_t gid
);
157 __hidden
extern bool lxc_setgroups(gid_t list
[], size_t size
);
158 __hidden
extern bool lxc_drop_groups(void);
160 /* Find an unused loop device and associate it with source. */
161 __hidden
extern int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
);
163 /* Clear all mounts on a given node.
164 * >= 0 successfully cleared. The number returned is the number of umounts
166 * < 0 error umounting. Return -errno.
168 __hidden
extern int lxc_unstack_mountpoint(const char *path
, bool lazy
);
171 * run_command runs a command and collect it's std{err,out} output in buf.
173 * @param[out] buf The buffer where the commands std{err,out] output will be
174 * read into. If no output was produced, buf will be memset
176 * @param[in] buf_size The size of buf. This function will reserve one byte for
178 * @param[in] child_fn The function to be run in the child process. This
179 * function must exec.
180 * @param[in] args Arguments to be passed to child_fn.
182 __hidden
extern int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
);
185 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
187 * @param[out] buf The buffer where the commands std{err,out] output will be
188 * read into. If no output was produced, buf will be memset
190 * @param[in] buf_size The size of buf. This function will reserve one byte for
192 * @param[in] child_fn The function to be run in the child process. This
193 * function must exec.
194 * @param[in] args Arguments to be passed to child_fn.
196 __hidden
extern int run_command_status(char *buf
, size_t buf_size
, int (*child_fn
)(void *),
199 __hidden
extern bool lxc_nic_exists(char *nic
);
201 static inline uint64_t lxc_getpagesize(void)
205 pgsz
= sysconf(_SC_PAGESIZE
);
212 /* If n is not a power of 2 this function will return the next power of 2
213 * greater than that number. Note that this function always returns the *next*
214 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
215 * as argument this function will return 2048 although the closest power of 2
217 * If the caller passes in 0 they will receive 0 in return since this is invalid
218 * input and 0 is not a power of 2.
220 __hidden
extern uint64_t lxc_find_next_power2(uint64_t n
);
222 /* Set a signal the child process will receive after the parent has died. */
223 __hidden
extern int lxc_set_death_signal(int signal
, pid_t parent
, int parent_status_fd
);
224 __hidden
extern int lxc_rm_rf(const char *dirname
);
225 __hidden
extern bool lxc_can_use_pidfd(int pidfd
);
227 __hidden
extern int fix_stdio_permissions(uid_t uid
);
229 static inline bool uid_valid(uid_t uid
)
231 return uid
!= LXC_INVALID_UID
;
234 static inline bool gid_valid(gid_t gid
)
236 return gid
!= LXC_INVALID_GID
;
239 __hidden
extern bool multiply_overflow(int64_t base
, uint64_t mult
, int64_t *res
);
241 __hidden
extern int safe_mount_beneath(const char *beneath
, const char *src
, const char *dst
,
242 const char *fstype
, unsigned int flags
, const void *data
);
243 __hidden
extern int safe_mount_beneath_at(int beneat_fd
, const char *src
, const char *dst
,
244 const char *fstype
, unsigned int flags
, const void *data
);
245 __hidden __lxc_unused
int print_r(int fd
, const char *path
);
247 static inline int copy_struct_from_client(__u32 server_size
, void *dst
,
248 __u32 client_size
, const void *src
)
250 __u32 size
= min(server_size
, client_size
);
251 __u32 rest
= min(server_size
, client_size
) - size
;
253 /* Deal with trailing bytes. */
254 if (client_size
< server_size
) {
255 memset(dst
+ size
, 0, rest
);
256 } else if (client_size
> server_size
) {
257 /* TODO: Actually come up with a nice way to test for 0. */
261 memcpy(dst
, src
, size
);
265 static inline __u32
copy_struct_to_client(__u32 client_size
, void *dst
,
266 __u32 server_size
, const void *src
)
268 __u32 size
= min(server_size
, client_size
);
269 memcpy(dst
, src
, size
);
273 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
274 static inline int is_in_comm(const char *s
)
276 __do_free
char *buf
= NULL
;
277 __do_free
char *comm
= NULL
;
280 buf
= file_to_buf("/proc/self/comm", &buf_size
);
287 comm
= malloc(buf_size
+ 1);
290 memcpy(comm
, buf
, buf_size
);
291 comm
[buf_size
] = '\0';
293 return strstr(comm
, s
) != NULL
;
295 #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
297 #endif /* __LXC_UTILS_H */