This commit was generated by cvs2svn to compensate for changes in r120,
[util-vserver.git] / lib / syscall-legacy.hc
1 // $Id: syscall-legacy.hc,v 1.1.4.6 2004/02/05 03:52:45 ensc Exp $ --*- 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 <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <syscall.h>
39 #include <asm/unistd.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 inline static int
55 set_ipv4root_rev0(unsigned long ip)
56 {
57   return syscall(__NR_set_ipv4root_rev0, ip);
58 }
59
60 inline static int
61 set_ipv4root_rev1(unsigned long ip, unsigned long bcast)
62 {
63   return syscall(__NR_set_ipv4root_rev1, ip, bcast);
64 }
65
66 inline static int
67 set_ipv4root_rev2(unsigned long *ip, int nb, unsigned long bcast)
68 {
69   return syscall(__NR_set_ipv4root_rev2, ip, nb, bcast);
70 }
71
72 inline static int
73 set_ipv4root_rev3(unsigned long *ip, int nb, unsigned long bcast, unsigned long * mask)
74 {
75   return syscall(__NR_set_ipv4root_rev3, ip, nb, bcast, mask);
76 }
77
78 #else
79 inline static _syscall1(int, set_ipv4root_rev0, unsigned long, ip)
80 inline static _syscall2(int, set_ipv4root_rev1, unsigned long, ip, unsigned long, bcast)
81 inline static _syscall3(int, set_ipv4root_rev2, unsigned long *, ip, int, nb, unsigned long, bcast)
82 inline static _syscall4(int, set_ipv4root_rev3, unsigned long *, ip, int, nb, unsigned long, bcast, unsigned long *, mask)
83 #endif
84
85 static int def_NR_new_s_context = 273;
86 #undef __NR_new_s_context
87 static int __NR_new_s_context_rev0;
88   //static int __NR_new_s_context_rev1;
89 static int rev_s_context=0;
90
91 #ifdef ENSC_SYSCALL_TRADITIONAL
92 inline static int
93 new_s_context_rev0(int newctx, int remove_cap, int flags)
94 {
95   return syscall(__NR_new_s_context_rev0, newctx, remove_cap, flags);
96 }
97 #else
98 inline static _syscall3(int, new_s_context_rev0, int, newctx, int, remove_cap, int, flags)
99     //static _syscall4(int, new_s_context_rev1, int, nbctx, int *, ctxs, int, remove_cap, int, flags)
100 #endif
101
102 #if 0
103 #undef __NR_set_ctxlimit
104 static int __NR_set_ctxlimit=-1;
105 static int rev_set_ctxlimit=-1;
106
107 static _syscall2 (int, set_ctxlimit, int, resource, long, limit)
108 #endif
109
110 static void init()
111 {
112         static int is_init = 0;
113         if (!is_init){
114                 FILE *fin = fopen ("/proc/self/status","r");
115                 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
116                 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
117                 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
118                 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
119                 __NR_new_s_context_rev0 = def_NR_new_s_context;
120                   //__NR_new_s_context_rev1 = def_NR_new_s_context;
121                 if (fin != NULL){
122                         char line[100];
123                         while (fgets(line,sizeof(line)-1,fin)!=NULL){
124                                 int num;
125                                 char title[100],rev[100];
126                                 rev[0] = '\0';
127                                 if (sscanf(line,"%s %d %s",title,&num,rev)>=2){
128                                         if (strcmp(title,"__NR_set_ipv4root:")==0){
129                                                 __NR_set_ipv4root_rev0 = num;
130                                                 __NR_set_ipv4root_rev1 = num;
131                                                 __NR_set_ipv4root_rev2 = num;
132                                                 __NR_set_ipv4root_rev3 = num;
133                                                 if (strncmp(rev,"rev",3)==0){
134                                                         rev_ipv4root = atoi(rev+3);
135                                                 }
136 #if 0                                           
137                                         }else if (strcmp(title,"__NR_set_ctxlimit:")==0){
138                                                 __NR_set_ctxlimit = num;
139                                                 if (strncmp(rev,"rev",3)==0){
140                                                         rev_set_ctxlimit = atoi(rev+3);
141                                                 }
142 #endif                                          
143                                         }else if (strcmp(title,"__NR_new_s_context:")==0){
144                                                 __NR_new_s_context_rev0 = num;
145                                                   //__NR_new_s_context_rev1 = num;
146                                                 if (strncmp(rev,"rev",3)==0){
147                                                         rev_s_context = atoi(rev+3);
148                                                 }
149                                         }
150                                 }
151                         }
152                         fclose (fin);
153                 }
154                 is_init = 1;
155         }
156 }
157
158 void vc_init_legacy()
159 {
160         init();
161 }
162
163 static ALWAYSINLINE int
164 vc_new_s_context_legacy(int ctx, int remove_cap, int flags)
165 {
166         int ret = -1;
167         init();
168         if (rev_s_context == 0){
169                 return new_s_context_rev0(ctx, remove_cap, flags);
170         }else{
171                 errno = -ENOSYS;
172                 ret   = -1;
173         }
174         return ret;
175 }
176
177 static ALWAYSINLINE int
178 vc_set_ipv4root_legacy_internal (
179         unsigned long ip[],
180         int nb,
181         unsigned long bcast,
182         unsigned long mask[])
183 {
184         init();
185         if (rev_ipv4root == 0){
186                 if (nb > 1){
187                         fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
188                 }
189                 return set_ipv4root_rev0 (ip[0]);
190         }else if (rev_ipv4root == 1){
191                 if (nb > 1){
192                         fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
193                 }
194                 return set_ipv4root_rev1 (ip[0],bcast);
195         }else if (rev_ipv4root == 2){
196                 return set_ipv4root_rev2 (ip,nb,bcast);
197         }else if (rev_ipv4root == 3){
198                 return set_ipv4root_rev3 (ip,nb,bcast,mask);
199         }
200         errno = EINVAL;
201         return -1;
202 }
203
204 static ALWAYSINLINE int
205 vc_set_ipv4root_legacy(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips)
206 {
207   unsigned long ip[nb];
208   unsigned long mask[nb];
209   size_t                i;
210
211   for (i=0; i<nb; ++i) {
212     ip[i]   = ips[i].ip;
213     mask[i] = ips[i].mask;
214   }
215
216   return vc_set_ipv4root_legacy_internal(ip, nb, bcast, mask);
217 }