]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
syscall_wrappers: move setns()
[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 __LXC_UTILS_H
24 #define __LXC_UTILS_H
25
26 /* Properly support loop devices on 32bit systems. */
27 #define _FILE_OFFSET_BITS 64
28
29 #include <errno.h>
30 #include <linux/loop.h>
31 #include <linux/types.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <sys/syscall.h>
36 #include <sys/types.h>
37 #include <sys/vfs.h>
38 #include <unistd.h>
39
40 #include "file_utils.h"
41 #include "initutils.h"
42 #include "macro.h"
43 #include "raw_syscalls.h"
44 #include "string_utils.h"
45
46 /* returns 1 on success, 0 if there were any failures */
47 extern int lxc_rmdir_onedev(const char *path, const char *exclude);
48 extern int get_u16(unsigned short *val, const char *arg, int base);
49 extern int mkdir_p(const char *dir, mode_t mode);
50 extern char *get_rundir(void);
51
52 /* Define getline() if missing from the C library */
53 #ifndef HAVE_GETLINE
54 #ifdef HAVE_FGETLN
55 #include <../include/getline.h>
56 #endif
57 #endif
58
59 /* Define sethostname() if missing from the C library */
60 #ifndef HAVE_SETHOSTNAME
61 static inline int sethostname(const char *name, size_t len)
62 {
63 #ifdef __NR_sethostname
64 return syscall(__NR_sethostname, name, len);
65 #else
66 errno = ENOSYS;
67 return -1;
68 #endif
69 }
70 #endif
71
72 /* Define unshare() if missing from the C library */
73 #ifndef HAVE_UNSHARE
74 static inline int unshare(int flags)
75 {
76 #ifdef __NR_unshare
77 return syscall(__NR_unshare, flags);
78 #else
79 errno = ENOSYS;
80 return -1;
81 #endif
82 }
83 #else
84 extern int unshare(int);
85 #endif
86
87 /* Define signalfd() if missing from the C library */
88 #ifdef HAVE_SYS_SIGNALFD_H
89 # include <sys/signalfd.h>
90 #else
91 /* assume kernel headers are too old */
92 #include <stdint.h>
93 struct signalfd_siginfo
94 {
95 uint32_t ssi_signo;
96 int32_t ssi_errno;
97 int32_t ssi_code;
98 uint32_t ssi_pid;
99 uint32_t ssi_uid;
100 int32_t ssi_fd;
101 uint32_t ssi_tid;
102 uint32_t ssi_band;
103 uint32_t ssi_overrun;
104 uint32_t ssi_trapno;
105 int32_t ssi_status;
106 int32_t ssi_int;
107 uint64_t ssi_ptr;
108 uint64_t ssi_utime;
109 uint64_t ssi_stime;
110 uint64_t ssi_addr;
111 uint8_t __pad[48];
112 };
113
114 # ifndef __NR_signalfd4
115 /* assume kernel headers are too old */
116 # if __i386__
117 # define __NR_signalfd4 327
118 # elif __x86_64__
119 # define __NR_signalfd4 289
120 # elif __powerpc__
121 # define __NR_signalfd4 313
122 # elif __s390x__
123 # define __NR_signalfd4 322
124 # elif __arm__
125 # define __NR_signalfd4 355
126 # elif __mips__ && _MIPS_SIM == _ABIO32
127 # define __NR_signalfd4 4324
128 # elif __mips__ && _MIPS_SIM == _ABI64
129 # define __NR_signalfd4 5283
130 # elif __mips__ && _MIPS_SIM == _ABIN32
131 # define __NR_signalfd4 6287
132 # endif
133 #endif
134
135 # ifndef __NR_signalfd
136 /* assume kernel headers are too old */
137 # if __i386__
138 # define __NR_signalfd 321
139 # elif __x86_64__
140 # define __NR_signalfd 282
141 # elif __powerpc__
142 # define __NR_signalfd 305
143 # elif __s390x__
144 # define __NR_signalfd 316
145 # elif __arm__
146 # define __NR_signalfd 349
147 # elif __mips__ && _MIPS_SIM == _ABIO32
148 # define __NR_signalfd 4317
149 # elif __mips__ && _MIPS_SIM == _ABI64
150 # define __NR_signalfd 5276
151 # elif __mips__ && _MIPS_SIM == _ABIN32
152 # define __NR_signalfd 6280
153 # endif
154 #endif
155
156 static inline int signalfd(int fd, const sigset_t *mask, int flags)
157 {
158 int retval;
159
160 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
161 if (errno == ENOSYS && flags == 0)
162 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
163 return retval;
164 }
165 #endif
166
167 static inline int lxc_set_cloexec(int fd)
168 {
169 return fcntl(fd, F_SETFD, FD_CLOEXEC);
170 }
171
172 /* Struct to carry child pid from lxc_popen() to lxc_pclose().
173 * Not an opaque struct to allow direct access to the underlying FILE *
174 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
175 * without additional wrappers.
176 */
177 struct lxc_popen_FILE {
178 int pipe;
179 FILE *f;
180 pid_t child_pid;
181 };
182
183 /* popen(command, "re") replacement that restores default signal mask
184 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
185 * In short, popen(command, "re") does pipe() + fork() + exec()
186 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
187 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
188 * On error returns NULL.
189 */
190 extern struct lxc_popen_FILE *lxc_popen(const char *command);
191
192 /* pclose() replacement to be used on struct lxc_popen_FILE *,
193 * returned by lxc_popen().
194 * Waits for associated process to terminate, returns its exit status and
195 * frees resources, pointed to by struct lxc_popen_FILE *.
196 */
197 extern int lxc_pclose(struct lxc_popen_FILE *fp);
198
199 /*
200 * wait on a child we forked
201 */
202 extern int wait_for_pid(pid_t pid);
203 extern int lxc_wait_for_pid_status(pid_t pid);
204
205 #if HAVE_LIBGNUTLS
206 #define SHA_DIGEST_LENGTH 20
207 extern int sha1sum_file(char *fnam, unsigned char *md_value);
208 #endif
209
210 /* initialize rand with urandom */
211 extern int randseed(bool);
212
213 /* are we unprivileged with respect to our namespaces */
214 inline static bool am_guest_unpriv(void) {
215 return geteuid() != 0;
216 }
217
218 /* are we unprivileged with respect to init_user_ns */
219 inline static bool am_host_unpriv(void)
220 {
221 FILE *f;
222 uid_t user, host, count;
223 int ret;
224
225 if (geteuid() != 0)
226 return true;
227
228 /* Now: are we in a user namespace? Because then we're also
229 * unprivileged.
230 */
231 f = fopen("/proc/self/uid_map", "r");
232 if (!f) {
233 return false;
234 }
235
236 ret = fscanf(f, "%u %u %u", &user, &host, &count);
237 fclose(f);
238 if (ret != 3) {
239 return false;
240 }
241
242 if (user != 0 || host != 0 || count != UINT32_MAX)
243 return true;
244 return false;
245 }
246
247 /*
248 * parse /proc/self/uid_map to find what @orig maps to
249 */
250 extern uid_t get_ns_uid(uid_t orig);
251 /*
252 * parse /proc/self/gid_map to find what @orig maps to
253 */
254 extern gid_t get_ns_gid(gid_t orig);
255
256 extern bool dir_exists(const char *path);
257
258 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
259 extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
260
261 extern bool is_shared_mountpoint(const char *path);
262 extern int detect_shared_rootfs(void);
263 extern bool detect_ramfs_rootfs(void);
264 extern char *on_path(const char *cmd, const char *rootfs);
265 extern bool cgns_supported(void);
266 extern char *choose_init(const char *rootfs);
267 extern bool switch_to_ns(pid_t pid, const char *ns);
268 extern char *get_template_path(const char *t);
269 extern int safe_mount(const char *src, const char *dest, const char *fstype,
270 unsigned long flags, const void *data,
271 const char *rootfs);
272 extern int lxc_mount_proc_if_needed(const char *rootfs);
273 extern int open_devnull(void);
274 extern int set_stdfds(int fd);
275 extern int null_stdfds(void);
276 extern int lxc_preserve_ns(const int pid, const char *ns);
277
278 /* Check whether a signal is blocked by a process. */
279 extern bool task_blocks_signal(pid_t pid, int signal);
280
281 /* Switch to a new uid and gid.
282 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
283 */
284 extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
285 extern bool lxc_setgroups(int size, gid_t list[]);
286
287 /* Find an unused loop device and associate it with source. */
288 extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
289
290 /* Clear all mounts on a given node.
291 * >= 0 successfully cleared. The number returned is the number of umounts
292 * performed.
293 * < 0 error umounting. Return -errno.
294 */
295 extern int lxc_unstack_mountpoint(const char *path, bool lazy);
296
297 /*
298 * run_command runs a command and collect it's std{err,out} output in buf.
299 *
300 * @param[out] buf The buffer where the commands std{err,out] output will be
301 * read into. If no output was produced, buf will be memset
302 * to 0.
303 * @param[in] buf_size The size of buf. This function will reserve one byte for
304 * \0-termination.
305 * @param[in] child_fn The function to be run in the child process. This
306 * function must exec.
307 * @param[in] args Arguments to be passed to child_fn.
308 */
309 extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
310 void *args);
311
312 /* Concatenate all passed-in strings into one path. Do not fail. If any piece
313 * is not prefixed with '/', add a '/'.
314 */
315 __attribute__((sentinel)) extern char *must_concat(const char *first, ...);
316 __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
317 __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
318
319 /* return copy of string @entry; do not fail. */
320 extern char *must_copy_string(const char *entry);
321
322 /* Re-alllocate a pointer, do not fail */
323 extern void *must_realloc(void *orig, size_t sz);
324
325 extern bool lxc_nic_exists(char *nic);
326
327 static inline uint64_t lxc_getpagesize(void)
328 {
329 int64_t pgsz;
330
331 pgsz = sysconf(_SC_PAGESIZE);
332 if (pgsz <= 0)
333 pgsz = 1 << 12;
334
335 return pgsz;
336 }
337
338 /* If n is not a power of 2 this function will return the next power of 2
339 * greater than that number. Note that this function always returns the *next*
340 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
341 * as argument this function will return 2048 although the closest power of 2
342 * would be 1024.
343 * If the caller passes in 0 they will receive 0 in return since this is invalid
344 * input and 0 is not a power of 2.
345 */
346 extern uint64_t lxc_find_next_power2(uint64_t n);
347
348 static inline pid_t lxc_raw_gettid(void)
349 {
350 #ifdef SYS_gettid
351 return syscall(SYS_gettid);
352 #else
353 return lxc_raw_getpid();
354 #endif
355 }
356
357 /* Set a signal the child process will receive after the parent has died. */
358 extern int lxc_set_death_signal(int signal, pid_t parent);
359 extern int fd_cloexec(int fd, bool cloexec);
360 extern int recursive_destroy(char *dirname);
361 extern int lxc_setup_keyring(void);
362
363 #endif /* __LXC_UTILS_H */