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