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