Changeset 2468 in kBuild for trunk/src/lib/restartable-syscall-wrappers.c
- Timestamp:
- Jul 12, 2011 12:50:50 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/restartable-syscall-wrappers.c
r2449 r2468 38 38 #include <sys/types.h> 39 39 #include <sys/stat.h> 40 #include <utime.h> 40 41 #include <dlfcn.h> 41 42 #include <errno.h> … … 80 81 #define XSTR(x) XSTR_INNER(x) 81 82 82 83 84 extern int WRAP64(open)(const char *pszName, int fFlags, ...);85 int open(const char *pszName, int fFlags, ...)86 {87 mode_t fMode;88 va_list va;89 int fd;90 91 va_start(va, fFlags);92 fMode = va_arg(va, mode_t);93 va_end(va);94 95 do96 fd = WRAP64(open)(pszName, fFlags, fMode);97 while (fd == -1 && SHOULD_RESTART());98 return fd;99 }100 101 102 #if !defined(KBUILD_OS_LINUX) /* no wrapper */103 extern int WRAP(mkdir)(const char *pszName, mode_t fMode);104 int mkdir(const char *pszName, mode_t fMode)105 {106 int rc;107 do108 rc = WRAP(mkdir)(pszName, fMode);109 while (rc == -1 && SHOULD_RESTART());110 return rc;111 }112 #endif113 114 extern int WRAP64(stat)(const char *pszName, struct stat *pStBuf);115 int stat(const char *pszName, struct stat *pStBuf)116 {117 int rc;118 do119 rc = WRAP64(stat)(pszName, pStBuf);120 while (rc == -1 && SHOULD_RESTART());121 return rc;122 }123 124 extern int WRAP64(lstat)(const char *pszName, struct stat *pStBuf);125 int lstat(const char *pszName, struct stat *pStBuf)126 {127 int rc;128 do129 rc = WRAP64(lstat)(pszName, pStBuf);130 while (rc == -1 && SHOULD_RESTART());131 return rc;132 }133 134 extern ssize_t WRAP(read)(int fd, void *pvBuf, size_t cbBuf);135 ssize_t read(int fd, void *pvBuf, size_t cbBuf)136 {137 ssize_t cbRead;138 do139 cbRead = WRAP(read)(fd, pvBuf, cbBuf);140 while (cbRead == -1 && SHOULD_RESTART());141 return cbRead;142 }143 144 extern ssize_t WRAP(write)(int fd, void *pvBuf, size_t cbBuf);145 ssize_t write(int fd, void *pvBuf, size_t cbBuf)146 {147 ssize_t cbWritten;148 do149 cbWritten = WRAP(write)(fd, pvBuf, cbBuf);150 while (cbWritten == -1 && SHOULD_RESTART());151 return cbWritten;152 }153 83 154 84 static int dlsym_libc(const char *pszSymbol, void **ppvSym) … … 213 143 } 214 144 215 #undef fopen 216 FILE *fopen(const char *pszName, const char *pszMode) 145 146 147 int open(const char *pszPath, int fFlags, ...) 217 148 { 149 mode_t fMode; 150 va_list va; 151 int fd; 218 152 static union 219 153 { 220 FILE *(* pfnFOpen)(const char *, const char *);154 int (* pfnReal)(const char *, int, mode_t); 221 155 void *pvSym; 222 156 } s_u; 223 FILE *pFile; 224 225 if ( !s_u.pfnFOpen 226 && dlsym_libc("fopen", &s_u.pvSym) != 0) 227 return NULL; 157 158 if ( !s_u.pfnReal 159 && dlsym_libc("open", &s_u.pvSym) != 0) 160 return -1; 161 162 va_start(va, fFlags); 163 fMode = va_arg(va, mode_t); 164 va_end(va); 228 165 229 166 do 230 pFile = s_u.pfnFOpen(pszName, pszMode);231 while ( !pFile&& SHOULD_RESTART());232 return pFile;167 fd = s_u.pfnReal(pszPath, fFlags, fMode); 168 while (fd == -1 && SHOULD_RESTART()); 169 return fd; 233 170 } 234 171 235 #undef fopen64 236 FILE *fopen64(const char *pszName, const char *pszMode) 172 int open64(const char *pszPath, int fFlags, ...) 237 173 { 174 mode_t fMode; 175 va_list va; 176 int fd; 238 177 static union 239 178 { 240 FILE *(* pfnFOpen64)(const char *, const char *);179 int (* pfnReal)(const char *, int, mode_t); 241 180 void *pvSym; 242 181 } s_u; 243 FILE *pFile; 244 245 if ( !s_u.pfnFOpen64 246 && dlsym_libc("fopen64", &s_u.pvSym) != 0) 247 return NULL; 182 183 if ( !s_u.pfnReal 184 && dlsym_libc("open64", &s_u.pvSym) != 0) 185 return -1; 186 187 va_start(va, fFlags); 188 fMode = va_arg(va, mode_t); 189 va_end(va); 248 190 249 191 do 250 pFile = s_u.pfnFOpen64(pszName, pszMode);251 while ( !pFile&& SHOULD_RESTART());252 return pFile;192 fd = s_u.pfnReal(pszPath, fFlags, fMode); 193 while (fd == -1 && SHOULD_RESTART()); 194 return fd; 253 195 } 254 196 255 /** @todo chmod, chown, chgrp, times, and possible some more. */ 256 197 #define WRAP_FN(a_Name, a_ParamsWithTypes, a_ParamsNoType, a_RetType, a_RetFailed) \ 198 a_RetType a_Name a_ParamsWithTypes \ 199 { \ 200 static union \ 201 { \ 202 a_RetType (* pfnReal) a_ParamsWithTypes; \ 203 void *pvSym; \ 204 } s_u; \ 205 a_RetType rc; \ 206 \ 207 if ( !s_u.pfnReal \ 208 && dlsym_libc(#a_Name, &s_u.pvSym) != 0) \ 209 return a_RetFailed; \ 210 \ 211 do \ 212 rc = s_u.pfnReal a_ParamsNoType; \ 213 while (rc == a_RetFailed && SHOULD_RESTART()); \ 214 return rc; \ 215 } typedef int ignore_semi_colon_##a_Name 216 217 #undef mkdir 218 WRAP_FN(mkdir, (const char *pszPath, mode_t fMode), (pszPath, fMode), int, -1); 219 220 #undef rmdir 221 WRAP_FN(rmdir, (const char *pszPath, mode_t fMode), (pszPath, fMode), int, -1); 222 223 #undef unlink 224 WRAP_FN(unlink, (const char *pszPath), (pszPath), int, -1); 225 226 #undef remove 227 WRAP_FN(remove, (const char *pszPath), (pszPath), int, -1); 228 229 #undef symlink 230 WRAP_FN(symlink, (const char *pszFrom, const char *pszTo), (pszFrom, pszTo), int, -1); 231 232 #undef link 233 WRAP_FN(link, (const char *pszFrom, const char *pszTo), (pszFrom, pszTo), int, -1); 234 235 #undef stat 236 WRAP_FN(stat, (const char *pszPath, struct stat *pStBuf), (pszPath, pStBuf), int, -1); 237 #undef stat64 238 WRAP_FN(stat64, (const char *pszPath, struct stat *pStBuf), (pszPath, pStBuf), int, -1); 239 240 #undef lstat 241 WRAP_FN(lstat, (const char *pszPath, struct stat *pStBuf), (pszPath, pStBuf), int, -1); 242 #undef lstat64 243 WRAP_FN(lstat64, (const char *pszPath, struct stat *pStBuf), (pszPath, pStBuf), int, -1); 244 245 #undef read 246 WRAP_FN(read, (int fd, void *pvBuf, size_t cbBuf), (fd, pvBuf, cbBuf), ssize_t, -1); 247 248 #undef write 249 WRAP_FN(write, (int fd, void *pvBuf, size_t cbBuf), (fd, pvBuf, cbBuf), ssize_t, -1); 250 251 #undef fopen 252 WRAP_FN(fopen, (const char *pszPath, const char *pszMode), (pszPath, pszMode), FILE *, NULL); 253 #undef fopen64 254 WRAP_FN(fopen64, (const char *pszPath, const char *pszMode), (pszPath, pszMode), FILE *, NULL); 255 256 #undef chmod 257 WRAP_FN(chmod, (const char *pszPath, mode_t fMode), (pszPath, fMode), int, -1); 258 #undef lchmod 259 WRAP_FN(lchmod, (const char *pszPath, mode_t fMode), (pszPath, fMode), int, -1); 260 261 #undef chown 262 WRAP_FN(chown, (const char *pszPath, uid_t uid, gid_t gid), (pszPath, uid, gid), int, -1); 263 #undef lchown 264 WRAP_FN(lchown, (const char *pszPath, uid_t uid, gid_t gid), (pszPath, uid, gid), int, -1); 265 266 #undef utime 267 WRAP_FN(utime, (const char *pszPath, const struct utimbuf *pTimes), (pszPath, pTimes), int, -1); 268 269 #undef utimes 270 WRAP_FN(utimes, (const char *pszPath, const struct timeval *paTimes), (pszPath, paTimes), int, -1); 271 272 #undef pathconf 273 WRAP_FN(pathconf, (const char *pszPath, int iCfgNm), (pszPath, iCfgNm), long, -1); 274 275
Note:
See TracChangeset
for help on using the changeset viewer.