forked from philhofer/distill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.scm
324 lines (306 loc) · 13.1 KB
/
service.scm
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
(define symbolic? (perhaps symbol?))
(define maybe-int? (perhaps integer?))
(define maybe-string? (perhaps string?))
(define maybe-symbolic? (perhaps symbolic?))
;; note: field names for <longrun>, <oneshot>, and <bundle>
;; correspond precisely to the file names that s6-rc-compile
;; expects to be present in a service definition directory
(define-kvector-type
<longrun>
longrun*
longrun?
(type: 'longrun (eq?/c 'longrun))
(run: #f list?)
(finish: #f (perhaps list?))
(pipeline-name: #f maybe-symbolic?)
(nosetsid: #f maybe-symbolic?)
(max-death-tally: #f maybe-int?)
(down-signal: #f maybe-int?)
(timeout-up: #f maybe-int?)
(timeout-down: #f maybe-int?)
(timeout-kill: #f maybe-int?)
(timeout-finish: #f maybe-int?)
(dependencies: #f maybe-string?)
(notification-fd: #f maybe-int?)
(producer-for: #f maybe-symbolic?)
(consumer-for: #f maybe-symbolic?))
(define-kvector-type
<oneshot>
oneshot*
oneshot?
(type: 'oneshot (eq?/c 'oneshot))
(up: #f list?)
(down: #f (perhaps list?))
(timeout-up: #f maybe-int?)
(timeout-down: #f maybe-int?)
(dependencies: #f maybe-string?))
(define-kvector-type
<bundle>
%make-bundle
bundle?
(type: 'bundle (eq?/c 'bundle))
(contents: #f string?))
(: bundle* (#!rest vector --> vector))
(define (bundle* . args)
(%make-bundle
contents: (map-lines service-name args)))
(: spec? (* -> boolean))
(define spec?
(or/c longrun? oneshot? bundle?))
(define (prepend-spec-artifacts name spec lst)
(let* ((dir name)
(field->file (lambda (kw val)
(let ((file (keyword->string kw)))
(interned
(filepath-join "/src/services" dir file)
#o644
(with-output-to-string
(lambda () (if (pair? val)
(write-exexpr val)
(begin (display val) (newline))))))))))
(kvector-foldl
spec
(lambda (kw val lst)
(if val (cons (field->file kw val) lst) lst))
lst)))
(define (s6-rc-db artifacts)
(package-template
label: "s6-rc-db"
src: artifacts
dir: "/"
tools: (list busybox-core execline-tools s6 s6-rc)
inputs: '()
build: `(if (mkdir -p "/out/etc/s6-rc")
s6-rc-compile "/out/etc/s6-rc/compiled" "/src/services")))
(define-kvector-type
<service>
make-service
service?
(service-name name: #f symbol?)
(service-inputs inputs: '() (list-of (or/c procedure? artifact?)))
(service-users users: '() (list-of procedure?))
(service-groups groups: '() (list-of procedure?))
(service-after after: '() (list-of (or/c procedure? string? symbol?)))
(service-spec spec: #f spec?))
(: update-service (vector #!rest * -> vector))
(define (update-service svc . args)
(apply make-service
(append (kvector->list svc) args)))
(: named* (string -> (vector -> boolean)))
(define (named* pat)
(let ((sre (glob->sre pat)))
(lambda (svc)
(irregex-match? sre (symbol->string (service-name svc))))))
;; service-depends takes a service and
;; a sequence of all service and yields
;; a sequence of services that must be
;; started before 'svc'
(define (service-depends svc all)
(let ((after (service-after svc)))
(if (null? after)
'()
(let* ((match? (lambda (svc)
(let loop ((lst after))
(if (null? lst)
#f
(let ((head (car lst))
(rest (cdr lst)))
(or (cond
((string? head)
(string=? head (symbol->string (service-name svc))))
((symbol? head)
(eq? head (service-name svc)))
((procedure? head)
(head svc))
(else (fatal "invalid 'service#after' element" head)))
(loop rest))))))))
(letrec ((filter (lambda (pred? lst)
(if (null? lst)
'()
(let ((head (car lst)))
(if (pred? head)
(cons head (filter pred? (cdr lst)))
(filter pred? (cdr lst))))))))
(filter match? all))))))
(define (prepend-service-artifacts svc all lst)
(let ((depends (service-depends svc all))
(spec (service-spec svc)))
(prepend-spec-artifacts
(service-name svc)
(if (null? depends)
spec
(kupdate
spec
dependencies: (map-lines service-name depends)))
lst)))
;; services->packages converts a list of
;; services, a list of users, and a list of groups
;; into all of the packages and artifacts
;; that the services depend upon, including
;; the necessary init scripts and binaries
(: services->packages ((list-of vector) (list-of vector) (list-of vector) -> (list-of procedure)))
(define (services->packages all extra-users extra-groups)
(let* ((fold (lambda (proc init lst)
(let loop ((out init)
(lst lst))
(if (null? lst) out (loop (proc (car lst) out) (cdr lst))))))
(sublists (lambda (lst field init)
(fold (lambda (svc lst)
(let loop ((items (field svc))
(lst lst))
(if (null? items)
lst
(let ((i (car items)))
(loop
(cdr items)
(if (memq i lst) lst (cons i lst)))))))
init
lst)))
(reqpkgs (list s6 s6-rc busybox-full execline-tools hard))
(tail (sublists all service-inputs reqpkgs))
(default (make-service
name: 'default
spec: (%make-bundle
type: 'bundle
contents: (map-lines service-name all))))
(arts (fold
(lambda (svc lst)
(prepend-service-artifacts svc all lst))
'() (cons default all)))
(db (s6-rc-db arts)))
(append
(cons db tail)
(groups+users->artifacts
(sublists all service-groups extra-groups)
(sublists all service-users extra-users))
(boot-scripts))))
(define *service-dir* "/run/service")
(define *catchall-fifo* "/run/service/s6-svscan-log/fifo")
(define *uncaught-logs* "/run/uncaught-logs")
;; init-script is the execline script for /init
(define (init-script)
`(export
PATH "/usr/bin:/bin:/usr/sbin:/sbin"
export LC_ALL "C.UTF-8"
/bin/umask "022"
foreground (echo "init starting")
;; for some reason, hidepid=2 doesn't take without remounting:
foreground (mount -t proc -o "rw,nosuid,nodev,noexec,relatime" proc /proc)
foreground (mount -t proc -o "remount,rw,nosuid,nodev,noexec,relatime,hidepid=2" proc /proc)
foreground (mount -t sysfs -o "noexec,nosuid,nodev" sysfs /sys)
;; note: some kernels are configured to automagically mount /dev,
;; so no worries if this doesn't mount on its own
foreground (mount -t devtmpfs -o "exec,nosuid,mode=0755,size=2M" devtmpfs /dev)
foreground (mkdir -p /dev/pts)
foreground (mount -t devpts -o "gid=5,mode=0620,noexec,nosuid" devpts /dev/pts)
foreground (mkdir -p /dev/shm)
foreground (mount -t tmpfs -o "nodev,nosuid,noexec,size=5%,mode=1777" shm /dev/shm)
foreground (mount -t tmpfs -o "exec,nosuid,nodev,mode=0755" tmpfs /run)
;; to keep world-writeable /tmp from being a trivial DoS vector,
;; limit the size of the mount to some fixed % of RAM
;; TODO: make this configurable?
foreground (mount -t tmpfs -o "exec,nosuid,nodev,mode=1777,size=10%" tmpfs /tmp)
foreground (if (test -x /sbin/preboot)
if (echo "running preboot")
/sbin/preboot)
if (mkdir -p ,*service-dir*)
if (cp -a "/etc/early-services/." ,*service-dir*)
foreground (mkfifo -m "0600" ,*catchall-fifo*)
if (mkdir -p ,*uncaught-logs*)
foreground (chown catchlog:catchlog ,*uncaught-logs*)
foreground (chmod "2700" ,*uncaught-logs*)
piperw 3 4
;; we shouldn't run s6-rc-init until
;; s6-svscan has started, so we need
;; to do a little pipe juggling
background (fdclose
4
fdmove 0 3
withstdinas ignored
redirfd -r 0 /dev/null
if (s6-rc-init -c /etc/s6-rc/compiled
-l /run/s6-rc
,*service-dir*)
redirfd -w 1 ,*catchall-fifo*
fdmove -c 2 1
s6-rc -v2 -u change default)
fdclose 3
foreground (echo "beginning s6-svscan")
redirfd -r 0 /dev/null
redirfd -w -n -b 1 ,*catchall-fifo*
fdmove -c 2 1
s6-svscan -d 4 -t0 ,*service-dir*))
(define (boot-scripts)
(let* ((script* (lambda (path body)
(interned path #o744
(with-output-to-string
(lambda () (write-exexpr body))))))
(scanctl* (lambda (script)
(script* script `(background ; fork so we can setsid
(s6-setsid ; don't get killed when the shell dies!
foreground (s6-rc -v2 -bad -t 10000 change)
foreground (s6-svscanctl -t -b /run/service))))))
(init (script* "/sbin/init" (init-script)))
(reboot (script* "/sbin/reboot"
'(background
(s6-setsid
foreground (s6-rc -v2 -bad -t 10000 change)
foreground (kill -15 -1)
foreground (sleep 1)
foreground (sync)
hard reboot))))
(poweroff (scanctl* "/sbin/poweroff"))
(halt (scanctl* "/sbin/halt"))
(crash (script* "/etc/early-services/.s6-svscan/crash"
'(foreground
(redirfd -w 1 /dev/console
fdmove -c 2 1
echo "pid 1 is crashing!")
foreground (kill -15 -1)
foreground (sleep 1)
foreground (kill -9 -1)
foreground (sleep 1)
foreground (sync)
hard reboot)))
(finish (script* "/etc/early-services/.s6-svscan/finish"
;; note: at this point it is expected that
;; everything under process supervision is already dead,
;; but there still may be other processes running
'(redirfd
-w 1 /dev/console
fdmove -c 2 1
foreground (kill -15 -1)
foreground (sleep 1)
foreground (kill -9 -1)
foreground (sync)
hard poweroff)))
(logger (interned "/etc/early-services/s6-svscan-log/run"
#o744
(with-output-to-string
(lambda ()
(write-exexpr
`(redirfd
-w 2 /dev/console
redirfd -r -n -b 0 fifo
s6-setuidgid catchlog
exec -c
s6-log t /run/uncaught-logs)))))))
(list init logger finish crash reboot poweroff halt
(interned-dir "/tmp" #o1777)
(interned-dir "/dev" #o755)
(interned-dir "/var" #o755)
(interned-dir "/run" #o755)
(interned-dir "/proc" #o555)
(interned-dir "/sys" #o555)
(interned-dir "/boot" #o755)
(interned-dir "/root" #o755)
(interned-dir "/home" #o755)
;; TODO: vendor these:
(remote-file
"https://salsa.debian.org/md/netbase/-/raw/master/etc/services"
"14CskWLv_71RLWwcMAe2GbqVQx2VsHTxPR1I8OPtcD4="
"/etc/services" #o644)
(remote-file
"https://salsa.debian.org/md/netbase/-/raw/master/etc/protocols"
"gykO3waWpzCvDqGEFidDstWolF4YoIjMyvOkG16y1b4="
"/etc/protocols" #o644))))