1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 (test-assert "swapon, ENOENT/EPERM"
66 (swapon "/does-not-exist")
69 (memv (system-error-errno args) (list EPERM ENOENT)))))
71 (test-assert "swapoff, ENOENT/EINVAL/EPERM"
74 (swapoff "/does-not-exist")
77 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
79 (test-assert "mkdtemp!"
80 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
81 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
82 (and (file-exists? dir)
87 (test-equal "statfs, ENOENT"
91 (statfs "/does-not-exist"))
92 (compose system-error-errno list)))
95 (let ((fs (statfs "/")))
96 (and (file-system? fs)
97 (> (file-system-block-size fs) 0)
98 (>= (file-system-blocks-available fs) 0)
99 (>= (file-system-blocks-free fs)
100 (file-system-blocks-available fs)))))
102 (define (user-namespace pid)
103 (string-append "/proc/" (number->string pid) "/ns/user"))
105 (define perform-container-tests?
106 (and (user-namespace-supported?)
107 (unprivileged-user-namespace-supported?)))
109 (unless perform-container-tests?
112 (match (clone (logior CLONE_NEWUSER SIGCHLD))
113 (0 (primitive-exit 42))
115 ;; Check if user namespaces are different.
116 (and (not (equal? (readlink (user-namespace pid))
117 (readlink (user-namespace (getpid)))))
120 (= 42 (status:exit-val status))))))))
122 (unless perform-container-tests?
125 (match (clone (logior CLONE_NEWUSER SIGCHLD))
126 (0 (primitive-exit 0))
130 (match (primitive-fork)
133 ;; Join the user namespace.
134 (call-with-input-file (user-namespace clone-pid)
136 (setns (port->fdes port) 0)))
142 ;; Wait for the child process to join the namespace.
144 (let ((result (and (equal? (readlink (user-namespace clone-pid))
145 (readlink (user-namespace fork-pid))))))
151 ;; XXX: Skip this test when running Linux > 4.7.5 to work around
152 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
153 (when (or (not perform-container-tests?)
154 (version>? (utsname:release (uname)) "4.7.5")
156 ;; Skip on Ubuntu's 4.4 kernels, which contain a backport of the
157 ;; faulty code: <https://bugs.gnu.org/25476>.
158 (member (utsname:release (uname))
159 '("4.4.0-21-generic" "4.4.0-59-generic"
160 "4.4.0-116-generic")))
162 (test-equal "pivot-root"
166 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
172 (call-with-temporary-directory
174 (let ((put-old (string-append root "/real-root")))
175 (mount "none" root "tmpfs")
177 (call-with-output-file (string-append root "/test")
179 (display "testing\n" port)))
180 (pivot-root root put-old)
181 ;; The test file should now be located inside the root directory.
182 (write (file-exists? "/test") out)
185 (primitive-exit 0))))
188 (let ((result (read in)))
190 (and (zero? (match (waitpid pid)
192 (status:exit-val status))))
193 (eq? #t result))))))))
195 (test-equal "scandir*, ENOENT"
199 (scandir* "/does/not/exist"))
201 (system-error-errno args))))
203 (test-equal "scandir*, ASCII file names"
204 (scandir (dirname (search-path %load-path "guix/base32.scm"))
206 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
207 (((names . properties) ...)
210 (test-equal "scandir*, UTF-8 file names"
212 (call-with-temporary-directory
214 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
215 ;; name to the system call.
216 (let ((creat (pointer->procedure int
217 (dynamic-func "creat" (dynamic-link))
219 (creat (string->pointer (string-append directory "/α")
222 (creat (string->pointer (string-append directory "/λ")
225 (let ((locale (setlocale LC_ALL)))
228 ;; Make sure that even in a C locale we get the right result.
229 (setlocale LC_ALL "C"))
231 (match (scandir* directory)
232 (((names . properties) ...)
235 (setlocale LC_ALL locale))))))))
237 (test-assert "scandir*, properties"
238 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
239 (every (lambda (entry name)
241 ((name2 . properties)
242 (and (string=? name2 name)
243 (let* ((full (string-append directory "/" name))
245 (inode (assoc-ref properties 'inode))
246 (type (assoc-ref properties 'type)))
247 (and (= inode (stat:ino stat))
248 (or (eq? type 'unknown)
249 (eq? type (stat:type stat)))))))))
251 (scandir directory (const #t) string<?))))
253 (false-if-exception (delete-file temp-file))
254 (test-equal "fcntl-flock wait"
255 42 ; the child's exit status
256 (let ((file (open-file temp-file "w0b")))
257 ;; Acquire an exclusive lock.
258 (fcntl-flock file 'write-lock)
259 (match (primitive-fork)
264 ;; Reopen FILE read-only so we can have a read lock.
265 (let ((file (open-file temp-file "r0b")))
266 ;; Wait until we can acquire the lock.
267 (fcntl-flock file 'read-lock)
268 (primitive-exit (read file)))
271 (primitive-exit 2))))
273 ;; Write garbage and wait.
274 (display "hello, world!" file)
278 ;; Write the real answer.
279 (seek file 0 SEEK_SET)
280 (truncate-file file 0)
284 ;; Unlock, which should let the child continue.
285 (fcntl-flock file 'unlock)
289 (let ((result (status:exit-val status)))
293 (test-equal "fcntl-flock non-blocking"
294 EAGAIN ; the child's exit status
297 (match (primitive-fork)
304 ;; Wait for the green light.
307 ;; Open FILE read-only so we can have a read lock.
308 (let ((file (open-file temp-file "w0")))
311 ;; This attempt should throw EAGAIN.
312 (fcntl-flock file 'write-lock #:wait? #f))
314 (primitive-exit (pk 'errno errno)))))
317 (primitive-exit -2))))
320 (let ((file (open-file temp-file "w0")))
321 ;; Acquire an exclusive lock.
322 (fcntl-flock file 'write-lock)
324 ;; Tell the child to continue.
325 (write 'green-light output)
326 (force-output output)
330 (let ((result (status:exit-val status)))
331 (fcntl-flock file 'unlock)
335 (test-equal "set-thread-name"
337 (let ((name (thread-name)))
338 (set-thread-name "Syscall Test")
339 (let ((new-name (thread-name)))
340 (set-thread-name name)
343 (test-assert "all-network-interface-names"
344 (match (all-network-interface-names)
345 (((? string? names) ..1)
346 (member "lo" names))))
348 (test-assert "network-interface-names"
349 (match (network-interface-names)
350 (((? string? names) ..1)
351 (lset<= string=? names (all-network-interface-names)))))
353 (test-assert "network-interface-flags"
354 (let* ((sock (socket AF_INET SOCK_STREAM 0))
355 (flags (network-interface-flags sock "lo")))
357 (and (not (zero? (logand flags IFF_LOOPBACK)))
358 (not (zero? (logand flags IFF_UP))))))
360 (test-equal "loopback-network-interface?"
362 (and (loopback-network-interface? "lo")
365 (loopback-network-interface? "nonexistent")
368 (system-error-errno args)))))
370 (test-equal "loopback-network-interface-running?"
372 (and (network-interface-running? "lo")
375 (network-interface-running? "nonexistent")
378 (system-error-errno args)))))
380 (test-skip (if (zero? (getuid)) 1 0))
381 (test-assert "set-network-interface-flags"
382 (let ((sock (socket AF_INET SOCK_STREAM 0)))
385 (set-network-interface-flags sock "lo" IFF_UP))
388 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
389 (memv (system-error-errno args) (list EPERM EACCES))))))
391 (test-equal "network-interface-address lo"
392 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
393 (let* ((sock (socket AF_INET SOCK_STREAM 0))
394 (addr (network-interface-address sock "lo")))
398 (test-skip (if (zero? (getuid)) 1 0))
399 (test-assert "set-network-interface-address"
400 (let ((sock (socket AF_INET SOCK_STREAM 0)))
403 (set-network-interface-address sock "nonexistent"
406 (inet-pton AF_INET "127.12.14.15")
410 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
411 (memv (system-error-errno args) (list EPERM EACCES))))))
413 (test-equal "network-interface-netmask lo"
414 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
415 (let* ((sock (socket AF_INET SOCK_STREAM 0))
416 (addr (network-interface-netmask sock "lo")))
420 (test-skip (if (zero? (getuid)) 1 0))
421 (test-assert "set-network-interface-netmask"
422 (let ((sock (socket AF_INET SOCK_STREAM 0)))
425 (set-network-interface-netmask sock "nonexistent"
428 (inet-pton AF_INET "255.0.0.0")
432 (memv (system-error-errno args) (list EPERM EACCES))))))
434 (test-equal "network-interfaces returns one or more interfaces"
436 (match (network-interfaces)
438 (list (every interface? interfaces)
439 (every string? (map interface-name interfaces))
440 (every (lambda (sockaddr)
441 ;; Sometimes interfaces have no associated address.
442 (or (vector? sockaddr)
444 (map interface-address interfaces))))))
446 (test-equal "network-interfaces returns \"lo\""
447 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
448 (match (filter (lambda (interface)
449 (string=? "lo" (interface-name interface)))
450 (network-interfaces))
452 (list (every (lambda (lo)
453 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
455 (match (find (lambda (lo)
456 (= AF_INET (sockaddr:fam (interface-address lo))))
459 (lo (interface-address lo)))))))
461 (test-skip (if (zero? (getuid)) 1 0))
462 (test-assert "add-network-route/gateway"
463 (let ((sock (socket AF_INET SOCK_STREAM 0))
464 (gateway (make-socket-address AF_INET
465 (inet-pton AF_INET "192.168.0.1")
469 (add-network-route/gateway sock gateway))
472 (memv (system-error-errno args) (list EPERM EACCES))))))
474 (test-skip (if (zero? (getuid)) 1 0))
475 (test-assert "delete-network-route"
476 (let ((sock (socket AF_INET SOCK_STREAM 0))
477 (destination (make-socket-address AF_INET INADDR_ANY 0)))
480 (delete-network-route sock destination))
483 (memv (system-error-errno args) (list EPERM EACCES))))))
485 (test-equal "tcgetattr ENOTTY"
489 (call-with-input-file "/dev/null"
491 (tcgetattr (fileno port)))))
492 (compose system-error-errno list)))
494 (test-skip (if (and (file-exists? "/proc/self/fd/0")
495 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
499 (test-assert "tcgetattr"
500 (let ((termios (tcgetattr 0)))
501 (and (termios? termios)
502 (> (termios-input-speed termios) 0)
503 (> (termios-output-speed termios) 0))))
505 (test-assert "tcsetattr"
506 (let ((first (tcgetattr 0)))
507 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
508 (equal? first (tcgetattr 0))))
510 (test-assert "terminal-window-size ENOTTY"
511 (call-with-input-file "/dev/null"
515 (terminal-window-size port))
517 ;; Accept EINVAL, which some old Linux versions might return.
518 (memv (system-error-errno args)
519 (list ENOTTY EINVAL)))))))
521 (test-assert "terminal-columns"
522 (> (terminal-columns) 0))
524 (test-assert "terminal-columns non-file port"
525 (> (terminal-columns (open-input-string "Join us now, share the software!"))
528 (test-assert "utmpx-entries"
529 (match (utmpx-entries)
530 (((? utmpx? entries) ...)
531 (every (lambda (entry)
532 (match (utmpx-user entry)
534 ;; Ensure we have a valid PID for those entries where it
536 (or (not (memv (utmpx-login-type entry)
537 (list (login-type INIT_PROCESS)
538 (login-type LOGIN_PROCESS)
539 (login-type USER_PROCESS))))
540 (> (utmpx-pid entry) 0)))
541 (#f ;might be DEAD_PROCESS
545 (test-assert "read-utmpx, EOF"
546 (eof-object? (read-utmpx (%make-void-port "r"))))
548 (unless (access? "/var/run/utmpx" O_RDONLY)
550 (test-assert "read-utmpx"
551 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
552 (or (utmpx? result) (eof-object? result))))
556 (false-if-exception (delete-file temp-file))