summaryrefslogtreecommitdiff
path: root/appl/cmd/broke.b
blob: 41f2dd89b56b87aea917c5137a4ab11d1092fbc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
implement Broke;

include "sys.m";
	sys: Sys;
include "draw.m";

Broke: module
{
	init:	fn(nil: ref Draw->Context, args: list of string);
};

init(nil: ref Draw->Context, nil: list of string)
{
	sys = load Sys Sys->PATH;
	fd := sys->open("/prog", Sys->OREAD);
	if(fd == nil)
		err(sys->sprint("can't open /prog: %r"));
	killed := "";
	for(;;){
		(n, dir) := sys->dirread(fd);
		if(n <= 0){
			if(n < 0)
				err(sys->sprint("error reading /prog: %r"));
			break;
		}
		for(i := 0; i < n; i++)
			if(isbroken(dir[i].name) && kill(dir[i].name))
				killed += sys->sprint(" %s", dir[i].name);
	}
	if(killed != nil)
		sys->print("%s\n", killed);
}

isbroken(pid: string): int
{
	statf := "/prog/" + pid + "/status";
	fd := sys->open(statf, Sys->OREAD);
	if (fd == nil)
		return 0;
	buf := array[256] of byte;
	n := sys->read(fd, buf, len buf);
	if (n < 0) {	# process died or is exiting
		# sys->fprint(stderr(), "broke: can't read %s: %r\n", statf);
		return 0;
	}
	(nf, l) := sys->tokenize(string buf[0:n], " ");
	return nf >= 5 && hd tl tl tl tl l == "broken";
}

kill(pid: string): int
{
	ctl := "/prog/" + pid + "/ctl";
	fd := sys->open(ctl, sys->OWRITE);
	if(fd == nil || sys->fprint(fd, "kill") < 0){
		sys->fprint(stderr(), "broke: can't kill %s: %r\n", pid);	# but press on
		return 0;
	}
	return 1;
}

err(s: string)
{
	sys->fprint(sys->fildes(2), "broke: %s\n", s);
	raise "fail:error";
}

stderr(): ref Sys->FD
{
	return sys->fildes(2);
}

user(): string
{
	fd := sys->open("/dev/user", sys->OREAD);
	if(fd == nil)
		return "inferno";

	buf := array[64] of byte;
	n := sys->read(fd, buf, len buf);
	if(n <= 0)
		return "inferno";

	return string buf[0:n];
}