1 // $Id: syscall-legacy.hc,v 1.10 2005/05/02 21:41:49 ensc Exp $ --*- c -*--
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on syscall.cc by Jacques Gelinas
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)
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.
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.
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).
25 We rely on /proc/self/status to find the syscall number.
27 If it is not there, we rely on adm/unistd.h.
29 If this file does not have those system calls (not a patched kernel source)
30 we rely on static values in this file.
32 #include "safechroot-internal.hc"
37 #include <asm/unistd.h>
40 #include "syscall-wrap.h"
42 // Here is the trick. We keep a copy of the define, then undef it
43 // and then later, we try to locate the value reading /proc/self/status
44 // If this fails, we have the old preserved copy.
45 static int def_NR_set_ipv4root = 274;
46 #undef __NR_set_ipv4root
48 static int __NR_set_ipv4root_rev0;
49 static int __NR_set_ipv4root_rev1;
50 static int __NR_set_ipv4root_rev2;
51 static int __NR_set_ipv4root_rev3;
52 static int rev_ipv4root=0;
54 #ifdef ENSC_SYSCALL_TRADITIONAL
55 # if defined __dietlibc__
56 extern long int syscall (long int __sysno, ...);
60 set_ipv4root_rev0(unsigned long ip)
62 return syscall(__NR_set_ipv4root_rev0, ip);
66 set_ipv4root_rev1(unsigned long ip, unsigned long bcast)
68 return syscall(__NR_set_ipv4root_rev1, ip, bcast);
72 set_ipv4root_rev2(unsigned long *ip, int nb, unsigned long bcast)
74 return syscall(__NR_set_ipv4root_rev2, ip, nb, bcast);
78 set_ipv4root_rev3(unsigned long *ip, int nb, unsigned long bcast, unsigned long * mask)
80 return syscall(__NR_set_ipv4root_rev3, ip, nb, bcast, mask);
83 #else // ENSC_SYSCALL_TRADITIONAL
84 inline static _syscall1(int, set_ipv4root_rev0, unsigned long, ip)
85 inline static _syscall2(int, set_ipv4root_rev1, unsigned long, ip, unsigned long, bcast)
86 inline static _syscall3(int, set_ipv4root_rev2, unsigned long *, ip, int, nb, unsigned long, bcast)
87 inline static _syscall4(int, set_ipv4root_rev3, unsigned long *, ip, int, nb, unsigned long, bcast, unsigned long *, mask)
88 #endif // ENSC_SYSCALL_TRADITIONAL
90 static int def_NR_new_s_context = 273;
91 #undef __NR_new_s_context
92 static int __NR_new_s_context_rev0;
93 static int rev_s_context=0;
96 #ifdef ENSC_SYSCALL_TRADITIONAL
98 new_s_context_rev0(int newctx, int remove_cap, int flags)
100 return syscall(__NR_new_s_context_rev0, newctx, remove_cap, flags);
102 #else // ENSC_SYSCALL_TRADITIONAL
103 inline static _syscall3(int, new_s_context_rev0, int, newctx, int, remove_cap, int, flags)
104 #endif // ENSC_SYSCALL_TRADITIONAL
107 static bool is_init = false;
109 #include "utils-legacy.h"
112 # define WRITE_MSG(FD,X) (void)(write(FD,X,sizeof(X)-1))
117 getNumRevPair(char const *str, int *num, int *rev)
119 char const * blank_pos = strchr(str, ' ');
120 char const * eol_pos = strchr(str, '\n');
123 if (*num==0) return false;
125 if (blank_pos!=0 && eol_pos!=0 && blank_pos<eol_pos &&
126 strncmp(blank_pos+1, "rev", 3)==0)
127 *rev = atoi(blank_pos+4);
132 #define SET_TAG_POS(TAG) \
133 pos = strstr(buf, (TAG)); \
134 if (pos) pos+=sizeof(TAG)-1
136 static bool init_internal()
138 size_t bufsize = utilvserver_getProcEntryBufsize();
140 char const * pos = 0;
141 pid_t pid = getpid();
146 pos=utilvserver_getProcEntry(pid, 0, buf, bufsize);
147 if (pos==0 && errno==EAGAIN) return false;
149 SET_TAG_POS("\n__NR_set_ipv4root: ");
150 if ( pos!=0 && getNumRevPair(pos, &num, &rev_ipv4root) ) {
151 __NR_set_ipv4root_rev0 =
152 __NR_set_ipv4root_rev1 =
153 __NR_set_ipv4root_rev2 =
154 __NR_set_ipv4root_rev3 = num;
157 SET_TAG_POS("\n__NR_new_s_context: ");
158 if ( pos!=0 && getNumRevPair(pos, &num, &rev_s_context) )
159 __NR_new_s_context_rev0 = num;
169 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
170 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
171 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
172 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
173 __NR_new_s_context_rev0 = def_NR_new_s_context;
175 while (!init_internal() && errno==EAGAIN) {}
181 void vc_init_legacy()
186 void vc_init_internal_legacy(int ctx_rev, int ctx_number,
187 int ipv4_rev, int ipv4_number)
189 rev_s_context = ctx_rev;
190 __NR_new_s_context_rev0 = ctx_number;
192 rev_ipv4root = ipv4_rev;
193 __NR_set_ipv4root_rev0 = ipv4_number;
194 __NR_set_ipv4root_rev1 = ipv4_number;
195 __NR_set_ipv4root_rev2 = ipv4_number;
196 __NR_set_ipv4root_rev3 = ipv4_number;
201 static ALWAYSINLINE xid_t
202 vc_new_s_context_legacy(int ctx, int remove_cap, int flags)
206 if (rev_s_context == 0){
207 return new_s_context_rev0(ctx, remove_cap, flags);
215 static ALWAYSINLINE int
216 vc_set_ipv4root_legacy_internal (
220 unsigned long mask[])
223 if (rev_ipv4root == 0){
225 WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
227 return set_ipv4root_rev0 (ip[0]);
228 }else if (rev_ipv4root == 1){
230 WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
232 return set_ipv4root_rev1 (ip[0],bcast);
233 }else if (rev_ipv4root == 2){
234 return set_ipv4root_rev2 (ip,nb,bcast);
235 }else if (rev_ipv4root == 3){
236 return set_ipv4root_rev3 (ip,nb,bcast,mask);
242 static ALWAYSINLINE int
243 vc_set_ipv4root_legacy(uint32_t bcast, size_t nb, struct vc_ip_mask_pair const *ips)
245 unsigned long ip[nb];
246 unsigned long mask[nb];
249 for (i=0; i<nb; ++i) {
251 mask[i] = ips[i].mask;
254 return vc_set_ipv4root_legacy_internal(ip, nb, bcast, mask);