all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob fd56c8dae7e7606d556c88c0d2dfac112e15901f 9154 bytes (raw)
name: gnu/build/dbus-service.scm 	 # note: path name is non-authoritative(*)

  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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 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.
;;;
;;; Code:

(define-module (gnu build dbus-service)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (ice-9 peg)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:autoload (shepherd service) (fork+exec-command)
  #:export (%send-dbus-binary
            %send-dbus-bus
            %send-dbus-user
            %send-dbus-group
            %send-dbus-debug
            send-dbus

            parse-dbus-reply
            deserialize-item
            serialize-boolean
            dbus-dict->alist
            dbus-array->list

            dbus-available-services
            dbus-service-available?

            with-retries))

;;;
;;; 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))))))

\f
;;;
;;; D-Bus reply parser.
;;;

(define (parse-dbus-reply reply)
  "Return the parse tree of REPLY, a string returned by the 'dbus-send'
command."
  ;; Refer to 'man 1 dbus-send' for the grammar reference.  Note that the
  ;; format of the replies doesn't match the format of the input, which is the
  ;; one documented, but it gives an idea.  For an even better reference, see
  ;; the `print_iter' procedure of the 'dbus-print-message.c' file from the
  ;; 'dbus' package sources.
  (define-peg-string-patterns
    "contents <- header (item / container (item / container*)?)
     item <-- WS type WS value NL
     container <- array / dict / variant
     array <-- array-start (item / container)* array-end
     dict <-- array-start dict-entry* array-end
     dict-entry <-- dict-entry-start item item dict-entry-end
     variant <-- variant-start item
     type <-- 'string' / 'int16' / 'uint16' / 'int32' / 'uint32' / 'int64' /
              'uint64' / 'double' / 'byte' / 'boolean' / 'objpath'
     value <-- (!NL .)* NL
     header < (!NL .)* NL
     variant-start < WS 'variant'
     array-start < WS 'array [' NL
     array-end < WS ']' NL
     dict-entry-start < WS 'dict entry(' NL
     dict-entry-end < WS ')' NL
     DQ < '\"'
     WS < ' '*
     NL < '\n'*")

  (peg:tree (match-pattern contents reply)))

(define (strip-quotes text)
  "Strip the leading and trailing double quotes (\") characters from TEXT."
  (let* ((text* (if (string-prefix? "\"" text)
                    (string-drop text 1)
                    text))
         (text** (if (string-suffix? "\"" text*)
                     (string-drop-right text* 1)
                     text*)))
    text**))

(define (deserialize-item item)
  "Return the value described by the ITEM parse tree as a Guile object."
  ;; Strings are printed wrapped in double quotes (see the print_iter
  ;; procedure in dbus-print-message.c).
  (match item
    (('item ('type "string") ('value value))
     (strip-quotes value))
    (('item ('type "boolean") ('value value))
     (if (string=? "true" value)
         #t
         #f))
    (('item _ ('value value))
     value)))

(define (serialize-boolean bool)
  "Return the serialized format expected by dbus-send for BOOL."
  (format #f "boolean:~:[false~;true~]" bool))

(define (dbus-dict->alist dict-parse-tree)
  "Translate a dict parse tree to an alist."
  (define (tuples->alist tuples)
    (map (lambda (x) (apply cons x)) tuples))

  (match dict-parse-tree
    ('dict
     '())
    (('dict ('dict-entry keys values) ...)
     (let ((keys* (map deserialize-item keys))
           (values* (map deserialize-item values)))
       (tuples->alist (zip keys* values*))))))

;;;
(define (dbus-array->list array-parse-tree)
  "Translate an array parse tree to a list."
  (match array-parse-tree
    ('array
     '())
    (('array items ...)
     (map deserialize-item items))))

\f
;;;
;;; Low-level, D-Bus-related procedures.
;;;

;;; The following parameters are used in the jami-service-type service
;;; definition to conveniently customize the behavior of the send-dbus helper,
;;; even when called indirectly.
(define %send-dbus-binary (make-parameter "dbus-send"))
(define %send-dbus-bus (make-parameter #f))
(define %send-dbus-user (make-parameter #f))
(define %send-dbus-group (make-parameter #f))
(define %send-dbus-debug (make-parameter #f))

(define* (send-dbus #:key service path interface method
                    bus
                    dbus-send
                    user group
                    timeout
                    arguments)
  "Return the response of DBUS-SEND, else raise an error.  Unless explicitly
provided, DBUS-SEND takes the value of the %SEND-DBUS-BINARY parameter.  BUS
can be used to specify the bus address, such as 'unix:path=/var/run/jami/bus'.
Alternatively, the %SEND-DBUS-BUS parameter can be used.  ARGUMENTS can be
used to pass input values to a D-Bus method call.  TIMEOUT is the amount of
time to wait for a reply in milliseconds before giving up with an error.  USER
and GROUP allow choosing under which user/group the DBUS-SEND command is
executed.  Alternatively, the %SEND-DBUS-USER and %SEND-DBUS-GROUP parameters
can be used instead."
  (let* ((command `(,(if dbus-send
                         dbus-send
                         (%send-dbus-binary))
                    ,@(if (or bus (%send-dbus-bus))
                          (list (string-append "--bus="
                                               (or bus (%send-dbus-bus))))
                          '())
                    "--print-reply"
                    ,@(if timeout
                          (list (format #f "--reply-timeout=~d" timeout))
                          '())
                    ,(string-append "--dest=" service) ;e.g., cx.ring.Ring
                    ,path            ;e.g., /cx/ring/Ring/ConfigurationManager
                    ,(string-append interface "." method)
                    ,@(or arguments '())))
         (temp-port (mkstemp! (string-copy "/tmp/dbus-send-output-XXXXXXX")))
         (temp-file (port-filename temp-port)))
    (dynamic-wind
      (lambda ()
        (let* ((uid (or (and=> (or user (%send-dbus-user))
                               (compose passwd:uid getpwnam)) -1))
               (gid (or (and=> (or group (%send-dbus-group))
                               (compose group:gid getgrnam)) -1)))
          (chown temp-port uid gid)))
      (lambda ()
        (let ((pid (fork+exec-command command
                                      #:user (or user (%send-dbus-user))
                                      #:group (or group (%send-dbus-group))
                                      #:log-file temp-file)))
          (match (waitpid pid)
            ((_ . status)
             (let ((exit-status (status:exit-val status))
                   (output (call-with-port temp-port get-string-all)))
               (if (= 0 exit-status)
                   output
                   (error "the send-dbus command exited with: "
                          command exit-status output)))))))
      (lambda ()
        (false-if-exception (delete-file temp-file))))))

\f
;;;
;;; Higher-level, D-Bus-related procedures.
;;;

(define (dbus-available-services)
  "Return the list of available (acquired) D-Bus services."
  (let ((reply (parse-dbus-reply
                (send-dbus #:service "org.freedesktop.DBus"
                           #:path "/org/freedesktop/DBus"
                           #:interface "org.freedesktop.DBus"
                           #:method "ListNames"))))
    ;; Remove entries such as ":1.7".
    (remove (cut string-prefix? ":" <>)
            (dbus-array->list reply))))

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

debug log:

solving fd56c8dae7 ...
found fd56c8dae7 in https://yhetil.org/guix/875yrxyrjr.fsf@gmail.com/

applying [1/1] https://yhetil.org/guix/875yrxyrjr.fsf@gmail.com/
diff --git a/gnu/build/dbus-service.scm b/gnu/build/dbus-service.scm
new file mode 100644
index 0000000000..fd56c8dae7

Checking patch gnu/build/dbus-service.scm...
Applied patch gnu/build/dbus-service.scm cleanly.

index at:
100644 fd56c8dae7e7606d556c88c0d2dfac112e15901f	gnu/build/dbus-service.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.