@@ -12,7 +12,7 @@ use core::sync::atomic::AtomicI32;
1212use hashbrown:: HashMap ;
1313use kernel_hal:: VirtAddr ;
1414use rcore_fs:: vfs:: { FileSystem , INode } ;
15- use spin:: Mutex ;
15+ use spin:: * ;
1616use zircon_object:: {
1717 object:: { KernelObject , KoID , Signal } ,
1818 signal:: Futex ,
@@ -143,6 +143,8 @@ struct LinuxProcessInner {
143143 ///
144144 /// Omit leading '/'.
145145 current_working_directory : String ,
146+ /// file open number limit
147+ file_limit : RLimit ,
146148 /// Opened files
147149 files : HashMap < FileDesc , Arc < dyn FileLike > > ,
148150 /// Futexes
@@ -151,6 +153,25 @@ struct LinuxProcessInner {
151153 children : HashMap < KoID , Arc < Process > > ,
152154}
153155
156+ /// resource limit
157+ #[ repr( C ) ]
158+ #[ derive( Debug , Copy , Clone ) ]
159+ pub struct RLimit {
160+ /// soft limit
161+ pub cur : u64 ,
162+ /// hard limit
163+ pub max : u64 ,
164+ }
165+
166+ impl Default for RLimit {
167+ fn default ( ) -> Self {
168+ RLimit {
169+ cur : 1024 ,
170+ max : 1024 ,
171+ }
172+ }
173+ }
174+
154175/// process exit code defination
155176pub type ExitCode = i32 ;
156177
@@ -209,16 +230,40 @@ impl LinuxProcess {
209230
210231 /// Add a file to the file descriptor table.
211232 pub fn add_file ( & self , file : Arc < dyn FileLike > ) -> LxResult < FileDesc > {
212- let mut inner = self . inner . lock ( ) ;
233+ let inner = self . inner . lock ( ) ;
213234 let fd = inner. get_free_fd ( ) ;
214- inner. files . insert ( fd, file) ;
215- Ok ( fd)
235+ self . insert_file ( inner, fd, file)
216236 }
217237
218238 /// Add a file to the file descriptor table at given `fd`.
219- pub fn add_file_at ( & self , fd : FileDesc , file : Arc < dyn FileLike > ) {
239+ pub fn add_file_at ( & self , fd : FileDesc , file : Arc < dyn FileLike > ) -> LxResult < FileDesc > {
240+ let inner = self . inner . lock ( ) ;
241+ self . insert_file ( inner, fd, file)
242+ }
243+
244+ /// insert a file and fd into the file descriptor table
245+ fn insert_file (
246+ & self ,
247+ mut inner : MutexGuard < LinuxProcessInner > ,
248+ fd : FileDesc ,
249+ file : Arc < dyn FileLike > ,
250+ ) -> LxResult < FileDesc > {
251+ if inner. files . len ( ) < inner. file_limit . cur as usize {
252+ inner. files . insert ( fd, file) ;
253+ Ok ( fd)
254+ } else {
255+ Err ( LxError :: EMFILE )
256+ }
257+ }
258+
259+ /// get and set file limit number
260+ pub fn file_limit ( & self , new_limit : Option < RLimit > ) -> RLimit {
220261 let mut inner = self . inner . lock ( ) ;
221- inner. files . insert ( fd, file) ;
262+ let old = inner. file_limit ;
263+ if let Some ( limit) = new_limit {
264+ inner. file_limit = limit;
265+ }
266+ old
222267 }
223268
224269 /// Get the `File` with given `fd`.
0 commit comments