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.

196 lines
4.6 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 - 2003 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. while (fgets(line, 1024, stdin) != NULL) {
  112. if ( sscanf(line, "%d fork() = %d", &pid, &newpid) == 2 ||
  113. sscanf(line, "%d vfork() = %d", &pid, &newpid) == 2 ||
  114. sscanf(line, "%d <... fork resumed> ) = %d",
  115. &pid, &newpid) == 2 ||
  116. sscanf(line, "%d <... vfork resumed> ) = %d",
  117. &pid, &newpid) == 2) {
  118. sp1 = getproc(pid)->cwd;
  119. sp2 = malloc( strlen(sp1) + 1);
  120. strcpy(sp2, sp1);
  121. newproc(newpid, sp2 );
  122. continue;
  123. }
  124. if ( sscanf(line, "%d _exit(%d", &pid, &newpid) == 2 ) {
  125. freeproc(pid);
  126. continue;
  127. }
  128. if ( sscanf(line, "%d chdir(\"%[^\"]", &pid, buf1) == 2 ) {
  129. chdir( getproc(pid)->cwd ); chdir( buf1 );
  130. setproc(pid, getcwd((char *) NULL, 0) );
  131. continue;
  132. }
  133. if ( sscanf(line, "%d open(\"%[^\"]\", %s",
  134. &pid, buf1, buf2) == 3 ) {
  135. if (strstr(buf2, "O_RDONLY") == NULL) logfile = wlog;
  136. else logfile = rlog;
  137. if (strstr(buf2, "O_DIRECTORY") != NULL) continue;
  138. if (buf1[0] == '/') {
  139. fprintf(logfile, "%d: %s\n", pid, buf1);
  140. } else {
  141. sp1 = getproc(pid)->cwd;
  142. if (!strcmp(sp1, "/")) sp1="";
  143. fprintf(logfile, "%d: %s/%s\n",
  144. pid, sp1, buf1);
  145. }
  146. continue;
  147. }
  148. if ( sscanf(line, "%d mkdir(\"%[^\"]\", ", &pid, buf1) == 2 ||
  149. sscanf(line, "%d utime(\"%[^\"]\", ", &pid, buf1) == 2 ||
  150. sscanf(line, "%d link(\"%[^\"]\", \"%[^\"]\"",
  151. &pid, buf2, buf1) == 3 ||
  152. sscanf(line, "%d symlink(\"%[^\"]\", \"%[^\"]\"",
  153. &pid, buf2, buf1) == 3 ||
  154. sscanf(line, "%d rename(\"%[^\"]\", \"%[^\"]\"",
  155. &pid, buf2, buf1) == 3 ) {
  156. if (buf1[0] == '/') {
  157. fprintf(wlog, "%d: %s\n", pid, buf1);
  158. } else {
  159. sp1 = getproc(pid)->cwd;
  160. if (!strcmp(sp1, "/")) sp1="";
  161. fprintf(wlog, "%d: %s/%s\n",
  162. pid, sp1, buf1);
  163. }
  164. continue;
  165. }
  166. }
  167. return 0;
  168. }