unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 10fda9f22340135e93eecddaa2c3921fd3ef0160 11458 bytes (raw)
name: guix/scripts/processes.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
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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 John Soo <jsoo1@asu.edu>
;;;
;;; 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/>.

(define-module (guix scripts processes)
  #:use-module ((guix store) #:select (%store-prefix))
  #:use-module (guix scripts)
  #:use-module (guix ui)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (srfi srfi-37)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 format)
  #:export (process?
            process-id
            process-parent-id
            process-command
            processes

            daemon-session?
            daemon-session-process
            daemon-session-client
            daemon-session-children
            daemon-session-locks-held
            daemon-sessions

            guix-processes))

;; Process as can be found in /proc on GNU/Linux.
(define-record-type <process>
  (process id parent command)
  process?
  (id       process-id)                           ;integer
  (parent   process-parent-id)                    ;integer | #f
  (command  process-command))                     ;list of strings

(define (write-process process port)
  (format port "#<process ~a>" (process-id process)))

(set-record-type-printer! <process> write-process)

(define (read-status-ppid port)
  "Read the PPID from PORT, an input port on a /proc/PID/status file.  Return
#f for PID 1 and kernel pseudo-processes."
  (let loop ()
    (match (read-line port)
      ((? eof-object?) #f)
      (line
       (if (string-prefix? "PPid:" line)
           (string->number (string-trim-both (string-drop line 5)))
           (loop))))))

(define %not-nul
  (char-set-complement (char-set #\nul)))

(define (read-command-line port)
  "Read the zero-split command line from PORT, a /proc/PID/cmdline file, and
return it as a list."
  (string-tokenize (read-string port) %not-nul))

(define (processes)
  "Return a list of process records representing the currently alive
processes."
  ;; This assumes a Linux-compatible /proc file system.  There exists one for
  ;; GNU/Hurd.
  (filter-map (lambda (pid)
                ;; There's a TOCTTOU race here.  If we get ENOENT, simply
                ;; ignore PID.
                (catch 'system-error
                  (lambda ()
                    (define ppid
                      (call-with-input-file (string-append "/proc/" pid "/status")
                        read-status-ppid))
                    (define command
                      (call-with-input-file (string-append "/proc/" pid "/cmdline")
                        read-command-line))
                    (process (string->number pid) ppid command))
                  (lambda args
                    (if (= ENOENT (system-error-errno args))
                        #f
                        (apply throw args)))))
              (scandir "/proc" string->number)))

(define (process-open-files process)
  "Return the list of files currently open by PROCESS."
  (let ((directory (string-append "/proc/"
                                  (number->string (process-id process))
                                  "/fd")))
    (filter-map (lambda (fd)
                  ;; There's a TOCTTOU race here, hence the 'catch'.
                  (catch 'system-error
                    (lambda ()
                      (readlink (string-append directory "/" fd)))
                    (lambda args
                      (if (= ENOENT (system-error-errno args))
                          #f
                          (apply throw args)))))
                (or (scandir directory string->number) '()))))

;; Daemon session.
(define-record-type <daemon-session>
  (daemon-session process client children locks)
  daemon-session?
  (process    daemon-session-process)             ;<process>
  (client     daemon-session-client)              ;<process>
  (children   daemon-session-children)            ;list of <process>
  (locks      daemon-session-locks-held))         ;list of strings

(define (daemon-sessions)
  "Return two values: the list of <daemon-session> denoting the currently
active sessions, and the master 'guix-daemon' process."
  (define (lock-file? file)
    (and (string-prefix? (%store-prefix) file)
         (string-suffix? ".lock" file)))

  (let* ((processes (processes))
         (daemons   (filter (lambda (process)
                              (match (process-command process)
                                ((argv0 _ ...)
                                 (string=? (basename argv0) "guix-daemon"))
                                (_ #f)))
                            processes))
         (children  (filter (lambda (process)
                              (match (process-command process)
                                ((argv0 (= string->number argv1) _ ...)
                                 (integer? argv1))
                                (_ #f)))
                            daemons))
         (master    (remove (lambda (process)
                              (memq process children))
                            daemons)))
    (define (lookup-process pid)
      (find (lambda (process)
              (and (process-id process)
                   (= pid (process-id process))))
            processes))

    (define (lookup-children pid)
      (filter (lambda (process)
                (and (process-parent-id process)
                     (= pid (process-parent-id process))))
              processes))

    (define (child-process->session process)
      (match (process-command process)
        ((argv0 (= string->number client) _ ...)
         (let ((files  (process-open-files process))
               (client (lookup-process client)))
           ;; After a client has died, there's a window during which its
           ;; corresponding 'guix-daemon' process is still alive, in which
           ;; case 'lookup-process' returns #f.  In that case ignore the
           ;; session.
           (and client
                (daemon-session process client
                                (lookup-children
                                 (process-id process))
                                (filter lock-file? files)))))))

    (values (filter-map child-process->session children)
            master)))

(define (lock->record lock port)
  (format port "LockHeld: ~a~%" lock))

(define (daemon-session->recutils session port)
  "Display SESSION information in recutils format on PORT."
  (format port "SessionPID: ~a~%"
          (process-id (daemon-session-process session)))
  (format port "ClientPID: ~a~%"
          (process-id (daemon-session-client session)))
  (format port "ClientCommand:~{ ~a~}~%"
          (process-command (daemon-session-client session)))
  (for-each (lambda (lock) (lock->record lock port))
            (daemon-session-locks-held session))
  (for-each (lambda (process)
              (format port "ChildProcess: ~a:~{ ~a~}~%"
                      (process-id process)
                      (process-command process)))
            (daemon-session-children session)))

(define (format-single-record port)
  "Display denormalized session information to PORT."
  (for-each (lambda (session)
              (daemon-session->recutils session port)
                (newline port))
            (daemon-sessions)))

(define session-rec-type
  ;; Also includes ClientCommand and LockHeld but it doesn't seem to be
  ;; possible to express a plain string field (the default) without further
  ;; restrictions
  "%rec: Session
%type: PID int
%type: ClientPID int
%key: PID")

(define child-process-rec-type
  ;; Also includes Command but it doesn't seem to be possible to
  ;; express a plain string field (the default) without further restrictions
  "%rec: ChildProcess
%type: Session rec Session
%type: PID int
%key: PID")

(define (session-key->field session port)
  "Display SESSION PID as field on PORT."
  (format
   port "Session: ~a"
   (process-id (daemon-session-process session))))

(define (session-scalars->normalized-record session port)
  "Display SESSION scalar fields to PORT in normalized form."
  (format port "PID: ~a~%"
          (process-id (daemon-session-process session)))
  (format port "ClientPID: ~a~%"
          (process-id (daemon-session-client session)))
  (format port "ClientCommand:~{ ~a~}~%"
          (process-command (daemon-session-client session))))

(define (child-process->normalized-record process port)
  "Display PROCESS record on PORT in normalized form"
  (format port "PID: ~a" (process-id process))
  (newline port)
  (format port "Command:~{ ~a~}" (process-command process)))

(define (format-normalized port)
  (define sessions (daemon-sessions))

  (format port session-rec-type)
  (newline port)
  (newline port)
  (for-each
   (lambda (session)
     (session-scalars->normalized-record session port)
     (for-each
      (lambda (lock) (lock->record lock port))
      (daemon-session-locks-held session))
     (newline port))
   sessions)

  (format port child-process-rec-type)
  (newline port)
  (newline port)
  (for-each
   (lambda (session)
     (for-each
      (lambda (process)
        (session-key->field session port)
        (newline port)
        (child-process->normalized-record process port)
        (newline port)
        (newline port))
      (daemon-session-children session)))
   sessions))

\f
;;;
;;; Options.
;;;

(define %options
  (list (option '(#\h "help") #f #f
                (lambda args
                  (show-help)
                  (exit 0)))
        (option '(#\V "version") #f #f
                (lambda args
                  (show-version-and-exit "guix processes")))
        (option '("normalize") #f #f
                (lambda (opt name arg result)
                  (alist-cons 'normalize #t result)))))

(define (show-help)
  (display (G_ "Usage: guix processes
List the current Guix sessions and their processes."))
  (newline)
  (display (G_ "
  -h, --help             display this help and exit"))
  (display (G_ "
  -V, --version          display version information and exit"))
  (newline)
  (display (G_ "
  --normalize            display results as normalized record sets"))
  (newline)
  (show-bug-report-information))

(define %default-options '())

\f
;;;
;;; Entry point.
;;;

(define-command (guix-processes . args)
  (category plumbing)
  (synopsis "list currently running sessions")
  (define options
    (parse-command-line args %options (list %default-options)))

  (with-paginated-output-port port
    (match (assoc-ref options 'normalize)
      (#t (format-normalized port))
      (_ (format-single-record port)))

    ;; Pass 'R' (instead of 'r') so 'less' correctly estimates line length.
    #:less-options "FRX"))

debug log:

solving 10fda9f223 ...
found 10fda9f223 in https://yhetil.org/guix-patches/878sbeoya2.fsf@asu.edu/
found b4ca7b1687 in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 b4ca7b168748a3ff306fc894622a55ede0c85172	guix/scripts/processes.scm

applying [1/1] https://yhetil.org/guix-patches/878sbeoya2.fsf@asu.edu/
diff --git a/guix/scripts/processes.scm b/guix/scripts/processes.scm
index b4ca7b1687..10fda9f223 100644

Checking patch guix/scripts/processes.scm...
Applied patch guix/scripts/processes.scm cleanly.

index at:
100644 10fda9f22340135e93eecddaa2c3921fd3ef0160	guix/scripts/processes.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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).