roots.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2017, 2019 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. (define-module (guix store roots)
  19. #:use-module (guix config)
  20. #:use-module ((guix store) #:select (store-path? %gc-roots-directory))
  21. #:use-module (guix sets)
  22. #:use-module (guix build syscalls)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 regex)
  28. #:use-module (ice-9 rdelim)
  29. #:use-module (ice-9 ftw)
  30. #:use-module (rnrs io ports)
  31. #:re-export (%gc-roots-directory)
  32. #:export (gc-roots
  33. user-owned?
  34. busy-store-items))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; This module provides tools to list and access garbage collector roots ("GC
  38. ;;; roots").
  39. ;;;
  40. ;;; Code:
  41. (define %profile-directory
  42. ;; Directory where user profiles are stored.
  43. ;; XXX: This is redundant with the definition in (guix profiles) and not
  44. ;; entirely needed since in practice /var/guix/gcroots/profiles links to
  45. ;; it.
  46. (string-append %state-directory "/profiles"))
  47. (define (gc-roots)
  48. "Return the list of garbage collector roots (\"GC roots\"). This includes
  49. \"regular\" roots fount in %GC-ROOTS-DIRECTORY as well as indirect roots that
  50. are user-controlled symlinks stored anywhere on the file system."
  51. (define (regular? file)
  52. (match file
  53. (((or "." "..") . _) #f)
  54. (_ #t)))
  55. (define (file-type=? type)
  56. (match-lambda
  57. ((file . properties)
  58. (match (assq-ref properties 'type)
  59. ('unknown
  60. (let ((stat (lstat file)))
  61. (eq? type (stat:type stat))))
  62. (actual-type
  63. (eq? type actual-type))))))
  64. (define directory?
  65. (file-type=? 'directory))
  66. (define symlink?
  67. (file-type=? 'symlink))
  68. (define canonical-root
  69. (match-lambda
  70. ((file . properties)
  71. (let ((target (readlink file)))
  72. (cond ((store-path? target)
  73. ;; Regular root: FILE points to the store.
  74. file)
  75. ;; Indirect root: FILE points to a user-controlled file outside
  76. ;; the store.
  77. ((string-prefix? "/" target)
  78. target)
  79. (else
  80. (string-append (dirname file) "/" target)))))))
  81. (let loop ((directories (list %gc-roots-directory
  82. %profile-directory))
  83. (roots '())
  84. (visited (set)))
  85. (match directories
  86. (()
  87. roots)
  88. ((directory . rest)
  89. (if (set-contains? visited directory)
  90. (loop rest roots visited)
  91. (let*-values (((scope)
  92. (cut string-append directory "/" <>))
  93. ((sub-directories files)
  94. (partition directory?
  95. (map (match-lambda
  96. ((file . properties)
  97. (cons (scope file) properties)))
  98. (scandir* directory regular?)))))
  99. (loop (append rest (map first sub-directories))
  100. (append (map canonical-root (filter symlink? files))
  101. roots)
  102. (set-insert directory visited))))))))
  103. (define* (user-owned? root #:optional (uid (getuid)))
  104. "Return true if ROOT exists and is owned by UID, false otherwise."
  105. ;; If ROOT is an indirect root, then perhaps it no longer exists. Thus,
  106. ;; catch 'system-error' exceptions.
  107. (catch 'system-error
  108. (lambda ()
  109. (define stat
  110. (lstat root))
  111. (= (stat:uid stat) uid))
  112. (const #f)))
  113. ;;;
  114. ;;; Listing "busy" store items: those referenced by currently running
  115. ;;; processes.
  116. ;;;
  117. (define %proc-directory
  118. ;; Mount point of Linuxish /proc file system.
  119. "/proc")
  120. (define (proc-file-roots dir file)
  121. "Return a one-element list containing the file pointed to by DIR/FILE,
  122. or the empty list."
  123. (or (and=> (false-if-exception (readlink (string-append dir "/" file)))
  124. list)
  125. '()))
  126. (define proc-exe-roots (cut proc-file-roots <> "exe"))
  127. (define proc-cwd-roots (cut proc-file-roots <> "cwd"))
  128. (define (proc-fd-roots dir)
  129. "Return the list of store files referenced by DIR, which is a
  130. /proc/XYZ directory."
  131. (let ((dir (string-append dir "/fd")))
  132. (filter-map (lambda (file)
  133. (let ((target (false-if-exception
  134. (readlink (string-append dir "/" file)))))
  135. (and target
  136. (string-prefix? "/" target)
  137. target)))
  138. (or (scandir dir string->number) '()))))
  139. (define (proc-maps-roots dir)
  140. "Return the list of store files referenced by DIR, which is a
  141. /proc/XYZ directory."
  142. (define %file-mapping-line
  143. (make-regexp "^.*[[:blank:]]+/([^ ]+)$"))
  144. (call-with-input-file (string-append dir "/maps")
  145. (lambda (maps)
  146. (let loop ((line (read-line maps))
  147. (roots '()))
  148. (cond ((eof-object? line)
  149. roots)
  150. ((regexp-exec %file-mapping-line line)
  151. =>
  152. (lambda (match)
  153. (let ((file (string-append "/"
  154. (match:substring match 1))))
  155. (loop (read-line maps)
  156. (cons file roots)))))
  157. (else
  158. (loop (read-line maps) roots)))))))
  159. (define (proc-environ-roots dir)
  160. "Return the list of store files referenced by DIR/environ, where DIR is a
  161. /proc/XYZ directory."
  162. (define split-on-nul
  163. (cute string-tokenize <>
  164. (char-set-complement (char-set #\nul))))
  165. (define (rhs-file-names str)
  166. (let ((equal (string-index str #\=)))
  167. (if equal
  168. (let* ((str (substring str (+ 1 equal)))
  169. (rx (string-append (regexp-quote %store-directory)
  170. "/[0-9a-z]{32}-[a-zA-Z0-9\\._+-]+")))
  171. (map match:substring (list-matches rx str)))
  172. '())))
  173. (define environ
  174. (string-append dir "/environ"))
  175. (append-map rhs-file-names
  176. (split-on-nul
  177. (call-with-input-file environ
  178. get-string-all))))
  179. (define (referenced-files)
  180. "Return the list of referenced store items."
  181. (append-map (lambda (pid)
  182. (let ((proc (string-append %proc-directory "/" pid)))
  183. (catch 'system-error
  184. (lambda ()
  185. (append (proc-exe-roots proc)
  186. (proc-cwd-roots proc)
  187. (proc-fd-roots proc)
  188. (proc-maps-roots proc)
  189. (proc-environ-roots proc)))
  190. (lambda args
  191. (let ((err (system-error-errno args)))
  192. (if (or (= ENOENT err) ;TOCTTOU race
  193. (= ESRCH err) ;ditto
  194. (= EACCES err)) ;not running as root
  195. '()
  196. (apply throw args)))))))
  197. (scandir %proc-directory string->number
  198. (lambda (a b)
  199. (< (string->number a) (string->number b))))))
  200. (define canonicalize-store-item
  201. (let* ((store (string-append %store-directory "/"))
  202. (prefix (string-length store)))
  203. (lambda (file)
  204. "Return #f if FILE is not a store item; otherwise, return the store file
  205. name without any sub-directory components."
  206. (and (string-prefix? store file)
  207. (string-append store
  208. (let ((base (string-drop file prefix)))
  209. (match (string-index base #\/)
  210. (#f base)
  211. (slash (string-take base slash)))))))))
  212. (define (busy-store-items)
  213. "Return the list of store items used by the currently running processes.
  214. This code should typically run as root; it allows the garbage collector to
  215. determine which store items must not be deleted."
  216. (delete-duplicates
  217. (filter-map canonicalize-store-item (referenced-files))))