1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
4 ;;; Copyright © 2020 Simon South <simon@simonsouth.net>
5 ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (test-syscalls)
23 #:use-module (guix utils)
24 #:use-module (guix build syscalls)
25 #:use-module (gnu build linux-container)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-64)
29 #:use-module (srfi srfi-71)
30 #:use-module (system foreign)
31 #:use-module ((ice-9 ftw) #:select (scandir))
32 #:use-module (ice-9 match))
34 ;; Test the (guix build syscalls) module, although there's not much that can
35 ;; actually be tested without being root.
38 (string-append "t-utils-" (number->string (getpid))))
41 (test-begin "syscalls")
43 (test-equal "mount, ENOENT"
47 (mount "/dev/null" "/does-not-exist" "ext2")
49 (compose system-error-errno list)))
51 (test-assert "umount, ENOENT/EPERM"
54 (umount "/does-not-exist")
57 ;; Both return values have been encountered in the wild.
58 (memv (system-error-errno args) (list EPERM ENOENT)))))
61 ;; Check for one of the common mount points.
62 (let ((mounts (mounts)))
65 (let ((mount (find (lambda (mount)
66 (string=? (mount-point mount) point))
69 (string=? (mount-type mount) type)))))
72 ("/dev/shm" . "tmpfs")))))
74 (test-assert "mount-points"
75 ;; Reportedly "/" is not always listed as a mount point, so check a few
76 ;; others (see <http://bugs.gnu.org/20261>.)
77 (any (cute member <> (mount-points))
78 '("/" "/proc" "/sys" "/dev")))
80 (false-if-exception (delete-file temp-file))
81 (test-equal "utime with AT_SYMLINK_NOFOLLOW"
84 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
85 ;; define as of Guile 2.2.4.
86 (symlink "/nowhere" temp-file)
87 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
88 (let ((st (lstat temp-file)))
89 (delete-file temp-file)
90 ;; Note: 'utimensat' does not change 'ctime'.
91 (list (stat:mtime st) (stat:atime st)))))
93 (test-assert "swapon, ENOSYS/ENOENT/EPERM"
96 (swapon "/does-not-exist")
99 (memv (system-error-errno args) (list EPERM ENOENT ENOSYS)))))
101 (test-assert "swapoff, ENOSYS/ENOENT/EINVAL/EPERM"
104 (swapoff "/does-not-exist")
107 (memv (system-error-errno args) (list EPERM EINVAL ENOENT ENOSYS)))))
109 (test-assert "mkdtemp!"
110 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
111 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
112 (and (file-exists? dir)
117 (test-equal "statfs, ENOENT"
121 (statfs "/does-not-exist"))
122 (compose system-error-errno list)))
124 (test-assert "statfs"
125 (let ((fs (statfs "/")))
126 (and (file-system? fs)
127 (> (file-system-block-size fs) 0)
128 (>= (file-system-blocks-available fs) 0)
129 (>= (file-system-blocks-free fs)
130 (file-system-blocks-available fs)))))
132 (define (user-namespace pid)
133 (string-append "/proc/" (number->string pid) "/ns/user"))
135 (define perform-container-tests?
136 (and (user-namespace-supported?)
137 (unprivileged-user-namespace-supported?)))
139 (unless perform-container-tests?
142 (match (clone (logior CLONE_NEWUSER SIGCHLD))
143 (0 (primitive-exit 42))
145 ;; Check if user namespaces are different.
146 (and (not (equal? (readlink (user-namespace pid))
147 (readlink (user-namespace (getpid)))))
150 (= 42 (status:exit-val status))))))))
152 (unless perform-container-tests?
155 (match (clone (logior CLONE_NEWUSER SIGCHLD))
156 (0 (primitive-exit 0))
160 (match (primitive-fork)
163 ;; Join the user namespace.
164 (call-with-input-file (user-namespace clone-pid)
166 (setns (port->fdes port) 0)))
172 ;; Wait for the child process to join the namespace.
174 (let ((result (and (equal? (readlink (user-namespace clone-pid))
175 (readlink (user-namespace fork-pid))))))
181 (when (not perform-container-tests?)
183 (test-equal "pivot-root"
185 (match (socketpair AF_UNIX SOCK_STREAM 0)
187 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
193 (call-with-temporary-directory
195 (display "ready\n" child)
196 (read child) ;wait for "go!"
197 (let ((put-old (string-append root "/real-root")))
198 (mount "none" root "tmpfs")
200 (call-with-output-file (string-append root "/test")
202 (display "testing\n" port)))
203 (pivot-root root put-old)
204 ;; The test file should now be located inside the root directory.
205 (write (and (file-exists? "/test") 'success!) child)
208 (primitive-exit 0))))
213 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
214 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
215 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
217 (display "deny" port)))
218 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
220 (format port "0 ~d 1" (getuid))))
221 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
223 (format port "0 ~d 1" (getgid))))
224 (display "go!\n" parent)
225 (let ((result (read parent)))
227 (and (zero? (match (waitpid pid)
229 (status:exit-val status))))
232 (test-equal "scandir*, ENOENT"
236 (scandir* "/does/not/exist"))
238 (system-error-errno args))))
240 (test-equal "scandir*, ASCII file names"
241 (scandir (dirname (search-path %load-path "guix/base32.scm"))
243 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
244 (((names . properties) ...)
247 (test-equal "scandir*, UTF-8 file names"
249 (call-with-temporary-directory
251 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
252 ;; name to the system call.
253 (let ((creat (pointer->procedure int
254 (dynamic-func "creat" (dynamic-link))
256 (creat (string->pointer (string-append directory "/α")
259 (creat (string->pointer (string-append directory "/λ")
262 (let ((locale (setlocale LC_ALL)))
265 ;; Make sure that even in a C locale we get the right result.
266 (setlocale LC_ALL "C"))
268 (match (scandir* directory)
269 (((names . properties) ...)
272 (setlocale LC_ALL locale))))))))
274 (test-assert "scandir*, properties"
275 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
276 (every (lambda (entry name)
278 ((name2 . properties)
279 (and (string=? name2 name)
280 (let* ((full (string-append directory "/" name))
282 (inode (assoc-ref properties 'inode))
283 (type (assoc-ref properties 'type)))
284 (and (= inode (stat:ino stat))
285 (or (eq? type 'unknown)
286 (eq? type (stat:type stat)))))))))
288 (scandir directory (const #t) string<?))))
290 (false-if-exception (delete-file temp-file))
291 (test-assert "getxattr, setxattr"
292 (let ((key "user.translator")
293 (value "/hurd/pfinet\0")
294 (file (open-file temp-file "w0")))
297 (setxattr temp-file key value)
298 (string=? (getxattr temp-file key) value))
300 ;; Accept ENOTSUP, if the file-system does not support extended user
302 (memv (system-error-errno args) (list ENOTSUP))))))
304 (false-if-exception (delete-file temp-file))
305 (test-equal "fcntl-flock wait"
306 42 ; the child's exit status
307 (let ((file (open-file temp-file "w0b")))
308 ;; Acquire an exclusive lock.
309 (fcntl-flock file 'write-lock)
310 (match (primitive-fork)
315 ;; Reopen FILE read-only so we can have a read lock.
316 (let ((file (open-file temp-file "r0b")))
317 ;; Wait until we can acquire the lock.
318 (fcntl-flock file 'read-lock)
319 (primitive-exit (read file)))
322 (primitive-exit 2))))
324 ;; Write garbage and wait.
325 (display "hello, world!" file)
329 ;; Write the real answer.
330 (seek file 0 SEEK_SET)
331 (truncate-file file 0)
335 ;; Unlock, which should let the child continue.
336 (fcntl-flock file 'unlock)
340 (let ((result (status:exit-val status)))
344 (test-equal "fcntl-flock non-blocking"
345 EAGAIN ; the child's exit status
348 (match (primitive-fork)
355 ;; Wait for the green light.
358 ;; Open FILE read-only so we can have a read lock.
359 (let ((file (open-file temp-file "w0")))
362 ;; This attempt should throw EAGAIN.
363 (fcntl-flock file 'write-lock #:wait? #f))
365 (primitive-exit (pk 'errno errno)))))
368 (primitive-exit -2))))
371 (let ((file (open-file temp-file "w0")))
372 ;; Acquire an exclusive lock.
373 (fcntl-flock file 'write-lock)
375 ;; Tell the child to continue.
376 (write 'green-light output)
377 (force-output output)
381 (let ((result (status:exit-val status)))
382 (fcntl-flock file 'unlock)
386 (test-equal "set-thread-name"
388 (let ((name (thread-name)))
389 (set-thread-name "Syscall Test")
390 (let ((new-name (thread-name)))
391 (set-thread-name name)
394 (test-assert "all-network-interface-names"
395 (match (all-network-interface-names)
396 (((? string? names) ..1)
397 (member "lo" names))))
399 (test-assert "network-interface-names"
400 (match (remove (lambda (interface)
401 ;; Ignore interface aliases since they don't show up in
402 ;; (all-network-interface-names).
403 (string-contains interface ":"))
404 (network-interface-names))
405 (((? string? names) ..1)
406 (lset<= string=? names (all-network-interface-names)))))
408 (test-assert "network-interface-flags"
409 (let* ((sock (socket AF_INET SOCK_STREAM 0))
410 (flags (network-interface-flags sock "lo")))
412 (and (not (zero? (logand flags IFF_LOOPBACK)))
413 (not (zero? (logand flags IFF_UP))))))
415 (test-equal "loopback-network-interface?"
417 (and (loopback-network-interface? "lo")
420 (loopback-network-interface? "nonexistent")
423 (system-error-errno args)))))
425 (test-equal "loopback-network-interface-running?"
427 (and (network-interface-running? "lo")
430 (network-interface-running? "nonexistent")
433 (system-error-errno args)))))
435 (test-skip (if (zero? (getuid)) 1 0))
436 (test-assert "set-network-interface-flags"
437 (let ((sock (socket AF_INET SOCK_STREAM 0)))
440 (set-network-interface-flags sock "lo" IFF_UP))
443 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
444 (memv (system-error-errno args) (list EPERM EACCES))))))
446 (test-equal "network-interface-address lo"
447 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
448 (let* ((sock (socket AF_INET SOCK_STREAM 0))
449 (addr (network-interface-address sock "lo")))
453 (test-skip (if (zero? (getuid)) 1 0))
454 (test-assert "set-network-interface-address"
455 (let ((sock (socket AF_INET SOCK_STREAM 0)))
458 (set-network-interface-address sock "nonexistent"
461 (inet-pton AF_INET "127.12.14.15")
465 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
466 (memv (system-error-errno args) (list EPERM EACCES))))))
468 (test-equal "network-interface-netmask lo"
469 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
470 (let* ((sock (socket AF_INET SOCK_STREAM 0))
471 (addr (network-interface-netmask sock "lo")))
475 (test-skip (if (zero? (getuid)) 1 0))
476 (test-assert "set-network-interface-netmask"
477 (let ((sock (socket AF_INET SOCK_STREAM 0)))
480 (set-network-interface-netmask sock "nonexistent"
483 (inet-pton AF_INET "255.0.0.0")
487 (memv (system-error-errno args) (list EPERM EACCES))))))
489 (test-equal "network-interfaces returns one or more interfaces"
491 (match (network-interfaces)
493 (list (every interface? interfaces)
494 (every string? (map interface-name interfaces))
495 (every (lambda (sockaddr)
496 ;; Sometimes interfaces have no associated address.
497 (or (vector? sockaddr)
499 (map interface-address interfaces))))))
501 (test-equal "network-interfaces returns \"lo\""
502 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
503 (match (filter (lambda (interface)
504 (string=? "lo" (interface-name interface)))
505 (network-interfaces))
507 (list (every (lambda (lo)
508 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
510 (match (find (lambda (lo)
511 (= AF_INET (sockaddr:fam (interface-address lo))))
514 (lo (interface-address lo)))))))
516 (test-skip (if (zero? (getuid)) 1 0))
517 (test-assert "add-network-route/gateway"
518 (let ((sock (socket AF_INET SOCK_STREAM 0))
519 (gateway (make-socket-address AF_INET
520 (inet-pton AF_INET "192.168.0.1")
524 (add-network-route/gateway sock gateway))
527 (memv (system-error-errno args) (list EPERM EACCES))))))
529 (test-skip (if (zero? (getuid)) 1 0))
530 (test-assert "delete-network-route"
531 (let ((sock (socket AF_INET SOCK_STREAM 0))
532 (destination (make-socket-address AF_INET INADDR_ANY 0)))
535 (delete-network-route sock destination))
538 (memv (system-error-errno args) (list EPERM EACCES))))))
540 (test-equal "tcgetattr ENOTTY"
544 (call-with-input-file "/dev/null"
546 (tcgetattr (fileno port)))))
547 (compose system-error-errno list)))
549 (test-skip (if (and (file-exists? "/proc/self/fd/0")
550 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
554 (test-assert "tcgetattr"
555 (let ((termios (tcgetattr 0)))
556 (and (termios? termios)
557 (> (termios-input-speed termios) 0)
558 (> (termios-output-speed termios) 0))))
560 (test-assert "tcsetattr"
561 (let ((first (tcgetattr 0)))
562 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
563 (equal? first (tcgetattr 0))))
565 (test-assert "terminal-window-size ENOTTY"
566 (call-with-input-file "/dev/null"
570 (terminal-window-size port))
572 ;; Accept EINVAL, which some old Linux versions might return.
573 (memv (system-error-errno args)
574 (list ENOTTY EINVAL)))))))
576 (test-assert "terminal-columns"
577 (> (terminal-columns) 0))
579 (test-assert "terminal-columns non-file port"
580 (> (terminal-columns (open-input-string "Join us now, share the software!"))
583 (test-assert "terminal-rows"
584 (> (terminal-rows) 0))
586 (test-assert "openpty"
587 (let ((head inferior (openpty)))
588 (and (integer? head) (integer? inferior)
589 (let ((port (fdopen inferior "r+0")))
596 (test-equal "openpty + login-tty"
598 (let ((head inferior (openpty)))
599 (match (primitive-fork)
604 (setvbuf (current-input-port) 'none)
608 (read)) ;this gets EIO when HEAD is closed
610 (primitive-_exit 42))))
612 (close-fdes inferior)
613 (let ((head (fdopen head "r+0")))
614 (write '(hello world) head)
615 (let ((result (read head)))
620 (test-assert "utmpx-entries"
621 (match (utmpx-entries)
622 (((? utmpx? entries) ...)
623 (every (lambda (entry)
624 (match (utmpx-user entry)
626 ;; Ensure we have a valid PID for those entries where it
628 (or (not (memv (utmpx-login-type entry)
629 (list (login-type INIT_PROCESS)
630 (login-type LOGIN_PROCESS)
631 (login-type USER_PROCESS))))
632 (> (utmpx-pid entry) 0)))
633 (#f ;might be DEAD_PROCESS
637 (test-assert "read-utmpx, EOF"
638 (eof-object? (read-utmpx (%make-void-port "r"))))
640 (unless (access? "/var/run/utmpx" O_RDONLY)
642 (test-assert "read-utmpx"
643 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
644 (or (utmpx? result) (eof-object? result))))
646 (when (zero? (getuid))
648 (test-equal "add-to-entropy-count"
650 (call-with-output-file "/dev/urandom"
654 (add-to-entropy-count port 77)
657 (system-error-errno args))))))
661 (false-if-exception (delete-file temp-file))