3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on syscall.cc by Jacques Gelinas
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)
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.
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.
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).
25 We rely on /proc/self/status to find the syscall number.
27 If it is not there, we rely on adm/unistd.h.
29 If this file does not have those system calls (not a patched kernel source)
30 we rely on static values in this file.
37 #include <asm/unistd.h>
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 = 227;
46 #undef __NR_set_ipv4root
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;
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)
60 static int def_NR_new_s_context = 226;
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;
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)
69 #undef __NR_set_ctxlimit
70 static int __NR_set_ctxlimit=-1;
71 static int rev_set_ctxlimit=-1;
73 static _syscall2 (int, set_ctxlimit, int, resource, long, limit)
75 #undef __NR_chrootsafe
76 static int __NR_chrootsafe=-1;
77 static int rev_chrootsafe=-1;
79 static _syscall1 (int, chrootsafe, const char *, dir)
83 static int is_init = 0;
85 FILE *fin = fopen ("/proc/self/status","r");
86 __NR_set_ipv4root_rev0 = def_NR_set_ipv4root;
87 __NR_set_ipv4root_rev1 = def_NR_set_ipv4root;
88 __NR_set_ipv4root_rev2 = def_NR_set_ipv4root;
89 __NR_set_ipv4root_rev3 = def_NR_set_ipv4root;
90 __NR_new_s_context_rev1 = def_NR_new_s_context;
93 while (fgets(line,sizeof(line)-1,fin)!=NULL){
95 char title[100],rev[100];
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);
106 }else if (strcmp(title,"__NR_set_ctxlimit:")==0){
107 __NR_set_ctxlimit = num;
108 if (strncmp(rev,"rev",3)==0){
109 rev_set_ctxlimit = atoi(rev+3);
111 }else if (strcmp(title,"__NR_chrootsafe:")==0){
112 __NR_chrootsafe = num;
113 if (strncmp(rev,"rev",3)==0){
114 rev_chrootsafe = atoi(rev+3);
116 }else if (strcmp(title,"__NR_new_s_context:")==0){
117 __NR_new_s_context_rev0 = num;
118 __NR_new_s_context_rev1 = num;
119 if (strncmp(rev,"rev",3)==0){
120 rev_s_context = atoi(rev+3);
136 int call_new_s_context(int nbctx, int ctxs[], int remove_cap, int flags)
140 if (rev_s_context == 0){
143 fprintf (stderr,"The current kernel does not support new_s_context revision 1\n");
144 }else if (nbctx == 0){
145 ret = new_s_context_rev0(-2,remove_cap,flags);
146 }else if (nbctx == 1){
147 ret = new_s_context_rev0(ctxs[0],remove_cap,flags);
150 fprintf (stderr,"new_s_context rev1 %d %d\n",nbctx,ctxs[0]);
151 ret = new_s_context_rev1(nbctx,ctxs,remove_cap,flags);
156 int call_set_ipv4root (
160 unsigned long mask[])
163 if (rev_ipv4root == 0){
165 fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
167 return set_ipv4root_rev0 (ip[0]);
168 }else if (rev_ipv4root == 1){
170 fprintf (stderr,"set_ipv4root: Several IP number specified, but this kernel only supports one. Ignored\n");
172 return set_ipv4root_rev1 (ip[0],bcast);
173 }else if (rev_ipv4root == 2){
174 return set_ipv4root_rev2 (ip,nb,bcast);
175 }else if (rev_ipv4root == 3){
176 return set_ipv4root_rev3 (ip,nb,bcast,mask);
182 int call_chrootsafe (const char *dir)
185 if (rev_chrootsafe == -1){
186 fprintf (stderr,"chrootsafe: Unsupported system call, update kernel\n");
187 }else if (rev_chrootsafe == 0){
188 return chrootsafe (dir);
190 fprintf (stderr,"chrootsafe: kernel support version %d, application expects version 0\n"
198 Return != 0 if chrootsafe is available
203 return rev_chrootsafe != -1;
206 int call_set_ctxlimit (int res, long limit)
209 if (rev_set_ctxlimit == -1){
210 fprintf (stderr,"set_ctxlimit: Unsupported system call, update kernel\n");
211 }else if (rev_set_ctxlimit == 0){
212 return set_ctxlimit (res,limit);
214 fprintf (stderr,"set_ctxlimit: kernel support version %d, application expects version 0\n"