removed all traces of <stdio.h> usage
[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 <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <syscall.h>
38 #include <asm/unistd.h>
39 #include <stdbool.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 bool     is_init = false;
83
84 #include "utils-legacy.h"
85
86 #ifndef WRITE_MSG
87 #  define WRITE_MSG(FD,X)         (void)(write(FD,X,sizeof(X)-1))
88 #endif
89
90
91 static bool
92 getNumRevPair(char const *str, int *num, int *rev)
93 {
94   char const *  blank_pos = strchr(str, ' ');
95   char const *  eol_pos   = strchr(str, '\n');
96   
97   *num = atoi(str);
98   if (*num==0) return false;
99   
100   if (blank_pos!=0 && eol_pos!=0 && blank_pos<eol_pos &&
101       strncmp(blank_pos+1, "rev", 3)==0)
102     *rev = atoi(blank_pos+4);
103
104   return true;
105 }
106
107 #define SET_TAG_POS(TAG)                        \
108   pos = strstr(buf, (TAG));                     \
109   if (pos) pos+=sizeof(TAG)-1
110
111 static bool init_internal()
112 {
113   size_t                        bufsize = utilvserver_getProcEntryBufsize();
114   char                          buf[bufsize];
115   char const *                  pos = 0;
116   pid_t                         pid = getpid();
117   int                           num;
118
119   errno = 0;
120
121   pos=utilvserver_getProcEntry(pid, 0, buf, bufsize);
122   if (pos==0 && errno==EAGAIN) return false;
123   
124   SET_TAG_POS("\n__NR_set_ipv4root: ");
125   if ( pos!=0 && getNumRevPair(pos, &num, &rev_ipv4root) ) {
126     __NR_set_ipv4root_rev0 =
127       __NR_set_ipv4root_rev1 =
128       __NR_set_ipv4root_rev2 =
129       __NR_set_ipv4root_rev3 = num;
130   }
131
132 #if 0  
133   SET_TAG_POS("\n__NR_set_ctxlimit: ");
134   if ( pos!=0 && getNumRevPair(pos, &num, &rev_set_ctxlimit) )
135     __NR_set_ctxlimit = num;
136 #endif  
137
138   SET_TAG_POS("\n__NR_chrootsafe: ");
139   if ( pos!=0 && getNumRevPair(pos, &num, &rev_chrootsafe) )
140     __NR_chrootsafe   = num;
141
142   SET_TAG_POS("\n__NR_new_s_context: ");
143   if ( pos!=0 && getNumRevPair(pos, &num, &rev_s_context) )
144     __NR_new_s_context_rev0 = num;
145
146   return true;
147 }
148
149 #undef SET_TAG_POS
150
151 static void init()
152 {
153         if (!is_init){
154                 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
155                 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
156                 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
157                 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
158                 __NR_new_s_context_rev0 = def_NR_new_s_context;
159
160                 while (!init_internal() && errno==EAGAIN) {}
161
162                 is_init = true;
163         }
164 }
165
166 void vc_init_legacy()
167 {
168         init();
169 }
170
171 void vc_init_internal_legacy(int ctx_rev, int ctx_number,
172                              int ipv4_rev, int ipv4_number)
173 {       
174   rev_s_context           = ctx_rev;
175   __NR_new_s_context_rev0 = ctx_number;
176
177   rev_ipv4root            = ipv4_rev;
178   __NR_set_ipv4root_rev0  = ipv4_number;
179   __NR_set_ipv4root_rev1  = ipv4_number;
180   __NR_set_ipv4root_rev2  = ipv4_number;
181   __NR_set_ipv4root_rev3  = ipv4_number;
182
183   is_init = true;
184 }
185
186 static ALWAYSINLINE int
187 vc_new_s_context_legacy(int ctx, int remove_cap, int flags)
188 {
189         int ret = -1;
190         init();
191         if (rev_s_context == 0){
192                 return new_s_context_rev0(ctx, remove_cap, flags);
193         }else{
194                 errno = -ENOSYS;
195                 ret   = -1;
196         }
197         return ret;
198 }
199
200 static ALWAYSINLINE int
201 vc_set_ipv4root_legacy_internal (
202         unsigned long ip[],
203         int nb,
204         unsigned long bcast,
205         unsigned long mask[])
206 {
207         init();
208         if (rev_ipv4root == 0){
209                 if (nb > 1){
210                         WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
211                 }
212                 return set_ipv4root_rev0 (ip[0]);
213         }else if (rev_ipv4root == 1){
214                 if (nb > 1){
215                         WRITE_MSG(2,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
216                 }
217                 return set_ipv4root_rev1 (ip[0],bcast);
218         }else if (rev_ipv4root == 2){
219                 return set_ipv4root_rev2 (ip,nb,bcast);
220         }else if (rev_ipv4root == 3){
221                 return set_ipv4root_rev3 (ip,nb,bcast,mask);
222         }
223         errno = EINVAL;
224         return -1;
225 }
226
227 static ALWAYSINLINE int
228 vc_set_ipv4root_legacy(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips)
229 {
230   unsigned long ip[nb];
231   unsigned long mask[nb];
232   size_t                i;
233
234   for (i=0; i<nb; ++i) {
235     ip[i]   = ips[i].ip;
236     mask[i] = ips[i].mask;
237   }
238
239   return vc_set_ipv4root_legacy_internal(ip, nb, bcast, mask);
240 }
241
242 static ALWAYSINLINE int
243 vc_chrootsafe_legacy (const char *dir)
244 {
245         init();
246         if (rev_chrootsafe == -1){
247                 vc_tell_unsafe_chroot();
248                 return chroot(dir);
249         }else if (rev_chrootsafe == 0){
250                 return chrootsafe (dir);
251         }else{
252                 WRITE_MSG(2, "chrootsafe: kernel supports wrong version, application expects version 0\n");
253         }
254         errno = EINVAL;
255         return -1;
256 }
257
258 #if 0
259 /*
260         Return != 0 if chrootsafe is available
261 */
262 int has_chrootsafe()
263 {
264         init();
265         return rev_chrootsafe != -1;
266 }
267
268 int call_set_ctxlimit (int res, long limit)
269 {
270         init();
271         if (rev_set_ctxlimit == -1){
272                 fprintf (stderr,"set_ctxlimit: Unsupported system call, update kernel\n");
273         }else if (rev_set_ctxlimit == 0){
274                 return set_ctxlimit (res,limit);
275         }else{
276                 fprintf (stderr,"set_ctxlimit: kernel support version %d, application expects version 0\n"
277                         ,rev_set_ctxlimit);
278         }
279         errno = EINVAL;
280         return -1;
281 }
282
283
284 #endif