]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.h
Merge pull request #1128 from brauner/2016-08-14/fix_text_file_busy
[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
a1e5280d
CB
26#include "config.h"
27
6a44839f 28#include <errno.h>
61a1d519 29#include <stdarg.h>
d0386d66 30#include <stdio.h>
502657d5 31#include <stdbool.h>
ec346ea1 32#include <sys/syscall.h>
c797a220 33#include <sys/types.h>
9c4693b8 34#include <unistd.h>
f2363e38 35
4295c5de 36#include "initutils.h"
c797a220 37
60bf62d4 38/* returns 1 on success, 0 if there were any failures */
18aa217b 39extern int lxc_rmdir_onedev(char *path, const char *exclude);
7c11d57a 40extern int get_u16(unsigned short *val, const char *arg, int base);
1b09f2c0 41extern int mkdir_p(const char *dir, mode_t mode);
fd8c2777 42extern char *get_rundir(void);
9e60f51d 43
6a44839f
DE
44/* Define getline() if missing from the C library */
45#ifndef HAVE_GETLINE
46#ifdef HAVE_FGETLN
47#include <../include/getline.h>
48#endif
49#endif
50
51/* Define setns() if missing from the C library */
52#ifndef HAVE_SETNS
53static inline int setns(int fd, int nstype)
54{
55#ifdef __NR_setns
56 return syscall(__NR_setns, fd, nstype);
a1258e6d 57#elif defined(__NR_set_ns)
92e23841 58 return syscall(__NR_set_ns, fd, nstype);
6a44839f
DE
59#else
60 errno = ENOSYS;
61 return -1;
62#endif
63}
64#endif
65
66/* Define unshare() if missing from the C library */
67#ifndef HAVE_UNSHARE
68static inline int unshare(int flags)
69{
70#ifdef __NR_unshare
71 return syscall(__NR_unshare, flags);
72#else
73 errno = ENOSYS;
74 return -1;
75#endif
76}
77#else
78int unshare(int);
79#endif
80
b5159817
DE
81/* Define signalfd() if missing from the C library */
82#ifdef HAVE_SYS_SIGNALFD_H
83# include <sys/signalfd.h>
84#else
85/* assume kernel headers are too old */
86#include <stdint.h>
87struct signalfd_siginfo
88{
89 uint32_t ssi_signo;
90 int32_t ssi_errno;
91 int32_t ssi_code;
92 uint32_t ssi_pid;
93 uint32_t ssi_uid;
94 int32_t ssi_fd;
95 uint32_t ssi_tid;
96 uint32_t ssi_band;
97 uint32_t ssi_overrun;
98 uint32_t ssi_trapno;
99 int32_t ssi_status;
100 int32_t ssi_int;
101 uint64_t ssi_ptr;
102 uint64_t ssi_utime;
103 uint64_t ssi_stime;
104 uint64_t ssi_addr;
105 uint8_t __pad[48];
106};
107
108# ifndef __NR_signalfd4
109/* assume kernel headers are too old */
110# if __i386__
111# define __NR_signalfd4 327
112# elif __x86_64__
113# define __NR_signalfd4 289
114# elif __powerpc__
115# define __NR_signalfd4 313
116# elif __s390x__
117# define __NR_signalfd4 322
180edd67
SG
118# elif __arm__
119# define __NR_signalfd4 355
b5159817
DE
120# endif
121#endif
122
123# ifndef __NR_signalfd
124/* assume kernel headers are too old */
125# if __i386__
126# define __NR_signalfd 321
127# elif __x86_64__
128# define __NR_signalfd 282
129# elif __powerpc__
130# define __NR_signalfd 305
131# elif __s390x__
132# define __NR_signalfd 316
180edd67
SG
133# elif __arm__
134# define __NR_signalfd 349
b5159817
DE
135# endif
136#endif
137
138static inline int signalfd(int fd, const sigset_t *mask, int flags)
139{
140 int retval;
141
142 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
143 if (errno == ENOSYS && flags == 0)
144 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
145 return retval;
146}
147#endif
148
ebec9176
AM
149/* Struct to carry child pid from lxc_popen() to lxc_pclose().
150 * Not an opaque struct to allow direct access to the underlying FILE *
151 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
152 * without additional wrappers.
153 */
154struct lxc_popen_FILE {
155 FILE *f;
156 pid_t child_pid;
157};
158
159/* popen(command, "re") replacement that restores default signal mask
160 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
161 * In short, popen(command, "re") does pipe() + fork() + exec()
162 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
ebec9176
AM
163 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
164 * On error returns NULL.
165 */
166extern struct lxc_popen_FILE *lxc_popen(const char *command);
167
168/* pclose() replacement to be used on struct lxc_popen_FILE *,
169 * returned by lxc_popen().
170 * Waits for associated process to terminate, returns its exit status and
171 * frees resources, pointed to by struct lxc_popen_FILE *.
ebec9176
AM
172 */
173extern int lxc_pclose(struct lxc_popen_FILE *fp);
174
e51d4895
DE
175/**
176 * BUILD_BUG_ON - break compile if a condition is true.
177 * @condition: the condition which the compiler should know is false.
178 *
179 * If you have some code which relies on certain constants being equal, or
180 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
181 * detect if someone changes it.
182 *
183 * The implementation uses gcc's reluctance to create a negative array, but
184 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
185 * to inline functions). So as a fallback we use the optimizer; if it can't
186 * prove the condition is false, it will cause a link error on the undefined
187 * "__build_bug_on_failed". This error message can be harder to track down
188 * though, hence the two different methods.
189 */
190#ifndef __OPTIMIZE__
191#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
192#else
193extern int __build_bug_on_failed;
194#define BUILD_BUG_ON(condition) \
195 do { \
196 ((void)sizeof(char[1 - 2*!!(condition)])); \
197 if (condition) __build_bug_on_failed = 1; \
198 } while(0)
199#endif
200
9be53773
SH
201/*
202 * wait on a child we forked
203 */
204extern int wait_for_pid(pid_t pid);
c797a220 205extern int lxc_wait_for_pid_status(pid_t pid);
9be53773 206
92f023dc 207/* send and receive buffers completely */
650468bb
CS
208extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count);
209extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count);
210extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf);
3ce74686
SH
211#if HAVE_LIBGNUTLS
212#define SHA_DIGEST_LENGTH 20
213extern int sha1sum_file(char *fnam, unsigned char *md_value);
214#endif
92f023dc 215
0e95426b
CS
216/* read and write whole files */
217extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline);
218extern int lxc_read_from_file(const char *filename, void* buf, size_t count);
0e95426b 219
61a1d519
CS
220/* convert variadic argument lists to arrays (for execl type argument lists) */
221extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
222extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
223
502657d5
CS
224/* Some simple string functions; if they return pointers, they are allocated buffers. */
225extern char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack);
226extern bool lxc_string_in_array(const char *needle, const char **haystack);
227extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix);
228/* Normalize and split path: Leading and trailing / are removed, multiple
229 * / are compactified, .. and . are resolved (.. on the top level is considered
230 * identical to .).
231 * Examples:
232 * / -> { NULL }
233 * foo/../bar -> { bar, NULL }
234 * ../../ -> { NULL }
235 * ./bar/baz/.. -> { bar, NULL }
236 * foo//bar -> { foo, bar, NULL }
237 */
238extern char **lxc_normalize_path(const char *path);
24b51482 239extern char *lxc_append_paths(const char *first, const char *second);
502657d5
CS
240/* Note: the following two functions use strtok(), so they will never
241 * consider an empty element, even if two delimiters are next to
242 * each other.
243 */
244extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep);
245extern char **lxc_string_split(const char *string, char sep);
246extern char **lxc_string_split_and_trim(const char *string, char sep);
247
248/* some simple array manipulation utilities */
249typedef void (*lxc_free_fn)(void *);
250typedef void *(*lxc_dup_fn)(void *);
251extern int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment);
252extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
253extern size_t lxc_array_len(void **array);
502657d5 254
799f29ab 255extern void **lxc_append_null_to_array(void **array, size_t count);
1adbd020 256
a1e5280d 257/* mmap() wrapper. lxc_strmmap() will take care to \0-terminate files so that
1adbd020 258 * normal string-handling functions can be used on the buffer. */
25086a5f
CB
259extern void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
260 off_t offset);
a1e5280d 261/* munmap() wrapper. Use it to free memory mmap()ed with lxc_strmmap(). */
25086a5f 262extern int lxc_strmunmap(void *addr, size_t length);
1adbd020 263
508c263e
SH
264//initialize rand with urandom
265extern int randseed(bool);
052616eb 266
1354955b
SH
267inline static bool am_unpriv(void) {
268 return geteuid() != 0;
269}
5d897655
SH
270
271/*
272 * parse /proc/self/uid_map to find what @orig maps to
273 */
274extern uid_t get_ns_uid(uid_t orig);
c476bdce
SH
275
276extern bool dir_exists(const char *path);
93c379f0
ÇO
277
278#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
279uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
2c6f3fc9
SH
280
281int detect_shared_rootfs(void);
b7f954bb 282int detect_ramfs_rootfs(void);
9d9c111c 283char *on_path(char *cmd, const char *rootfs);
76a26f55 284bool file_exists(const char *f);
12983ba4 285bool cgns_supported(void);
9d9c111c 286char *choose_init(const char *rootfs);
735f2c6e 287int print_to_file(const char *file, const char *content);
51d0854c 288bool switch_to_ns(pid_t pid, const char *ns);
e1daebd9 289int is_dir(const char *path);
6010a416 290char *get_template_path(const char *t);
0a4be28d 291int setproctitle(char *title);
592fd47a
SH
292int safe_mount(const char *src, const char *dest, const char *fstype,
293 unsigned long flags, const void *data, const char *rootfs);
ced03a01 294int mount_proc_if_needed(const char *rootfs);
f8dd0275
AM
295int open_devnull(void);
296int set_stdfds(int fd);
69aeabac 297int null_stdfds(void);
ccb4cabe 298int lxc_count_file_lines(const char *fn);
330ae3d3
CB
299
300/* Check whether a signal is blocked by a process. */
301bool task_blocking_signal(pid_t pid, int signal);
5b72de5f 302#endif /* __LXC_UTILS_H */