]>
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 | */ |
f1a4a029 ÇO |
23 | #ifndef __LXC_UTILS_H |
24 | #define __LXC_UTILS_H | |
0ad19a3f | 25 | |
c6868a1f CB |
26 | /* Properly support loop devices on 32bit systems. */ |
27 | #define _FILE_OFFSET_BITS 64 | |
28 | ||
a1e5280d CB |
29 | #include "config.h" |
30 | ||
6a44839f | 31 | #include <errno.h> |
61a1d519 | 32 | #include <stdarg.h> |
d0386d66 | 33 | #include <stdio.h> |
502657d5 | 34 | #include <stdbool.h> |
c6868a1f CB |
35 | #include <unistd.h> |
36 | #include <linux/loop.h> | |
ec346ea1 | 37 | #include <sys/syscall.h> |
c797a220 | 38 | #include <sys/types.h> |
f2363e38 | 39 | |
4295c5de | 40 | #include "initutils.h" |
c797a220 | 41 | |
b07511df CB |
42 | /* Useful macros */ |
43 | /* Maximum number for 64 bit integer is a string with 21 digits: 2^64 - 1 = 21 */ | |
eab15c1e CB |
44 | #define LXC_NUMSTRLEN64 21 |
45 | #define LXC_LINELEN 4096 | |
4bc3b759 | 46 | #define LXC_IDMAPLEN 4096 |
b07511df | 47 | |
60bf62d4 | 48 | /* returns 1 on success, 0 if there were any failures */ |
18aa217b | 49 | extern int lxc_rmdir_onedev(char *path, const char *exclude); |
7c11d57a | 50 | extern int get_u16(unsigned short *val, const char *arg, int base); |
1b09f2c0 | 51 | extern int mkdir_p(const char *dir, mode_t mode); |
fd8c2777 | 52 | extern char *get_rundir(void); |
9e60f51d | 53 | |
6a44839f DE |
54 | /* Define getline() if missing from the C library */ |
55 | #ifndef HAVE_GETLINE | |
56 | #ifdef HAVE_FGETLN | |
57 | #include <../include/getline.h> | |
58 | #endif | |
59 | #endif | |
60 | ||
61 | /* Define setns() if missing from the C library */ | |
62 | #ifndef HAVE_SETNS | |
63 | static inline int setns(int fd, int nstype) | |
64 | { | |
65 | #ifdef __NR_setns | |
66 | return syscall(__NR_setns, fd, nstype); | |
a1258e6d | 67 | #elif defined(__NR_set_ns) |
92e23841 | 68 | return syscall(__NR_set_ns, fd, nstype); |
6a44839f DE |
69 | #else |
70 | errno = ENOSYS; | |
71 | return -1; | |
72 | #endif | |
73 | } | |
74 | #endif | |
75 | ||
76 | /* Define unshare() if missing from the C library */ | |
77 | #ifndef HAVE_UNSHARE | |
78 | static inline int unshare(int flags) | |
79 | { | |
80 | #ifdef __NR_unshare | |
81 | return syscall(__NR_unshare, flags); | |
82 | #else | |
83 | errno = ENOSYS; | |
84 | return -1; | |
85 | #endif | |
86 | } | |
87 | #else | |
88 | int unshare(int); | |
89 | #endif | |
90 | ||
b5159817 DE |
91 | /* Define signalfd() if missing from the C library */ |
92 | #ifdef HAVE_SYS_SIGNALFD_H | |
93 | # include <sys/signalfd.h> | |
94 | #else | |
95 | /* assume kernel headers are too old */ | |
96 | #include <stdint.h> | |
97 | struct signalfd_siginfo | |
98 | { | |
99 | uint32_t ssi_signo; | |
100 | int32_t ssi_errno; | |
101 | int32_t ssi_code; | |
102 | uint32_t ssi_pid; | |
103 | uint32_t ssi_uid; | |
104 | int32_t ssi_fd; | |
105 | uint32_t ssi_tid; | |
106 | uint32_t ssi_band; | |
107 | uint32_t ssi_overrun; | |
108 | uint32_t ssi_trapno; | |
109 | int32_t ssi_status; | |
110 | int32_t ssi_int; | |
111 | uint64_t ssi_ptr; | |
112 | uint64_t ssi_utime; | |
113 | uint64_t ssi_stime; | |
114 | uint64_t ssi_addr; | |
115 | uint8_t __pad[48]; | |
116 | }; | |
117 | ||
118 | # ifndef __NR_signalfd4 | |
119 | /* assume kernel headers are too old */ | |
120 | # if __i386__ | |
121 | # define __NR_signalfd4 327 | |
122 | # elif __x86_64__ | |
123 | # define __NR_signalfd4 289 | |
124 | # elif __powerpc__ | |
125 | # define __NR_signalfd4 313 | |
126 | # elif __s390x__ | |
127 | # define __NR_signalfd4 322 | |
180edd67 SG |
128 | # elif __arm__ |
129 | # define __NR_signalfd4 355 | |
f53b5916 JC |
130 | # elif __mips__ && _MIPS_SIM == _ABIO32 |
131 | # define __NR_signalfd4 4324 | |
132 | # elif __mips__ && _MIPS_SIM == _ABI64 | |
133 | # define __NR_signalfd4 5283 | |
134 | # elif __mips__ && _MIPS_SIM == _ABIN32 | |
135 | # define __NR_signalfd4 6287 | |
b5159817 DE |
136 | # endif |
137 | #endif | |
138 | ||
139 | # ifndef __NR_signalfd | |
140 | /* assume kernel headers are too old */ | |
141 | # if __i386__ | |
142 | # define __NR_signalfd 321 | |
143 | # elif __x86_64__ | |
144 | # define __NR_signalfd 282 | |
145 | # elif __powerpc__ | |
146 | # define __NR_signalfd 305 | |
147 | # elif __s390x__ | |
148 | # define __NR_signalfd 316 | |
180edd67 SG |
149 | # elif __arm__ |
150 | # define __NR_signalfd 349 | |
f53b5916 JC |
151 | # elif __mips__ && _MIPS_SIM == _ABIO32 |
152 | # define __NR_signalfd 4317 | |
153 | # elif __mips__ && _MIPS_SIM == _ABI64 | |
154 | # define __NR_signalfd 5276 | |
155 | # elif __mips__ && _MIPS_SIM == _ABIN32 | |
156 | # define __NR_signalfd 6280 | |
b5159817 DE |
157 | # endif |
158 | #endif | |
159 | ||
160 | static inline int signalfd(int fd, const sigset_t *mask, int flags) | |
161 | { | |
162 | int retval; | |
163 | ||
164 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
165 | if (errno == ENOSYS && flags == 0) | |
166 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
167 | return retval; | |
168 | } | |
169 | #endif | |
170 | ||
c6868a1f CB |
171 | /* loop devices */ |
172 | #ifndef LO_FLAGS_AUTOCLEAR | |
173 | #define LO_FLAGS_AUTOCLEAR 4 | |
174 | #endif | |
175 | ||
176 | #ifndef LOOP_CTL_GET_FREE | |
177 | #define LOOP_CTL_GET_FREE 0x4C82 | |
178 | #endif | |
179 | ||
ebec9176 AM |
180 | /* Struct to carry child pid from lxc_popen() to lxc_pclose(). |
181 | * Not an opaque struct to allow direct access to the underlying FILE * | |
182 | * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f)) | |
183 | * without additional wrappers. | |
184 | */ | |
185 | struct lxc_popen_FILE { | |
186 | FILE *f; | |
187 | pid_t child_pid; | |
188 | }; | |
189 | ||
190 | /* popen(command, "re") replacement that restores default signal mask | |
191 | * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3). | |
192 | * In short, popen(command, "re") does pipe() + fork() + exec() | |
193 | * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec(). | |
ebec9176 AM |
194 | * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose(). |
195 | * On error returns NULL. | |
196 | */ | |
197 | extern struct lxc_popen_FILE *lxc_popen(const char *command); | |
198 | ||
199 | /* pclose() replacement to be used on struct lxc_popen_FILE *, | |
200 | * returned by lxc_popen(). | |
201 | * Waits for associated process to terminate, returns its exit status and | |
202 | * frees resources, pointed to by struct lxc_popen_FILE *. | |
ebec9176 AM |
203 | */ |
204 | extern int lxc_pclose(struct lxc_popen_FILE *fp); | |
205 | ||
e51d4895 DE |
206 | /** |
207 | * BUILD_BUG_ON - break compile if a condition is true. | |
208 | * @condition: the condition which the compiler should know is false. | |
209 | * | |
210 | * If you have some code which relies on certain constants being equal, or | |
211 | * other compile-time-evaluated condition, you should use BUILD_BUG_ON to | |
212 | * detect if someone changes it. | |
213 | * | |
214 | * The implementation uses gcc's reluctance to create a negative array, but | |
215 | * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments | |
216 | * to inline functions). So as a fallback we use the optimizer; if it can't | |
217 | * prove the condition is false, it will cause a link error on the undefined | |
218 | * "__build_bug_on_failed". This error message can be harder to track down | |
219 | * though, hence the two different methods. | |
220 | */ | |
221 | #ifndef __OPTIMIZE__ | |
222 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | |
223 | #else | |
224 | extern int __build_bug_on_failed; | |
225 | #define BUILD_BUG_ON(condition) \ | |
226 | do { \ | |
227 | ((void)sizeof(char[1 - 2*!!(condition)])); \ | |
228 | if (condition) __build_bug_on_failed = 1; \ | |
229 | } while(0) | |
230 | #endif | |
231 | ||
9be53773 SH |
232 | /* |
233 | * wait on a child we forked | |
234 | */ | |
235 | extern int wait_for_pid(pid_t pid); | |
c797a220 | 236 | extern int lxc_wait_for_pid_status(pid_t pid); |
9be53773 | 237 | |
92f023dc | 238 | /* send and receive buffers completely */ |
650468bb CS |
239 | extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count); |
240 | extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count); | |
241 | extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf); | |
3ce74686 SH |
242 | #if HAVE_LIBGNUTLS |
243 | #define SHA_DIGEST_LENGTH 20 | |
244 | extern int sha1sum_file(char *fnam, unsigned char *md_value); | |
245 | #endif | |
92f023dc | 246 | |
0e95426b CS |
247 | /* read and write whole files */ |
248 | extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline); | |
249 | extern int lxc_read_from_file(const char *filename, void* buf, size_t count); | |
0e95426b | 250 | |
61a1d519 CS |
251 | /* convert variadic argument lists to arrays (for execl type argument lists) */ |
252 | extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup); | |
253 | extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip); | |
254 | ||
502657d5 CS |
255 | /* Some simple string functions; if they return pointers, they are allocated buffers. */ |
256 | extern char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack); | |
257 | extern bool lxc_string_in_array(const char *needle, const char **haystack); | |
258 | extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); | |
259 | /* Normalize and split path: Leading and trailing / are removed, multiple | |
260 | * / are compactified, .. and . are resolved (.. on the top level is considered | |
261 | * identical to .). | |
262 | * Examples: | |
263 | * / -> { NULL } | |
264 | * foo/../bar -> { bar, NULL } | |
265 | * ../../ -> { NULL } | |
266 | * ./bar/baz/.. -> { bar, NULL } | |
267 | * foo//bar -> { foo, bar, NULL } | |
268 | */ | |
269 | extern char **lxc_normalize_path(const char *path); | |
aeb3682f | 270 | /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */ |
c56a9652 | 271 | extern bool lxc_deslashify(char **path); |
24b51482 | 272 | extern char *lxc_append_paths(const char *first, const char *second); |
502657d5 CS |
273 | /* Note: the following two functions use strtok(), so they will never |
274 | * consider an empty element, even if two delimiters are next to | |
275 | * each other. | |
276 | */ | |
277 | extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep); | |
278 | extern char **lxc_string_split(const char *string, char sep); | |
279 | extern char **lxc_string_split_and_trim(const char *string, char sep); | |
000dfda7 CB |
280 | /* Append string to NULL-terminated string array. */ |
281 | extern int lxc_append_string(char ***list, char *entry); | |
502657d5 CS |
282 | |
283 | /* some simple array manipulation utilities */ | |
284 | typedef void (*lxc_free_fn)(void *); | |
285 | typedef void *(*lxc_dup_fn)(void *); | |
286 | extern int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment); | |
287 | extern void lxc_free_array(void **array, lxc_free_fn element_free_fn); | |
288 | extern size_t lxc_array_len(void **array); | |
502657d5 | 289 | |
799f29ab | 290 | extern void **lxc_append_null_to_array(void **array, size_t count); |
1adbd020 | 291 | |
a1e5280d | 292 | /* mmap() wrapper. lxc_strmmap() will take care to \0-terminate files so that |
1adbd020 | 293 | * normal string-handling functions can be used on the buffer. */ |
25086a5f CB |
294 | extern void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd, |
295 | off_t offset); | |
a1e5280d | 296 | /* munmap() wrapper. Use it to free memory mmap()ed with lxc_strmmap(). */ |
25086a5f | 297 | extern int lxc_strmunmap(void *addr, size_t length); |
1adbd020 | 298 | |
508c263e SH |
299 | //initialize rand with urandom |
300 | extern int randseed(bool); | |
052616eb | 301 | |
1354955b SH |
302 | inline static bool am_unpriv(void) { |
303 | return geteuid() != 0; | |
304 | } | |
5d897655 SH |
305 | |
306 | /* | |
307 | * parse /proc/self/uid_map to find what @orig maps to | |
308 | */ | |
309 | extern uid_t get_ns_uid(uid_t orig); | |
c476bdce SH |
310 | |
311 | extern bool dir_exists(const char *path); | |
93c379f0 ÇO |
312 | |
313 | #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) | |
314 | uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval); | |
2c6f3fc9 SH |
315 | |
316 | int detect_shared_rootfs(void); | |
fa454c8e | 317 | bool detect_ramfs_rootfs(void); |
df6a2945 | 318 | char *on_path(const char *cmd, const char *rootfs); |
76a26f55 | 319 | bool file_exists(const char *f); |
12983ba4 | 320 | bool cgns_supported(void); |
9d9c111c | 321 | char *choose_init(const char *rootfs); |
735f2c6e | 322 | int print_to_file(const char *file, const char *content); |
51d0854c | 323 | bool switch_to_ns(pid_t pid, const char *ns); |
e1daebd9 | 324 | int is_dir(const char *path); |
6010a416 | 325 | char *get_template_path(const char *t); |
0a4be28d | 326 | int setproctitle(char *title); |
592fd47a SH |
327 | int safe_mount(const char *src, const char *dest, const char *fstype, |
328 | unsigned long flags, const void *data, const char *rootfs); | |
943144d9 | 329 | int lxc_mount_proc_if_needed(const char *rootfs); |
f8dd0275 AM |
330 | int open_devnull(void); |
331 | int set_stdfds(int fd); | |
69aeabac | 332 | int null_stdfds(void); |
ccb4cabe | 333 | int lxc_count_file_lines(const char *fn); |
a687256f | 334 | int lxc_preserve_ns(const int pid, const char *ns); |
330ae3d3 CB |
335 | |
336 | /* Check whether a signal is blocked by a process. */ | |
337 | bool task_blocking_signal(pid_t pid, int signal); | |
6bc2eafe CB |
338 | |
339 | /* Helper functions to parse numbers. */ | |
340 | int lxc_safe_uint(const char *numstr, unsigned int *converted); | |
b5f845e7 | 341 | int lxc_safe_int(const char *numstr, int *converted); |
8c57d930 | 342 | int lxc_safe_long(const char *numstr, long int *converted); |
6bc2eafe | 343 | |
dbaf55a3 CB |
344 | /* Switch to a new uid and gid. */ |
345 | int lxc_switch_uid_gid(uid_t uid, gid_t gid); | |
346 | int lxc_setgroups(int size, gid_t list[]); | |
347 | ||
c6868a1f CB |
348 | /* Find an unused loop device and associate it with source. */ |
349 | int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags); | |
350 | ||
5b72de5f | 351 | #endif /* __LXC_UTILS_H */ |