hermit/fd/
mod.rs

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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::future::{self, Future};
use core::task::Poll::{Pending, Ready};
use core::time::Duration;

use async_trait::async_trait;
#[cfg(any(feature = "tcp", feature = "udp"))]
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};

use crate::arch::kernel::core_local::core_scheduler;
use crate::executor::block_on;
use crate::fs::{DirectoryEntry, FileAttr, SeekWhence};
use crate::io;

mod eventfd;
#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
pub(crate) mod socket;
pub(crate) mod stdio;

pub(crate) const STDIN_FILENO: FileDescriptor = 0;
pub(crate) const STDOUT_FILENO: FileDescriptor = 1;
pub(crate) const STDERR_FILENO: FileDescriptor = 2;

#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
#[derive(Debug)]
pub(crate) enum Endpoint {
	#[cfg(any(feature = "tcp", feature = "udp"))]
	Ip(IpEndpoint),
	#[cfg(feature = "vsock")]
	Vsock(socket::vsock::VsockEndpoint),
}

#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
#[derive(Debug)]
pub(crate) enum ListenEndpoint {
	#[cfg(any(feature = "tcp", feature = "udp"))]
	Ip(IpListenEndpoint),
	#[cfg(feature = "vsock")]
	Vsock(socket::vsock::VsockListenEndpoint),
}

#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub(crate) enum SocketOption {
	TcpNoDelay,
}

#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub(crate) enum IoCtl {
	NonBlocking,
}

pub(crate) type FileDescriptor = i32;

bitflags! {
	/// Options for opening files
	#[derive(Debug, Copy, Clone, Default)]
	pub struct OpenOption: i32 {
		const O_RDONLY = 0o0000;
		const O_WRONLY = 0o0001;
		const O_RDWR = 0o0002;
		const O_CREAT = 0o0100;
		const O_EXCL = 0o0200;
		const O_TRUNC = 0o1000;
		const O_APPEND = 0o2000;
		const O_DIRECT = 0o40000;
		const O_DIRECTORY = 0o200000;
	}
}

