]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
Merge pull request #2657 from ssup2/master
[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 #include <errno.h>
30 #include <linux/loop.h>
31 #include <linux/types.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <sys/syscall.h>
36 #include <sys/types.h>
37 #include <sys/vfs.h>
38 #include <unistd.h>
39
40 #include "file_utils.h"
41 #include "initutils.h"
42 #include "macro.h"
43 #include "string_utils.h"
44
45 #ifdef HAVE_LINUX_MEMFD_H
46 #include <linux/memfd.h>
47 #endif
48
49 /* returns 1 on success, 0 if there were any failures */
50 extern int lxc_rmdir_onedev(const char *path, const char *exclude);
51 extern int get_u16(unsigned short *val, const char *arg, int base);
52 extern int mkdir_p(const char *dir, mode_t mode);
53 extern char *get_rundir(void);
54
55 /* Define getline() if missing from the C library */
56 #ifndef HAVE_GETLINE
57 #ifdef HAVE_FGETLN
58 #include <../include/getline.h>
59 #endif
60 #endif
61
62 #if !defined(__NR_setns) && !defined(__NR_set_ns)
63 #if defined(__x86_64__)
64 #define __NR_setns 308
65 #elif defined(__i386__)
66 #define __NR_setns 346
67 #elif defined(__arm__)
68 #define __NR_setns 375
69 #elif defined(__aarch64__)
70 #define __NR_setns 375
71 #elif defined(__powerpc__)
72 #define __NR_setns 350
73 #elif defined(__s390__)
74 #define __NR_setns 339
75 #endif
76 #endif
77
78 /* Define setns() if missing from the C library */
79 #ifndef HAVE_SETNS
80 static inline int setns(int fd, int nstype)
81 {
82 #ifdef __NR_setns
83 return syscall(__NR_setns, fd, nstype);
84 #elif defined(__NR_set_ns)
85 return syscall(__NR_set_ns, fd, nstype);
86 #else
87 errno = ENOSYS;
88 return -1;
89 #endif
90 }
91 #endif
92
93 /* Define sethostname() if missing from the C library */
94 #ifndef HAVE_SETHOSTNAME
95 static inline int sethostname(const char *name, size_t len)
96 {
97 #ifdef __NR_sethostname
98 return syscall(__NR_sethostname, name, len);
99 #else
100 errno = ENOSYS;
101 return -1;
102 #endif
103 }
104 #endif
105
106 /* Define unshare() if missing from the C library */
107 #ifndef HAVE_UNSHARE
108 static inline int unshare(int flags)
109 {
110 #ifdef __NR_unshare
111 return syscall(__NR_unshare, flags);
112 #else
113 errno = ENOSYS;
114 return -1;
115 #endif
116 }
117 #else
118 extern int unshare(int);
119 #endif
120
121 /* Define signalfd() if missing from the C library */
122 #ifdef HAVE_SYS_SIGNALFD_H
123 # include <sys/signalfd.h>
124 #else
125 /* assume kernel headers are too old */
126 #include <stdint.h>
127 struct signalfd_siginfo
128 {
129 uint32_t ssi_signo;
130 int32_t ssi_errno;
131 int32_t ssi_code;
132 uint32_t ssi_pid;
133 uint32_t ssi_uid;
134 int32_t ssi_fd;
135 uint32_t ssi_tid;
136 uint32_t ssi_band;
137 uint32_t ssi_overrun;
138 uint32_t ssi_trapno;
139 int32_t ssi_status;
140 int32_t ssi_int;
141 uint64_t ssi_ptr;
142 uint64_t ssi_utime;
143 uint64_t ssi_stime;
144 uint64_t ssi_addr;
145 uint8_t __pad[48];
146 };
147
148 # ifndef __NR_signalfd4
149 /* assume kernel headers are too old */
150 # if __i386__
151 # define __NR_signalfd4 327
152 # elif __x86_64__
153 # define __NR_signalfd4 289
154 # elif __powerpc__
155 # define __NR_signalfd4 313
156 # elif __s390x__
157 # define __NR_signalfd4 322
158 # elif __arm__
159 # define __NR_signalfd4 355
160 # elif __mips__ && _MIPS_SIM == _ABIO32
161 # define __NR_signalfd4 4324
162 # elif __mips__ && _MIPS_SIM == _ABI64
163 # define __NR_signalfd4 5283
164 # elif __mips__ && _MIPS_SIM == _ABIN32
165 # define __NR_signalfd4 6287
166 # endif
167 #endif
168
169 # ifndef __NR_signalfd
170 /* assume kernel headers are too old */
171 # if __i386__
172 # define __NR_signalfd 321
173 # elif __x86_64__
174 # define __NR_signalfd 282
175 # elif __powerpc__
176 # define __NR_signalfd 305
177 # elif __s390x__
178 # define __NR_signalfd 316
179 # elif __arm__
180 # define __NR_signalfd 349
181 # elif __mips__ && _MIPS_SIM == _ABIO32
182 # define __NR_signalfd 4317
183 # elif __mips__ && _MIPS_SIM == _ABI64
184 # define __NR_signalfd 5276
185 # elif __mips__ && _MIPS_SIM == _ABIN32
186 # define __NR_signalfd 6280
187 # endif
188 #endif
189
190 static inline int signalfd(int fd, const sigset_t *mask, int flags)
191 {
192 int retval;
193
194 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
195 if (errno == ENOSYS && flags == 0)
196 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
197 return retval;
198 }
199 #endif
200
201 #ifndef HAVE_MEMFD_CREATE
202 static inline int memfd_create(const char *name, unsigned int flags) {
203 #ifndef __NR_memfd_create
204 #if defined __i386__
205 #define __NR_memfd_create 356
206 #elif defined __x86_64__
207 #define __NR_memfd_create 319
208 #elif defined __arm__
209 #define __NR_memfd_create 385
210 #elif defined __aarch64__
211 #define __NR_memfd_create 279
212 #elif defined __s390__
213 #define __NR_memfd_create 350
214 #elif defined __powerpc__
215 #define __NR_memfd_create 360
216 #elif defined __sparc__
217 #define __NR_memfd_create 348
218 #elif defined __blackfin__
219 #define __NR_memfd_create 390
220 #elif defined __ia64__
221 #define __NR_memfd_create 1340
222 #elif defined _MIPS_SIM
223 #if _MIPS_SIM == _MIPS_SIM_ABI32
224 #define __NR_memfd_create 4354
225 #endif
226 #if _MIPS_SIM == _MIPS_SIM_NABI32
227 #define __NR_memfd_create 6318
228 #endif
229 #if _MIPS_SIM == _MIPS_SIM_ABI64
230 #define __NR_memfd_create 5314
231 #endif
232 #endif
233 #endif
234 #ifdef __NR_memfd_create
235 return syscall(__NR_memfd_create, name, flags);
236 #else
237 errno = ENOSYS;
238 return -1;
239 #endif
240 }
241 #else
242 extern int memfd_create(const char *name, unsigned int flags);
243 #endif
244
245 static inline int lxc_set_cloexec(int fd)
246 {
247 return fcntl(fd, F_SETFD, FD_CLOEXEC);
248 }
249
250 /* Struct to carry child pid from lxc_popen() to lxc_pclose().
251 * Not an opaque struct to allow direct access to the underlying FILE *
252 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
253 * without additional wrappers.
254 */
255 struct lxc_popen_FILE {
256 int pipe;
257 FILE *f;
258 pid_t child_pid;
259 };
260
261 /* popen(command, "re") replacement that restores default signal mask
262 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
263 * In short, popen(command, "re") does pipe() + fork() + exec()
264 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
265 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
266 * On error returns NULL.
267 */
268 extern struct lxc_popen_FILE *lxc_popen(const char *command);
269
270 /* pclose() replacement to be used on struct lxc_popen_FILE *,
271 * returned by lxc_popen().
272 * Waits for associated process to terminate, returns its exit status and
273 * frees resources, pointed to by struct lxc_popen_FILE *.
274 */
275 extern int lxc_pclose(struct lxc_popen_FILE *fp);
276
277 /*
278 * wait on a child we forked
279 */
280 extern int wait_for_pid(pid_t pid);
281 extern int lxc_wait_for_pid_status(pid_t pid);
282
283 #if HAVE_LIBGNUTLS
284 #define SHA_DIGEST_LENGTH 20
285 extern int sha1sum_file(char *fnam, unsigned char *md_value);
286 #endif
287
288 /* initialize rand with urandom */
289 extern int randseed(bool);
290
291 /* are we unprivileged with respect to our namespaces */
292 inline static bool am_guest_unpriv(void) {
293 return geteuid() != 0;
294 }
295
296 /* are we unprivileged with respect to init_user_ns */
297 inline static bool am_host_unpriv(void)
298 {
299 FILE *f;
300 uid_t user, host, count;
301 int ret;
302
303 if (geteuid() != 0)
304 return true;
305
306 /* Now: are we in a user namespace? Because then we're also
307 * unprivileged.
308 */
309 f = fopen("/proc/self/uid_map", "r");
310 if (!f) {
311 return false;
312 }
313
314 ret = fscanf(f, "%u %u %u", &user, &host, &count);
315 fclose(f);
316 if (ret != 3) {
317 return false;
318 }
319
320 if (user != 0 || host != 0 || count != UINT32_MAX)
321 return true;
322 return false;
323 }
324
325 /*
326 * parse /proc/self/uid_map to find what @orig maps to
327 */
328 extern uid_t get_ns_uid(uid_t orig);
329 /*
330 * parse /proc/self/gid_map to find what @orig maps to
331 */
332 extern gid_t get_ns_gid(gid_t orig);
333
334 extern bool dir_exists(const char *path);
335
336 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
337 extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
338
339 extern bool is_shared_mountpoint(const char *path);
340 extern int detect_shared_rootfs(void);
341 extern bool detect_ramfs_rootfs(void);
342 extern char *on_path(const char *cmd, const char *rootfs);
343 extern bool cgns_supported(void);
344 extern char *choose_init(const char *rootfs);
345 extern bool switch_to_ns(pid_t pid, const char *ns);
346 extern char *get_template_path(const char *t);
347 extern int safe_mount(const char *src, const char *dest, const char *fstype,
348 unsigned long flags, const void *data,
349 const char *rootfs);
350 extern int lxc_mount_proc_if_needed(const char *rootfs);
351 extern int open_devnull(void);
352 extern int set_stdfds(int fd);
353 extern int null_stdfds(void);
354 extern int lxc_preserve_ns(const int pid, const char *ns);
355
356 /* Check whether a signal is blocked by a process. */
357 extern bool task_blocks_signal(pid_t pid, int signal);
358
359 /* Switch to a new uid and gid.
360 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
361 */
362 extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
363 extern bool lxc_setgroups(int size, gid_t list[]);
364
365 /* Find an unused loop device and associate it with source. */
366 extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
367
368 /* Clear all mounts on a given node.
369 * >= 0 successfully cleared. The number returned is the number of umounts
370 * performed.
371 * < 0 error umounting. Return -errno.
372 */
373 extern int lxc_unstack_mountpoint(const char *path, bool lazy);
374
375 /*
376 * run_command runs a command and collect it's std{err,out} output in buf.
377 *
378 * @param[out] buf The buffer where the commands std{err,out] output will be
379 * read into. If no output was produced, buf will be memset
380 * to 0.
381 * @param[in] buf_size The size of buf. This function will reserve one byte for
382 * \0-termination.
383 * @param[in] child_fn The function to be run in the child process. This
384 * function must exec.
385 * @param[in] args Arguments to be passed to child_fn.
386 */
387 extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
388 void *args);
389
390 /* Concatenate all passed-in strings into one path. Do not fail. If any piece
391 * is not prefixed with '/', add a '/'.
392 */
393 __attribute__((sentinel)) extern char *must_concat(const char *first, ...);
394 __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
395 __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
396
397 /* return copy of string @entry; do not fail. */
398 extern char *must_copy_string(const char *entry);
399
400 /* Re-alllocate a pointer, do not fail */
401 extern void *must_realloc(void *orig, size_t sz);
402
403 extern bool lxc_nic_exists(char *nic);
404
405 static inline uint64_t lxc_getpagesize(void)
406 {
407 int64_t pgsz;
408
409 pgsz = sysconf(_SC_PAGESIZE);
410 if (pgsz <= 0)
411 pgsz = 1 << 12;
412
413 return pgsz;
414 }
415
416 /* If n is not a power of 2 this function will return the next power of 2
417 * greater than that number. Note that this function always returns the *next*
418 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
419 * as argument this function will return 2048 although the closest power of 2
420 * would be 1024.
421 * If the caller passes in 0 they will receive 0 in return since this is invalid
422 * input and 0 is not a power of 2.
423 */
424 extern uint64_t lxc_find_next_power2(uint64_t n);
425
426 static inline pid_t lxc_raw_gettid(void)
427 {
428 #ifdef SYS_gettid
429 return syscall(SYS_gettid);
430 #else
431 return lxc_raw_getpid();
432 #endif
433 }
434
435 /* Set a signal the child process will receive after the parent has died. */
436 extern int lxc_set_death_signal(int signal);
437 extern int fd_cloexec(int fd, bool cloexec);
438 extern int recursive_destroy(char *dirname);
439
440 #endif /* __LXC_UTILS_H */