]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
26 /* Properly support loop devices on 32bit systems. */
27 #define _FILE_OFFSET_BITS 64
36 #include <linux/loop.h>
37 #include <linux/types.h>
38 #include <sys/syscall.h>
39 #include <sys/types.h>
42 #ifdef HAVE_LINUX_MEMFD_H
43 #include <linux/memfd.h>
46 #include "file_utils.h"
47 #include "initutils.h"
49 #include "string_utils.h"
51 /* returns 1 on success, 0 if there were any failures */
52 extern int lxc_rmdir_onedev(const char *path
, const char *exclude
);
53 extern int get_u16(unsigned short *val
, const char *arg
, int base
);
54 extern int mkdir_p(const char *dir
, mode_t mode
);
55 extern char *get_rundir(void);
57 /* Define getline() if missing from the C library */
60 #include <../include/getline.h>
64 #if !defined(__NR_setns) && !defined(__NR_set_ns)
65 #if defined(__x86_64__)
66 #define __NR_setns 308
67 #elif defined(__i386__)
68 #define __NR_setns 346
69 #elif defined(__arm__)
70 #define __NR_setns 375
71 #elif defined(__aarch64__)
72 #define __NR_setns 375
73 #elif defined(__powerpc__)
74 #define __NR_setns 350
75 #elif defined(__s390__)
76 #define __NR_setns 339
80 /* Define setns() if missing from the C library */
82 static inline int setns(int fd
, int nstype
)
85 return syscall(__NR_setns
, fd
, nstype
);
86 #elif defined(__NR_set_ns)
87 return syscall(__NR_set_ns
, fd
, nstype
);
95 /* Define sethostname() if missing from the C library */
96 #ifndef HAVE_SETHOSTNAME
97 static inline int sethostname(const char *name
, size_t len
)
99 #ifdef __NR_sethostname
100 return syscall(__NR_sethostname
, name
, len
);
108 /* Define unshare() if missing from the C library */
110 static inline int unshare(int flags
)
113 return syscall(__NR_unshare
, flags
);
120 extern int unshare(int);
123 /* Define signalfd() if missing from the C library */
124 #ifdef HAVE_SYS_SIGNALFD_H
125 # include <sys/signalfd.h>
127 /* assume kernel headers are too old */
129 struct signalfd_siginfo
139 uint32_t ssi_overrun
;
150 # ifndef __NR_signalfd4
151 /* assume kernel headers are too old */
153 # define __NR_signalfd4 327
155 # define __NR_signalfd4 289
157 # define __NR_signalfd4 313
159 # define __NR_signalfd4 322
161 # define __NR_signalfd4 355
162 # elif __mips__ && _MIPS_SIM == _ABIO32
163 # define __NR_signalfd4 4324
164 # elif __mips__ && _MIPS_SIM == _ABI64
165 # define __NR_signalfd4 5283
166 # elif __mips__ && _MIPS_SIM == _ABIN32
167 # define __NR_signalfd4 6287
171 # ifndef __NR_signalfd
172 /* assume kernel headers are too old */
174 # define __NR_signalfd 321
176 # define __NR_signalfd 282
178 # define __NR_signalfd 305
180 # define __NR_signalfd 316
182 # define __NR_signalfd 349
183 # elif __mips__ && _MIPS_SIM == _ABIO32
184 # define __NR_signalfd 4317
185 # elif __mips__ && _MIPS_SIM == _ABI64
186 # define __NR_signalfd 5276
187 # elif __mips__ && _MIPS_SIM == _ABIN32
188 # define __NR_signalfd 6280
192 static inline int signalfd(int fd
, const sigset_t
*mask
, int flags
)
196 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
197 if (errno
== ENOSYS
&& flags
== 0)
198 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
203 #ifndef HAVE_MEMFD_CREATE
204 static inline int memfd_create(const char *name
, unsigned int flags
) {
205 #ifndef __NR_memfd_create
207 #define __NR_memfd_create 356
208 #elif defined __x86_64__
209 #define __NR_memfd_create 319
210 #elif defined __arm__
211 #define __NR_memfd_create 385
212 #elif defined __aarch64__
213 #define __NR_memfd_create 279
214 #elif defined __s390__
215 #define __NR_memfd_create 350
216 #elif defined __powerpc__
217 #define __NR_memfd_create 360
218 #elif defined __sparc__
219 #define __NR_memfd_create 348
220 #elif defined __blackfin__
221 #define __NR_memfd_create 390
222 #elif defined __ia64__
223 #define __NR_memfd_create 1340
224 #elif defined _MIPS_SIM
225 #if _MIPS_SIM == _MIPS_SIM_ABI32
226 #define __NR_memfd_create 4354
228 #if _MIPS_SIM == _MIPS_SIM_NABI32
229 #define __NR_memfd_create 6318
231 #if _MIPS_SIM == _MIPS_SIM_ABI64
232 #define __NR_memfd_create 5314
236 #ifdef __NR_memfd_create
237 return syscall(__NR_memfd_create
, name
, flags
);
244 extern int memfd_create(const char *name
, unsigned int flags
);
247 static inline int lxc_set_cloexec(int fd
)
249 return fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
252 /* Struct to carry child pid from lxc_popen() to lxc_pclose().
253 * Not an opaque struct to allow direct access to the underlying FILE *
254 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
255 * without additional wrappers.
257 struct lxc_popen_FILE
{
263 /* popen(command, "re") replacement that restores default signal mask
264 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
265 * In short, popen(command, "re") does pipe() + fork() + exec()
266 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
267 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
268 * On error returns NULL.
270 extern struct lxc_popen_FILE
*lxc_popen(const char *command
);
272 /* pclose() replacement to be used on struct lxc_popen_FILE *,
273 * returned by lxc_popen().
274 * Waits for associated process to terminate, returns its exit status and
275 * frees resources, pointed to by struct lxc_popen_FILE *.
277 extern int lxc_pclose(struct lxc_popen_FILE
*fp
);
280 * wait on a child we forked
282 extern int wait_for_pid(pid_t pid
);
283 extern int lxc_wait_for_pid_status(pid_t pid
);
286 #define SHA_DIGEST_LENGTH 20
287 extern int sha1sum_file(char *fnam
, unsigned char *md_value
);
290 /* initialize rand with urandom */
291 extern int randseed(bool);
293 /* are we unprivileged with respect to our namespaces */
294 inline static bool am_guest_unpriv(void) {
295 return geteuid() != 0;
298 /* are we unprivileged with respect to init_user_ns */
299 inline static bool am_host_unpriv(void)
302 uid_t user
, host
, count
;
308 /* Now: are we in a user namespace? Because then we're also
311 f
= fopen("/proc/self/uid_map", "r");
316 ret
= fscanf(f
, "%u %u %u", &user
, &host
, &count
);
322 if (user
!= 0 || host
!= 0 || count
!= UINT32_MAX
)
328 * parse /proc/self/uid_map to find what @orig maps to
330 extern uid_t
get_ns_uid(uid_t orig
);
332 extern bool dir_exists(const char *path
);
334 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
335 extern uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
);
337 extern bool is_shared_mountpoint(const char *path
);
338 extern int detect_shared_rootfs(void);
339 extern bool detect_ramfs_rootfs(void);
340 extern char *on_path(const char *cmd
, const char *rootfs
);
341 extern bool cgns_supported(void);
342 extern char *choose_init(const char *rootfs
);
343 extern bool switch_to_ns(pid_t pid
, const char *ns
);
344 extern char *get_template_path(const char *t
);
345 extern int safe_mount(const char *src
, const char *dest
, const char *fstype
,
346 unsigned long flags
, const void *data
,
348 extern int lxc_mount_proc_if_needed(const char *rootfs
);
349 extern int open_devnull(void);
350 extern int set_stdfds(int fd
);
351 extern int null_stdfds(void);
352 extern int lxc_preserve_ns(const int pid
, const char *ns
);
354 /* Check whether a signal is blocked by a process. */
355 extern bool task_blocks_signal(pid_t pid
, int signal
);
357 /* Switch to a new uid and gid. */
358 extern int lxc_switch_uid_gid(uid_t uid
, gid_t gid
);
359 extern int lxc_setgroups(int size
, gid_t list
[]);
361 /* Find an unused loop device and associate it with source. */
362 extern int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
);
364 /* Clear all mounts on a given node.
365 * >= 0 successfully cleared. The number returned is the number of umounts
367 * < 0 error umounting. Return -errno.
369 extern int lxc_unstack_mountpoint(const char *path
, bool lazy
);
372 * run_command runs a command and collect it's std{err,out} output in buf.
374 * @param[out] buf The buffer where the commands std{err,out] output will be
375 * read into. If no output was produced, buf will be memset
377 * @param[in] buf_size The size of buf. This function will reserve one byte for
379 * @param[in] child_fn The function to be run in the child process. This
380 * function must exec.
381 * @param[in] args Arguments to be passed to child_fn.
383 extern int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *),
386 /* Concatenate all passed-in strings into one path. Do not fail. If any piece
387 * is not prefixed with '/', add a '/'.
389 __attribute__((sentinel
)) extern char *must_concat(const char *first
, ...);
390 __attribute__((sentinel
)) extern char *must_make_path(const char *first
, ...);
391 __attribute__((sentinel
)) extern char *must_append_path(char *first
, ...);
393 /* return copy of string @entry; do not fail. */
394 extern char *must_copy_string(const char *entry
);
396 /* Re-alllocate a pointer, do not fail */
397 extern void *must_realloc(void *orig
, size_t sz
);
399 extern bool lxc_nic_exists(char *nic
);
401 static inline uint64_t lxc_getpagesize(void)
405 pgsz
= sysconf(_SC_PAGESIZE
);
412 /* If n is not a power of 2 this function will return the next power of 2
413 * greater than that number. Note that this function always returns the *next*
414 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
415 * as argument this function will return 2048 although the closest power of 2
417 * If the caller passes in 0 they will receive 0 in return since this is invalid
418 * input and 0 is not a power of 2.
420 extern uint64_t lxc_find_next_power2(uint64_t n
);
422 static inline pid_t
lxc_raw_gettid(void)
425 return syscall(SYS_gettid
);
427 return lxc_raw_getpid();
431 /* Set a signal the child process will receive after the parent has died. */
432 extern int lxc_set_death_signal(int signal
);
433 extern int fd_cloexec(int fd
, bool cloexec
);
434 extern int recursive_destroy(char *dirname
);
436 #endif /* __LXC_UTILS_H */