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.

457 lines
9.7 KiB

  1. /* ROCK Linux Wrapper for getting a list of created files
  2. *
  3. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  4. *
  5. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  6. * Please add additional copyright information _after_ the line containing
  7. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  8. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  9. *
  10. * ROCK Linux: rock-src/misc/tools-source/fl_wrapper_open.c
  11. * ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version. A copy of the GNU General Public
  17. * License can be found at Documentation/COPYING.
  18. *
  19. * Many people helped and are helping developing ROCK Linux. Please
  20. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  21. * file for details.
  22. *
  23. * --- ROCK-COPYRIGHT-NOTE-END ---
  24. */
  25. extern int open(const char* f, int a, ...);
  26. int (*orig_open)(const char* f, int a, ...) = 0;
  27. int open(const char* f, int a, ...)
  28. {
  29. struct status_t status;
  30. int old_errno=errno;
  31. int rc;
  32. handle_file_access_before("open", f, &status);
  33. if (!orig_open) orig_open = get_dl_symbol("open");
  34. errno=old_errno;
  35. # if DEBUG == 1
  36. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original open(\"%s\", ...) at %p (wrapper is at %p).\n",
  37. getpid(), f, orig_open, open);
  38. # endif
  39. if (a & O_CREAT)
  40. {
  41. va_list ap;
  42. mode_t b = 0;
  43. va_start(ap, a);
  44. b = va_arg(ap, mode_t);
  45. va_end(ap);
  46. rc = orig_open(f, a, b);
  47. }
  48. else
  49. rc = orig_open(f, a);
  50. old_errno=errno;
  51. # if FD_TRACKER == 1
  52. if (rc != -1)
  53. {
  54. char *buf = 0;
  55. int free_buf = 0;
  56. if (f[0] != '/')
  57. {
  58. char *buf2 = get_current_dir_name();
  59. if (asprintf(&buf,"%s%s%s", buf2,
  60. strcmp(buf2,"/") ? "/" : "", f) != -1)
  61. {
  62. f = buf; free_buf = 1;
  63. }
  64. free(buf2);
  65. }
  66. register_fd(rc, f, &status);
  67. if (free_buf) free(buf);
  68. }
  69. # else
  70. handle_file_access_after("open", f, &status);
  71. # endif
  72. errno=old_errno;
  73. return rc;
  74. }
  75. extern int open64(const char* f, int a, ...);
  76. int (*orig_open64)(const char* f, int a, ...) = 0;
  77. int open64(const char* f, int a, ...)
  78. {
  79. struct status_t status;
  80. int old_errno=errno;
  81. int rc;
  82. handle_file_access_before("open64", f, &status);
  83. if (!orig_open64) orig_open64 = get_dl_symbol("open64");
  84. errno=old_errno;
  85. # if DEBUG == 1
  86. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original open64(\"%s\", ...) at %p (wrapper is at %p).\n",
  87. getpid(), f, orig_open64, open64);
  88. # endif
  89. if (a & O_CREAT)
  90. {
  91. va_list ap;
  92. mode_t b = 0;
  93. va_start(ap, a);
  94. b = va_arg(ap, mode_t);
  95. va_end(ap);
  96. rc = orig_open64(f, a, b);
  97. }
  98. else
  99. rc = orig_open64(f, a);
  100. old_errno=errno;
  101. # if FD_TRACKER == 1
  102. if (rc != -1)
  103. {
  104. char *buf = 0;
  105. int free_buf = 0;
  106. if (f[0] != '/')
  107. {
  108. char *buf2 = get_current_dir_name();
  109. if (asprintf(&buf,"%s%s%s", buf2,
  110. strcmp(buf2,"/") ? "/" : "", f) != -1)
  111. {
  112. f = buf; free_buf = 1;
  113. }
  114. free(buf2);
  115. }
  116. register_fd(rc, f, &status);
  117. if (free_buf) free(buf);
  118. }
  119. # else
  120. handle_file_access_after("open64", f, &status);
  121. # endif
  122. errno=old_errno;
  123. return rc;
  124. }
  125. extern int creat(const char* f, mode_t m);
  126. int (*orig_creat)(const char* f, mode_t m) = 0;
  127. int creat(const char* f, mode_t m)
  128. {
  129. struct status_t status;
  130. int old_errno=errno;
  131. int rc;
  132. handle_file_access_before("creat", f, &status);
  133. if (!orig_creat) orig_creat = get_dl_symbol("creat");
  134. # if DEBUG == 1
  135. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original creat(\"%s\", ...) at %p (wrapper is at %p).\n",
  136. getpid(), f, orig_creat, creat);
  137. # endif
  138. errno=old_errno;
  139. rc = orig_creat(f, m);
  140. old_errno=errno;
  141. # if FD_TRACKER == 1
  142. if (rc != -1)
  143. {
  144. char *buf = 0;
  145. int free_buf = 0;
  146. if (f[0] != '/')
  147. {
  148. char *buf2 = get_current_dir_name();
  149. if (asprintf(&buf,"%s%s%s", buf2,
  150. strcmp(buf2,"/") ? "/" : "", f) != -1)
  151. {
  152. f = buf; free_buf = 1;
  153. }
  154. free(buf2);
  155. }
  156. register_fd(rc, f, &status);
  157. if (free_buf) free(buf);
  158. }
  159. # else
  160. handle_file_access_after("creat", f, &status);
  161. # endif
  162. errno=old_errno;
  163. return rc;
  164. }
  165. extern int creat64(const char* f, mode_t m);
  166. int (*orig_creat64)(const char* f, mode_t m) = 0;
  167. int creat64(const char* f, mode_t m)
  168. {
  169. struct status_t status;
  170. int old_errno=errno;
  171. int rc;
  172. handle_file_access_before("creat64", f, &status);
  173. if (!orig_creat64) orig_creat64 = get_dl_symbol("creat64");
  174. # if DEBUG == 1
  175. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original creat64(%s, ...) at %p (wrapper is at %p).\n",
  176. getpid(), f, orig_creat64, creat64);
  177. # endif
  178. errno=old_errno;
  179. rc = orig_creat64(f, m);
  180. old_errno=errno;
  181. # if FD_TRACKER == 1
  182. if (rc != -1)
  183. {
  184. char *buf = 0;
  185. int free_buf = 0;
  186. if (f[0] != '/')
  187. {
  188. char *buf2 = get_current_dir_name();
  189. if (asprintf(&buf,"%s%s%s", buf2,
  190. strcmp(buf2,"/") ? "/" : "", f) != -1)
  191. {
  192. f = buf; free_buf = 1;
  193. }
  194. free(buf2);
  195. }
  196. register_fd(rc, f, &status);
  197. if (free_buf) free(buf);
  198. }
  199. # else
  200. handle_file_access_after("creat64", f, &status);
  201. # endif
  202. errno=old_errno;
  203. return rc;
  204. }
  205. extern FILE* fopen(const char* f, const char* g);
  206. FILE* (*orig_fopen)(const char* f, const char* g) = 0;
  207. FILE* fopen(const char* f, const char* g)
  208. {
  209. struct status_t status;
  210. int old_errno=errno;
  211. FILE* rc;
  212. handle_file_access_before("fopen", f, &status);
  213. if (!orig_fopen) orig_fopen = get_dl_symbol("fopen");
  214. # if DEBUG == 1
  215. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original fopen() at %p (wrapper is at %p).\n",
  216. getpid(), orig_fopen, fopen);
  217. # endif
  218. errno=old_errno;
  219. rc = orig_fopen(f, g);
  220. old_errno=errno;
  221. # if FD_TRACKER == 1
  222. if (rc != 0)
  223. {
  224. char *buf = 0;
  225. int free_buf = 0;
  226. if (f[0] != '/')
  227. {
  228. char *buf2 = get_current_dir_name();
  229. if (asprintf(&buf,"%s%s%s", buf2,
  230. strcmp(buf2,"/") ? "/" : "", f) != -1)
  231. {
  232. f = buf; free_buf = 1;
  233. }
  234. free(buf2);
  235. }
  236. register_fd(fileno(rc), f, &status);
  237. if (free_buf) free(buf);
  238. }
  239. # else
  240. handle_file_access_after("fopen", f, &status);
  241. # endif
  242. errno=old_errno;
  243. return rc;
  244. }
  245. extern FILE* fopen64(const char* f, const char* g);
  246. FILE* (*orig_fopen64)(const char* f, const char* g) = 0;
  247. FILE* fopen64(const char* f, const char* g)
  248. {
  249. struct status_t status;
  250. int old_errno=errno;
  251. FILE* rc;
  252. handle_file_access_before("fopen64", f, &status);
  253. if (!orig_fopen64) orig_fopen64 = get_dl_symbol("fopen64");
  254. # if DEBUG == 1
  255. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original fopen64() at %p (wrapper is at %p).\n",
  256. getpid(), orig_fopen64, fopen64);
  257. # endif
  258. errno=old_errno;
  259. rc = orig_fopen64(f, g);
  260. old_errno=errno;
  261. # if FD_TRACKER == 1
  262. if (rc != 0)
  263. {
  264. char *buf = 0;
  265. int free_buf = 0;
  266. if (f[0] != '/')
  267. {
  268. char *buf2 = get_current_dir_name();
  269. if (asprintf(&buf,"%s%s%s", buf2,
  270. strcmp(buf2,"/") ? "/" : "", f) != -1)
  271. {
  272. f = buf; free_buf = 1;
  273. }
  274. free(buf2);
  275. }
  276. register_fd(fileno(rc), f, &status);
  277. if (free_buf) free(buf);
  278. }
  279. # else
  280. handle_file_access_after("fopen64", f, &status);
  281. # endif
  282. errno=old_errno;
  283. return rc;
  284. }
  285. extern int dup(int fd);
  286. int (*orig_dup)(int fd) = 0;
  287. int dup(int fd)
  288. {
  289. int old_errno = errno;
  290. int rc;
  291. if (!orig_dup) orig_dup = get_dl_symbol("dup");
  292. # if DEBUG == 1
  293. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original dup(%d) at %p (wrapper is at %p).\n",
  294. getpid(), fd, orig_dup, dup);
  295. # endif
  296. errno=old_errno;
  297. rc = orig_dup(fd);
  298. old_errno = errno;
  299. # if FD_TRACKER == 1
  300. if (rc != -1)
  301. {
  302. struct pid_reg *pid = *find_pid(getpid());
  303. if (pid)
  304. {
  305. struct fd_reg *oldfd = *find_fd(pid, fd);
  306. if (oldfd) register_fd(rc, oldfd->filename, &oldfd->status);
  307. }
  308. }
  309. # endif
  310. errno=old_errno;
  311. return rc;
  312. }
  313. extern int dup2(int fd, int fd2);
  314. int (*orig_dup2)(int fd, int fd2) = 0;
  315. int dup2(int oldfd, int newfd)
  316. {
  317. int old_errno = errno;
  318. int rc;
  319. if (!orig_dup2) orig_dup2 = get_dl_symbol("dup2");
  320. # if DEBUG == 1
  321. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original dup2(%d, %d) at %p (wrapper is at %p).\n",
  322. getpid(), oldfd, newfd, orig_dup2, dup2);
  323. # endif
  324. errno=old_errno;
  325. rc = orig_dup2(oldfd, newfd);
  326. old_errno = errno;
  327. # if FD_TRACKER == 1
  328. if (rc != -1)
  329. {
  330. struct pid_reg *pid = *find_pid(getpid());
  331. if (pid)
  332. {
  333. struct fd_reg *fd = *find_fd(pid, newfd);
  334. if (fd && oldfd != newfd)
  335. {
  336. handle_file_access_after("dup2", fd->filename, &fd->status);
  337. deregister_fd(newfd);
  338. }
  339. fd = *find_fd(pid, oldfd);
  340. if (fd) register_fd(rc, fd->filename, &fd->status);
  341. }
  342. }
  343. # endif
  344. errno=old_errno;
  345. return rc;
  346. }
  347. extern int fcntl(int fd, int cmd, ...);
  348. int (*orig_fcntl)(int fd, int cmd, ...) = 0;
  349. int fcntl(int fd, int cmd, ...)
  350. {
  351. int old_errno = errno;
  352. int rc;
  353. int fd2 = -1;
  354. if (!orig_fcntl) orig_fcntl = get_dl_symbol("fcntl");
  355. # if DEBUG == 1
  356. if (debug) fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original fcntl(%d, %d, ...) at %p (wrapper is at %p).\n",
  357. getpid(), fd, cmd, orig_fcntl, fcntl);
  358. # endif
  359. errno=old_errno;
  360. if (cmd & F_GETLK || cmd & F_SETLK || cmd & F_SETLKW)
  361. {
  362. va_list ap;
  363. struct flock *b = 0;
  364. va_start(ap, cmd);
  365. b = va_arg(ap, struct flock*);
  366. va_end(ap);
  367. rc = orig_fcntl(fd, cmd, b);
  368. } else {
  369. va_list ap;
  370. long b = 0;
  371. va_start(ap, cmd);
  372. b = va_arg(ap, long);
  373. va_end(ap);
  374. rc = orig_fcntl(fd, cmd, b);
  375. fd2 = (int) b;
  376. }
  377. old_errno = errno;
  378. # if FD_TRACKER == 1
  379. if (rc != -1 && cmd == F_DUPFD)
  380. {
  381. struct pid_reg *pid = *find_pid(getpid());
  382. if (pid)
  383. {
  384. struct fd_reg *oldfd = *find_fd(pid, fd);
  385. if (oldfd) register_fd(rc, oldfd->filename, &oldfd->status);
  386. }
  387. }
  388. # endif
  389. errno=old_errno;
  390. return rc;
  391. }