use ENSC_SYSCALL_TRADITIONAL
[util-vserver.git] / util-vserver / lib / syscall-legacy.hc
1 // $Id$ --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on syscall.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         This tells the system call number for new_s_context and set_ipv4root
22         using /proc/self/status. This helps until the vserver project is
23         included officially in the kernel (and has its own syscall).
24
25         We rely on /proc/self/status to find the syscall number.
26
27         If it is not there, we rely on adm/unistd.h.
28
29         If this file does not have those system calls (not a patched kernel source)
30         we rely on static values in this file.
31 */
32 #include "safechroot-internal.hc"
33
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <syscall.h>
38 #include <asm/unistd.h>
39 #include <stdbool.h>
40
41 // Here is the trick. We keep a copy of the define, then undef it
42 // and then later, we try to locate the value reading /proc/self/status
43 // If this fails, we have the old preserved copy.
44 static int def_NR_set_ipv4root = 274;
45 #undef __NR_set_ipv4root
46
47 static int __NR_set_ipv4root_rev0;
48 static int __NR_set_ipv4root_rev1;
49 static int __NR_set_ipv4root_rev2;
50 static int __NR_set_ipv4root_rev3;
51 static int rev_ipv4root=0;
52
53 #ifdef ENSC_SYSCALL_TRADITIONAL
54 #if defined __dietlibc__
55 extern long int syscall (long int __sysno, ...);
56 #endif
57
58 inline static int
59 set_ipv4root_rev0(unsigned long ip)
60 {
61   return syscall(__NR_set_ipv4root_rev0, ip);
62 }
63
64 inline static int
65 set_ipv4root_rev1(unsigned long ip, unsigned long bcast)
66 {
67   return syscall(__NR_set_ipv4root_rev1, ip, bcast);
68 }
69
70 inline static int
71 set_ipv4root_rev2(unsigned long *ip, int nb, unsigned long bcast)
72 {
73   return syscall(__NR_set_ipv4root_rev2, ip, nb, bcast);
74 }
75
76 inline static int
77 set_ipv4root_rev3(unsigned long *ip, int nb, unsigned long bcast, unsigned long * mask)
78 {
79   return syscall(__NR_set_ipv4root_rev3, ip, nb, bcast, mask);
80 }
81
82 #else
83 inline static _syscall1(int, set_ipv4root_rev0, unsigned long, ip)
84 inline static _syscall2(int, set_ipv4root_rev1, unsigned long, ip, unsigned long, bcast)
85 inline static _syscall3(int, set_ipv4root_rev2, unsigned long *, ip, int, nb, unsigned long, bcast)
86 inline static _syscall4(int, set_ipv4root_rev3, unsigned long *, ip, int, nb, unsigned long, bcast, unsigned long *, mask)
87 #endif
88
89 static int def_NR_new_s_context = 273;
90 #undef __NR_new_s_context
91 static int __NR_new_s_context_rev0;
92   //static int __NR_new_s_context_rev1;
93 static int rev_s_context=0;
94
95 #if defined(__pic__) && defined(__i386)
96 inline static xid_t
97 new_s_context_rev0(int newctx, int remove_cap, int flags)
98 {
99   return syscall(__NR_new_s_context_rev0, newctx, remove_cap, flags);
100 }
101 #else
102 inline static _syscall3(int, new_s_context_rev0, int, newctx, int, remove_cap, int, flags)
103     //static _syscall4(int, new_s_context_rev1, int, nbctx, int *, ctxs, int, remove_cap, int, flags)
104 #endif
105
106 #if 0
107 #undef __NR_set_ctxlimit
108 static int __NR_set_ctxlimit=-1;
109 static int rev_set_ctxlimit=-1;
110
111 static _syscall2 (int, set_ctxlimit, int, resource, long, limit)
112 #endif
113
114 static bool     is_init = false;
115
116 #include "utils-legacy.h"
117
118 #ifndef WRITE_MSG
119 #  define WRITE_MSG(FD,X)         (void)(write(FD,X,sizeof(X)-1))
120 #endif
121
122
123 static bool
124 getNumRevPair(char const *str, int *num, int *rev)
125 {
126   char const *  blank_pos = strchr(str, ' ');
127   char const *  eol_pos   = strchr(str, '\n');
128   
129   *num = atoi(str);
130   if (*num==0) return false;
131   
132   if (blank_pos!=0 && eol_pos!=0 && blank_pos<eol_pos &&
133       strncmp(blank_pos+1, "rev", 3)==0)
134     *rev = atoi(blank_pos+4);
135
136   return true;
137 }
138
139 #define SET_TAG_POS(TAG)                        \
140   pos = strstr(buf, (TAG));                     \
141   if (pos) pos+=sizeof(TAG)-1
142
143 static bool init_internal()
144 {
145   size_t                        bufsize = utilvserver_getProcEntryBufsize();
146   char                          buf[bufsize];
147   char const *                  pos = 0;
148   pid_t                         pid = getpid();
149   int                           num;
150
151   errno = 0;
152
153   pos=utilvserver_getProcEntry(pid, 0, buf, bufsize);
154   if (pos==0 && errno==EAGAIN) return false;
155   
156   SET_TAG_POS("\n__NR_set_ipv4root: ");
157   if ( pos!=0 && getNumRevPair(pos, &num, &rev_ipv4root) ) {
158     __NR_set_ipv4root_rev0 =
159       __NR_set_ipv4root_rev1 =
160       __NR_set_ipv4root_rev2 =
161       __NR_set_ipv4root_rev3 = num;
162   }
163
164   SET_TAG_POS("\n__NR_new_s_context: ");
165   if ( pos!=0 && getNumRevPair(pos, &num, &rev_s_context) )
166     __NR_new_s_context_rev0 = num;
167
168   return true;
169 }
170
171 #undef SET_TAG_POS
172
173 static void init()
174 {
175         if (!is_init){
176                 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
177                 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
178                 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
179                 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
180                 __NR_new_s_context_rev0 = def_NR_new_s_context;
181
182                 while (!init_internal() && errno==EAGAIN) {}
183
184                 is_init = true;
185         }
186 }
187
188 void vc_init_legacy()
189 {
190         init();
191 }
192
193 void vc_init_internal_legacy(int ctx_rev, int ctx_number,
194                              int ipv4_rev, int ipv4_number)
195 {       
196   rev_s_context           = ctx_rev;
197   __NR_new_s_context_rev0 = ctx_number;
198
199   rev_ipv4root            = ipv4_rev;
200   __NR_set_ipv4root_rev0  = ipv4_number;
201   __NR_set_ipv4root_rev1  = ipv4_number;
202   __NR_set_ipv4root_rev2  = ipv4_number;
203   __NR_set_ipv4root_rev3  = ipv4_number;
204
205   is_init = true;
206 }
207
208 static ALWAYSINLINE xid_t
209 vc_new_s_context_legacy(int ctx, int remove_cap, int flags)
210 {
211         xid_t ret = -1;
212         init();
213         if (rev_s_context == 0){
214                 return new_s_context_rev0(ctx, remove_cap, flags);
215         }else{
216                 errno = -ENOSYS;
217                 ret   = VC_NOCTX;
218         }
219         return ret;
220 }
221
222 static ALWAYSINLINE int
223 vc_set_ipv4root_legacy_internal (
224         unsigned long ip[],
225         int nb,
226         unsigned long bcast,
227         unsigned long mask[])
228 {
229         init();
230         if (rev_ipv4root == 0){
231                 if (nb > 1){
232                         WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
233                 }
234                 return set_ipv4root_rev0 (ip[0]);
235         }else if (rev_ipv4root == 1){
236                 if (nb > 1){
237                         WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
238                 }
239                 return set_ipv4root_rev1 (ip[0],bcast);
240         }else if (rev_ipv4root == 2){
241                 return set_ipv4root_rev2 (ip,nb,bcast);
242         }else if (rev_ipv4root == 3){
243                 return set_ipv4root_rev3 (ip,nb,bcast,mask);
244         }
245         errno = EINVAL;
246         return -1;
247 }
248
249 static ALWAYSINLINE int
250 vc_set_ipv4root_legacy(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips)
251 {
252   unsigned long ip[nb];
253   unsigned long mask[nb];
254   size_t                i;
255
256   for (i=0; i<nb; ++i) {
257     ip[i]   = ips[i].ip;
258     mask[i] = ips[i].mask;
259   }
260
261   return vc_set_ipv4root_legacy_internal(ip, nb, bcast, mask);
262 }