mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.5 KiB

  1. #!/bin/bash
  2. #
  3. # This shell-script generates the fl_wrapper.c source file.
  4. cat << EOT
  5. /* ROCK Linux Wrapper for getting a list of created files
  6. *
  7. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  8. *
  9. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  10. * Please add additional copyright information _after_ the line containing
  11. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  12. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  13. *
  14. * ROCK Linux: rock-src/misc/tools-source/fl_wrapper.c.sh
  15. * ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version. A copy of the GNU General Public
  21. * License can be found at Documentation/COPYING.
  22. *
  23. * Many people helped and are helping developing ROCK Linux. Please
  24. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  25. * file for details.
  26. *
  27. * --- ROCK-COPYRIGHT-NOTE-END ---
  28. *
  29. * Copyright (C) 2004-2005 The T2 SDE Project
  30. *
  31. * gcc -Wall -O2 -ldl -shared -o fl_wrapper.so fl_wrapper.c
  32. *
  33. * !!! THIS FILE IS AUTO-GENERATED BY $0 !!!
  34. *
  35. * ELF Dynamic Loading Documentation:
  36. * - http://www.linuxdoc.org/HOWTO/GCC-HOWTO-7.html
  37. * - http://www.educ.umu.se/~bjorn/mhonarc-files/linux-gcc/msg00576.html
  38. * - /usr/include/dlfcn.h
  39. */
  40. /* Headers and prototypes */
  41. #define DEBUG 1
  42. #define DLOPEN_LIBC 1
  43. #define FD_TRACKER 1
  44. #define _GNU_SOURCE
  45. #define _REENTRANT
  46. #define open xxx_open
  47. #define open64 xxx_open64
  48. #define mknod xxx_mknod
  49. #define _LARGEFILE_SOURCE
  50. #define _LARGEFILE64_SOURCE
  51. # include <dlfcn.h>
  52. # include <errno.h>
  53. # include <fcntl.h>
  54. # include <stdio.h>
  55. # include <stdlib.h>
  56. # include <string.h>
  57. # include <sys/stat.h>
  58. # include <sys/types.h>
  59. # include <unistd.h>
  60. # include <utime.h>
  61. # include <stdarg.h>
  62. # include <sched.h>
  63. #undef _LARGEFILE64_SOURCE
  64. #undef _LARGEFILE_SOURCE
  65. #undef mknod
  66. #undef open
  67. #undef open64
  68. static void * get_dl_symbol(char *);
  69. struct status_t {
  70. ino_t inode;
  71. off_t size;
  72. time_t mtime;
  73. time_t ctime;
  74. };
  75. static void handle_file_access_before(const char *, const char *, struct status_t *);
  76. static void handle_file_access_after(const char *, const char *, struct status_t *);
  77. char *wlog = 0, *rlog = 0, *cmdname = "unkown";
  78. # if DEBUG == 1
  79. int debug = 0;
  80. # endif
  81. /* Wrapper Functions */
  82. EOT
  83. # This has been made with cpp-macros before until they turned to be absolutely
  84. # unreadable ...
  85. #
  86. add_wrapper()
  87. {
  88. line="$( echo "$*" | sed 's/ *, */,/g' )"
  89. old_ifs="$IFS" ; IFS="," ; set $line ; IFS="$old_ifs"
  90. ret_type=$1 ; shift ; function=$1 ; shift
  91. p1="" ; p2="" ; for x ; do p1="$p1$x, " ; done
  92. for x ; do x="${x%%'[]'}" ; p2="$p2${x##* }, " ; done
  93. p1="${p1%, }" ; p2="${p2%, }"
  94. if [ "${function#exec}" = "${function}" ]
  95. then
  96. echo ; cat << EOT
  97. extern $ret_type $function($p1);
  98. $ret_type (*orig_$function)($p1) = 0;
  99. $ret_type $function($p1)
  100. {
  101. struct status_t status;
  102. int old_errno=errno;
  103. $ret_type rc;
  104. handle_file_access_before("$function", f, &status);
  105. if (!orig_$function) orig_$function = get_dl_symbol("$function");
  106. # if DEBUG == 1
  107. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original $function() at %p (wrapper is at %p).\n",
  108. getpid(), orig_$function, $function);
  109. # endif
  110. errno=old_errno;
  111. rc = orig_$function($p2);
  112. old_errno=errno;
  113. handle_file_access_after("$function", f, &status);
  114. errno=old_errno;
  115. return rc;
  116. }
  117. EOT
  118. else
  119. echo ; cat << EOT
  120. extern int fcntl(int fd, int cmd, ...);
  121. extern $ret_type $function($p1);
  122. $ret_type (*orig_$function)($p1) = 0;
  123. $ret_type $function($p1)
  124. {
  125. int old_errno=errno;
  126. int rc;
  127. handle_file_access_after("$function", f, 0);
  128. if (!orig_$function) orig_$function = get_dl_symbol("$function");
  129. if (!orig_fcntl) orig_fcntl = get_dl_symbol("fcntl");
  130. # if FD_TRACKER == 1
  131. struct pid_reg *pid = *find_pid(getpid());
  132. struct fd_reg ** fd_iter;
  133. if (pid)
  134. {
  135. fd_iter = &pid->fd_head;
  136. while (*fd_iter != 0)
  137. {
  138. (*fd_iter)->closed = orig_fcntl((*fd_iter)->fd, F_GETFD) & FD_CLOEXEC;
  139. fd_iter = &(*fd_iter)->next;
  140. }
  141. pid->executed = 1;
  142. }
  143. # endif
  144. # if DEBUG == 1
  145. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original $function() at %p (wrapper is at %p).\n",
  146. getpid(), orig_$function, $function);
  147. # endif
  148. errno=old_errno;
  149. rc = orig_$function($p2);
  150. old_errno=errno;
  151. # if FD_TRACKER == 1
  152. if (pid)
  153. {
  154. fd_iter = &pid->fd_head;
  155. while (*fd_iter != 0)
  156. {
  157. (*fd_iter)->closed = 0;
  158. fd_iter = &(*fd_iter)->next;
  159. }
  160. pid->executed = 0;
  161. }
  162. # endif
  163. errno=old_errno;
  164. return rc;
  165. }
  166. EOT
  167. fi
  168. }
  169. echo
  170. cat fd_tracker.c
  171. cat fl_wrapper_execl.c
  172. cat fl_wrapper_open.c
  173. cat fl_wrapper_close.c
  174. # add_wrapper 'FILE*, fopen, const char* f, const char* g'
  175. # add_wrapper 'FILE*, fopen64, const char* f, const char* g'
  176. add_wrapper 'int, mkdir, const char* f, mode_t m'
  177. add_wrapper 'int, mknod, const char* f, mode_t m, dev_t d'
  178. add_wrapper 'int, link, const char* s, const char* f'
  179. add_wrapper 'int, symlink, const char* s, const char* f'
  180. add_wrapper 'int, rename, const char* s, const char* f'
  181. add_wrapper 'int, utime, const char* f, const struct utimbuf* t'
  182. add_wrapper 'int, utimes, const char* f, struct timeval* t'
  183. add_wrapper 'int, execv, const char* f, char* const a[]'
  184. add_wrapper 'int, execve, const char* f, char* const a[], char* const e[]'
  185. echo
  186. cat fl_wrapper_internal.c