rewrote and enhanced it
[util-vserver.git] / util-vserver / src / wrappers.h
1 // $Id$    --*- c++ -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifndef H_UTIL_VSERVER_SRC_WRAPPERS_H
20 #define H_UTIL_VSERVER_SRC_WRAPPERS_H
21
22 #include "compat.h"
23 #include "compat-pivot_root.h"
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <sched.h>
32 #include <grp.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <sys/stat.h>
36 #include <sys/mount.h>
37 #include <sys/ioctl.h>
38
39 #define WRAPPER_DECL    UNUSED ALWAYSINLINE
40
41 static UNUSED void 
42 FatalErrnoError(bool condition, char const msg[]) /*@*/
43 {
44   extern int    wrapper_exit_code;
45
46   if (!condition)       return;
47   perror(msg);
48
49   exit(wrapper_exit_code);
50 }
51
52 inline static WRAPPER_DECL void
53 Eclose(int s)
54 {
55   FatalErrnoError(close(s)==-1, "close()");
56 }
57
58 inline static WRAPPER_DECL void
59 Echdir(char const path[])
60 {
61   FatalErrnoError(chdir(path)==-1, "chdir()");
62 }
63
64 inline static WRAPPER_DECL void
65 Efchdir(int fd)
66 {
67   FatalErrnoError(fchdir(fd)==-1, "fchdir()");
68 }
69
70 inline static WRAPPER_DECL void
71 Echroot(char const path[])
72 {
73   FatalErrnoError(chroot(path)==-1, "chroot()");
74 }
75
76 inline static WRAPPER_DECL void
77 Eexecv(char const *path, char *argv[])
78 {
79   FatalErrnoError(execv(path,argv)==-1, "execv()");
80 }
81
82 inline static WRAPPER_DECL int
83 Eopen(char const *fname, int flags, mode_t mode)
84 {
85   int   res = open(fname, flags, mode);
86   FatalErrnoError(res==-1, "open()");
87
88   return res;
89 }
90
91 inline static WRAPPER_DECL void
92 Eumount2(char const *path, int flag)
93 {
94   FatalErrnoError(umount2(path,flag)==-1, "umount2()");
95 }
96
97 inline static WRAPPER_DECL void
98 Emount(const char *source, const char *target,
99        const char *filesystemtype, unsigned long mountflags,
100        const void *data)
101 {
102   FatalErrnoError(mount(source, target, filesystemtype,
103                         mountflags, data)==-1, "mount()");
104 }
105
106 inline static WRAPPER_DECL void
107 Emkdir(const char *pathname, mode_t mode)
108 {
109   FatalErrnoError(mkdir(pathname,mode)==-1, "mkdir()");
110 }
111
112 inline static WRAPPER_DECL void
113 Epivot_root(const char *new_root, const char *put_old)
114 {
115   FatalErrnoError(pivot_root(new_root, put_old)==-1, "pivot_root()");
116 }
117
118 inline static WRAPPER_DECL pid_t
119 Eclone(int (*fn)(void *), void *child_stack, int flags, void *arg)
120 {
121   pid_t         res;
122 #ifndef __dietlibc__
123   res = clone(fn, child_stack, flags, arg);
124 #else
125   res = clone((void*(*)(void*))(fn), child_stack, flags, arg);
126 #endif
127   FatalErrnoError(res==-1, "clone()");
128   return res;
129 }
130
131
132 inline static WRAPPER_DECL pid_t
133 Ewait4(pid_t pid, int *status, int options,
134        struct rusage *rusage)
135 {
136   pid_t         res;
137   res = wait4(pid, status, options, rusage);
138   FatalErrnoError(res==-1, "wait4()");
139   return res;
140 }
141
142 inline static WRAPPER_DECL void
143 Epipe(int filedes[2])
144 {
145   FatalErrnoError(pipe(filedes)==-1, "pipe()");
146 }
147
148 inline static WRAPPER_DECL pid_t
149 Efork()
150 {
151   pid_t         res;
152   res = fork();
153   FatalErrnoError(res==-1, "fork()");
154   return res;
155 }
156
157 inline static WRAPPER_DECL size_t
158 Eread(int fd, void *ptr, size_t len)
159 {
160   size_t        res = read(fd, ptr, len);
161   FatalErrnoError((ssize_t)(res)==-1, "read()");
162
163   return res;
164 }
165
166 inline static WRAPPER_DECL size_t
167 Ewrite(int fd, void const *ptr, size_t len)
168 {
169   size_t        res = write(fd, ptr, len);
170   FatalErrnoError((ssize_t)(res)==-1, "write()");
171
172   return res;
173 }
174
175 inline static WRAPPER_DECL void
176 Ereadlink(const char *path, char *buf, size_t bufsiz)
177 {
178   FatalErrnoError(readlink(path, buf, bufsiz)==-1, "readlink()");
179 }
180
181 inline static WRAPPER_DECL void
182 Esymlink(const char *oldpath, const char *newpath)
183 {
184   FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
185 }
186
187 inline static WRAPPER_DECL void
188 Eunlink(char const *pathname)
189 {
190   FatalErrnoError(unlink(pathname)==-1, "unlink()");
191 }
192
193 inline static WRAPPER_DECL void
194 Egetrlimit(int resource, struct rlimit *rlim)
195 {
196   FatalErrnoError(getrlimit(resource, rlim)==-1, "getrlimit()");
197 }
198
199 inline static WRAPPER_DECL void
200 Esetrlimit(int resource, struct rlimit const *rlim)
201 {
202   FatalErrnoError(setrlimit(resource, rlim)==-1, "setrlimit()");
203 }
204
205 inline static void
206 Esetuid(uid_t uid)
207 {
208   FatalErrnoError(setuid(uid)==-1, "setuid()");
209 }
210
211 inline static void
212 Esetgid(gid_t gid)
213 {
214   FatalErrnoError(setgid(gid)==-1, "setgid()");
215 }
216
217 inline static void
218 Esetgroups(size_t size, const gid_t *list)
219 {
220   FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
221 }
222
223 inline static WRAPPER_DECL int
224 Edup2(int oldfd, int newfd)
225 {
226   register int          res = dup2(oldfd, newfd);
227   FatalErrnoError(res==-1, "dup2()");
228
229   return res;
230 }
231
232 inline static WRAPPER_DECL void *
233 Emalloc(size_t size)
234 {
235   register void               *res = malloc(size);
236   FatalErrnoError(res==0 && size!=0, "malloc()");
237   return res;
238 }
239
240 /*@unused@*/
241 inline static WRAPPER_DECL /*@null@*//*@only@*/ void *
242 Erealloc(/*@only@*//*@out@*//*@null@*/ void *ptr,
243          size_t new_size)
244     /*@ensures maxSet(result) == new_size@*/
245     /*@modifies *ptr@*/
246 {
247   register void         *res = realloc(ptr, new_size);
248   FatalErrnoError(res==0 && new_size!=0, "realloc()");
249
250   return res;
251 }
252
253 inline static WRAPPER_DECL off_t
254 Elseek(int fildes, off_t offset, int whence)
255 {
256   off_t         res = lseek(fildes, offset, whence);
257   FatalErrnoError(res==(off_t)-1, "lseek()");
258   return res;
259 }
260
261 inline static WRAPPER_DECL int
262 Emkstemp(char *template)
263 {
264   int           res = mkstemp(template);
265   FatalErrnoError(res==-1, "mkstemp()");
266   return res;
267 }
268
269 inline static WRAPPER_DECL void
270 Eioctl(int fd, int request, void *p)
271 {
272   int   res = ioctl(fd, request, p);
273   FatalErrnoError(res<0, "ioctl()");
274 }
275
276 #undef WRAPPER_DECL
277
278 #endif  //  H_UTIL_VSERVER_SRC_WRAPPERS_H