deduplication: Remove 'false-if-system-error', now unused.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
6ef80eb0 2;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
b4abdeb6 3;;; Copyright © 2015 David Thompson <davet@gnu.org>
29fa45f4
LC
4;;;
5;;; This file is part of GNU Guix.
6;;;
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.
11;;;
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.
16;;;
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/>.
19
20(define-module (test-syscalls)
df3ce5c1 21 #:use-module (guix utils)
29fa45f4 22 #:use-module (guix build syscalls)
b7d48312 23 #:use-module (gnu build linux-container)
4d54785c 24 #:use-module (srfi srfi-1)
381ac93b 25 #:use-module (srfi srfi-26)
7585016f 26 #:use-module (srfi srfi-64)
fa73c193
LC
27 #:use-module (system foreign)
28 #:use-module ((ice-9 ftw) #:select (scandir))
7585016f 29 #:use-module (ice-9 match))
29fa45f4
LC
30
31;; Test the (guix build syscalls) module, although there's not much that can
32;; actually be tested without being root.
33
4e0ea3eb
LC
34(define temp-file
35 (string-append "t-utils-" (number->string (getpid))))
36
37\f
29fa45f4
LC
38(test-begin "syscalls")
39
40(test-equal "mount, ENOENT"
41 ENOENT
42 (catch 'system-error
43 (lambda ()
44 (mount "/dev/null" "/does-not-exist" "ext2")
45 #f)
46 (compose system-error-errno list)))
47
35066aa5 48(test-assert "umount, ENOENT/EPERM"
29fa45f4
LC
49 (catch 'system-error
50 (lambda ()
51 (umount "/does-not-exist")
52 #f)
35066aa5
LC
53 (lambda args
54 ;; Both return values have been encountered in the wild.
55 (memv (system-error-errno args) (list EPERM ENOENT)))))
29fa45f4 56
ccea821b 57(test-assert "mount-points"
381ac93b
LC
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")))
ccea821b 62
715fc9d4
LC
63(test-assert "swapon, ENOENT/EPERM"
64 (catch 'system-error
65 (lambda ()
66 (swapon "/does-not-exist")
67 #f)
68 (lambda args
69 (memv (system-error-errno args) (list EPERM ENOENT)))))
70
2793c0fb 71(test-assert "swapoff, ENOENT/EINVAL/EPERM"
715fc9d4
LC
72 (catch 'system-error
73 (lambda ()
74 (swapoff "/does-not-exist")
75 #f)
76 (lambda args
2793c0fb 77 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
715fc9d4 78
b4abdeb6
DT
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)
83 (begin
84 (rmdir dir)
85 #t))))
86
a1f70878
LC
87(test-equal "statfs, ENOENT"
88 ENOENT
89 (catch 'system-error
90 (lambda ()
91 (statfs "/does-not-exist"))
92 (compose system-error-errno list)))
93
94(test-assert "statfs"
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)))))
101
8950ed11
DT
102(define (user-namespace pid)
103 (string-append "/proc/" (number->string pid) "/ns/user"))
104
b7d48312
DT
105(define perform-container-tests?
106 (and (user-namespace-supported?)
107 (unprivileged-user-namespace-supported?)))
108
109(unless perform-container-tests?
b62a3ebc 110 (test-skip 1))
8950ed11
DT
111(test-assert "clone"
112 (match (clone (logior CLONE_NEWUSER SIGCHLD))
113 (0 (primitive-exit 42))
114 (pid
115 ;; Check if user namespaces are different.
116 (and (not (equal? (readlink (user-namespace pid))
117 (readlink (user-namespace (getpid)))))
118 (match (waitpid pid)
119 ((_ . status)
120 (= 42 (status:exit-val status))))))))
121
b7d48312 122(unless perform-container-tests?
b62a3ebc 123 (test-skip 1))
43ace6ea
DT
124(test-assert "setns"
125 (match (clone (logior CLONE_NEWUSER SIGCHLD))
126 (0 (primitive-exit 0))
127 (clone-pid
128 (match (pipe)
129 ((in . out)
130 (match (primitive-fork)
131 (0
132 (close in)
133 ;; Join the user namespace.
134 (call-with-input-file (user-namespace clone-pid)
135 (lambda (port)
136 (setns (port->fdes port) 0)))
137 (write 'done out)
138 (close out)
139 (primitive-exit 0))
140 (fork-pid
141 (close out)
142 ;; Wait for the child process to join the namespace.
143 (read in)
144 (let ((result (and (equal? (readlink (user-namespace clone-pid))
145 (readlink (user-namespace fork-pid))))))
146 ;; Clean up.
147 (waitpid clone-pid)
148 (waitpid fork-pid)
149 result))))))))
8950ed11 150
a91d75ec
LC
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?)
6ef80eb0
LC
154 (version>? (utsname:release (uname)) "4.7.5")
155
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")))
b62a3ebc 161 (test-skip 1))
fe9bdb58
LC
162(test-equal "pivot-root"
163 #t
df3ce5c1
DT
164 (match (pipe)
165 ((in . out)
166 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
167 (0
fe9bdb58
LC
168 (dynamic-wind
169 (const #t)
170 (lambda ()
171 (close in)
172 (call-with-temporary-directory
173 (lambda (root)
174 (let ((put-old (string-append root "/real-root")))
175 (mount "none" root "tmpfs")
176 (mkdir put-old)
177 (call-with-output-file (string-append root "/test")
178 (lambda (port)
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)
183 (close out)))))
184 (lambda ()
185 (primitive-exit 0))))
df3ce5c1
DT
186 (pid
187 (close out)
188 (let ((result (read in)))
189 (close in)
190 (and (zero? (match (waitpid pid)
191 ((_ . status)
192 (status:exit-val status))))
193 (eq? #t result))))))))
194
fa73c193
LC
195(test-equal "scandir*, ENOENT"
196 ENOENT
197 (catch 'system-error
198 (lambda ()
199 (scandir* "/does/not/exist"))
200 (lambda args
201 (system-error-errno args))))
202
203(test-equal "scandir*, ASCII file names"
204 (scandir (dirname (search-path %load-path "guix/base32.scm"))
205 (const #t) string<?)
206 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
207 (((names . properties) ...)
208 names)))
209
210(test-equal "scandir*, UTF-8 file names"
211 '("." ".." "α" "λ")
212 (call-with-temporary-directory
213 (lambda (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))
218 (list '* int))))
219 (creat (string->pointer (string-append directory "/α")
220 "UTF-8")
221 #o644)
222 (creat (string->pointer (string-append directory "/λ")
223 "UTF-8")
224 #o644)
225 (let ((locale (setlocale LC_ALL)))
226 (dynamic-wind
227 (lambda ()
228 ;; Make sure that even in a C locale we get the right result.
229 (setlocale LC_ALL "C"))
230 (lambda ()
231 (match (scandir* directory)
232 (((names . properties) ...)
233 names)))
234 (lambda ()
235 (setlocale LC_ALL locale))))))))
236
237(test-assert "scandir*, properties"
238 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
239 (every (lambda (entry name)
240 (match entry
241 ((name2 . properties)
242 (and (string=? name2 name)
243 (let* ((full (string-append directory "/" name))
244 (stat (lstat full))
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)))))))))
250 (scandir* directory)
251 (scandir directory (const #t) string<?))))
252
4e0ea3eb
LC
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)
260 (0
261 (dynamic-wind
262 (const #t)
263 (lambda ()
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)))
269 (primitive-exit 1))
270 (lambda ()
271 (primitive-exit 2))))
272 (pid
273 ;; Write garbage and wait.
274 (display "hello, world!" file)
275 (force-output file)
276 (sleep 1)
277
278 ;; Write the real answer.
279 (seek file 0 SEEK_SET)
280 (truncate-file file 0)
281 (write 42 file)
282 (force-output file)
283
284 ;; Unlock, which should let the child continue.
285 (fcntl-flock file 'unlock)
286
287 (match (waitpid pid)
288 ((_ . status)
289 (let ((result (status:exit-val status)))
290 (close-port file)
291 result)))))))
292
293(test-equal "fcntl-flock non-blocking"
294 EAGAIN ; the child's exit status
295 (match (pipe)
296 ((input . output)
297 (match (primitive-fork)
298 (0
299 (dynamic-wind
300 (const #t)
301 (lambda ()
302 (close-port output)
303
304 ;; Wait for the green light.
305 (read-char input)
306
307 ;; Open FILE read-only so we can have a read lock.
308 (let ((file (open-file temp-file "w0")))
309 (catch 'flock-error
310 (lambda ()
311 ;; This attempt should throw EAGAIN.
312 (fcntl-flock file 'write-lock #:wait? #f))
313 (lambda (key errno)
314 (primitive-exit (pk 'errno errno)))))
315 (primitive-exit -1))
316 (lambda ()
317 (primitive-exit -2))))
318 (pid
319 (close-port input)
320 (let ((file (open-file temp-file "w0")))
321 ;; Acquire an exclusive lock.
322 (fcntl-flock file 'write-lock)
323
324 ;; Tell the child to continue.
325 (write 'green-light output)
326 (force-output output)
327
328 (match (waitpid pid)
329 ((_ . status)
330 (let ((result (status:exit-val status)))
331 (fcntl-flock file 'unlock)
332 (close-port file)
333 result)))))))))
334
aa401f9b
LC
335(test-equal "set-thread-name"
336 "Syscall Test"
337 (let ((name (thread-name)))
338 (set-thread-name "Syscall Test")
339 (let ((new-name (thread-name)))
340 (set-thread-name name)
341 new-name)))
342
b89e7405
LC
343(test-assert "all-network-interface-names"
344 (match (all-network-interface-names)
4d54785c
LC
345 (((? string? names) ..1)
346 (member "lo" names))))
347
b89e7405
LC
348(test-assert "network-interface-names"
349 (match (network-interface-names)
7585016f 350 (((? string? names) ..1)
b89e7405 351 (lset<= string=? names (all-network-interface-names)))))
7585016f 352
973eea34 353(test-assert "network-interface-flags"
c9bf64d6 354 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
355 (flags (network-interface-flags sock "lo")))
356 (close-port sock)
357 (and (not (zero? (logand flags IFF_LOOPBACK)))
358 (not (zero? (logand flags IFF_UP))))))
359
360(test-equal "loopback-network-interface?"
361 ENODEV
362 (and (loopback-network-interface? "lo")
363 (catch 'system-error
364 (lambda ()
365 (loopback-network-interface? "nonexistent")
366 #f)
367 (lambda args
368 (system-error-errno args)))))
369
0bc6fe32
DM
370(test-equal "loopback-network-interface-running?"
371 ENODEV
372 (and (network-interface-running? "lo")
373 (catch 'system-error
374 (lambda ()
375 (network-interface-running? "nonexistent")
376 #f)
377 (lambda args
378 (system-error-errno args)))))
379
c9bf64d6 380(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 381(test-assert "set-network-interface-flags"
c9bf64d6
LC
382 (let ((sock (socket AF_INET SOCK_STREAM 0)))
383 (catch 'system-error
384 (lambda ()
385 (set-network-interface-flags sock "lo" IFF_UP))
386 (lambda args
387 (close-port sock)
d35c5e29
LC
388 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
389 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
390
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")))
395 (close-port sock)
396 addr))
397
54e515eb 398(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 399(test-assert "set-network-interface-address"
c9bf64d6
LC
400 (let ((sock (socket AF_INET SOCK_STREAM 0)))
401 (catch 'system-error
402 (lambda ()
403 (set-network-interface-address sock "nonexistent"
404 (make-socket-address
405 AF_INET
406 (inet-pton AF_INET "127.12.14.15")
407 0)))
408 (lambda args
409 (close-port sock)
d35c5e29
LC
410 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
411 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 412
67e5f3b7
LC
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")))
417 (close-port sock)
418 addr))
419
420(test-skip (if (zero? (getuid)) 1 0))
421(test-assert "set-network-interface-netmask"
422 (let ((sock (socket AF_INET SOCK_STREAM 0)))
423 (catch 'system-error
424 (lambda ()
425 (set-network-interface-netmask sock "nonexistent"
426 (make-socket-address
427 AF_INET
428 (inet-pton AF_INET "255.0.0.0")
429 0)))
430 (lambda args
431 (close-port sock)
432 (memv (system-error-errno args) (list EPERM EACCES))))))
433
e7f5691d
LC
434(test-equal "network-interfaces returns one or more interfaces"
435 '(#t #t #t)
436 (match (network-interfaces)
437 ((interfaces ..1)
438 (list (every interface? interfaces)
439 (every string? (map interface-name interfaces))
7adbe85e
LC
440 (every (lambda (sockaddr)
441 ;; Sometimes interfaces have no associated address.
442 (or (vector? sockaddr)
443 (not sockaddr)))
444 (map interface-address interfaces))))))
e7f5691d
LC
445
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))
451 ((loopbacks ..1)
452 (list (every (lambda (lo)
453 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
454 loopbacks)
455 (match (find (lambda (lo)
456 (= AF_INET (sockaddr:fam (interface-address lo))))
457 loopbacks)
458 (#f #f)
459 (lo (interface-address lo)))))))
460
9e38e3cf
LC
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")
466 0)))
467 (catch 'system-error
468 (lambda ()
469 (add-network-route/gateway sock gateway))
470 (lambda args
471 (close-port sock)
472 (memv (system-error-errno args) (list EPERM EACCES))))))
473
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)))
478 (catch 'system-error
479 (lambda ()
480 (delete-network-route sock destination))
481 (lambda args
482 (close-port sock)
483 (memv (system-error-errno args) (list EPERM EACCES))))))
484
ae4ff9f3
LC
485(test-equal "tcgetattr ENOTTY"
486 ENOTTY
487 (catch 'system-error
488 (lambda ()
489 (call-with-input-file "/dev/null"
490 (lambda (port)
491 (tcgetattr (fileno port)))))
492 (compose system-error-errno list)))
493
494(test-skip (if (and (file-exists? "/proc/self/fd/0")
495 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
496 0
497 2))
498
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))))
504
505(test-assert "tcsetattr"
506 (let ((first (tcgetattr 0)))
a8f3424b 507 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
ae4ff9f3
LC
508 (equal? first (tcgetattr 0))))
509
5cd25aad 510(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
511 (call-with-input-file "/dev/null"
512 (lambda (port)
513 (catch 'system-error
514 (lambda ()
515 (terminal-window-size port))
516 (lambda args
5cd25aad
LC
517 ;; Accept EINVAL, which some old Linux versions might return.
518 (memv (system-error-errno args)
519 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
520
521(test-assert "terminal-columns"
522 (> (terminal-columns) 0))
523
6d2b4391
LC
524(test-assert "terminal-columns non-file port"
525 (> (terminal-columns (open-input-string "Join us now, share the software!"))
526 0))
527
15030972
LC
528(test-assert "utmpx-entries"
529 (match (utmpx-entries)
530 (((? utmpx? entries) ...)
531 (every (lambda (entry)
532 (match (utmpx-user entry)
533 ((? string?)
4aac8d05
LC
534 ;; Ensure we have a valid PID for those entries where it
535 ;; makes sense.
536 (or (not (memv (utmpx-login-type entry)
537 (list (login-type INIT_PROCESS)
538 (login-type LOGIN_PROCESS)
539 (login-type USER_PROCESS))))
a1a8b7f2 540 (> (utmpx-pid entry) 0)))
15030972
LC
541 (#f ;might be DEAD_PROCESS
542 #t)))
543 entries))))
544
3483f004
LC
545(test-assert "read-utmpx, EOF"
546 (eof-object? (read-utmpx (%make-void-port "r"))))
547
548(unless (access? "/var/run/utmpx" O_RDONLY)
f18eded8 549 (test-skip 1))
3483f004
LC
550(test-assert "read-utmpx"
551 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
552 (or (utmpx? result) (eof-object? result))))
553
29fa45f4 554(test-end)
4e0ea3eb
LC
555
556(false-if-exception (delete-file temp-file))