]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
spelling: timeout
[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 static inline int lxc_set_cloexec(int fd)
60 {
61 return fcntl(fd, F_SETFD, FD_CLOEXEC);
62 }
63
64 /* Struct to carry child pid from lxc_popen() to lxc_pclose().
65 * Not an opaque struct to allow direct access to the underlying FILE *
66 * (i.e., struct lxc_popen_FILE *file; fgets(buf, sizeof(buf), file->f))
67 * without additional wrappers.
68 */
69 struct lxc_popen_FILE {
70 int pipe;
71 FILE *f;
72 pid_t child_pid;
73 };
74
75 /* popen(command, "re") replacement that restores default signal mask
76 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
77 * In short, popen(command, "re") does pipe() + fork() + exec()
78 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
79 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
80 * On error returns NULL.
81 */
82 extern struct lxc_popen_FILE *lxc_popen(const char *command);
83
84 /* pclose() replacement to be used on struct lxc_popen_FILE *,
85 * returned by lxc_popen().
86 * Waits for associated process to terminate, returns its exit status and
87 * frees resources, pointed to by struct lxc_popen_FILE *.
88 */
89 extern int lxc_pclose(struct lxc_popen_FILE *fp);
90
91 /*
92 * wait on a child we forked
93 */
94 extern int wait_for_pid(pid_t pid);
95 extern int lxc_wait_for_pid_status(pid_t pid);
96
97 #if HAVE_LIBGNUTLS
98 #define SHA_DIGEST_LENGTH 20
99 extern int sha1sum_file(char *fnam, unsigned char *md_value);
100 #endif
101
102 /* initialize rand with urandom */
103 extern int randseed(bool);
104
105 /* are we unprivileged with respect to our namespaces */
106 inline static bool am_guest_unpriv(void) {
107 return geteuid() != 0;
108 }
109
110 /* are we unprivileged with respect to init_user_ns */
111 inline static bool am_host_unpriv(void)
112 {
113 FILE *f;
114 uid_t user, host, count;
115 int ret;
116
117 if (geteuid() != 0)
118 return true;
119
120 /* Now: are we in a user namespace? Because then we're also
121 * unprivileged.
122 */
123 f = fopen("/proc/self/uid_map", "r");
124 if (!f) {
125 return false;
126 }
127
128 ret = fscanf(f, "%u %u %u", &user, &host, &count);
129 fclose(f);
130 if (ret != 3) {
131 return false;
132 }
133
134 if (user != 0 || host != 0 || count != UINT32_MAX)
135 return true;
136 return false;
137 }
138
139 /*
140 * parse /proc/self/uid_map to find what @orig maps to
141 */
142 extern uid_t get_ns_uid(uid_t orig);
143 /*
144 * parse /proc/self/gid_map to find what @orig maps to
145 */
146 extern gid_t get_ns_gid(gid_t orig);
147
148 extern bool dir_exists(const char *path);
149
150 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
151 extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
152
153 extern bool is_shared_mountpoint(const char *path);
154 extern int detect_shared_rootfs(void);
155 extern bool detect_ramfs_rootfs(void);
156 extern char *on_path(const char *cmd, const char *rootfs);
157 extern bool cgns_supported(void);
158 extern char *choose_init(const char *rootfs);
159 extern bool switch_to_ns(pid_t pid, const char *ns);
160 extern char *get_template_path(const char *t);
161 extern int safe_mount(const char *src, const char *dest, const char *fstype,
162 unsigned long flags, const void *data,
163 const char *rootfs);
164 extern int lxc_mount_proc_if_needed(const char *rootfs);
165 extern int open_devnull(void);
166 extern int set_stdfds(int fd);
167 extern int null_stdfds(void);
168 extern int lxc_preserve_ns(const int pid, const char *ns);
169
170 /* Check whether a signal is blocked by a process. */
171 extern bool task_blocks_signal(pid_t pid, int signal);
172
173 /* Switch to a new uid and gid.
174 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
175 */
176 extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
177 extern bool lxc_setgroups(int size, gid_t list[]);
178
179 /* Find an unused loop device and associate it with source. */
180 extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
181
182 /* Clear all mounts on a given node.
183 * >= 0 successfully cleared. The number returned is the number of umounts
184 * performed.
185 * < 0 error umounting. Return -errno.
186 */
187 extern int lxc_unstack_mountpoint(const char *path, bool lazy);
188
189 /*
190 * run_command runs a command and collect it's std{err,out} output in buf.
191 *
192 * @param[out] buf The buffer where the commands std{err,out] output will be
193 * read into. If no output was produced, buf will be memset
194 * to 0.
195 * @param[in] buf_size The size of buf. This function will reserve one byte for
196 * \0-termination.
197 * @param[in] child_fn The function to be run in the child process. This
198 * function must exec.
199 * @param[in] args Arguments to be passed to child_fn.
200 */
201 extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *),
202 void *args);
203
204 /* Concatenate all passed-in strings into one path. Do not fail. If any piece
205 * is not prefixed with '/', add a '/'.
206 */
207 __attribute__((sentinel)) extern char *must_concat(const char *first, ...);
208 __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
209 __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
210
211 /* return copy of string @entry; do not fail. */
212 extern char *must_copy_string(const char *entry);
213
214 /* Re-allocate a pointer, do not fail */
215 extern void *must_realloc(void *orig, size_t sz);
216
217 extern bool lxc_nic_exists(char *nic);
218
219 static inline uint64_t lxc_getpagesize(void)
220 {
221 int64_t pgsz;
222
223 pgsz = sysconf(_SC_PAGESIZE);
224 if (pgsz <= 0)
225 pgsz = 1 << 12;
226
227 return pgsz;
228 }
229
230 /* If n is not a power of 2 this function will return the next power of 2
231 * greater than that number. Note that this function always returns the *next*
232 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
233 * as argument this function will return 2048 although the closest power of 2
234 * would be 1024.
235 * If the caller passes in 0 they will receive 0 in return since this is invalid
236 * input and 0 is not a power of 2.
237 */
238 extern uint64_t lxc_find_next_power2(uint64_t n);
239
240 /* Set a signal the child process will receive after the parent has died. */
241 extern int lxc_set_death_signal(int signal, pid_t parent);
242 extern int fd_cloexec(int fd, bool cloexec);
243 extern int recursive_destroy(char *dirname);
244 extern int lxc_setup_keyring(void);
245
246 #endif /* __LXC_UTILS_H */