ProcFS.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Singleton.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/FileSystem/Custody.h>
  11. #include <Kernel/FileSystem/FileDescription.h>
  12. #include <Kernel/FileSystem/ProcFS.h>
  13. #include <Kernel/FileSystem/VirtualFileSystem.h>
  14. #include <Kernel/Heap/kmalloc.h>
  15. #include <Kernel/Process.h>
  16. #include <Kernel/Sections.h>
  17. #include <LibC/errno_numbers.h>
  18. namespace Kernel {
  19. static Singleton<ProcFSComponentRegistry> s_the;
  20. ProcFSComponentRegistry& ProcFSComponentRegistry::the()
  21. {
  22. return *s_the;
  23. }
  24. UNMAP_AFTER_INIT void ProcFSComponentRegistry::initialize()
  25. {
  26. VERIFY(!s_the.is_initialized());
  27. s_the.ensure_instance();
  28. }
  29. UNMAP_AFTER_INIT ProcFSComponentRegistry::ProcFSComponentRegistry()
  30. : m_root_directory(ProcFSRootDirectory::must_create())
  31. {
  32. }
  33. RefPtr<ProcFS> ProcFS::create()
  34. {
  35. return adopt_ref_if_nonnull(new (nothrow) ProcFS);
  36. }
  37. ProcFS::ProcFS()
  38. : m_root_inode(ProcFSComponentRegistry::the().root_directory().to_inode(*this))
  39. {
  40. }
  41. ProcFS::~ProcFS()
  42. {
  43. }
  44. KResult ProcFS::initialize()
  45. {
  46. return KSuccess;
  47. }
  48. Inode& ProcFS::root_inode()
  49. {
  50. return *m_root_inode;
  51. }
  52. ProcFSInode::ProcFSInode(const ProcFS& fs, InodeIndex index)
  53. : Inode(const_cast<ProcFS&>(fs), index)
  54. {
  55. }
  56. ProcFSInode::~ProcFSInode()
  57. {
  58. }
  59. void ProcFSInode::flush_metadata()
  60. {
  61. }
  62. KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t)
  63. {
  64. return EROFS;
  65. }
  66. KResultOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(StringView, mode_t, dev_t, uid_t, gid_t)
  67. {
  68. return EROFS;
  69. }
  70. KResult ProcFSInode::remove_child(const StringView&)
  71. {
  72. return EROFS;
  73. }
  74. KResult ProcFSInode::chmod(mode_t)
  75. {
  76. return EPERM;
  77. }
  78. KResult ProcFSInode::chown(uid_t, gid_t)
  79. {
  80. return EPERM;
  81. }
  82. KResult ProcFSInode::truncate(u64)
  83. {
  84. return EPERM;
  85. }
  86. NonnullRefPtr<ProcFSGlobalInode> ProcFSGlobalInode::create(const ProcFS& fs, const ProcFSExposedComponent& component)
  87. {
  88. return adopt_ref(*new (nothrow) ProcFSGlobalInode(fs, component));
  89. }
  90. ProcFSGlobalInode::ProcFSGlobalInode(const ProcFS& fs, const ProcFSExposedComponent& component)
  91. : ProcFSInode(fs, component.component_index())
  92. , m_associated_component(component)
  93. {
  94. }
  95. void ProcFSGlobalInode::did_seek(FileDescription& description, off_t new_offset)
  96. {
  97. if (new_offset != 0)
  98. return;
  99. auto result = m_associated_component->refresh_data(description);
  100. if (result.is_error()) {
  101. // Subsequent calls to read will return EIO!
  102. dbgln("ProcFS: Could not refresh contents: {}", result.error());
  103. }
  104. }
  105. KResult ProcFSGlobalInode::attach(FileDescription& description)
  106. {
  107. return m_associated_component->refresh_data(description);
  108. }
  109. KResultOr<size_t> ProcFSGlobalInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* fd) const
  110. {
  111. return m_associated_component->read_bytes(offset, count, buffer, fd);
  112. }
  113. StringView ProcFSGlobalInode::name() const
  114. {
  115. return m_associated_component->name();
  116. }
  117. KResult ProcFSGlobalInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const
  118. {
  119. VERIFY_NOT_REACHED();
  120. }
  121. KResultOr<NonnullRefPtr<Inode>> ProcFSGlobalInode::lookup(StringView)
  122. {
  123. VERIFY_NOT_REACHED();
  124. }
  125. InodeMetadata ProcFSGlobalInode::metadata() const
  126. {
  127. MutexLocker locker(m_inode_lock);
  128. InodeMetadata metadata;
  129. metadata.inode = { fsid(), m_associated_component->component_index() };
  130. metadata.mode = S_IFREG | m_associated_component->required_mode();
  131. metadata.uid = m_associated_component->owner_user();
  132. metadata.gid = m_associated_component->owner_group();
  133. metadata.size = m_associated_component->size();
  134. metadata.mtime = m_associated_component->modified_time();
  135. return metadata;
  136. }
  137. KResultOr<size_t> ProcFSGlobalInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, FileDescription* fd)
  138. {
  139. return m_associated_component->write_bytes(offset, count, buffer, fd);
  140. }
  141. NonnullRefPtr<ProcFSDirectoryInode> ProcFSDirectoryInode::create(const ProcFS& procfs, const ProcFSExposedComponent& component)
  142. {
  143. return adopt_ref(*new (nothrow) ProcFSDirectoryInode(procfs, component));
  144. }
  145. ProcFSDirectoryInode::ProcFSDirectoryInode(const ProcFS& fs, const ProcFSExposedComponent& component)
  146. : ProcFSGlobalInode(fs, component)
  147. {
  148. }
  149. ProcFSDirectoryInode::~ProcFSDirectoryInode()
  150. {
  151. }
  152. InodeMetadata ProcFSDirectoryInode::metadata() const
  153. {
  154. MutexLocker locker(m_inode_lock);
  155. InodeMetadata metadata;
  156. metadata.inode = { fsid(), m_associated_component->component_index() };
  157. metadata.mode = S_IFDIR | m_associated_component->required_mode();
  158. metadata.uid = m_associated_component->owner_user();
  159. metadata.gid = m_associated_component->owner_group();
  160. metadata.size = 0;
  161. metadata.mtime = m_associated_component->modified_time();
  162. return metadata;
  163. }
  164. KResult ProcFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  165. {
  166. MutexLocker locker(procfs().m_lock);
  167. return m_associated_component->traverse_as_directory(procfs().fsid(), move(callback));
  168. }
  169. KResultOr<NonnullRefPtr<Inode>> ProcFSDirectoryInode::lookup(StringView name)
  170. {
  171. MutexLocker locker(procfs().m_lock);
  172. auto component = m_associated_component->lookup(name);
  173. if (!component)
  174. return ENOMEM;
  175. return component->to_inode(procfs());
  176. }
  177. NonnullRefPtr<ProcFSLinkInode> ProcFSLinkInode::create(const ProcFS& procfs, const ProcFSExposedComponent& component)
  178. {
  179. return adopt_ref(*new (nothrow) ProcFSLinkInode(procfs, component));
  180. }
  181. ProcFSLinkInode::ProcFSLinkInode(const ProcFS& fs, const ProcFSExposedComponent& component)
  182. : ProcFSGlobalInode(fs, component)
  183. {
  184. }
  185. InodeMetadata ProcFSLinkInode::metadata() const
  186. {
  187. MutexLocker locker(m_inode_lock);
  188. InodeMetadata metadata;
  189. metadata.inode = { fsid(), m_associated_component->component_index() };
  190. metadata.mode = S_IFLNK | m_associated_component->required_mode();
  191. metadata.uid = m_associated_component->owner_user();
  192. metadata.gid = m_associated_component->owner_group();
  193. metadata.size = 0;
  194. metadata.mtime = m_associated_component->modified_time();
  195. return metadata;
  196. }
  197. ProcFSProcessAssociatedInode::ProcFSProcessAssociatedInode(const ProcFS& fs, ProcessID associated_pid, InodeIndex determined_index)
  198. : ProcFSInode(fs, determined_index)
  199. , m_pid(associated_pid)
  200. {
  201. }
  202. KResultOr<size_t> ProcFSProcessAssociatedInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, FileDescription*)
  203. {
  204. VERIFY_NOT_REACHED();
  205. }
  206. NonnullRefPtr<ProcFSProcessDirectoryInode> ProcFSProcessDirectoryInode::create(const ProcFS& procfs, ProcessID pid)
  207. {
  208. return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessDirectoryInode(procfs, pid)).release_nonnull();
  209. }
  210. ProcFSProcessDirectoryInode::ProcFSProcessDirectoryInode(const ProcFS& procfs, ProcessID pid)
  211. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_pid_directory(pid))
  212. {
  213. }
  214. KResult ProcFSProcessDirectoryInode::attach(FileDescription&)
  215. {
  216. return KSuccess;
  217. }
  218. InodeMetadata ProcFSProcessDirectoryInode::metadata() const
  219. {
  220. MutexLocker locker(m_inode_lock);
  221. auto process = Process::from_pid(associated_pid());
  222. if (!process)
  223. return {};
  224. InodeMetadata metadata;
  225. metadata.inode = { fsid(), process->component_index() };
  226. metadata.mode = S_IFDIR | process->required_mode();
  227. metadata.uid = process->owner_user();
  228. metadata.gid = process->owner_group();
  229. metadata.size = 0;
  230. metadata.mtime = process->modified_time();
  231. return metadata;
  232. }
  233. KResultOr<size_t> ProcFSProcessDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, FileDescription*) const
  234. {
  235. VERIFY_NOT_REACHED();
  236. }
  237. KResult ProcFSProcessDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  238. {
  239. MutexLocker locker(procfs().m_lock);
  240. auto process = Process::from_pid(associated_pid());
  241. if (!process)
  242. return EINVAL;
  243. return process->traverse_as_directory(procfs().fsid(), move(callback));
  244. }
  245. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessDirectoryInode::lookup(StringView name)
  246. {
  247. MutexLocker locker(procfs().m_lock);
  248. auto process = Process::from_pid(associated_pid());
  249. if (!process)
  250. return ESRCH;
  251. if (name == "fd")
  252. return ProcFSProcessSubDirectoryInode::create(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions, associated_pid());
  253. if (name == "stacks")
  254. return ProcFSProcessSubDirectoryInode::create(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, associated_pid());
  255. if (name == "unveil")
  256. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::Unveil, associated_pid());
  257. if (name == "pledge")
  258. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::Pledge, associated_pid());
  259. if (name == "fds")
  260. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::FileDescriptions, associated_pid());
  261. if (name == "exe")
  262. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::BinaryLink, associated_pid());
  263. if (name == "cwd")
  264. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink, associated_pid());
  265. if (name == "perf_events")
  266. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents, associated_pid());
  267. if (name == "vm")
  268. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats, associated_pid());
  269. if (name == "root")
  270. return ProcFSProcessPropertyInode::create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::RootLink, associated_pid());
  271. return ENOENT;
  272. }
  273. NonnullRefPtr<ProcFSProcessSubDirectoryInode> ProcFSProcessSubDirectoryInode::create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  274. {
  275. return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessSubDirectoryInode(procfs, sub_directory_type, pid)).release_nonnull();
  276. }
  277. ProcFSProcessSubDirectoryInode::ProcFSProcessSubDirectoryInode(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  278. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_sub_directory(pid, sub_directory_type))
  279. , m_sub_directory_type(sub_directory_type)
  280. {
  281. }
  282. KResultOr<size_t> ProcFSProcessSubDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, FileDescription*) const
  283. {
  284. VERIFY_NOT_REACHED();
  285. }
  286. KResult ProcFSProcessSubDirectoryInode::attach(FileDescription&)
  287. {
  288. return KSuccess;
  289. }
  290. void ProcFSProcessSubDirectoryInode::did_seek(FileDescription&, off_t)
  291. {
  292. VERIFY_NOT_REACHED();
  293. }
  294. InodeMetadata ProcFSProcessSubDirectoryInode::metadata() const
  295. {
  296. MutexLocker locker(m_inode_lock);
  297. auto process = Process::from_pid(associated_pid());
  298. if (!process)
  299. return {};
  300. InodeMetadata metadata;
  301. metadata.inode = { fsid(), process->component_index() };
  302. metadata.mode = S_IFDIR | process->required_mode();
  303. metadata.uid = process->owner_user();
  304. metadata.gid = process->owner_group();
  305. metadata.size = 0;
  306. metadata.mtime = process->modified_time();
  307. return metadata;
  308. }
  309. KResult ProcFSProcessSubDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  310. {
  311. MutexLocker locker(procfs().m_lock);
  312. auto process = Process::from_pid(associated_pid());
  313. if (!process)
  314. return EINVAL;
  315. switch (m_sub_directory_type) {
  316. case SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions:
  317. return process->traverse_file_descriptions_directory(procfs().fsid(), move(callback));
  318. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  319. return process->traverse_stacks_directory(procfs().fsid(), move(callback));
  320. default:
  321. VERIFY_NOT_REACHED();
  322. }
  323. VERIFY_NOT_REACHED();
  324. }
  325. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessSubDirectoryInode::lookup(StringView name)
  326. {
  327. MutexLocker locker(procfs().m_lock);
  328. auto process = Process::from_pid(associated_pid());
  329. if (!process)
  330. return ESRCH;
  331. RefPtr<Inode> inode;
  332. switch (m_sub_directory_type) {
  333. case SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions:
  334. inode = process->lookup_file_descriptions_directory(procfs(), name);
  335. break;
  336. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  337. inode = process->lookup_stacks_directory(procfs(), name);
  338. break;
  339. default:
  340. VERIFY_NOT_REACHED();
  341. }
  342. if (!inode)
  343. return ENOENT;
  344. return inode.release_nonnull();
  345. }
  346. NonnullRefPtr<ProcFSProcessPropertyInode> ProcFSProcessPropertyInode::create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  347. {
  348. return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessPropertyInode(procfs, file_description_index, pid)).release_nonnull();
  349. }
  350. NonnullRefPtr<ProcFSProcessPropertyInode> ProcFSProcessPropertyInode::create_for_thread_stack(const ProcFS& procfs, ThreadID stack_thread_index, ProcessID pid)
  351. {
  352. return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessPropertyInode(procfs, stack_thread_index, pid)).release_nonnull();
  353. }
  354. NonnullRefPtr<ProcFSProcessPropertyInode> ProcFSProcessPropertyInode::create_for_pid_property(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  355. {
  356. return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessPropertyInode(procfs, main_property_type, pid)).release_nonnull();
  357. }
  358. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  359. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(pid, main_property_type))
  360. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Reserved)
  361. {
  362. m_possible_data.property_type = main_property_type;
  363. }
  364. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  365. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_file_description(pid, file_description_index))
  366. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions)
  367. {
  368. m_possible_data.property_index = file_description_index;
  369. }
  370. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, ThreadID thread_stack_index, ProcessID pid)
  371. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid, thread_stack_index))
  372. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  373. {
  374. m_possible_data.property_index = thread_stack_index.value();
  375. }
  376. KResult ProcFSProcessPropertyInode::attach(FileDescription& description)
  377. {
  378. return refresh_data(description);
  379. }
  380. void ProcFSProcessPropertyInode::did_seek(FileDescription& description, off_t offset)
  381. {
  382. if (offset != 0)
  383. return;
  384. (void)refresh_data(description);
  385. }
  386. static mode_t determine_procfs_process_inode_mode(SegmentedProcFSIndex::ProcessSubDirectory parent_sub_directory_type, SegmentedProcFSIndex::MainProcessProperty main_property)
  387. {
  388. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions)
  389. return S_IFLNK | 0400;
  390. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  391. return S_IFREG | 0400;
  392. VERIFY(parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  393. if (main_property == SegmentedProcFSIndex::MainProcessProperty::BinaryLink)
  394. return S_IFLNK | 0777;
  395. if (main_property == SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink)
  396. return S_IFLNK | 0777;
  397. if (main_property == SegmentedProcFSIndex::MainProcessProperty::RootLink)
  398. return S_IFLNK | 0777;
  399. return S_IFREG | 0400;
  400. }
  401. InodeMetadata ProcFSProcessPropertyInode::metadata() const
  402. {
  403. MutexLocker locker(m_inode_lock);
  404. auto process = Process::from_pid(associated_pid());
  405. if (!process)
  406. return {};
  407. InodeMetadata metadata;
  408. metadata.inode = { fsid(), process->component_index() };
  409. metadata.mode = determine_procfs_process_inode_mode(m_parent_sub_directory_type, m_possible_data.property_type);
  410. metadata.uid = process->owner_user();
  411. metadata.gid = process->owner_group();
  412. metadata.size = 0;
  413. metadata.mtime = process->modified_time();
  414. return metadata;
  415. }
  416. KResult ProcFSProcessPropertyInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const
  417. {
  418. VERIFY_NOT_REACHED();
  419. }
  420. KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  421. {
  422. dbgln_if(PROCFS_DEBUG, "ProcFS ProcessInformation: read_bytes offset: {} count: {}", offset, count);
  423. VERIFY(offset >= 0);
  424. VERIFY(buffer.user_or_kernel_ptr());
  425. if (!description) {
  426. KBufferBuilder builder;
  427. auto process = Process::from_pid(associated_pid());
  428. if (!process)
  429. return KResult(ESRCH);
  430. if (auto result = try_to_acquire_data(*process, builder); result.is_error())
  431. return result;
  432. auto data_buffer = builder.build();
  433. if (!data_buffer)
  434. return KResult(EFAULT);
  435. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  436. if (!buffer.write(data_buffer->data() + offset, nread))
  437. return KResult(EFAULT);
  438. return nread;
  439. }
  440. if (!description->data()) {
  441. dbgln("ProcFS Process Information: Do not have cached data!");
  442. return KResult(EIO);
  443. }
  444. MutexLocker locker(m_refresh_lock);
  445. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  446. auto& data_buffer = typed_cached_data.buffer;
  447. if (!data_buffer || (size_t)offset >= data_buffer->size())
  448. return 0;
  449. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  450. if (!buffer.write(data_buffer->data() + offset, nread))
  451. return KResult(EFAULT);
  452. return nread;
  453. }
  454. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessPropertyInode::lookup(StringView)
  455. {
  456. return EINVAL;
  457. }
  458. static KResult build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data)
  459. {
  460. cached_data.buffer = builder.build();
  461. if (!cached_data.buffer)
  462. return ENOMEM;
  463. return KSuccess;
  464. }
  465. KResult ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBufferBuilder& builder) const
  466. {
  467. // FIXME: Verify process is already ref-counted
  468. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::FileDescriptions) {
  469. if (auto result = process.procfs_get_file_description_link(m_possible_data.property_index, builder); result.is_error())
  470. return result.error();
  471. return KSuccess;
  472. }
  473. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks) {
  474. if (auto result = process.procfs_get_thread_stack(m_possible_data.property_index, builder); result.is_error())
  475. return result.error();
  476. return KSuccess;
  477. }
  478. VERIFY(m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  479. switch (m_possible_data.property_type) {
  480. case SegmentedProcFSIndex::MainProcessProperty::Unveil:
  481. return process.procfs_get_unveil_stats(builder);
  482. case SegmentedProcFSIndex::MainProcessProperty::Pledge:
  483. return process.procfs_get_pledge_stats(builder);
  484. case SegmentedProcFSIndex::MainProcessProperty::FileDescriptions:
  485. return process.procfs_get_fds_stats(builder);
  486. case SegmentedProcFSIndex::MainProcessProperty::BinaryLink:
  487. return process.procfs_get_binary_link(builder);
  488. case SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink:
  489. return process.procfs_get_current_work_directory_link(builder);
  490. case SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents:
  491. return process.procfs_get_perf_events(builder);
  492. case SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats:
  493. return process.procfs_get_virtual_memory_stats(builder);
  494. case SegmentedProcFSIndex::MainProcessProperty::RootLink:
  495. return process.procfs_get_root_link(builder);
  496. default:
  497. VERIFY_NOT_REACHED();
  498. }
  499. }
  500. KResult ProcFSProcessPropertyInode::refresh_data(FileDescription& description)
  501. {
  502. // For process-specific inodes, hold the process's ptrace lock across refresh
  503. // and refuse to load data if the process is not dumpable.
  504. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  505. auto process = Process::from_pid(associated_pid());
  506. if (!process)
  507. return KResult(ESRCH);
  508. process->ptrace_lock().lock();
  509. if (!process->is_dumpable()) {
  510. process->ptrace_lock().unlock();
  511. return EPERM;
  512. }
  513. ScopeGuard guard = [&] {
  514. process->ptrace_lock().unlock();
  515. };
  516. MutexLocker locker(m_refresh_lock);
  517. auto& cached_data = description.data();
  518. if (!cached_data) {
  519. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  520. if (!cached_data)
  521. return ENOMEM;
  522. }
  523. KBufferBuilder builder;
  524. if (auto result = try_to_acquire_data(*process, builder); result.is_error())
  525. return result;
  526. return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
  527. }
  528. }