]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 /* Properly support loop devices on 32bit systems. */
7 #define _FILE_OFFSET_BITS 64
10 #define MAX_GRBUF_SIZE 65536
14 #include <linux/loop.h>
15 #include <linux/types.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
25 #include "file_utils.h"
26 #include "initutils.h"
28 #include "memory_utils.h"
29 #include "process_utils.h"
30 #include "string_utils.h"
32 /* returns 1 on success, 0 if there were any failures */
33 __hidden
extern int lxc_rmdir_onedev(const char *path
, const char *exclude
);
34 __hidden
extern int get_u16(unsigned short *val
, const char *arg
, int base
);
35 __hidden
extern int mkdir_p(const char *dir
, mode_t mode
);
36 __hidden
extern char *get_rundir(void);
38 /* Define getline() if missing from the C library */
41 #include <../include/getline.h>
45 static inline int lxc_set_cloexec(int fd
)
47 return fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
51 * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
52 * struct to allow direct access to the underlying FILE without additional
55 struct lxc_popen_FILE
{
61 /* popen(command, "re") replacement that restores default signal mask
62 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
63 * In short, popen(command, "re") does pipe() + fork() + exec()
64 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
65 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
66 * On error returns NULL.
68 __hidden
extern struct lxc_popen_FILE
*lxc_popen(const char *command
);
70 /* pclose() replacement to be used on struct lxc_popen_FILE *,
71 * returned by lxc_popen().
72 * Waits for associated process to terminate, returns its exit status and
73 * frees resources, pointed to by struct lxc_popen_FILE *.
75 __hidden
extern int lxc_pclose(struct lxc_popen_FILE
*fp
);
77 static inline void __auto_lxc_pclose__(struct lxc_popen_FILE
**f
)
82 #define __do_lxc_pclose __attribute__((__cleanup__(__auto_lxc_pclose__)))
85 * wait on a child we forked
87 __hidden
extern int wait_for_pid(pid_t pid
);
88 __hidden
extern int lxc_wait_for_pid_status(pid_t pid
);
89 __hidden
extern int wait_for_pidfd(int pidfd
);
92 __hidden
extern int sha1sum_file(char *fnam
, unsigned char *md_value
, unsigned int *md_len
);
95 /* initialize rand with urandom */
96 __hidden
extern int randseed(bool);
98 /* are we unprivileged with respect to our namespaces */
99 inline static bool am_guest_unpriv(void) {
100 return geteuid() != 0;
103 /* are we unprivileged with respect to init_user_ns */
104 inline static bool am_host_unpriv(void)
106 __do_fclose
FILE *f
= NULL
;
107 uid_t user
, host
, count
;
113 /* Now: are we in a user namespace? Because then we're also
116 f
= fopen("/proc/self/uid_map", "re");
120 ret
= fscanf(f
, "%u %u %u", &user
, &host
, &count
);
124 return user
!= 0 || host
!= 0 || count
!= UINT32_MAX
;
128 * parse /proc/self/uid_map to find what @orig maps to
130 __hidden
extern uid_t
get_ns_uid(uid_t orig
);
132 * parse /proc/self/gid_map to find what @orig maps to
134 __hidden
extern gid_t
get_ns_gid(gid_t orig
);
136 __hidden
extern bool dir_exists(const char *path
);
138 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
139 __hidden
extern uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
);
141 __hidden
extern bool is_shared_mountpoint(const char *path
);
142 __hidden
extern int detect_shared_rootfs(void);
143 __hidden
extern bool detect_ramfs_rootfs(void);
144 __hidden
extern char *on_path(const char *cmd
, const char *rootfs
);
145 __hidden
extern bool cgns_supported(void);
146 __hidden
extern char *choose_init(const char *rootfs
);
147 __hidden
extern bool switch_to_ns(pid_t pid
, const char *ns
);
148 __hidden
extern char *get_template_path(const char *t
);
149 __hidden
extern int safe_mount(const char *src
, const char *dest
, const char *fstype
,
150 unsigned long flags
, const void *data
, const char *rootfs
);
151 __hidden
extern int lxc_mount_proc_if_needed(const char *rootfs
);
152 __hidden
extern int open_devnull(void);
153 __hidden
extern int set_stdfds(int fd
);
154 __hidden
extern int null_stdfds(void);
155 __hidden
extern int lxc_preserve_ns(const int pid
, const char *ns
);
157 /* Check whether a signal is blocked by a process. */
158 __hidden
extern bool task_blocks_signal(pid_t pid
, int signal
);
160 /* Switch to a new uid and gid.
161 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
163 __hidden
extern bool lxc_switch_uid_gid(uid_t uid
, gid_t gid
);
164 __hidden
extern bool lxc_setgroups(int size
, gid_t list
[]);
166 /* Find an unused loop device and associate it with source. */
167 __hidden
extern int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
);
169 /* Clear all mounts on a given node.
170 * >= 0 successfully cleared. The number returned is the number of umounts
172 * < 0 error umounting. Return -errno.
174 __hidden
extern int lxc_unstack_mountpoint(const char *path
, bool lazy
);
177 * run_command runs a command and collect it's std{err,out} output in buf.
179 * @param[out] buf The buffer where the commands std{err,out] output will be
180 * read into. If no output was produced, buf will be memset
182 * @param[in] buf_size The size of buf. This function will reserve one byte for
184 * @param[in] child_fn The function to be run in the child process. This
185 * function must exec.
186 * @param[in] args Arguments to be passed to child_fn.
188 __hidden
extern int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
);
191 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
193 * @param[out] buf The buffer where the commands std{err,out] output will be
194 * read into. If no output was produced, buf will be memset
196 * @param[in] buf_size The size of buf. This function will reserve one byte for
198 * @param[in] child_fn The function to be run in the child process. This
199 * function must exec.
200 * @param[in] args Arguments to be passed to child_fn.
202 __hidden
extern int run_command_status(char *buf
, size_t buf_size
, int (*child_fn
)(void *),
205 __hidden
extern bool lxc_nic_exists(char *nic
);
207 static inline uint64_t lxc_getpagesize(void)
211 pgsz
= sysconf(_SC_PAGESIZE
);
218 /* If n is not a power of 2 this function will return the next power of 2
219 * greater than that number. Note that this function always returns the *next*
220 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
221 * as argument this function will return 2048 although the closest power of 2
223 * If the caller passes in 0 they will receive 0 in return since this is invalid
224 * input and 0 is not a power of 2.
226 __hidden
extern uint64_t lxc_find_next_power2(uint64_t n
);
228 /* Set a signal the child process will receive after the parent has died. */
229 __hidden
extern int lxc_set_death_signal(int signal
, pid_t parent
, int parent_status_fd
);
230 __hidden
extern int fd_cloexec(int fd
, bool cloexec
);
231 __hidden
extern int lxc_rm_rf(const char *dirname
);
232 __hidden
extern bool lxc_can_use_pidfd(int pidfd
);
234 __hidden
extern int fix_stdio_permissions(uid_t uid
);
236 static inline bool uid_valid(uid_t uid
)
238 return uid
!= LXC_INVALID_UID
;
241 static inline bool gid_valid(gid_t gid
)
243 return gid
!= LXC_INVALID_GID
;
246 __hidden
extern bool multiply_overflow(int64_t base
, uint64_t mult
, int64_t *res
);
248 __hidden
extern int safe_mount_beneath(const char *beneath
, const char *src
, const char *dst
,
249 const char *fstype
, unsigned int flags
, const void *data
);
250 __hidden
extern int safe_mount_beneath_at(int beneat_fd
, const char *src
, const char *dst
,
251 const char *fstype
, unsigned int flags
, const void *data
);
253 #endif /* __LXC_UTILS_H */