xref: /plan9/sys/src/cmd/snap/snap.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "snap.h"
5 
6 void
usage(void)7 usage(void)
8 {
9 	fprint(2, "usage: %s [-o snapfile] pid...\n", argv0);
10 	exits("usage");
11 }
12 
13 void
main(int argc,char ** argv)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 'o':
26 		ofile = ARGF();
27 		break;
28 	default:
29 		usage();
30 	}ARGEND;
31 
32 	if(argc < 1)
33 		usage();
34 
35 	/* get kernel compilation time */
36 	if((d = dirstat("#/")) == nil) {
37 		fprint(2, "cannot stat #/ ???\n");
38 		exits("stat");
39 	}
40 
41 	if((b = Bopen(ofile, OWRITE)) == nil) {
42 		fprint(2, "cannot write to \"%s\"\n", ofile);
43 		exits("Bopen");
44 	}
45 
46 	if((user = getuser()) == nil)
47 		user = "gre";
48 	if((sys = sysname()) == nil)
49 		sys = "gnot";
50 	if((arch = getenv("cputype")) == nil)
51 		arch = "unknown";
52 	if((term = getenv("terminal")) == nil)
53 		term = "unknown terminal type";
54 
55 	Bprint(b, "process snapshot %ld %s@%s %s %ld \"%s\"\n",
56 		time(0), user, sys, arch, d->mtime, term);
57 	me = getpid();
58 	for(i=0; i<argc; i++) {
59 		if((pid = atol(argv[i])) == me)
60 			fprint(2, "warning: will not snapshot self\n");
61 		else if(p = snap(pid, 1))
62 			writesnap(b, p);
63 	}
64 	exits(0);
65 }
66