]>
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 | |
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> |
f2363e38 | 33 | |
6a44839f | 34 | #include "config.h" |
4295c5de | 35 | #include "initutils.h" |
c797a220 | 36 | |
60bf62d4 | 37 | /* returns 1 on success, 0 if there were any failures */ |
18aa217b | 38 | extern int lxc_rmdir_onedev(char *path, const char *exclude); |
7c11d57a | 39 | extern int get_u16(unsigned short *val, const char *arg, int base); |
1b09f2c0 | 40 | extern int mkdir_p(const char *dir, mode_t mode); |
fd8c2777 | 41 | extern char *get_rundir(void); |
9e60f51d | 42 | |
6a44839f DE |
43 | /* Define getline() if missing from the C library */ |
44 | #ifndef HAVE_GETLINE | |
45 | #ifdef HAVE_FGETLN | |
46 | #include <../include/getline.h> | |
47 | #endif | |
48 | #endif | |
49 | ||
50 | /* Define setns() if missing from the C library */ | |
51 | #ifndef HAVE_SETNS | |
52 | static inline int setns(int fd, int nstype) | |
53 | { | |
54 | #ifdef __NR_setns | |
55 | return syscall(__NR_setns, fd, nstype); | |
a1258e6d | 56 | #elif defined(__NR_set_ns) |
92e23841 | 57 | return syscall(__NR_set_ns, fd, nstype); |
6a44839f DE |
58 | #else |
59 | errno = ENOSYS; | |
60 | return -1; | |
61 | #endif | |
62 | } | |
63 | #endif | |
64 | ||
65 | /* Define unshare() if missing from the C library */ | |
66 | #ifndef HAVE_UNSHARE | |
67 | static inline int unshare(int flags) | |
68 | { | |
69 | #ifdef __NR_unshare | |
70 | return syscall(__NR_unshare, flags); | |
71 | #else | |
72 | errno = ENOSYS; | |
73 | return -1; | |
74 | #endif | |
75 | } | |
76 | #else | |
77 | int unshare(int); | |
78 | #endif | |
79 | ||
b5159817 DE |
80 | /* Define signalfd() if missing from the C library */ |
81 | #ifdef HAVE_SYS_SIGNALFD_H | |
82 | # include <sys/signalfd.h> | |
83 | #else | |
84 | /* assume kernel headers are too old */ | |
85 | #include <stdint.h> | |
86 | struct signalfd_siginfo | |
87 | { | |
88 | uint32_t ssi_signo; | |
89 | int32_t ssi_errno; | |
90 | int32_t ssi_code; | |
91 | uint32_t ssi_pid; | |
92 | uint32_t ssi_uid; | |
93 | int32_t ssi_fd; | |
94 | uint32_t ssi_tid; | |
95 | uint32_t ssi_band; | |
96 | uint32_t ssi_overrun; | |
97 | uint32_t ssi_trapno; | |
98 | int32_t ssi_status; | |
99 | int32_t ssi_int; | |
100 | uint64_t ssi_ptr; | |
101 | uint64_t ssi_utime; | |
102 | uint64_t ssi_stime; | |
103 | uint64_t ssi_addr; | |
104 | uint8_t __pad[48]; | |
105 | }; | |
106 | ||
107 | # ifndef __NR_signalfd4 | |
108 | /* assume kernel headers are too old */ | |
109 | # if __i386__ | |
110 | # define __NR_signalfd4 327 | |
111 | # elif __x86_64__ | |
112 | # define __NR_signalfd4 289 | |
113 | # elif __powerpc__ | |
114 | # define __NR_signalfd4 313 | |
115 | # elif __s390x__ | |
116 | # define __NR_signalfd4 322 | |
180edd67 SG |
117 | # elif __arm__ |
118 | # define __NR_signalfd4 355 | |
b5159817 DE |
119 | # endif |
120 | #endif | |
121 | ||
122 | # ifndef __NR_signalfd | |
123 | /* assume kernel headers are too old */ | |
124 | # if __i386__ | |
125 | # define __NR_signalfd 321 | |
126 | # elif __x86_64__ | |
127 | # define __NR_signalfd 282 | |
128 | # elif __powerpc__ | |
129 | # define __NR_signalfd 305 | |
130 | # elif __s390x__ | |
131 | # define __NR_signalfd 316 | |
180edd67 SG |
132 | # elif __arm__ |
133 | # define __NR_signalfd 349 | |
b5159817 DE |
134 | # endif |
135 | #endif | |
136 | ||
137 | static inline int signalfd(int fd, const sigset_t *mask, int flags) | |
138 | { | |
139 | int retval; | |
140 | ||
141 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
142 | if (errno == ENOSYS && flags == 0) | |
143 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
144 | return retval; | |
145 | } | |
146 | #endif | |
147 | ||
ebec9176 AM |
148 | /* Struct to carry child pid from lxc_popen() to lxc_pclose(). |
149 | * Not an opaque struct to allow direct access to the underlying FILE * | |
150 | * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f)) | |
151 | * without additional wrappers. | |
152 | */ | |
153 | struct lxc_popen_FILE { | |
154 | FILE *f; | |
155 | pid_t child_pid; | |
156 | }; | |
157 | ||
158 | /* popen(command, "re") replacement that restores default signal mask | |
159 | * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3). | |
160 | * In short, popen(command, "re") does pipe() + fork() + exec() | |
161 | * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec(). | |
ebec9176 AM |
162 | * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose(). |
163 | * On error returns NULL. | |
164 | */ | |
165 | extern struct lxc_popen_FILE *lxc_popen(const char *command); | |
166 | ||
167 | /* pclose() replacement to be used on struct lxc_popen_FILE *, | |
168 | * returned by lxc_popen(). | |
169 | * Waits for associated process to terminate, returns its exit status and | |
170 | * frees resources, pointed to by struct lxc_popen_FILE *. | |
ebec9176 AM |
171 | */ |
172 | extern int lxc_pclose(struct lxc_popen_FILE *fp); | |
173 | ||
e51d4895 DE |
174 | /** |
175 | * BUILD_BUG_ON - break compile if a condition is true. | |
176 | * @condition: the condition which the compiler should know is false. | |
177 | * | |
178 | * If you have some code which relies on certain constants being equal, or | |
179 | * other compile-time-evaluated condition, you should use BUILD_BUG_ON to | |
180 | * detect if someone changes it. | |
181 | * | |
182 | * The implementation uses gcc's reluctance to create a negative array, but | |
183 | * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments | |
184 | * to inline functions). So as a fallback we use the optimizer; if it can't | |
185 | * prove the condition is false, it will cause a link error on the undefined | |
186 | * "__build_bug_on_failed". This error message can be harder to track down | |
187 | * though, hence the two different methods. | |
188 | */ | |
189 | #ifndef __OPTIMIZE__ | |
190 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | |
191 | #else | |
192 | extern int __build_bug_on_failed; | |
193 | #define BUILD_BUG_ON(condition) \ | |
194 | do { \ | |
195 | ((void)sizeof(char[1 - 2*!!(condition)])); \ | |
196 | if (condition) __build_bug_on_failed = 1; \ | |
197 | } while(0) | |
198 | #endif | |
199 | ||
9be53773 SH |
200 | /* |
201 | * wait on a child we forked | |
202 | */ | |
203 | extern int wait_for_pid(pid_t pid); | |
c797a220 | 204 | extern int lxc_wait_for_pid_status(pid_t pid); |
9be53773 | 205 | |
92f023dc | 206 | /* send and receive buffers completely */ |
650468bb CS |
207 | extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count); |
208 | extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count); | |
209 | extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf); | |
3ce74686 SH |
210 | #if HAVE_LIBGNUTLS |
211 | #define SHA_DIGEST_LENGTH 20 | |
212 | extern int sha1sum_file(char *fnam, unsigned char *md_value); | |
213 | #endif | |
92f023dc | 214 | |
0e95426b CS |
215 | /* read and write whole files */ |
216 | extern int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline); | |
217 | extern int lxc_read_from_file(const char *filename, void* buf, size_t count); | |
0e95426b | 218 | |
61a1d519 CS |
219 | /* convert variadic argument lists to arrays (for execl type argument lists) */ |
220 | extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup); | |
221 | extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip); | |
222 | ||
502657d5 CS |
223 | /* Some simple string functions; if they return pointers, they are allocated buffers. */ |
224 | extern char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack); | |
225 | extern bool lxc_string_in_array(const char *needle, const char **haystack); | |
226 | extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix); | |
227 | /* Normalize and split path: Leading and trailing / are removed, multiple | |
228 | * / are compactified, .. and . are resolved (.. on the top level is considered | |
229 | * identical to .). | |
230 | * Examples: | |
231 | * / -> { NULL } | |
232 | * foo/../bar -> { bar, NULL } | |
233 | * ../../ -> { NULL } | |
234 | * ./bar/baz/.. -> { bar, NULL } | |
235 | * foo//bar -> { foo, bar, NULL } | |
236 | */ | |
237 | extern char **lxc_normalize_path(const char *path); | |
24b51482 | 238 | extern char *lxc_append_paths(const char *first, const char *second); |
502657d5 CS |
239 | /* Note: the following two functions use strtok(), so they will never |
240 | * consider an empty element, even if two delimiters are next to | |
241 | * each other. | |
242 | */ | |
243 | extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep); | |
244 | extern char **lxc_string_split(const char *string, char sep); | |
245 | extern char **lxc_string_split_and_trim(const char *string, char sep); | |
246 | ||
247 | /* some simple array manipulation utilities */ | |
248 | typedef void (*lxc_free_fn)(void *); | |
249 | typedef void *(*lxc_dup_fn)(void *); | |
250 | extern int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment); | |
251 | extern void lxc_free_array(void **array, lxc_free_fn element_free_fn); | |
252 | extern size_t lxc_array_len(void **array); | |
502657d5 | 253 | |
799f29ab | 254 | extern void **lxc_append_null_to_array(void **array, size_t count); |
508c263e SH |
255 | //initialize rand with urandom |
256 | extern int randseed(bool); | |
052616eb | 257 | |
1354955b SH |
258 | inline static bool am_unpriv(void) { |
259 | return geteuid() != 0; | |
260 | } | |
5d897655 SH |
261 | |
262 | /* | |
263 | * parse /proc/self/uid_map to find what @orig maps to | |
264 | */ | |
265 | extern uid_t get_ns_uid(uid_t orig); | |
c476bdce SH |
266 | |
267 | extern bool dir_exists(const char *path); | |
93c379f0 ÇO |
268 | |
269 | #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL) | |
270 | uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval); | |
2c6f3fc9 SH |
271 | |
272 | int detect_shared_rootfs(void); | |
b7f954bb | 273 | int detect_ramfs_rootfs(void); |
9d9c111c | 274 | char *on_path(char *cmd, const char *rootfs); |
76a26f55 | 275 | bool file_exists(const char *f); |
9d9c111c | 276 | char *choose_init(const char *rootfs); |
735f2c6e | 277 | int print_to_file(const char *file, const char *content); |
51d0854c | 278 | bool switch_to_ns(pid_t pid, const char *ns); |
e1daebd9 | 279 | int is_dir(const char *path); |
6010a416 | 280 | char *get_template_path(const char *t); |
0a4be28d | 281 | int setproctitle(char *title); |
592fd47a SH |
282 | int safe_mount(const char *src, const char *dest, const char *fstype, |
283 | unsigned long flags, const void *data, const char *rootfs); | |
ced03a01 | 284 | int mount_proc_if_needed(const char *rootfs); |
69aeabac | 285 | int null_stdfds(void); |
5b72de5f | 286 | #endif /* __LXC_UTILS_H */ |