Commit | Line | Data |
---|---|---|
63eb2b89 LC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org> | |
3 | ;;; | |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (guix scripts processes) | |
20 | #:use-module ((guix store) #:select (%store-prefix)) | |
21 | #:use-module (guix scripts) | |
22 | #:use-module (guix ui) | |
23 | #:use-module (srfi srfi-1) | |
24 | #:use-module (srfi srfi-9) | |
25 | #:use-module (srfi srfi-9 gnu) | |
26 | #:use-module (srfi srfi-37) | |
27 | #:use-module (ice-9 ftw) | |
28 | #:use-module (ice-9 match) | |
29 | #:use-module (ice-9 rdelim) | |
30 | #:use-module (ice-9 format) | |
31 | #:export (process? | |
32 | process-id | |
33 | process-parent-id | |
34 | process-command | |
35 | processes | |
36 | ||
37 | daemon-session? | |
38 | daemon-session-process | |
39 | daemon-session-client | |
40 | daemon-session-children | |
41 | daemon-session-locks-held | |
42 | daemon-sessions | |
43 | ||
44 | guix-processes)) | |
45 | ||
46 | ;; Process as can be found in /proc on GNU/Linux. | |
47 | (define-record-type <process> | |
48 | (process id parent command) | |
49 | process? | |
50 | (id process-id) ;integer | |
51 | (parent process-parent-id) ;integer | #f | |
52 | (command process-command)) ;list of strings | |
53 | ||
54 | (define (write-process process port) | |
55 | (format port "#<process ~a>" (process-id process))) | |
56 | ||
57 | (set-record-type-printer! <process> write-process) | |
58 | ||
59 | (define (read-status-ppid port) | |
60 | "Read the PPID from PORT, an input port on a /proc/PID/status file. Return | |
61 | #f for PID 1 and kernel pseudo-processes." | |
62 | (let loop () | |
63 | (match (read-line port) | |
64 | ((? eof-object?) #f) | |
65 | (line | |
66 | (if (string-prefix? "PPid:" line) | |
67 | (string->number (string-trim-both (string-drop line 5))) | |
68 | (loop)))))) | |
69 | ||
70 | (define %not-nul | |
71 | (char-set-complement (char-set #\nul))) | |
72 | ||
73 | (define (read-command-line port) | |
74 | "Read the zero-split command line from PORT, a /proc/PID/cmdline file, and | |
75 | return it as a list." | |
76 | (string-tokenize (read-string port) %not-nul)) | |
77 | ||
78 | (define (processes) | |
79 | "Return a list of process records representing the currently alive | |
80 | processes." | |
81 | ;; This assumes a Linux-compatible /proc file system. There exists one for | |
82 | ;; GNU/Hurd. | |
83 | (filter-map (lambda (pid) | |
84 | ;; There's a TOCTTOU race here. If we get ENOENT, simply | |
85 | ;; ignore PID. | |
86 | (catch 'system-error | |
87 | (lambda () | |
88 | (define ppid | |
89 | (call-with-input-file (string-append "/proc/" pid "/status") | |
90 | read-status-ppid)) | |
91 | (define command | |
92 | (call-with-input-file (string-append "/proc/" pid "/cmdline") | |
93 | read-command-line)) | |
94 | (process (string->number pid) ppid command)) | |
95 | (lambda args | |
96 | (if (= ENOENT (system-error-errno args)) | |
97 | #f | |
98 | (apply throw args))))) | |
99 | (scandir "/proc" string->number))) | |
100 | ||
101 | (define (process-open-files process) | |
102 | "Return the list of files currently open by PROCESS." | |
103 | (let ((directory (string-append "/proc/" | |
104 | (number->string (process-id process)) | |
105 | "/fd"))) | |
106 | (map (lambda (fd) | |
107 | (readlink (string-append directory "/" fd))) | |
108 | (or (scandir directory string->number) '())))) | |
109 | ||
110 | ;; Daemon session. | |
111 | (define-record-type <daemon-session> | |
112 | (daemon-session process client children locks) | |
113 | daemon-session? | |
114 | (process daemon-session-process) ;<process> | |
115 | (client daemon-session-client) ;<process> | |
116 | (children daemon-session-children) ;list of <process> | |
117 | (locks daemon-session-locks-held)) ;list of strings | |
118 | ||
119 | (define (daemon-sessions) | |
120 | "Return two values: the list of <daemon-session> denoting the currently | |
121 | active sessions, and the master 'guix-daemon' process." | |
122 | (define (lock-file? file) | |
123 | (and (string-prefix? (%store-prefix) file) | |
124 | (string-suffix? ".lock" file))) | |
125 | ||
126 | (let* ((processes (processes)) | |
127 | (daemons (filter (lambda (process) | |
128 | (match (process-command process) | |
129 | ((argv0 _ ...) | |
130 | (string=? (basename argv0) "guix-daemon")) | |
131 | (_ #f))) | |
132 | processes)) | |
133 | (children (filter (lambda (process) | |
134 | (match (process-command process) | |
135 | ((argv0 (= string->number argv1) _ ...) | |
136 | (integer? argv1)) | |
137 | (_ #f))) | |
138 | daemons)) | |
139 | (master (remove (lambda (process) | |
140 | (memq process children)) | |
141 | daemons))) | |
142 | (define (lookup-process pid) | |
143 | (find (lambda (process) | |
144 | (and (process-id process) | |
145 | (= pid (process-id process)))) | |
146 | processes)) | |
147 | ||
148 | (define (lookup-children pid) | |
149 | (filter (lambda (process) | |
150 | (and (process-parent-id process) | |
151 | (= pid (process-parent-id process)))) | |
152 | processes)) | |
153 | ||
154 | (values (map (lambda (process) | |
155 | (match (process-command process) | |
156 | ((argv0 (= string->number client) _ ...) | |
157 | (let ((files (process-open-files process))) | |
158 | (daemon-session process | |
159 | (lookup-process client) | |
160 | (lookup-children (process-id process)) | |
161 | (filter lock-file? files)))))) | |
162 | children) | |
163 | master))) | |
164 | ||
165 | (define (daemon-session->recutils session port) | |
166 | "Display SESSION information in recutils format on PORT." | |
167 | (format port "SessionPID: ~a~%" | |
168 | (process-id (daemon-session-process session))) | |
169 | (format port "ClientPID: ~a~%" | |
170 | (process-id (daemon-session-client session))) | |
171 | (format port "ClientCommand:~{ ~a~}~%" | |
172 | (process-command (daemon-session-client session))) | |
173 | (for-each (lambda (lock) | |
174 | (format port "LockHeld: ~a~%" lock)) | |
175 | (daemon-session-locks-held session)) | |
176 | (for-each (lambda (process) | |
177 | (format port "ChildProcess: ~a:~{ ~a~}~%" | |
178 | (process-id process) | |
179 | (process-command process))) | |
180 | (daemon-session-children session))) | |
181 | ||
182 | \f | |
183 | ;;; | |
184 | ;;; Options. | |
185 | ;;; | |
186 | ||
187 | (define %options | |
188 | (list (option '(#\h "help") #f #f | |
189 | (lambda args | |
190 | (show-help) | |
191 | (exit 0))) | |
192 | (option '(#\V "version") #f #f | |
193 | (lambda args | |
194 | (show-version-and-exit "guix processes"))))) | |
195 | ||
196 | (define (show-help) | |
197 | (display (G_ "Usage: guix processes | |
198 | List the current Guix sessions and their processes.")) | |
199 | (newline) | |
200 | (display (G_ " | |
201 | -h, --help display this help and exit")) | |
202 | (display (G_ " | |
203 | -V, --version display version information and exit")) | |
204 | (newline) | |
205 | (show-bug-report-information)) | |
206 | ||
207 | \f | |
208 | ;;; | |
209 | ;;; Entry point. | |
210 | ;;; | |
211 | ||
212 | (define (guix-processes . args) | |
213 | (define options | |
214 | (args-fold* args %options | |
215 | (lambda (opt name arg result) | |
216 | (leave (G_ "~A: unrecognized option~%") name)) | |
217 | cons | |
218 | '())) | |
219 | ||
220 | (for-each (lambda (session) | |
221 | (daemon-session->recutils session (current-output-port)) | |
222 | (newline)) | |
223 | (daemon-sessions))) |