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