]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
lxc-attach: Completely rework lxc-attach and move to API function
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #ifndef _utils_h
24 #define _utils_h
25
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include "config.h"
30
31 /* returns 1 on success, 0 if there were any failures */
32 extern int lxc_rmdir_onedev(char *path);
33 extern int lxc_setup_fs(void);
34 extern int get_u16(unsigned short *val, const char *arg, int base);
35 extern int mkdir_p(const char *dir, mode_t mode);
36 /*
37 * Return a newly allocated buffer containing the default container
38 * path. Caller must free this buffer.
39 */
40 extern const char *default_lxc_path(void);
41 extern const char *default_zfs_root(void);
42 extern const char *default_lvm_vg(void);
43
44 /* Define getline() if missing from the C library */
45 #ifndef HAVE_GETLINE
46 #ifdef HAVE_FGETLN
47 #include <../include/getline.h>
48 #endif
49 #endif
50
51 /* Define setns() if missing from the C library */
52 #ifndef HAVE_SETNS
53 static inline int setns(int fd, int nstype)
54 {
55 #ifdef __NR_setns
56 return syscall(__NR_setns, fd, nstype);
57 #else
58 errno = ENOSYS;
59 return -1;
60 #endif
61 }
62 #endif
63
64 /* Define unshare() if missing from the C library */
65 #ifndef HAVE_UNSHARE
66 static inline int unshare(int flags)
67 {
68 #ifdef __NR_unshare
69 return syscall(__NR_unshare, flags);
70 #else
71 errno = ENOSYS;
72 return -1;
73 #endif
74 }
75 #else
76 int unshare(int);
77 #endif
78
79 /* Define signalfd() if missing from the C library */
80 #ifdef HAVE_SYS_SIGNALFD_H
81 # include <sys/signalfd.h>
82 #else
83 /* assume kernel headers are too old */
84 #include <stdint.h>
85 struct signalfd_siginfo
86 {
87 uint32_t ssi_signo;
88 int32_t ssi_errno;
89 int32_t ssi_code;
90 uint32_t ssi_pid;
91 uint32_t ssi_uid;
92 int32_t ssi_fd;
93 uint32_t ssi_tid;
94 uint32_t ssi_band;
95 uint32_t ssi_overrun;
96 uint32_t ssi_trapno;
97 int32_t ssi_status;
98 int32_t ssi_int;
99 uint64_t ssi_ptr;
100 uint64_t ssi_utime;
101 uint64_t ssi_stime;
102 uint64_t ssi_addr;
103 uint8_t __pad[48];
104 };
105
106 # ifndef __NR_signalfd4
107 /* assume kernel headers are too old */
108 # if __i386__
109 # define __NR_signalfd4 327
110 # elif __x86_64__
111 # define __NR_signalfd4 289
112 # elif __powerpc__
113 # define __NR_signalfd4 313
114 # elif __s390x__
115 # define __NR_signalfd4 322
116 # endif
117 #endif
118
119 # ifndef __NR_signalfd
120 /* assume kernel headers are too old */
121 # if __i386__
122 # define __NR_signalfd 321
123 # elif __x86_64__
124 # define __NR_signalfd 282
125 # elif __powerpc__
126 # define __NR_signalfd 305
127 # elif __s390x__
128 # define __NR_signalfd 316
129 # endif
130 #endif
131
132 static inline int signalfd(int fd, const sigset_t *mask, int flags)
133 {
134 int retval;
135
136 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
137 if (errno == ENOSYS && flags == 0)
138 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
139 return retval;
140 }
141 #endif
142
143
144 /**
145 * BUILD_BUG_ON - break compile if a condition is true.
146 * @condition: the condition which the compiler should know is false.
147 *
148 * If you have some code which relies on certain constants being equal, or
149 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
150 * detect if someone changes it.
151 *
152 * The implementation uses gcc's reluctance to create a negative array, but
153 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
154 * to inline functions). So as a fallback we use the optimizer; if it can't
155 * prove the condition is false, it will cause a link error on the undefined
156 * "__build_bug_on_failed". This error message can be harder to track down
157 * though, hence the two different methods.
158 */
159 #ifndef __OPTIMIZE__
160 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
161 #else
162 extern int __build_bug_on_failed;
163 #define BUILD_BUG_ON(condition) \
164 do { \
165 ((void)sizeof(char[1 - 2*!!(condition)])); \
166 if (condition) __build_bug_on_failed = 1; \
167 } while(0)
168 #endif
169
170 /*
171 * wait on a child we forked
172 */
173 extern int wait_for_pid(pid_t pid);
174 extern int lxc_wait_for_pid_status(pid_t pid);
175
176 /* send and receive buffers completely */
177 extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count);
178 extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count);
179 extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf);
180 #if HAVE_LIBGNUTLS
181 #define SHA_DIGEST_LENGTH 20
182 extern int sha1sum_file(char *fnam, unsigned char *md_value);
183 #endif
184
185 #endif