user_namespace.rs 3.2 KB

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