]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.h
utils: make lxc_setgroups() return bool
[mirror_lxc.git] / src / lxc / utils.h
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
f1a4a029
ÇO
23#ifndef __LXC_UTILS_H
24#define __LXC_UTILS_H
0ad19a3f 25
c6868a1f
CB
26/* Properly support loop devices on 32bit systems. */
27#define _FILE_OFFSET_BITS 64
28
a1e5280d
CB
29#include "config.h"
30
6a44839f 31#include <errno.h>
61a1d519 32#include <stdarg.h>
d0386d66 33#include <stdio.h>
502657d5 34#include <stdbool.h>
c6868a1f
CB
35#include <unistd.h>
36#include <linux/loop.h>
5038d11a 37#include <linux/types.h>
ec346ea1 38#include <sys/syscall.h>
c797a220 39#include <sys/types.h>
a035c53a 40#include <sys/vfs.h>
f2363e38 41
f749d524
CB
42#ifdef HAVE_LINUX_MEMFD_H
43#include <linux/memfd.h>
44#endif
45
37ef15bb 46#include "file_utils.h"
4295c5de 47#include "initutils.h"
279c45ee 48#include "macro.h"
37ef15bb 49#include "string_utils.h"
b07511df 50
60bf62d4 51/* returns 1 on success, 0 if there were any failures */
41dc7155 52extern int lxc_rmdir_onedev(const char *path, const char *exclude);
7c11d57a 53extern int get_u16(unsigned short *val, const char *arg, int base);
1b09f2c0 54extern int mkdir_p(const char *dir, mode_t mode);
fd8c2777 55extern char *get_rundir(void);
9e60f51d 56
6a44839f
DE
57/* Define getline() if missing from the C library */
58#ifndef HAVE_GETLINE
59#ifdef HAVE_FGETLN
60#include <../include/getline.h>
61#endif
62#endif
63
9e320621
CB
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
77 #endif
78#endif
79
6a44839f
DE
80/* Define setns() if missing from the C library */
81#ifndef HAVE_SETNS
82static inline int setns(int fd, int nstype)
83{
84#ifdef __NR_setns
85 return syscall(__NR_setns, fd, nstype);
a1258e6d 86#elif defined(__NR_set_ns)
92e23841 87 return syscall(__NR_set_ns, fd, nstype);
6a44839f
DE
88#else
89 errno = ENOSYS;
90 return -1;
91#endif
92}
93#endif
94
ac181b5c
MPS
95/* Define sethostname() if missing from the C library */
96#ifndef HAVE_SETHOSTNAME
b14fc100 97static inline int sethostname(const char *name, size_t len)
ac181b5c
MPS
98{
99#ifdef __NR_sethostname
100return syscall(__NR_sethostname, name, len);
101#else
102errno = ENOSYS;
103return -1;
104#endif
105}
106#endif
107
6a44839f
DE
108/* Define unshare() if missing from the C library */
109#ifndef HAVE_UNSHARE
110static inline int unshare(int flags)
111{
112#ifdef __NR_unshare
113 return syscall(__NR_unshare, flags);
114#else
115 errno = ENOSYS;
116 return -1;
117#endif
118}
119#else
1a0e70ac 120extern int unshare(int);
6a44839f
DE
121#endif
122
b5159817
DE
123/* Define signalfd() if missing from the C library */
124#ifdef HAVE_SYS_SIGNALFD_H
125# include <sys/signalfd.h>
126#else
127/* assume kernel headers are too old */
128#include <stdint.h>
129struct signalfd_siginfo
130{
131 uint32_t ssi_signo;
132 int32_t ssi_errno;
133 int32_t ssi_code;
134 uint32_t ssi_pid;
135 uint32_t ssi_uid;
136 int32_t ssi_fd;
137 uint32_t ssi_tid;
138 uint32_t ssi_band;
139 uint32_t ssi_overrun;
140 uint32_t ssi_trapno;
141 int32_t ssi_status;
142 int32_t ssi_int;
143 uint64_t ssi_ptr;
144 uint64_t ssi_utime;
145 uint64_t ssi_stime;
146 uint64_t ssi_addr;
147 uint8_t __pad[48];
148};
149
150# ifndef __NR_signalfd4
151/* assume kernel headers are too old */
152# if __i386__
153# define __NR_signalfd4 327
154# elif __x86_64__
155# define __NR_signalfd4 289
156# elif __powerpc__
157# define __NR_signalfd4 313
158# elif __s390x__
159# define __NR_signalfd4 322
180edd67
SG
160# elif __arm__
161# define __NR_signalfd4 355
f53b5916
JC
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
b5159817
DE
168# endif
169#endif
170
171# ifndef __NR_signalfd
172/* assume kernel headers are too old */
173# if __i386__
174# define __NR_signalfd 321
175# elif __x86_64__
176# define __NR_signalfd 282
177# elif __powerpc__
178# define __NR_signalfd 305
179# elif __s390x__
180# define __NR_signalfd 316
180edd67
SG
181# elif __arm__
182# define __NR_signalfd 349
f53b5916
JC
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
b5159817
DE
189# endif
190#endif
191
192static inline int signalfd(int fd, const sigset_t *mask, int flags)
193{
194 int retval;
195
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);
199 return retval;
200}
201#endif
202
f749d524
CB
203#ifndef HAVE_MEMFD_CREATE
204static inline int memfd_create(const char *name, unsigned int flags) {
205 #ifndef __NR_memfd_create
206 #if defined __i386__
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
227 #endif
228 #if _MIPS_SIM == _MIPS_SIM_NABI32
229 #define __NR_memfd_create 6318
230 #endif
231 #if _MIPS_SIM == _MIPS_SIM_ABI64
232 #define __NR_memfd_create 5314
233 #endif
234 #endif
235 #endif
236 #ifdef __NR_memfd_create
237 return syscall(__NR_memfd_create, name, flags);
238 #else
239 errno = ENOSYS;
240 return -1;
241 #endif
242}
243#else
244extern int memfd_create(const char *name, unsigned int flags);
245#endif
246
b499121f
CB
247static inline int lxc_set_cloexec(int fd)
248{
249 return fcntl(fd, F_SETFD, FD_CLOEXEC);
250}
251
ebec9176
AM
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.
256 */
257struct lxc_popen_FILE {
8bd8018e 258 int pipe;
ebec9176
AM
259 FILE *f;
260 pid_t child_pid;
261};
262
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().
ebec9176
AM
267 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
268 * On error returns NULL.
269 */
270extern struct lxc_popen_FILE *lxc_popen(const char *command);
271
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 *.
ebec9176
AM
276 */
277extern int lxc_pclose(struct lxc_popen_FILE *fp);
278
9be53773
SH
279/*
280 * wait on a child we forked
281 */
282extern int wait_for_pid(pid_t pid);
c797a220 283extern int lxc_wait_for_pid_status(pid_t pid);
9be53773 284
3ce74686
SH
285#if HAVE_LIBGNUTLS
286#define SHA_DIGEST_LENGTH 20
287extern int sha1sum_file(char *fnam, unsigned char *md_value);
288#endif
92f023dc 289
1a0e70ac 290/* initialize rand with urandom */
508c263e 291extern int randseed(bool);
052616eb 292
477aa378
SH
293/* are we unprivileged with respect to our namespaces */
294inline static bool am_guest_unpriv(void) {
295 return geteuid() != 0;
296}
297
298/* are we unprivileged with respect to init_user_ns */
5384e99d 299inline static bool am_host_unpriv(void)
4692c01a
TA
300{
301 FILE *f;
302 uid_t user, host, count;
303 int ret;
304
305 if (geteuid() != 0)
306 return true;
307
308 /* Now: are we in a user namespace? Because then we're also
309 * unprivileged.
310 */
311 f = fopen("/proc/self/uid_map", "r");
312 if (!f) {
313 return false;
314 }
315
316 ret = fscanf(f, "%u %u %u", &user, &host, &count);
317 fclose(f);
318 if (ret != 3) {
319 return false;
320 }
321
322 if (user != 0 || host != 0 || count != UINT32_MAX)
323 return true;
324 return false;
1354955b 325}
5d897655
SH
326
327/*
328 * parse /proc/self/uid_map to find what @orig maps to
329 */
330extern uid_t get_ns_uid(uid_t orig);
b962868f
CB
331/*
332 * parse /proc/self/gid_map to find what @orig maps to
333 */
334extern gid_t get_ns_gid(gid_t orig);
c476bdce
SH
335
336extern bool dir_exists(const char *path);
93c379f0
ÇO
337
338#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
1a0e70ac
CB
339extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
340
f6310f18 341extern bool is_shared_mountpoint(const char *path);
1a0e70ac
CB
342extern int detect_shared_rootfs(void);
343extern bool detect_ramfs_rootfs(void);
344extern char *on_path(const char *cmd, const char *rootfs);
1a0e70ac
CB
345extern bool cgns_supported(void);
346extern char *choose_init(const char *rootfs);
1a0e70ac 347extern bool switch_to_ns(pid_t pid, const char *ns);
1a0e70ac 348extern char *get_template_path(const char *t);
1a0e70ac
CB
349extern int safe_mount(const char *src, const char *dest, const char *fstype,
350 unsigned long flags, const void *data,
351 const char *rootfs);
352extern int lxc_mount_proc_if_needed(const char *rootfs);
353extern int open_devnull(void);
354extern int set_stdfds(int fd);
355extern int null_stdfds(void);
1a0e70ac 356extern int lxc_preserve_ns(const int pid, const char *ns);
330ae3d3
CB
357
358/* Check whether a signal is blocked by a process. */
573ad77f 359extern bool task_blocks_signal(pid_t pid, int signal);
6bc2eafe 360
db2d1af1
CB
361/* Switch to a new uid and gid.
362 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
363 */
1a0e70ac 364extern int lxc_switch_uid_gid(uid_t uid, gid_t gid);
8af07f82 365extern bool lxc_setgroups(int size, gid_t list[]);
dbaf55a3 366
c6868a1f 367/* Find an unused loop device and associate it with source. */
1a0e70ac 368extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
c6868a1f 369
74251e49
CB
370/* Clear all mounts on a given node.
371 * >= 0 successfully cleared. The number returned is the number of umounts
372 * performed.
373 * < 0 error umounting. Return -errno.
374 */
1a0e70ac 375extern int lxc_unstack_mountpoint(const char *path, bool lazy);
74251e49 376
ea3a694f
CB
377/*
378 * run_command runs a command and collect it's std{err,out} output in buf.
379 *
380 * @param[out] buf The buffer where the commands std{err,out] output will be
381 * read into. If no output was produced, buf will be memset
382 * to 0.
383 * @param[in] buf_size The size of buf. This function will reserve one byte for
384 * \0-termination.
385 * @param[in] child_fn The function to be run in the child process. This
386 * function must exec.
387 * @param[in] args Arguments to be passed to child_fn.
388 */
1a0e70ac
CB
389extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
390 void *args);
ea3a694f 391
04ad7ffe
CB
392/* Concatenate all passed-in strings into one path. Do not fail. If any piece
393 * is not prefixed with '/', add a '/'.
394 */
eb5c2e6a 395__attribute__((sentinel)) extern char *must_concat(const char *first, ...);
0c3deb94
CB
396__attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
397__attribute__((sentinel)) extern char *must_append_path(char *first, ...);
04ad7ffe
CB
398
399/* return copy of string @entry; do not fail. */
1a0e70ac 400extern char *must_copy_string(const char *entry);
04ad7ffe
CB
401
402/* Re-alllocate a pointer, do not fail */
1a0e70ac 403extern void *must_realloc(void *orig, size_t sz);
04ad7ffe 404
d75c14e2 405extern bool lxc_nic_exists(char *nic);
0059379f
CB
406
407static inline uint64_t lxc_getpagesize(void)
408{
409 int64_t pgsz;
410
411 pgsz = sysconf(_SC_PAGESIZE);
412 if (pgsz <= 0)
413 pgsz = 1 << 12;
414
415 return pgsz;
416}
a035c53a 417
6222c3f4
CB
418/* If n is not a power of 2 this function will return the next power of 2
419 * greater than that number. Note that this function always returns the *next*
420 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
421 * as argument this function will return 2048 although the closest power of 2
422 * would be 1024.
423 * If the caller passes in 0 they will receive 0 in return since this is invalid
424 * input and 0 is not a power of 2.
425 */
426extern uint64_t lxc_find_next_power2(uint64_t n);
427
5288a74f
CB
428static inline pid_t lxc_raw_gettid(void)
429{
430#ifdef SYS_gettid
431 return syscall(SYS_gettid);
432#else
433 return lxc_raw_getpid();
434#endif
435}
436
1fd0f41e
CB
437/* Set a signal the child process will receive after the parent has died. */
438extern int lxc_set_death_signal(int signal);
a9d4ebc1 439extern int fd_cloexec(int fd, bool cloexec);
d7ab0375 440extern int recursive_destroy(char *dirname);
1fd0f41e 441
5b72de5f 442#endif /* __LXC_UTILS_H */