Merged with SYSCALL_SWITCH branch (sswitch_merge tag)
[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 <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
54 static _syscall1(int, set_ipv4root_rev0, unsigned long, ip)
55 static _syscall2(int, set_ipv4root_rev1, unsigned long, ip, unsigned long, bcast)
56 static _syscall3(int, set_ipv4root_rev2, unsigned long *, ip, int, nb, unsigned long, bcast)
57 static _syscall4(int, set_ipv4root_rev3, unsigned long *, ip, int, nb, unsigned long, bcast, unsigned long *, mask)
58
59 static int def_NR_new_s_context = 273;
60 #undef __NR_new_s_context
61 static int __NR_new_s_context_rev0;
62   //static int __NR_new_s_context_rev1;
63 static int rev_s_context=0;
64
65 static _syscall3(int, new_s_context_rev0, int, newctx, int, remove_cap, int, flags)
66     //static _syscall4(int, new_s_context_rev1, int, nbctx, int *, ctxs, int, remove_cap, int, flags)
67
68 #if 0
69 #undef __NR_set_ctxlimit
70 static int __NR_set_ctxlimit=-1;
71 static int rev_set_ctxlimit=-1;
72
73 static _syscall2 (int, set_ctxlimit, int, resource, long, limit)
74 #endif
75
76 #undef __NR_chrootsafe
77 static int __NR_chrootsafe=-1;
78 static int rev_chrootsafe=-1;
79
80 static _syscall1 (int, chrootsafe, const char *, dir)
81   
82 static void init()
83 {
84         static int is_init = 0;
85         if (!is_init){
86                 FILE *fin = fopen ("/proc/self/status","r");
87                 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
88                 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
89                 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
90                 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
91                 __NR_new_s_context_rev0 = def_NR_new_s_context;
92                   //__NR_new_s_context_rev1 = def_NR_new_s_context;
93                 if (fin != NULL){
94                         char line[100];
95                         while (fgets(line,sizeof(line)-1,fin)!=NULL){
96                                 int num;
97                                 char title[100],rev[100];
98                                 rev[0] = '\0';
99                                 if (sscanf(line,"%s %d %s",title,&num,rev)>=2){
100                                         if (strcmp(title,"__NR_set_ipv4root:")==0){
101                                                 __NR_set_ipv4root_rev0 = num;
102                                                 __NR_set_ipv4root_rev1 = num;
103                                                 __NR_set_ipv4root_rev2 = num;
104                                                 __NR_set_ipv4root_rev3 = num;
105                                                 if (strncmp(rev,"rev",3)==0){
106                                                         rev_ipv4root = atoi(rev+3);
107                                                 }
108 #if 0                                           
109                                         }else if (strcmp(title,"__NR_set_ctxlimit:")==0){
110                                                 __NR_set_ctxlimit = num;
111                                                 if (strncmp(rev,"rev",3)==0){
112                                                         rev_set_ctxlimit = atoi(rev+3);
113                                                 }
114 #endif                                          
115                                         }else if (strcmp(title,"__NR_chrootsafe:")==0){
116                                                 __NR_chrootsafe = num;
117                                                 if (strncmp(rev,"rev",3)==0){
118                                                         rev_chrootsafe = atoi(rev+3);
119                                                 }
120                                         }else if (strcmp(title,"__NR_new_s_context:")==0){
121                                                 __NR_new_s_context_rev0 = num;
122                                                   //__NR_new_s_context_rev1 = num;
123                                                 if (strncmp(rev,"rev",3)==0){
124                                                         rev_s_context = atoi(rev+3);
125                                                 }
126                                         }
127                                 }
128                         }
129                         fclose (fin);
130                 }
131                 is_init = 1;
132         }
133 }
134
135 void vc_init_legacy()
136 {
137         init();
138 }
139
140 static ALWAYSINLINE int
141 vc_new_s_context_legacy(int ctx, int remove_cap, int flags)
142 {
143         int ret = -1;
144         init();
145         if (rev_s_context == 0){
146                 return new_s_context_rev0(ctx, remove_cap, flags);
147         }else{
148                 errno = -ENOSYS;
149                 ret   = -1;
150         }
151         return ret;
152 }
153
154 static ALWAYSINLINE int
155 vc_set_ipv4root_legacy_internal (
156         unsigned long ip[],
157         int nb,
158         unsigned long bcast,
159         unsigned long mask[])
160 {
161         init();
162         if (rev_ipv4root == 0){
163                 if (nb > 1){
164                         fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
165                 }
166                 return set_ipv4root_rev0 (ip[0]);
167         }else if (rev_ipv4root == 1){
168                 if (nb > 1){
169                         fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
170                 }
171                 return set_ipv4root_rev1 (ip[0],bcast);
172         }else if (rev_ipv4root == 2){
173                 return set_ipv4root_rev2 (ip,nb,bcast);
174         }else if (rev_ipv4root == 3){
175                 return set_ipv4root_rev3 (ip,nb,bcast,mask);
176         }
177         errno = EINVAL;
178         return -1;
179 }
180
181 static ALWAYSINLINE int
182 vc_set_ipv4root_legacy(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips)
183 {
184   unsigned long ip[nb];
185   unsigned long mask[nb];
186   size_t                i;
187
188   for (i=0; i<nb; ++i) {
189     ip[i]   = ips[i].ip;
190     mask[i] = ips[i].mask;
191   }
192
193   return vc_set_ipv4root_legacy_internal(ip, nb, bcast, mask);
194 }
195
196 static ALWAYSINLINE int
197 vc_chrootsafe_legacy (const char *dir)
198 {
199         init();
200         if (rev_chrootsafe == -1){
201                 vc_tell_unsafe_chroot();
202                 return chroot(dir);
203         }else if (rev_chrootsafe == 0){
204                 return chrootsafe (dir);
205         }else{
206                 fprintf (stderr,"chrootsafe: kernel support version %d, application expects version 0\n"
207                         ,rev_chrootsafe);
208         }
209         errno = EINVAL;
210         return -1;
211 }
212
213 #if 0
214 /*
215         Return != 0 if chrootsafe is available
216 */
217 int has_chrootsafe()
218 {
219         init();
220         return rev_chrootsafe != -1;
221 }
222
223 int call_set_ctxlimit (int res, long limit)
224 {
225         init();
226         if (rev_set_ctxlimit == -1){
227                 fprintf (stderr,"set_ctxlimit: Unsupported system call, update kernel\n");
228         }else if (rev_set_ctxlimit == 0){
229                 return set_ctxlimit (res,limit);
230         }else{
231                 fprintf (stderr,"set_ctxlimit: kernel support version %d, application expects version 0\n"
232                         ,rev_set_ctxlimit);
233         }
234         errno = EINVAL;
235         return -1;
236 }
237
238
239 #endif