1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (test-syscalls)
21 #:use-module (guix utils)
22 #:use-module (guix build syscalls)
23 #:use-module (gnu build linux-container)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-26)
26 #:use-module (srfi srfi-64)
27 #:use-module (system foreign)
28 #:use-module ((ice-9 ftw) #:select (scandir))
29 #:use-module (ice-9 match))
31 ;; Test the (guix build syscalls) module, although there's not much that can
32 ;; actually be tested without being root.
35 (string-append "t-utils-" (number->string (getpid))))
38 (test-begin "syscalls")
40 (test-equal "mount, ENOENT"
44 (mount "/dev/null" "/does-not-exist" "ext2")
46 (compose system-error-errno list)))
48 (test-assert "umount, ENOENT/EPERM"
51 (umount "/does-not-exist")
54 ;; Both return values have been encountered in the wild.
55 (memv (system-error-errno args) (list EPERM ENOENT)))))
57 (test-assert "mount-points"
58 ;; Reportedly "/" is not always listed as a mount point, so check a few
59 ;; others (see <http://bugs.gnu.org/20261>.)
60 (any (cute member <> (mount-points))
61 '("/" "/proc" "/sys" "/dev")))
63 (false-if-exception (delete-file temp-file))
64 (test-equal "utime with AT_SYMLINK_NOFOLLOW"
67 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
68 ;; define as of Guile 2.2.4.
69 (symlink "/nowhere" temp-file)
70 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
71 (let ((st (lstat temp-file)))
72 (delete-file temp-file)
73 ;; Note: 'utimensat' does not change 'ctime'.
74 (list (stat:mtime st) (stat:atime st)))))
76 (test-assert "swapon, ENOENT/EPERM"
79 (swapon "/does-not-exist")
82 (memv (system-error-errno args) (list EPERM ENOENT)))))
84 (test-assert "swapoff, ENOENT/EINVAL/EPERM"
87 (swapoff "/does-not-exist")
90 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
92 (test-assert "mkdtemp!"
93 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
94 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
95 (and (file-exists? dir)
100 (test-equal "statfs, ENOENT"
104 (statfs "/does-not-exist"))
105 (compose system-error-errno list)))
107 (test-assert "statfs"
108 (let ((fs (statfs "/")))
109 (and (file-system? fs)
110 (> (file-system-block-size fs) 0)
111 (>= (file-system-blocks-available fs) 0)
112 (>= (file-system-blocks-free fs)
113 (file-system-blocks-available fs)))))
115 (define (user-namespace pid)
116 (string-append "/proc/" (number->string pid) "/ns/user"))
118 (define perform-container-tests?
119 (and (user-namespace-supported?)
120 (unprivileged-user-namespace-supported?)))
122 (unless perform-container-tests?
125 (match (clone (logior CLONE_NEWUSER SIGCHLD))
126 (0 (primitive-exit 42))
128 ;; Check if user namespaces are different.
129 (and (not (equal? (readlink (user-namespace pid))
130 (readlink (user-namespace (getpid)))))
133 (= 42 (status:exit-val status))))))))
135 (unless perform-container-tests?
138 (match (clone (logior CLONE_NEWUSER SIGCHLD))
139 (0 (primitive-exit 0))
143 (match (primitive-fork)
146 ;; Join the user namespace.
147 (call-with-input-file (user-namespace clone-pid)
149 (setns (port->fdes port) 0)))
155 ;; Wait for the child process to join the namespace.
157 (let ((result (and (equal? (readlink (user-namespace clone-pid))
158 (readlink (user-namespace fork-pid))))))
164 (when (not perform-container-tests?)
166 (test-equal "pivot-root"
168 (match (socketpair AF_UNIX SOCK_STREAM 0)
170 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
176 (call-with-temporary-directory
178 (display "ready\n" child)
179 (read child) ;wait for "go!"
180 (let ((put-old (string-append root "/real-root")))
181 (mount "none" root "tmpfs")
183 (call-with-output-file (string-append root "/test")
185 (display "testing\n" port)))
186 (pivot-root root put-old)
187 ;; The test file should now be located inside the root directory.
188 (write (and (file-exists? "/test") 'success!) child)
191 (primitive-exit 0))))
196 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
197 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
198 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
200 (display "deny" port)))
201 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
203 (format port "0 ~d 1" (getuid))))
204 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
206 (format port "0 ~d 1" (getgid))))
207 (display "go!\n" parent)
208 (let ((result (read parent)))
210 (and (zero? (match (waitpid pid)
212 (status:exit-val status))))
215 (test-equal "scandir*, ENOENT"
219 (scandir* "/does/not/exist"))
221 (system-error-errno args))))
223 (test-equal "scandir*, ASCII file names"
224 (scandir (dirname (search-path %load-path "guix/base32.scm"))
226 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
227 (((names . properties) ...)
230 (test-equal "scandir*, UTF-8 file names"
232 (call-with-temporary-directory
234 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
235 ;; name to the system call.
236 (let ((creat (pointer->procedure int
237 (dynamic-func "creat" (dynamic-link))
239 (creat (string->pointer (string-append directory "/α")
242 (creat (string->pointer (string-append directory "/λ")
245 (let ((locale (setlocale LC_ALL)))
248 ;; Make sure that even in a C locale we get the right result.
249 (setlocale LC_ALL "C"))
251 (match (scandir* directory)
252 (((names . properties) ...)
255 (setlocale LC_ALL locale))))))))
257 (test-assert "scandir*, properties"
258 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
259 (every (lambda (entry name)
261 ((name2 . properties)
262 (and (string=? name2 name)
263 (let* ((full (string-append directory "/" name))
265 (inode (assoc-ref properties 'inode))
266 (type (assoc-ref properties 'type)))
267 (and (= inode (stat:ino stat))
268 (or (eq? type 'unknown)
269 (eq? type (stat:type stat)))))))))
271 (scandir directory (const #t) string<?))))
273 (false-if-exception (delete-file temp-file))
274 (test-equal "fcntl-flock wait"
275 42 ; the child's exit status
276 (let ((file (open-file temp-file "w0b")))
277 ;; Acquire an exclusive lock.
278 (fcntl-flock file 'write-lock)
279 (match (primitive-fork)
284 ;; Reopen FILE read-only so we can have a read lock.
285 (let ((file (open-file temp-file "r0b")))
286 ;; Wait until we can acquire the lock.
287 (fcntl-flock file 'read-lock)
288 (primitive-exit (read file)))
291 (primitive-exit 2))))
293 ;; Write garbage and wait.
294 (display "hello, world!" file)
298 ;; Write the real answer.
299 (seek file 0 SEEK_SET)
300 (truncate-file file 0)
304 ;; Unlock, which should let the child continue.
305 (fcntl-flock file 'unlock)
309 (let ((result (status:exit-val status)))
313 (test-equal "fcntl-flock non-blocking"
314 EAGAIN ; the child's exit status
317 (match (primitive-fork)
324 ;; Wait for the green light.
327 ;; Open FILE read-only so we can have a read lock.
328 (let ((file (open-file temp-file "w0")))
331 ;; This attempt should throw EAGAIN.
332 (fcntl-flock file 'write-lock #:wait? #f))
334 (primitive-exit (pk 'errno errno)))))
337 (primitive-exit -2))))
340 (let ((file (open-file temp-file "w0")))
341 ;; Acquire an exclusive lock.
342 (fcntl-flock file 'write-lock)
344 ;; Tell the child to continue.
345 (write 'green-light output)
346 (force-output output)
350 (let ((result (status:exit-val status)))
351 (fcntl-flock file 'unlock)
355 (test-equal "set-thread-name"
357 (let ((name (thread-name)))
358 (set-thread-name "Syscall Test")
359 (let ((new-name (thread-name)))
360 (set-thread-name name)
363 (test-assert "all-network-interface-names"
364 (match (all-network-interface-names)
365 (((? string? names) ..1)
366 (member "lo" names))))
368 (test-assert "network-interface-names"
369 (match (network-interface-names)
370 (((? string? names) ..1)
371 (lset<= string=? names (all-network-interface-names)))))
373 (test-assert "network-interface-flags"
374 (let* ((sock (socket AF_INET SOCK_STREAM 0))
375 (flags (network-interface-flags sock "lo")))
377 (and (not (zero? (logand flags IFF_LOOPBACK)))
378 (not (zero? (logand flags IFF_UP))))))
380 (test-equal "loopback-network-interface?"
382 (and (loopback-network-interface? "lo")
385 (loopback-network-interface? "nonexistent")
388 (system-error-errno args)))))
390 (test-equal "loopback-network-interface-running?"
392 (and (network-interface-running? "lo")
395 (network-interface-running? "nonexistent")
398 (system-error-errno args)))))
400 (test-skip (if (zero? (getuid)) 1 0))
401 (test-assert "set-network-interface-flags"
402 (let ((sock (socket AF_INET SOCK_STREAM 0)))
405 (set-network-interface-flags sock "lo" IFF_UP))
408 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
409 (memv (system-error-errno args) (list EPERM EACCES))))))
411 (test-equal "network-interface-address lo"
412 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
413 (let* ((sock (socket AF_INET SOCK_STREAM 0))
414 (addr (network-interface-address sock "lo")))
418 (test-skip (if (zero? (getuid)) 1 0))
419 (test-assert "set-network-interface-address"
420 (let ((sock (socket AF_INET SOCK_STREAM 0)))
423 (set-network-interface-address sock "nonexistent"
426 (inet-pton AF_INET "127.12.14.15")
430 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
431 (memv (system-error-errno args) (list EPERM EACCES))))))
433 (test-equal "network-interface-netmask lo"
434 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
435 (let* ((sock (socket AF_INET SOCK_STREAM 0))
436 (addr (network-interface-netmask sock "lo")))
440 (test-skip (if (zero? (getuid)) 1 0))
441 (test-assert "set-network-interface-netmask"
442 (let ((sock (socket AF_INET SOCK_STREAM 0)))
445 (set-network-interface-netmask sock "nonexistent"
448 (inet-pton AF_INET "255.0.0.0")
452 (memv (system-error-errno args) (list EPERM EACCES))))))
454 (test-equal "network-interfaces returns one or more interfaces"
456 (match (network-interfaces)
458 (list (every interface? interfaces)
459 (every string? (map interface-name interfaces))
460 (every (lambda (sockaddr)
461 ;; Sometimes interfaces have no associated address.
462 (or (vector? sockaddr)
464 (map interface-address interfaces))))))
466 (test-equal "network-interfaces returns \"lo\""
467 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
468 (match (filter (lambda (interface)
469 (string=? "lo" (interface-name interface)))
470 (network-interfaces))
472 (list (every (lambda (lo)
473 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
475 (match (find (lambda (lo)
476 (= AF_INET (sockaddr:fam (interface-address lo))))
479 (lo (interface-address lo)))))))
481 (test-skip (if (zero? (getuid)) 1 0))
482 (test-assert "add-network-route/gateway"
483 (let ((sock (socket AF_INET SOCK_STREAM 0))
484 (gateway (make-socket-address AF_INET
485 (inet-pton AF_INET "192.168.0.1")
489 (add-network-route/gateway sock gateway))
492 (memv (system-error-errno args) (list EPERM EACCES))))))
494 (test-skip (if (zero? (getuid)) 1 0))
495 (test-assert "delete-network-route"
496 (let ((sock (socket AF_INET SOCK_STREAM 0))
497 (destination (make-socket-address AF_INET INADDR_ANY 0)))
500 (delete-network-route sock destination))
503 (memv (system-error-errno args) (list EPERM EACCES))))))
505 (test-equal "tcgetattr ENOTTY"
509 (call-with-input-file "/dev/null"
511 (tcgetattr (fileno port)))))
512 (compose system-error-errno list)))
514 (test-skip (if (and (file-exists? "/proc/self/fd/0")
515 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
519 (test-assert "tcgetattr"
520 (let ((termios (tcgetattr 0)))
521 (and (termios? termios)
522 (> (termios-input-speed termios) 0)
523 (> (termios-output-speed termios) 0))))
525 (test-assert "tcsetattr"
526 (let ((first (tcgetattr 0)))
527 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
528 (equal? first (tcgetattr 0))))
530 (test-assert "terminal-window-size ENOTTY"
531 (call-with-input-file "/dev/null"
535 (terminal-window-size port))
537 ;; Accept EINVAL, which some old Linux versions might return.
538 (memv (system-error-errno args)
539 (list ENOTTY EINVAL)))))))
541 (test-assert "terminal-columns"
542 (> (terminal-columns) 0))
544 (test-assert "terminal-columns non-file port"
545 (> (terminal-columns (open-input-string "Join us now, share the software!"))
548 (test-assert "terminal-rows"
549 (> (terminal-rows) 0))
551 (test-assert "utmpx-entries"
552 (match (utmpx-entries)
553 (((? utmpx? entries) ...)
554 (every (lambda (entry)
555 (match (utmpx-user entry)
557 ;; Ensure we have a valid PID for those entries where it
559 (or (not (memv (utmpx-login-type entry)
560 (list (login-type INIT_PROCESS)
561 (login-type LOGIN_PROCESS)
562 (login-type USER_PROCESS))))
563 (> (utmpx-pid entry) 0)))
564 (#f ;might be DEAD_PROCESS
568 (test-assert "read-utmpx, EOF"
569 (eof-object? (read-utmpx (%make-void-port "r"))))
571 (unless (access? "/var/run/utmpx" O_RDONLY)
573 (test-assert "read-utmpx"
574 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
575 (or (utmpx? result) (eof-object? result))))
577 (when (zero? (getuid))
579 (test-equal "add-to-entropy-count"
581 (call-with-output-file "/dev/urandom"
585 (add-to-entropy-count port 77)
588 (system-error-errno args))))))
592 (false-if-exception (delete-file temp-file))