xref: /DragonOS/kernel/src/namespaces/user_namespace.rs (revision 7b0ef10895108a0de5ff5ef3d2f93f40cf2e33a5)
1 #![allow(dead_code, unused_variables, unused_imports)]
2 
3 use alloc::boxed::Box;
4 
5 use crate::libs::rwlock::RwLock;
6 use alloc::string::String;
7 use alloc::string::ToString;
8 
9 use alloc::vec::Vec;
10 use system_error::SystemError;
11 
12 use crate::namespaces::namespace::NsCommon;
13 use crate::namespaces::ucount::UCounts;
14 use crate::process::fork::CloneFlags;
15 use crate::process::Pid;
16 use alloc::sync::Arc;
17 
18 use super::namespace::NsOperations;
19 use super::ucount::Ucount::Counts;
20 
21 const UID_GID_MAP_MAX_BASE_EXTENTS: usize = 5;
22 const UCOUNT_MAX: u32 = 62636;
23 /// 管理用户ID和组ID的映射
24 #[allow(dead_code)]
25 #[derive(Clone, Debug)]
26 struct UidGidMap {
27     nr_extents: u32,
28     extent: Vec<UidGidExtent>,
29 }
30 
31 ///区间映射
32 #[allow(dead_code)]
33 #[derive(Clone, Debug)]
34 struct UidGidExtent {
35     first: u32,
36     lower_first: u32,
37     count: u32,
38 }
39 #[derive(Debug)]
40 pub struct UserNamespace {
41     uid_map: UidGidMap,
42     gid_map: UidGidMap,
43     progid_map: UidGidMap,
44     ///项目ID映射
45     parent: Option<Arc<UserNamespace>>,
46     level: u32,
47     owner: usize,
48     group: usize,
49     ns_common: Arc<NsCommon>,
50     flags: u32,
51     pid: Arc<RwLock<Pid>>,
52     pub ucounts: Option<Arc<UCounts>>,
53     pub ucount_max: Vec<u32>, //vec![u32; UCOUNT_COUNTS as usize],
54     pub rlimit_max: Vec<u32>, // vec![u32; UCOUNT_RLIMIT_COUNTS as usize],
55 }
56 
57 impl Default for UserNamespace {
58     fn default() -> Self {
59         Self::new()
60     }
61 }
62 #[derive(Debug)]
63 struct UserNsOperations {
64     name: String,
65     clone_flags: CloneFlags,
66 }
67 impl UserNsOperations {
68     pub fn new(name: String) -> Self {
69         Self {
70             name,
71             clone_flags: CloneFlags::CLONE_NEWUSER,
72         }
73     }
74 }
75 impl NsOperations for UserNsOperations {
76     fn get(&self, pid: Pid) -> Option<Arc<NsCommon>> {
77         unimplemented!()
78     }
79     fn get_parent(&self, ns_common: Arc<NsCommon>) -> Result<Arc<NsCommon>, SystemError> {
80         unimplemented!()
81     }
82     fn install(
83         &self,
84         nsset: &mut super::NsSet,
85         ns_common: Arc<NsCommon>,
86     ) -> Result<(), SystemError> {
87         unimplemented!()
88     }
89     fn owner(&self, ns_common: Arc<NsCommon>) -> Arc<UserNamespace> {
90         unimplemented!()
91     }
92     fn put(&self, ns_common: Arc<NsCommon>) {
93         unimplemented!()
94     }
95 }
96 impl UidGidMap {
97     pub fn new() -> Self {
98         Self {
99             nr_extents: 1,
100             extent: vec![UidGidExtent::new(); UID_GID_MAP_MAX_BASE_EXTENTS],
101         }
102     }
103 }
104 
105 impl UidGidExtent {
106     pub fn new() -> Self {
107         Self {
108             first: 0,
109             lower_first: 0,
110             count: u32::MAX,
111         }
112     }
113 }
114 
115 impl UserNamespace {
116     pub fn new() -> Self {
117         Self {
118             uid_map: UidGidMap::new(),
119             gid_map: UidGidMap::new(),
120             progid_map: UidGidMap::new(),
121             owner: 0,
122             level: 0,
123             group: 0,
124             flags: 1,
125             parent: None,
126             ns_common: Arc::new(NsCommon::new(Box::new(UserNsOperations::new(
127                 "User".to_string(),
128             )))),
129             pid: Arc::new(RwLock::new(Pid::new(1))),
130             ucount_max: vec![UCOUNT_MAX; Counts as usize],
131             ucounts: None,
132             rlimit_max: vec![65535, 10, 32000, 64 * 1024],
133         }
134     }
135 }
136