1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.
//! Execute system call directly without `std` or `libc`.
//!
//! - [Documentation](https://docs.rs/nc)
//! - [Release notes](https://github.com/xushaohua/nc/tags)
//!
//! ## Usage
//!
//! Add this to `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! nc = "0.9"
//! ```
//!
//! ## Examples
//!
//! Get file stat:
//! ```rust
//! let mut statbuf = nc::stat_t::default();
//! let filepath = "/etc/passwd";
//! #[cfg(target_os = "linux")]
//! let ret = {
//! #[cfg(not(any(
//! target_arch = "aarch64",
//! target_arch = "loongarch64",
//! target_arch = "riscv64"
//! )))]
//! unsafe {
//! nc::stat(filepath, &mut statbuf)
//! }
//!
//! #[cfg(any(
//! target_arch = "aarch64",
//! target_arch = "loongarch64",
//! target_arch = "riscv64"
//! ))]
//! unsafe {
//! nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0)
//! }
//! };
//! #[cfg(any(target_os = "android", target_os = "freebsd"))]
//! let ret = unsafe { nc::fstatat(nc::AT_FDCWD, filepath, &mut statbuf, 0) };
//! match ret {
//! Ok(_) => println!("s: {:?}", statbuf),
//! Err(errno) => eprintln!("Failed to get file status, got errno: {}", errno),
//! }
//! ```
//!
//! Fork process:
//! ```rust
//! let pid = unsafe { nc::fork() };
//! match pid {
//! Err(errno) => eprintln!("Failed to call fork(), err: {}", nc::strerror(errno)),
//! Ok(0) => {
//! // Child process
//! println!("[child] pid: {}", unsafe { nc::getpid() });
//! let args = ["ls", "-l", "-a"];
//! let env = ["DISPLAY=wayland"];
//! let ret = unsafe { nc::execve("/bin/ls", &args, &env) };
//! assert!(ret.is_ok());
//! }
//! Ok(child_pid) => {
//! // Parent process
//! println!("[main] child pid is: {child_pid}");
//! }
//! }
//! ```
//!
//! Kill init process:
//! ```rust
//! let ret = unsafe { nc::kill(1, nc::SIGTERM) };
//! assert_eq!(ret, Err(nc::EPERM));
//! ```
//!
//! Or handle signals:
//! ```
//! fn handle_alarm(signum: i32) {
//! assert_eq!(signum, nc::SIGALRM);
//! }
//!
//! fn main() {
//! let sa = nc::new_sigaction(handle_alarm);
//! let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
//! assert!(ret.is_ok());
//! let remaining = unsafe { nc::alarm(1) };
//! let mask = nc::sigset_t::default();
//! let ret = unsafe { nc::rt_sigsuspend(&mask) };
//! assert!(ret.is_err());
//! assert_eq!(ret, Err(nc::EINTR));
//! assert_eq!(remaining, Ok(0));
//! }
//! ```
//!
//! Or get system info:
//! ```rust
//! pub fn cstr_to_str(input: &[u8]) -> &str {
//! let nul_index = input.iter().position(|&b| b == 0).unwrap_or(input.len());
//! std::str::from_utf8(&input[0..nul_index]).unwrap()
//! }
//!
//! fn main() {
//! let mut uts = nc::utsname_t::default();
//! let ret = unsafe { nc::uname(&mut uts) };
//! assert!(ret.is_ok());
//!
//! let mut result = Vec::new();
//!
//! result.push(cstr_to_str(&uts.sysname));
//! result.push(cstr_to_str(&uts.nodename));
//! result.push(cstr_to_str(&uts.release));
//! result.push(cstr_to_str(&uts.version));
//! result.push(cstr_to_str(&uts.machine));
//! let domain_name = cstr_to_str(&uts.domainname);
//! if domain_name != "(none)" {
//! result.push(domain_name);
//! }
//!
//! let result = result.join(" ");
//! println!("{}", result);
//! }
//! ```
//!
//! ## Supported Operating Systems and Architectures
//!
//! - linux
//! - aarch64
//! - arm
//! - loongarch64
//! - mips
//! - mips64
//! - mips64el
//! - mipsel
//! - powerpc64
//! - powerpc64le
//! - riscv64
//! - s390x
//! - x86
//! - x86-64
//! - android
//! - aarch64
//! - freebsd
//! - x86-64
//! - netbsd
//! - x86-64
//! - mac os
//! - x86-64
//!
//! ## Related projects
//!
//! * [nix][nix]
//! * [syscall][syscall]
//! * [relibc][relibc]
//!
//! [syscall]: https://github.com/kmcallister/syscall.rs
//! [relibc]: https://gitlab.redox-os.org/redox-os/relibc.git
//! [nix]: https://github.com/nix-rust/nix
extern crate alloc;
extern crate std;
pub use Errno;
pub use *;
// Re-export functions
pub use *;
pub use *;