885641e9a7d429b93f1ac2fbd2ed985314d16734
[util-vserver.git] / ensc_wrappers / wrappers-unistd.hc
1 // $Id: wrappers-unistd.hc,v 1.9 2005/07/03 12:33:44 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2004 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 #ifndef H_ENSC_IN_WRAPPERS_H
19 #  error wrappers_handler.hc can not be used in this way
20 #endif
21
22 inline static WRAPPER_DECL void
23 Eclose(int s)
24 {
25   FatalErrnoError(close(s)==-1, "close()");
26 }
27
28 inline static WRAPPER_DECL void
29 Echdir(char const path[])
30 {
31   FatalErrnoError(chdir(path)==-1, "chdir()");
32 }
33
34 inline static WRAPPER_DECL void
35 Efchdir(int fd)
36 {
37   FatalErrnoError(fchdir(fd)==-1, "fchdir()");
38 }
39
40 inline static WRAPPER_DECL void
41 Echroot(char const path[])
42 {
43   FatalErrnoError(chroot(path)==-1, "chroot()");
44 }
45
46 inline static WRAPPER_DECL NORETURN void
47 Eexecv(char const *path, char *argv[])
48 {
49   execv(path,argv);
50   FatalErrnoErrorFail("execv()");
51 }
52
53 inline static WRAPPER_DECL NORETURN void
54 Eexecvp(char const *path, char *argv[])
55 {
56   execvp(path,argv);
57   FatalErrnoErrorFail("execvp()");
58 }
59
60 inline static WRAPPER_DECL NORETURN void
61 EexecvpD(char const *path, char *argv[])
62 {
63   execvp(path,argv);
64   {
65     ENSC_DETAIL1(msg, "execvp", path, 1);
66     FatalErrnoErrorFail(msg);
67   }
68 }
69
70 inline static WRAPPER_DECL void
71 Epipe(int filedes[2])
72 {
73   FatalErrnoError(pipe(filedes)==-1, "pipe()");
74 }
75
76 inline static WRAPPER_DECL pid_t
77 Efork()
78 {
79   pid_t         res;
80   res = fork();
81   FatalErrnoError(res==-1, "fork()");
82   return res;
83 }
84
85 inline static WRAPPER_DECL size_t
86 Eread(int fd, void *ptr, size_t len)
87 {
88   ssize_t       res = read(fd, ptr, len);
89   FatalErrnoError(res==-1, "read()");
90
91   return res;
92 }
93
94 inline static WRAPPER_DECL size_t
95 Ewrite(int fd, void const *ptr, size_t len)
96 {
97   ssize_t       res = write(fd, ptr, len);
98   FatalErrnoError(res==-1, "write()");
99
100   return res;
101 }
102
103 inline static WRAPPER_DECL size_t
104 Ereadlink(const char *path, char *buf, size_t bufsiz)
105 {
106   ssize_t       res = readlink(path, buf, bufsiz);
107   FatalErrnoError(res==-1, "readlink()");
108
109   return res;
110 }
111
112 inline static WRAPPER_DECL size_t
113 EreadlinkD(const char *path, char *buf, size_t bufsiz)
114 {
115   ssize_t       res = readlink(path, buf, bufsiz);
116   ENSC_DETAIL1(msg, "readlink", path, 1);
117   FatalErrnoError((ssize_t)(res)==-1, msg);
118
119   return res;
120 }
121
122 inline static WRAPPER_DECL void
123 Esymlink(const char *oldpath, const char *newpath)
124 {
125   FatalErrnoError(symlink(oldpath, newpath)==-1, "symlink()");
126 }
127
128 inline static WRAPPER_DECL void
129 EsymlinkD(const char *oldpath, const char *newpath)
130 {
131   ENSC_DETAIL2(msg, "symlink", oldpath, newpath, 1, 1);
132   FatalErrnoError(symlink(oldpath, newpath)==-1, msg);
133 }
134
135 inline static WRAPPER_DECL void
136 Eunlink(char const *pathname)
137 {
138   FatalErrnoError(unlink(pathname)==-1, "unlink()");
139 }
140
141 inline static void
142 Esetuid(uid_t uid)
143 {
144   FatalErrnoError(setuid(uid)==-1, "setuid()");
145 }
146
147 inline static void
148 Esetgid(gid_t gid)
149 {
150   FatalErrnoError(setgid(gid)==-1, "setgid()");
151 }
152
153 #if defined(_GRP_H) && (defined(__USE_BSD) || defined(__dietlibc__))
154 inline static void
155 Esetgroups(size_t size, const gid_t *list)
156 {
157   FatalErrnoError(setgroups(size, list)==-1, "setgroups()");
158 }
159 #endif
160
161 inline static WRAPPER_DECL int
162 Edup2(int oldfd, int newfd)
163 {
164   register int          res = dup2(oldfd, newfd);
165   FatalErrnoError(res==-1, "dup2()");
166
167   return res;
168 }
169
170 inline static WRAPPER_DECL int
171 Edup(int fd)
172 {
173   register int          res = dup(fd);
174   FatalErrnoError(res==-1, "dup()");
175
176   return res;
177 }
178
179 inline static WRAPPER_DECL pid_t
180 Esetsid()
181 {
182   register pid_t const  res = setsid();
183   FatalErrnoError(res==-1, "setsid()");
184
185   return res;
186 }
187
188 inline static WRAPPER_DECL int
189 Emkstemp(char *template)
190 {
191   int           res = mkstemp(template);
192   FatalErrnoError(res==-1, "mkstemp()");
193   return res;
194 }
195
196 inline static WRAPPER_DECL off_t
197 Elseek(int fildes, off_t offset, int whence)
198 {
199   off_t         res = lseek(fildes, offset, whence);
200   FatalErrnoError(res==(off_t)-1, "lseek()");
201   return res;
202 }
203
204 inline static WRAPPER_DECL void
205 Enice(int n)
206 {
207   FatalErrnoError(nice(n)==-1, "nice()");
208 }
209
210 inline static WRAPPER_DECL void
211 Etruncate(const char *path, off_t length)
212 {
213   FatalErrnoError(truncate(path,length)==-1, "truncate()");
214 }
215
216 inline static WRAPPER_DECL void
217 Eftruncate(int fd, off_t length)
218 {
219   FatalErrnoError(ftruncate(fd,length)==-1, "ftruncate()");
220 }