bitflags! {
	#[derive(Debug, Copy, Clone, Default)]
	pub struct PollEvent: i16 {
		const POLLIN = 0x1;
		const POLLPRI = 0x2;
		const POLLOUT = 0x4;
		const POLLERR = 0x8;
		const POLLHUP = 0x10;
		const POLLNVAL = 0x20;
		const POLLRDNORM = 0x040;
		const POLLRDBAND = 0x080;
		const POLLWRNORM = 0x0100;
		const POLLWRBAND = 0x0200;
		const POLLRDHUP = 0x2000;
	}
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct PollFd {
	/// file descriptor
	pub fd: i32,
	/// events to look for
	pub events: PollEvent,
	/// events returned
	pub revents: PollEvent,
}

bitflags! {
	#[derive(Debug, Default, Copy, Clone)]
	pub struct EventFlags: i16 {
		const EFD_SEMAPHORE = 0o1;
		const EFD_NONBLOCK = 0o4000;
		const EFD_CLOEXEC = 0o40000;
	}
}

bitflags! {
	#[derive(Debug, Copy, Clone)]
	pub struct AccessPermission: u32 {
		const S_IFMT = 0o170000;
		const S_IFSOCK = 0o140000;
		const S_IFLNK = 0o120000;
		const S_IFREG = 0o100000;
		const S_IFBLK = 0o060000;
		const S_IFDIR = 0o040000;
		const S_IFCHR = 0o020000;
		const S_IFIFO = 0o010000;
		const S_IRUSR = 0o400;
		const S_IWUSR = 0o200;
		const S_IXUSR = 0o100;
		const S_IRWXU = 0o700;
		const S_IRGRP = 0o040;
		const S_IWGRP = 0o020;
		const S_IXGRP = 0o010;
		const S_IRWXG = 0o070;
		const S_IROTH = 0o004;
		const S_IWOTH = 0o002;
		const S_IXOTH = 0o001;
		const S_IRWXO = 0o007;
		// Allow bits unknown to us to be set externally. See bitflags documentation for further explanation.
		const _ = !0;
	}
}

impl Default for AccessPermission {
	fn default() -> Self {
		AccessPermission::from_bits(0o666).unwrap()
	}
}

#[async_trait]
pub(crate) trait ObjectInterface: Sync + Send + core::fmt::Debug {
	/// check if an IO event is possible
	async fn poll(&self, _event: PollEvent) -> io::Result<PollEvent> {
		Ok(PollEvent::empty())
	}

	/// `async_read` attempts to read `len` bytes from the object references
	/// by the descriptor
	async fn read(&self, _buf: &mut [u8]) -> io::Result<usize> {
		Err(io::Error::ENOSYS)
	}

	/// `async_write` attempts to write `len` bytes to the object references
	/// by the descriptor
	async fn write(&self, _buf: &[u8]) -> io::Result<usize> {
		Err(io::Error::ENOSYS)
	}

	/// `lseek` function repositions the offset of the file descriptor fildes
	async fn lseek(&self, _offset: isize, _whence: SeekWhence) -> io::Result<isize> {
		Err(io::Error::EINVAL)
	}

	/// `fstat`
	async fn fstat(&self) -> io::Result<FileAttr> {
		Err(io::Error::EINVAL)
	}

	/// 'readdir' returns a pointer to a dirent structure
	/// representing the next directory entry in the directory stream
	/// pointed to by the file descriptor
	async fn readdir(&self) -> io::Result<Vec<DirectoryEntry>> {
		Err(io::Error::EINVAL)
	}

	/// `accept` a connection on a socket
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn accept(&self) -> io::Result<(Arc<dyn ObjectInterface>, Endpoint)> {
		Err(io::Error::EINVAL)
	}

	/// initiate a connection on a socket
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn connect(&self, _endpoint: Endpoint) -> io::Result<()> {
		Err(io::Error::EINVAL)
	}

	/// `bind` a name to a socket
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn bind(&self, _name: ListenEndpoint) -> io::Result<()> {
		Err(io::Error::EINVAL)
	}

	/// `listen` for connections on a socket
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn listen(&self, _backlog: i32) -> io::Result<()> {
		Err(io::Error::EINVAL)
	}

	/// `setsockopt` sets options on sockets
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn setsockopt(&self, _opt: SocketOption, _optval: bool) -> io::Result<()> {
		Err(io::Error::ENOTSOCK)
	}

	/// `getsockopt` gets options on sockets
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn getsockopt(&self, _opt: SocketOption) -> io::Result<bool> {
		Err(io::Error::ENOTSOCK)
	}

	/// `getsockname` gets socket name
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn getsockname(&self) -> io::Result<Option<Endpoint>> {
		Ok(None)
	}

	/// `getpeername` get address of connected peer
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	#[allow(dead_code)]
	async fn getpeername(&self) -> io::Result<Option<Endpoint>> {
		Ok(None)
	}

	/// receive a message from a socket
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn recvfrom(&self, _buffer: &mut [u8]) -> io::Result<(usize, Endpoint)> {
		Err(io::Error::ENOSYS)
	}

	/// send a message from a socket
	///
	/// The sendto() function shall send a message.
	/// If the socket is a connectionless-mode socket, the message shall
	/// If a peer address has been prespecified, either the message shall
	/// be sent to the address specified by dest_addr (overriding the pre-specified peer
	/// address).
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn sendto(&self, _buffer: &[u8], _endpoint: Endpoint) -> io::Result<usize> {
		Err(io::Error::ENOSYS)
	}

	/// shut down part of a full-duplex connection
	#[cfg(any(feature = "tcp", feature = "udp", feature = "vsock"))]
	async fn shutdown(&self, _how: i32) -> io::Result<()> {
		Err(io::Error::ENOSYS)
	}

	/// The `ioctl` function manipulates the underlying device parameters of special
	/// files.
	async fn ioctl(&self, _cmd: IoCtl, _value: bool) -> io::Result<()> {
		Err(io::Error::ENOSYS)
	}
}

pub(crate) fn read(fd: FileDescriptor, buf: &mut [u8]) -> io::Result<usize> {
	let obj = get_object(fd)?;

	if buf.is_empty() {
		return Ok(0);
	}

	block_on(obj.read(buf), None)
}

pub(crate) fn lseek(fd: FileDescriptor, offset: isize, whence: SeekWhence) -> io::Result<isize> {
	let obj = get_object(fd)?;

	block_on(obj.lseek(offset, whence), None)
}

pub(crate) fn write(fd: FileDescriptor, buf: &[u8]) -> io::Result<usize> {
	let obj = get_object(fd)?;

	if buf.is_empty() {
		return Ok(0);
	}

	block_on(obj.write(buf), None)
}

async fn poll_fds(fds: &mut [PollFd]) -> io::Result<u64> {
	future::poll_fn(|cx| {
		let mut counter: u64 = 0;

		for i in &mut *fds {
			let fd = i.fd;
			i.revents = PollEvent::empty();
			let mut pinned_obj = core::pin::pin!(core_scheduler().get_object(fd));
			if let Ready(Ok(obj)) = pinned_obj.as_mut().poll(cx) {
				let mut pinned = core::pin::pin!(obj.poll(i.events));
				if let Ready(Ok(e)) = pinned.as_mut().poll(cx) {
					if !e.is_empty() {
						counter += 1;
						i.revents = e;
					}
				}
			}
		}

		if counter > 0 {
			Ready(Ok(counter))
		} else {
			Pending
		}
	})
	.await
}

/// Wait for some event on a file descriptor.
///
/// The unix-like `poll` waits for one of a set of file descriptors
/// to become ready to perform I/O. The set of file descriptors to be
/// monitored is specified in the `fds` argument, which is an array
/// of structs of `PollFd`.
pub fn poll(fds: &mut [PollFd], timeout: Option<Duration>) -> io::Result<u64> {
	let result = block_on(poll_fds(fds), timeout);
	if let Err(ref e) = result {
		if timeout.is_some() {
			// A return value of zero indicates that the system call timed out
			if *e == io::Error::EAGAIN {
				return Ok(0);
			}
		}
	}

	result
}

pub fn fstat(fd: FileDescriptor) -> io::Result<FileAttr> {
	let obj = get_object(fd)?;
	block_on(obj.fstat(), None)
}

/// Wait for some event on a file descriptor.
///
/// `eventfd` creates an linux-like "eventfd object" that can be used
/// as an event wait/notify mechanism by user-space applications, and by
/// the kernel to notify user-space applications of events. The
/// object contains an unsigned 64-bit integer counter
/// that is maintained by the kernel. This counter is initialized
/// with the value specified in the argument `initval`.
///
/// As its return value, `eventfd` returns a new file descriptor that
/// can be used to refer to the eventfd object.
///
/// The following values may be bitwise set in flags to change the
/// behavior of `eventfd`:
///
/// `EFD_NONBLOCK`: Set the file descriptor in non-blocking mode
/// `EFD_SEMAPHORE`: Provide semaphore-like semantics for reads
/// from the new file descriptor.
pub fn eventfd(initval: u64, flags: EventFlags) -> io::Result<FileDescriptor> {
	let obj = self::eventfd::EventFd::new(initval, flags);

	let fd = block_on(core_scheduler().insert_object(Arc::new(obj)), None)?;

	Ok(fd)
}

pub(crate) fn get_object(fd: FileDescriptor) -> io::Result<Arc<dyn ObjectInterface>> {
	block_on(core_scheduler().get_object(fd), None)
}

pub(crate) fn insert_object(obj: Arc<dyn ObjectInterface>) -> io::Result<FileDescriptor> {
	block_on(core_scheduler().insert_object(obj), None)
}

// The dup system call allocates a new file descriptor that refers
// to the same open file description as the descriptor oldfd. The new
// file descriptor number is guaranteed to be the lowest-numbered
// file descriptor that was unused in the calling process.
pub(crate) fn dup_object(fd: FileDescriptor) -> io::Result<FileDescriptor> {
	block_on(core_scheduler().dup_object(fd), None)
}

pub(crate) fn remove_object(fd: FileDescriptor) -> io::Result<Arc<dyn ObjectInterface>> {
	block_on(core_scheduler().remove_object(fd), None)
}