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.

205 lines
5.3 KiB

  1. /*
  2. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. *
  4. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. * Please add additional copyright information _after_ the line containing
  6. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. *
  9. * ROCK Linux: rock-src/misc/tools-source/fl_stparse.c
  10. * ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version. A copy of the GNU General Public
  16. * License can be found at Documentation/COPYING.
  17. *
  18. * Many people helped and are helping developing ROCK Linux. Please
  19. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. * file for details.
  21. *
  22. * --- ROCK-COPYRIGHT-NOTE-END ---
  23. */
  24. #define _GNU_SOURCE
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #define MAXPROCS 512
  30. struct procs_t {
  31. int pid;
  32. char * cwd;
  33. } procs[MAXPROCS];
  34. void newproc(int pid, char * cwd) {
  35. int c;
  36. for (c=0; c<MAXPROCS; c++)
  37. if (procs[c].pid == 0) {
  38. procs[c].pid = pid;
  39. procs[c].cwd = cwd;
  40. return;
  41. }
  42. fprintf(stderr, "fl_stparse: No more free procs in list!\n");
  43. exit(1);
  44. }
  45. struct procs_t * getproc(int pid) {
  46. int c;
  47. for (c=0; c<MAXPROCS; c++)
  48. if (procs[c].pid == pid) {
  49. return procs + c;
  50. }
  51. fprintf(stderr, "fl_stparse: Can't find proc %d!\n", pid);
  52. exit(1);
  53. }
  54. void setproc(int pid, char * cwd) {
  55. int c;
  56. for (c=0; c<MAXPROCS; c++)
  57. if (procs[c].pid == pid) {
  58. free(procs[c].cwd);
  59. procs[c].cwd = cwd;
  60. return;
  61. }
  62. fprintf(stderr, "fl_stparse: Can't set proc %d!\n", pid);
  63. exit(1);
  64. }
  65. void freeproc(int pid) {
  66. int c;
  67. for (c=0; c<MAXPROCS; c++)
  68. if (procs[c].pid == pid) {
  69. procs[c].pid=0;
  70. free(procs[c].cwd);
  71. procs[c].cwd=NULL;
  72. return;
  73. }
  74. fprintf(stderr, "fl_stparse: Can't free proc %d!\n", pid);
  75. exit(1);
  76. }
  77. int main(int argc, char ** argv) {
  78. char line[1024];
  79. char buf1[1024];
  80. char buf2[1024];
  81. int pid, newpid;
  82. char *sp1, *sp2;
  83. FILE *wlog = fopen("/dev/null", "w");
  84. FILE *rlog = fopen("/dev/null", "w");
  85. FILE *logfile;
  86. int opt;
  87. while ( (opt = getopt(argc, argv, "w:r:")) != -1 ) {
  88. switch (opt) {
  89. case 'w':
  90. fclose(wlog);
  91. wlog = fopen(optarg, "w");
  92. break;
  93. case 'r':
  94. fclose(rlog);
  95. rlog = fopen(optarg, "w");
  96. break;
  97. default:
  98. fprintf(stderr, "Usage: %s [-w wlog-file] "
  99. "[-r rlog-file]\n", argv[0]);
  100. return 1;
  101. }
  102. }
  103. if (fgets(line, 1024, stdin) &&
  104. sscanf(line, "%d", &pid) == 1 ) {
  105. newproc(pid, getcwd((char *) NULL, 0) );
  106. } else {
  107. fprintf(stderr, "fl_stparse: Can't init using first line "
  108. "of input!\n");
  109. return 1;
  110. }
  111. do {
  112. // ignore this line if syscall returned error
  113. if ( strstr(line, " = -1 E") ) continue;
  114. if ( sscanf(line, "%d fork() = %d", &pid, &newpid) == 2 ||
  115. sscanf(line, "%d clone(%[^)]) = %d", &pid, buf1,
  116. &newpid) == 3 ||
  117. sscanf(line, "%d vfork() = %d", &pid, &newpid) == 2 ||
  118. sscanf(line, "%d <... fork resumed> ) = %d",
  119. &pid, &newpid) == 2 ||
  120. sscanf(line, "%d <... clone resumed> %[^)]) = %d",
  121. &pid, buf1,
  122. &newpid) == 3 ||
  123. sscanf(line, "%d <... clone resumed> ) = %d",
  124. &pid, &newpid) == 2 ||
  125. sscanf(line, "%d <... vfork resumed> ) = %d",
  126. &pid, &newpid) == 2) {
  127. sp1 = getproc(pid)->cwd;
  128. sp2 = malloc( strlen(sp1) + 1);
  129. strcpy(sp2, sp1);
  130. newproc(newpid, sp2 );
  131. continue;
  132. }
  133. if ( sscanf(line, "%d _exit(%d", &pid, &newpid) == 2 ||
  134. sscanf(line, "%d exit(%d", &pid, &newpid) == 2 ||
  135. sscanf(line, "%d exit_group(%d", &pid, &newpid) == 2 ) {
  136. freeproc(pid);
  137. continue;
  138. }
  139. if ( sscanf(line, "%d chdir(\"%[^\"]", &pid, buf1) == 2 ) {
  140. chdir( getproc(pid)->cwd ); chdir( buf1 );
  141. setproc(pid, getcwd((char *) NULL, 0) );
  142. continue;
  143. }
  144. if ( sscanf(line, "%d open(\"%[^\"]\", %s",
  145. &pid, buf1, buf2) == 3 ) {
  146. if (strstr(buf2, "O_RDONLY") == NULL) logfile = wlog;
  147. else logfile = rlog;
  148. if (strstr(buf2, "O_DIRECTORY") != NULL) continue;
  149. if (buf1[0] == '/') {
  150. fprintf(logfile, "%d: %s\n", pid, buf1);
  151. } else {
  152. sp1 = getproc(pid)->cwd;
  153. if (!strcmp(sp1, "/")) sp1="";
  154. fprintf(logfile, "%d: %s/%s\n",
  155. pid, sp1, buf1);
  156. }
  157. continue;
  158. }
  159. if ( sscanf(line, "%d mkdir(\"%[^\"]\", ", &pid, buf1) == 2 ||
  160. sscanf(line, "%d utime(\"%[^\"]\", ", &pid, buf1) == 2 ||
  161. sscanf(line, "%d link(\"%[^\"]\", \"%[^\"]\"",
  162. &pid, buf2, buf1) == 3 ||
  163. sscanf(line, "%d symlink(\"%[^\"]\", \"%[^\"]\"",
  164. &pid, buf2, buf1) == 3 ||
  165. sscanf(line, "%d rename(\"%[^\"]\", \"%[^\"]\"",
  166. &pid, buf2, buf1) == 3 ) {
  167. if (buf1[0] == '/') {
  168. fprintf(wlog, "%d: %s\n", pid, buf1);
  169. } else {
  170. sp1 = getproc(pid)->cwd;
  171. if (!strcmp(sp1, "/")) sp1="";
  172. fprintf(wlog, "%d: %s/%s\n",
  173. pid, sp1, buf1);
  174. }
  175. continue;
  176. }
  177. } while (fgets(line, 1024, stdin) != NULL);
  178. return 0;
  179. }