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 (system foreign)
30 #:use-module ((ice-9 ftw) #:select (scandir))
31 #:use-module (ice-9 match))
33 ;; Test the (guix build syscalls) module, although there's not much that can
34 ;; actually be tested without being root.
37 (string-append "t-utils-" (number->string (getpid))))
40 (test-begin "syscalls")
42 (test-equal "mount, ENOENT"
46 (mount "/dev/null" "/does-not-exist" "ext2")
48 (compose system-error-errno list)))
50 (test-assert "umount, ENOENT/EPERM"
53 (umount "/does-not-exist")
56 ;; Both return values have been encountered in the wild.
57 (memv (system-error-errno args) (list EPERM ENOENT)))))
60 ;; Check for one of the common mount points.
61 (let ((mounts (mounts)))
64 (let ((mount (find (lambda (mount)
65 (string=? (mount-point mount) point))
68 (string=? (mount-type mount) type)))))
71 ("/dev/shm" . "tmpfs")))))
73 (test-assert "mount-points"
74 ;; Reportedly "/" is not always listed as a mount point, so check a few
75 ;; others (see <http://bugs.gnu.org/20261>.)
76 (any (cute member <> (mount-points))
77 '("/" "/proc" "/sys" "/dev")))
79 (false-if-exception (delete-file temp-file))
80 (test-equal "utime with AT_SYMLINK_NOFOLLOW"
83 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
84 ;; define as of Guile 2.2.4.
85 (symlink "/nowhere" temp-file)
86 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
87 (let ((st (lstat temp-file)))
88 (delete-file temp-file)
89 ;; Note: 'utimensat' does not change 'ctime'.
90 (list (stat:mtime st) (stat:atime st)))))
92 (test-assert "swapon, ENOSYS/ENOENT/EPERM"
95 (swapon "/does-not-exist")
98 (memv (system-error-errno args) (list EPERM ENOENT ENOSYS)))))
100 (test-assert "swapoff, ENOSYS/ENOENT/EINVAL/EPERM"
103 (swapoff "/does-not-exist")
106 (memv (system-error-errno args) (list EPERM EINVAL ENOENT ENOSYS)))))
108 (test-assert "mkdtemp!"
109 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
110 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
111 (and (file-exists? dir)
116 (test-equal "statfs, ENOENT"
120 (statfs "/does-not-exist"))
121 (compose system-error-errno list)))
123 (test-assert "statfs"
124 (let ((fs (statfs "/")))
125 (and (file-system? fs)
126 (> (file-system-block-size fs) 0)
127 (>= (file-system-blocks-available fs) 0)
128 (>= (file-system-blocks-free fs)
129 (file-system-blocks-available fs)))))
131 (define (user-namespace pid)
132 (string-append "/proc/" (number->string pid) "/ns/user"))
134 (define perform-container-tests?
135 (and (user-namespace-supported?)
136 (unprivileged-user-namespace-supported?)))
138 (unless perform-container-tests?
141 (match (clone (logior CLONE_NEWUSER SIGCHLD))
142 (0 (primitive-exit 42))
144 ;; Check if user namespaces are different.
145 (and (not (equal? (readlink (user-namespace pid))
146 (readlink (user-namespace (getpid)))))
149 (= 42 (status:exit-val status))))))))
151 (unless perform-container-tests?
154 (match (clone (logior CLONE_NEWUSER SIGCHLD))
155 (0 (primitive-exit 0))
159 (match (primitive-fork)
162 ;; Join the user namespace.
163 (call-with-input-file (user-namespace clone-pid)
165 (setns (port->fdes port) 0)))
171 ;; Wait for the child process to join the namespace.
173 (let ((result (and (equal? (readlink (user-namespace clone-pid))
174 (readlink (user-namespace fork-pid))))))
180 (when (not perform-container-tests?)
182 (test-equal "pivot-root"
184 (match (socketpair AF_UNIX SOCK_STREAM 0)
186 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
192 (call-with-temporary-directory
194 (display "ready\n" child)
195 (read child) ;wait for "go!"
196 (let ((put-old (string-append root "/real-root")))
197 (mount "none" root "tmpfs")
199 (call-with-output-file (string-append root "/test")
201 (display "testing\n" port)))
202 (pivot-root root put-old)
203 ;; The test file should now be located inside the root directory.
204 (write (and (file-exists? "/test") 'success!) child)
207 (primitive-exit 0))))
212 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
213 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
214 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
216 (display "deny" port)))
217 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
219 (format port "0 ~d 1" (getuid))))
220 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
222 (format port "0 ~d 1" (getgid))))
223 (display "go!\n" parent)
224 (let ((result (read parent)))
226 (and (zero? (match (waitpid pid)
228 (status:exit-val status))))
231 (test-equal "scandir*, ENOENT"
235 (scandir* "/does/not/exist"))
237 (system-error-errno args))))
239 (test-equal "scandir*, ASCII file names"
240 (scandir (dirname (search-path %load-path "guix/base32.scm"))
242 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
243 (((names . properties) ...)
246 (test-equal "scandir*, UTF-8 file names"
248 (call-with-temporary-directory
250 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
251 ;; name to the system call.
252 (let ((creat (pointer->procedure int
253 (dynamic-func "creat" (dynamic-link))
255 (creat (string->pointer (string-append directory "/α")
258 (creat (string->pointer (string-append directory "/λ")
261 (let ((locale (setlocale LC_ALL)))
264 ;; Make sure that even in a C locale we get the right result.
265 (setlocale LC_ALL "C"))
267 (match (scandir* directory)
268 (((names . properties) ...)
271 (setlocale LC_ALL locale))))))))
273 (test-assert "scandir*, properties"
274 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
275 (every (lambda (entry name)
277 ((name2 . properties)
278 (and (string=? name2 name)
279 (let* ((full (string-append directory "/" name))
281 (inode (assoc-ref properties 'inode))
282 (type (assoc-ref properties 'type)))
283 (and (= inode (stat:ino stat))
284 (or (eq? type 'unknown)
285 (eq? type (stat:type stat)))))))))
287 (scandir directory (const #t) string<?))))
289 (false-if-exception (delete-file temp-file))
290 (test-assert "getxattr, setxattr"
291 (let ((key "user.translator")
292 (value "/hurd/pfinet\0")
293 (file (open-file temp-file "w0")))
296 (setxattr temp-file key value)
297 (string=? (getxattr temp-file key) value))
299 ;; Accept ENOTSUP, if the file-system does not support extended user
301 (memv (system-error-errno args) (list ENOTSUP))))))
303 (false-if-exception (delete-file temp-file))
304 (test-equal "fcntl-flock wait"
305 42 ; the child's exit status
306 (let ((file (open-file temp-file "w0b")))
307 ;; Acquire an exclusive lock.
308 (fcntl-flock file 'write-lock)
309 (match (primitive-fork)
314 ;; Reopen FILE read-only so we can have a read lock.
315 (let ((file (open-file temp-file "r0b")))
316 ;; Wait until we can acquire the lock.
317 (fcntl-flock file 'read-lock)
318 (primitive-exit (read file)))
321 (primitive-exit 2))))
323 ;; Write garbage and wait.
324 (display "hello, world!" file)
328 ;; Write the real answer.
329 (seek file 0 SEEK_SET)
330 (truncate-file file 0)
334 ;; Unlock, which should let the child continue.
335 (fcntl-flock file 'unlock)
339 (let ((result (status:exit-val status)))
343 (test-equal "fcntl-flock non-blocking"
344 EAGAIN ; the child's exit status
347 (match (primitive-fork)
354 ;; Wait for the green light.
357 ;; Open FILE read-only so we can have a read lock.
358 (let ((file (open-file temp-file "w0")))
361 ;; This attempt should throw EAGAIN.
362 (fcntl-flock file 'write-lock #:wait? #f))
364 (primitive-exit (pk 'errno errno)))))
367 (primitive-exit -2))))
370 (let ((file (open-file temp-file "w0")))
371 ;; Acquire an exclusive lock.
372 (fcntl-flock file 'write-lock)
374 ;; Tell the child to continue.
375 (write 'green-light output)
376 (force-output output)
380 (let ((result (status:exit-val status)))
381 (fcntl-flock file 'unlock)
385 (test-equal "set-thread-name"
387 (let ((name (thread-name)))
388 (set-thread-name "Syscall Test")
389 (let ((new-name (thread-name)))
390 (set-thread-name name)
393 (test-assert "all-network-interface-names"
394 (match (all-network-interface-names)
395 (((? string? names) ..1)
396 (member "lo" names))))
398 (test-assert "network-interface-names"
399 (match (remove (lambda (interface)
400 ;; Ignore interface aliases since they don't show up in
401 ;; (all-network-interface-names).
402 (string-contains interface ":"))
403 (network-interface-names))
404 (((? string? names) ..1)
405 (lset<= string=? names (all-network-interface-names)))))
407 (test-assert "network-interface-flags"
408 (let* ((sock (socket AF_INET SOCK_STREAM 0))
409 (flags (network-interface-flags sock "lo")))
411 (and (not (zero? (logand flags IFF_LOOPBACK)))
412 (not (zero? (logand flags IFF_UP))))))
414 (test-equal "loopback-network-interface?"
416 (and (loopback-network-interface? "lo")
419 (loopback-network-interface? "nonexistent")
422 (system-error-errno args)))))
424 (test-equal "loopback-network-interface-running?"
426 (and (network-interface-running? "lo")
429 (network-interface-running? "nonexistent")
432 (system-error-errno args)))))
434 (test-skip (if (zero? (getuid)) 1 0))
435 (test-assert "set-network-interface-flags"
436 (let ((sock (socket AF_INET SOCK_STREAM 0)))
439 (set-network-interface-flags sock "lo" IFF_UP))
442 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
443 (memv (system-error-errno args) (list EPERM EACCES))))))
445 (test-equal "network-interface-address lo"
446 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
447 (let* ((sock (socket AF_INET SOCK_STREAM 0))
448 (addr (network-interface-address sock "lo")))
452 (test-skip (if (zero? (getuid)) 1 0))
453 (test-assert "set-network-interface-address"
454 (let ((sock (socket AF_INET SOCK_STREAM 0)))
457 (set-network-interface-address sock "nonexistent"
460 (inet-pton AF_INET "127.12.14.15")
464 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
465 (memv (system-error-errno args) (list EPERM EACCES))))))
467 (test-equal "network-interface-netmask lo"
468 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
469 (let* ((sock (socket AF_INET SOCK_STREAM 0))
470 (addr (network-interface-netmask sock "lo")))
474 (test-skip (if (zero? (getuid)) 1 0))
475 (test-assert "set-network-interface-netmask"
476 (let ((sock (socket AF_INET SOCK_STREAM 0)))
479 (set-network-interface-netmask sock "nonexistent"
482 (inet-pton AF_INET "255.0.0.0")
486 (memv (system-error-errno args) (list EPERM EACCES))))))
488 (test-equal "network-interfaces returns one or more interfaces"
490 (match (network-interfaces)
492 (list (every interface? interfaces)
493 (every string? (map interface-name interfaces))
494 (every (lambda (sockaddr)
495 ;; Sometimes interfaces have no associated address.
496 (or (vector? sockaddr)
498 (map interface-address interfaces))))))
500 (test-equal "network-interfaces returns \"lo\""
501 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
502 (match (filter (lambda (interface)
503 (string=? "lo" (interface-name interface)))
504 (network-interfaces))
506 (list (every (lambda (lo)
507 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
509 (match (find (lambda (lo)
510 (= AF_INET (sockaddr:fam (interface-address lo))))
513 (lo (interface-address lo)))))))
515 (test-skip (if (zero? (getuid)) 1 0))
516 (test-assert "add-network-route/gateway"
517 (let ((sock (socket AF_INET SOCK_STREAM 0))
518 (gateway (make-socket-address AF_INET
519 (inet-pton AF_INET "192.168.0.1")
523 (add-network-route/gateway sock gateway))
526 (memv (system-error-errno args) (list EPERM EACCES))))))
528 (test-skip (if (zero? (getuid)) 1 0))
529 (test-assert "delete-network-route"
530 (let ((sock (socket AF_INET SOCK_STREAM 0))
531 (destination (make-socket-address AF_INET INADDR_ANY 0)))
534 (delete-network-route sock destination))
537 (memv (system-error-errno args) (list EPERM EACCES))))))
539 (test-equal "tcgetattr ENOTTY"
543 (call-with-input-file "/dev/null"
545 (tcgetattr (fileno port)))))
546 (compose system-error-errno list)))
548 (test-skip (if (and (file-exists? "/proc/self/fd/0")
549 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
553 (test-assert "tcgetattr"
554 (let ((termios (tcgetattr 0)))
555 (and (termios? termios)
556 (> (termios-input-speed termios) 0)
557 (> (termios-output-speed termios) 0))))
559 (test-assert "tcsetattr"
560 (let ((first (tcgetattr 0)))
561 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
562 (equal? first (tcgetattr 0))))
564 (test-assert "terminal-window-size ENOTTY"
565 (call-with-input-file "/dev/null"
569 (terminal-window-size port))
571 ;; Accept EINVAL, which some old Linux versions might return.
572 (memv (system-error-errno args)
573 (list ENOTTY EINVAL)))))))
575 (test-assert "terminal-columns"
576 (> (terminal-columns) 0))
578 (test-assert "terminal-columns non-file port"
579 (> (terminal-columns (open-input-string "Join us now, share the software!"))
582 (test-assert "terminal-rows"
583 (> (terminal-rows) 0))
585 (test-assert "utmpx-entries"
586 (match (utmpx-entries)
587 (((? utmpx? entries) ...)
588 (every (lambda (entry)
589 (match (utmpx-user entry)
591 ;; Ensure we have a valid PID for those entries where it
593 (or (not (memv (utmpx-login-type entry)
594 (list (login-type INIT_PROCESS)
595 (login-type LOGIN_PROCESS)
596 (login-type USER_PROCESS))))
597 (> (utmpx-pid entry) 0)))
598 (#f ;might be DEAD_PROCESS
602 (test-assert "read-utmpx, EOF"
603 (eof-object? (read-utmpx (%make-void-port "r"))))
605 (unless (access? "/var/run/utmpx" O_RDONLY)
607 (test-assert "read-utmpx"
608 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
609 (or (utmpx? result) (eof-object? result))))
611 (when (zero? (getuid))
613 (test-equal "add-to-entropy-count"
615 (call-with-output-file "/dev/urandom"
619 (add-to-entropy-count port 77)
622 (system-error-errno args))))))
626 (false-if-exception (delete-file temp-file))