Elseek(), Emkstemp(): added
[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
38 #define WRAPPER_DECL    UNUSED ALWAYSINLINE
39
40 static UNUSED void 
41 FatalErrnoError(bool condition, char const msg[]) /*@*/
42 {
43   extern int    wrapper_exit_code;
44
45   if (!condition)       return;
46   perror(msg);
47
48   exit(wrapper_exit_code);
49 }
50
51 inline static WRAPPER_DECL void
52 Eclose(int s)
53 {
54   FatalErrnoError(close(s)==-1, "close()");
55 }
56
57 inline static WRAPPER_DECL void
58 Echdir(char const path[])
59 {
60   FatalErrnoError(chdir(path)==-1, "chdir()");
61 }
62
63 inline static WRAPPER_DECL void
64 Efchdir(int fd)
65 {
66   FatalErrnoError(fchdir(fd)==-1, "fchdir()");
67 }
68
69 inline static WRAPPER_DECL void
70 Echroot(char const path[])
71 {
72   FatalErrnoError(chroot(path)==-1, "chroot()");
73 }
74
75 inline static WRAPPER_DECL void
76 Eexecv(char const *path, char *argv[])
77 {
78   FatalErrnoError(execv(path,argv)==-1, "execv()");
79 }
80
81 inline static WRAPPER_DECL int
82 Eopen(char const *fname, int flags, mode_t mode)
83 {
84   int   res = open(fname, flags, mode);
85   FatalErrnoError(res==-1, "open()");
86
87   return res;
88 }
89
90 inline static WRAPPER_DECL void
91 Eumount2(char const *path, int flag)
92 {
93   FatalErrnoError(umount2(path,flag)==-1, "umount2()");
94 }
95
96 inline static WRAPPER_DECL void
97 Emount(const char *source, const char *target,
98        const char *filesystemtype, unsigned long mountflags,
99        const void *data)
100 {
101   FatalErrnoError(mount(source, target, filesystemtype,
102                         mountflags, data)==-1, "mount()");
103 }
104
105 inline static WRAPPER_DECL void
106 Emkdir(const char *pathname, mode_t mode)
107 {
108   FatalErrnoError(mkdir(pathname,mode)==-1, "mkdir()");
109 }
110
111 inline static WRAPPER_DECL void
112 Epivot_root(const char *new_root, const char *put_old)
113 {
114   FatalErrnoError(pivot_root(new_root, put_old)==-1, "pivot_root()");
115 }
116
117 inline static WRAPPER_DECL pid_t
118 Eclone(int (*fn)(void *), void *child_stack, int flags, void *arg)
119 {
120   pid_t         res;
121 #ifndef __dietlibc__
122   res = clone(fn, child_stack, flags, arg);
123 #else
124   res = clone((void*(*)(void*))(fn), child_stack, flags, arg);
125 #endif
126   FatalErrnoError(res==-1, "clone()");
127   return res;
128 }
129
130
131 inline static WRAPPER_DECL pid_t
132 Ewait4(pid_t pid, int *status, int options,
133        struct rusage *rusage)
134 {
135   pid_t         res;
136   res = wait4(pid, status, options, rusage);
137   FatalErrnoError(res==-1, "wait4()");
138   return res;
139 }
140
141 inline static WRAPPER_DECL void
142 Epipe(int filedes[2])
143 {
144   FatalErrnoError(pipe(filedes)==-1, "pipe()");
145 }
146
147 inline static WRAPPER_DECL pid_t
148 Efork()
149 {
150   pid_t         res;
151   res = fork();
152   FatalErrnoError(res==-1, "fork()");
153   return res;
154 }
155
156 inline static WRAPPER_DECL size_t
157 Eread(int fd, void *ptr, size_t len)
158 {
159   size_t        res = read(fd, ptr, len);
160   FatalErrnoError((ssize_t)(res)==-1, "read()");
161
162   return res;
163 }
164
165 inline static WRAPPER_DECL size_t
166 Ewrite(int fd, void const *ptr, size_t len)
167 {
168   size_t        res = write(fd, ptr, len);
169   FatalErrnoError((ssize_t)(res)==-1, "write()");
170
171   return res;
172 }
173
174 inline static WRAPPER_DECL void
175 Ereadlink(const char *path, char *buf, size_t bufsiz)
176 {
177   FatalErrnoError(readlink(path, buf, bufsiz)==-1, "readlink()");
178 }
179
180 inline static WRAPPER_DECL void
181 Esymlink(const char *oldpath, const char *newpath)
182 {
183   FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
184 }
185
186 inline static WRAPPER_DECL void
187 Eunlink(char const *pathname)
188 {
189   FatalErrnoError(unlink(pathname)==-1, "unlink()");
190 }
191
192 inline static WRAPPER_DECL void
193 Egetrlimit(int resource, struct rlimit *rlim)
194 {
195   FatalErrnoError(getrlimit(resource, rlim)==-1, "getrlimit()");
196 }
197
198 inline static WRAPPER_DECL void
199 Esetrlimit(int resource, struct rlimit const *rlim)
200 {
201   FatalErrnoError(setrlimit(resource, rlim)==-1, "setrlimit()");
202 }
203
204 inline static void
205 Esetuid(uid_t uid)
206 {
207   FatalErrnoError(setuid(uid)==-1, "setuid()");
208 }
209
210 inline static void
211 Esetgid(gid_t gid)
212 {
213   FatalErrnoError(setgid(gid)==-1, "setgid()");
214 }
215
216 inline static void
217 Esetgroups(size_t size, const gid_t *list)
218 {
219   FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
220 }
221
222 inline static WRAPPER_DECL int
223 Edup2(int oldfd, int newfd)
224 {
225   register int          res = dup2(oldfd, newfd);
226   FatalErrnoError(res==-1, "dup2()");
227
228   return res;
229 }
230
231 inline static WRAPPER_DECL void *
232 Emalloc(size_t size)
233 {
234   register void               *res = malloc(size);
235   FatalErrnoError(res==0 && size!=0, "malloc()");
236   return res;
237 }
238
239 /*@unused@*/
240 inline static WRAPPER_DECL /*@null@*//*@only@*/ void *
241 Erealloc(/*@only@*//*@out@*//*@null@*/ void *ptr,
242          size_t new_size)
243     /*@ensures maxSet(result) == new_size@*/
244     /*@modifies *ptr@*/
245 {
246   register void         *res = realloc(ptr, new_size);
247   FatalErrnoError(res==0 && new_size!=0, "realloc()");
248
249   return res;
250 }
251
252 inline static WRAPPER_DECL off_t
253 Elseek(int fildes, off_t offset, int whence)
254 {
255   off_t         res = lseek(fildes, offset, whence);
256   FatalErrnoError(res==(off_t)-1, "lseek()");
257   return res;
258 }
259
260 inline static WRAPPER_DECL int
261 Emkstemp(char *template)
262 {
263   int           res = mkstemp(template);
264   FatalErrnoError(res==-1, "mkstemp()");
265   return res;
266 }
267
268 #undef WRAPPER_DECL
269
270 #endif  //  H_UTIL_VSERVER_SRC_WRAPPERS_H