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