aboutsummaryrefslogtreecommitdiff
path: root/whispers/services/whispers.scm
blob: bd2d7f5b57fc6cf84dab3775d61b6266cd9ee58a (about) (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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
;;; Whispers --- Stealth VPN and ssh tunneler
;;; Copyright � 2023 Runciter <runciter@whispers-vpn.org>
;;;
;;; This file is part of Whispers.
;;;
;;; Whispers is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; Whispers is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Whispers.  If not, see <http://www.gnu.org/licenses/>.

(define-module (whispers services whispers)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (gnu system shadow)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services mcron)
  #:use-module (gnu services admin)
  #:use-module (gnu packages base)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages networking)
  #:use-module (whispers packages whispers)
  #:use-module (srfi srfi-1)
  #:export (whispers-configuration
            whispers-configuration?
            whispers-user-group
            whispers-user-group?
            whispers-user-group-user
            whispers-user-group-group
            user-container-name
            lieutenant-path->socket-file-path
            whispers-service-type))

(define-record-type* <whispers-configuration>
  whispers-configuration make-whispers-configuration
  whispers-configuration?
  this-whispers-configuration
  ;; A file-like-object
  (coreutils-package                whispers-configuration-coreutils-package
                                    (default coreutils))
  ;; A file-like-object
  (util-linux-package               whispers-configuration-util-linux-package
                                    (default util-linux))
  ;; A file-like-object
  (whispers-package                 whispers-configuration-whispers-package
                                    (default whispers))
  ;; A symbol
  (name                             whispers-configuration-name
                                    (default 'whispers))
  ;; A list of guix service objects
  (lieutenants                      whispers-configuration-lieutenants
                                    (default '()))
  ;; A list of symbols
  (requires                         whispers-configuration-requires
                                    (default '()))
  ;; A string
  (user                             whispers-configuration-user
                                    (default "root"))
  ;; A boolean value
  (extend-user?                     whispers-configuration-extend-user?
                                    (default #f))
  ;; A string
  (group                            whispers-configuration-group
                                    (default "root"))
  ;; A boolean value
  (extend-group?                    whispers-configuration-extend-group?
                                    (default #f))
  ;; A string
  (timeout                          whispers-configuration-timeout
                                    (default '(default-pid-file-timeout)))
  ;; A list of package objects
  (extra-packages                   whispers-configuration-extra-packages
                                    (default (list)))
  ;; A list of shepherd-action records
  (extra-actions                    whispers-configuration-extra-actions
                                    (default (list)))
  ;; A boolean value
  (pre-start-action?                whispers-configuration-pre-start-action?
                                    (default #f))
  ;; A boolean value
  (post-stop-action?                whispers-configuration-post-stop-action?
                                    (default #f))
  ;; A boolean value
  (%auto-start?                     whispers-configuration-auto-start?
                                    (default #t)))

(define-record-type* <whispers-user-group>
  whispers-user-group make-whispers-user-group
  whispers-user-group?
  this-whispers-user-group
  ;; A string
  (user                 whispers-user-group-user
                        (default "johndoe"))
  ;; A string
  (group                whispers-user-group-group
                        (default "loner")))

(define (user-container-name user)
  "Return the string \"root-user\" if the string USER is equal to
\"root\", return the string USER otherwise. This is exported to other
modules as a dirty ad-hoc convenience function, for use by modules which
extend a sub-tree whose first branching is done on a per-handle basis."
  (if (equal? user "root")
      "root-user"
      user))

(define (lieutenant-path->socket-file-path lieutenant-path)
  "Returns as a string the expected path to the socket of the whispers
lieutenant shepherd at the whispers path defined by the string
LIEUTENANT-PATH. This is exported to other modules as a dirty ad-hoc
convenience function."
  (string-append "/run/whispers"
                 lieutenant-path
                 "/unix-sockets/"
                 (last (string-split lieutenant-path
                                     #\/))
                 ".sock"))

(define (shepherd-service-lieutenate parent-path
                                     parent-user
                                     parent-group
                                     config)
  "Message-passing along the strings PARENT-USER and PARENT-GROUP and
configurable by CONFIG, a record of the <whispers-configuration> type,
returns a one argument procedure taking a root shepherd service
extension as its single parameter and returning a <shepherd-service>
type guix records for a shepherd service of a lieutenant of a whispers
service at the top of a whispers tree or sub-tree, as defined by the
string PARENT-PATH."
  (lambda (extension)
    (if (whispers-configuration? config)
        ((whispers-shepherd-tree parent-path
                                 parent-user
                                 parent-group) config)
        ((service-extension-compute extension) config))))

(define (shepherd-services-lieutenate parent-path parent-user parent-group)
  "Message-passing along the strings PARENT-USER and PARENT-GROUP,
returns a one argument procedure taking a record of the
<whispers-configuration> type and returning a list of <shepherd-service>
type guix records for the lieutenants of a whispers service at the top
of a whispers tree or sub-tree, as defined by the string PARENT-PATH."
  (lambda (lieutenant)
    (map (shepherd-service-lieutenate parent-path
                                      parent-user
                                      parent-group
                                      (service-value lieutenant))
         (filter (lambda (extension)
                   (equal? (service-extension-target extension)
                           shepherd-root-service-type))
                 (service-type-extensions (service-kind lieutenant))))))

(define (lieutenants-list parent-path
                          parent-user
                          parent-group
                          config)
  (apply append
         (apply append
                (map (shepherd-services-lieutenate parent-path
                                                   parent-user
                                                   parent-group)
                     (whispers-configuration-lieutenants config)))))

(define (shepherd-configuration-file parent-path
                                     parent-user
                                     parent-group
                                     config)
  "Returns a guix store shepherd configuration file for a whispers
shepherd service at the root of a whispers tree or sub-tree, as defined
by the string PARENT-PATH, message-passing along the strings PARENT-USER
and PARENT-PATH, configurable by CONFIG, a record of the
<whispers-configuration> type."
  ;; copied and modified from guix's gnu/services/shepherd.scm
  (let* ((lieutenants (lieutenants-list parent-path
                                        parent-user
                                        parent-group
                                        config))
         (files (map shepherd-service-file lieutenants)))
    (define shepherd-config
      #~(begin (unless (null? '#$files)
                 (apply register-services (map load '#$files)))
               (map apply
                    (map (lambda whatever start-in-the-background)
                         '#$(map shepherd-service-provision
                                 (filter shepherd-service-auto-start?
                                         lieutenants)))
                    '#$(map list (map shepherd-service-provision
                                      (filter shepherd-service-auto-start?
                                              lieutenants))))))
    (scheme-file (string-append
                  (symbol->string (whispers-configuration-name config))
                  ".conf")
                 shepherd-config)))

(define (whispers-shepherd-tree parent-path parent-user parent-group)
  "Returns a list of one <shepherd-service> type guix record defining
the shepherd service at the top of a shepherd tree of sub-tree, equipped
with a shepherd configuration file defining shepherd services for the
lieutenants of the returned serivce.

It takes the following parameters:
 - The string PARENT-PATH is the location of the shepherd service
daemonizing the returned whispers service in the top-level whispers tree
of the OS.
 - The string PARENT-USER is the user name of the user as which runs the
shepherd service daemonizing the returned whispers service in the
top-level whispers tree of the OS.
 - The string PARENT-GROUP is the group name of the group as which runs
the shepherd service daemonizing the returned whispers service in the
top-level whispers tree of the OS."
  (lambda (config)
    (list
     (let* ((name-sym (whispers-configuration-name config))
            (name-str (symbol->string name-sym))
            (user (whispers-configuration-user config))
            (group (whispers-configuration-group config))
            (timeout (whispers-configuration-timeout config))
            (lieutenants (lieutenants-list parent-path
                                           parent-user
                                           parent-group
                                           config))
            (lieutenants-names-sym (map car
                                        (map shepherd-service-provision
                                             lieutenants)))
            (lieutenants-names-str (map symbol->string
                                        lieutenants-names-sym))
            (lieutenants-path (string-append parent-path
                                             name-str
                                             "/"))
            (requires (whispers-configuration-requires config))
            (parent-runtime-dir (string-append "/run"
                                               parent-path))
            (runtime-dir (string-append parent-runtime-dir
                                        name-str))
            (pid-file (string-append runtime-dir
                                     "/"
                                     name-str
                                     ".pid"))
            (unix-socket-dir (string-append runtime-dir
                                            "/"
                                            "unix-sockets"))
            (unix-socket (string-append unix-socket-dir
                                        "/"
                                        name-str
                                        ".sock"))
            (superior-runtime-dir (dirname parent-runtime-dir))
            (superior-unix-socket-dir (string-append superior-runtime-dir
                                                     "/"
                                                     "unix-sockets"))
            (superior-unix-socket (string-append superior-unix-socket-dir
                                                 "/"
                                                 (basename
                                                  superior-runtime-dir)
                                                 ".sock"))
            (parent-log-dir (string-append "/var/log"
                                           parent-path))
            (log-dir (string-append parent-log-dir
                                    name-str))
            (log-file (string-append log-dir
                                     "/"
                                     name-str
                                     ".log"))
            (echo-package (whispers-configuration-coreutils-package config))
            (rmdir-package echo-package)
            (mount-pkg (whispers-configuration-util-linux-package config))
            (pre-start? (whispers-configuration-pre-start-action? config))
            (post-stop? (whispers-configuration-post-stop-action? config))
            (extra-actions (whispers-configuration-extra-actions config)))
       (shepherd-service
        (documentation "Shepherd controllable from the root shepherd.")
        (provision `(,name-sym))
        (requirement requires)
        (modules (append '((shepherd comm)
                           (shepherd support)
                           (ice-9 match)
                           (ice-9 ftw)
                           (ice-9 regex))
                         %default-modules))
        (start #~(lambda whatever
                   (perform-service-action (lookup-service '#$name-sym)
                                           'make-tmpfs
                                           #$runtime-dir
                                           #$user
                                           #$group
                                           #$(number->string #o755 8))
                   (perform-service-action (lookup-service '#$name-sym)
                                           'make-tmpfs
                                           #$unix-socket-dir
                                           #$user
                                           #$group
                                           #$(number->string #o700 8))
                   (perform-service-action (lookup-service '#$name-sym)
                                           'make-directory
                                           #$log-dir
                                           #$user
                                           #$group
                                           #$(number->string #o755 8))
                   (when (file-exists? #$unix-socket)
                     (delete-file #$unix-socket))
                   (when #$pre-start?
                     (perform-service-action (lookup-service '#$name-sym)
                                             'pre-start))
                   ((make-forkexec-constructor
                     (list "/run/current-system/profile/bin/shepherd"
                           (string-append
                            "--config="
                            #$(shepherd-configuration-file lieutenants-path
                                                           user
                                                           group
                                                           config))
                           (string-append "--pid="
                                          #$pid-file)
                           "-l"
                           #$log-file
                           "-s"
                           #$unix-socket)
                     #:user #$(if (equal? user parent-user)
                                  #f
                                  user)
                     #:group #$(if (equal? group parent-group)
                                   #f
                                   group)
                     #:pid-file #$pid-file
                     #:pid-file-timeout #$timeout))))
        (actions
         (append
          extra-actions
          (list
           (shepherd-action
            (name 'make-directory)
            (documentation "Create a directory at the string PATH
if it is not exiting. Set the uid of the sting USER, the gid of the
string GROUP and set the string MODE converted to an octal number as the
directory's permission bits.")
            (procedure
             #~(lambda (running path user group mode)
                 (unless (file-exists? path)
                   (display "Directory ")
                   (display path)
                   (display " not existing, creating.")
                   (display "\n")
                   (mkdir path))
                 (let ((uid (passwd:uid (getpwnam user)))
                       (gid (group:gid (getgrnam group))))
                   (chown path uid gid))
                 (chmod path (string->number mode 8)))))
           (shepherd-action
            (name 'make-tmpfs)
            (documentation "After creating a directory at the mount point if
necessary, mount a filesystem of type tmpfs at the mount point defined
by the string PATH if it is not already mounted, owned by the uid of the
string USER and with group set at the gid of the string GROUP, and mount
point permissions set to the string MODE taken as an octal number.")
            (procedure
             #~(lambda (running path user group mode)
                 (let ((uid (number->string (passwd:uid (getpwnam user))))
                       (gid (number->string (group:gid (getgrnam group)))))
                   (perform-service-action (lookup-service '#$name-sym)
                                           'clear-tmpfs
                                           path)
                   (perform-service-action (lookup-service '#$name-sym)
                                           'make-directory
                                           path
                                           user
                                           group
                                           mode)
                   ((make-system-constructor
                     #$(file-append mount-pkg "/bin/findmnt")
                     " "
                     path
                     " "
                     "&&"
                     " "
                     #$(file-append echo-package "/bin/echo")
                     " "
                     "tmpfs at"
                     " "
                     path
                     " "
                     "already mounted, aborting make-tmpfs action."
                     " "
                     "||"
                     " "
                     #$(file-append mount-pkg "/bin/mount")
                     " "
                     "-t"
                     " "
                     "tmpfs"
                     " "
                     "-o"
                     " "
                     (string-append "rw,nosuid,nodev,relatime"
                                    ",size=1633420k,nr_inodes=408355"
                                    ",mode="
                                    mode
                                    ",uid="
                                    uid
                                    ",gid="
                                    gid)
                     " "
                     "tmpfs"
                     " "
                     path))))))
           (shepherd-action
            (name 'subdirs-list)
            (documentation "Return the list of non-trivial
subdirectories of the directory whose path is the string PATH.")
            (procedure
             #~(lambda (running path)
                 (if (scandir path)
                     (let* ((dir? (lambda (file-name)
                                    (equal? 'directory
                                            (stat:type (stat file-name)))))
                            (subdir? (lambda (file-name)
                                       (and (dir? file-name)
                                            (not (equal? file-name
                                                         (string-append
                                                          path
                                                          "/"
                                                          ".")))
                                            (not (equal? file-name
                                                         (string-append
                                                          path
                                                          "/"
                                                          ".."))))))
                            (absolutes (map string-append
                                            (map (lambda (whatever)
                                                   (string-append path
                                                                  "/"))
                                                 (scandir path))
                                            (scandir path))))
                       (filter subdir? absolutes))
                     '()))))
           (shepherd-action
            (name 'clear-tmpfs)
            (documentation "Unmount a filesystem of type tmpfs at the
mount point defined by the string PATH if it is mounted. Delete the
mount point after unmounting.")
            (procedure
             #~(lambda (running path)
                 ;; It may be necessary to 'clear-tmpfs down the
                 ;; directory tree when a lieutenant is stopped using its
                 ;; internal stop root action instead of being stopped by
                 ;; its stop action in its controlling shepherd. Possibly
                 ;; also necessary in case a whispers shepherd process
                 ;; unexpectedly dies.
                 (let ((serv-obj (lookup-service '#$name-sym)))
                   (map (lambda (subdir-path)
                          (perform-service-action serv-obj
                                                  'clear-tmpfs
                                                  subdir-path))
                        (perform-service-action serv-obj
                                                'subdirs-list
                                                path)))
                 ((make-system-constructor
                   #$(file-append mount-pkg "/bin/findmnt")
                   " "
                   path
                   " "
                   "&&"
                   " "
                   #$(file-append echo-package "/bin/echo")
                   " "
                   "tmpfs at"
                   " "
                   path
                   " "
                   "mounted, proceeding with clear-tmpfs action."
                   " "
                   "&&"
                   " "
                   #$(file-append mount-pkg "/bin/umount")
                   " "
                   "-t"
                   " "
                   "tmpfs"
                   " "
                   path
                   " "
                   "&&"
                   " "
                   #$(file-append rmdir-package "/bin/rmdir")
                   " "
                   path)))))
           (shepherd-action
            (name 'socket)
            (documentation "Return a string containing the path to the
socket file of the shepherd daemon daemonized by this service.")
            (procedure
             #~(lambda (running)
                 #$unix-socket)))
           (shepherd-action
            (name 'display-socket)
            (documentation "Display to standard output a string containing
the path to the socket file of the shepherd daemon daemonized by this
service.")
            (procedure
             #~(lambda (running)
                 (let ((serv-obj (lookup-service '#$name-sym)))
                   (local-output (perform-service-action serv-obj
                                                         'socket))))))
           (shepherd-action
            (name 'superior-socket)
            (documentation "Return a string containing the path to the
socket file of the shepherd daemon daemonized by the whispers superior
of this service.")
            (procedure
             #~(lambda (running)
                 #$superior-unix-socket)))
           (shepherd-action
            (name 'display-superior-socket)
            (documentation "Display to standard output a string containing
the path to the socket file of the shepherd daemon daemonized by the
whispers superior of this service.")
            (procedure
             #~(lambda (running)
                 (let ((serv-obj (lookup-service '#$name-sym))
                       (sup 'superior-socket))
                   (local-output (perform-service-action serv-obj
                                                         sup))))))
           (shepherd-action
            (name 'display-load-path)
            (documentation "For debugging purposes, display the
guile load path that is enforced in this action's prodedure scope.")
            (procedure
             #~(lambda (running)
                 (display %load-path)
                 (display "\n"))))
           (shepherd-action
            (name 'display-load-compiled-path)
            (documentation "For debugging purposes, display the
guile compiled load path that is enforced in this action's prodedure
scope.")
            (procedure
             #~(lambda (running)
                 (display %load-compiled-path)
                 (display "\n"))))
           (shepherd-action
            (name 'display-lieutenant-action)
            (documentation "Perform the action named by the string
ACTION-STR of the service providing the string SERVICE-STR of the
shepherd daemon daemonized by this whispers service. The arguement
strings ARGS are passed to the action. For debugging purposes, display
the return value of the aforementioned lieutenant service's action.")
            (procedure
             #~(lambda (running action-str service-str . args)
                 (display (apply perform-service-action
                                 (append (list '#$name-sym
                                               'lieutenant-action
                                               action-str
                                               service-str)
                                         args)))
                 (display "\n"))))
           ;; FIXME?: there's problems with this? Maybe risky.
           (shepherd-action
            (name 'lieutenant-action)
            (documentation "Perform the action named by the string
ACTION-STR of the service providing the string SERVICE-STR of the
shepherd daemon daemonized by this whispers service. The arguement
strings ARGS are passed to the action. Return the return value of
the aforementioned lieutenant service's action.")
            (procedure
             #~(lambda (running action-str service-str . args)
                 ;; inspired by (shepherd scripts herd).
                 (define lieutenant-port
                   (let ((serv-obj (lookup-service '#$name-sym)))
                     (open-connection (perform-service-action serv-obj
                                                              'socket))))
                 (let ((action-sym (string->symbol action-str))
                       (service-sym (string->symbol service-str)))
                   (write-command (shepherd-command action-sym
                                                    service-sym
                                                    #:arguments
                                                    args)
                                  lieutenant-port))
                 (define ret
                   (match (read lieutenant-port)
                     (('reply ('version 0)
                              ('result result) ('error #f)
                              ('messages messages))
                      (unless (null? messages)
                        (for-each (lambda (message)
                                    (display message)
                                    (display "\n"))
                                  messages))
                      (if (pair? result)
                          (car result)
                          #f))))
                 (close-port lieutenant-port)
                 ret)))
           ;; FIXME: do not use: bad hangs (bidirectional communication?)
           (shepherd-action
            (name 'superior-action)
            (documentation "Perform the action named by the string
ACTION-STR of the service providing the string SERVICE-STR of the
shepherd daemon daemonizing the shepherd daemon of the whispers superior
of this whispers service. The arguement strings ARGS are passed to the
action. Return the return value of the aforementioned superior shepherd's
action.")
            (procedure
             #~(lambda (running action-str service-str . args)
                                        ; inspired by (shepherd scripts herd)
                 (define superior-port
                   (open-connection (car (perform-service-action
                                          (lookup-service '#$name-sym)
                                          'superior-socket))))
                 (let ((action-sym (string->symbol action-str))
                       (service-sym (string->symbol service-str)))
                   (write-command (shepherd-command action-sym
                                                    service-sym
                                                    #:arguments
                                                    args)
                                  superior-port))
                 (define ret
                   (match (read superior-port)
                     (('reply ('version 0)
                              ('result result) ('error #f)
                              ('messages messages))
                      (unless (null? messages)
                        (for-each (lambda (message)
                                    (display message)
                                    (display "\n"))
                                  messages))
                      (if (pair? result)
                          (car result)
                          #f))))
                 (close-port superior-port)
                 ret))))))
        (stop #~(lambda (pid)
                  (map (lambda (lieutenant-name)
                         (perform-service-action (lookup-service '#$name-sym)
                                                 'lieutenant-action
                                                 "stop"
                                                 lieutenant-name))
                       '#$lieutenants-names-str)
                  (define ret ((make-kill-destructor) pid))
                  (when #$post-stop?
                    (perform-service-action (lookup-service '#$name-sym)
                                            'post-stop))
                  (perform-service-action (lookup-service '#$name-sym)
                                          'clear-tmpfs
                                          #$unix-socket-dir)
                  (perform-service-action (lookup-service '#$name-sym)
                                          'clear-tmpfs
                                          #$runtime-dir)
                  ret))
        (auto-start? (whispers-configuration-auto-start? config)))))))

(define (whispers-service-type? service)
  "Returns a predicate which is true if SERVICE, a service object, is a
service of type whispers-service-type"
  (equal? (service-kind service) whispers-service-type))

(define (whispers-tree-log-files parent-path)
  "Returns a one argument procedure taking a record of the
<whispers-configuration> type as its argement and returning the list of
log files from the whispers-service-type services located under the
string PARENT-PATH in the whispers service top-level tree or sub-tree
configured by the aforementioned configuration record."
  (lambda (config)
    (let* ((name-sym (whispers-configuration-name config))
           (name-str (symbol->string name-sym))
           (lieutenants (whispers-configuration-lieutenants config)))
      (append `(,(string-append "/var/log"
                                parent-path
                                "/"
                                name-str
                                ".log"))
              (apply append
                     (map (whispers-tree-log-files (string-append parent-path
                                                                  "/"
                                                                  name-str))
                          (map service-value
                               (filter whispers-service-type?
                                       lieutenants))))))))

(define (whispers-log-rotation config)
  "Returns a record of the <log-rotation> type specifying the log
rotations of the whispers-service-type type services contained inside
the whispers tree of a top-level service of the whisper-service-type
type, configurable by CONFIG, a record of the <whispers-configuration>
type."
  (list (log-rotation (frequency 'daily)
                      (files ((whispers-tree-log-files "") config)))))

(define (whispers-user-accounts config)
  "Returns a list of group and user records needed to support a whispers
service tree, configuration by CONFIG, a record of the
<whispers-configuration> type."
  (let* ((user (whispers-configuration-user config))
         (extend-user? (whispers-configuration-extend-user? config))
         (group (whispers-configuration-group config))
         (extend-group? (whispers-configuration-extend-group? config))
         (lieutenants (whispers-configuration-lieutenants config))
         (whispers-lieutenants (filter whispers-service-type? lieutenants)))
    (append (if extend-group?
                (list (user-group (name group)
                                  (system? #t)))
                (list))
            (if extend-user?
                (list (user-account (name user)
                                    (group group)
                                    (create-home-directory? #f)
                                    (system? #t)))
                (list))
            (apply append
                   (map whispers-user-accounts
                        (map service-value
                             whispers-lieutenants))))))

(define (collect-compute-r service-type-target)
  "Return a one argument procedure taking a service object as its single
argument and returning a list appending outputs of the compute
procedures of all the extensions of target SERVICE-TYPE-TARGET from the
services which are not themselves of type whispers-service-type from a
whispers top-level tree or sub-tree defined by the service given as its
argument."
  (lambda (service)
    (if (whispers-service-type? service)
        (let* ((config (service-value service))
               (lieutenants (whispers-configuration-lieutenants config)))
          (apply append
                 (map (collect-compute-r service-type-target)
                      lieutenants)))
        (let* ((kind (service-kind service))
               (exts (filter (lambda (extension)
                               (equal? (service-extension-target extension)
                                       service-type-target))
                             (service-type-extensions kind)))
               (computes (map service-extension-compute exts)))
          (apply append
                 (map (lambda (compute)
                        (compute (service-value service)))
                      computes))))))

(define (collect-compute service-type-target)
  "Return a one argument procedure taking a whispers-configuration type
guix record as its single argument and returning a list appending
outputs of the compute procedures of all the extensions of target
SERVICE-TYPE-TARGET from the services which are not themselves of type
whispers-service-type from a whispers top-level tree defined by the
service given as its argument."
  (lambda (config)
    (let ((lieutenants (whispers-configuration-lieutenants config)))
      (apply append (map (collect-compute-r service-type-target)
                         lieutenants)))))

(define whispers-service-type
  (let ((coreutils-package whispers-configuration-coreutils-package)
        (util-linux-package whispers-configuration-util-linux-package)
        (whispers-pkg whispers-configuration-whispers-package)
        (extra-packages whispers-configuration-extra-packages)
        (get-lieutenants whispers-configuration-lieutenants))
    (service-type
     (name 'whispers)
     (description "Shepherd process controllable from the root shepherd.")
     (extensions
      (list (service-extension shepherd-root-service-type
                               (whispers-shepherd-tree "/" "root" "root"))
            (service-extension rottlog-service-type
                               whispers-log-rotation)
            (service-extension account-service-type
                               whispers-user-accounts)
            (service-extension profile-service-type
                               (lambda (config)
                                 (append (list (coreutils-package config)
                                               (util-linux-package config)
                                               (whispers-pkg config))
                                         (extra-packages config))))
            (service-extension profile-service-type
                               (collect-compute profile-service-type))
            (service-extension rottlog-service-type
                               (collect-compute rottlog-service-type))
            (service-extension rottlog-service-type
                               (collect-compute account-service-type))
            (service-extension mcron-service-type
                               (collect-compute mcron-service-type))))
     (compose concatenate)
     (extend (lambda (config lieutenants-new)
               (whispers-configuration
                (inherit config)
                (lieutenants (append (get-lieutenants config)
                                     lieutenants-new)))))
     (default-value (whispers-configuration)))))