Advertisement
micromike

LD_PRELOAD example

Mar 21st, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. /*
  2.  * this define is important
  3.  * do not delete it or gcc will complain that RTLD_NEXT is undefined
  4.  * http://stackoverflow.com/questions/1777397/rtld-next-undeclared
  5.  */
  6. #define _GNU_SOURCE
  7.  
  8. #include <stdio.h>
  9. #include <dlfcn.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12.  
  13. int printf(const char *format, ...)
  14. {
  15.     va_list list;
  16.     char *parg;
  17.     /* function pointer for original printf */
  18.     typeof(printf) *old_printf;
  19.    
  20.     /* get the parameters */
  21.     va_start(list, format);
  22.     vasprintf(&parg, format, list);
  23.     va_end(list);
  24.  
  25.     /* what the log function should be */
  26.     puts("hooked printf success\n");
  27.  
  28.     /* call the original printf */
  29.     old_printf = dlsym(RTLD_NEXT, "printf");
  30.     (*old_printf)("%s", parg);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement