gnu: slurm-drmaa: Update to 1.1.1.
[jackhill/guix/guix.git] / tests / syscalls.scm
CommitLineData
29fa45f4 1;;; GNU Guix --- Functional package management for GNU
1deca767 2;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 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
25c7ff6a
LC
63(false-if-exception (delete-file temp-file))
64(test-equal "utime with AT_SYMLINK_NOFOLLOW"
65 '(0 0)
66 (begin
67 ;; Test libguile's utime with AT_SYMLINK_NOFOLLOW, which libguile does not
68 ;; define as of Guile 2.2.4.
69 (symlink "/nowhere" temp-file)
70 (utime temp-file 0 0 0 0 AT_SYMLINK_NOFOLLOW)
71 (let ((st (lstat temp-file)))
72 (delete-file temp-file)
73 ;; Note: 'utimensat' does not change 'ctime'.
74 (list (stat:mtime st) (stat:atime st)))))
75
715fc9d4
LC
76(test-assert "swapon, ENOENT/EPERM"
77 (catch 'system-error
78 (lambda ()
79 (swapon "/does-not-exist")
80 #f)
81 (lambda args
82 (memv (system-error-errno args) (list EPERM ENOENT)))))
83
2793c0fb 84(test-assert "swapoff, ENOENT/EINVAL/EPERM"
715fc9d4
LC
85 (catch 'system-error
86 (lambda ()
87 (swapoff "/does-not-exist")
88 #f)
89 (lambda args
2793c0fb 90 (memv (system-error-errno args) (list EPERM EINVAL ENOENT)))))
715fc9d4 91
b4abdeb6
DT
92(test-assert "mkdtemp!"
93 (let* ((tmp (or (getenv "TMPDIR") "/tmp"))
94 (dir (mkdtemp! (string-append tmp "/guix-test-XXXXXX"))))
95 (and (file-exists? dir)
96 (begin
97 (rmdir dir)
98 #t))))
99
a1f70878
LC
100(test-equal "statfs, ENOENT"
101 ENOENT
102 (catch 'system-error
103 (lambda ()
104 (statfs "/does-not-exist"))
105 (compose system-error-errno list)))
106
107(test-assert "statfs"
108 (let ((fs (statfs "/")))
109 (and (file-system? fs)
110 (> (file-system-block-size fs) 0)
111 (>= (file-system-blocks-available fs) 0)
112 (>= (file-system-blocks-free fs)
113 (file-system-blocks-available fs)))))
114
8950ed11
DT
115(define (user-namespace pid)
116 (string-append "/proc/" (number->string pid) "/ns/user"))
117
b7d48312
DT
118(define perform-container-tests?
119 (and (user-namespace-supported?)
120 (unprivileged-user-namespace-supported?)))
121
122(unless perform-container-tests?
b62a3ebc 123 (test-skip 1))
8950ed11
DT
124(test-assert "clone"
125 (match (clone (logior CLONE_NEWUSER SIGCHLD))
126 (0 (primitive-exit 42))
127 (pid
128 ;; Check if user namespaces are different.
129 (and (not (equal? (readlink (user-namespace pid))
130 (readlink (user-namespace (getpid)))))
131 (match (waitpid pid)
132 ((_ . status)
133 (= 42 (status:exit-val status))))))))
134
b7d48312 135(unless perform-container-tests?
b62a3ebc 136 (test-skip 1))
43ace6ea
DT
137(test-assert "setns"
138 (match (clone (logior CLONE_NEWUSER SIGCHLD))
139 (0 (primitive-exit 0))
140 (clone-pid
141 (match (pipe)
142 ((in . out)
143 (match (primitive-fork)
144 (0
145 (close in)
146 ;; Join the user namespace.
147 (call-with-input-file (user-namespace clone-pid)
148 (lambda (port)
149 (setns (port->fdes port) 0)))
150 (write 'done out)
151 (close out)
152 (primitive-exit 0))
153 (fork-pid
154 (close out)
155 ;; Wait for the child process to join the namespace.
156 (read in)
157 (let ((result (and (equal? (readlink (user-namespace clone-pid))
158 (readlink (user-namespace fork-pid))))))
159 ;; Clean up.
160 (waitpid clone-pid)
161 (waitpid fork-pid)
162 result))))))))
8950ed11 163
1deca767 164(when (not perform-container-tests?)
b62a3ebc 165 (test-skip 1))
fe9bdb58 166(test-equal "pivot-root"
1deca767
LC
167 'success!
168 (match (socketpair AF_UNIX SOCK_STREAM 0)
169 ((parent . child)
df3ce5c1
DT
170 (match (clone (logior CLONE_NEWUSER CLONE_NEWNS SIGCHLD))
171 (0
fe9bdb58
LC
172 (dynamic-wind
173 (const #t)
174 (lambda ()
1deca767 175 (close parent)
fe9bdb58
LC
176 (call-with-temporary-directory
177 (lambda (root)
1deca767
LC
178 (display "ready\n" child)
179 (read child) ;wait for "go!"
fe9bdb58
LC
180 (let ((put-old (string-append root "/real-root")))
181 (mount "none" root "tmpfs")
182 (mkdir put-old)
183 (call-with-output-file (string-append root "/test")
184 (lambda (port)
185 (display "testing\n" port)))
186 (pivot-root root put-old)
187 ;; The test file should now be located inside the root directory.
1deca767
LC
188 (write (and (file-exists? "/test") 'success!) child)
189 (close child)))))
fe9bdb58
LC
190 (lambda ()
191 (primitive-exit 0))))
df3ce5c1 192 (pid
1deca767
LC
193 (close child)
194 (match (read parent)
195 ('ready
196 ;; Set up the UID/GID mapping so that we can mkdir on the tmpfs:
197 ;; <https://bugzilla.kernel.org/show_bug.cgi?id=183461>.
198 (call-with-output-file (format #f "/proc/~d/setgroups" pid)
199 (lambda (port)
200 (display "deny" port)))
201 (call-with-output-file (format #f "/proc/~d/uid_map" pid)
202 (lambda (port)
203 (format port "0 ~d 1" (getuid))))
204 (call-with-output-file (format #f "/proc/~d/gid_map" pid)
205 (lambda (port)
206 (format port "0 ~d 1" (getgid))))
207 (display "go!\n" parent)
208 (let ((result (read parent)))
209 (close parent)
210 (and (zero? (match (waitpid pid)
211 ((_ . status)
212 (status:exit-val status))))
213 result)))))))))
df3ce5c1 214
fa73c193
LC
215(test-equal "scandir*, ENOENT"
216 ENOENT
217 (catch 'system-error
218 (lambda ()
219 (scandir* "/does/not/exist"))
220 (lambda args
221 (system-error-errno args))))
222
223(test-equal "scandir*, ASCII file names"
224 (scandir (dirname (search-path %load-path "guix/base32.scm"))
225 (const #t) string<?)
226 (match (scandir* (dirname (search-path %load-path "guix/base32.scm")))
227 (((names . properties) ...)
228 names)))
229
230(test-equal "scandir*, UTF-8 file names"
231 '("." ".." "α" "λ")
232 (call-with-temporary-directory
233 (lambda (directory)
234 ;; Wrap 'creat' to make sure that we really pass a UTF-8-encoded file
235 ;; name to the system call.
236 (let ((creat (pointer->procedure int
237 (dynamic-func "creat" (dynamic-link))
238 (list '* int))))
239 (creat (string->pointer (string-append directory "/α")
240 "UTF-8")
241 #o644)
242 (creat (string->pointer (string-append directory "/λ")
243 "UTF-8")
244 #o644)
245 (let ((locale (setlocale LC_ALL)))
246 (dynamic-wind
247 (lambda ()
248 ;; Make sure that even in a C locale we get the right result.
249 (setlocale LC_ALL "C"))
250 (lambda ()
251 (match (scandir* directory)
252 (((names . properties) ...)
253 names)))
254 (lambda ()
255 (setlocale LC_ALL locale))))))))
256
257(test-assert "scandir*, properties"
258 (let ((directory (dirname (search-path %load-path "guix/base32.scm"))))
259 (every (lambda (entry name)
260 (match entry
261 ((name2 . properties)
262 (and (string=? name2 name)
263 (let* ((full (string-append directory "/" name))
264 (stat (lstat full))
265 (inode (assoc-ref properties 'inode))
266 (type (assoc-ref properties 'type)))
267 (and (= inode (stat:ino stat))
268 (or (eq? type 'unknown)
269 (eq? type (stat:type stat)))))))))
270 (scandir* directory)
271 (scandir directory (const #t) string<?))))
272
df058423
JN
273(false-if-exception (delete-file temp-file))
274(test-assert "getxattr, setxattr"
275 (let ((key "user.translator")
276 (value "/hurd/pfinet\0")
277 (file (open-file temp-file "w0")))
278 (setxattr temp-file key value)
279 (string=? (getxattr temp-file key) value)))
280
4e0ea3eb
LC
281(false-if-exception (delete-file temp-file))
282(test-equal "fcntl-flock wait"
283 42 ; the child's exit status
284 (let ((file (open-file temp-file "w0b")))
285 ;; Acquire an exclusive lock.
286 (fcntl-flock file 'write-lock)
287 (match (primitive-fork)
288 (0
289 (dynamic-wind
290 (const #t)
291 (lambda ()
292 ;; Reopen FILE read-only so we can have a read lock.
293 (let ((file (open-file temp-file "r0b")))
294 ;; Wait until we can acquire the lock.
295 (fcntl-flock file 'read-lock)
296 (primitive-exit (read file)))
297 (primitive-exit 1))
298 (lambda ()
299 (primitive-exit 2))))
300 (pid
301 ;; Write garbage and wait.
302 (display "hello, world!" file)
303 (force-output file)
304 (sleep 1)
305
306 ;; Write the real answer.
307 (seek file 0 SEEK_SET)
308 (truncate-file file 0)
309 (write 42 file)
310 (force-output file)
311
312 ;; Unlock, which should let the child continue.
313 (fcntl-flock file 'unlock)
314
315 (match (waitpid pid)
316 ((_ . status)
317 (let ((result (status:exit-val status)))
318 (close-port file)
319 result)))))))
320
321(test-equal "fcntl-flock non-blocking"
322 EAGAIN ; the child's exit status
323 (match (pipe)
324 ((input . output)
325 (match (primitive-fork)
326 (0
327 (dynamic-wind
328 (const #t)
329 (lambda ()
330 (close-port output)
331
332 ;; Wait for the green light.
333 (read-char input)
334
335 ;; Open FILE read-only so we can have a read lock.
336 (let ((file (open-file temp-file "w0")))
337 (catch 'flock-error
338 (lambda ()
339 ;; This attempt should throw EAGAIN.
340 (fcntl-flock file 'write-lock #:wait? #f))
341 (lambda (key errno)
342 (primitive-exit (pk 'errno errno)))))
343 (primitive-exit -1))
344 (lambda ()
345 (primitive-exit -2))))
346 (pid
347 (close-port input)
348 (let ((file (open-file temp-file "w0")))
349 ;; Acquire an exclusive lock.
350 (fcntl-flock file 'write-lock)
351
352 ;; Tell the child to continue.
353 (write 'green-light output)
354 (force-output output)
355
356 (match (waitpid pid)
357 ((_ . status)
358 (let ((result (status:exit-val status)))
359 (fcntl-flock file 'unlock)
360 (close-port file)
361 result)))))))))
362
aa401f9b
LC
363(test-equal "set-thread-name"
364 "Syscall Test"
365 (let ((name (thread-name)))
366 (set-thread-name "Syscall Test")
367 (let ((new-name (thread-name)))
368 (set-thread-name name)
369 new-name)))
370
b89e7405
LC
371(test-assert "all-network-interface-names"
372 (match (all-network-interface-names)
4d54785c
LC
373 (((? string? names) ..1)
374 (member "lo" names))))
375
b89e7405
LC
376(test-assert "network-interface-names"
377 (match (network-interface-names)
7585016f 378 (((? string? names) ..1)
b89e7405 379 (lset<= string=? names (all-network-interface-names)))))
7585016f 380
973eea34 381(test-assert "network-interface-flags"
c9bf64d6 382 (let* ((sock (socket AF_INET SOCK_STREAM 0))
973eea34
LC
383 (flags (network-interface-flags sock "lo")))
384 (close-port sock)
385 (and (not (zero? (logand flags IFF_LOOPBACK)))
386 (not (zero? (logand flags IFF_UP))))))
387
388(test-equal "loopback-network-interface?"
389 ENODEV
390 (and (loopback-network-interface? "lo")
391 (catch 'system-error
392 (lambda ()
393 (loopback-network-interface? "nonexistent")
394 #f)
395 (lambda args
396 (system-error-errno args)))))
397
0bc6fe32
DM
398(test-equal "loopback-network-interface-running?"
399 ENODEV
400 (and (network-interface-running? "lo")
401 (catch 'system-error
402 (lambda ()
403 (network-interface-running? "nonexistent")
404 #f)
405 (lambda args
406 (system-error-errno args)))))
407
c9bf64d6 408(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 409(test-assert "set-network-interface-flags"
c9bf64d6
LC
410 (let ((sock (socket AF_INET SOCK_STREAM 0)))
411 (catch 'system-error
412 (lambda ()
413 (set-network-interface-flags sock "lo" IFF_UP))
414 (lambda args
415 (close-port sock)
d35c5e29
LC
416 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
417 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6
LC
418
419(test-equal "network-interface-address lo"
420 (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)
421 (let* ((sock (socket AF_INET SOCK_STREAM 0))
422 (addr (network-interface-address sock "lo")))
423 (close-port sock)
424 addr))
425
54e515eb 426(test-skip (if (zero? (getuid)) 1 0))
d35c5e29 427(test-assert "set-network-interface-address"
c9bf64d6
LC
428 (let ((sock (socket AF_INET SOCK_STREAM 0)))
429 (catch 'system-error
430 (lambda ()
431 (set-network-interface-address sock "nonexistent"
432 (make-socket-address
433 AF_INET
434 (inet-pton AF_INET "127.12.14.15")
435 0)))
436 (lambda args
437 (close-port sock)
d35c5e29
LC
438 ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32.
439 (memv (system-error-errno args) (list EPERM EACCES))))))
c9bf64d6 440
67e5f3b7
LC
441(test-equal "network-interface-netmask lo"
442 (make-socket-address AF_INET (inet-pton AF_INET "255.0.0.0") 0)
443 (let* ((sock (socket AF_INET SOCK_STREAM 0))
444 (addr (network-interface-netmask sock "lo")))
445 (close-port sock)
446 addr))
447
448(test-skip (if (zero? (getuid)) 1 0))
449(test-assert "set-network-interface-netmask"
450 (let ((sock (socket AF_INET SOCK_STREAM 0)))
451 (catch 'system-error
452 (lambda ()
453 (set-network-interface-netmask sock "nonexistent"
454 (make-socket-address
455 AF_INET
456 (inet-pton AF_INET "255.0.0.0")
457 0)))
458 (lambda args
459 (close-port sock)
460 (memv (system-error-errno args) (list EPERM EACCES))))))
461
e7f5691d
LC
462(test-equal "network-interfaces returns one or more interfaces"
463 '(#t #t #t)
464 (match (network-interfaces)
465 ((interfaces ..1)
466 (list (every interface? interfaces)
467 (every string? (map interface-name interfaces))
7adbe85e
LC
468 (every (lambda (sockaddr)
469 ;; Sometimes interfaces have no associated address.
470 (or (vector? sockaddr)
471 (not sockaddr)))
472 (map interface-address interfaces))))))
e7f5691d
LC
473
474(test-equal "network-interfaces returns \"lo\""
475 (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0))
476 (match (filter (lambda (interface)
477 (string=? "lo" (interface-name interface)))
478 (network-interfaces))
479 ((loopbacks ..1)
480 (list (every (lambda (lo)
481 (not (zero? (logand IFF_LOOPBACK (interface-flags lo)))))
482 loopbacks)
483 (match (find (lambda (lo)
484 (= AF_INET (sockaddr:fam (interface-address lo))))
485 loopbacks)
486 (#f #f)
487 (lo (interface-address lo)))))))
488
9e38e3cf
LC
489(test-skip (if (zero? (getuid)) 1 0))
490(test-assert "add-network-route/gateway"
491 (let ((sock (socket AF_INET SOCK_STREAM 0))
492 (gateway (make-socket-address AF_INET
493 (inet-pton AF_INET "192.168.0.1")
494 0)))
495 (catch 'system-error
496 (lambda ()
497 (add-network-route/gateway sock gateway))
498 (lambda args
499 (close-port sock)
500 (memv (system-error-errno args) (list EPERM EACCES))))))
501
502(test-skip (if (zero? (getuid)) 1 0))
503(test-assert "delete-network-route"
504 (let ((sock (socket AF_INET SOCK_STREAM 0))
505 (destination (make-socket-address AF_INET INADDR_ANY 0)))
506 (catch 'system-error
507 (lambda ()
508 (delete-network-route sock destination))
509 (lambda args
510 (close-port sock)
511 (memv (system-error-errno args) (list EPERM EACCES))))))
512
ae4ff9f3
LC
513(test-equal "tcgetattr ENOTTY"
514 ENOTTY
515 (catch 'system-error
516 (lambda ()
517 (call-with-input-file "/dev/null"
518 (lambda (port)
519 (tcgetattr (fileno port)))))
520 (compose system-error-errno list)))
521
522(test-skip (if (and (file-exists? "/proc/self/fd/0")
523 (string-prefix? "/dev/pts/" (readlink "/proc/self/fd/0")))
524 0
525 2))
526
527(test-assert "tcgetattr"
528 (let ((termios (tcgetattr 0)))
529 (and (termios? termios)
530 (> (termios-input-speed termios) 0)
531 (> (termios-output-speed termios) 0))))
532
533(test-assert "tcsetattr"
534 (let ((first (tcgetattr 0)))
a8f3424b 535 (tcsetattr 0 (tcsetattr-action TCSANOW) first)
ae4ff9f3
LC
536 (equal? first (tcgetattr 0))))
537
5cd25aad 538(test-assert "terminal-window-size ENOTTY"
29ff6d9f
LC
539 (call-with-input-file "/dev/null"
540 (lambda (port)
541 (catch 'system-error
542 (lambda ()
543 (terminal-window-size port))
544 (lambda args
5cd25aad
LC
545 ;; Accept EINVAL, which some old Linux versions might return.
546 (memv (system-error-errno args)
547 (list ENOTTY EINVAL)))))))
29ff6d9f
LC
548
549(test-assert "terminal-columns"
550 (> (terminal-columns) 0))
551
6d2b4391
LC
552(test-assert "terminal-columns non-file port"
553 (> (terminal-columns (open-input-string "Join us now, share the software!"))
554 0))
555
4593f5a6
LC
556(test-assert "terminal-rows"
557 (> (terminal-rows) 0))
558
15030972
LC
559(test-assert "utmpx-entries"
560 (match (utmpx-entries)
561 (((? utmpx? entries) ...)
562 (every (lambda (entry)
563 (match (utmpx-user entry)
564 ((? string?)
4aac8d05
LC
565 ;; Ensure we have a valid PID for those entries where it
566 ;; makes sense.
567 (or (not (memv (utmpx-login-type entry)
568 (list (login-type INIT_PROCESS)
569 (login-type LOGIN_PROCESS)
570 (login-type USER_PROCESS))))
a1a8b7f2 571 (> (utmpx-pid entry) 0)))
15030972
LC
572 (#f ;might be DEAD_PROCESS
573 #t)))
574 entries))))
575
3483f004
LC
576(test-assert "read-utmpx, EOF"
577 (eof-object? (read-utmpx (%make-void-port "r"))))
578
579(unless (access? "/var/run/utmpx" O_RDONLY)
f18eded8 580 (test-skip 1))
3483f004
LC
581(test-assert "read-utmpx"
582 (let ((result (call-with-input-file "/var/run/utmpx" read-utmpx)))
583 (or (utmpx? result) (eof-object? result))))
584
5e5f7167
LC
585(when (zero? (getuid))
586 (test-skip 1))
587(test-equal "add-to-entropy-count"
588 EPERM
589 (call-with-output-file "/dev/urandom"
590 (lambda (port)
591 (catch 'system-error
592 (lambda ()
593 (add-to-entropy-count port 77)
594 #f)
595 (lambda args
596 (system-error-errno args))))))
597
29fa45f4 598(test-end)
4e0ea3eb
LC
599
600(false-if-exception (delete-file temp-file))