aboutsummaryrefslogtreecommitdiff
path: root/gnu/build/dbus-service.scm
blob: 3ae45ad755fd88e42520bf636a1eb0e74a1d4f73 (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
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix 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.
;;;
;;; GNU Guix 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 GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;;; This module contains procedures to interact with D-Bus via the 'dbus-send'
;;; command line utility.  Before using any public procedure
;;;
;;; Code:

(define-module (gnu build dbus-service)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:autoload (d-bus protocol connections) (d-bus-conn?
                                           d-bus-conn-flush
                                           d-bus-connect
                                           d-bus-disconnect
                                           d-bus-session-bus-address
                                           d-bus-system-bus-address)
  #:autoload (d-bus protocol messages) (MESSAGE_TYPE_METHOD_CALL
                                        d-bus-headers-ref
                                        d-bus-message-body
                                        d-bus-message-headers
                                        d-bus-read-message
                                        d-bus-write-message
                                        header-PATH
                                        header-DESTINATION
                                        header-INTERFACE
                                        header-MEMBER
                                        header-SIGNATURE
                                        make-d-bus-message)
  #:export (%dbus-query-timeout

            initialize-dbus-connection!
            %current-dbus-connection
            send-dbus
            call-dbus-method

            dbus-available-services
            dbus-service-available?

            with-retries))

(define %dbus-query-timeout 2)          ;in seconds

