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