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