1use itertools::Itertools;
6use regex::Regex;
7use starnix_core::mm::{
8 MemoryAccessor, MemoryAccessorExt, MemoryManager, PAGE_SIZE, ProcMapsFile, ProcSmapsFile,
9};
10use starnix_core::security;
11use starnix_core::task::{
12 CurrentTask, Task, TaskPersistentInfo, TaskStateCode, ThreadGroup, ThreadGroupKey,
13 path_from_root,
14};
15use starnix_core::vfs::buffers::{InputBuffer, OutputBuffer};
16use starnix_core::vfs::pseudo::dynamic_file::{DynamicFile, DynamicFileBuf, DynamicFileSource};
17use starnix_core::vfs::pseudo::simple_directory::SimpleDirectory;
18use starnix_core::vfs::pseudo::simple_file::{
19 BytesFile, BytesFileOps, SimpleFileNode, parse_i32_file, parse_unsigned_file,
20 serialize_for_file,
21};
22use starnix_core::vfs::pseudo::stub_empty_file::StubEmptyFile;
23use starnix_core::vfs::pseudo::vec_directory::{VecDirectory, VecDirectoryEntry};
24use starnix_core::vfs::{
25 CallbackSymlinkNode, CloseFreeSafe, DirectoryEntryType, DirentSink, FdNumber, FileObject,
26 FileOps, FileSystemHandle, FsNode, FsNodeHandle, FsNodeInfo, FsNodeOps, FsStr, FsString,
27 ProcMountinfoFile, ProcMountsFile, SeekTarget, SymlinkTarget, default_seek, emit_dotdot,
28 fileops_impl_directory, fileops_impl_noop_sync, fileops_impl_seekable,
29 fileops_impl_unbounded_seek, fs_node_impl_dir_readonly,
30};
31use starnix_logging::{bug_ref, track_stub};
32use starnix_sync::{FileOpsCore, Locked};
33use starnix_task_command::TaskCommand;
34use starnix_types::ownership::{TempRef, WeakRef};
35use starnix_types::time::duration_to_scheduler_clock;
36use starnix_uapi::auth::{
37 CAP_SYS_NICE, CAP_SYS_RESOURCE, Capabilities, PTRACE_MODE_ATTACH_REALCREDS,
38 PTRACE_MODE_NOAUDIT, PTRACE_MODE_READ_FSCREDS, PtraceAccessMode,
39};
40use starnix_uapi::device_type::DeviceType;
41use starnix_uapi::errors::Errno;
42use starnix_uapi::file_mode::{Access, FileMode, mode};
43use starnix_uapi::open_flags::OpenFlags;
44use starnix_uapi::resource_limits::Resource;
45use starnix_uapi::user_address::UserAddress;
46use starnix_uapi::{
47 OOM_ADJUST_MIN, OOM_DISABLE, OOM_SCORE_ADJ_MIN, RLIM_INFINITY, errno, error, ino_t, off_t,
48 pid_t, uapi,
49};
50use std::borrow::Cow;
51use std::ops::{Deref, Range};
52use std::sync::{Arc, LazyLock, Weak};
53
54fn task_entries(scope: TaskEntryScope) -> Vec<(FsString, FileMode)> {
56 let mut entries = vec![
58 (b"cgroup".into(), mode!(IFREG, 0o444)),
59 (b"cwd".into(), mode!(IFLNK, 0o777)),
60 (b"exe".into(), mode!(IFLNK, 0o777)),
61 (b"fd".into(), mode!(IFDIR, 0o500)),
62 (b"fdinfo".into(), mode!(IFDIR, 0o777)),
63 (b"io".into(), mode!(IFREG, 0o400)),
64 (b"limits".into(), mode!(IFREG, 0o444)),
65 (b"maps".into(), mode!(IFREG, 0o444)),
66 (b"mem".into(), mode!(IFREG, 0o600)),
67 (b"root".into(), mode!(IFLNK, 0o777)),
68 (b"sched".into(), mode!(IFREG, 0o644)),
69 (b"schedstat".into(), mode!(IFREG, 0o444)),
70 (b"smaps".into(), mode!(IFREG, 0o444)),
71 (b"stat".into(), mode!(IFREG, 0o444)),
72 (b"statm".into(), mode!(IFREG, 0o444)),
73 (b"status".into(), mode!(IFREG, 0o444)),
74 (b"cmdline".into(), mode!(IFREG, 0o444)),
75 (b"environ".into(), mode!(IFREG, 0o400)),
76 (b"auxv".into(), mode!(IFREG, 0o400)),
77 (b"comm".into(), mode!(IFREG, 0o644)),
78 (b"attr".into(), mode!(IFDIR, 0o555)),
79 (b"ns".into(), mode!(IFDIR, 0o511)),
80 (b"mountinfo".into(), mode!(IFREG, 0o444)),
81 (b"mounts".into(), mode!(IFREG, 0o444)),
82 (b"oom_adj".into(), mode!(IFREG, 0o744)),
83 (b"oom_score".into(), mode!(IFREG, 0o444)),
84 (b"oom_score_adj".into(), mode!(IFREG, 0o744)),
85 (b"timerslack_ns".into(), mode!(IFREG, 0o666)),
86 (b"wchan".into(), mode!(IFREG, 0o444)),
87 (b"clear_refs".into(), mode!(IFREG, 0o200)),
88 (b"pagemap".into(), mode!(IFREG, 0o400)),
89 ];
90
91 if scope == TaskEntryScope::ThreadGroup {
92 entries.push((b"task".into(), mode!(IFDIR, 0o555)));
93 }
94
95 entries
96}
97
98#[derive(Copy, Clone, Eq, PartialEq)]
99pub enum TaskEntryScope {
100 Task,
101 ThreadGroup,
102}
103
104pub struct TaskDirectory {
112 task_weak: WeakRef<Task>,
113 scope: TaskEntryScope,
114 inode_range: Range<ino_t>,
115}
116
117#[derive(Clone)]
118struct TaskDirectoryNode(Arc<TaskDirectory>);
119
120impl Deref for TaskDirectoryNode {
121 type Target = TaskDirectory;
122
123 fn deref(&self) -> &Self::Target {
124 &self.0
125 }
126}
127
128impl TaskDirectory {
129 fn new(fs: &FileSystemHandle, task: &TempRef<'_, Task>, scope: TaskEntryScope) -> FsNodeHandle {
130 let creds = task.real_creds().euid_as_fscred();
131 let task_weak = WeakRef::from(task);
132 fs.create_node_and_allocate_node_id(
133 TaskDirectoryNode(Arc::new(TaskDirectory {
134 task_weak,
135 scope,
136 inode_range: fs.allocate_ino_range(task_entries(scope).len()),
137 })),
138 FsNodeInfo::new(mode!(IFDIR, 0o555), creds),
139 )
140 }
141}
142
143impl FsNodeOps for TaskDirectoryNode {
144 fs_node_impl_dir_readonly!();
145
146 fn create_file_ops(
147 &self,
148 _locked: &mut Locked<FileOpsCore>,
149 _node: &FsNode,
150 _current_task: &CurrentTask,
151 _flags: OpenFlags,
152 ) -> Result<Box<dyn FileOps>, Errno> {
153 Ok(Box::new(self.clone()))
154 }
155
156 fn lookup(
157 &self,
158 _locked: &mut Locked<FileOpsCore>,
159 node: &FsNode,
160 _current_task: &CurrentTask,
161 name: &FsStr,
162 ) -> Result<FsNodeHandle, Errno> {
163 let task_weak = self.task_weak.clone();
164 let creds = node.info().cred();
165 let fs = node.fs();
166 let (mode, ino) = task_entries(self.scope)
167 .into_iter()
168 .enumerate()
169 .find_map(|(index, (n, mode))| {
170 if name == *n {
171 Some((mode, self.inode_range.start + index as ino_t))
172 } else {
173 None
174 }
175 })
176 .ok_or_else(|| errno!(ENOENT))?;
177
178 let ops: Box<dyn FsNodeOps> = match &**name {
180 b"cgroup" => Box::new(CgroupFile::new_node(task_weak)),
181 b"cwd" => Box::new(CallbackSymlinkNode::new({
182 move || Ok(SymlinkTarget::Node(Task::from_weak(&task_weak)?.fs().cwd()))
183 })),
184 b"exe" => Box::new(CallbackSymlinkNode::new({
185 move || {
186 let task = Task::from_weak(&task_weak)?;
187 if let Some(node) = task.mm().ok().and_then(|mm| mm.executable_node()) {
188 Ok(SymlinkTarget::Node(node))
189 } else {
190 error!(ENOENT)
191 }
192 }
193 })),
194 b"fd" => Box::new(FdDirectory::new(task_weak)),
195 b"fdinfo" => Box::new(FdInfoDirectory::new(task_weak)),
196 b"io" => Box::new(IoFile::new_node()),
197 b"limits" => Box::new(LimitsFile::new_node(task_weak)),
198 b"maps" => Box::new(PtraceCheckedNode::new_node(
199 task_weak,
200 PTRACE_MODE_READ_FSCREDS,
201 |_, _, task| Ok(ProcMapsFile::new(task)),
202 )),
203 b"mem" => Box::new(MemFile::new_node(task_weak)),
204 b"root" => Box::new(CallbackSymlinkNode::new({
205 move || Ok(SymlinkTarget::Node(Task::from_weak(&task_weak)?.fs().root()))
206 })),
207 b"sched" => Box::new(StubEmptyFile::new_node(bug_ref!("https://fxbug.dev/322893980"))),
208 b"schedstat" => {
209 Box::new(StubEmptyFile::new_node(bug_ref!("https://fxbug.dev/322894256")))
210 }
211 b"smaps" => Box::new(PtraceCheckedNode::new_node(
212 task_weak,
213 PTRACE_MODE_READ_FSCREDS,
214 |_, _, task| Ok(ProcSmapsFile::new(task)),
215 )),
216 b"stat" => Box::new(StatFile::new_node(task_weak, self.scope)),
217 b"statm" => Box::new(StatmFile::new_node(task_weak)),
218 b"status" => Box::new(StatusFile::new_node(task_weak)),
219 b"cmdline" => Box::new(CmdlineFile::new_node(task_weak)),
220 b"environ" => Box::new(EnvironFile::new_node(task_weak)),
221 b"auxv" => Box::new(AuxvFile::new_node(task_weak)),
222 b"comm" => {
223 let task = self.task_weak.upgrade().ok_or_else(|| errno!(ESRCH))?;
224 Box::new(CommFile::new_node(task_weak, task.persistent_info.clone()))
225 }
226 b"attr" => {
227 let dir = SimpleDirectory::new();
228 dir.edit(&fs, |dir| {
229 for (attr, name) in [
230 (security::ProcAttr::Current, "current"),
231 (security::ProcAttr::Exec, "exec"),
232 (security::ProcAttr::FsCreate, "fscreate"),
233 (security::ProcAttr::KeyCreate, "keycreate"),
234 (security::ProcAttr::SockCreate, "sockcreate"),
235 ] {
236 dir.entry_etc(
237 name.into(),
238 AttrNode::new(task_weak.clone(), attr),
239 mode!(IFREG, 0o666),
240 DeviceType::NONE,
241 creds,
242 );
243 }
244 dir.entry_etc(
245 "prev".into(),
246 AttrNode::new(task_weak, security::ProcAttr::Previous),
247 mode!(IFREG, 0o444),
248 DeviceType::NONE,
249 creds,
250 );
251 });
252 Box::new(dir)
253 }
254 b"ns" => Box::new(NsDirectory { task: task_weak }),
255 b"mountinfo" => Box::new(ProcMountinfoFile::new_node(task_weak)),
256 b"mounts" => Box::new(ProcMountsFile::new_node(task_weak)),
257 b"oom_adj" => Box::new(OomAdjFile::new_node(task_weak)),
258 b"oom_score" => Box::new(OomScoreFile::new_node(task_weak)),
259 b"oom_score_adj" => Box::new(OomScoreAdjFile::new_node(task_weak)),
260 b"timerslack_ns" => Box::new(TimerslackNsFile::new_node(task_weak)),
261 b"wchan" => Box::new(BytesFile::new_node(b"0".to_vec())),
262 b"clear_refs" => Box::new(ClearRefsFile::new_node(task_weak)),
263 b"pagemap" => Box::new(PtraceCheckedNode::new_node(
264 task_weak,
265 PTRACE_MODE_READ_FSCREDS,
266 |_, _, _| Ok(StubEmptyFile::new(bug_ref!("https://fxbug.dev/452096300"))),
267 )),
268 b"task" => {
269 let task = self.task_weak.upgrade().ok_or_else(|| errno!(ESRCH))?;
270 Box::new(TaskListDirectory { thread_group: Arc::downgrade(&task.thread_group()) })
271 }
272 name => unreachable!(
273 "entry \"{:?}\" should be supported to keep in sync with task_entries()",
274 name
275 ),
276 };
277
278 Ok(fs.create_node(ino, ops, FsNodeInfo::new(mode, creds)))
279 }
280}
281
282impl CloseFreeSafe for TaskDirectory {}
284impl FileOps for TaskDirectory {
285 fileops_impl_directory!();
286 fileops_impl_noop_sync!();
287 fileops_impl_unbounded_seek!();
288
289 fn readdir(
290 &self,
291 _locked: &mut Locked<FileOpsCore>,
292 file: &FileObject,
293 _current_task: &CurrentTask,
294 sink: &mut dyn DirentSink,
295 ) -> Result<(), Errno> {
296 emit_dotdot(file, sink)?;
297
298 for (index, (name, mode)) in
301 task_entries(self.scope).into_iter().enumerate().skip(sink.offset() as usize - 2)
302 {
303 sink.add(
304 self.inode_range.start + index as ino_t,
305 sink.offset() + 1,
306 DirectoryEntryType::from_mode(mode),
307 name.as_ref(),
308 )?;
309 }
310 Ok(())
311 }
312
313 fn as_thread_group_key(&self, _file: &FileObject) -> Result<ThreadGroupKey, Errno> {
314 let task = self.task_weak.upgrade().ok_or_else(|| errno!(ESRCH))?;
315 Ok(task.thread_group().into())
316 }
317}
318
319pub fn pid_directory(
321 current_task: &CurrentTask,
322 fs: &FileSystemHandle,
323 task: &TempRef<'_, Task>,
324) -> FsNodeHandle {
325 let fs_node = TaskDirectory::new(fs, task, TaskEntryScope::ThreadGroup);
328
329 security::task_to_fs_node(current_task, task, &fs_node);
330 fs_node
331}
332
333fn tid_directory(fs: &FileSystemHandle, task: &TempRef<'_, Task>) -> FsNodeHandle {
335 TaskDirectory::new(fs, task, TaskEntryScope::Task)
336}
337
338struct FdDirectory {
343 task: WeakRef<Task>,
344}
345
346impl FdDirectory {
347 fn new(task: WeakRef<Task>) -> Self {
348 Self { task }
349 }
350}
351
352impl FsNodeOps for FdDirectory {
353 fs_node_impl_dir_readonly!();
354
355 fn create_file_ops(
356 &self,
357 _locked: &mut Locked<FileOpsCore>,
358 _node: &FsNode,
359 _current_task: &CurrentTask,
360 _flags: OpenFlags,
361 ) -> Result<Box<dyn FileOps>, Errno> {
362 Ok(VecDirectory::new_file(fds_to_directory_entries(
363 Task::from_weak(&self.task)?.files.get_all_fds(),
364 )))
365 }
366
367 fn lookup(
368 &self,
369 _locked: &mut Locked<FileOpsCore>,
370 node: &FsNode,
371 _current_task: &CurrentTask,
372 name: &FsStr,
373 ) -> Result<FsNodeHandle, Errno> {
374 let fd = FdNumber::from_fs_str(name).map_err(|_| errno!(ENOENT))?;
375 let task = Task::from_weak(&self.task)?;
376 let file = task.files.get_allowing_opath(fd).map_err(|_| errno!(ENOENT))?;
378 let mode = FileMode::IFLNK | Access::from_open_flags(file.flags()).user_mode();
380 let task_reference = self.task.clone();
381 Ok(node.fs().create_node_and_allocate_node_id(
382 CallbackSymlinkNode::new(move || {
383 let task = Task::from_weak(&task_reference)?;
384 let file = task.files.get_allowing_opath(fd).map_err(|_| errno!(ENOENT))?;
385 Ok(SymlinkTarget::Node(file.name.to_passive()))
386 }),
387 FsNodeInfo::new(mode, task.real_fscred()),
388 ))
389 }
390}
391
392const NS_ENTRIES: &[&str] = &[
393 "cgroup",
394 "ipc",
395 "mnt",
396 "net",
397 "pid",
398 "pid_for_children",
399 "time",
400 "time_for_children",
401 "user",
402 "uts",
403];
404
405struct AttrNode {
407 attr: security::ProcAttr,
408 task: WeakRef<Task>,
409}
410
411impl AttrNode {
412 fn new(task: WeakRef<Task>, attr: security::ProcAttr) -> impl FsNodeOps {
413 SimpleFileNode::new(move |_, _| Ok(AttrNode { attr, task: task.clone() }))
414 }
415}
416
417impl FileOps for AttrNode {
418 fileops_impl_seekable!();
419 fileops_impl_noop_sync!();
420
421 fn writes_update_seek_offset(&self) -> bool {
422 false
423 }
424
425 fn read(
426 &self,
427 _locked: &mut Locked<FileOpsCore>,
428 _file: &FileObject,
429 current_task: &CurrentTask,
430 offset: usize,
431 data: &mut dyn OutputBuffer,
432 ) -> Result<usize, Errno> {
433 let task = Task::from_weak(&self.task)?;
434 let response = security::get_procattr(current_task, &task, self.attr)?;
435 data.write(&response[offset..])
436 }
437
438 fn write(
439 &self,
440 _locked: &mut Locked<FileOpsCore>,
441 _file: &FileObject,
442 current_task: &CurrentTask,
443 offset: usize,
444 data: &mut dyn InputBuffer,
445 ) -> Result<usize, Errno> {
446 let task = Task::from_weak(&self.task)?;
447
448 if current_task.temp_task() != task {
450 return error!(EPERM);
451 }
452 if offset != 0 {
453 return error!(EINVAL);
454 }
455
456 let data = data.read_all()?;
457 let data_len = data.len();
458 security::set_procattr(current_task, self.attr, data.as_slice())?;
459 Ok(data_len)
460 }
461}
462
463struct NsDirectory {
465 task: WeakRef<Task>,
466}
467
468impl FsNodeOps for NsDirectory {
469 fs_node_impl_dir_readonly!();
470
471 fn create_file_ops(
472 &self,
473 _locked: &mut Locked<FileOpsCore>,
474 _node: &FsNode,
475 _current_task: &CurrentTask,
476 _flags: OpenFlags,
477 ) -> Result<Box<dyn FileOps>, Errno> {
478 Ok(VecDirectory::new_file(
481 NS_ENTRIES
482 .iter()
483 .map(|&name| VecDirectoryEntry {
484 entry_type: DirectoryEntryType::LNK,
485 name: FsString::from(name),
486 inode: None,
487 })
488 .collect(),
489 ))
490 }
491
492 fn lookup(
493 &self,
494 _locked: &mut Locked<FileOpsCore>,
495 node: &FsNode,
496 current_task: &CurrentTask,
497 name: &FsStr,
498 ) -> Result<FsNodeHandle, Errno> {
499 let name = String::from_utf8(name.to_vec()).map_err(|_| errno!(ENOENT))?;
504 let mut elements = name.split(':');
505 let ns = elements.next().expect("name must not be empty");
506 if !NS_ENTRIES.contains(&ns) {
508 return error!(ENOENT);
509 }
510
511 let task = Task::from_weak(&self.task)?;
512 if let Some(id) = elements.next() {
513 static NS_IDENTIFIER_RE: LazyLock<Regex> =
515 LazyLock::new(|| Regex::new("^\\[[0-9]+\\]$").unwrap());
516 if !NS_IDENTIFIER_RE.is_match(id) {
517 return error!(ENOENT);
518 }
519 let node_info = || FsNodeInfo::new(mode!(IFREG, 0o444), task.real_fscred());
520 let fallback = || {
521 node.fs().create_node_and_allocate_node_id(BytesFile::new_node(vec![]), node_info())
522 };
523 Ok(match ns {
524 "cgroup" => {
525 track_stub!(TODO("https://fxbug.dev/297313673"), "cgroup namespaces");
526 fallback()
527 }
528 "ipc" => {
529 track_stub!(TODO("https://fxbug.dev/297313673"), "ipc namespaces");
530 fallback()
531 }
532 "mnt" => node.fs().create_node_and_allocate_node_id(
533 current_task.task.fs().namespace(),
534 node_info(),
535 ),
536 "net" => {
537 track_stub!(TODO("https://fxbug.dev/297313673"), "net namespaces");
538 fallback()
539 }
540 "pid" => {
541 track_stub!(TODO("https://fxbug.dev/297313673"), "pid namespaces");
542 fallback()
543 }
544 "pid_for_children" => {
545 track_stub!(TODO("https://fxbug.dev/297313673"), "pid_for_children namespaces");
546 fallback()
547 }
548 "time" => {
549 track_stub!(TODO("https://fxbug.dev/297313673"), "time namespaces");
550 fallback()
551 }
552 "time_for_children" => {
553 track_stub!(
554 TODO("https://fxbug.dev/297313673"),
555 "time_for_children namespaces"
556 );
557 fallback()
558 }
559 "user" => {
560 track_stub!(TODO("https://fxbug.dev/297313673"), "user namespaces");
561 fallback()
562 }
563 "uts" => {
564 track_stub!(TODO("https://fxbug.dev/297313673"), "uts namespaces");
565 fallback()
566 }
567 _ => return error!(ENOENT),
568 })
569 } else {
570 let id = current_task.task.fs().namespace().id;
572 Ok(node.fs().create_node_and_allocate_node_id(
573 CallbackSymlinkNode::new(move || {
574 Ok(SymlinkTarget::Path(format!("{name}:[{id}]").into()))
575 }),
576 FsNodeInfo::new(mode!(IFLNK, 0o7777), task.real_fscred()),
577 ))
578 }
579 }
580}
581
582struct FdInfoDirectory {
588 task: WeakRef<Task>,
589}
590
591impl FdInfoDirectory {
592 fn new(task: WeakRef<Task>) -> Self {
593 Self { task }
594 }
595}
596
597impl FsNodeOps for FdInfoDirectory {
598 fs_node_impl_dir_readonly!();
599
600 fn create_file_ops(
601 &self,
602 _locked: &mut Locked<FileOpsCore>,
603 _node: &FsNode,
604 _current_task: &CurrentTask,
605 _flags: OpenFlags,
606 ) -> Result<Box<dyn FileOps>, Errno> {
607 Ok(VecDirectory::new_file(fds_to_directory_entries(
608 Task::from_weak(&self.task)?.files.get_all_fds(),
609 )))
610 }
611
612 fn lookup(
613 &self,
614 locked: &mut Locked<FileOpsCore>,
615 node: &FsNode,
616 current_task: &CurrentTask,
617 name: &FsStr,
618 ) -> Result<FsNodeHandle, Errno> {
619 let task = Task::from_weak(&self.task)?;
620 let fd = FdNumber::from_fs_str(name).map_err(|_| errno!(ENOENT))?;
621 let file = task.files.get_allowing_opath(fd).map_err(|_| errno!(ENOENT))?;
622 let pos = *file.offset.lock();
623 let flags = file.flags();
624 let mut data = format!("pos:\t{}\nflags:\t0{:o}\n", pos, flags.bits()).into_bytes();
625 if let Some(extra_fdinfo) = file.extra_fdinfo(locked, current_task) {
626 data.extend_from_slice(extra_fdinfo.as_slice());
627 }
628 Ok(node.fs().create_node_and_allocate_node_id(
629 BytesFile::new_node(data),
630 FsNodeInfo::new(mode!(IFREG, 0o444), task.real_fscred()),
631 ))
632 }
633}
634
635fn fds_to_directory_entries(fds: Vec<FdNumber>) -> Vec<VecDirectoryEntry> {
636 fds.into_iter()
637 .map(|fd| VecDirectoryEntry {
638 entry_type: DirectoryEntryType::DIR,
639 name: fd.raw().to_string().into(),
640 inode: None,
641 })
642 .collect()
643}
644
645struct TaskListDirectory {
647 thread_group: Weak<ThreadGroup>,
648}
649
650impl TaskListDirectory {
651 fn thread_group(&self) -> Result<Arc<ThreadGroup>, Errno> {
652 self.thread_group.upgrade().ok_or_else(|| errno!(ESRCH))
653 }
654}
655
656impl FsNodeOps for TaskListDirectory {
657 fs_node_impl_dir_readonly!();
658
659 fn create_file_ops(
660 &self,
661 _locked: &mut Locked<FileOpsCore>,
662 _node: &FsNode,
663 _current_task: &CurrentTask,
664 _flags: OpenFlags,
665 ) -> Result<Box<dyn FileOps>, Errno> {
666 Ok(VecDirectory::new_file(
667 self.thread_group()?
668 .read()
669 .task_ids()
670 .map(|tid| VecDirectoryEntry {
671 entry_type: DirectoryEntryType::DIR,
672 name: tid.to_string().into(),
673 inode: None,
674 })
675 .collect(),
676 ))
677 }
678
679 fn lookup(
680 &self,
681 _locked: &mut Locked<FileOpsCore>,
682 node: &FsNode,
683 _current_task: &CurrentTask,
684 name: &FsStr,
685 ) -> Result<FsNodeHandle, Errno> {
686 let thread_group = self.thread_group()?;
687 let tid = std::str::from_utf8(name)
688 .map_err(|_| errno!(ENOENT))?
689 .parse::<pid_t>()
690 .map_err(|_| errno!(ENOENT))?;
691 if !thread_group.read().contains_task(tid) {
693 return error!(ENOENT);
694 }
695
696 let pid_state = thread_group.kernel.pids.read();
697 let weak_task = pid_state.get_task(tid);
698 let task = weak_task.upgrade().ok_or_else(|| errno!(ENOENT))?;
699 std::mem::drop(pid_state);
700
701 Ok(tid_directory(&node.fs(), &task))
702 }
703}
704
705#[derive(Clone)]
706struct CgroupFile(WeakRef<Task>);
707impl CgroupFile {
708 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
709 DynamicFile::new_node(Self(task))
710 }
711}
712impl DynamicFileSource for CgroupFile {
713 fn generate(
714 &self,
715 _current_task: &CurrentTask,
716 sink: &mut DynamicFileBuf,
717 ) -> Result<(), Errno> {
718 let task = Task::from_weak(&self.0)?;
719 let cgroup = task.kernel().cgroups.cgroup2.get_cgroup(task.thread_group());
720 let path = path_from_root(cgroup)?;
721 sink.write(format!("0::{}\n", path).as_bytes());
722 Ok(())
723 }
724}
725
726fn fill_buf_from_addr_range(
727 task: &Task,
728 range_start: UserAddress,
729 range_end: UserAddress,
730 sink: &mut DynamicFileBuf,
731) -> Result<(), Errno> {
732 #[allow(clippy::manual_saturating_arithmetic)]
733 let len = range_end.ptr().checked_sub(range_start.ptr()).unwrap_or(0);
734 let buf = task.read_memory_partial_to_vec(range_start, len)?;
738 sink.write(&buf[..]);
739 Ok(())
740}
741
742#[derive(Clone)]
744pub struct CmdlineFile {
745 task: WeakRef<Task>,
746}
747impl CmdlineFile {
748 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
749 DynamicFile::new_node(Self { task })
750 }
751}
752impl DynamicFileSource for CmdlineFile {
753 fn generate(
754 &self,
755 _current_task: &CurrentTask,
756 sink: &mut DynamicFileBuf,
757 ) -> Result<(), Errno> {
758 let Some(task) = self.task.upgrade() else {
760 return Ok(());
761 };
762 let Ok(mm) = task.mm() else {
764 return Ok(());
765 };
766 let (start, end) = {
767 let mm_state = mm.state.read();
768 (mm_state.argv_start, mm_state.argv_end)
769 };
770 fill_buf_from_addr_range(&task, start, end, sink)
771 }
772}
773
774struct PtraceCheckedNode {}
775
776impl PtraceCheckedNode {
777 pub fn new_node<F, O>(
778 task: WeakRef<Task>,
779 mode: PtraceAccessMode,
780 create_ops: F,
781 ) -> impl FsNodeOps
782 where
783 F: Fn(&mut Locked<FileOpsCore>, &CurrentTask, TempRef<'_, Task>) -> Result<O, Errno>
784 + Send
785 + Sync
786 + 'static,
787 O: FileOps,
788 {
789 SimpleFileNode::new(move |locked, current_task: &CurrentTask| {
790 let task = Task::from_weak(&task)?;
791 if task.mm().is_ok() {
793 current_task
794 .check_ptrace_access_mode(locked, mode, &task)
795 .map_err(|_| errno!(EACCES))?;
796 }
797 create_ops(locked, current_task, task)
798 })
799 }
800}
801
802#[derive(Clone)]
804pub struct EnvironFile {
805 task: WeakRef<Task>,
806}
807impl EnvironFile {
808 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
809 PtraceCheckedNode::new_node(task, PTRACE_MODE_READ_FSCREDS, |_, _, task| {
810 Ok(DynamicFile::new(Self { task: task.into() }))
811 })
812 }
813}
814impl DynamicFileSource for EnvironFile {
815 fn generate(
816 &self,
817 _current_task: &CurrentTask,
818 sink: &mut DynamicFileBuf,
819 ) -> Result<(), Errno> {
820 let task = Task::from_weak(&self.task)?;
821 let Ok(mm) = task.mm() else {
823 return Ok(());
824 };
825 let (start, end) = {
826 let mm_state = mm.state.read();
827 (mm_state.environ_start, mm_state.environ_end)
828 };
829 fill_buf_from_addr_range(&task, start, end, sink)
830 }
831}
832
833#[derive(Clone)]
835pub struct AuxvFile {
836 task: WeakRef<Task>,
837}
838impl AuxvFile {
839 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
840 PtraceCheckedNode::new_node(task, PTRACE_MODE_READ_FSCREDS, |_, _, task| {
841 Ok(DynamicFile::new(Self { task: task.into() }))
842 })
843 }
844}
845impl DynamicFileSource for AuxvFile {
846 fn generate(
847 &self,
848 _current_task: &CurrentTask,
849 sink: &mut DynamicFileBuf,
850 ) -> Result<(), Errno> {
851 let task = Task::from_weak(&self.task)?;
852 let Ok(mm) = task.mm() else {
854 return Ok(());
855 };
856 let (start, end) = {
857 let mm_state = mm.state.read();
858 (mm_state.auxv_start, mm_state.auxv_end)
859 };
860 fill_buf_from_addr_range(&task, start, end, sink)
861 }
862}
863
864pub struct CommFile {
866 task: WeakRef<Task>,
867 info: TaskPersistentInfo,
868}
869impl CommFile {
870 pub fn new_node(task: WeakRef<Task>, info: TaskPersistentInfo) -> impl FsNodeOps {
871 SimpleFileNode::new(move |_, _| {
872 Ok(DynamicFile::new(CommFile { task: task.clone(), info: info.clone() }))
873 })
874 }
875}
876
877impl DynamicFileSource for CommFile {
878 fn generate(
879 &self,
880 _current_task: &CurrentTask,
881 sink: &mut DynamicFileBuf,
882 ) -> Result<(), Errno> {
883 sink.write(self.info.command_guard().comm_name());
884 sink.write(b"\n");
885 Ok(())
886 }
887
888 fn write(
889 &self,
890 _locked: &mut Locked<FileOpsCore>,
891 current_task: &CurrentTask,
892 _offset: usize,
893 data: &mut dyn InputBuffer,
894 ) -> Result<usize, Errno> {
895 let task = Task::from_weak(&self.task)?;
896 if !Arc::ptr_eq(&task.thread_group(), ¤t_task.thread_group()) {
897 return error!(EINVAL);
898 }
899 let bytes = data.read_all()?;
902 task.set_command_name(TaskCommand::new(&bytes));
903 Ok(bytes.len())
904 }
905}
906
907#[derive(Clone)]
909pub struct IoFile {}
910impl IoFile {
911 pub fn new_node() -> impl FsNodeOps {
912 DynamicFile::new_node(Self {})
913 }
914}
915impl DynamicFileSource for IoFile {
916 fn generate(
917 &self,
918 _current_task: &CurrentTask,
919 sink: &mut DynamicFileBuf,
920 ) -> Result<(), Errno> {
921 track_stub!(TODO("https://fxbug.dev/322874250"), "/proc/pid/io");
922 sink.write(b"rchar: 0\n");
923 sink.write(b"wchar: 0\n");
924 sink.write(b"syscr: 0\n");
925 sink.write(b"syscw: 0\n");
926 sink.write(b"read_bytes: 0\n");
927 sink.write(b"write_bytes: 0\n");
928 sink.write(b"cancelled_write_bytes: 0\n");
929 Ok(())
930 }
931}
932
933#[derive(Clone)]
935pub struct LimitsFile(WeakRef<Task>);
936impl LimitsFile {
937 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
938 DynamicFile::new_node(Self(task))
939 }
940}
941impl DynamicFileSource for LimitsFile {
942 fn generate_locked(
943 &self,
944 locked: &mut Locked<FileOpsCore>,
945 _current_task: &CurrentTask,
946 sink: &mut DynamicFileBuf,
947 ) -> Result<(), Errno> {
948 let task = Task::from_weak(&self.0)?;
949 let limits = task.thread_group().limits.lock(locked);
950
951 let write_limit = |sink: &mut DynamicFileBuf, value| {
952 if value == RLIM_INFINITY as u64 {
953 sink.write(format!("{:<20}", "unlimited").as_bytes());
954 } else {
955 sink.write(format!("{:<20}", value).as_bytes());
956 }
957 };
958 sink.write(
959 format!("{:<25}{:<20}{:<20}{:<10}\n", "Limit", "Soft Limit", "Hard Limit", "Units")
960 .as_bytes(),
961 );
962 for resource in Resource::ALL {
963 let desc = resource.desc();
964 let limit = limits.get(resource);
965 sink.write(format!("{:<25}", desc.name).as_bytes());
966 write_limit(sink, limit.rlim_cur);
967 write_limit(sink, limit.rlim_max);
968 if !desc.unit.is_empty() {
969 sink.write(format!("{:<10}", desc.unit).as_bytes());
970 }
971 sink.write(b"\n");
972 }
973 Ok(())
974 }
975}
976
977pub struct MemFile {
979 mm: Weak<MemoryManager>,
980
981 task: WeakRef<Task>,
985}
986
987impl MemFile {
988 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
989 PtraceCheckedNode::new_node(task, PTRACE_MODE_ATTACH_REALCREDS, |_, _, task| {
990 let mm = task.mm().ok().as_ref().map(Arc::downgrade).unwrap_or_default();
991 Ok(Self { mm, task: task.into() })
992 })
993 }
994}
995
996impl FileOps for MemFile {
997 fileops_impl_noop_sync!();
998
999 fn is_seekable(&self) -> bool {
1000 true
1001 }
1002
1003 fn seek(
1004 &self,
1005 _locked: &mut Locked<FileOpsCore>,
1006 _file: &FileObject,
1007 _current_task: &CurrentTask,
1008 current_offset: off_t,
1009 target: SeekTarget,
1010 ) -> Result<off_t, Errno> {
1011 default_seek(current_offset, target, || error!(EINVAL))
1012 }
1013
1014 fn read(
1015 &self,
1016 _locked: &mut Locked<FileOpsCore>,
1017 _file: &FileObject,
1018 current_task: &CurrentTask,
1019 offset: usize,
1020 data: &mut dyn OutputBuffer,
1021 ) -> Result<usize, Errno> {
1022 let Some(_task) = self.task.upgrade() else {
1023 return Ok(0);
1024 };
1025 let Some(mm) = self.mm.upgrade() else {
1026 return Ok(0);
1027 };
1028 let mut addr = UserAddress::from(offset as u64);
1029 data.write_each(&mut |bytes| {
1030 let read_bytes = if current_task.has_same_address_space(Some(&mm)) {
1031 current_task.read_memory_partial(addr, bytes)
1032 } else {
1033 mm.syscall_read_memory_partial(addr, bytes)
1034 }
1035 .map_err(|_| errno!(EIO))?;
1036 let actual = read_bytes.len();
1037 addr = (addr + actual)?;
1038 Ok(actual)
1039 })
1040 }
1041
1042 fn write(
1043 &self,
1044 _locked: &mut Locked<FileOpsCore>,
1045 _file: &FileObject,
1046 current_task: &CurrentTask,
1047 offset: usize,
1048 data: &mut dyn InputBuffer,
1049 ) -> Result<usize, Errno> {
1050 let Some(_task) = self.task.upgrade() else {
1051 return Ok(0);
1052 };
1053 let Some(mm) = self.mm.upgrade() else {
1054 return Ok(0);
1055 };
1056 let addr = UserAddress::from(offset as u64);
1057 let mut written = 0;
1058 let result = data.peek_each(&mut |bytes| {
1059 let actual = if current_task.has_same_address_space(Some(&mm)) {
1060 current_task.write_memory_partial((addr + written)?, bytes)
1061 } else {
1062 mm.syscall_write_memory_partial((addr + written)?, bytes)
1063 }
1064 .map_err(|_| errno!(EIO))?;
1065 written += actual;
1066 Ok(actual)
1067 });
1068 data.advance(written)?;
1069 result
1070 }
1071}
1072
1073#[derive(Clone)]
1074pub struct StatFile {
1075 task: WeakRef<Task>,
1076 scope: TaskEntryScope,
1077}
1078
1079impl StatFile {
1080 pub fn new_node(task: WeakRef<Task>, scope: TaskEntryScope) -> impl FsNodeOps {
1081 DynamicFile::new_node(Self { task, scope })
1082 }
1083}
1084impl DynamicFileSource for StatFile {
1085 fn generate_locked(
1086 &self,
1087 locked: &mut Locked<FileOpsCore>,
1088 current_task: &CurrentTask,
1089 sink: &mut DynamicFileBuf,
1090 ) -> Result<(), Errno> {
1091 let task = Task::from_weak(&self.task)?;
1092
1093 let pid: pid_t; let comm: TaskCommand;
1097 let state: char;
1098 let ppid: pid_t;
1099 let pgrp: pid_t; let session: pid_t;
1101 let tty_nr: i32;
1102 let tpgid: i32 = 0;
1103 let flags: u32 = 0;
1104 let minflt: u64 = 0; let cminflt: u64 = 0;
1106 let majflt: u64 = 0;
1107 let cmajflt: u64 = 0;
1108 let utime: i64;
1109 let stime: i64; let cutime: i64;
1111 let cstime: i64;
1112 let priority: i64 = 0;
1113 let nice: i64;
1114 let num_threads: i64; let itrealvalue: i64 = 0;
1116 let mut starttime: u64 = 0;
1117 let mut vsize: usize = 0;
1118 let mut rss: usize = 0;
1119 let mut rsslim: u64 = 0; let mut startcode: u64 = 0;
1121 let mut endcode: u64 = 0;
1122 let mut startstack: usize = 0;
1123 let mut kstkesp: u64 = 0;
1124 let mut kstkeip: u64 = 0; let signal: u64 = 0;
1126 let blocked: u64 = 0;
1127 let siginore: u64 = 0;
1128 let sigcatch: u64 = 0;
1129 let mut wchan: u64 = 0; let nswap: u64 = 0;
1131 let cnswap: u64 = 0;
1132 let exit_signal: i32 = 0;
1133 let processor: i32 = 0;
1134 let rt_priority: u32 = 0; let policy: u32 = 0;
1136 let delayacct_blkio_ticks: u64 = 0;
1137 let guest_time: u64 = 0;
1138 let cguest_time: i64 = 0;
1139 let mut start_data: u64 = 0; let mut end_data: u64 = 0;
1141 let mut start_brk: u64 = 0;
1142 let mut arg_start: usize = 0;
1143 let mut arg_end: usize = 0;
1144 let mut env_start: usize = 0; let mut env_end: usize = 0;
1146 let mut exit_code: i32 = 0;
1147
1148 pid = task.get_tid();
1149 comm = task.command();
1150 state = task.state_code().code_char();
1151 nice = task.read().scheduler_state.normal_priority().as_nice() as i64;
1152
1153 {
1154 let thread_group = task.thread_group().read();
1155 ppid = thread_group.get_ppid();
1156 pgrp = thread_group.process_group.leader;
1157 session = thread_group.process_group.session.leader;
1158
1159 {
1161 let session = thread_group.process_group.session.read();
1162 tty_nr = session
1163 .controlling_terminal
1164 .as_ref()
1165 .map(|t| t.terminal.device().bits())
1166 .unwrap_or(0) as i32;
1167 }
1168
1169 cutime = duration_to_scheduler_clock(thread_group.children_time_stats.user_time);
1170 cstime = duration_to_scheduler_clock(thread_group.children_time_stats.system_time);
1171
1172 num_threads = thread_group.tasks_count() as i64;
1173 }
1174
1175 let time_stats = match self.scope {
1176 TaskEntryScope::Task => task.time_stats(),
1177 TaskEntryScope::ThreadGroup => task.thread_group().time_stats(),
1178 };
1179 utime = duration_to_scheduler_clock(time_stats.user_time);
1180 stime = duration_to_scheduler_clock(time_stats.system_time);
1181
1182 if let Ok(info) = task.thread_group().process.info() {
1183 starttime =
1184 duration_to_scheduler_clock(info.start_time - zx::MonotonicInstant::ZERO) as u64;
1185 }
1186
1187 if let Ok(mm) = task.mm() {
1188 let mem_stats = mm.get_stats(current_task);
1189 let page_size = *PAGE_SIZE as usize;
1190 vsize = mem_stats.vm_size;
1191 rss = mem_stats.vm_rss / page_size;
1192 rsslim = task.thread_group().limits.lock(locked).get(Resource::RSS).rlim_max;
1193
1194 {
1195 let mm_state = mm.state.read();
1196 startstack = mm_state.stack_start.ptr();
1197 arg_start = mm_state.argv_start.ptr();
1198 arg_end = mm_state.argv_end.ptr();
1199 env_start = mm_state.environ_start.ptr();
1200 env_end = mm_state.environ_end.ptr();
1201 }
1202 }
1203
1204 if !current_task
1208 .check_ptrace_access_mode(locked, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT, &task)
1209 .is_ok()
1210 {
1211 startcode = 1;
1212 endcode = 1;
1213 startstack = 0;
1214 kstkesp = 0;
1215 kstkeip = 0;
1216 wchan = 0;
1217 start_data = 0;
1218 end_data = 0;
1219 start_brk = 0;
1220 arg_start = 0;
1221 arg_end = 0;
1222 env_start = 0;
1223 env_end = 0;
1224 exit_code = 0;
1225 }
1226
1227 writeln!(
1228 sink,
1229 "{pid} ({comm}) {state} {ppid} {pgrp} {session} {tty_nr} {tpgid} {flags} {minflt} {cminflt} {majflt} {cmajflt} {utime} {stime} {cutime} {cstime} {priority} {nice} {num_threads} {itrealvalue} {starttime} {vsize} {rss} {rsslim} {startcode} {endcode} {startstack} {kstkesp} {kstkeip} {signal} {blocked} {siginore} {sigcatch} {wchan} {nswap} {cnswap} {exit_signal} {processor} {rt_priority} {policy} {delayacct_blkio_ticks} {guest_time} {cguest_time} {start_data} {end_data} {start_brk} {arg_start} {arg_end} {env_start} {env_end} {exit_code}"
1230 )?;
1231
1232 Ok(())
1233 }
1234}
1235
1236#[derive(Clone)]
1237pub struct StatmFile {
1238 task: WeakRef<Task>,
1239}
1240impl StatmFile {
1241 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1242 DynamicFile::new_node(Self { task })
1243 }
1244}
1245impl DynamicFileSource for StatmFile {
1246 fn generate(&self, current_task: &CurrentTask, sink: &mut DynamicFileBuf) -> Result<(), Errno> {
1247 let task = Task::from_weak(&self.task)?;
1249 let mem_stats = match task.mm() {
1250 Ok(mm) => mm.get_stats(current_task),
1251 Err(_) => Default::default(),
1252 };
1253 let page_size = *PAGE_SIZE as usize;
1254
1255 writeln!(
1257 sink,
1258 "{} {} {} {} 0 {} 0",
1259 mem_stats.vm_size / page_size,
1260 mem_stats.vm_rss / page_size,
1261 mem_stats.rss_shared / page_size,
1262 mem_stats.vm_exe / page_size,
1263 (mem_stats.vm_data + mem_stats.vm_stack) / page_size
1264 )?;
1265 Ok(())
1266 }
1267}
1268
1269#[derive(Clone)]
1270pub struct StatusFile(WeakRef<Task>);
1271impl StatusFile {
1272 pub fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1273 DynamicFile::new_node(Self(task))
1274 }
1275}
1276impl DynamicFileSource for StatusFile {
1277 fn generate(&self, current_task: &CurrentTask, sink: &mut DynamicFileBuf) -> Result<(), Errno> {
1278 let task = &self.0.upgrade();
1279 let (tgid, pid, creds_string) = {
1280 if let Some(task) = task {
1281 track_stub!(TODO("https://fxbug.dev/297440106"), "/proc/pid/status zombies");
1282 write!(sink, "Name:\t")?;
1286 sink.write(task.persistent_info.command_guard().comm_name());
1287 let creds = task.persistent_info.real_creds();
1288 (
1289 Some(task.persistent_info.pid()),
1290 Some(task.persistent_info.tid()),
1291 Some(format!(
1292 "Uid:\t{}\t{}\t{}\t{}\nGid:\t{}\t{}\t{}\t{}\nGroups:\t{}",
1293 creds.uid,
1294 creds.euid,
1295 creds.saved_uid,
1296 creds.fsuid,
1297 creds.gid,
1298 creds.egid,
1299 creds.saved_gid,
1300 creds.fsgid,
1301 creds.groups.iter().map(|n| n.to_string()).join(" ")
1302 )),
1303 )
1304 } else {
1305 (None, None, None)
1306 }
1307 };
1308
1309 writeln!(sink)?;
1310
1311 if let Some(task) = task {
1312 writeln!(sink, "Umask:\t0{:03o}", task.fs().umask().bits())?;
1313 let task_state = task.read();
1314 writeln!(sink, "SigBlk:\t{:016x}", task_state.signal_mask().0)?;
1315 writeln!(sink, "SigPnd:\t{:016x}", task_state.task_specific_pending_signals().0)?;
1316 writeln!(
1317 sink,
1318 "ShdPnd:\t{:x}",
1319 task.thread_group().pending_signals.lock().pending().0
1320 )?;
1321 writeln!(sink, "NoNewPrivs:\t{}", task_state.no_new_privs() as u8)?;
1322
1323 let creds = task.real_creds();
1325 let cap_mask = Capabilities::all_existent();
1326 writeln!(sink, "CapInh:\t{:016x}", creds.cap_inheritable & cap_mask)?;
1327 writeln!(sink, "CapPrm:\t{:016x}", creds.cap_permitted & cap_mask)?;
1328 writeln!(sink, "CapEff:\t{:016x}", creds.cap_effective & cap_mask)?;
1329 writeln!(sink, "CapBnd:\t{:016x}", creds.cap_bounding & cap_mask)?;
1330 writeln!(sink, "CapAmb:\t{:016x}", creds.cap_ambient & cap_mask)?;
1331 }
1332
1333 let state_code =
1334 if let Some(task) = task { task.state_code() } else { TaskStateCode::Zombie };
1335 writeln!(sink, "State:\t{} ({})", state_code.code_char(), state_code.name())?;
1336
1337 if let Some(tgid) = tgid {
1338 writeln!(sink, "Tgid:\t{}", tgid)?;
1339 }
1340 if let Some(pid) = pid {
1341 writeln!(sink, "Pid:\t{}", pid)?;
1342 }
1343 let (ppid, threads, tracer_pid) = if let Some(task) = task {
1344 let tracer_pid = task.read().ptrace.as_ref().map_or(0, |p| p.get_pid());
1345 let task_group = task.thread_group().read();
1346 (task_group.get_ppid(), task_group.tasks_count(), tracer_pid)
1347 } else {
1348 (1, 1, 0)
1349 };
1350 writeln!(sink, "PPid:\t{}", ppid)?;
1351 writeln!(sink, "TracerPid:\t{}", tracer_pid)?;
1352
1353 if let Some(creds_string) = creds_string {
1354 writeln!(sink, "{}", creds_string)?;
1355 }
1356
1357 if let Some(task) = task {
1358 if let Ok(mm) = task.mm() {
1359 let mem_stats = mm.get_stats(current_task);
1360 writeln!(sink, "VmSize:\t{} kB", mem_stats.vm_size / 1024)?;
1361 writeln!(sink, "VmLck:\t{} kB", mem_stats.vm_lck / 1024)?;
1362 writeln!(sink, "VmRSS:\t{} kB", mem_stats.vm_rss / 1024)?;
1363 writeln!(sink, "RssAnon:\t{} kB", mem_stats.rss_anonymous / 1024)?;
1364 writeln!(sink, "RssFile:\t{} kB", mem_stats.rss_file / 1024)?;
1365 writeln!(sink, "RssShmem:\t{} kB", mem_stats.rss_shared / 1024)?;
1366 writeln!(sink, "VmData:\t{} kB", mem_stats.vm_data / 1024)?;
1367 writeln!(sink, "VmStk:\t{} kB", mem_stats.vm_stack / 1024)?;
1368 writeln!(sink, "VmExe:\t{} kB", mem_stats.vm_exe / 1024)?;
1369 writeln!(sink, "VmSwap:\t{} kB", mem_stats.vm_swap / 1024)?;
1370 writeln!(sink, "VmHWM:\t{} kB", mem_stats.vm_rss_hwm / 1024)?;
1371 }
1372 let seccomp = task.seccomp_filter_state.get() as u8;
1374 writeln!(sink, "Seccomp:\t{}", seccomp)?;
1375 }
1376
1377 writeln!(sink, "Threads:\t{}", std::cmp::max(1, threads))?;
1379
1380 Ok(())
1381 }
1382}
1383
1384struct OomScoreFile(WeakRef<Task>);
1385
1386impl OomScoreFile {
1387 fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1388 BytesFile::new_node(Self(task))
1389 }
1390}
1391
1392impl BytesFileOps for OomScoreFile {
1393 fn read(&self, _current_task: &CurrentTask) -> Result<Cow<'_, [u8]>, Errno> {
1394 let _task = Task::from_weak(&self.0)?;
1395 track_stub!(TODO("https://fxbug.dev/322873459"), "/proc/pid/oom_score");
1396 Ok(serialize_for_file(0).into())
1397 }
1398}
1399
1400const OOM_ADJUST_MAX: i32 = uapi::OOM_ADJUST_MAX as i32;
1402const OOM_SCORE_ADJ_MAX: i32 = uapi::OOM_SCORE_ADJ_MAX as i32;
1403
1404struct OomAdjFile(WeakRef<Task>);
1405impl OomAdjFile {
1406 fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1407 BytesFile::new_node(Self(task))
1408 }
1409}
1410
1411impl BytesFileOps for OomAdjFile {
1412 fn write(&self, current_task: &CurrentTask, data: Vec<u8>) -> Result<(), Errno> {
1413 let value = parse_i32_file(&data)?;
1414 let oom_score_adj = if value == OOM_DISABLE {
1415 OOM_SCORE_ADJ_MIN
1416 } else {
1417 if !(OOM_ADJUST_MIN..=OOM_ADJUST_MAX).contains(&value) {
1418 return error!(EINVAL);
1419 }
1420 let fraction = (value - OOM_ADJUST_MIN) / (OOM_ADJUST_MAX - OOM_ADJUST_MIN);
1421 fraction * (OOM_SCORE_ADJ_MAX - OOM_SCORE_ADJ_MIN) + OOM_SCORE_ADJ_MIN
1422 };
1423 security::check_task_capable(current_task, CAP_SYS_RESOURCE)?;
1424 let task = Task::from_weak(&self.0)?;
1425 task.write().oom_score_adj = oom_score_adj;
1426 Ok(())
1427 }
1428
1429 fn read(&self, _current_task: &CurrentTask) -> Result<Cow<'_, [u8]>, Errno> {
1430 let task = Task::from_weak(&self.0)?;
1431 let oom_score_adj = task.read().oom_score_adj;
1432 let oom_adj = if oom_score_adj == OOM_SCORE_ADJ_MIN {
1433 OOM_DISABLE
1434 } else {
1435 let fraction =
1436 (oom_score_adj - OOM_SCORE_ADJ_MIN) / (OOM_SCORE_ADJ_MAX - OOM_SCORE_ADJ_MIN);
1437 fraction * (OOM_ADJUST_MAX - OOM_ADJUST_MIN) + OOM_ADJUST_MIN
1438 };
1439 Ok(serialize_for_file(oom_adj).into())
1440 }
1441}
1442
1443struct OomScoreAdjFile(WeakRef<Task>);
1444
1445impl OomScoreAdjFile {
1446 fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1447 BytesFile::new_node(Self(task))
1448 }
1449}
1450
1451impl BytesFileOps for OomScoreAdjFile {
1452 fn write(&self, current_task: &CurrentTask, data: Vec<u8>) -> Result<(), Errno> {
1453 let value = parse_i32_file(&data)?;
1454 if !(OOM_SCORE_ADJ_MIN..=OOM_SCORE_ADJ_MAX).contains(&value) {
1455 return error!(EINVAL);
1456 }
1457 security::check_task_capable(current_task, CAP_SYS_RESOURCE)?;
1458 let task = Task::from_weak(&self.0)?;
1459 task.write().oom_score_adj = value;
1460 Ok(())
1461 }
1462
1463 fn read(&self, _current_task: &CurrentTask) -> Result<Cow<'_, [u8]>, Errno> {
1464 let task = Task::from_weak(&self.0)?;
1465 let oom_score_adj = task.read().oom_score_adj;
1466 Ok(serialize_for_file(oom_score_adj).into())
1467 }
1468}
1469
1470struct TimerslackNsFile(WeakRef<Task>);
1471
1472impl TimerslackNsFile {
1473 fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1474 BytesFile::new_node(Self(task))
1475 }
1476}
1477
1478impl BytesFileOps for TimerslackNsFile {
1479 fn write(&self, current_task: &CurrentTask, data: Vec<u8>) -> Result<(), Errno> {
1480 let target_task = Task::from_weak(&self.0)?;
1481 let same_task =
1482 current_task.task.thread_group().leader == target_task.thread_group().leader;
1483 if !same_task {
1484 security::check_task_capable(current_task, CAP_SYS_NICE)?;
1485 security::check_setsched_access(current_task, &target_task)?;
1486 };
1487
1488 let value = parse_unsigned_file(&data)?;
1489 target_task.write().set_timerslack_ns(value);
1490 Ok(())
1491 }
1492
1493 fn read(&self, current_task: &CurrentTask) -> Result<Cow<'_, [u8]>, Errno> {
1494 let target_task = Task::from_weak(&self.0)?;
1495 let same_task =
1496 current_task.task.thread_group().leader == target_task.thread_group().leader;
1497 if !same_task {
1498 security::check_task_capable(current_task, CAP_SYS_NICE)?;
1499 security::check_getsched_access(current_task, &target_task)?;
1500 };
1501
1502 let timerslack_ns = target_task.read().timerslack_ns;
1503 Ok(serialize_for_file(timerslack_ns).into())
1504 }
1505}
1506
1507struct ClearRefsFile(WeakRef<Task>);
1508
1509impl ClearRefsFile {
1510 fn new_node(task: WeakRef<Task>) -> impl FsNodeOps {
1511 BytesFile::new_node(Self(task))
1512 }
1513}
1514
1515impl BytesFileOps for ClearRefsFile {
1516 fn write(&self, _current_task: &CurrentTask, _data: Vec<u8>) -> Result<(), Errno> {
1517 let _task = Task::from_weak(&self.0)?;
1518 track_stub!(TODO("https://fxbug.dev/396221597"), "/proc/pid/clear_refs");
1519 Ok(())
1520 }
1521}