-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.rs
More file actions
91 lines (83 loc) · 2.54 KB
/
Copy pathfiles.rs
File metadata and controls
91 lines (83 loc) · 2.54 KB
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
//! linux-parity: partial
//! linux-source: vendor/linux/kernel
//! Task file table glue (`task_struct.files`).
//!
//! The ABI-facing `TaskStruct.files` field is an opaque pointer in
//! `kernel/task.rs` to preserve the Linux layout. The real implementation lives
//! in `fs/fdtable.rs` as an `Arc<FilesStruct>`.
//!
//! This module provides the unsafe cast + lifetime management helpers that
//! bridge the two.
extern crate alloc;
use alloc::sync::Arc;
use crate::fs::fdtable::{FilesStruct as FdTable, dup_fd};
use crate::kernel::task::{FilesStruct as OpaqueFiles, TaskStruct};
#[inline]
unsafe fn raw_ptr(tsk: *mut TaskStruct) -> *const FdTable {
unsafe { (*tsk).files as *const FdTable }
}
/// Install a new `files_struct` into `tsk`, consuming one strong reference.
///
/// # Safety
/// `tsk` must be a valid `TaskStruct` pointer.
pub unsafe fn set_task_files(tsk: *mut TaskStruct, files: Arc<FdTable>) {
let ptr = Arc::into_raw(files) as *mut FdTable;
unsafe {
(*tsk).files = ptr as *mut OpaqueFiles;
}
}
/// Borrow `tsk->files` as an owning `Arc`.
///
/// This function increments the strong count and returns a new `Arc`.
///
/// # Safety
/// `tsk` must be valid. The caller must not mutate `tsk->files` concurrently.
pub unsafe fn get_task_files(tsk: *mut TaskStruct) -> Option<Arc<FdTable>> {
if tsk.is_null() {
return None;
}
let ptr = unsafe { raw_ptr(tsk) };
if ptr.is_null() {
return None;
}
unsafe {
Arc::increment_strong_count(ptr);
Some(Arc::from_raw(ptr))
}
}
/// Drop one reference to `tsk->files` and NULL the pointer.
///
/// # Safety
/// `tsk` must be valid. After this call, `tsk->files` is NULL.
pub unsafe fn drop_task_files(tsk: *mut TaskStruct) {
if tsk.is_null() {
return;
}
let ptr = unsafe { raw_ptr(tsk) };
if ptr.is_null() {
return;
}
unsafe {
(*tsk).files = core::ptr::null_mut();
drop(Arc::from_raw(ptr));
}
}
/// Duplicate a parent's `files_struct` for a newly created child.
///
/// Mirrors Linux's `copy_files()`: `CLONE_FILES` shares the table; otherwise
/// clone an independent copy.
///
/// # Safety
/// `child` and `parent` must be valid pointers.
pub unsafe fn copy_files(child: *mut TaskStruct, parent: *mut TaskStruct, clone_files: bool) {
unsafe {
(*child).files = core::ptr::null_mut();
}
let Some(parent_arc) = (unsafe { get_task_files(parent) }) else {
return;
};
let new = dup_fd(&parent_arc, clone_files);
unsafe {
set_task_files(child, new);
}
}