1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 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 (test-equal "pivot-root"
160 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
166 (call-with-temporary-directory
168 (let ((put-old (string-append root "/real-root")))
169 (mount "none" root "tmpfs")
171 (call-with-output-file (string-append root "/test")
173 (display "testing\n" port)))
174 (pivot-root root put-old)
175 ;; The test file should now be located inside the root directory.
176 (write (file-exists? "/test") out)
179 (primitive-exit 0))))
182 (let ((result (read in)))
184 (and (zero? (match (waitpid pid)
186 (status:exit-val status))))
187 (eq? #t result))))))))
189 (test-equal "scandir*, ENOENT"
193 (scandir* "/does/not/exist"))
195 (system-error-errno args))))
197 (test-equal "scandir*, ASCII file names"
198 (scandir (dirname (search-path %load-path "guix/base32.scm"))
200 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
201 (((names . properties) ...)
204 (test-equal "scandir*, UTF-8 file names"
206 (call-with-temporary-directory
208 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
209 ;; name to the system call.
210 (let ((creat (pointer->procedure int
211 (dynamic-func "creat" (dynamic-link))
213 (creat (string->pointer (string-append directory "/α")
216 (creat (string->pointer (string-append directory "/λ")
219 (let ((locale (setlocale LC_ALL)))
222 ;; Make sure that even in a C locale we get the right result.
223 (setlocale LC_ALL "C"))
225 (match (scandir* directory)
226 (((names . properties) ...)
229 (setlocale LC_ALL locale))))))))
231 (test-assert "scandir*, properties"
232 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
233 (every (lambda (entry name)
235 ((name2 . properties)
236 (and (string=? name2 name)
237 (let* ((full (string-append directory "/" name))
239 (inode (assoc-ref properties 'inode))
240 (type (assoc-ref properties 'type)))
241 (and (= inode (stat:ino stat))
242 (or (eq? type 'unknown)
243 (eq? type (stat:type stat)))))))))
245 (scandir directory (const #t) string<?))))
247 (false-if-exception (delete-file temp-file))
248 (test-equal "fcntl-flock wait"
249 42 ; the child's exit status
250 (let ((file (open-file temp-file "w0b")))
251 ;; Acquire an exclusive lock.
252 (fcntl-flock file 'write-lock)
253 (match (primitive-fork)
258 ;; Reopen FILE read-only so we can have a read lock.
259 (let ((file (open-file temp-file "r0b")))
260 ;; Wait until we can acquire the lock.
261 (fcntl-flock file 'read-lock)
262 (primitive-exit (read file)))
265 (primitive-exit 2))))
267 ;; Write garbage and wait.
268 (display "hello, world!" file)
272 ;; Write the real answer.
273 (seek file 0 SEEK_SET)
274 (truncate-file file 0)
278 ;; Unlock, which should let the child continue.
279 (fcntl-flock file 'unlock)
283 (let ((result (status:exit-val status)))
287 (test-equal "fcntl-flock non-blocking"
288 EAGAIN ; the child's exit status
291 (match (primitive-fork)
298 ;; Wait for the green light.
301 ;; Open FILE read-only so we can have a read lock.
302 (let ((file (open-file temp-file "w0")))
305 ;; This attempt should throw EAGAIN.
306 (fcntl-flock file 'write-lock #:wait? #f))
308 (primitive-exit (pk 'errno errno)))))
311 (primitive-exit -2))))
314 (let ((file (open-file temp-file "w0")))
315 ;; Acquire an exclusive lock.
316 (fcntl-flock file 'write-lock)
318 ;; Tell the child to continue.
319 (write 'green-light output)
320 (force-output output)
324 (let ((result (status:exit-val status)))
325 (fcntl-flock file 'unlock)
329 (test-equal "set-thread-name"
331 (let ((name (thread-name)))
332 (set-thread-name "Syscall Test")
333 (let ((new-name (thread-name)))
334 (set-thread-name name)
337 (test-assert "all-network-interface-names"
338 (match (all-network-interface-names)
339 (((? string? names) ..1)
340 (member "lo" names))))
342 (test-assert "network-interface-names"
343 (match (network-interface-names)
344 (((? string? names) ..1)
345 (lset<= string=? names (all-network-interface-names)))))
347 (test-assert "network-interface-flags"
348 (let* ((sock (socket AF_INET SOCK_STREAM 0))
349 (flags (network-interface-flags sock "lo")))
351 (and (not (zero? (logand flags IFF_LOOPBACK)))
352 (not (zero? (logand flags IFF_UP))))))
354 (test-equal "loopback-network-interface?"
356 (and (loopback-network-interface? "lo")
359 (loopback-network-interface? "nonexistent")
362 (system-error-errno args)))))
364 (test-equal "loopback-network-interface-running?"
366 (and (network-interface-running? "lo")
369 (network-interface-running? "nonexistent")
372 (system-error-errno args)))))
374 (test-skip (if (zero? (getuid)) 1 0))
375 (test-assert "set-network-interface-flags"
376 (let ((sock (socket AF_INET SOCK_STREAM 0)))
379 (set-network-interface-flags sock "lo" IFF_UP))
382 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
383 (memv (system-error-errno args) (list EPERM EACCES))))))
385 (test-equal "network-interface-address lo"
386 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
387 (let* ((sock (socket AF_INET SOCK_STREAM 0))
388 (addr (network-interface-address sock "lo")))
392 (test-skip (if (zero? (getuid)) 1 0))
393 (test-assert "set-network-interface-address"
394 (let ((sock (socket AF_INET SOCK_STREAM 0)))
397 (set-network-interface-address sock "nonexistent"
400 (inet-pton AF_INET "127.12.14.15")
404 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
405 (memv (system-error-errno args) (list EPERM EACCES))))))
407 (test-equal "network-interface-netmask lo"
408 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
409 (let* ((sock (socket AF_INET SOCK_STREAM 0))
410 (addr (network-interface-netmask sock "lo")))
414 (test-skip (if (zero? (getuid)) 1 0))
415 (test-assert "set-network-interface-netmask"
416 (let ((sock (socket AF_INET SOCK_STREAM 0)))
419 (set-network-interface-netmask sock "nonexistent"
422 (inet-pton AF_INET "255.0.0.0")
426 (memv (system-error-errno args) (list EPERM EACCES))))))
428 (test-equal "network-interfaces returns one or more interfaces"
430 (match (network-interfaces)
432 (list (every interface? interfaces)
433 (every string? (map interface-name interfaces))
434 (every (lambda (sockaddr)
435 ;; Sometimes interfaces have no associated address.
436 (or (vector? sockaddr)
438 (map interface-address interfaces))))))
440 (test-equal "network-interfaces returns \"lo\""
441 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
442 (match (filter (lambda (interface)
443 (string=? "lo" (interface-name interface)))
444 (network-interfaces))
446 (list (every (lambda (lo)
447 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
449 (match (find (lambda (lo)
450 (= AF_INET (sockaddr:fam (interface-address lo))))
453 (lo (interface-address lo)))))))
455 (test-skip (if (zero? (getuid)) 1 0))
456 (test-assert "add-network-route/gateway"
457 (let ((sock (socket AF_INET SOCK_STREAM 0))
458 (gateway (make-socket-address AF_INET
459 (inet-pton AF_INET "192.168.0.1")
463 (add-network-route/gateway sock gateway))
466 (memv (system-error-errno args) (list EPERM EACCES))))))
468 (test-skip (if (zero? (getuid)) 1 0))
469 (test-assert "delete-network-route"
470 (let ((sock (socket AF_INET SOCK_STREAM 0))
471 (destination (make-socket-address AF_INET INADDR_ANY 0)))
474 (delete-network-route sock destination))
477 (memv (system-error-errno args) (list EPERM EACCES))))))
479 (test-equal "tcgetattr ENOTTY"
483 (call-with-input-file "/dev/null"
485 (tcgetattr (fileno port)))))
486 (compose system-error-errno list)))
488 (test-skip (if (and (file-exists? "/proc/self/fd/0")
489 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
493 (test-assert "tcgetattr"
494 (let ((termios (tcgetattr 0)))
495 (and (termios? termios)
496 (> (termios-input-speed termios) 0)
497 (> (termios-output-speed termios) 0))))
499 (test-assert "tcsetattr"
500 (let ((first (tcgetattr 0)))
501 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
502 (equal? first (tcgetattr 0))))
504 (test-assert "terminal-window-size ENOTTY"
505 (call-with-input-file "/dev/null"
509 (terminal-window-size port))
511 ;; Accept EINVAL, which some old Linux versions might return.
512 (memv (system-error-errno args)
513 (list ENOTTY EINVAL)))))))
515 (test-assert "terminal-columns"
516 (> (terminal-columns) 0))
518 (test-assert "terminal-columns non-file port"
519 (> (terminal-columns (open-input-string "Join us now, share the software!"))
522 (test-assert "utmpx-entries"
523 (match (utmpx-entries)
524 (((? utmpx? entries) ...)
525 (every (lambda (entry)
526 (match (utmpx-user entry)
528 (or (eqv? (login-type BOOT_TIME) (utmpx-login-type entry))
529 (> (utmpx-pid entry) 0)))
530 (#f ;might be DEAD_PROCESS
534 (test-assert "read-utmpx, EOF"
535 (eof-object? (read-utmpx (%make-void-port "r"))))
537 (unless (access? "/var/run/utmpx" O_RDONLY)
539 (test-assert "read-utmpx"
540 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
541 (or (utmpx? result) (eof-object? result))))
545 (false-if-exception (delete-file temp-file))