Landin library reference discuss source

platform/libc/linux.ldn

1--  platform/libc on Linux: glibc's and musl's numbers and records, which
2--  only a program linked with --link=libc calls, and the declarations
3--  Linux has that the other systems name otherwise.  Darwin's and
4--  FreeBSD's own are in darwin.ldn and freebsd.ldn; what every system
5--  declares alike is in libc.ldn.
6
7fixed if compiler.os == linux then
8    --- The calling thread's errno cell, `__errno_location`.  Read it immediately after
9    --- the failing call, before any other call that may set it.
10    public extern(c) link(symbol: "__errno_location") errno_cell:
11        () -> (cell: ptr mut i32)
12
13    --- `open` flag: create the file if it does not exist.  Linux keeps one
14    --- value on every architecture; Darwin and FreeBSD share the BSD one.
15    public o_creat: i32 = 64
16    --- `open` flag: truncate an existing regular file to length zero.
17    public o_trunc: i32 = 512
18    --- `open` flag: with `o_creat`, fail with EEXIST if the name is taken.
19    public o_excl: i32 = 128
20    --- `open` flag: every write goes to the end of the file.
21    public o_append: i32 = 1024
22    --- `mmap` flag: the mapping is backed by no file and starts zeroed.
23    public map_anonymous: i32 = 32
24    --- `poll`'s count of records, C's `nfds_t`.
25    public poll_count: type = u64
26
27    --- `clock_gettime` clock: time since a point the system fixes, never
28    --- set back.  Each system numbers it its own way.
29    public clock_monotonic: i32 = 1
30
31    --  Full records rather than guessed opaque storage, each selected by its
32    --  system and C ABI by name, so a system or an ABI none names has no
33    --  `file_status` rather than another's.  Darwin arm64 uses the ordinary
34    --  stat symbol with its 64-bit inode ABI.
35
36    fixed if compiler.c_sysv_lp64 then
37        --- `sizeof (struct stat)` in the native C headers.
38        public file_status_size: usize = 144
39        --- C's `struct stat`, as `stat` fills it on this system.
40        public file_status: type = struct
41            device: u64
42            inode: u64
43            links: u64
44            mode: u32
45            uid: u32
46            gid: u32
47            padding: i32
48            special_device: u64
49            bytes: i64
50            block_size: i64
51            blocks: i64
52            accessed: timespec
53            modified: timespec
54            changed: timespec
55            reserved: [3]i64
56        end file_status
57    elsif (compiler.c_aapcs64_lp64 or compiler.c_riscv_lp64d) then
58        --- `sizeof (struct stat)` in the native C headers.
59        public file_status_size: usize = 128
60        --- C's `struct stat`, as `stat` fills it on this system.
61        public file_status: type = struct
62            device: u64
63            inode: u64
64            mode: u32
65            links: u32
66            uid: u32
67            gid: u32
68            special_device: u64
69            padding1: u64
70            bytes: i64
71            block_size: i32
72            padding2: i32
73            blocks: i64
74            accessed: timespec
75            modified: timespec
76            changed: timespec
77            reserved: [2]u32
78        end file_status
79    end if
80
81    fixed if compiler.arch == arm64 then
82        --- `open` flag: fail with ENOTDIR unless the path names a
83        --- directory.  Linux's arm64 keeps a value of its own.
84        public o_directory: i32 = 16384
85    else
86        --- `open` flag: fail with ENOTDIR unless the path names a
87        --- directory.  Linux's arm64 keeps a value of its own.
88        public o_directory: i32 = 65536
89    end if
90    --- `fstatat` flag: report a symbolic link itself.
91    public at_symlink_nofollow: i32 = 256
92
93    --- C's `struct dirent` as `readdir` returns it.  glibc gives no name
94    --- length: the name ends at its NUL.
95    public directory_record: type = struct
96        inode: u64
97        offset: i64
98        record_length: u16
99        kind: u8
100        name: [256]u8
101    end directory_record
102
103    --- The directory descriptor that makes an `*at` call's relative path
104    --- relative to the working directory.
105    public at_fdcwd: i32 = -100
106    --- `unlinkat` flag: remove an empty directory rather than a file.
107    public at_removedir: i32 = 512
108
109    --- Make the directory `path`, relative to `directory` (or `at_fdcwd`),
110    --- with the permission bits `mode`, which the process umask filters:
111    --- 0, or -1 with the reason in errno.
112    public extern(c) mkdirat: (directory: i32, path: ptr u8, mode: u32)
113                              -> (status: i32)
114
115    --- `open` and `pipe2` flag: the new descriptor is closed when the
116    --- process executes a program, set with it.
117    public o_cloexec: i32 = 524288
118    --- `fcntl` command: duplicate a descriptor onto the lowest free number
119    --- at or above the argument, closed on exec.
120    public f_dupfd_cloexec: i32 = 1030
121
122    --- `pipe` with `flags`, `o_cloexec` among them, set as the pipe is
123    --- made: 0, or -1 with the reason in errno.  Darwin has none.
124    public extern(c) pipe2: (ends: ptr mut i32, flags: i32) -> (status: i32)
125
126    --- `socket` domain: IPv6, AF_INET6, which each system numbers its own
127    --- way.
128    public af_inet6: i32 = 10
129    --- `setsockopt` level: the socket itself, SOL_SOCKET.
130    public sol_socket: i32 = 1
131    --- `sol_socket` option, an `i32`: a listener may bind an address that
132    --- earlier connections still hold in TIME_WAIT.
133    public so_reuseaddr: i32 = 2
134    --- `sol_socket` option, read as an `i32`: the pending error, cleared as
135    --- it is read.
136    public so_error: i32 = 4
137    --- `sol_socket` option, a `time_limit`: how long a receive waits.
138    public so_rcvtimeo: i32 = 20
139    --- `sol_socket` option, a `time_limit`: how long a send waits.
140    public so_sndtimeo: i32 = 21
141    --- `ipproto_ipv6` option, an `i32`: an IPv6 socket takes IPv6 alone.
142    public ipv6_v6only: i32 = 26
143    --- `socket`, `socketpair` and `accept4` flag: closed on exec, set with
144    --- the descriptor.  Darwin has none.
145    public sock_cloexec: i32 = 524288
146    --- `send` flag: a send to a peer that has gone fails with EPIPE and
147    --- raises no SIGPIPE.  Darwin has none.
148    public msg_nosignal: i32 = 16384
149    --- `sizeof (struct sockaddr_un)`'s path: the longest Unix-domain path
150    --- with its NUL.
151    public sockaddr_un_path: u32 = 108
152
153    --- C's `struct timeval`, which a socket time limit is.
154    public time_limit: type = struct
155        seconds: i64
156        microseconds: i64
157    end time_limit
158
159    --- glibc's `sigset_t`, 1024 bits.
160    public signal_set: type = struct
161        bits: [16]u64
162    end signal_set
163
164    --- C's `struct sigaction` as glibc declares it.
165    public signal_action: type = struct
166        handler: u64
167        mask: signal_set
168        flags: i32
169        padding: i32
170        restorer: u64
171    end signal_action
172
173    --- `accept` with `flags`, `sock_cloexec` among them, set as the socket
174    --- is made: Linux's and FreeBSD's; Darwin has none.
175    public extern(c) accept4: (descriptor: i32, address: address_filled,
176                               length: address_length, flags: i32)
177                              -> (accepted: i32)
178
179    --- Set the permission bits of the file `path` names, relative to
180    --- `directory`, to `mode`, following a final symbolic link with
181    --- `flags` zero: 0, or -1 with the reason in errno.
182    public extern(c) fchmodat: (directory: i32, path: ptr u8, mode: u32,
183                                flags: i32) -> (status: i32)
184    --- Set the permission bits of the file open as `descriptor`: 0, or -1
185    --- with the reason in errno.
186    public extern(c) fchmod: (descriptor: i32, mode: u32) -> (status: i32)
187    --- Hand the file's data to its device, and its status only where
188    --- reading the data back needs it: 0, or -1 with the reason in errno.
189    --- Darwin declares none.
190    public extern(c) fdatasync: (descriptor: i32) -> (status: i32)
191
192    --- `linkat` flag: follow a final symbolic link of the old path.
193    public at_symlink_follow: i32 = 1024
194    --- `open` flag: a terminal opened does not become the controlling one.
195    public o_noctty: i32 = 256
196    --- `fcntl` command: describe a record lock in the way, `F_GETLK`.
197    public f_getlk: i32 = 5
198    --- `fcntl` command: take or give up a record lock at once, `F_SETLK`.
199    public f_setlk: i32 = 6
200    --- `fcntl` command: take a record lock, waiting for it, `F_SETLKW`.
201    public f_setlkw: i32 = 7
202    --- A record lock's type: shared.
203    public f_rdlck: i16 = 0
204    --- A record lock's type: exclusive.
205    public f_wrlck: i16 = 1
206    --- A record lock's type: none.
207    public f_unlck: i16 = 2
208
209    --- C's `struct flock`, a record lock: its type, where its start counts
210    --- from, its start and length, zero to the end of the file however far
211    --- it reaches, and the process holding it, which `f_getlk` fills.
212    public lock_record: type = struct
213        kind: i16
214        whence: i16
215        padding: i32
216        start: i64
217        length: i64
218        process: i32
219        spare: i32
220    end lock_record
221
222    --- glibc's `struct passwd`: the account's name, password, user and
223    --- group IDs, comment, home directory and shell, the strings pointers
224    --- into the buffer the call was given.
225    public account_record: type = struct
226        name: usize
227        password: usize
228        user: u32
229        group: u32
230        comment: usize
231        home: usize
232        shell: usize
233    end account_record
234
235    --- `dlopen` mode: the object's symbols resolve no other object's
236    --- references.  Darwin's default is `rtld_global`, so it is always
237    --- given.
238    public rtld_local: i32 = 0
239    --- `dlopen` mode: the object's symbols resolve the references of an
240    --- object loaded after it.
241    public rtld_global: i32 = 256
242end if