1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/***********************************************************************
* log.c: Handles the system calls that create files and logs them.
***********************************************************************
* This file is part of the package paco
* Copyright (C) 2004-2009 David Rosal
* For more information visit http://paco.sourceforge.net
***********************************************************************/
#include "config.h"
#include <dirent.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#define __have_64__ (HAVE_OPEN64 && HAVE_CREAT64 && HAVE_TRUNCATE64 \
&& HAVE_FOPEN64 && HAVE_FREOPEN64)
#define CHECK_INIT do { \
if (!lp_tmpfile) lp_init(); \
} while (0)
#define PACO_BUFSIZE 4096
static int (*libc_creat) (const char*, mode_t);
static int (*libc_link) (const char*, const char*);
static int (*libc_open) (const char*, int, ...);
static int (*libc_rename) (const char*, const char*);
static int (*libc_symlink) (const char*, const char*);
static int (*libc_truncate) (const char*, off_t);
static FILE*(*libc_fopen) (const char*, const char*);
static FILE*(*libc_freopen) (const char*, const char*, FILE*);
#if __have_64__
static int (*libc_creat64) (const char*, mode_t);
static int (*libc_open64) (const char*, int, ...);
static int (*libc_truncate64) (const char*, off64_t);
static FILE*(*libc_fopen64) (const char*, const char*);
static FILE*(*libc_freopen64) (const char*, const char*, FILE*);
#endif /* __have_64__ */
static char* lp_tmpfile;
static int lp_debug;
static void lp_die(const char* fmt, ...)
{
va_list ap;
fflush(stdout);
fputs("libpaco-log: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
exit(EXIT_FAILURE);
}
static void* lp_dlsym(const char* symbol)
{
void* ret;
char* error;
dlerror();
if (!(ret = dlsym(RTLD_NEXT, symbol))) {
error = (char*)dlerror();
lp_die("dlsym(%p, \"%s\"): %s", RTLD_NEXT, symbol,
error ? error : "failed");
}
return ret;
}
static void lp_init()
{
static char* dbg = NULL;
/* handle libc */
libc_creat = lp_dlsym("creat");
libc_link = lp_dlsym("link");
libc_open = lp_dlsym("open");
libc_rename = lp_dlsym("rename");
libc_symlink = lp_dlsym("symlink");
libc_truncate = lp_dlsym("truncate");
libc_fopen = lp_dlsym("fopen");
libc_freopen = lp_dlsym("freopen");
#if __have_64__
libc_open64 = lp_dlsym("open64");
libc_creat64 = lp_dlsym("creat64");
libc_truncate64 = lp_dlsym("truncate64");
libc_fopen64 = lp_dlsym("fopen64");
libc_freopen64 = lp_dlsym("freopen64");
#endif /* __have_64__ */
/* read the environment */
if (!lp_tmpfile && !(lp_tmpfile = getenv("PACO_TMPFILE")))
lp_die("variable %s undefined", "PACO_TMPFILE"); \
if (!dbg && (dbg = getenv("PACO_DEBUG")))
lp_debug = !strcmp(dbg, "yes");
}
static void lp_log(const char* path, const char* fmt, ...)
{
static char abs_path[PACO_BUFSIZE];
va_list a;
int fd, len, __errno = errno;
if (!strcmp(path, "/dev/tty") || !strcmp(path, "/dev/null") ||
!strncmp(path, "/proc/", 6))
goto ____end;
CHECK_INIT;
if (lp_debug) {
fflush(stdout);
fprintf(stderr, "paco :: ");
va_start(a, fmt);
vfprintf(stderr, fmt, a);
va_end(a);
putc('\n', stderr);
}
/* "Absolutize" relative paths */
if (path[0] == '/') {
strncpy(abs_path, path, PACO_BUFSIZE - 1);
abs_path[PACO_BUFSIZE - 1] = '\0';
}
else if (getcwd(abs_path, PACO_BUFSIZE)) {
strncat(abs_path, "/", PACO_BUFSIZE - strlen(abs_path) - 1);
strncat(abs_path, path, PACO_BUFSIZE - strlen(abs_path) - 1);
}
else
snprintf(abs_path, PACO_BUFSIZE, "./%s", path);
strncat(abs_path, "\n", PACO_BUFSIZE - strlen(abs_path) - 1);
if ((fd = libc_open(lp_tmpfile, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0)
lp_die("open(\"%s\"): %s", lp_tmpfile, strerror(errno));
len = strlen(abs_path);
if (write(fd, abs_path, len) != len)
lp_die("%s: write(): %s", lp_tmpfile, strerror(errno));
if (close(fd) < 0)
lp_die("close(%d): %s", fd, strerror(errno));
____end:
errno = __errno;
}
/************************/
/* System call handlers */
/************************/
FILE* fopen(const char* path, const char* mode)
{
FILE* ret;
CHECK_INIT;
ret = libc_fopen(path, mode);
if (ret && strpbrk(mode, "wa+"))
lp_log(path, "fopen(\"%s\", \"%s\")", path, mode);
return ret;
}
FILE* freopen(const char* path, const char* mode, FILE* stream)
{
FILE* ret;
CHECK_INIT;
ret = libc_freopen(path, mode, stream);
if (ret && strpbrk(mode, "wa+"))
lp_log(path, "freopen(\"%s\", \"%s\")", path, mode);
return ret;
}
/*
* If NEWBUF isn't a directory write it to the log, otherwise log files it and
* its subdirectories contain.
*/
static void log_rename(const char* oldpath, const char* newpath)
{
char oldbuf[PACO_BUFSIZE], newbuf[PACO_BUFSIZE];
struct stat st;
DIR* dir;
struct dirent* e;
size_t oldlen, newlen;
int __errno = errno; /* save global errno */
/* The NEWpath file doesn't exist. */
if (-1 == lstat(newpath, &st))
goto ____end;
else if (!S_ISDIR(st.st_mode)) {
/* NEWpath is a file or a symlink. */
lp_log(newpath, "rename(\"%s\", \"%s\")", oldpath, newpath);
goto ____end;
}
/* Make sure we have enough space for the following slashes. */
oldlen = strlen(oldpath);
newlen = strlen(newpath);
if (oldlen + 2 >= PACO_BUFSIZE || newlen + 2 >= PACO_BUFSIZE)
goto ____end;
strcpy(oldbuf, oldpath);
strcpy(newbuf, newpath);
newbuf[PACO_BUFSIZE - 1] = oldbuf[PACO_BUFSIZE - 1] = '\0';
/* We can do this in the loop below, buf it's more efficient to do
that once. These slashes will separate the path NEWBUF/OLDBUF
contains from names of its files/subdirectories. */
oldbuf[oldlen++] = newbuf[newlen++] = '/';
oldbuf[oldlen] = newbuf[newlen] = '\0';
dir = opendir(newbuf);
while ((e = readdir(dir))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
strncat(oldbuf, e->d_name, PACO_BUFSIZE - oldlen - 1);
strncat(newbuf, e->d_name, PACO_BUFSIZE - newlen - 1);
log_rename(oldbuf, newbuf);
oldbuf[oldlen] = newbuf[newlen] = '\0';
}
closedir(dir);
____end:
/* Restore global errno */
errno = __errno;
}
int rename(const char* oldpath, const char* newpath)
{
int ret;
CHECK_INIT;
if ((ret = libc_rename(oldpath, newpath)) != -1)
log_rename(oldpath, newpath);
return ret;
}
int creat(const char* path, mode_t mode)
{
int ret;
CHECK_INIT;
ret = libc_open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
if (ret != -1)
lp_log(path, "creat(\"%s\", 0%o)", path, (int)mode);
return ret;
}
int link(const char* oldpath, const char* newpath)
{
int ret;
CHECK_INIT;
if ((ret = libc_link(oldpath, newpath)) != -1)
lp_log(newpath, "link(\"%s\", \"%s\")", oldpath, newpath);
return ret;
}
int truncate(const char* path, off_t length)
{
int ret;
CHECK_INIT;
if ((ret = libc_truncate(path, length)) != -1)
lp_log(path, "truncate(\"%s\", %d)", path, (int)length);
return ret;
}
int open(const char* path, int flags, ...)
{
va_list a;
mode_t mode;
int accmode, ret;
CHECK_INIT;
va_start(a, flags);
mode = va_arg(a, mode_t);
va_end(a);
if ((ret = libc_open(path, flags, mode)) != -1) {
accmode = flags & O_ACCMODE;
if (accmode == O_WRONLY || accmode == O_RDWR)
lp_log(path, "open(\"%s\")", path);
}
return ret;
}
int symlink(const char* oldpath, const char* newpath)
{
int ret;
CHECK_INIT;
if ((ret = libc_symlink(oldpath, newpath)) != -1)
lp_log(newpath, "symlink(\"%s\", \"%s\")", oldpath, newpath);
return ret;
}
#if __have_64__
int creat64(const char* path, mode_t mode)
{
int ret;
CHECK_INIT;
if ((ret = libc_open64(path, O_CREAT | O_WRONLY | O_TRUNC, mode)) != -1)
lp_log(path, "creat64(\"%s\")", path);
return ret;
}
int open64(const char* path, int flags, ...)
{
va_list a;
mode_t mode;
int accmode, ret;
CHECK_INIT;
va_start(a, flags);
mode = va_arg(a, mode_t);
va_end(a);
if ((ret = libc_open64(path, flags, mode)) != -1) {
accmode = flags & O_ACCMODE;
if (accmode == O_WRONLY || accmode == O_RDWR)
lp_log(path, "open64(\"%s\")", path);
}
return ret;
}
int truncate64(const char* path, off64_t length)
{
int ret;
CHECK_INIT;
if ((ret = libc_truncate64(path, length)) != -1)
lp_log(path, "truncate64(\"%s\", %d)", path, (int)length);
return ret;
}
FILE* fopen64(const char* path, const char* mode)
{
FILE* ret;
CHECK_INIT;
ret = libc_fopen64(path, mode);
if (ret && strpbrk(mode, "wa+"))
lp_log(path, "fopen64(\"%s\", \"%s\")", path, mode);
return ret;
}
FILE* freopen64(const char* path, const char* mode, FILE* stream)
{
FILE* ret;
CHECK_INIT;
ret = libc_freopen64(path, mode, stream);
if (ret && strpbrk(mode, "wa+"))
lp_log(path, "freopen64(\"%s\", \"%s\")", path, mode);
return ret;
}
#endif /* __have_64__ */
|