;;; Use Fibers' sleep to enable cooperative scheduling in Shepherd >= 0.9.0,
;;; which is required at least for the Jami service.
(define sleep*
  (lambda ()                            ;delay execution
    (if (resolve-module '(fibers) #f #:ensure #f)
        (module-ref (resolve-interface '(fibers)) 'sleep)
        (begin
          (format #t "Fibers not available -- blocking 'sleep' in use~%")
          sleep))))

;;;
;;; Utilities.
;;;

(define-syntax-rule (with-retries n delay body ...)
  "Retry the code in BODY up to N times until it doesn't raise an exception nor
return #f, else raise an error.  A delay of DELAY seconds is inserted before
each retry."
  (let loop ((attempts 0))
    (catch #t
      (lambda ()
        (let ((result (begin body ...)))
          (if (not result)
              (error "failed attempt" attempts)
              result)))
      (lambda args
        (if (< attempts n)
            (begin
              ((sleep*) delay)            ;else wait and retry
              (loop (+ 1 attempts)))
            (error "maximum number of retry attempts reached"
                   body ... args))))))


;;;
;;; Low level wrappers above AC/D-Bus.
;;;

;; The active D-Bus connection (a parameter) used by the other procedures.
(define %current-dbus-connection (make-parameter #f))

(define* (initialize-dbus-connection!
          #:key (address (or (d-bus-session-bus-address)
                             (d-bus-system-bus-address))))
  "Initialize the D-Bus connection.  ADDRESS should be the address of the D-Bus
session, e.g. \"unix:path=/var/run/dbus/system_bus_socket\", the default value
if ADDRESS is not provided and DBUS_SESSION_BUS_ADDRESS is not set.  Return
the initialized D-Bus connection."
  ;; Clear current correction if already active.
  (when (d-bus-conn? (%current-dbus-connection))
    (d-bus-disconnect (%current-dbus-connection)))

  (let ((connection (d-bus-connect address)))
    (%current-dbus-connection connection) ;update connection parameter
    (call-dbus-method "Hello"))           ;initial handshake

  (%current-dbus-connection))

(define* (send-dbus message #:key
                    (connection (%current-dbus-connection))
                    timeout)
  "Send a D-Bus MESSAGE to CONNECTION and return the body of its reply.  Up to
READ-RETRIES replies are read until a matching reply is found, else an error
is raised.  MESSAGE is to be constructed with `make-d-bus-message'.  When the
body contains a single element, it is returned directly, else the body
elements are returned as a list.  TIMEOUT is a timeout value in seconds."
  (let ((serial     (d-bus-write-message connection message))
        (start-time (current-time time-monotonic))
        (timeout* (or timeout %dbus-query-timeout)))
    (d-bus-conn-flush connection)
    (let retry ()
      (when (> (time-second (time-difference (current-time time-monotonic)
                                             start-time))
               timeout*)
        (error 'dbus "fail to get reply in timeout" timeout*))
      (let* ((reply (d-bus-read-message connection))
             (reply-headers (d-bus-message-headers reply))
             (reply-serial (d-bus-headers-ref reply-headers 'REPLY_SERIAL))
             (error-name (d-bus-headers-ref reply-headers 'ERROR_NAME))
             (body (d-bus-message-body reply)))
        ;; Validate the reply matches the message.
        (when error-name
          (error 'dbus "method failed with error" error-name body))
        ;; Some replies do not include a serial header, such as the for the
        ;; org.freedesktop.DBus NameAcquired one.
        (if (and reply-serial (= serial reply-serial))
            (match body
              ((x x* ..1)               ;contains 2 ore more elements
               body)
              ((x)
               x)                       ;single element; return it directly
              (#f #f))
            (retry))))))

(define (argument->signature-type argument)
  "Infer the D-Bus signature type from ARGUMENT."
  ;; XXX: avoid ..1 when using vectors due to a bug (?) in (ice-9 match).
  (match argument
    ((? boolean?) "b")
    ((? string?) "s")
    (#((? string?) (? string?) ...) "as")
    (#(((? string?) . (? string?))
       ((? string?) . (? string?)) ...) "a{ss}")
    (_ (error 'dbus "no rule to infer type from argument" argument))))

(define* (call-dbus-method method
                           #:key
                           (path "/org/freedesktop/DBus")
                           (destination "org.freedesktop.DBus")
                           (interface "org.freedesktop.DBus")
                           (connection (%current-dbus-connection))
                           arguments
                           timeout)
  "Call the D-Bus method specified by METHOD, PATH, DESTINATION and INTERFACE.
The currently active D-Bus CONNECTION is used unless explicitly provided.
Method arguments may be provided via ARGUMENTS sent as the message body.
TIMEOUT limit the maximum time to allow for the reply.  Return the body of the
reply."
  (let ((message (make-d-bus-message
                  MESSAGE_TYPE_METHOD_CALL 0 #f '()
                  `#(,(header-PATH        path)
                     ,(header-DESTINATION destination)
                     ,(header-INTERFACE   interface)
                     ,(header-MEMBER      method)
                     ,@(if arguments
                           (list (header-SIGNATURE
                                  (string-join
                                   (map argument->signature-type arguments)
                                   "")))
                           '()))
                  arguments)))
    (send-dbus message #:connection connection #:timeout timeout)))


;;;
;;; Higher-level, D-Bus procedures.
;;;

(define (dbus-available-services)
  "Return the list of available (acquired) D-Bus services."
  (let ((names (vector->list (call-dbus-method "ListNames"))))
    ;; Remove entries such as ":1.7".
    (remove (cut string-prefix? ":" <>) names)))

(define (dbus-service-available? service)
  "Predicate to check for the D-Bus SERVICE availability."
  (member service (dbus-available-services)))

;; Local Variables:
;; eval: (put 'with-retries 'scheme-indent-function 2)
;; End:
alled during a system reconfigure. * gnu/system.scm (operating-system-bootloader-crypto-devices): Memoize procedure. Include the mapped devices source location information in the warnings. Add a hint to help users fix the warning. Maxim Cournoyer 2022-04-07services: shepherd: Default to version 0.9....* gnu/services/shepherd.scm (scm->go): Define 'shepherd&co' and pass it to 'with-extensions'. (shepherd-configuration-file): Call 'start-in-the-background' when it is defined. (<shepherd-configuration>)[shepherd]: Default to SHEPHERD-0.9. * gnu/system.scm (hurd-default-essential-services): Use SHEPHERD-0.8. Ludovic Courtès 2022-03-21system: Use 'shadow-with-man-pages' in %BASE-PACKAGES-UTILS....* gnu/system.scm (%base-packages-utils): Replace SHADOW with SHADOW-WITH-MAN-PAGES. Ludovic Courtès 2022-03-16system: Improve 'read-boot-parameters' incompatibility diagnostic....Previously, when reading an incompatible "parameters" file, 'guix system' would print a warning and then crash with a wrong-type-arg backtrace because code expects 'read-boot-parameters' to always return a <boot-parameters> record. * gnu/system.scm (read-boot-parameters): Upon incompatibility, raise an error instead of returning #f. Also raise a '&fix-hint' condition. * tests/boot-parameters.scm ("read, construction, mandatory fields"): Define 'test-read-boot-parameters' as a macro; expect 'formatted-message?' exceptions rather than #f returns. Ludovic Courtès 2022-03-07system: Set kernel name for riscv64-linux....* gnu/system.scm (system-linux-image-file-name): Add option for riscv64. Efraim Flashner 2022-03-01initrd: Use non-hyphenated kernel command-line parameter names....This is to make it less surprising, given the common convention sets forth by the kernel Linux command-line parameters. * gnu/build/linux-boot.scm (boot-system): Rename '--load', '--repl', '--root' and '--system' to 'gnu.load', 'gnu.repl', 'root' and 'gnu.system', respectively. Adjust doc. (find-long-option): Adjust doc. * gnu/installer/parted.scm (installer-root-partition-path): Adjust accordingly. * gnu/system.scm (bootable-kernel-arguments): Add a VERSION argument and update doc. Use VERSION to conditionally return old style vs new style initrd arguments. (%boot-parameters-version): Increment to 1. (operating-system-boot-parameters): Adjust doc. (operating-system-boot-parameters-file): Likewise. * gnu/system/linux-initrd.scm (raw-initrd, base-initrd): Likewise. * doc/guix.texi: Adjust doc. * gnu/build/activation.scm (boot-time-system): Adjust accordingly. * gnu/build/hurd-boot.scm (boot-hurd-system): Likewise. * gnu/packages/commencement.scm (%final-inputs-riscv64): Adjust comment. Maxim Cournoyer 2022-03-01system: Streamline operating-system-boot-parameters-file a bit....* gnu/system.scm (operating-system-boot-parameters-file) [SYSTEM-KERNEL-ARGUMENTS?]: Remove unused argument (it had no callers) and adjust doc, moving the self-referential tip to... * gnu/system.scm (operating-system-boot-parameters): ... here, reworded for clarity. Suggested-by: Ludovic Courtès <ludo@gnu.org> Maxim Cournoyer 2022-03-01system: Add a version field to the <boot-parameters> record....This version field exposes the (already present) version information of a boot parameters file. * gnu/system.scm (%boot-parameters-version): New variable. (<boot-parameters>)[version]: New field. (read-boot-parameters): Use it. (operating-system-boot-parameters-file): Likewise. * tests/boot-parameters.scm (test-read-boot-parameters): Use %boot-parameters-version as the default version value in the template. Maxim Cournoyer 2022-01-01system: Allow 'chfn' to change the user's full name....Fixes <https://issues.guix.gnu.org/52539>. Reported by Jacob First <jacob.first@member.fsf.org>. * gnu/build/accounts.scm (allocate-passwd): Add comment as to why 'real-name' is taken from PREVIOUS. Add (not system?) to the condition. * gnu/system.scm (operating-system-etc-service) <login.defs>: Add "CHFN_RESTRICT". * gnu/system.scm (%setuid-programs): Add "chfn". * gnu/system/pam.scm (base-pam-services): Add "chfn". * doc/guix.texi (User Accounts): Document it. Ludovic Courtès 2021-12-08system: Mark 'services' field as thunked....This allows us to make services dependent on (%current-system), for example. * gnu/system.scm (<operating-system>)[services]: Mark as thunked. Ludovic Courtès 2021-11-23Merge branch 'master' into core-updates-frozenLudovic Courtès 2021-11-23system: Filter out boot dependencies from swap-space....* gnu/systems.scm (swap-services): Filter them. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Josselin Poiret 2021-11-23system: Warn about swap-devices format change...* gnu/system.scm (warn-swap-devices-change, %warn-swap-devices-change): Add them. * gnu/system.scm (operating-system) [swap-devices]: Use it. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Josselin Poiret 2021-11-23system: Rework swap space support, add dependencies....* gnu/system/file-systems.scm (swap-space): Add it. * gnu/system.scm (operating-system)[swap-devices]: Update comment. * gnu/services/base.scm (swap-space->shepherd-service-name, swap-deprecated->shepherd-service-name, swap->shepherd-service-name): Add them. * gnu/services/base.scm (swap-service-type, swap-service): Use the new records. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Josselin Poiret 2021-11-17gnu: system: Improve location of some configuration warnings....* gnu/bootloader.scm (%warn-target-field-deprecation): Remove it. * gnu/bootloader.scm (warn-target-field-deprecation): Use define-with-syntax-properties. * gnu/system.scm (ensure-setuid-program-list): Ditto. Also rename the 'location' variable to 'properties'. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Josselin Poiret 2021-10-12Merge remote-tracking branch 'origin/master' into core-updates-frozen.Mathieu Othacehe 2021-10-02system: Introduce the os-release file....* gnu/system.scm (os-release): New procedure. (operating-system-etc-service): Use it. Mathieu Othacehe 2021-10-02system: Add guix-icons to the base packages....* gnu/system.scm (%base-packages-artwork): New variable. (%base-packages): Add it. Mathieu Othacehe 2021-09-23system: Add xfsprogs to base packages....This makes them available in the Guix System installer. * gnu/system.scm (%base-packages-disk-utilities): Add xfsprogs. Tobias Geerinckx-Rice