Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5.  
  6. static int teefd = -1;
  7. static int is_tty = -1;
  8. static size_t (*read_original)(int, void *, size_t);
  9.  
  10. static char fname_buf[200];
  11.  
  12. __attribute__((constructor))
  13. void ctor(void)
  14. {
  15.  
  16.   sprintf(fname_buf, "/tmp/log-more-stdin-%d.log", getpid());
  17.  
  18.     teefd = open(
  19.         fname_buf,
  20.         O_CREAT|O_WRONLY|O_TRUNC, 0666);
  21.  
  22.     // resolve symbol read
  23.     read_original = dlsym(RTLD_NEXT, "read");
  24.  
  25.     /* char *newargv[] = { "vendor/log-more-logger.rb", "hello", "world", NULL }; */
  26.     char *newenviron[] = { NULL };
  27.     int pid = fork();
  28.     if(pid == 0) {
  29.       int r = execle("vendor/log-more-logger.rb", "log-more-logger.rb",
  30.           NULL, newenviron);
  31.     }
  32. }
  33.  
  34. size_t read(int fd, void *buffer, size_t size)
  35. {
  36.     // invoke original read(...)
  37.     size_t count = read_original(fd, buffer, size);
  38.  
  39.     // if fd is STDIN and teefd is open, echo input
  40.     if(fd == 0 && teefd != -1 && isatty(fd))
  41.     {
  42.         write(teefd, buffer, count);
  43.         /* fsync(teefd); */
  44.     }
  45.  
  46.     // return result of read_original(...)
  47.     return count;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement