2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
30 #include <sys/syscall.h>
31 #include <sys/types.h>
35 /* returns 1 on success, 0 if there were any failures */
36 extern int lxc_rmdir_onedev(char *path
);
37 extern int lxc_setup_fs(void);
38 extern int get_u16(unsigned short *val
, const char *arg
, int base
);
39 extern int mkdir_p(const char *dir
, mode_t mode
);
41 * Return a buffer containing the default container path.
42 * Caller must NOT free this buffer, since it may be static.
44 extern const char *lxc_global_config_value(const char *option_name
);
45 extern const char *default_lxc_path(void);
46 extern const char *default_zfs_root(void);
47 extern const char *default_lvm_vg(void);
49 /* Define getline() if missing from the C library */
52 #include <../include/getline.h>
56 /* Define setns() if missing from the C library */
58 static inline int setns(int fd
, int nstype
)
61 return syscall(__NR_setns
, fd
, nstype
);
69 /* Define unshare() if missing from the C library */
71 static inline int unshare(int flags
)
74 return syscall(__NR_unshare
, flags
);
84 /* Define signalfd() if missing from the C library */
85 #ifdef HAVE_SYS_SIGNALFD_H
86 # include <sys/signalfd.h>
88 /* assume kernel headers are too old */
90 struct signalfd_siginfo
100 uint32_t ssi_overrun
;
111 # ifndef __NR_signalfd4
112 /* assume kernel headers are too old */
114 # define __NR_signalfd4 327
116 # define __NR_signalfd4 289
118 # define __NR_signalfd4 313
120 # define __NR_signalfd4 322
122 # define __NR_signalfd4 355
126 # ifndef __NR_signalfd
127 /* assume kernel headers are too old */
129 # define __NR_signalfd 321
131 # define __NR_signalfd 282
133 # define __NR_signalfd 305
135 # define __NR_signalfd 316
137 # define __NR_signalfd 349
141 static inline int signalfd(int fd
, const sigset_t
*mask
, int flags
)
145 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
146 if (errno
== ENOSYS
&& flags
== 0)
147 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
152 /* open a file with O_CLOEXEC */
153 FILE *fopen_cloexec(const char *path
, const char *mode
);
157 * BUILD_BUG_ON - break compile if a condition is true.
158 * @condition: the condition which the compiler should know is false.
160 * If you have some code which relies on certain constants being equal, or
161 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
162 * detect if someone changes it.
164 * The implementation uses gcc's reluctance to create a negative array, but
165 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
166 * to inline functions). So as a fallback we use the optimizer; if it can't
167 * prove the condition is false, it will cause a link error on the undefined
168 * "__build_bug_on_failed". This error message can be harder to track down
169 * though, hence the two different methods.
172 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
174 extern int __build_bug_on_failed
;
175 #define BUILD_BUG_ON(condition) \
177 ((void)sizeof(char[1 - 2*!!(condition)])); \
178 if (condition) __build_bug_on_failed = 1; \
183 * wait on a child we forked
185 extern int wait_for_pid(pid_t pid
);
186 extern int lxc_wait_for_pid_status(pid_t pid
);
188 /* send and receive buffers completely */
189 extern ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
);
190 extern ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
);
191 extern ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
);
193 #define SHA_DIGEST_LENGTH 20
194 extern int sha1sum_file(char *fnam
, unsigned char *md_value
);
197 /* read and write whole files */
198 extern int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
);
199 extern int lxc_read_from_file(const char *filename
, void* buf
, size_t count
);
201 /* convert variadic argument lists to arrays (for execl type argument lists) */
202 extern char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
);
203 extern const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
);
205 /* Some simple string functions; if they return pointers, they are allocated buffers. */
206 extern char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
);
207 extern bool lxc_string_in_array(const char *needle
, const char **haystack
);
208 extern char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
);
209 /* Normalize and split path: Leading and trailing / are removed, multiple
210 * / are compactified, .. and . are resolved (.. on the top level is considered
214 * foo/../bar -> { bar, NULL }
216 * ./bar/baz/.. -> { bar, NULL }
217 * foo//bar -> { foo, bar, NULL }
219 extern char **lxc_normalize_path(const char *path
);
220 /* Note: the following two functions use strtok(), so they will never
221 * consider an empty element, even if two delimiters are next to
224 extern bool lxc_string_in_list(const char *needle
, const char *haystack
, char sep
);
225 extern char **lxc_string_split(const char *string
, char sep
);
226 extern char **lxc_string_split_and_trim(const char *string
, char sep
);
228 /* some simple array manipulation utilities */
229 typedef void (*lxc_free_fn
)(void *);
230 typedef void *(*lxc_dup_fn
)(void *);
231 extern int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
);
232 extern void lxc_free_array(void **array
, lxc_free_fn element_free_fn
);
233 extern size_t lxc_array_len(void **array
);
234 extern void **lxc_dup_array(void **array
, lxc_dup_fn element_dup_fn
, lxc_free_fn element_free_fn
);