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>
38 #define WRAPPER_DECL UNUSED ALWAYSINLINE
41 FatalErrnoError(bool condition, char const msg[]) /*@*/
43 extern int wrapper_exit_code;
45 if (!condition) return;
48 exit(wrapper_exit_code);
51 inline static WRAPPER_DECL void
54 FatalErrnoError(close(s)==-1, "close()");
57 inline static WRAPPER_DECL void
58 Echdir(char const path[])
60 FatalErrnoError(chdir(path)==-1, "chdir()");
63 inline static WRAPPER_DECL void
66 FatalErrnoError(fchdir(fd)==-1, "fchdir()");
69 inline static WRAPPER_DECL void
70 Echroot(char const path[])
72 FatalErrnoError(chroot(path)==-1, "chroot()");
75 inline static WRAPPER_DECL void
76 Eexecv(char const *path, char *argv[])
78 FatalErrnoError(execv(path,argv)==-1, "execv()");
81 inline static WRAPPER_DECL int
82 Eopen(char const *fname, int flags, mode_t mode)
84 int res = open(fname, flags, mode);
85 FatalErrnoError(res==-1, "open()");
90 inline static WRAPPER_DECL void
91 Eumount2(char const *path, int flag)
93 FatalErrnoError(umount2(path,flag)==-1, "umount2()");
96 inline static WRAPPER_DECL void
97 Emount(const char *source, const char *target,
98 const char *filesystemtype, unsigned long mountflags,
101 FatalErrnoError(mount(source, target, filesystemtype,
102 mountflags, data)==-1, "mount()");
105 inline static WRAPPER_DECL void
106 Emkdir(const char *pathname, mode_t mode)
108 FatalErrnoError(mkdir(pathname,mode)==-1, "mkdir()");
111 inline static WRAPPER_DECL void
112 Epivot_root(const char *new_root, const char *put_old)
114 FatalErrnoError(pivot_root(new_root, put_old)==-1, "pivot_root()");
117 inline static WRAPPER_DECL pid_t
118 Eclone(int (*fn)(void *), void *child_stack, int flags, void *arg)
122 res = clone(fn, child_stack, flags, arg);
124 res = clone((void*(*)(void*))(fn), child_stack, flags, arg);
126 FatalErrnoError(res==-1, "clone()");
131 inline static WRAPPER_DECL pid_t
132 Ewait4(pid_t pid, int *status, int options,
133 struct rusage *rusage)
136 res = wait4(pid, status, options, rusage);
137 FatalErrnoError(res==-1, "wait4()");
141 inline static WRAPPER_DECL void
142 Epipe(int filedes[2])
144 FatalErrnoError(pipe(filedes)==-1, "pipe()");
147 inline static WRAPPER_DECL pid_t
152 FatalErrnoError(res==-1, "fork()");
156 inline static WRAPPER_DECL size_t
157 Eread(int fd, void *ptr, size_t len)
159 size_t res = read(fd, ptr, len);
160 FatalErrnoError((ssize_t)(res)==-1, "read()");
165 inline static WRAPPER_DECL size_t
166 Ewrite(int fd, void const *ptr, size_t len)
168 size_t res = write(fd, ptr, len);
169 FatalErrnoError((ssize_t)(res)==-1, "write()");
174 inline static WRAPPER_DECL void
175 Ereadlink(const char *path, char *buf, size_t bufsiz)
177 FatalErrnoError(readlink(path, buf, bufsiz)==-1, "readlink()");
180 inline static WRAPPER_DECL void
181 Esymlink(const char *oldpath, const char *newpath)
183 FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
186 inline static WRAPPER_DECL void
187 Eunlink(char const *pathname)
189 FatalErrnoError(unlink(pathname)==-1, "unlink()");
192 inline static WRAPPER_DECL void
193 Egetrlimit(int resource, struct rlimit *rlim)
195 FatalErrnoError(getrlimit(resource, rlim)==-1, "getrlimit()");
198 inline static WRAPPER_DECL void
199 Esetrlimit(int resource, struct rlimit const *rlim)
201 FatalErrnoError(setrlimit(resource, rlim)==-1, "setrlimit()");
207 FatalErrnoError(setuid(uid)==-1, "setuid()");
213 FatalErrnoError(setgid(gid)==-1, "setgid()");
217 Esetgroups(size_t size, const gid_t *list)
219 FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
222 inline static WRAPPER_DECL int
223 Edup2(int oldfd, int newfd)
225 register int res = dup2(oldfd, newfd);
226 FatalErrnoError(res==-1, "dup2()");
231 inline static UNUSED void *
234 register void *res = malloc(size);
235 FatalErrnoError(res==0 && size!=0, "malloc()");
240 inline static /*@null@*//*@only@*/ void *
241 Erealloc(/*@only@*//*@out@*//*@null@*/ void *ptr,
243 /*@ensures maxSet(result) == new_size@*/
246 register void *res = realloc(ptr, new_size);
247 FatalErrnoError(res==0 && new_size!=0, "realloc()");
254 #endif // H_UTIL_VSERVER_SRC_WRAPPERS_H