Return 0 on any error, older kernels will return -EPERM for unknown syscalls.
[util-vserver.git] / lib / vserver-internal.h
1 // $Id$    --*- c++ -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 #ifdef H_VSERVER_SYSCALL_INTERNAL_H
19 #  error vserver-internal.h must not be included more than once
20 #endif
21
22 #ifndef H_VSERVER_SYSCALL_INTERNAL_H
23 #define H_VSERVER_SYSCALL_INTERNAL_H
24
25 #include <asm/unistd.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <unistd.h>
29
30 #include "internal.h"
31 #include "syscall-wrap.h"
32
33 #if !defined(__NR_vserver) && defined(ENSC_SYSCALL__NR_vserver)
34 #  define __NR_vserver  ENSC_SYSCALL__NR_vserver
35 #endif
36
37 inline static ALWAYSINLINE void vc_noop0() {}
38
39 #define VC_PREFIX       0)
40 #define VC_SUFFIX       else (void)(vc_noop0()
41 #define CALL_VC_NOOP    vc_noop0()
42 #define CALL_VC_GENERAL(ID, SUFFIX, FUNC, ...)                          \
43   VC_PREFIX; VC_SELECT(ID) return FUNC ## _ ## SUFFIX(__VA_ARGS__); VC_SUFFIX
44 #define CALL_VC_GENERAL_CONFIG(BIT, SUFFIX, FUNC, ...)                  \
45   VC_PREFIX; VC_CBIT(BIT)  return FUNC ## _ ## SUFFIX(__VA_ARGS__); VC_SUFFIX
46
47 #ifdef VC_MULTIVERSION_SYSCALL
48 #  define VC_SELECT(ID) if (ver>=(ID))
49 #  define VC_CBIT(BIT)  if ((conf&BIT) == BIT)
50 #  define CALL_VC(...)                                  \
51   do {                                                  \
52     int ver = utilvserver_checkCompatVersion();         \
53     uint_least32_t conf = utilvserver_checkCompatConfig();      \
54     if (ver==-1) return -1;                             \
55     VC_SUFFIX, __VA_ARGS__, VC_PREFIX;                  \
56     errno = ENOSYS;                                     \
57     return -1;                                          \
58   } while (0)
59 #else
60 #  define VC_SELECT(ID) if (1)
61 #  define VC_CBIT(BIT)  if (1)
62 #  define CALL_VC(...)                                  \
63   do {                                                  \
64     if (1) {} VC_SUFFIX, __VA_ARGS__, VC_PREFIX;        \
65     errno = ENOSYS; return -1;                          \
66   } while (0)
67 #endif
68
69 #ifdef VC_ENABLE_API_COMPAT
70 #  define CALL_VC_COMPAT(F,...) CALL_VC_GENERAL(0x00010000, compat, F, __VA_ARGS__)
71 #else
72 #  define CALL_VC_COMPAT(F,...) CALL_VC_NOOP
73 #endif
74
75 #ifdef VC_ENABLE_API_LEGACY
76 #  define CALL_VC_LEGACY(F,...) CALL_VC_GENERAL(0x00000000, legacy, F, __VA_ARGS__)
77 #else
78 #  define CALL_VC_LEGACY(F,...) CALL_VC_NOOP
79 #endif
80
81 #ifdef VC_ENABLE_API_V11
82 #  define CALL_VC_V11(F,...)    CALL_VC_GENERAL(0x00010000, v11, F, __VA_ARGS__)
83 #else
84 #  define CALL_VC_V11(F,...)    CALL_VC_NOOP
85 #endif
86
87 #ifdef VC_ENABLE_API_V13
88 #  define CALL_VC_V13(F,...)    CALL_VC_GENERAL(0x00010011, v13, F, __VA_ARGS__)
89 #else
90 #  define CALL_VC_V13(F,...)    CALL_VC_NOOP
91 #endif
92
93 #ifdef VC_ENABLE_API_V13
94 #  define CALL_VC_V13A(F,...)   CALL_VC_GENERAL(0x00010012, v13, F, __VA_ARGS__)
95 #else
96 #  define CALL_VC_V13A(F,...)   CALL_VC_NOOP
97 #endif
98
99 #ifdef VC_ENABLE_API_V13
100 #  define CALL_VC_V13B(F,...)   CALL_VC_GENERAL(0x00010021, v13b, F, __VA_ARGS__)
101 #else
102 #  define CALL_VC_V13B(F,...)   CALL_VC_NOOP
103 #endif
104
105 #ifdef VC_ENABLE_API_V13OBS
106 #  define CALL_VC_V13OBS(F,...) CALL_VC_GENERAL(0x00010011, v13obs, F, __VA_ARGS__)
107 #else
108 #  define CALL_VC_V13OBS(F,...) CALL_VC_NOOP
109 #endif
110
111 #ifdef VC_ENABLE_API_V21
112 #  define CALL_VC_V21(F,...)    CALL_VC_GENERAL(0x00020100, v21, F, __VA_ARGS__)
113 #else
114 #  define CALL_VC_V21(F,...)    CALL_VC_NOOP
115 #endif
116
117 #ifdef VC_ENABLE_API_V21
118 #  define CALL_VC_SPACES(F,...) CALL_VC_GENERAL_CONFIG(VC_VCI_SPACES, spaces, F, __VA_ARGS__)
119 #else
120 #  define CALL_VC_SPACES(F,...) CALL_VC_NOOP
121 #endif
122
123 #ifdef VC_ENABLE_API_NET
124 #  define CALL_VC_NET(F,...)    CALL_VC_GENERAL(0x00010016, net, F, __VA_ARGS__)
125 #else
126 #  define CALL_VC_NET(F,...)    CALL_VC_NOOP
127 #endif
128
129 #ifdef VC_ENABLE_API_FSCOMPAT
130 #  define CALL_VC_FSCOMPAT(F,...)       CALL_VC_GENERAL(0x00010000, fscompat, F, __VA_ARGS__)
131 #else
132 #  define CALL_VC_FSCOMPAT(F,...)       CALL_VC_NOOP
133 #endif
134
135 #ifdef VC_ENABLE_API_OLDPROC
136 #  define CALL_VC_OLDPROC(F,...)        CALL_VC_GENERAL(0x00000000, oldproc, F, __VA_ARGS__)
137 #else
138 #  define CALL_VC_OLDPROC(F,...)        CALL_VC_NOOP
139 #endif
140
141 #ifdef VC_ENABLE_API_OLDUTS
142 #  define CALL_VC_OLDUTS(F,...)         CALL_VC_GENERAL(0x00000000, olduts, F, __VA_ARGS__)
143 #else
144 #  define CALL_VC_OLDUTS(F,...)         CALL_VC_NOOP
145 #endif
146
147
148   // Some  kernel <-> userspace wrappers; they should be noops in most cases
149
150 #if 1
151 #  define CTX_KERNEL2USER(X)    (((X)==(uint32_t)(-1)) ? VC_NOCTX   : \
152                                  ((X)==(uint32_t)(-2)) ? VC_SAMECTX : \
153                                  (xid_t)(X))
154
155 #  define CTX_USER2KERNEL(X)    (((X)==VC_DYNAMIC_XID) ? (uint32_t)(-1) : \
156                                  ((X)==VC_SAMECTX)     ? (uint32_t)(-2) : \
157                                  (uint32_t)(X))
158 #else
159 #  define CTX_USER2KERNEL(X)    (X)
160 #  define CTX_KERNEL2USER(X)    (X)
161 #endif
162
163 #if 1
164 #  define EXT2FLAGS_USER2KERNEL(X)      (((X) & ~(VC_IMMUTABLE_FILE_FL|VC_IMMUTABLE_LINK_FL)) | \
165                                          ((X) & VC_IMMUTABLE_FILE_FL ? EXT2_IMMUTABLE_FILE_FL : 0) | \
166                                          ((X) & VC_IMMUTABLE_LINK_FL ? EXT2_IMMUTABLE_LINK_FL : 0))
167 #  define EXT2FLAGS_KERNEL2USER(X)      (((X) & ~(EXT2_IMMUTABLE_FILE_FL|EXT2_IMMUTABLE_LINK_FL)) | \
168                                          ((X) & EXT2_IMMUTABLE_FILE_FL ? VC_IMMUTABLE_FILE_FL : 0) | \
169                                          ((X) & EXT2_IMMUTABLE_LINK_FL ? VC_IMMUTABLE_LINK_FL : 0))
170 #else
171 #  define EXT2FLAGS_KERNEL2USER(X)      (X)
172 #  define EXT2FLAGS_USER2KERNEL(X)      (X)
173 #endif
174
175 #if 1
176 #  define VHI_USER2KERNEL(X)            ((((X)==vcVHI_CONTEXT)    ? VHIN_CONTEXT    : \
177                                           ((X)==vcVHI_SYSNAME)    ? VHIN_SYSNAME    : \
178                                           ((X)==vcVHI_NODENAME)   ? VHIN_NODENAME   : \
179                                           ((X)==vcVHI_RELEASE)    ? VHIN_RELEASE    : \
180                                           ((X)==vcVHI_VERSION)    ? VHIN_VERSION    : \
181                                           ((X)==vcVHI_MACHINE)    ? VHIN_MACHINE    : \
182                                           ((X)==vcVHI_DOMAINNAME) ? VHIN_DOMAINNAME : \
183                                           (X)))
184 #  define VHI_KERNEL2USER(X)            ((((X)==VHIN_CONTEXT)     ? vcVHI_CONTEXT    : \
185                                           ((X)==VHIN_SYSNAME)     ? vcVHI_SYSNAME    : \
186                                           ((X)==VHIN_NODENAME)    ? vcVHI_NODENAME   : \
187                                           ((X)==VHIN_RELEASE)     ? vcVHI_RELEASE    : \
188                                           ((X)==VHIN_VERSION)     ? vcVHI_VERSION    : \
189                                           ((X)==VHIN_MACHINE)     ? vcVHI_MACHINE    : \
190                                           ((X)==VHIN_DOMAINNAME)  ? vcVHI_DOMAINNAME : \
191                                           (X)))
192 #else
193 #  define VHI_USER2KERNEL(X)            (X)
194 #  define VHI_KERNEL2USER(X)            (X)
195 #endif
196
197 #if 1
198 #  define NID_KERNEL2USER(X)    (((X)==(uint32_t)(-1)) ? VC_NONID   : \
199                                  (xid_t)(X))
200
201 #  define NID_USER2KERNEL(X)    (((X)==VC_DYNAMIC_NID) ? (uint32_t)(-1) : \
202                                  (uint32_t)(X))
203 #else
204 #  define NID_USER2KERNEL(X)    (X)
205 #  define NID_KERNEL2USER(X)    (X)
206 #endif
207
208 #if 1
209 #  define NETTYPE_USER2KERNEL(X)        ((X)==vcNET_IPV4   ? NXA_TYPE_IPV4     : \
210                                          (X)==vcNET_IPV6   ? NXA_TYPE_IPV6     : \
211                                          (X)==vcNET_IPV4B  ? (NXA_TYPE_IPV4 | NXA_MOD_BCAST) : \
212                                          (X)==vcNET_IPV6B  ? (NXA_TYPE_IPV6 | NXA_MOD_BCAST) : \
213                                          (X)==vcNET_ANY    ? NXA_TYPE_ANY      : \
214                                          (X))
215 #  define NETTYPE_KERNEL2USER(X)        ((X)==NXA_TYPE_IPV4     ? vcNET_IPV4   : \
216                                          (X)==NXA_TYPE_IPV6     ? vcNET_IPV6   : \
217                                          (X)==(NXA_TYPE_IPV4|NXA_MOD_BCAST) ? vcNET_IPV4B : \
218                                          (X)==(NXA_TYPE_IPV6|NXA_MOD_BCAST) ? vcNET_IPV6B : \
219                                          (X)==NXA_TYPE_ANY      ? vcNET_ANY    : \
220                                          (X))
221 #else
222 #  define NETTYPE_USER2KERNEL(X)        (X)
223 #  define NETTYPE_KERNEL2USER(X)        (X)
224 #endif
225
226 #define CDLIM_USER2KERNEL(X)            ((X)==VC_CDLIM_UNSET    ? CDLIM_UNSET    : \
227                                          (X)==VC_CDLIM_INFINITY ? CDLIM_INFINITY : \
228                                          (X)==VC_CDLIM_KEEP     ? CDLIM_KEEP     : \
229                                          (X))
230
231   /// the __typeof__ thing is a hack to deal with the kernel interface
232   /// using an unsigned long long value for a uint32_t type
233 #define CDLIM_KERNEL2USER(X)            ((X)==(__typeof__(X))CDLIM_UNSET       ? VC_CDLIM_UNSET    : \
234                                          (X)==(__typeof__(X))CDLIM_INFINITY    ? VC_CDLIM_INFINITY : \
235                                          (X)==(__typeof__(X))CDLIM_KEEP        ? VC_CDLIM_KEEP     : \
236                                          (X))
237
238
239 #define ENSC_STRUCT_IDX(STRUCT,ATTR)                    \
240   ((char*)(&(STRUCT).ATTR) - (char*)(&(STRUCT)))
241 #define ENSC_SAME_STRUCT_IDX(LHS,RHS,ATTR)                      \
242   (ENSC_STRUCT_IDX(LHS,ATTR) == ENSC_STRUCT_IDX(RHS,ATTR))
243 #define ENSC_SAME_STRUCT_ITEM(LHS,RHS,ATTR)     \
244   (ENSC_SAME_STRUCT_IDX(LHS,RHS,ATTR) &&        \
245    sizeof((LHS).ATTR)==sizeof((RHS).ATTR) &&    \
246    sizeof(LHS)==sizeof(RHS))
247
248 #define EXT2_IOC_GETCONTEXT             _IOR('x', 1, long)
249 #define EXT2_IOC_SETCONTEXT             _IOW('x', 2, long)
250
251 #ifndef HAVE_VSERVER
252 #ifdef ENSC_SYSCALL_TRADITIONAL
253 inline static UNUSED ALWAYSINLINE
254 int vserver(uint32_t cmd, uint32_t id, void *data)
255 {
256   return syscall(__NR_vserver, cmd, id, data);
257 }
258 #else
259 inline static UNUSED ALWAYSINLINE
260 _syscall3(int, vserver,
261           uint32_t, cmd, uint32_t, id, void *, data)
262 #endif
263 #endif
264
265 #endif  //  H_VSERVER_SYSCALL_INTERNAL_H