make it compilable with non-C99 compilers
[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 <sys/types.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <sys/mount.h>
36
37 #define WRAPPER_DECL    UNUSED ALWAYSINLINE
38
39 static UNUSED void 
40 FatalErrnoError(bool condition, char const msg[]) /*@*/
41 {
42   extern int    wrapper_exit_code;
43
44   if (!condition)       return;
45   perror(msg);
46
47   exit(wrapper_exit_code);
48 }
49
50 inline static WRAPPER_DECL void
51 Eclose(int s)
52 {
53   FatalErrnoError(close(s)==-1, "close()");
54 }
55
56 inline static WRAPPER_DECL void
57 Echdir(char const path[])
58 {
59   FatalErrnoError(chdir(path)==-1, "chdir()");
60 }
61
62 inline static WRAPPER_DECL void
63 Efchdir(int fd)
64 {
65   FatalErrnoError(fchdir(fd)==-1, "fchdir()");
66 }
67
68 inline static WRAPPER_DECL void
69 Echroot(char const path[])
70 {
71   FatalErrnoError(chroot(path)==-1, "chroot()");
72 }
73
74 inline static WRAPPER_DECL void
75 Eexecv(char const *path, char *argv[])
76 {
77   FatalErrnoError(execv(path,argv)==-1, "execv()");
78 }
79
80 inline static WRAPPER_DECL int
81 Eopen(char const *fname, int flags, mode_t mode)
82 {
83   int   res = open(fname, flags, mode);
84   FatalErrnoError(res==-1, "open()");
85
86   return res;
87 }
88
89 inline static WRAPPER_DECL void
90 Eumount2(char const *path, int flag)
91 {
92   FatalErrnoError(umount2(path,flag)==-1, "umount2()");
93 }
94
95 inline static WRAPPER_DECL void
96 Emount(const char *source, const char *target,
97        const char *filesystemtype, unsigned long mountflags,
98        const void *data)
99 {
100   FatalErrnoError(mount(source, target, filesystemtype,
101                         mountflags, data)==-1, "mount()");
102 }
103
104 inline static WRAPPER_DECL void
105 Emkdir(const char *pathname, mode_t mode)
106 {
107   FatalErrnoError(mkdir(pathname,mode)==-1, "mkdir()");
108 }
109
110 inline static WRAPPER_DECL void
111 Epivot_root(const char *new_root, const char *put_old)
112 {
113   FatalErrnoError(pivot_root(new_root, put_old)==-1, "pivot_root()");
114 }
115
116 inline static WRAPPER_DECL pid_t
117 Eclone(int (*fn)(void *), void *child_stack, int flags, void *arg)
118 {
119   pid_t         res;
120 #ifndef __dietlibc__
121   res = clone(fn, child_stack, flags, arg);
122 #else
123   res = clone((void*(*)(void*))(fn), child_stack, flags, arg);
124 #endif
125   FatalErrnoError(res==-1, "clone()");
126   return res;
127 }
128
129
130 inline static WRAPPER_DECL pid_t
131 Ewait4(pid_t pid, int *status, int options,
132        struct rusage *rusage)
133 {
134   pid_t         res;
135   res = wait4(pid, status, options, rusage);
136   FatalErrnoError(res==-1, "wait4()");
137   return res;
138 }
139
140 inline static WRAPPER_DECL void
141 Epipe(int filedes[2])
142 {
143   FatalErrnoError(pipe(filedes)==-1, "pipe()");
144 }
145
146 inline static WRAPPER_DECL pid_t
147 Efork()
148 {
149   pid_t         res;
150   res = fork();
151   FatalErrnoError(res==-1, "fork()");
152   return res;
153 }
154
155 inline static WRAPPER_DECL size_t
156 Eread(int fd, void *ptr, size_t len)
157 {
158   size_t        res = read(fd, ptr, len);
159   FatalErrnoError((ssize_t)(res)==-1, "read()");
160
161   return res;
162 }
163
164 inline static WRAPPER_DECL size_t
165 Ewrite(int fd, void const *ptr, size_t len)
166 {
167   size_t        res = write(fd, ptr, len);
168   FatalErrnoError((ssize_t)(res)==-1, "write()");
169
170   return res;
171 }
172
173 inline static WRAPPER_DECL void
174 Ereadlink(const char *path, char *buf, size_t bufsiz)
175 {
176   FatalErrnoError(readlink(path, buf, bufsiz)==-1, "readlink()");
177 }
178
179 inline static WRAPPER_DECL void
180 Esymlink(const char *oldpath, const char *newpath)
181 {
182   FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
183 }
184
185 inline static WRAPPER_DECL void
186 Eunlink(char const *pathname)
187 {
188   FatalErrnoError(unlink(pathname)==-1, "unlink()");
189 }
190
191 inline static WRAPPER_DECL void
192 Egetrlimit(int resource, struct rlimit *rlim)
193 {
194   FatalErrnoError(getrlimit(resource, rlim)==-1, "getrlimit()");
195 }
196
197 inline static WRAPPER_DECL void
198 Esetrlimit(int resource, struct rlimit const *rlim)
199 {
200   FatalErrnoError(setrlimit(resource, rlim)==-1, "setrlimit()");
201 }
202
203
204 #undef WRAPPER_DECL
205
206 #endif  //  H_UTIL_VSERVER_SRC_WRAPPERS_H