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