3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
19 #ifndef H_UTIL_VSERVER_SRC_WRAPPERS_H
20 #define H_UTIL_VSERVER_SRC_WRAPPERS_H
23 #include "compat-pivot_root.h"
33 #include <sys/types.h>
36 #include <sys/mount.h>
37 #include <sys/ioctl.h>
39 #define WRAPPER_DECL UNUSED ALWAYSINLINE
42 FatalErrnoError(bool condition, char const msg[]) /*@*/
44 extern int wrapper_exit_code;
46 if (!condition) return;
49 exit(wrapper_exit_code);
52 inline static WRAPPER_DECL void
55 FatalErrnoError(close(s)==-1, "close()");
58 inline static WRAPPER_DECL void
59 Echdir(char const path[])
61 FatalErrnoError(chdir(path)==-1, "chdir()");
64 inline static WRAPPER_DECL void
67 FatalErrnoError(fchdir(fd)==-1, "fchdir()");
70 inline static WRAPPER_DECL void
71 Echroot(char const path[])
73 FatalErrnoError(chroot(path)==-1, "chroot()");
76 inline static WRAPPER_DECL void
77 Eexecv(char const *path, char *argv[])
79 FatalErrnoError(execv(path,argv)==-1, "execv()");
82 inline static WRAPPER_DECL int
83 Eopen(char const *fname, int flags, mode_t mode)
85 int res = open(fname, flags, mode);
86 FatalErrnoError(res==-1, "open()");
91 inline static WRAPPER_DECL void
92 Eumount2(char const *path, int flag)
94 FatalErrnoError(umount2(path,flag)==-1, "umount2()");
97 inline static WRAPPER_DECL void
98 Emount(const char *source, const char *target,
99 const char *filesystemtype, unsigned long mountflags,
102 FatalErrnoError(mount(source, target, filesystemtype,
103 mountflags, data)==-1, "mount()");
106 inline static WRAPPER_DECL void
107 Emkdir(const char *pathname, mode_t mode)
109 FatalErrnoError(mkdir(pathname,mode)==-1, "mkdir()");
112 inline static WRAPPER_DECL void
113 Epivot_root(const char *new_root, const char *put_old)
115 FatalErrnoError(pivot_root(new_root, put_old)==-1, "pivot_root()");
118 inline static WRAPPER_DECL pid_t
119 Eclone(int (*fn)(void *), void *child_stack, int flags, void *arg)
123 res = clone(fn, child_stack, flags, arg);
125 res = clone((void*(*)(void*))(fn), child_stack, flags, arg);
127 FatalErrnoError(res==-1, "clone()");
132 inline static WRAPPER_DECL pid_t
133 Ewait4(pid_t pid, int *status, int options,
134 struct rusage *rusage)
137 res = wait4(pid, status, options, rusage);
138 FatalErrnoError(res==-1, "wait4()");
142 inline static WRAPPER_DECL void
143 Epipe(int filedes[2])
145 FatalErrnoError(pipe(filedes)==-1, "pipe()");
148 inline static WRAPPER_DECL pid_t
153 FatalErrnoError(res==-1, "fork()");
157 inline static WRAPPER_DECL size_t
158 Eread(int fd, void *ptr, size_t len)
160 size_t res = read(fd, ptr, len);
161 FatalErrnoError((ssize_t)(res)==-1, "read()");
166 inline static WRAPPER_DECL size_t
167 Ewrite(int fd, void const *ptr, size_t len)
169 size_t res = write(fd, ptr, len);
170 FatalErrnoError((ssize_t)(res)==-1, "write()");
175 inline static WRAPPER_DECL void
176 Ereadlink(const char *path, char *buf, size_t bufsiz)
178 FatalErrnoError(readlink(path, buf, bufsiz)==-1, "readlink()");
181 inline static WRAPPER_DECL void
182 Esymlink(const char *oldpath, const char *newpath)
184 FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
187 inline static WRAPPER_DECL void
188 Eunlink(char const *pathname)
190 FatalErrnoError(unlink(pathname)==-1, "unlink()");
193 inline static WRAPPER_DECL void
194 Egetrlimit(int resource, struct rlimit *rlim)
196 FatalErrnoError(getrlimit(resource, rlim)==-1, "getrlimit()");
199 inline static WRAPPER_DECL void
200 Esetrlimit(int resource, struct rlimit const *rlim)
202 FatalErrnoError(setrlimit(resource, rlim)==-1, "setrlimit()");
208 FatalErrnoError(setuid(uid)==-1, "setuid()");
214 FatalErrnoError(setgid(gid)==-1, "setgid()");
218 Esetgroups(size_t size, const gid_t *list)
220 FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
223 inline static WRAPPER_DECL int
224 Edup2(int oldfd, int newfd)
226 register int res = dup2(oldfd, newfd);
227 FatalErrnoError(res==-1, "dup2()");
232 inline static WRAPPER_DECL void *
235 register void *res = malloc(size);
236 FatalErrnoError(res==0 && size!=0, "malloc()");
241 inline static WRAPPER_DECL /*@null@*//*@only@*/ void *
242 Erealloc(/*@only@*//*@out@*//*@null@*/ void *ptr,
244 /*@ensures maxSet(result) == new_size@*/
247 register void *res = realloc(ptr, new_size);
248 FatalErrnoError(res==0 && new_size!=0, "realloc()");
253 inline static WRAPPER_DECL off_t
254 Elseek(int fildes, off_t offset, int whence)
256 off_t res = lseek(fildes, offset, whence);
257 FatalErrnoError(res==(off_t)-1, "lseek()");
261 inline static WRAPPER_DECL int
262 Emkstemp(char *template)
264 int res = mkstemp(template);
265 FatalErrnoError(res==-1, "mkstemp()");
269 inline static WRAPPER_DECL void
270 Eioctl(int fd, int request, void *p)
272 int res = ioctl(fd, request, p);
273 FatalErrnoError(res<0, "ioctl()");
276 inline static WRAPPER_DECL pid_t
279 register pid_t const res = setsid();
280 FatalErrnoError(res==-1, "setsid()");
287 #endif // H_UTIL_VSERVER_SRC_WRAPPERS_H