]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.h
plamo: Update template to use lxc.include and add plamo.common.conf
[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);
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
db27c8d7
CS
149/* open a file with O_CLOEXEC */
150FILE *fopen_cloexec(const char *path, const char *mode);
151
b5159817 152
ebec9176
AM
153/* Struct to carry child pid from lxc_popen() to lxc_pclose().
154 * Not an opaque struct to allow direct access to the underlying FILE *
155 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
156 * without additional wrappers.
157 */
158struct lxc_popen_FILE {
159 FILE *f;
160 pid_t child_pid;
161};
162
163/* popen(command, "re") replacement that restores default signal mask
164 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
165 * In short, popen(command, "re") does pipe() + fork() + exec()
166 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
ebec9176
AM
167 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
168 * On error returns NULL.
169 */
170extern struct lxc_popen_FILE *lxc_popen(const char *command);
171
172/* pclose() replacement to be used on struct lxc_popen_FILE *,
173 * returned by lxc_popen().
174 * Waits for associated process to terminate, returns its exit status and
175 * frees resources, pointed to by struct lxc_popen_FILE *.
ebec9176
AM
176 */
177extern int lxc_pclose(struct lxc_popen_FILE *fp);
178
e51d4895
DE
179/**
180 * BUILD_BUG_ON - break compile if a condition is true.
181 * @condition: the condition which the compiler should know is false.
182 *
183 * If you have some code which relies on certain constants being equal, or
184 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
185 * detect if someone changes it.
186 *
187 * The implementation uses gcc's reluctance to create a negative array, but
188 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
189 * to inline functions). So as a fallback we use the optimizer; if it can't
190 * prove the condition is false, it will cause a link error on the undefined
191 * "__build_bug_on_failed". This error message can be harder to track down
192 * though, hence the two different methods.
193 */
194#ifndef __OPTIMIZE__
195#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
196#else
197extern int __build_bug_on_failed;
198#define BUILD_BUG_ON(condition) \
199 do { \
200 ((void)sizeof(char[1 - 2*!!(condition)])); \
201 if (condition) __build_bug_on_failed = 1; \
202 } while(0)
203#endif
204
9be53773
SH
205/*
206 * wait on a child we forked
207 */
208extern int wait_for_pid(pid_t pid);
c797a220 209extern int lxc_wait_for_pid_status(pid_t pid);
9be53773 210
92f023dc 211/* send and receive buffers completely */
650468bb
CS
212extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count);
213extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count);
214extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf);
3ce74686
SH
215#if HAVE_LIBGNUTLS
216#define SHA_DIGEST_LENGTH 20
217extern int sha1sum_file(char *fnam, unsigned char *md_value);
218#endif
92f023dc 219
0e95426b
CS
220/* read and write whole files */
221extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline);
222extern int lxc_read_from_file(const char *filename, void* buf, size_t count);
0e95426b 223
61a1d519
CS
224/* convert variadic argument lists to arrays (for execl type argument lists) */
225extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
226extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
227
502657d5
CS
228/* Some simple string functions; if they return pointers, they are allocated buffers. */
229extern char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack);
230extern bool lxc_string_in_array(const char *needle, const char **haystack);
231extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix);
232/* Normalize and split path: Leading and trailing / are removed, multiple
233 * / are compactified, .. and . are resolved (.. on the top level is considered
234 * identical to .).
235 * Examples:
236 * / -> { NULL }
237 * foo/../bar -> { bar, NULL }
238 * ../../ -> { NULL }
239 * ./bar/baz/.. -> { bar, NULL }
240 * foo//bar -> { foo, bar, NULL }
241 */
242extern char **lxc_normalize_path(const char *path);
24b51482 243extern char *lxc_append_paths(const char *first, const char *second);
502657d5
CS
244/* Note: the following two functions use strtok(), so they will never
245 * consider an empty element, even if two delimiters are next to
246 * each other.
247 */
248extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep);
249extern char **lxc_string_split(const char *string, char sep);
250extern char **lxc_string_split_and_trim(const char *string, char sep);
251
252/* some simple array manipulation utilities */
253typedef void (*lxc_free_fn)(void *);
254typedef void *(*lxc_dup_fn)(void *);
255extern int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment);
256extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
257extern size_t lxc_array_len(void **array);
502657d5 258
799f29ab 259extern void **lxc_append_null_to_array(void **array, size_t count);
052616eb 260
307cf2a6 261#endif