]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.h
lxccontainer: properly cleanup on mount injection failure
[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
b9f80409
AK
29#ifndef MAX_GRBUF_SIZE
30#define MAX_GRBUF_SIZE 65536
31#endif
32
6a44839f 33#include <errno.h>
c6868a1f 34#include <linux/loop.h>
5038d11a 35#include <linux/types.h>
d38dd64a
CB
36#include <stdarg.h>
37#include <stdbool.h>
38#include <stdio.h>
ec346ea1 39#include <sys/syscall.h>
c797a220 40#include <sys/types.h>
a035c53a 41#include <sys/vfs.h>
d38dd64a 42#include <unistd.h>
f749d524 43
37ef15bb 44#include "file_utils.h"
4295c5de 45#include "initutils.h"
279c45ee 46#include "macro.h"
d7b58715 47#include "raw_syscalls.h"
37ef15bb 48#include "string_utils.h"
b07511df 49
60bf62d4 50/* returns 1 on success, 0 if there were any failures */
41dc7155 51extern int lxc_rmdir_onedev(const char *path, const char *exclude);
7c11d57a 52extern int get_u16(unsigned short *val, const char *arg, int base);
1b09f2c0 53extern int mkdir_p(const char *dir, mode_t mode);
fd8c2777 54extern char *get_rundir(void);
9e60f51d 55
6a44839f
DE
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
b499121f
CB
63static inline int lxc_set_cloexec(int fd)
64{
65 return fcntl(fd, F_SETFD, FD_CLOEXEC);
66}
67
ebf3a6af
CB
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.
ebec9176
AM
72 */
73struct lxc_popen_FILE {
8bd8018e 74 int pipe;
ebec9176
AM
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().
ebec9176
AM
83 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
84 * On error returns NULL.
85 */
86extern 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 *.
ebec9176
AM
92 */
93extern int lxc_pclose(struct lxc_popen_FILE *fp);
94
9be53773
SH
95/*
96 * wait on a child we forked
97 */
98extern int wait_for_pid(pid_t pid);
c797a220 99extern int lxc_wait_for_pid_status(pid_t pid);
9be53773 100
fa2bb6ba
SH
101#if HAVE_OPENSSL
102extern int sha1sum_file(char *fnam, unsigned char *md_value, int *md_len);
3ce74686 103#endif
92f023dc 104
1a0e70ac 105/* initialize rand with urandom */
508c263e 106extern int randseed(bool);
052616eb 107
477aa378
SH
108/* are we unprivileged with respect to our namespaces */
109inline static bool am_guest_unpriv(void) {
110 return geteuid() != 0;
111}
112
113/* are we unprivileged with respect to init_user_ns */
5384e99d 114inline static bool am_host_unpriv(void)
4692c01a
TA
115{
116 FILE *f;
117 uid_t user, host, count;
118 int ret;
119
120 if (geteuid() != 0)
121 return true;
122
123 /* Now: are we in a user namespace? Because then we're also
124 * unprivileged.
125 */
126 f = fopen("/proc/self/uid_map", "r");
127 if (!f) {
128 return false;
129 }
130
131 ret = fscanf(f, "%u %u %u", &user, &host, &count);
132 fclose(f);
133 if (ret != 3) {
134 return false;
135 }
136
137 if (user != 0 || host != 0 || count != UINT32_MAX)
138 return true;
139 return false;
1354955b 140}
5d897655
SH
141
142/*
143 * parse /proc/self/uid_map to find what @orig maps to
144 */
145extern uid_t get_ns_uid(uid_t orig);
b962868f
CB
146/*
147 * parse /proc/self/gid_map to find what @orig maps to
148 */
149extern gid_t get_ns_gid(gid_t orig);
c476bdce
SH
150
151extern bool dir_exists(const char *path);
93c379f0
ÇO
152
153#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
1a0e70ac
CB
154extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
155
f6310f18 156extern bool is_shared_mountpoint(const char *path);
1a0e70ac
CB
157extern int detect_shared_rootfs(void);
158extern bool detect_ramfs_rootfs(void);
159extern char *on_path(const char *cmd, const char *rootfs);
1a0e70ac
CB
160extern bool cgns_supported(void);
161extern char *choose_init(const char *rootfs);
1a0e70ac 162extern bool switch_to_ns(pid_t pid, const char *ns);
1a0e70ac 163extern char *get_template_path(const char *t);
1a0e70ac
CB
164extern int safe_mount(const char *src, const char *dest, const char *fstype,
165 unsigned long flags, const void *data,
166 const char *rootfs);
167extern int lxc_mount_proc_if_needed(const char *rootfs);
168extern int open_devnull(void);
169extern int set_stdfds(int fd);
170extern int null_stdfds(void);
1a0e70ac 171extern int lxc_preserve_ns(const int pid, const char *ns);
330ae3d3
CB
172
173/* Check whether a signal is blocked by a process. */
573ad77f 174extern bool task_blocks_signal(pid_t pid, int signal);
6bc2eafe 175
db2d1af1
CB
176/* Switch to a new uid and gid.
177 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
178 */
464c4611 179extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
8af07f82 180extern bool lxc_setgroups(int size, gid_t list[]);
dbaf55a3 181
c6868a1f 182/* Find an unused loop device and associate it with source. */
1a0e70ac 183extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
c6868a1f 184
74251e49
CB
185/* Clear all mounts on a given node.
186 * >= 0 successfully cleared. The number returned is the number of umounts
187 * performed.
188 * < 0 error umounting. Return -errno.
189 */
1a0e70ac 190extern int lxc_unstack_mountpoint(const char *path, bool lazy);
74251e49 191
ea3a694f
CB
192/*
193 * run_command runs a command and collect it's std{err,out} output in buf.
194 *
195 * @param[out] buf The buffer where the commands std{err,out] output will be
196 * read into. If no output was produced, buf will be memset
197 * to 0.
198 * @param[in] buf_size The size of buf. This function will reserve one byte for
199 * \0-termination.
200 * @param[in] child_fn The function to be run in the child process. This
201 * function must exec.
202 * @param[in] args Arguments to be passed to child_fn.
203 */
1a0e70ac
CB
204extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
205 void *args);
ea3a694f 206
99a8edfc 207/*
208 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
209 *
210 * @param[out] buf The buffer where the commands std{err,out] output will be
211 * read into. If no output was produced, buf will be memset
212 * to 0.
213 * @param[in] buf_size The size of buf. This function will reserve one byte for
214 * \0-termination.
215 * @param[in] child_fn The function to be run in the child process. This
216 * function must exec.
217 * @param[in] args Arguments to be passed to child_fn.
218 */
219extern int run_command_status(char *buf, size_t buf_size, int (*child_fn)(void *),
220 void *args);
221
04ad7ffe
CB
222/* Concatenate all passed-in strings into one path. Do not fail. If any piece
223 * is not prefixed with '/', add a '/'.
224 */
eb5c2e6a 225__attribute__((sentinel)) extern char *must_concat(const char *first, ...);
0c3deb94
CB
226__attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
227__attribute__((sentinel)) extern char *must_append_path(char *first, ...);
04ad7ffe
CB
228
229/* return copy of string @entry; do not fail. */
1a0e70ac 230extern char *must_copy_string(const char *entry);
04ad7ffe 231
54d423b8 232/* Re-allocate a pointer, do not fail */
1a0e70ac 233extern void *must_realloc(void *orig, size_t sz);
04ad7ffe 234
d75c14e2 235extern bool lxc_nic_exists(char *nic);
0059379f
CB
236
237static inline uint64_t lxc_getpagesize(void)
238{
239 int64_t pgsz;
240
241 pgsz = sysconf(_SC_PAGESIZE);
242 if (pgsz <= 0)
243 pgsz = 1 << 12;
244
245 return pgsz;
246}
a035c53a 247
6222c3f4
CB
248/* If n is not a power of 2 this function will return the next power of 2
249 * greater than that number. Note that this function always returns the *next*
250 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
251 * as argument this function will return 2048 although the closest power of 2
252 * would be 1024.
253 * If the caller passes in 0 they will receive 0 in return since this is invalid
254 * input and 0 is not a power of 2.
255 */
256extern uint64_t lxc_find_next_power2(uint64_t n);
257
1fd0f41e 258/* Set a signal the child process will receive after the parent has died. */
c7f493ae 259extern int lxc_set_death_signal(int signal, pid_t parent);
a9d4ebc1 260extern int fd_cloexec(int fd, bool cloexec);
d7ab0375 261extern int recursive_destroy(char *dirname);
b25291da 262extern int lxc_setup_keyring(void);
1fd0f41e 263
5b72de5f 264#endif /* __LXC_UTILS_H */