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