gnu: tomb: Update phase style.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
15030972 2;;; Copyright © 2014, 2015, 2016, 2017 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?)
154 (version>? (utsname:release (uname)) "4.7.5"))
b62a3ebc 155 (test-skip 1))
fe9bdb58
LC
156(test-equal "pivot-root"
157 #t
df3ce5c1
DT
158 (match (pipe)
159 ((in . out)
160 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
161 (0
fe9bdb58
LC
162 (dynamic-wind
163 (const #t)
164 (lambda ()
165 (close in)
166 (call-with-temporary-directory
167 (lambda (root)
168 (let ((put-old (string-append root "/real-root")))
169 (mount "none" root "tmpfs")
170 (mkdir put-old)
171 (call-with-output-file (string-append root "/test")
172 (lambda (port)
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)
177 (close out)))))
178 (lambda ()
179 (primitive-exit 0))))
df3ce5c1
DT
180 (pid
181 (close out)
182 (let ((result (read in)))
183 (close in)
184 (and (zero? (match (waitpid pid)
185 ((_ . status)
186 (status:exit-val status))))
187 (eq? #t result))))))))
188
fa73c193
LC
189(test-equal "scandir*, ENOENT"
190 ENOENT
191 (catch 'system-error
192 (lambda ()
193 (scandir* "/does/not/exist"))
194 (lambda args
195 (system-error-errno args))))
196
197(test-equal "scandir*, ASCII file names"
198 (scandir (dirname (search-path %load-path "guix/base32.scm"))
199 (const #t) string<?)
200 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
201 (((names . properties) ...)
202 names)))
203
204(test-equal "scandir*, UTF-8 file names"
205 '("." ".." "α" "λ")
206 (call-with-temporary-directory
207 (lambda (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))
212 (list '* int))))
213 (creat (string->pointer (string-append directory "/α")
214 "UTF-8")
215 #o644)
216 (creat (string->pointer (string-append directory "/λ")
217 "UTF-8")
218 #o644)
219 (let ((locale (setlocale LC_ALL)))
220 (dynamic-wind
221 (lambda ()
222 ;; Make sure that even in a C locale we get the right result.
223 (setlocale LC_ALL "C"))
224 (lambda ()
225 (match (scandir* directory)
226 (((names . properties) ...)
227 names)))
228 (lambda ()
229 (setlocale LC_ALL locale))))))))
230
231(test-assert "scandir*, properties"
232 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
233 (every (lambda (entry name)
234 (match entry
235 ((name2 . properties)
236 (and (string=? name2 name)
237 (let* ((full (string-append directory "/" name))
238 (stat (lstat full))
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)))))))))
244 (scandir* directory)
245 (scandir directory (const #t) string<?))))
246
4e0ea3eb
LC
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)
254 (0
255 (dynamic-wind
256 (const #t)
257 (lambda ()
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)))
263 (primitive-exit 1))
264 (lambda ()
265 (primitive-exit 2))))
266 (pid
267 ;; Write garbage and wait.
268 (display "hello, world!" file)
269 (force-output file)
270 (sleep 1)
271
272 ;; Write the real answer.
273 (seek file 0 SEEK_SET)
274 (truncate-file file 0)
275 (write 42 file)
276 (force-output file)
277
278 ;; Unlock, which should let the child continue.
279 (fcntl-flock file 'unlock)
280
281 (match (waitpid pid)
282 ((_ . status)
283 (let ((result (status:exit-val status)))
284 (close-port file)
285 result)))))))
286
287(test-equal "fcntl-flock non-blocking"
288 EAGAIN ; the child's exit status
289 (match (pipe)
290 ((input . output)
291 (match (primitive-fork)
292 (0
293 (dynamic-wind
294 (const #t)
295 (lambda ()
296 (close-port output)
297
298 ;; Wait for the green light.
299 (read-char input)
300
301 ;; Open FILE read-only so we can have a read lock.
302 (let ((file (open-file temp-file "w0")))
303 (catch 'flock-error
304 (lambda ()
305 ;; This attempt should throw EAGAIN.
306 (fcntl-flock file 'write-lock #:wait? #f))
307 (lambda (key errno)
308 (primitive-exit (pk 'errno errno)))))
309 (primitive-exit -1))
310 (lambda ()
311 (primitive-exit -2))))
312 (pid
313 (close-port input)
314 (let ((file (open-file temp-file "w0")))
315 ;; Acquire an exclusive lock.
316 (fcntl-flock file 'write-lock)
317
318 ;; Tell the child to continue.
319 (write 'green-light output)
320 (force-output output)
321
322 (match (waitpid pid)
323 ((_ . status)
324 (let ((result (status:exit-val status)))
325 (fcntl-flock file 'unlock)
326 (close-port file)
327 result)))))))))
328
aa401f9b
LC
329(test-equal "set-thread-name"
330 "Syscall Test"
331 (let ((name (thread-name)))
332 (set-thread-name "Syscall Test")
333 (let ((new-name (thread-name)))
334 (set-thread-name name)
335 new-name)))
336
b89e7405
LC
337(test-assert "all-network-interface-names"
338 (match (all-network-interface-names)
4d54785c
LC
339 (((? string? names) ..1)
340 (member "lo" names))))
341
b89e7405
LC
342(test-assert "network-interface-names"
343 (match (network-interface-names)
7585016f 344 (((? string? names) ..1)
b89e7405 345 (lset<= string=? names (all-network-interface-names)))))
7585016f 346
973eea34 347(test-assert "network-interface-flags"
c9bf64d6 348 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
349 (flags (network-interface-flags sock "lo")))
350 (close-port sock)
351 (and (not (zero? (logand flags IFF_LOOPBACK)))
352 (not (zero? (logand flags IFF_UP))))))
353
354(test-equal "loopback-network-interface?"
355 ENODEV
356 (and (loopback-network-interface? "lo")
357 (catch 'system-error
358 (lambda ()
359 (loopback-network-interface? "nonexistent")
360 #f)
361 (lambda args
362 (system-error-errno args)))))
363
0bc6fe32
DM
364(test-equal "loopback-network-interface-running?"
365 ENODEV
366 (and (network-interface-running? "lo")
367 (catch 'system-error
368 (lambda ()
369 (network-interface-running? "nonexistent")
370 #f)
371 (lambda args
372 (system-error-errno args)))))
373
c9bf64d6 374(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 375(test-assert "set-network-interface-flags"
c9bf64d6
LC
376 (let ((sock (socket AF_INET SOCK_STREAM 0)))
377 (catch 'system-error
378 (lambda ()
379 (set-network-interface-flags sock "lo" IFF_UP))
380 (lambda args
381 (close-port sock)
d35c5e29
LC
382 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
383 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
384
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")))
389 (close-port sock)
390 addr))
391
54e515eb 392(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 393(test-assert "set-network-interface-address"
c9bf64d6
LC
394 (let ((sock (socket AF_INET SOCK_STREAM 0)))
395 (catch 'system-error
396 (lambda ()
397 (set-network-interface-address sock "nonexistent"
398 (make-socket-address
399 AF_INET
400 (inet-pton AF_INET "127.12.14.15")
401 0)))
402 (lambda args
403 (close-port sock)
d35c5e29
LC
404 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
405 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 406
67e5f3b7
LC
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")))
411 (close-port sock)
412 addr))
413
414(test-skip (if (zero? (getuid)) 1 0))
415(test-assert "set-network-interface-netmask"
416 (let ((sock (socket AF_INET SOCK_STREAM 0)))
417 (catch 'system-error
418 (lambda ()
419 (set-network-interface-netmask sock "nonexistent"
420 (make-socket-address
421 AF_INET
422 (inet-pton AF_INET "255.0.0.0")
423 0)))
424 (lambda args
425 (close-port sock)
426 (memv (system-error-errno args) (list EPERM EACCES))))))
427
e7f5691d
LC
428(test-equal "network-interfaces returns one or more interfaces"
429 '(#t #t #t)
430 (match (network-interfaces)
431 ((interfaces ..1)
432 (list (every interface? interfaces)
433 (every string? (map interface-name interfaces))
7adbe85e
LC
434 (every (lambda (sockaddr)
435 ;; Sometimes interfaces have no associated address.
436 (or (vector? sockaddr)
437 (not sockaddr)))
438 (map interface-address interfaces))))))
e7f5691d
LC
439
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))
445 ((loopbacks ..1)
446 (list (every (lambda (lo)
447 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
448 loopbacks)
449 (match (find (lambda (lo)
450 (= AF_INET (sockaddr:fam (interface-address lo))))
451 loopbacks)
452 (#f #f)
453 (lo (interface-address lo)))))))
454
9e38e3cf
LC
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")
460 0)))
461 (catch 'system-error
462 (lambda ()
463 (add-network-route/gateway sock gateway))
464 (lambda args
465 (close-port sock)
466 (memv (system-error-errno args) (list EPERM EACCES))))))
467
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)))
472 (catch 'system-error
473 (lambda ()
474 (delete-network-route sock destination))
475 (lambda args
476 (close-port sock)
477 (memv (system-error-errno args) (list EPERM EACCES))))))
478
ae4ff9f3
LC
479(test-equal "tcgetattr ENOTTY"
480 ENOTTY
481 (catch 'system-error
482 (lambda ()
483 (call-with-input-file "/dev/null"
484 (lambda (port)
485 (tcgetattr (fileno port)))))
486 (compose system-error-errno list)))
487
488(test-skip (if (and (file-exists? "/proc/self/fd/0")
489 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
490 0
491 2))
492
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))))
498
499(test-assert "tcsetattr"
500 (let ((first (tcgetattr 0)))
a8f3424b 501 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
ae4ff9f3
LC
502 (equal? first (tcgetattr 0))))
503
5cd25aad 504(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
505 (call-with-input-file "/dev/null"
506 (lambda (port)
507 (catch 'system-error
508 (lambda ()
509 (terminal-window-size port))
510 (lambda args
5cd25aad
LC
511 ;; Accept EINVAL, which some old Linux versions might return.
512 (memv (system-error-errno args)
513 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
514
515(test-assert "terminal-columns"
516 (> (terminal-columns) 0))
517
6d2b4391
LC
518(test-assert "terminal-columns non-file port"
519 (> (terminal-columns (open-input-string "Join us now, share the software!"))
520 0))
521
15030972
LC
522(test-assert "utmpx-entries"
523 (match (utmpx-entries)
524 (((? utmpx? entries) ...)
525 (every (lambda (entry)
526 (match (utmpx-user entry)
527 ((? string?)
4aac8d05
LC
528 ;; Ensure we have a valid PID for those entries where it
529 ;; makes sense.
530 (or (not (memv (utmpx-login-type entry)
531 (list (login-type INIT_PROCESS)
532 (login-type LOGIN_PROCESS)
533 (login-type USER_PROCESS))))
a1a8b7f2 534 (> (utmpx-pid entry) 0)))
15030972
LC
535 (#f ;might be DEAD_PROCESS
536 #t)))
537 entries))))
538
3483f004
LC
539(test-assert "read-utmpx, EOF"
540 (eof-object? (read-utmpx (%make-void-port "r"))))
541
542(unless (access? "/var/run/utmpx" O_RDONLY)
f18eded8 543 (test-skip 1))
3483f004
LC
544(test-assert "read-utmpx"
545 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
546 (or (utmpx? result) (eof-object? result))))
547
29fa45f4 548(test-end)
4e0ea3eb
LC
549
550(false-if-exception (delete-file temp-file))