OpenSDE Framework (without history before r20070)
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.

190 lines
4.3 KiB

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