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
);
40 extern void remove_trailing_slashes(char *p
);
41 extern const char *get_rundir(void);
44 * Return a buffer containing the default container path.
45 * Caller must NOT free this buffer, since it may be static.
47 extern const char *lxc_global_config_value(const char *option_name
);
48 extern const char *default_lxc_path(void);
49 extern const char *default_zfs_root(void);
50 extern const char *default_lvm_vg(void);
51 extern const char *default_lvm_thin_pool(void);
52 extern const char *default_cgroup_use(void);
53 extern const char *default_cgroup_pattern(void);
54 /* Define getline() if missing from the C library */
57 #include <../include/getline.h>
61 /* Define setns() if missing from the C library */
63 static inline int setns(int fd
, int nstype
)
66 return syscall(__NR_setns
, fd
, nstype
);
74 /* Define unshare() if missing from the C library */
76 static inline int unshare(int flags
)
79 return syscall(__NR_unshare
, flags
);
89 /* Define signalfd() if missing from the C library */
90 #ifdef HAVE_SYS_SIGNALFD_H
91 # include <sys/signalfd.h>
93 /* assume kernel headers are too old */
95 struct signalfd_siginfo
105 uint32_t ssi_overrun
;
116 # ifndef __NR_signalfd4
117 /* assume kernel headers are too old */
119 # define __NR_signalfd4 327
121 # define __NR_signalfd4 289
123 # define __NR_signalfd4 313
125 # define __NR_signalfd4 322
127 # define __NR_signalfd4 355
131 # ifndef __NR_signalfd
132 /* assume kernel headers are too old */
134 # define __NR_signalfd 321
136 # define __NR_signalfd 282
138 # define __NR_signalfd 305
140 # define __NR_signalfd 316
142 # define __NR_signalfd 349
146 static inline int signalfd(int fd
, const sigset_t
*mask
, int flags
)
150 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
151 if (errno
== ENOSYS
&& flags
== 0)
152 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
157 /* open a file with O_CLOEXEC */
158 FILE *fopen_cloexec(const char *path
, const char *mode
);
162 * BUILD_BUG_ON - break compile if a condition is true.
163 * @condition: the condition which the compiler should know is false.
165 * If you have some code which relies on certain constants being equal, or
166 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
167 * detect if someone changes it.
169 * The implementation uses gcc's reluctance to create a negative array, but
170 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
171 * to inline functions). So as a fallback we use the optimizer; if it can't
172 * prove the condition is false, it will cause a link error on the undefined
173 * "__build_bug_on_failed". This error message can be harder to track down
174 * though, hence the two different methods.
177 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
179 extern int __build_bug_on_failed
;
180 #define BUILD_BUG_ON(condition) \
182 ((void)sizeof(char[1 - 2*!!(condition)])); \
183 if (condition) __build_bug_on_failed = 1; \
188 * wait on a child we forked
190 extern int wait_for_pid(pid_t pid
);
191 extern int lxc_wait_for_pid_status(pid_t pid
);
193 /* send and receive buffers completely */
194 extern ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
);
195 extern ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
);
196 extern ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
);
198 #define SHA_DIGEST_LENGTH 20
199 extern int sha1sum_file(char *fnam
, unsigned char *md_value
);
202 /* read and write whole files */
203 extern int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
);
204 extern int lxc_read_from_file(const char *filename
, void* buf
, size_t count
);
206 /* convert variadic argument lists to arrays (for execl type argument lists) */
207 extern char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
);
208 extern const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
);
210 /* Some simple string functions; if they return pointers, they are allocated buffers. */
211 extern char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
);
212 extern bool lxc_string_in_array(const char *needle
, const char **haystack
);
213 extern char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
);
214 /* Normalize and split path: Leading and trailing / are removed, multiple
215 * / are compactified, .. and . are resolved (.. on the top level is considered
219 * foo/../bar -> { bar, NULL }
221 * ./bar/baz/.. -> { bar, NULL }
222 * foo//bar -> { foo, bar, NULL }
224 extern char **lxc_normalize_path(const char *path
);
225 extern char *lxc_append_paths(const char *first
, const char *second
);
226 /* Note: the following two functions use strtok(), so they will never
227 * consider an empty element, even if two delimiters are next to
230 extern bool lxc_string_in_list(const char *needle
, const char *haystack
, char sep
);
231 extern char **lxc_string_split(const char *string
, char sep
);
232 extern char **lxc_string_split_and_trim(const char *string
, char sep
);
234 /* some simple array manipulation utilities */
235 typedef void (*lxc_free_fn
)(void *);
236 typedef void *(*lxc_dup_fn
)(void *);
237 extern int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
);
238 extern void lxc_free_array(void **array
, lxc_free_fn element_free_fn
);
239 extern size_t lxc_array_len(void **array
);
240 extern void **lxc_dup_array(void **array
, lxc_dup_fn element_dup_fn
, lxc_free_fn element_free_fn
);
242 extern void **lxc_append_null_to_array(void **array
, size_t count
);
244 extern void dump_stacktrace(void);