Landin library reference source

compiler/tests/fixtures/runtime/linux-system-calls/main.ldn

1import hosted/linux
2
3--  hosted/linux's system calls, made directly with no C library
4--  beneath them, on every Linux architecture.  Each failure comes back as
5--  minus its errno.  The paths are NUL-terminated: /dev/zero and /dev/null
6--  exist everywhere; a missing parent gives ENOENT, a path through a
7--  character device ENOTDIR, and a read-only sysctl, which refuses a writer
8--  whoever it is, EACCES.
9zero_name: [10]u8 =
10    [47, 100, 101, 118, 47, 122, 101, 114, 111, 0]
11null_name: [10]u8 =
12    [47, 100, 101, 118, 47, 110, 117, 108, 108, 0]
13null_again: [17]u8 =
14    [47, 100, 101, 118, 47, 46, 46, 47, 100, 101, 118, 47, 110, 117, 108,
15     108, 0]
16missing: [30]u8 =
17    [47, 108, 97, 110, 100, 105, 110, 45, 97, 98, 115, 101, 110, 116, 45,
18     100, 105, 114, 101, 99, 116, 111, 114, 121, 47, 102, 105, 108, 101,
19     0]
20not_directory: [15]u8 =
21    [47, 100, 101, 118, 47, 110, 117, 108, 108, 47, 102, 105, 108, 101, 0]
22sysctl: [27]u8 =
23    [47, 112, 114, 111, 99, 47, 115, 121, 115, 47, 107, 101, 114, 110,
24     101, 108, 47, 111, 115, 114, 101, 108, 101, 97, 115, 101, 0]
25
26--  A character device's file type in a statx mode.
27type_mask: u16 = 0o170000
28character_device: u16 = 0o020000
29
30public main: () -> (code: i32) =
31    code = 1
32    message: []u8 = "system calls\n"
33    sent: isize = linux.write(1, addr message[0], lenof message)
34    return when sent <> isize(lenof message)
35
36    code = 2
37    zero: i32 = linux.openat(linux.at_fdcwd, addr zero_name[0],
38                             linux.o_rdonly, 0)
39    return when zero < 0
40    mut buffer: [8]u8 = [8 of 255]
41    count: isize = linux.read(zero, addr buffer[0], 8)
42    return when count <> 8
43    for at in usize(0)..<usize(8) do
44        return when buffer[at] <> 0
45    end for
46
47    code = 3
48    return when linux.close(zero) <> 0
49    --  EBADF: the descriptor was released.
50    return when linux.close(zero) <> -9
51
52    code = 4
53    return when linux.openat(linux.at_fdcwd, addr missing[0],
54                             linux.o_rdonly, 0) <> -linux.enoent
55    code = 5
56    return when linux.openat(linux.at_fdcwd, addr not_directory[0],
57                             linux.o_rdonly, 0) <> -linux.enotdir
58    code = 6
59    return when linux.openat(linux.at_fdcwd, addr sysctl[0],
60                             linux.o_wronly, 0) <> -linux.eacces
61
62    code = 7
63    mut first: linux.file_status = zeroed
64    return when linux.statx(linux.at_fdcwd, addr null_name[0], 0,
65                            linux.statx_ino, addr first) <> 0
66    return when (first.mask & linux.statx_ino) == 0
67    return when (first.mode & type_mask) <> character_device
68
69    code = 8
70    mut second: linux.file_status = zeroed
71    return when linux.statx(linux.at_fdcwd, addr null_again[0], 0,
72                            linux.statx_ino, addr second) <> 0
73    return when second.inode <> first.inode
74      or second.device_major <> first.device_major
75      or second.device_minor <> first.device_minor
76
77    code = 9
78    mut absent: linux.file_status = zeroed
79    return when linux.statx(linux.at_fdcwd, addr missing[0], 0,
80                            linux.statx_ino, addr absent) <> -linux.enoent
81    code = 42
82end main