]>
Commit | Line | Data |
---|---|---|
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> |
6a44839f | 33 | #include "config.h" |
c797a220 | 34 | |
60bf62d4 SH |
35 | /* returns 1 on success, 0 if there were any failures */ |
36 | extern int lxc_rmdir_onedev(char *path); | |
6e4bb2e0 | 37 | extern int lxc_setup_fs(void); |
7c11d57a | 38 | extern int get_u16(unsigned short *val, const char *arg, int base); |
1b09f2c0 | 39 | extern int mkdir_p(const char *dir, mode_t mode); |
9e60f51d DE |
40 | extern const char *get_rundir(void); |
41 | ||
2a59a681 | 42 | /* |
d0386d66 CS |
43 | * Return a buffer containing the default container path. |
44 | * Caller must NOT free this buffer, since it may be static. | |
2a59a681 | 45 | */ |
d0386d66 | 46 | extern const char *lxc_global_config_value(const char *option_name); |
67e571de | 47 | extern const char *default_lxc_path(void); |
31a95fec SH |
48 | extern const char *default_zfs_root(void); |
49 | extern const char *default_lvm_vg(void); | |
307cf2a6 | 50 | |
6a44839f DE |
51 | /* Define getline() if missing from the C library */ |
52 | #ifndef HAVE_GETLINE | |
53 | #ifdef HAVE_FGETLN | |
54 | #include <../include/getline.h> | |
55 | #endif | |
56 | #endif | |
57 | ||
58 | /* Define setns() if missing from the C library */ | |
59 | #ifndef HAVE_SETNS | |
60 | static inline int setns(int fd, int nstype) | |
61 | { | |
62 | #ifdef __NR_setns | |
63 | return syscall(__NR_setns, fd, nstype); | |
64 | #else | |
65 | errno = ENOSYS; | |
66 | return -1; | |
67 | #endif | |
68 | } | |
69 | #endif | |
70 | ||
71 | /* Define unshare() if missing from the C library */ | |
72 | #ifndef HAVE_UNSHARE | |
73 | static inline int unshare(int flags) | |
74 | { | |
75 | #ifdef __NR_unshare | |
76 | return syscall(__NR_unshare, flags); | |
77 | #else | |
78 | errno = ENOSYS; | |
79 | return -1; | |
80 | #endif | |
81 | } | |
82 | #else | |
83 | int unshare(int); | |
84 | #endif | |
85 | ||
b5159817 DE |
86 | /* Define signalfd() if missing from the C library */ |
87 | #ifdef HAVE_SYS_SIGNALFD_H | |
88 | # include <sys/signalfd.h> | |
89 | #else | |
90 | /* assume kernel headers are too old */ | |
91 | #include <stdint.h> | |
92 | struct signalfd_siginfo | |
93 | { | |
94 | uint32_t ssi_signo; | |
95 | int32_t ssi_errno; | |
96 | int32_t ssi_code; | |
97 | uint32_t ssi_pid; | |
98 | uint32_t ssi_uid; | |
99 | int32_t ssi_fd; | |
100 | uint32_t ssi_tid; | |
101 | uint32_t ssi_band; | |
102 | uint32_t ssi_overrun; | |
103 | uint32_t ssi_trapno; | |
104 | int32_t ssi_status; | |
105 | int32_t ssi_int; | |
106 | uint64_t ssi_ptr; | |
107 | uint64_t ssi_utime; | |
108 | uint64_t ssi_stime; | |
109 | uint64_t ssi_addr; | |
110 | uint8_t __pad[48]; | |
111 | }; | |
112 | ||
113 | # ifndef __NR_signalfd4 | |
114 | /* assume kernel headers are too old */ | |
115 | # if __i386__ | |
116 | # define __NR_signalfd4 327 | |
117 | # elif __x86_64__ | |
118 | # define __NR_signalfd4 289 | |
119 | # elif __powerpc__ | |
120 | # define __NR_signalfd4 313 | |
121 | # elif __s390x__ | |
122 | # define __NR_signalfd4 322 | |
180edd67 SG |
123 | # elif __arm__ |
124 | # define __NR_signalfd4 355 | |
b5159817 DE |
125 | # endif |
126 | #endif | |
127 | ||
128 | # ifndef __NR_signalfd | |
129 | /* assume kernel headers are too old */ | |
130 | # if __i386__ | |
131 | # define __NR_signalfd 321 | |
132 | # elif __x86_64__ | |
133 | # define __NR_signalfd 282 | |
134 | # elif __powerpc__ | |
135 | # define __NR_signalfd 305 | |
136 | # elif __s390x__ | |
137 | # define __NR_signalfd 316 | |
180edd67 SG |
138 | # elif __arm__ |
139 | # define __NR_signalfd 349 | |
b5159817 DE |
140 | # endif |
141 | #endif | |
142 | ||
143 | static inline int signalfd(int fd, const sigset_t *mask, int flags) | |
144 | { | |
145 | int retval; | |
146 | ||
147 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
148 | if (errno == ENOSYS && flags == 0) | |
149 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
150 | return retval; | |
151 | } | |
152 | #endif | |
153 | ||
db27c8d7 CS |
154 | /* open a file with O_CLOEXEC */ |
155 | FILE *fopen_cloexec(const char *path, const char *mode); | |
156 | ||
b5159817 | 157 | |
e51d4895 DE |
158 | /** |
159 | * BUILD_BUG_ON - break compile if a condition is true. | |
160 | * @condition: the condition which the compiler should know is false. | |
161 | * | |
162 | * If you have some code which relies on certain constants being equal, or | |
163 | * other compile-time-evaluated condition, you should use BUILD_BUG_ON to | |
164 | * detect if someone changes it. | |
165 | * | |
166 | * The implementation uses gcc's reluctance to create a negative array, but | |
167 | * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments | |
168 | * to inline functions). So as a fallback we use the optimizer; if it can't | |
169 | * prove the condition is false, it will cause a link error on the undefined | |
170 | * "__build_bug_on_failed". This error message can be harder to track down | |
171 | * though, hence the two different methods. | |
172 | */ | |
173 | #ifndef __OPTIMIZE__ | |
174 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | |
175 | #else | |
176 | extern int __build_bug_on_failed; | |
177 | #define BUILD_BUG_ON(condition) \ | |
178 | do { \ | |
179 | ((void)sizeof(char[1 - 2*!!(condition)])); \ | |
180 | if (condition) __build_bug_on_failed = 1; \ | |
181 | } while(0) | |
182 | #endif | |
183 | ||
9be53773 SH |
184 | /* |
185 | * wait on a child we forked | |
186 | */ | |
187 | extern int wait_for_pid(pid_t pid); | |
c797a220 | 188 | extern int lxc_wait_for_pid_status(pid_t pid); |
9be53773 | 189 | |
92f023dc | 190 | /* send and receive buffers completely */ |
650468bb CS |
191 | extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count); |
192 | extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count); | |
193 | extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf); | |
3ce74686 SH |
194 | #if HAVE_LIBGNUTLS |
195 | #define SHA_DIGEST_LENGTH 20 | |
196 | extern int sha1sum_file(char *fnam, unsigned char *md_value); | |
197 | #endif | |
92f023dc | 198 | |
0e95426b CS |
199 | /* read and write whole files */ |
200 | extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline); | |
201 | extern int lxc_read_from_file(const char *filename, void* buf, size_t count); | |
0e95426b | 202 | |
61a1d519 CS |
203 | /* convert variadic argument lists to arrays (for execl type argument lists) */ |
204 | extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup); | |
205 | extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip); | |
206 | ||
502657d5 CS |
207 | /* Some simple string functions; if they return pointers, they are allocated buffers. */ |
208 | extern char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack); | |
209 | extern bool lxc_string_in_array(const char *needle, const char **haystack); | |
210 | extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); | |
211 | /* Normalize and split path: Leading and trailing / are removed, multiple | |
212 | * / are compactified, .. and . are resolved (.. on the top level is considered | |
213 | * identical to .). | |
214 | * Examples: | |
215 | * / -> { NULL } | |
216 | * foo/../bar -> { bar, NULL } | |
217 | * ../../ -> { NULL } | |
218 | * ./bar/baz/.. -> { bar, NULL } | |
219 | * foo//bar -> { foo, bar, NULL } | |
220 | */ | |
221 | extern char **lxc_normalize_path(const char *path); | |
24b51482 | 222 | extern char *lxc_append_paths(const char *first, const char *second); |
502657d5 CS |
223 | /* Note: the following two functions use strtok(), so they will never |
224 | * consider an empty element, even if two delimiters are next to | |
225 | * each other. | |
226 | */ | |
227 | extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep); | |
228 | extern char **lxc_string_split(const char *string, char sep); | |
229 | extern char **lxc_string_split_and_trim(const char *string, char sep); | |
230 | ||
231 | /* some simple array manipulation utilities */ | |
232 | typedef void (*lxc_free_fn)(void *); | |
233 | typedef void *(*lxc_dup_fn)(void *); | |
234 | extern int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment); | |
235 | extern void lxc_free_array(void **array, lxc_free_fn element_free_fn); | |
236 | extern size_t lxc_array_len(void **array); | |
237 | extern void **lxc_dup_array(void **array, lxc_dup_fn element_dup_fn, lxc_free_fn element_free_fn); | |
238 | ||
799f29ab | 239 | extern void **lxc_append_null_to_array(void **array, size_t count); |
307cf2a6 | 240 | #endif |