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