Skip to main content

nodejs/stdlib/
constants.rs

1//! The platform's errno, signal and filesystem constants.
2//!
3//! Every value comes from `libc`, so it is the number THIS build's platform
4//! really uses. That matters: `EADDRINUSE` is 48 on macOS and 98 on Linux,
5//! `SIGUSR1` is 30 and 10, `SIGSTOP` is 17 and 19. The signal table used to be
6//! a hardcoded list of macOS numbers, so `os.constants.signals.SIGUSR1` was
7//! wrong on every Linux host and `errno` was missing entirely.
8//!
9//! These feed `os.constants`, `fs.constants` and the legacy flat
10//! `require('constants')`, which is the union of all three.
11
12use crate::host::with_host;
13use fusevm::Value;
14use indexmap::IndexMap;
15
16/// `(name, value)` for every errno node exposes. A name absent on this platform
17/// is simply not listed, exactly as node's own table is platform-shaped.
18pub fn errno() -> Vec<(&'static str, i64)> {
19    let mut v: Vec<(&'static str, i64)> = vec![
20        ("E2BIG", libc::E2BIG as i64),
21        ("EACCES", libc::EACCES as i64),
22        ("EADDRINUSE", libc::EADDRINUSE as i64),
23        ("EADDRNOTAVAIL", libc::EADDRNOTAVAIL as i64),
24        ("EAFNOSUPPORT", libc::EAFNOSUPPORT as i64),
25        ("EAGAIN", libc::EAGAIN as i64),
26        ("EALREADY", libc::EALREADY as i64),
27        ("EBADF", libc::EBADF as i64),
28        ("EBADMSG", libc::EBADMSG as i64),
29        ("EBUSY", libc::EBUSY as i64),
30        ("ECANCELED", libc::ECANCELED as i64),
31        ("ECHILD", libc::ECHILD as i64),
32        ("ECONNABORTED", libc::ECONNABORTED as i64),
33        ("ECONNREFUSED", libc::ECONNREFUSED as i64),
34        ("ECONNRESET", libc::ECONNRESET as i64),
35        ("EDEADLK", libc::EDEADLK as i64),
36        ("EDESTADDRREQ", libc::EDESTADDRREQ as i64),
37        ("EDOM", libc::EDOM as i64),
38        ("EDQUOT", libc::EDQUOT as i64),
39        ("EEXIST", libc::EEXIST as i64),
40        ("EFAULT", libc::EFAULT as i64),
41        ("EFBIG", libc::EFBIG as i64),
42        ("EHOSTUNREACH", libc::EHOSTUNREACH as i64),
43        ("EIDRM", libc::EIDRM as i64),
44        ("EILSEQ", libc::EILSEQ as i64),
45        ("EINPROGRESS", libc::EINPROGRESS as i64),
46        ("EINTR", libc::EINTR as i64),
47        ("EINVAL", libc::EINVAL as i64),
48        ("EIO", libc::EIO as i64),
49        ("EISCONN", libc::EISCONN as i64),
50        ("EISDIR", libc::EISDIR as i64),
51        ("ELOOP", libc::ELOOP as i64),
52        ("EMFILE", libc::EMFILE as i64),
53        ("EMLINK", libc::EMLINK as i64),
54        ("EMSGSIZE", libc::EMSGSIZE as i64),
55        ("EMULTIHOP", libc::EMULTIHOP as i64),
56        ("ENAMETOOLONG", libc::ENAMETOOLONG as i64),
57        ("ENETDOWN", libc::ENETDOWN as i64),
58        ("ENETRESET", libc::ENETRESET as i64),
59        ("ENETUNREACH", libc::ENETUNREACH as i64),
60        ("ENFILE", libc::ENFILE as i64),
61        ("ENOBUFS", libc::ENOBUFS as i64),
62        ("ENODATA", libc::ENODATA as i64),
63        ("ENODEV", libc::ENODEV as i64),
64        ("ENOENT", libc::ENOENT as i64),
65        ("ENOEXEC", libc::ENOEXEC as i64),
66        ("ENOLCK", libc::ENOLCK as i64),
67        ("ENOLINK", libc::ENOLINK as i64),
68        ("ENOMEM", libc::ENOMEM as i64),
69        ("ENOMSG", libc::ENOMSG as i64),
70        ("ENOPROTOOPT", libc::ENOPROTOOPT as i64),
71        ("ENOSPC", libc::ENOSPC as i64),
72        ("ENOSR", libc::ENOSR as i64),
73        ("ENOSTR", libc::ENOSTR as i64),
74        ("ENOSYS", libc::ENOSYS as i64),
75        ("ENOTCONN", libc::ENOTCONN as i64),
76        ("ENOTDIR", libc::ENOTDIR as i64),
77        ("ENOTEMPTY", libc::ENOTEMPTY as i64),
78        ("ENOTSOCK", libc::ENOTSOCK as i64),
79        ("ENOTSUP", libc::ENOTSUP as i64),
80        ("ENOTTY", libc::ENOTTY as i64),
81        ("ENXIO", libc::ENXIO as i64),
82        ("EOPNOTSUPP", libc::EOPNOTSUPP as i64),
83        ("EOVERFLOW", libc::EOVERFLOW as i64),
84        ("EPERM", libc::EPERM as i64),
85        ("EPIPE", libc::EPIPE as i64),
86        ("EPROTO", libc::EPROTO as i64),
87        ("EPROTONOSUPPORT", libc::EPROTONOSUPPORT as i64),
88        ("EPROTOTYPE", libc::EPROTOTYPE as i64),
89        ("ERANGE", libc::ERANGE as i64),
90        ("EROFS", libc::EROFS as i64),
91        ("ESPIPE", libc::ESPIPE as i64),
92        ("ESRCH", libc::ESRCH as i64),
93        ("ESTALE", libc::ESTALE as i64),
94        ("ETIME", libc::ETIME as i64),
95        ("ETIMEDOUT", libc::ETIMEDOUT as i64),
96        ("ETXTBSY", libc::ETXTBSY as i64),
97        ("EWOULDBLOCK", libc::EWOULDBLOCK as i64),
98        ("EXDEV", libc::EXDEV as i64),
99    ];
100    v.sort_by_key(|(k, _)| *k);
101    v
102}
103
104/// `(name, number)` for every signal node exposes.
105pub fn signals() -> Vec<(&'static str, i64)> {
106    let mut v: Vec<(&'static str, i64)> = vec![
107        ("SIGHUP", libc::SIGHUP as i64),
108        ("SIGINT", libc::SIGINT as i64),
109        ("SIGQUIT", libc::SIGQUIT as i64),
110        ("SIGILL", libc::SIGILL as i64),
111        ("SIGTRAP", libc::SIGTRAP as i64),
112        ("SIGABRT", libc::SIGABRT as i64),
113        ("SIGIOT", libc::SIGABRT as i64),
114        ("SIGBUS", libc::SIGBUS as i64),
115        ("SIGFPE", libc::SIGFPE as i64),
116        ("SIGKILL", libc::SIGKILL as i64),
117        ("SIGUSR1", libc::SIGUSR1 as i64),
118        ("SIGSEGV", libc::SIGSEGV as i64),
119        ("SIGUSR2", libc::SIGUSR2 as i64),
120        ("SIGPIPE", libc::SIGPIPE as i64),
121        ("SIGALRM", libc::SIGALRM as i64),
122        ("SIGTERM", libc::SIGTERM as i64),
123        ("SIGCHLD", libc::SIGCHLD as i64),
124        ("SIGCONT", libc::SIGCONT as i64),
125        ("SIGSTOP", libc::SIGSTOP as i64),
126        ("SIGTSTP", libc::SIGTSTP as i64),
127        ("SIGTTIN", libc::SIGTTIN as i64),
128        ("SIGTTOU", libc::SIGTTOU as i64),
129        ("SIGURG", libc::SIGURG as i64),
130        ("SIGXCPU", libc::SIGXCPU as i64),
131        ("SIGXFSZ", libc::SIGXFSZ as i64),
132        ("SIGVTALRM", libc::SIGVTALRM as i64),
133        ("SIGPROF", libc::SIGPROF as i64),
134        ("SIGWINCH", libc::SIGWINCH as i64),
135        ("SIGIO", libc::SIGIO as i64),
136        ("SIGSYS", libc::SIGSYS as i64),
137    ];
138    // `SIGINFO` is a BSD signal; Linux has no such number.
139    #[cfg(target_os = "macos")]
140    v.push(("SIGINFO", libc::SIGINFO as i64));
141    v.sort_by_key(|(k, _)| *k);
142    v
143}
144
145/// `(name, value)` for `fs.constants` — open flags, access modes, file-type
146/// bits, permission bits and the copyfile flags.
147pub fn fs() -> Vec<(&'static str, i64)> {
148    vec![
149        ("UV_FS_SYMLINK_DIR", 1),
150        ("UV_FS_SYMLINK_JUNCTION", 2),
151        ("O_RDONLY", libc::O_RDONLY as i64),
152        ("O_WRONLY", libc::O_WRONLY as i64),
153        ("O_RDWR", libc::O_RDWR as i64),
154        ("UV_DIRENT_UNKNOWN", 0),
155        ("UV_DIRENT_FILE", 1),
156        ("UV_DIRENT_DIR", 2),
157        ("UV_DIRENT_LINK", 3),
158        ("UV_DIRENT_FIFO", 4),
159        ("UV_DIRENT_SOCKET", 5),
160        ("UV_DIRENT_CHAR", 6),
161        ("UV_DIRENT_BLOCK", 7),
162        ("S_IFMT", libc::S_IFMT as i64),
163        ("S_IFREG", libc::S_IFREG as i64),
164        ("S_IFDIR", libc::S_IFDIR as i64),
165        ("S_IFCHR", libc::S_IFCHR as i64),
166        ("S_IFBLK", libc::S_IFBLK as i64),
167        ("S_IFIFO", libc::S_IFIFO as i64),
168        ("S_IFLNK", libc::S_IFLNK as i64),
169        ("S_IFSOCK", libc::S_IFSOCK as i64),
170        ("O_CREAT", libc::O_CREAT as i64),
171        ("O_EXCL", libc::O_EXCL as i64),
172        ("O_NOCTTY", libc::O_NOCTTY as i64),
173        ("O_TRUNC", libc::O_TRUNC as i64),
174        ("O_APPEND", libc::O_APPEND as i64),
175        ("O_DIRECTORY", libc::O_DIRECTORY as i64),
176        ("O_NOFOLLOW", libc::O_NOFOLLOW as i64),
177        ("O_SYNC", libc::O_SYNC as i64),
178        ("O_DSYNC", libc::O_DSYNC as i64),
179        ("O_NONBLOCK", libc::O_NONBLOCK as i64),
180        ("S_IRWXU", libc::S_IRWXU as i64),
181        ("S_IRUSR", libc::S_IRUSR as i64),
182        ("S_IWUSR", libc::S_IWUSR as i64),
183        ("S_IXUSR", libc::S_IXUSR as i64),
184        ("S_IRWXG", libc::S_IRWXG as i64),
185        ("S_IRGRP", libc::S_IRGRP as i64),
186        ("S_IWGRP", libc::S_IWGRP as i64),
187        ("S_IXGRP", libc::S_IXGRP as i64),
188        ("S_IRWXO", libc::S_IRWXO as i64),
189        ("S_IROTH", libc::S_IROTH as i64),
190        ("S_IWOTH", libc::S_IWOTH as i64),
191        ("S_IXOTH", libc::S_IXOTH as i64),
192        ("F_OK", libc::F_OK as i64),
193        ("R_OK", libc::R_OK as i64),
194        ("W_OK", libc::W_OK as i64),
195        ("X_OK", libc::X_OK as i64),
196        ("UV_FS_COPYFILE_EXCL", 1),
197        ("COPYFILE_EXCL", 1),
198        ("UV_FS_COPYFILE_FICLONE", 2),
199        ("COPYFILE_FICLONE", 2),
200        ("UV_FS_COPYFILE_FICLONE_FORCE", 4),
201        ("COPYFILE_FICLONE_FORCE", 4),
202    ]
203}
204
205/// `os.constants.dlopen`.
206pub fn dlopen() -> Vec<(&'static str, i64)> {
207    vec![
208        ("RTLD_LAZY", libc::RTLD_LAZY as i64),
209        ("RTLD_NOW", libc::RTLD_NOW as i64),
210        ("RTLD_GLOBAL", libc::RTLD_GLOBAL as i64),
211        ("RTLD_LOCAL", libc::RTLD_LOCAL as i64),
212    ]
213}
214
215/// `os.constants.priority` — libuv's own scale, not a platform one.
216pub fn priority() -> Vec<(&'static str, i64)> {
217    vec![
218        ("PRIORITY_LOW", 19),
219        ("PRIORITY_BELOW_NORMAL", 10),
220        ("PRIORITY_NORMAL", 0),
221        ("PRIORITY_ABOVE_NORMAL", -7),
222        ("PRIORITY_HIGH", -14),
223        ("PRIORITY_HIGHEST", -20),
224    ]
225}
226
227/// `crypto.constants` — OpenSSL's own defines, which are the same numbers
228/// everywhere, plus the two padding constants libraries actually pass.
229pub fn crypto() -> Vec<(&'static str, i64)> {
230    vec![
231        ("SSL_OP_ALL", 0x80000BFF),
232        ("SSL_OP_ALLOW_NO_DHE_KEX", 0x400),
233        ("SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION", 0x40000),
234        ("SSL_OP_CIPHER_SERVER_PREFERENCE", 0x400000),
235        ("SSL_OP_CISCO_ANYCONNECT", 0x8000),
236        ("SSL_OP_COOKIE_EXCHANGE", 0x2000),
237        ("SSL_OP_CRYPTOPRO_TLSEXT_BUG", 0x80000000),
238        ("SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS", 0x800),
239        ("SSL_OP_LEGACY_SERVER_CONNECT", 0x4),
240        ("SSL_OP_NO_COMPRESSION", 0x20000),
241        ("SSL_OP_NO_ENCRYPT_THEN_MAC", 0x80000),
242        ("SSL_OP_NO_QUERY_MTU", 0x1000),
243        ("SSL_OP_NO_RENEGOTIATION", 0x40000000),
244        ("SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION", 0x10000),
245        ("SSL_OP_NO_SSLv2", 0x0),
246        ("SSL_OP_NO_SSLv3", 0x2000000),
247        ("SSL_OP_NO_TLSv1", 0x4000000),
248        ("SSL_OP_NO_TLSv1_1", 0x10000000),
249        ("SSL_OP_NO_TLSv1_2", 0x8000000),
250        ("SSL_OP_NO_TLSv1_3", 0x20000000),
251        ("SSL_OP_PRIORITIZE_CHACHA", 0x200000),
252        ("SSL_OP_TLS_ROLLBACK_BUG", 0x800000),
253        ("ENGINE_METHOD_RSA", 1),
254        ("ENGINE_METHOD_DSA", 2),
255        ("ENGINE_METHOD_DH", 4),
256        ("ENGINE_METHOD_RAND", 8),
257        ("ENGINE_METHOD_EC", 2048),
258        ("ENGINE_METHOD_CIPHERS", 64),
259        ("ENGINE_METHOD_DIGESTS", 128),
260        ("ENGINE_METHOD_PKEY_METHS", 512),
261        ("ENGINE_METHOD_PKEY_ASN1_METHS", 1024),
262        ("ENGINE_METHOD_ALL", 65535),
263        ("ENGINE_METHOD_NONE", 0),
264        ("DH_CHECK_P_NOT_SAFE_PRIME", 2),
265        ("DH_CHECK_P_NOT_PRIME", 1),
266        ("DH_UNABLE_TO_CHECK_GENERATOR", 4),
267        ("DH_NOT_SUITABLE_GENERATOR", 8),
268        ("RSA_PKCS1_PADDING", 1),
269        ("RSA_NO_PADDING", 3),
270        ("RSA_PKCS1_OAEP_PADDING", 4),
271        ("RSA_X931_PADDING", 5),
272        ("RSA_PKCS1_PSS_PADDING", 6),
273        ("RSA_PSS_SALTLEN_DIGEST", -1),
274        ("RSA_PSS_SALTLEN_MAX_SIGN", -2),
275        ("RSA_PSS_SALTLEN_AUTO", -2),
276        ("POINT_CONVERSION_COMPRESSED", 2),
277        ("POINT_CONVERSION_UNCOMPRESSED", 4),
278        ("POINT_CONVERSION_HYBRID", 6),
279        ("TLS1_VERSION", 769),
280        ("TLS1_1_VERSION", 770),
281        ("TLS1_2_VERSION", 771),
282        ("TLS1_3_VERSION", 772),
283        ("OPENSSL_VERSION_NUMBER", 0x30000000),
284        ("defaultCoreCipherList", 0),
285        ("defaultCipherList", 0),
286    ]
287}
288
289/// A `{name: value}` object from a constant table.
290pub fn object(entries: &[(&'static str, i64)]) -> Value {
291    with_host(|h| {
292        let mut m: IndexMap<String, Value> = IndexMap::new();
293        for (k, v) in entries {
294            m.insert((*k).to_string(), Value::Float(*v as f64));
295        }
296        h.new_object(m)
297    })
298}
299
300/// The legacy flat `require('constants')`: the union of the dlopen, errno,
301/// signal, fs and crypto tables, in the order node lists them. A name defined
302/// by more than one table keeps the FIRST definition, as node's does.
303pub fn flat() -> Vec<(&'static str, i64)> {
304    let mut out: Vec<(&'static str, i64)> = Vec::new();
305    for table in [dlopen(), errno(), signals(), fs(), crypto()] {
306        for (k, v) in table {
307            if !out.iter().any(|(n, _)| *n == k) {
308                out.push((k, v));
309            }
310        }
311    }
312    out
313}
314
315/// One member of the flat table, for a `require('constants').NAME` read.
316pub fn flat_lookup(name: &str) -> Option<Value> {
317    flat()
318        .into_iter()
319        .find(|(k, _)| *k == name)
320        .map(|(_, v)| Value::Float(v as f64))
321}