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 (ice-9 match))
29 ;; Test the (guix build syscalls) module, although there's not much that can
30 ;; actually be tested without being root.
33 (string-append "t-utils-" (number->string (getpid))))
36 (test-begin "syscalls")
38 (test-equal "mount, ENOENT"
42 (mount "/dev/null" "/does-not-exist" "ext2")
44 (compose system-error-errno list)))
46 (test-assert "umount, ENOENT/EPERM"
49 (umount "/does-not-exist")
52 ;; Both return values have been encountered in the wild.
53 (memv (system-error-errno args) (list EPERM ENOENT)))))
55 (test-assert "mount-points"
56 ;; Reportedly "/" is not always listed as a mount point, so check a few
57 ;; others (see <http://bugs.gnu.org/20261>.)
58 (any (cute member <> (mount-points))
59 '("/" "/proc" "/sys" "/dev")))
61 (test-assert "swapon, ENOENT/EPERM"
64 (swapon "/does-not-exist")
67 (memv (system-error-errno args) (list EPERM ENOENT)))))
69 (test-assert "swapoff, ENOENT/EINVAL/EPERM"
72 (swapoff "/does-not-exist")
75 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
77 (test-assert "mkdtemp!"
78 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
79 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
80 (and (file-exists? dir)
85 (test-equal "statfs, ENOENT"
89 (statfs "/does-not-exist"))
90 (compose system-error-errno list)))
93 (let ((fs (statfs "/")))
94 (and (file-system? fs)
95 (> (file-system-block-size fs) 0)
96 (>= (file-system-blocks-available fs) 0)
97 (>= (file-system-blocks-free fs)
98 (file-system-blocks-available fs)))))
100 (define (user-namespace pid)
101 (string-append "/proc/" (number->string pid) "/ns/user"))
103 (define perform-container-tests?
104 (and (user-namespace-supported?)
105 (unprivileged-user-namespace-supported?)))
107 (unless perform-container-tests?
110 (match (clone (logior CLONE_NEWUSER SIGCHLD))
111 (0 (primitive-exit 42))
113 ;; Check if user namespaces are different.
114 (and (not (equal? (readlink (user-namespace pid))
115 (readlink (user-namespace (getpid)))))
118 (= 42 (status:exit-val status))))))))
120 (unless perform-container-tests?
123 (match (clone (logior CLONE_NEWUSER SIGCHLD))
124 (0 (primitive-exit 0))
128 (match (primitive-fork)
131 ;; Join the user namespace.
132 (call-with-input-file (user-namespace clone-pid)
134 (setns (port->fdes port) 0)))
140 ;; Wait for the child process to join the namespace.
142 (let ((result (and (equal? (readlink (user-namespace clone-pid))
143 (readlink (user-namespace fork-pid))))))
149 ;; XXX: Skip this test when running Linux > 4.7.5 to work around
150 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
151 (when (or (not perform-container-tests?)
152 (version>? (utsname:release (uname)) "4.7.5"))
154 (test-equal "pivot-root"
158 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
164 (call-with-temporary-directory
166 (let ((put-old (string-append root "/real-root")))
167 (mount "none" root "tmpfs")
169 (call-with-output-file (string-append root "/test")
171 (display "testing\n" port)))
172 (pivot-root root put-old)
173 ;; The test file should now be located inside the root directory.
174 (write (file-exists? "/test") out)
177 (primitive-exit 0))))
180 (let ((result (read in)))
182 (and (zero? (match (waitpid pid)
184 (status:exit-val status))))
185 (eq? #t result))))))))
187 (false-if-exception (delete-file temp-file))
188 (test-equal "fcntl-flock wait"
189 42 ; the child's exit status
190 (let ((file (open-file temp-file "w0b")))
191 ;; Acquire an exclusive lock.
192 (fcntl-flock file 'write-lock)
193 (match (primitive-fork)
198 ;; Reopen FILE read-only so we can have a read lock.
199 (let ((file (open-file temp-file "r0b")))
200 ;; Wait until we can acquire the lock.
201 (fcntl-flock file 'read-lock)
202 (primitive-exit (read file)))
205 (primitive-exit 2))))
207 ;; Write garbage and wait.
208 (display "hello, world!" file)
212 ;; Write the real answer.
213 (seek file 0 SEEK_SET)
214 (truncate-file file 0)
218 ;; Unlock, which should let the child continue.
219 (fcntl-flock file 'unlock)
223 (let ((result (status:exit-val status)))
227 (test-equal "fcntl-flock non-blocking"
228 EAGAIN ; the child's exit status
231 (match (primitive-fork)
238 ;; Wait for the green light.
241 ;; Open FILE read-only so we can have a read lock.
242 (let ((file (open-file temp-file "w0")))
245 ;; This attempt should throw EAGAIN.
246 (fcntl-flock file 'write-lock #:wait? #f))
248 (primitive-exit (pk 'errno errno)))))
251 (primitive-exit -2))))
254 (let ((file (open-file temp-file "w0")))
255 ;; Acquire an exclusive lock.
256 (fcntl-flock file 'write-lock)
258 ;; Tell the child to continue.
259 (write 'green-light output)
260 (force-output output)
264 (let ((result (status:exit-val status)))
265 (fcntl-flock file 'unlock)
269 (test-assert "all-network-interface-names"
270 (match (all-network-interface-names)
271 (((? string? names) ..1)
272 (member "lo" names))))
274 (test-assert "network-interface-names"
275 (match (network-interface-names)
276 (((? string? names) ..1)
277 (lset<= string=? names (all-network-interface-names)))))
279 (test-assert "network-interface-flags"
280 (let* ((sock (socket AF_INET SOCK_STREAM 0))
281 (flags (network-interface-flags sock "lo")))
283 (and (not (zero? (logand flags IFF_LOOPBACK)))
284 (not (zero? (logand flags IFF_UP))))))
286 (test-equal "loopback-network-interface?"
288 (and (loopback-network-interface? "lo")
291 (loopback-network-interface? "nonexistent")
294 (system-error-errno args)))))
296 (test-skip (if (zero? (getuid)) 1 0))
297 (test-assert "set-network-interface-flags"
298 (let ((sock (socket AF_INET SOCK_STREAM 0)))
301 (set-network-interface-flags sock "lo" IFF_UP))
304 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
305 (memv (system-error-errno args) (list EPERM EACCES))))))
307 (test-equal "network-interface-address lo"
308 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
309 (let* ((sock (socket AF_INET SOCK_STREAM 0))
310 (addr (network-interface-address sock "lo")))
314 (test-skip (if (zero? (getuid)) 1 0))
315 (test-assert "set-network-interface-address"
316 (let ((sock (socket AF_INET SOCK_STREAM 0)))
319 (set-network-interface-address sock "nonexistent"
322 (inet-pton AF_INET "127.12.14.15")
326 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
327 (memv (system-error-errno args) (list EPERM EACCES))))))
329 (test-equal "network-interface-netmask lo"
330 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
331 (let* ((sock (socket AF_INET SOCK_STREAM 0))
332 (addr (network-interface-netmask sock "lo")))
336 (test-skip (if (zero? (getuid)) 1 0))
337 (test-assert "set-network-interface-netmask"
338 (let ((sock (socket AF_INET SOCK_STREAM 0)))
341 (set-network-interface-netmask sock "nonexistent"
344 (inet-pton AF_INET "255.0.0.0")
348 (memv (system-error-errno args) (list EPERM EACCES))))))
350 (test-equal "network-interfaces returns one or more interfaces"
352 (match (network-interfaces)
354 (list (every interface? interfaces)
355 (every string? (map interface-name interfaces))
356 (every (lambda (sockaddr)
357 ;; Sometimes interfaces have no associated address.
358 (or (vector? sockaddr)
360 (map interface-address interfaces))))))
362 (test-equal "network-interfaces returns \"lo\""
363 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
364 (match (filter (lambda (interface)
365 (string=? "lo" (interface-name interface)))
366 (network-interfaces))
368 (list (every (lambda (lo)
369 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
371 (match (find (lambda (lo)
372 (= AF_INET (sockaddr:fam (interface-address lo))))
375 (lo (interface-address lo)))))))
377 (test-skip (if (zero? (getuid)) 1 0))
378 (test-assert "add-network-route/gateway"
379 (let ((sock (socket AF_INET SOCK_STREAM 0))
380 (gateway (make-socket-address AF_INET
381 (inet-pton AF_INET "192.168.0.1")
385 (add-network-route/gateway sock gateway))
388 (memv (system-error-errno args) (list EPERM EACCES))))))
390 (test-skip (if (zero? (getuid)) 1 0))
391 (test-assert "delete-network-route"
392 (let ((sock (socket AF_INET SOCK_STREAM 0))
393 (destination (make-socket-address AF_INET INADDR_ANY 0)))
396 (delete-network-route sock destination))
399 (memv (system-error-errno args) (list EPERM EACCES))))))
401 (test-equal "tcgetattr ENOTTY"
405 (call-with-input-file "/dev/null"
407 (tcgetattr (fileno port)))))
408 (compose system-error-errno list)))
410 (test-skip (if (and (file-exists? "/proc/self/fd/0")
411 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
415 (test-assert "tcgetattr"
416 (let ((termios (tcgetattr 0)))
417 (and (termios? termios)
418 (> (termios-input-speed termios) 0)
419 (> (termios-output-speed termios) 0))))
421 (test-assert "tcsetattr"
422 (let ((first (tcgetattr 0)))
423 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
424 (equal? first (tcgetattr 0))))
426 (test-assert "terminal-window-size ENOTTY"
427 (call-with-input-file "/dev/null"
431 (terminal-window-size port))
433 ;; Accept EINVAL, which some old Linux versions might return.
434 (memv (system-error-errno args)
435 (list ENOTTY EINVAL)))))))
437 (test-assert "terminal-columns"
438 (> (terminal-columns) 0))
440 (test-assert "terminal-columns non-file port"
441 (> (terminal-columns (open-input-string "Join us now, share the software!"))
444 (test-assert "utmpx-entries"
445 (match (utmpx-entries)
446 (((? utmpx? entries) ...)
447 (every (lambda (entry)
448 (match (utmpx-user entry)
450 (or (eqv? (login-type BOOT_TIME) (utmpx-login-type entry))
451 (> (utmpx-pid entry) 0)))
452 (#f ;might be DEAD_PROCESS
456 (test-assert "read-utmpx, EOF"
457 (eof-object? (read-utmpx (%make-void-port "r"))))
459 (unless (access? "/var/run/utmpx" O_RDONLY)
461 (test-assert "read-utmpx"
462 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
463 (or (utmpx? result) (eof-object? result))))
467 (false-if-exception (delete-file temp-file))