aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtracelog/libtracelog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libtracelog/libtracelog.c')
-rw-r--r--src/libtracelog/libtracelog.c71
1 files changed, 53 insertions, 18 deletions
diff --git a/src/libtracelog/libtracelog.c b/src/libtracelog/libtracelog.c
index c3fd40a67..ff884c7d7 100644
--- a/src/libtracelog/libtracelog.c
+++ b/src/libtracelog/libtracelog.c
@@ -31,6 +31,7 @@
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <syslog.h> 32#include <syslog.h>
33#include <dirent.h> 33#include <dirent.h>
34#include <limits.h>
34 35
35//#define DEBUG 36//#define DEBUG
36 37
@@ -91,9 +92,9 @@ static void storage_add(const char *str) {
91 storage[h] = ptr; 92 storage[h] = ptr;
92} 93}
93 94
94char* cwd = NULL; // global variable for keeping current working directory 95// global variable to keep current working directory
95typedef int (*orig_chdir_t)(const char *pathname); 96static char* cwd = NULL;
96static orig_chdir_t orig_chdir = NULL; 97
97static char *storage_find(const char *str) { 98static char *storage_find(const char *str) {
98#ifdef DEBUG 99#ifdef DEBUG
99 printf("storage find %s\n", str); 100 printf("storage find %s\n", str);
@@ -107,17 +108,23 @@ static char *storage_find(const char *str) {
107 const char *tofind = str; 108 const char *tofind = str;
108 int allocated = 0; 109 int allocated = 0;
109 110
110 if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0]!='/') { 111 if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0] != '/') {
111 if (!orig_chdir) 112 if (cwd != NULL && str[0] != '/') {
112 orig_chdir = (orig_chdir_t)dlsym(RTLD_NEXT, "chdir"); 113 char *fullpath=malloc(PATH_MAX);
113 if (!orig_chdir(cwd)) { 114 if (!fullpath) {
114#ifdef DEBUG 115 fprintf(stderr, "Error: cannot allocate memory\n");
115 printf("chdir failed\n"); 116 return NULL;
116#endif 117 }
117 return NULL; 118 if (snprintf(fullpath, PATH_MAX, "%s/%s", cwd, str)<3) {
119 fprintf(stderr, "Error: snprintf failed\n");
120 free(fullpath);
121 return NULL;
122 }
123 tofind = realpath(fullpath, NULL);
124 free(fullpath);
125 } else {
126 tofind = realpath(str, NULL);
118 } 127 }
119
120 tofind = realpath(str, NULL);
121 if (!tofind) { 128 if (!tofind) {
122#ifdef DEBUG 129#ifdef DEBUG
123 printf("realpath failed\n"); 130 printf("realpath failed\n");
@@ -556,7 +563,7 @@ int stat64(const char *pathname, struct stat64 *buf) {
556#ifdef DEBUG 563#ifdef DEBUG
557 printf("%s %s\n", __FUNCTION__, pathname); 564 printf("%s %s\n", __FUNCTION__, pathname);
558#endif 565#endif
559 if (!orig_stat) 566 if (!orig_stat64)
560 orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64"); 567 orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64");
561 if (!blacklist_loaded) 568 if (!blacklist_loaded)
562 load_blacklist(); 569 load_blacklist();
@@ -592,7 +599,7 @@ int lstat64(const char *pathname, struct stat64 *buf) {
592#ifdef DEBUG 599#ifdef DEBUG
593 printf("%s %s\n", __FUNCTION__, pathname); 600 printf("%s %s\n", __FUNCTION__, pathname);
594#endif 601#endif
595 if (!orig_lstat) 602 if (!orig_lstat64)
596 orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64"); 603 orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64");
597 if (!blacklist_loaded) 604 if (!blacklist_loaded)
598 load_blacklist(); 605 load_blacklist();
@@ -641,9 +648,8 @@ DIR *opendir(const char *pathname) {
641} 648}
642 649
643// chdir 650// chdir
644// definition of orig_chdir placed before storage_find function 651typedef int (*orig_chdir_t)(const char *pathname);
645//typedef int (*orig_chdir_t)(const char *pathname); 652static orig_chdir_t orig_chdir = NULL;
646//static orig_chdir_t orig_chdir = NULL;
647int chdir(const char *pathname) { 653int chdir(const char *pathname) {
648#ifdef DEBUG 654#ifdef DEBUG
649 printf("%s %s\n", __FUNCTION__, pathname); 655 printf("%s %s\n", __FUNCTION__, pathname);
@@ -662,3 +668,32 @@ int chdir(const char *pathname) {
662 int rv = orig_chdir(pathname); 668 int rv = orig_chdir(pathname);
663 return rv; 669 return rv;
664} 670}
671
672// fchdir
673typedef int (*orig_fchdir_t)(int fd);
674static orig_fchdir_t orig_fchdir = NULL;
675int fchdir(int fd) {
676#ifdef DEBUG
677 printf("%s %d\n", __FUNCTION__, fd);
678#endif
679 if (!orig_fchdir)
680 orig_fchdir = (orig_fchdir_t)dlsym(RTLD_NEXT, "fchdir");
681
682 free(cwd);
683 char *pathname=malloc(PATH_MAX);
684 if (pathname) {
685 if (snprintf(pathname,PATH_MAX,"/proc/self/fd/%d", fd)>0) {
686 cwd = realpath(pathname, NULL);
687 } else {
688 cwd = NULL;
689 fprintf(stderr, "Error: snprintf failed\n");
690 }
691 free(pathname);
692 } else {
693 fprintf(stderr, "Error: cannot allocate memory\n");
694 cwd = NULL;
695 }
696
697 int rv = orig_fchdir(fd);
698 return rv;
699}