aboutsummaryrefslogtreecommitdiff
path: root/src/property/systemd.lisp
blob: b02c29fa903dba6af88990b4e869f522c4dafe94 (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
;;; Consfigurator -- Lisp declarative configuration management system

;;; Copyright (C) 2021  Sean Whitton <spwhitton@spwhitton.name>

;;; This file 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, or (at your option)
;;; any later version.

;;; This file 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 this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :consfigurator.property.systemd)
(named-readtables:in-readtable :consfigurator)

;;; Using systemctl(1), we cannot enable or disable user units unless the
;;; user's service manager is actually running, and using loginctl(1), we
;;; cannot enable or disable user lingering unless systemd is PID 1.
;;;
;;; Possibly we should manually create the symlinks in the former case, and
;;; touch and delete the file under /var/lib/systemd/linger in the latter
;;; case, so that more configuration is applicable to unbooted chroots.

(defun systemctl (fn user &rest args &aux (args (cons "systemctl" args)))
  (apply fn (if user (apply #'systemd--user args) args)))

(defprop started :posix (service &optional user)
  (:desc #?"systemd service ${service} started")
  (:check (or (service:no-services-p)
              (zerop (systemctl #'mrun user :for-exit "is-active" service))))
  (:apply (systemctl #'mrun user "start" service)))

(defprop stopped :posix (service &optional user)
  (:desc #?"systemd service ${service} stopped")
  (:check (or (service:no-services-p)
              (plusp (systemctl #'mrun user :for-exit "is-active" service))))
  (:apply (systemctl #'mrun user "stop" service)))

(defprop enabled :posix (service &optional user)
  (:desc #?"systemd service ${service} enabled")
  (:check (or (and user (service:no-services-p))
              (zerop (systemctl #'mrun user :for-exit "is-enabled" service))))
  (:apply (systemctl #'mrun user "enable" service)))

(defprop disabled :posix (service &optional user)
  (:desc #?"systemd service ${service} disabled")
  (:check
   (or
    (and user (service:no-services-p))
    (let ((status
            (stripln
             (systemctl #'run user :may-fail "is-enabled" service))))
      (or (string-prefix-p "linked" status)
          (string-prefix-p "masked" status)
          (memstring=
           status '("static" "disabled" "generated" "transient" "indirect"))))))
  (:apply (systemctl #'mrun user "disable" service)))

(defprop masked :posix (service &optional user)
  (:desc #?"systemd service ${service} masked")
  (:check
   (or (and user (service:no-services-p))
       (string-prefix-p
        "masked"
        (systemctl #'run user :may-fail "is-enabled" service))))
  (:apply (systemctl #'mrun user "mask" service))
  (:unapply (if (and user (service:no-services-p))
                :no-change
                (systemctl #'mrun user "unmask" service))))

(defprop lingering-enabled :posix (user)
  (:desc #?"User lingering enable for ${user}")
  (:check (or (service:no-services-p)
              (memstring= "Linger=yes" (runlines "loginctl" "show-user" user))))
  (:apply (mrun "loginctl" "enable-linger" user))
  (:unapply (if (service:no-services-p)
                :no-change
                (mrun "loginctl" "disable-linger" user))))