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>
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)))))
59 (test-assert "mount-points"
60 ;; Reportedly "/" is not always listed as a mount point, so check a few
61 ;; others (see <http://bugs.gnu.org/20261>.)
62 (any (cute member <> (mount-points))
63 '("/" "/proc" "/sys" "/dev")))
65 (false-if-exception (delete-file temp-file))
66 (test-equal "utime with AT_SYMLINK_NOFOLLOW"
69 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
70 ;; define as of Guile 2.2.4.
71 (symlink "/nowhere" temp-file)
72 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
73 (let ((st (lstat temp-file)))
74 (delete-file temp-file)
75 ;; Note: 'utimensat' does not change 'ctime'.
76 (list (stat:mtime st) (stat:atime st)))))
78 (test-assert "swapon, ENOSYS/ENOENT/EPERM"
81 (swapon "/does-not-exist")
84 (memv (system-error-errno args) (list EPERM ENOENT ENOSYS)))))
86 (test-assert "swapoff, ENOSYS/ENOENT/EINVAL/EPERM"
89 (swapoff "/does-not-exist")
92 (memv (system-error-errno args) (list EPERM EINVAL ENOENT ENOSYS)))))
94 (test-assert "mkdtemp!"
95 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
96 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
97 (and (file-exists? dir)
102 (test-equal "statfs, ENOENT"
106 (statfs "/does-not-exist"))
107 (compose system-error-errno list)))
109 (test-assert "statfs"
110 (let ((fs (statfs "/")))
111 (and (file-system? fs)
112 (> (file-system-block-size fs) 0)
113 (>= (file-system-blocks-available fs) 0)
114 (>= (file-system-blocks-free fs)
115 (file-system-blocks-available fs)))))
117 (define (user-namespace pid)
118 (string-append "/proc/" (number->string pid) "/ns/user"))
120 (define perform-container-tests?
121 (and (user-namespace-supported?)
122 (unprivileged-user-namespace-supported?)))
124 (unless perform-container-tests?
127 (match (clone (logior CLONE_NEWUSER SIGCHLD))
128 (0 (primitive-exit 42))
130 ;; Check if user namespaces are different.
131 (and (not (equal? (readlink (user-namespace pid))
132 (readlink (user-namespace (getpid)))))
135 (= 42 (status:exit-val status))))))))
137 (unless perform-container-tests?
140 (match (clone (logior CLONE_NEWUSER SIGCHLD))
141 (0 (primitive-exit 0))
145 (match (primitive-fork)
148 ;; Join the user namespace.
149 (call-with-input-file (user-namespace clone-pid)
151 (setns (port->fdes port) 0)))
157 ;; Wait for the child process to join the namespace.
159 (let ((result (and (equal? (readlink (user-namespace clone-pid))
160 (readlink (user-namespace fork-pid))))))
166 (when (not perform-container-tests?)
168 (test-equal "pivot-root"
170 (match (socketpair AF_UNIX SOCK_STREAM 0)
172 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
178 (call-with-temporary-directory
180 (display "ready\n" child)
181 (read child) ;wait for "go!"
182 (let ((put-old (string-append root "/real-root")))
183 (mount "none" root "tmpfs")
185 (call-with-output-file (string-append root "/test")
187 (display "testing\n" port)))
188 (pivot-root root put-old)
189 ;; The test file should now be located inside the root directory.
190 (write (and (file-exists? "/test") 'success!) child)
193 (primitive-exit 0))))
198 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
199 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
200 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
202 (display "deny" port)))
203 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
205 (format port "0 ~d 1" (getuid))))
206 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
208 (format port "0 ~d 1" (getgid))))
209 (display "go!\n" parent)
210 (let ((result (read parent)))
212 (and (zero? (match (waitpid pid)
214 (status:exit-val status))))
217 (test-equal "scandir*, ENOENT"
221 (scandir* "/does/not/exist"))
223 (system-error-errno args))))
225 (test-equal "scandir*, ASCII file names"
226 (scandir (dirname (search-path %load-path "guix/base32.scm"))
228 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
229 (((names . properties) ...)
232 (test-equal "scandir*, UTF-8 file names"
234 (call-with-temporary-directory
236 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
237 ;; name to the system call.
238 (let ((creat (pointer->procedure int
239 (dynamic-func "creat" (dynamic-link))
241 (creat (string->pointer (string-append directory "/α")
244 (creat (string->pointer (string-append directory "/λ")
247 (let ((locale (setlocale LC_ALL)))
250 ;; Make sure that even in a C locale we get the right result.
251 (setlocale LC_ALL "C"))
253 (match (scandir* directory)
254 (((names . properties) ...)
257 (setlocale LC_ALL locale))))))))
259 (test-assert "scandir*, properties"
260 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
261 (every (lambda (entry name)
263 ((name2 . properties)
264 (and (string=? name2 name)
265 (let* ((full (string-append directory "/" name))
267 (inode (assoc-ref properties 'inode))
268 (type (assoc-ref properties 'type)))
269 (and (= inode (stat:ino stat))
270 (or (eq? type 'unknown)
271 (eq? type (stat:type stat)))))))))
273 (scandir directory (const #t) string<?))))
275 (false-if-exception (delete-file temp-file))
276 (test-assert "getxattr, setxattr"
277 (let ((key "user.translator")
278 (value "/hurd/pfinet\0")
279 (file (open-file temp-file "w0")))
282 (setxattr temp-file key value)
283 (string=? (getxattr temp-file key) value))
285 ;; Accept ENOTSUP, if the file-system does not support extended user
287 (memv (system-error-errno args) (list ENOTSUP))))))
289 (false-if-exception (delete-file temp-file))
290 (test-equal "fcntl-flock wait"
291 42 ; the child's exit status
292 (let ((file (open-file temp-file "w0b")))
293 ;; Acquire an exclusive lock.
294 (fcntl-flock file 'write-lock)
295 (match (primitive-fork)
300 ;; Reopen FILE read-only so we can have a read lock.
301 (let ((file (open-file temp-file "r0b")))
302 ;; Wait until we can acquire the lock.
303 (fcntl-flock file 'read-lock)
304 (primitive-exit (read file)))
307 (primitive-exit 2))))
309 ;; Write garbage and wait.
310 (display "hello, world!" file)
314 ;; Write the real answer.
315 (seek file 0 SEEK_SET)
316 (truncate-file file 0)
320 ;; Unlock, which should let the child continue.
321 (fcntl-flock file 'unlock)
325 (let ((result (status:exit-val status)))
329 (test-equal "fcntl-flock non-blocking"
330 EAGAIN ; the child's exit status
333 (match (primitive-fork)
340 ;; Wait for the green light.
343 ;; Open FILE read-only so we can have a read lock.
344 (let ((file (open-file temp-file "w0")))
347 ;; This attempt should throw EAGAIN.
348 (fcntl-flock file 'write-lock #:wait? #f))
350 (primitive-exit (pk 'errno errno)))))
353 (primitive-exit -2))))
356 (let ((file (open-file temp-file "w0")))
357 ;; Acquire an exclusive lock.
358 (fcntl-flock file 'write-lock)
360 ;; Tell the child to continue.
361 (write 'green-light output)
362 (force-output output)
366 (let ((result (status:exit-val status)))
367 (fcntl-flock file 'unlock)
371 (test-equal "set-thread-name"
373 (let ((name (thread-name)))
374 (set-thread-name "Syscall Test")
375 (let ((new-name (thread-name)))
376 (set-thread-name name)
379 (test-assert "all-network-interface-names"
380 (match (all-network-interface-names)
381 (((? string? names) ..1)
382 (member "lo" names))))
384 (test-assert "network-interface-names"
385 (match (remove (lambda (interface)
386 ;; Ignore interface aliases since they don't show up in
387 ;; (all-network-interface-names).
388 (string-contains interface ":"))
389 (network-interface-names))
390 (((? string? names) ..1)
391 (lset<= string=? names (all-network-interface-names)))))
393 (test-assert "network-interface-flags"
394 (let* ((sock (socket AF_INET SOCK_STREAM 0))
395 (flags (network-interface-flags sock "lo")))
397 (and (not (zero? (logand flags IFF_LOOPBACK)))
398 (not (zero? (logand flags IFF_UP))))))
400 (test-equal "loopback-network-interface?"
402 (and (loopback-network-interface? "lo")
405 (loopback-network-interface? "nonexistent")
408 (system-error-errno args)))))
410 (test-equal "loopback-network-interface-running?"
412 (and (network-interface-running? "lo")
415 (network-interface-running? "nonexistent")
418 (system-error-errno args)))))
420 (test-skip (if (zero? (getuid)) 1 0))
421 (test-assert "set-network-interface-flags"
422 (let ((sock (socket AF_INET SOCK_STREAM 0)))
425 (set-network-interface-flags sock "lo" IFF_UP))
428 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
429 (memv (system-error-errno args) (list EPERM EACCES))))))
431 (test-equal "network-interface-address lo"
432 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
433 (let* ((sock (socket AF_INET SOCK_STREAM 0))
434 (addr (network-interface-address sock "lo")))
438 (test-skip (if (zero? (getuid)) 1 0))
439 (test-assert "set-network-interface-address"
440 (let ((sock (socket AF_INET SOCK_STREAM 0)))
443 (set-network-interface-address sock "nonexistent"
446 (inet-pton AF_INET "127.12.14.15")
450 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
451 (memv (system-error-errno args) (list EPERM EACCES))))))
453 (test-equal "network-interface-netmask lo"
454 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
455 (let* ((sock (socket AF_INET SOCK_STREAM 0))
456 (addr (network-interface-netmask sock "lo")))
460 (test-skip (if (zero? (getuid)) 1 0))
461 (test-assert "set-network-interface-netmask"
462 (let ((sock (socket AF_INET SOCK_STREAM 0)))
465 (set-network-interface-netmask sock "nonexistent"
468 (inet-pton AF_INET "255.0.0.0")
472 (memv (system-error-errno args) (list EPERM EACCES))))))
474 (test-equal "network-interfaces returns one or more interfaces"
476 (match (network-interfaces)
478 (list (every interface? interfaces)
479 (every string? (map interface-name interfaces))
480 (every (lambda (sockaddr)
481 ;; Sometimes interfaces have no associated address.
482 (or (vector? sockaddr)
484 (map interface-address interfaces))))))
486 (test-equal "network-interfaces returns \"lo\""
487 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
488 (match (filter (lambda (interface)
489 (string=? "lo" (interface-name interface)))
490 (network-interfaces))
492 (list (every (lambda (lo)
493 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
495 (match (find (lambda (lo)
496 (= AF_INET (sockaddr:fam (interface-address lo))))
499 (lo (interface-address lo)))))))
501 (test-skip (if (zero? (getuid)) 1 0))
502 (test-assert "add-network-route/gateway"
503 (let ((sock (socket AF_INET SOCK_STREAM 0))
504 (gateway (make-socket-address AF_INET
505 (inet-pton AF_INET "192.168.0.1")
509 (add-network-route/gateway sock gateway))
512 (memv (system-error-errno args) (list EPERM EACCES))))))
514 (test-skip (if (zero? (getuid)) 1 0))
515 (test-assert "delete-network-route"
516 (let ((sock (socket AF_INET SOCK_STREAM 0))
517 (destination (make-socket-address AF_INET INADDR_ANY 0)))
520 (delete-network-route sock destination))
523 (memv (system-error-errno args) (list EPERM EACCES))))))
525 (test-equal "tcgetattr ENOTTY"
529 (call-with-input-file "/dev/null"
531 (tcgetattr (fileno port)))))
532 (compose system-error-errno list)))
534 (test-skip (if (and (file-exists? "/proc/self/fd/0")
535 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
539 (test-assert "tcgetattr"
540 (let ((termios (tcgetattr 0)))
541 (and (termios? termios)
542 (> (termios-input-speed termios) 0)
543 (> (termios-output-speed termios) 0))))
545 (test-assert "tcsetattr"
546 (let ((first (tcgetattr 0)))
547 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
548 (equal? first (tcgetattr 0))))
550 (test-assert "terminal-window-size ENOTTY"
551 (call-with-input-file "/dev/null"
555 (terminal-window-size port))
557 ;; Accept EINVAL, which some old Linux versions might return.
558 (memv (system-error-errno args)
559 (list ENOTTY EINVAL)))))))
561 (test-assert "terminal-columns"
562 (> (terminal-columns) 0))
564 (test-assert "terminal-columns non-file port"
565 (> (terminal-columns (open-input-string "Join us now, share the software!"))
568 (test-assert "terminal-rows"
569 (> (terminal-rows) 0))
571 (test-assert "utmpx-entries"
572 (match (utmpx-entries)
573 (((? utmpx? entries) ...)
574 (every (lambda (entry)
575 (match (utmpx-user entry)
577 ;; Ensure we have a valid PID for those entries where it
579 (or (not (memv (utmpx-login-type entry)
580 (list (login-type INIT_PROCESS)
581 (login-type LOGIN_PROCESS)
582 (login-type USER_PROCESS))))
583 (> (utmpx-pid entry) 0)))
584 (#f ;might be DEAD_PROCESS
588 (test-assert "read-utmpx, EOF"
589 (eof-object? (read-utmpx (%make-void-port "r"))))
591 (unless (access? "/var/run/utmpx" O_RDONLY)
593 (test-assert "read-utmpx"
594 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
595 (or (utmpx? result) (eof-object? result))))
597 (when (zero? (getuid))
599 (test-equal "add-to-entropy-count"
601 (call-with-output-file "/dev/urandom"
605 (add-to-entropy-count port 77)
608 (system-error-errno args))))))
612 (false-if-exception (delete-file temp-file))