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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
implement Register;
#
# Copyright © 2003 Vita Nuova Holdings Limited. All rights reserved.
#
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
include "registries.m";
registries: Registries;
Registry, Attributes, Service: import registries;
include "grid/announce.m";
announce: Announce;
include "arg.m";
registered: ref Registries->Registered;
Register: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
sys->pctl(sys->FORKNS | sys->NEWPGRP, nil);
registries = load Registries Registries->PATH;
if (registries == nil)
badmod(Registries->PATH);
registries->init();
announce = load Announce Announce->PATH;
if (announce == nil)
badmod(Announce->PATH);
announce->init();
arg := load Arg Arg->PATH;
if (arg == nil)
badmod(Arg->PATH);
attrs := Attributes.new(("proto", "styx") :: ("auth", "none") :: ("resource","Cpu Pool") :: nil);
maxusers := -1;
autoexit := 0;
myaddr := "";
arg->init(argv);
arg->setusage("register [-u maxusers] [-e exit threshold] [-a attributes] { program }");
while ((opt := arg->opt()) != 0) {
case opt {
'm' =>
attrs.set("memory", memory());
'u' =>
if ((maxusers = int arg->earg()) <= 0)
arg->usage();
'e' =>
if ((autoexit = int arg->earg()) < 0)
arg->usage();
'A' =>
myaddr = arg->earg();
'a' =>
attr := arg->earg();
val := arg->earg();
attrs.set(attr, val);
}
}
argv = arg->argv();
if (argv == nil)
arg->usage();
(nil, plist) := sys->tokenize(hd argv, "{} \t\n");
arg = nil;
sysname := readfile("/dev/sysname");
reg: ref Registry;
reg = Registry.new("/mnt/registry");
if (reg == nil)
reg = Registry.connect(nil, nil, nil);
if (reg == nil)
error(sys->sprint("Could not find registry: %r\nMake sure that ndb/cs has been started and there is a registry announcing on the machine specified in /lib/ndb/local"));
c : sys->Connection;
if (myaddr == nil) {
(addr, conn) := announce->announce();
if (addr == nil)
error(sys->sprint("cannot announce: %r"));
myaddr = addr;
c = *conn;
}
else {
n: int;
(n, c) = sys->announce(myaddr);
if (n == -1)
error(sys->sprint("cannot announce: %r"));
(n, nil) = sys->tokenize(myaddr, "*");
if (n > 1) {
(nil, lst) := sys->tokenize(myaddr, "!");
if (len lst >= 3)
myaddr = "tcp!" + sysname +"!" + hd tl tl lst;
}
}
persist := 0;
if (attrs.get("name") == nil)
attrs.set("name", sysname);
err: string;
(registered, err) = reg.register(myaddr, attrs, persist);
if (err != nil)
error("could not register with registry: "+err);
mountfd := popen(ctxt, plist);
spawn listener(c, mountfd, maxusers);
}
listener(c: Sys->Connection, mountfd: ref sys->FD, maxusers: int)
{
for (;;) {
(n, nc) := sys->listen(c);
if (n == -1)
error(sys->sprint("listen failed: %r"));
dfd := sys->open(nc.dir + "/data", Sys->ORDWR);
if (maxusers != -1 && nusers >= maxusers)
sys->fprint(stderr(), "register: maxusers (%d) exceeded!\n", nusers);
else if (dfd != nil) {
sync := chan of int;
addr := readfile(nc.dir + "/remote");
if (addr == nil)
addr = "unknown";
if (addr[len addr - 1] == '\n')
addr = addr[:len addr - 1];
spawn proxy(sync, dfd, mountfd, addr);
<-sync;
}
}
}
proxy(sync: chan of int, dfd, mountfd: ref sys->FD, addr: string)
{
pid := sys->pctl(Sys->NEWFD | Sys->NEWNS, 1 :: 2 :: mountfd.fd :: dfd.fd :: nil);
dfd = sys->fildes(dfd.fd);
mountfd = sys->fildes(mountfd.fd);
sync <-= 1;
done := chan of int;
spawn exportit(dfd, done);
if (sys->mount(mountfd, nil, "/", sys->MREPL | sys->MCREATE, addr) == -1)
sys->fprint(stderr(), "register: proxy mount failed: %r\n");
nusers++;
<-done;
nusers--;
}
nusers := 0;
clock(tick: chan of int)
{
for (;;) {
sys->sleep(2000);
tick <-= 1;
}
}
exportit(dfd: ref sys->FD, done: chan of int)
{
sys->export(dfd, "/", sys->EXPWAIT);
done <-= 1;
}
popen(ctxt: ref Draw->Context, argv: list of string): ref Sys->FD
{
sync := chan of int;
fds := array[2] of ref Sys->FD;
sys->pipe(fds);
spawn runcmd(ctxt, argv, fds[0], sync);
<-sync;
return fds[1];
}
runcmd(ctxt: ref Draw->Context, argv: list of string, stdin: ref Sys->FD, sync: chan of int)
{
pid := sys->pctl(Sys->FORKFD, nil);
sys->dup(stdin.fd, 0);
stdin = nil;
sync <-= 0;
sh := load Sh Sh->PATH;
sh->run(ctxt, argv);
}
error(e: string)
{
sys->fprint(stderr(), "register: %s\n", e);
raise "fail:error";
}
user(): string
{
if ((s := readfile("/dev/user")) == nil)
return "none";
return s;
}
readfile(f: string): string
{
fd := sys->open(f, sys->OREAD);
if(fd == nil)
return nil;
buf := array[8192] of byte;
n := sys->read(fd, buf, len buf);
if(n < 0)
return nil;
return string buf[0:n];
}
stderr(): ref Sys->FD
{
return sys->fildes(2);
}
badmod(path: string)
{
sys->fprint(stderr(), "Register: cannot load %s: %r\n", path);
exit;
}
killg(pid: int)
{
if ((fd := sys->open("/prog/" + string pid + "/ctl", Sys->OWRITE)) != nil) {
sys->fprint(fd, "killgrp");
fd = nil;
}
}
memory(): string
{
buf := array[1024] of byte;
s := readfile("/dev/memory");
(nil, lst) := sys->tokenize(s, " \t\n");
if (len lst > 2) {
mem := int hd tl lst;
mem /= (1024*1024);
return string mem + "mb";
}
return "not known";
}
|