list-runtime-roots.in 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!@GUILE@ -ds
  2. !#
  3. ;;; GNU Guix --- Functional package management for GNU
  4. ;;; Copyright © 2012, 2013, 2014, 2017 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;
  21. ;;; List files being used at run time; these files are garbage collector
  22. ;;; roots. This is equivalent to `find-runtime-roots.pl' in Nix.
  23. ;;;
  24. (use-modules (ice-9 ftw)
  25. (ice-9 regex)
  26. (ice-9 rdelim)
  27. (ice-9 match)
  28. (srfi srfi-1)
  29. (srfi srfi-26)
  30. (rnrs io ports))
  31. (define %proc-directory
  32. ;; Mount point of Linuxish /proc file system.
  33. "/proc")
  34. (define %store-directory
  35. (or (getenv "NIX_STORE_DIR")
  36. "@storedir@"))
  37. (define (proc-file-roots dir file)
  38. "Return a one-element list containing the file pointed to by DIR/FILE,
  39. or the empty list."
  40. (or (and=> (false-if-exception (readlink (string-append dir "/" file)))
  41. list)
  42. '()))
  43. (define proc-exe-roots (cut proc-file-roots <> "exe"))
  44. (define proc-cwd-roots (cut proc-file-roots <> "cwd"))
  45. (define (proc-fd-roots dir)
  46. "Return the list of store files referenced by DIR, which is a
  47. /proc/XYZ directory."
  48. (let ((dir (string-append dir "/fd")))
  49. (filter-map (lambda (file)
  50. (let ((target (false-if-exception
  51. (readlink (string-append dir "/" file)))))
  52. (and target
  53. (string-prefix? "/" target)
  54. target)))
  55. (or (scandir dir string->number) '()))))
  56. (define (proc-maps-roots dir)
  57. "Return the list of store files referenced by DIR, which is a
  58. /proc/XYZ directory."
  59. (define %file-mapping-line
  60. (make-regexp "^.*[[:blank:]]+/([^ ]+)$"))
  61. (call-with-input-file (string-append dir "/maps")
  62. (lambda (maps)
  63. (let loop ((line (read-line maps))
  64. (roots '()))
  65. (cond ((eof-object? line)
  66. roots)
  67. ((regexp-exec %file-mapping-line line)
  68. =>
  69. (lambda (match)
  70. (let ((file (string-append "/"
  71. (match:substring match 1))))
  72. (loop (read-line maps)
  73. (cons file roots)))))
  74. (else
  75. (loop (read-line maps) roots)))))))
  76. (define (proc-environ-roots dir)
  77. "Return the list of store files referenced by DIR/environ, where DIR is a
  78. /proc/XYZ directory."
  79. (define split-on-nul
  80. (cute string-tokenize <>
  81. (char-set-complement (char-set #\nul))))
  82. (define (rhs-file-names str)
  83. (let ((equal (string-index str #\=)))
  84. (if equal
  85. (let* ((str (substring str (+ 1 equal)))
  86. (rx (string-append (regexp-quote %store-directory)
  87. "/[0-9a-z]{32}-[a-zA-Z0-9\\._+-]+")))
  88. (map match:substring (list-matches rx str)))
  89. '())))
  90. (define environ
  91. (string-append dir "/environ"))
  92. (append-map rhs-file-names
  93. (split-on-nul
  94. (call-with-input-file environ
  95. get-string-all))))
  96. (define (referenced-files)
  97. "Return the list of referenced store items."
  98. (append-map (lambda (pid)
  99. (let ((proc (string-append %proc-directory "/" pid)))
  100. (catch 'system-error
  101. (lambda ()
  102. (append (proc-exe-roots proc)
  103. (proc-cwd-roots proc)
  104. (proc-fd-roots proc)
  105. (proc-maps-roots proc)
  106. (proc-environ-roots proc)))
  107. (lambda args
  108. (let ((err (system-error-errno args)))
  109. (if (or (= ENOENT err) ;TOCTTOU race
  110. (= ESRCH err) ;ditto
  111. (= EACCES err)) ;not running as root
  112. '()
  113. (apply throw args)))))))
  114. (scandir %proc-directory string->number
  115. (lambda (a b)
  116. (< (string->number a) (string->number b))))))
  117. (define canonicalize-store-item
  118. (let* ((store (string-append %store-directory "/"))
  119. (prefix (string-length store)))
  120. (lambda (file)
  121. "Return #f if FILE is not a store item; otherwise, return the store file
  122. name without any sub-directory components."
  123. (and (string-prefix? store file)
  124. (string-append store
  125. (let ((base (string-drop file prefix)))
  126. (match (string-index base #\/)
  127. (#f base)
  128. (slash (string-take base slash)))))))))
  129. (for-each (cut simple-format #t "~a~%" <>)
  130. (delete-duplicates
  131. (filter-map canonicalize-store-item (referenced-files))))