snap.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "snap.h"
  5. void
  6. usage(void)
  7. {
  8. fprint(2, "usage: %s [-o snapfile] pid...\n", argv0);
  9. exits("usage");
  10. }
  11. void
  12. main(int argc, char **argv)
  13. {
  14. char *user, *sys, *arch, *term, *ofile;
  15. int i;
  16. long pid, me;
  17. Biobuf *b;
  18. Dir *d;
  19. Proc *p;
  20. ofile = "/fd/1";
  21. ARGBEGIN{
  22. case 'o':
  23. ofile = ARGF();
  24. break;
  25. default:
  26. usage();
  27. }ARGEND;
  28. if(argc < 1)
  29. usage();
  30. /* get kernel compilation time */
  31. if((d = dirstat("#/")) == nil) {
  32. fprint(2, "cannot stat #/ ???\n");
  33. exits("stat");
  34. }
  35. if((b = Bopen(ofile, OWRITE)) == nil) {
  36. fprint(2, "cannot write to \"%s\"\n", ofile);
  37. exits("Bopen");
  38. }
  39. if((user = getuser()) == nil)
  40. user = "gre";
  41. if((sys = sysname()) == nil)
  42. sys = "gnot";
  43. if((arch = getenv("cputype")) == nil)
  44. arch = "unknown";
  45. if((term = getenv("terminal")) == nil)
  46. term = "unknown terminal type";
  47. Bprint(b, "process snapshot %ld %s@%s %s %ld \"%s\"\n",
  48. time(0), user, sys, arch, d->mtime, term);
  49. me = getpid();
  50. for(i=0; i<argc; i++) {
  51. if((pid = atol(argv[i])) == me)
  52. fprint(2, "warning: will not snapshot self\n");
  53. else if(p = snap(pid, 1))
  54. writesnap(b, p);
  55. }
  56. exits(0);
  57. }