]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.h
fix non-root user cannot write /dev/stdout
[mirror_lxc.git] / src / lxc / utils.h
CommitLineData
cc73685d
CB
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
f1a4a029
ÇO
3#ifndef __LXC_UTILS_H
4#define __LXC_UTILS_H
0ad19a3f 5
c6868a1f
CB
6/* Properly support loop devices on 32bit systems. */
7#define _FILE_OFFSET_BITS 64
8
b9f80409
AK
9#ifndef MAX_GRBUF_SIZE
10#define MAX_GRBUF_SIZE 65536
11#endif
12
6a44839f 13#include <errno.h>
c6868a1f 14#include <linux/loop.h>
5038d11a 15#include <linux/types.h>
d38dd64a
CB
16#include <stdarg.h>
17#include <stdbool.h>
18#include <stdio.h>
ec346ea1 19#include <sys/syscall.h>
c797a220 20#include <sys/types.h>
a035c53a 21#include <sys/vfs.h>
d38dd64a 22#include <unistd.h>
f749d524 23
37ef15bb 24#include "file_utils.h"
4295c5de 25#include "initutils.h"
279c45ee 26#include "macro.h"
4110345b 27#include "memory_utils.h"
d7b58715 28#include "raw_syscalls.h"
37ef15bb 29#include "string_utils.h"
b07511df 30
60bf62d4 31/* returns 1 on success, 0 if there were any failures */
41dc7155 32extern int lxc_rmdir_onedev(const char *path, const char *exclude);
7c11d57a 33extern int get_u16(unsigned short *val, const char *arg, int base);
1b09f2c0 34extern int mkdir_p(const char *dir, mode_t mode);
fd8c2777 35extern char *get_rundir(void);
9e60f51d 36
6a44839f
DE
37/* Define getline() if missing from the C library */
38#ifndef HAVE_GETLINE
39#ifdef HAVE_FGETLN
40#include <../include/getline.h>
41#endif
42#endif
43
b499121f
CB
44static inline int lxc_set_cloexec(int fd)
45{
46 return fcntl(fd, F_SETFD, FD_CLOEXEC);
47}
48
ebf3a6af
CB
49/*
50 * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
51 * struct to allow direct access to the underlying FILE without additional
52 * wrappers.
ebec9176
AM
53 */
54struct lxc_popen_FILE {
8bd8018e 55 int pipe;
ebec9176
AM
56 FILE *f;
57 pid_t child_pid;
58};
59
60/* popen(command, "re") replacement that restores default signal mask
61 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
62 * In short, popen(command, "re") does pipe() + fork() + exec()
63 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
ebec9176
AM
64 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
65 * On error returns NULL.
66 */
67extern struct lxc_popen_FILE *lxc_popen(const char *command);
68
69/* pclose() replacement to be used on struct lxc_popen_FILE *,
70 * returned by lxc_popen().
71 * Waits for associated process to terminate, returns its exit status and
72 * frees resources, pointed to by struct lxc_popen_FILE *.
ebec9176
AM
73 */
74extern int lxc_pclose(struct lxc_popen_FILE *fp);
75
55022530
CB
76static inline void __auto_lxc_pclose__(struct lxc_popen_FILE **f)
77{
78 if (*f)
79 lxc_pclose(*f);
80}
81#define __do_lxc_pclose __attribute__((__cleanup__(__auto_lxc_pclose__)))
82
9be53773
SH
83/*
84 * wait on a child we forked
85 */
86extern int wait_for_pid(pid_t pid);
c797a220 87extern int lxc_wait_for_pid_status(pid_t pid);
39293f22 88extern int wait_for_pidfd(int pidfd);
9be53773 89
fa2bb6ba 90#if HAVE_OPENSSL
7c3d3976 91extern int sha1sum_file(char *fnam, unsigned char *md_value, unsigned int *md_len);
3ce74686 92#endif
92f023dc 93
1a0e70ac 94/* initialize rand with urandom */
508c263e 95extern int randseed(bool);
052616eb 96
477aa378
SH
97/* are we unprivileged with respect to our namespaces */
98inline static bool am_guest_unpriv(void) {
99 return geteuid() != 0;
100}
101
102/* are we unprivileged with respect to init_user_ns */
5384e99d 103inline static bool am_host_unpriv(void)
4692c01a 104{
4110345b 105 __do_fclose FILE *f = NULL;
4692c01a
TA
106 uid_t user, host, count;
107 int ret;
108
109 if (geteuid() != 0)
110 return true;
111
112 /* Now: are we in a user namespace? Because then we're also
113 * unprivileged.
114 */
4110345b
CB
115 f = fopen("/proc/self/uid_map", "re");
116 if (!f)
4692c01a 117 return false;
4692c01a
TA
118
119 ret = fscanf(f, "%u %u %u", &user, &host, &count);
4110345b 120 if (ret != 3)
4692c01a 121 return false;
4692c01a 122
4110345b 123 return user != 0 || host != 0 || count != UINT32_MAX;
1354955b 124}
5d897655
SH
125
126/*
127 * parse /proc/self/uid_map to find what @orig maps to
128 */
129extern uid_t get_ns_uid(uid_t orig);
b962868f
CB
130/*
131 * parse /proc/self/gid_map to find what @orig maps to
132 */
133extern gid_t get_ns_gid(gid_t orig);
c476bdce
SH
134
135extern bool dir_exists(const char *path);
93c379f0
ÇO
136
137#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
1a0e70ac
CB
138extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
139
f6310f18 140extern bool is_shared_mountpoint(const char *path);
1a0e70ac
CB
141extern int detect_shared_rootfs(void);
142extern bool detect_ramfs_rootfs(void);
143extern char *on_path(const char *cmd, const char *rootfs);
1a0e70ac
CB
144extern bool cgns_supported(void);
145extern char *choose_init(const char *rootfs);
1a0e70ac 146extern bool switch_to_ns(pid_t pid, const char *ns);
1a0e70ac 147extern char *get_template_path(const char *t);
1a0e70ac
CB
148extern int safe_mount(const char *src, const char *dest, const char *fstype,
149 unsigned long flags, const void *data,
150 const char *rootfs);
151extern int lxc_mount_proc_if_needed(const char *rootfs);
152extern int open_devnull(void);
153extern int set_stdfds(int fd);
154extern int null_stdfds(void);
1a0e70ac 155extern int lxc_preserve_ns(const int pid, const char *ns);
330ae3d3
CB
156
157/* Check whether a signal is blocked by a process. */
573ad77f 158extern bool task_blocks_signal(pid_t pid, int signal);
6bc2eafe 159
db2d1af1
CB
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.
162 */
464c4611 163extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
8af07f82 164extern bool lxc_setgroups(int size, gid_t list[]);
dbaf55a3 165
c6868a1f 166/* Find an unused loop device and associate it with source. */
1a0e70ac 167extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
c6868a1f 168
74251e49
CB
169/* Clear all mounts on a given node.
170 * >= 0 successfully cleared. The number returned is the number of umounts
171 * performed.
172 * < 0 error umounting. Return -errno.
173 */
1a0e70ac 174extern int lxc_unstack_mountpoint(const char *path, bool lazy);
74251e49 175
ea3a694f
CB
176/*
177 * run_command runs a command and collect it's std{err,out} output in buf.
178 *
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
181 * to 0.
182 * @param[in] buf_size The size of buf. This function will reserve one byte for
183 * \0-termination.
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.
187 */
1a0e70ac
CB
188extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
189 void *args);
ea3a694f 190
99a8edfc 191/*
192 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
193 *
194 * @param[out] buf The buffer where the commands std{err,out] output will be
195 * read into. If no output was produced, buf will be memset
196 * to 0.
197 * @param[in] buf_size The size of buf. This function will reserve one byte for
198 * \0-termination.
199 * @param[in] child_fn The function to be run in the child process. This
200 * function must exec.
201 * @param[in] args Arguments to be passed to child_fn.
202 */
203extern int run_command_status(char *buf, size_t buf_size, int (*child_fn)(void *),
204 void *args);
205
04ad7ffe 206/* return copy of string @entry; do not fail. */
1a0e70ac 207extern char *must_copy_string(const char *entry);
04ad7ffe 208
54d423b8 209/* Re-allocate a pointer, do not fail */
1a0e70ac 210extern void *must_realloc(void *orig, size_t sz);
04ad7ffe 211
d75c14e2 212extern bool lxc_nic_exists(char *nic);
0059379f
CB
213
214static inline uint64_t lxc_getpagesize(void)
215{
216 int64_t pgsz;
217
218 pgsz = sysconf(_SC_PAGESIZE);
219 if (pgsz <= 0)
220 pgsz = 1 << 12;
221
222 return pgsz;
223}
a035c53a 224
6222c3f4
CB
225/* If n is not a power of 2 this function will return the next power of 2
226 * greater than that number. Note that this function always returns the *next*
227 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
228 * as argument this function will return 2048 although the closest power of 2
229 * would be 1024.
230 * If the caller passes in 0 they will receive 0 in return since this is invalid
231 * input and 0 is not a power of 2.
232 */
233extern uint64_t lxc_find_next_power2(uint64_t n);
234
1fd0f41e 235/* Set a signal the child process will receive after the parent has died. */
4d8bdfa0 236extern int lxc_set_death_signal(int signal, pid_t parent, int parent_status_fd);
a9d4ebc1 237extern int fd_cloexec(int fd, bool cloexec);
8408a9cc 238extern int lxc_rm_rf(const char *dirname);
4fef78bc 239extern int lxc_setup_keyring(char *keyring_label);
39293f22 240extern bool lxc_can_use_pidfd(int pidfd);
1fd0f41e 241
6aff5157 242/* Fix the permissions of init PID's STDIO within the container to the specified user */
243extern void fix_stdio_permissions(uid_t uid);
244
5b72de5f 245#endif /* __LXC_UTILS_H */