CALL_VC(): return ENOSYS in errorcase
[util-vserver.git] / util-vserver / 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 <syscall.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <sys/syscall.h>
30 #include <unistd.h>
31
32 #include "internal.h"
33
34 #ifndef __NR_vserver
35 #  define __NR_vserver  273
36 #endif
37
38 #define VC_PREFIX       0)
39 #define VC_SUFFIX       else (void)((void)0
40 #define CALL_VC_NOOP    (void)0
41 #define CALL_VC_GENERAL(ID, SUFFIX, FUNC, ...)                          \
42   VC_PREFIX; VC_SELECT(ID) return FUNC ## _ ## SUFFIX(__VA_ARGS__); VC_SUFFIX
43
44 #ifdef VC_MULTIVERSION_SYSCALL
45 #  define VC_SELECT(ID) if (ver>=(ID))
46 #  define CALL_VC(...)                                  \
47   do {                                                  \
48     int ver = utilvserver_checkCompatVersion();         \
49     if (ver==-1) return -1;                             \
50     VC_SUFFIX, __VA_ARGS__, VC_PREFIX;                  \
51     errno = ENOSYS;                                     \
52     return -1;                                          \
53   } while (0)
54 #else
55 #  define VC_SELECT(ID) if (1)
56 #  define CALL_VC(...)                                  \
57   do {                                                  \
58     if (1) {} VC_SUFFIX, __VA_ARGS__, VC_PREFIX;        \
59     errno = ENOSYS; return -1;                          \
60   } while (0)
61 #endif
62
63 #ifdef VC_ENABLE_API_COMPAT
64 #  define CALL_VC_COMPAT(F,...) CALL_VC_GENERAL(0x00010000, compat, F, __VA_ARGS__)
65 #else
66 #  define CALL_VC_COMPAT(F,...) CALL_VC_NOOP
67 #endif
68
69 #ifdef VC_ENABLE_API_LEGACY
70 #  define CALL_VC_LEGACY(F,...) CALL_VC_GENERAL(0x00000000, legacy, F, __VA_ARGS__)
71 #else
72 #  define CALL_VC_LEGACY(F,...) CALL_VC_NOOP
73 #endif
74
75 #ifdef VC_ENABLE_API_V11
76 #  define CALL_VC_V11(F,...)    CALL_VC_GENERAL(0x00010000, v11, F, __VA_ARGS__)
77 #else
78 #  define CALL_VC_V11(F,...)    CALL_VC_NOOP
79 #endif
80
81 #ifdef VC_ENABLE_API_V13
82 #  define CALL_VC_V13(F,...)    CALL_VC_GENERAL(0x00010010, v13, F, __VA_ARGS__)
83 #else
84 #  define CALL_VC_V13(F,...)    CALL_VC_NOOP
85 #endif
86
87 #ifdef VC_ENABLE_API_FSCOMPAT
88 #  define CALL_VC_FSCOMPAT(F,...)       CALL_VC_GENERAL(0x00010000, fscompat, F, __VA_ARGS__)
89 #else
90 #  define CALL_VC_FSCOMPAT(F,...)       CALL_VC_NOOP
91 #endif
92
93 #ifdef VC_ENABLE_API_OLDPROC
94 #  define CALL_VC_OLDPROC(F,...)        CALL_VC_GENERAL(0x00000000, X, F, __VA_ARGS__)
95 #else
96 #  define CALL_VC_OLDPROC(F,...)        CALL_VC_NOOP
97 #endif
98
99 #if 1
100 #  define CTX_KERNEL2USER(X)    (((X)==(uint32_t)(-1)) ? VC_NOCTX   : \
101                                  ((X)==(uint32_t)(-2)) ? VC_SAMECTX : \
102                                  (xid_t)(X))
103
104 #  define CTX_USER2KERNEL(X)    (((X)==VC_RANDCTX) ? (uint32_t)(-1) : \
105                                  ((X)==VC_SAMECTX) ? (uint32_t)(-2) : \
106                                  (uint32_t)(X))
107 #else
108 #  define CTX_USER2KERNEL(X)    (X)
109 #  define CTX_KERNEL2USER(X)    (X)
110 #endif
111
112 #if 1
113 #  define EXT2FLAGS_USER2KERNEL(X)      (((X) & ~(VC_IMMUTABLE_FILE_FL|VC_IMMUTABLE_LINK_FL)) | \
114                                          ((X) & VC_IMMUTABLE_FILE_FL ? EXT2_IMMUTABLE_FILE_FL : 0) | \
115                                          ((X) & VC_IMMUTABLE_LINK_FL ? EXT2_IMMUTABLE_LINK_FL : 0))
116 #  define EXT2FLAGS_KERNEL2USER(X)      (((X) & ~(EXT2_IMMUTABLE_FILE_FL|EXT2_IMMUTABLE_LINK_FL)) | \
117                                          ((X) & EXT2_IMMUTABLE_FILE_FL ? VC_IMMUTABLE_FILE_FL : 0) | \
118                                          ((X) & EXT2_IMMUTABLE_LINK_FL ? VC_IMMUTABLE_LINK_FL : 0))
119 #else
120 #  define EXT2FLAGS_KERNEL2USER(X)      (X)
121 #  define EXT2FLAGS_USER2KERNEL(X)      (X)
122 #endif
123
124
125 #define EXT2_IOC_GETCONTEXT             _IOR('x', 1, long)
126 #define EXT2_IOC_SETCONTEXT             _IOW('x', 2, long)
127
128 #ifndef HAVE_VSERVER
129 #if defined(__pic__) && defined(__i386)
130 inline static UNUSED ALWAYSINLINE
131 int vserver(uint32_t cmd, uint32_t id, void *data)
132 {
133 #if defined __dietlibc__
134   extern long int syscall (long int __sysno, ...);
135 #endif
136  
137   return syscall(__NR_vserver, cmd, id, data);
138 }
139 #else
140 inline static UNUSED ALWAYSINLINE
141 _syscall3(int, vserver,
142           uint32_t, cmd, uint32_t, id, void *, data)
143 #endif
144 #endif
145
146 #endif  //  H_VSERVER_SYSCALL_INTERNAL_H