diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2020-09-29 12:02:09 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2020-09-29 21:56:27 +0200 |
| commit | d5366500ec1aeecad6fc292b195088e30aa715fd (patch) | |
| tree | 21673b5d3103d797bec4e54473e6824528312139 /gnu | |
| parent | 59261a22f9819b1fdf797ffba17af17d385d6c92 (diff) | |
secret-service: Add proper logging procedure and log to syslog.
* gnu/build/secret-service.scm (log): New macro.
(secret-service-send-secrets, secret-service-receive-secrets): Use it
instead of raw 'format' calls.
Diffstat (limited to 'gnu')
| -rw-r--r-- | gnu/build/secret-service.scm | 62 |
1 files changed, 29 insertions, 33 deletions
diff --git a/gnu/build/secret-service.scm b/gnu/build/secret-service.scm index 2cc59e0ee1f..46dcf1b9c3b 100644 --- a/gnu/build/secret-service.scm +++ b/gnu/build/secret-service.scm | |||
| @@ -35,6 +35,18 @@ | |||
| 35 | ;;; | 35 | ;;; |
| 36 | ;;; Code: | 36 | ;;; Code: |
| 37 | 37 | ||
| 38 | (define-syntax log | ||
| 39 | (lambda (s) | ||
| 40 | "Log the given message." | ||
| 41 | (syntax-case s () | ||
| 42 | ((_ fmt args ...) | ||
| 43 | (with-syntax ((fmt (string-append "secret service: " | ||
| 44 | (syntax->datum #'fmt)))) | ||
| 45 | ;; Log to the current output port. That way, when | ||
| 46 | ;; 'secret-service-send-secrets' is called from shepherd, output goes | ||
| 47 | ;; to syslog. | ||
| 48 | #'(format (current-output-port) fmt args ...)))))) | ||
| 49 | |||
| 38 | (define* (secret-service-send-secrets port secret-root | 50 | (define* (secret-service-send-secrets port secret-root |
| 39 | #:key (retry 60) | 51 | #:key (retry 60) |
| 40 | (handshake-timeout 120)) | 52 | (handshake-timeout 120)) |
| @@ -60,7 +72,7 @@ wait for at most HANDSHAKE-TIMEOUT seconds for handshake to complete. Return | |||
| 60 | (dump-port input sock)))) | 72 | (dump-port input sock)))) |
| 61 | files))) | 73 | files))) |
| 62 | 74 | ||
| 63 | (format (current-error-port) "sending secrets to ~a~%" port) | 75 | (log "sending secrets to ~a~%" port) |
| 64 | (let ((sock (socket AF_INET SOCK_STREAM 0)) | 76 | (let ((sock (socket AF_INET SOCK_STREAM 0)) |
| 65 | (addr (make-socket-address AF_INET INADDR_LOOPBACK port))) | 77 | (addr (make-socket-address AF_INET INADDR_LOOPBACK port))) |
| 66 | ;; Connect to QEMU on the forwarded port. The 'connect' call succeeds as | 78 | ;; Connect to QEMU on the forwarded port. The 'connect' call succeeds as |
| @@ -72,14 +84,12 @@ wait for at most HANDSHAKE-TIMEOUT seconds for handshake to complete. Return | |||
| 72 | (lambda (key . args) | 84 | (lambda (key . args) |
| 73 | (when (zero? retry) | 85 | (when (zero? retry) |
| 74 | (apply throw key args)) | 86 | (apply throw key args)) |
| 75 | (format (current-error-port) | 87 | (log "retrying connection [~a attempts left]~%" |
| 76 | "secret service: retrying connection [~a attempts left]~%" | 88 | (- retry 1)) |
| 77 | (- retry 1)) | ||
| 78 | (sleep 1) | 89 | (sleep 1) |
| 79 | (loop (1- retry))))) | 90 | (loop (1- retry))))) |
| 80 | 91 | ||
| 81 | (format (current-error-port) | 92 | (log "connected; waiting for handshake...~%") |
| 82 | "secret service: connected; waiting for handshake...~%") | ||
| 83 | 93 | ||
| 84 | ;; Wait for "hello" message from the server. This is the only way to know | 94 | ;; Wait for "hello" message from the server. This is the only way to know |
| 85 | ;; that we're really connected to the server inside the guest. | 95 | ;; that we're really connected to the server inside the guest. |
| @@ -87,25 +97,17 @@ wait for at most HANDSHAKE-TIMEOUT seconds for handshake to complete. Return | |||
| 87 | (((_) () ()) | 97 | (((_) () ()) |
| 88 | (match (read sock) | 98 | (match (read sock) |
| 89 | (('secret-service-server ('version version ...)) | 99 | (('secret-service-server ('version version ...)) |
| 90 | (format (current-error-port) | 100 | (log "sending files from ~s...~%" secret-root) |
| 91 | "secret service: sending files from ~s...~%" | ||
| 92 | secret-root) | ||
| 93 | (send-files sock) | 101 | (send-files sock) |
| 94 | (format (current-error-port) | 102 | (log "done sending files to port ~a~%" port) |
| 95 | "secret service: done sending files to port ~a~%" | ||
| 96 | port) | ||
| 97 | (close-port sock) | 103 | (close-port sock) |
| 98 | secret-root) | 104 | secret-root) |
| 99 | (x | 105 | (x |
| 100 | (format (current-error-port) | 106 | (log "invalid handshake ~s~%" x) |
| 101 | "secret service: invalid handshake ~s~%" | ||
| 102 | x) | ||
| 103 | (close-port sock) | 107 | (close-port sock) |
| 104 | #f))) | 108 | #f))) |
| 105 | ((() () ()) ;timeout | 109 | ((() () ()) ;timeout |
| 106 | (format (current-error-port) | 110 | (log "timeout while sending files to ~a~%" port) |
| 107 | "secret service: timeout while sending files to ~a~%" | ||
| 108 | port) | ||
| 109 | (close-port sock) | 111 | (close-port sock) |
| 110 | #f)))) | 112 | #f)))) |
| 111 | 113 | ||
| @@ -121,17 +123,14 @@ and #f otherwise." | |||
| 121 | (let ((sock (socket AF_INET SOCK_STREAM 0))) | 123 | (let ((sock (socket AF_INET SOCK_STREAM 0))) |
| 122 | (bind sock AF_INET INADDR_ANY port) | 124 | (bind sock AF_INET INADDR_ANY port) |
| 123 | (listen sock 1) | 125 | (listen sock 1) |
| 124 | (format (current-error-port) | 126 | (log "waiting for secrets on port ~a...~%" port) |
| 125 | "secret service: waiting for secrets on port ~a...~%" | ||
| 126 | port) | ||
| 127 | (match (select (list sock) '() '() 60) | 127 | (match (select (list sock) '() '() 60) |
| 128 | (((_) () ()) | 128 | (((_) () ()) |
| 129 | (match (accept sock) | 129 | (match (accept sock) |
| 130 | ((client . address) | 130 | ((client . address) |
| 131 | (format (current-error-port) | 131 | (log "client connection from ~a~%" |
| 132 | "secret service: client connection from ~a~%" | 132 | (inet-ntop (sockaddr:fam address) |
| 133 | (inet-ntop (sockaddr:fam address) | 133 | (sockaddr:addr address))) |
| 134 | (sockaddr:addr address))) | ||
| 135 | 134 | ||
| 136 | ;; Send a "hello" message. This allows the client running on the | 135 | ;; Send a "hello" message. This allows the client running on the |
| 137 | ;; host to know that it's now actually connected to server running | 136 | ;; host to know that it's now actually connected to server running |
| @@ -141,8 +140,7 @@ and #f otherwise." | |||
| 141 | (close-port sock) | 140 | (close-port sock) |
| 142 | client))) | 141 | client))) |
| 143 | ((() () ()) | 142 | ((() () ()) |
| 144 | (format (current-error-port) | 143 | (log "did not receive any secrets; time out~%") |
| 145 | "secret service: did not receive any secrets; time out~%") | ||
| 146 | (close-port sock) | 144 | (close-port sock) |
| 147 | #f)))) | 145 | #f)))) |
| 148 | 146 | ||
| @@ -169,20 +167,18 @@ and #f otherwise." | |||
| 169 | (('secrets ('version 0) | 167 | (('secrets ('version 0) |
| 170 | ('files ((files sizes modes) ...))) | 168 | ('files ((files sizes modes) ...))) |
| 171 | (for-each (lambda (file size mode) | 169 | (for-each (lambda (file size mode) |
| 172 | (format (current-error-port) | 170 | (log "installing file '~a' (~a bytes)...~%" |
| 173 | "secret service: \ | 171 | file size) |
| 174 | installing file '~a' (~a bytes)...~%" | ||
| 175 | file size) | ||
| 176 | (mkdir-p (dirname file)) | 172 | (mkdir-p (dirname file)) |
| 177 | (call-with-output-file file | 173 | (call-with-output-file file |
| 178 | (lambda (output) | 174 | (lambda (output) |
| 179 | (dump port output size) | 175 | (dump port output size) |
| 180 | (chmod file mode)))) | 176 | (chmod file mode)))) |
| 181 | files sizes modes) | 177 | files sizes modes) |
| 178 | (log "received ~a secret files~%" (length files)) | ||
| 182 | files) | 179 | files) |
| 183 | (_ | 180 | (_ |
| 184 | (format (current-error-port) | 181 | (log "invalid secrets received~%") |
| 185 | "secret service: invalid secrets received~%") | ||
| 186 | #f))) | 182 | #f))) |
| 187 | 183 | ||
| 188 | (let* ((port (wait-for-client port)) | 184 | (let* ((port (wait-for-client port)) |
