X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fcano%2Fold%2Fcano3.c;fp=c%2Fcano%2Fold%2Fcano3.c;h=68ebefb0d9f4f8def0c3e253cbad9887dc423197;hb=d3b2d1a75c5c62e4d7142124162e2deb8273b32b;hp=0000000000000000000000000000000000000000;hpb=b9a41889280efffe1d21fb094e175f0f204ea039;p=mirrors%2FPrograms.git diff --git a/c/cano/old/cano3.c b/c/cano/old/cano3.c new file mode 100644 index 0000000..68ebefb --- /dev/null +++ b/c/cano/old/cano3.c @@ -0,0 +1,99 @@ +#define _GNU_SOURCE +/* +#include +#include +#include +#include +#include +#include + + #include + #include + #include +*/ +#include +#include +#include +#include + +#define i { srand(23); fprintf(stderr,"%s();\n",__func__); } +//Catch open() calls (while redirecting filename): + +static const char *fd_to_path(int fd) { + static char path[PATH_MAX]; + sprintf(path,"/proc/self/fd/%d",fd); + int l = (int) readlink((char * __restrict__) path, path, PATH_MAX); + path[l]=0; + return path; +} + +int close(int fd) { + //fputs(fd_to_path(fd),stderr); + fprintf(stderr,"%s(%d,%s);\n",__func__,fd,fd_to_path(fd)); + if(fd>2) return syscall(SYS_close,fd); +} +int fclose(FILE *fd) { + //fputs(fd_to_path(fd),stderr); + fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fileno(fd),fd,fd_to_path(fileno(fd))); + static ssize_t (*fclose_orig)(FILE *fd); + if (!fclose_orig) { + fclose_orig = dlsym(RTLD_NEXT, "fclose"); + } + return fclose_orig(fd); + //return syscall(SYS_close,fileno(fd)); +} +/* +ssize_t write(int fd, const void *buf, size_t count) { + //fputs(fd_to_path(fd),stderr); + printf("%d",fd); + i; + return syscall(SYS_write,fd,buf,count); +} +*/ +/* +static const char *redirect_name(const char *name) +{ + if( + (strcmp(name,"/dev/random") == 0) || + (strcmp(name,"/dev/urandom") == 0) + ) { + printf("REDIRECT HIT: %s\n", name); + return "/dev/zero"; + } + return name; +} + + +int open(const char *filename, int flags, ...) +{ + //srand(23); + static int (*open_orig)(const char *, int, mode_t); + int ret; + va_list ap; + mode_t mode; + + if (!open_orig) { + open_orig = dlsym(RTLD_NEXT, "open"); + } + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + + if(strcmp(filename,"/tmp/lol.txt.asc") != 0) { + ret = open_orig(redirect_name(filename), flags, mode); + //ret = syscall(SYS_open,redirect_name(filename),flags,mode); + } else { + if(flags & O_RDONLY) { + ret = fileno(popen("cat aaaa", "r")); + } else { + ret = fileno(popen("tee aaaa", "w")); + } + } + + printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); + + return ret; +} + +*/