ProcFS.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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/OpenFileDescription.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. KResultOr<NonnullRefPtr<ProcFS>> ProcFS::try_create()
  34. {
  35. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFS());
  36. }
  37. ProcFS::ProcFS()
  38. {
  39. }
  40. ProcFS::~ProcFS()
  41. {
  42. }
  43. KResult ProcFS::initialize()
  44. {
  45. m_root_inode = static_ptr_cast<ProcFSDirectoryInode>(TRY(ProcFSComponentRegistry::the().root_directory().to_inode(*this)));
  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, UserID, GroupID)
  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(UserID, GroupID)
  79. {
  80. return EPERM;
  81. }
  82. KResult ProcFSInode::truncate(u64)
  83. {
  84. return EPERM;
  85. }
  86. KResultOr<NonnullRefPtr<ProcFSGlobalInode>> ProcFSGlobalInode::try_create(const ProcFS& fs, const ProcFSExposedComponent& component)
  87. {
  88. return adopt_nonnull_ref_or_enomem(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(OpenFileDescription& 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(OpenFileDescription& 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, OpenFileDescription* 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 = 0;
  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, OpenFileDescription* fd)
  138. {
  139. return m_associated_component->write_bytes(offset, count, buffer, fd);
  140. }
  141. KResultOr<NonnullRefPtr<ProcFSDirectoryInode>> ProcFSDirectoryInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component)
  142. {
  143. return adopt_nonnull_ref_or_enomem(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 = TRY(m_associated_component->lookup(name));
  173. return component->to_inode(procfs());
  174. }
  175. KResultOr<NonnullRefPtr<ProcFSLinkInode>> ProcFSLinkInode::try_create(const ProcFS& procfs, const ProcFSExposedComponent& component)
  176. {
  177. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSLinkInode(procfs, component));
  178. }
  179. ProcFSLinkInode::ProcFSLinkInode(const ProcFS& fs, const ProcFSExposedComponent& component)
  180. : ProcFSGlobalInode(fs, component)
  181. {
  182. }
  183. InodeMetadata ProcFSLinkInode::metadata() const
  184. {
  185. MutexLocker locker(m_inode_lock);
  186. InodeMetadata metadata;
  187. metadata.inode = { fsid(), m_associated_component->component_index() };
  188. metadata.mode = S_IFLNK | m_associated_component->required_mode();
  189. metadata.uid = m_associated_component->owner_user();
  190. metadata.gid = m_associated_component->owner_group();
  191. metadata.size = 0;
  192. metadata.mtime = m_associated_component->modified_time();
  193. return metadata;
  194. }
  195. ProcFSProcessAssociatedInode::ProcFSProcessAssociatedInode(const ProcFS& fs, ProcessID associated_pid, InodeIndex determined_index)
  196. : ProcFSInode(fs, determined_index)
  197. , m_pid(associated_pid)
  198. {
  199. }
  200. KResultOr<size_t> ProcFSProcessAssociatedInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*)
  201. {
  202. VERIFY_NOT_REACHED();
  203. }
  204. KResultOr<NonnullRefPtr<ProcFSProcessDirectoryInode>> ProcFSProcessDirectoryInode::try_create(const ProcFS& procfs, ProcessID pid)
  205. {
  206. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessDirectoryInode(procfs, pid));
  207. }
  208. ProcFSProcessDirectoryInode::ProcFSProcessDirectoryInode(const ProcFS& procfs, ProcessID pid)
  209. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_pid_directory(pid))
  210. {
  211. }
  212. KResult ProcFSProcessDirectoryInode::attach(OpenFileDescription&)
  213. {
  214. return KSuccess;
  215. }
  216. InodeMetadata ProcFSProcessDirectoryInode::metadata() const
  217. {
  218. MutexLocker locker(m_inode_lock);
  219. auto process = Process::from_pid(associated_pid());
  220. if (!process)
  221. return {};
  222. auto traits = process->procfs_traits();
  223. InodeMetadata metadata;
  224. metadata.inode = { fsid(), traits->component_index() };
  225. metadata.mode = S_IFDIR | traits->required_mode();
  226. metadata.uid = traits->owner_user();
  227. metadata.gid = traits->owner_group();
  228. metadata.size = 0;
  229. metadata.mtime = traits->modified_time();
  230. return metadata;
  231. }
  232. KResultOr<size_t> ProcFSProcessDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const
  233. {
  234. VERIFY_NOT_REACHED();
  235. }
  236. KResult ProcFSProcessDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  237. {
  238. MutexLocker locker(procfs().m_lock);
  239. auto process = Process::from_pid(associated_pid());
  240. if (!process)
  241. return EINVAL;
  242. return process->procfs_traits()->traverse_as_directory(procfs().fsid(), move(callback));
  243. }
  244. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessDirectoryInode::lookup(StringView name)
  245. {
  246. MutexLocker locker(procfs().m_lock);
  247. auto process = Process::from_pid(associated_pid());
  248. if (!process)
  249. return ESRCH;
  250. if (name == "fd"sv)
  251. return TRY(ProcFSProcessSubDirectoryInode::try_create(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions, associated_pid()));
  252. if (name == "stacks"sv)
  253. return TRY(ProcFSProcessSubDirectoryInode::try_create(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, associated_pid()));
  254. if (name == "unveil"sv)
  255. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::Unveil, associated_pid()));
  256. if (name == "pledge"sv)
  257. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::Pledge, associated_pid()));
  258. if (name == "fds"sv)
  259. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::OpenFileDescriptions, associated_pid()));
  260. if (name == "exe"sv)
  261. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::BinaryLink, associated_pid()));
  262. if (name == "cwd"sv)
  263. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink, associated_pid()));
  264. if (name == "perf_events"sv)
  265. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents, associated_pid()));
  266. if (name == "vm"sv)
  267. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats, associated_pid()));
  268. return ENOENT;
  269. }
  270. KResultOr<NonnullRefPtr<ProcFSProcessSubDirectoryInode>> ProcFSProcessSubDirectoryInode::try_create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  271. {
  272. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessSubDirectoryInode(procfs, sub_directory_type, pid));
  273. }
  274. ProcFSProcessSubDirectoryInode::ProcFSProcessSubDirectoryInode(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  275. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_sub_directory(pid, sub_directory_type))
  276. , m_sub_directory_type(sub_directory_type)
  277. {
  278. }
  279. KResultOr<size_t> ProcFSProcessSubDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const
  280. {
  281. VERIFY_NOT_REACHED();
  282. }
  283. KResult ProcFSProcessSubDirectoryInode::attach(OpenFileDescription&)
  284. {
  285. return KSuccess;
  286. }
  287. void ProcFSProcessSubDirectoryInode::did_seek(OpenFileDescription&, off_t)
  288. {
  289. VERIFY_NOT_REACHED();
  290. }
  291. InodeMetadata ProcFSProcessSubDirectoryInode::metadata() const
  292. {
  293. MutexLocker locker(m_inode_lock);
  294. auto process = Process::from_pid(associated_pid());
  295. if (!process)
  296. return {};
  297. auto traits = process->procfs_traits();
  298. InodeMetadata metadata;
  299. metadata.inode = { fsid(), traits->component_index() };
  300. metadata.mode = S_IFDIR | traits->required_mode();
  301. metadata.uid = traits->owner_user();
  302. metadata.gid = traits->owner_group();
  303. metadata.size = 0;
  304. metadata.mtime = traits->modified_time();
  305. return metadata;
  306. }
  307. KResult ProcFSProcessSubDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  308. {
  309. MutexLocker locker(procfs().m_lock);
  310. auto process = Process::from_pid(associated_pid());
  311. if (!process)
  312. return EINVAL;
  313. switch (m_sub_directory_type) {
  314. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  315. return process->traverse_file_descriptions_directory(procfs().fsid(), move(callback));
  316. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  317. return process->traverse_stacks_directory(procfs().fsid(), move(callback));
  318. default:
  319. VERIFY_NOT_REACHED();
  320. }
  321. VERIFY_NOT_REACHED();
  322. }
  323. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessSubDirectoryInode::lookup(StringView name)
  324. {
  325. MutexLocker locker(procfs().m_lock);
  326. auto process = Process::from_pid(associated_pid());
  327. if (!process)
  328. return ESRCH;
  329. switch (m_sub_directory_type) {
  330. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  331. return process->lookup_file_descriptions_directory(procfs(), name);
  332. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  333. return process->lookup_stacks_directory(procfs(), name);
  334. default:
  335. VERIFY_NOT_REACHED();
  336. }
  337. }
  338. KResultOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  339. {
  340. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, file_description_index, pid));
  341. }
  342. KResultOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_thread_stack(const ProcFS& procfs, ThreadID stack_thread_index, ProcessID pid)
  343. {
  344. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, stack_thread_index, pid));
  345. }
  346. KResultOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_pid_property(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  347. {
  348. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, main_property_type, pid));
  349. }
  350. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  351. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(pid, main_property_type))
  352. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Reserved)
  353. {
  354. m_possible_data.property_type = main_property_type;
  355. }
  356. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  357. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_file_description(pid, file_description_index))
  358. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  359. {
  360. m_possible_data.property_index = file_description_index;
  361. }
  362. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, ThreadID thread_stack_index, ProcessID pid)
  363. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid, thread_stack_index))
  364. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  365. {
  366. m_possible_data.property_index = thread_stack_index.value();
  367. }
  368. KResult ProcFSProcessPropertyInode::attach(OpenFileDescription& description)
  369. {
  370. return refresh_data(description);
  371. }
  372. void ProcFSProcessPropertyInode::did_seek(OpenFileDescription& description, off_t offset)
  373. {
  374. if (offset != 0)
  375. return;
  376. (void)refresh_data(description);
  377. }
  378. static mode_t determine_procfs_process_inode_mode(SegmentedProcFSIndex::ProcessSubDirectory parent_sub_directory_type, SegmentedProcFSIndex::MainProcessProperty main_property)
  379. {
  380. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  381. return S_IFLNK | 0400;
  382. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  383. return S_IFREG | 0400;
  384. VERIFY(parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  385. if (main_property == SegmentedProcFSIndex::MainProcessProperty::BinaryLink)
  386. return S_IFLNK | 0777;
  387. if (main_property == SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink)
  388. return S_IFLNK | 0777;
  389. return S_IFREG | 0400;
  390. }
  391. InodeMetadata ProcFSProcessPropertyInode::metadata() const
  392. {
  393. MutexLocker locker(m_inode_lock);
  394. auto process = Process::from_pid(associated_pid());
  395. if (!process)
  396. return {};
  397. auto traits = process->procfs_traits();
  398. InodeMetadata metadata;
  399. metadata.inode = { fsid(), traits->component_index() };
  400. metadata.mode = determine_procfs_process_inode_mode(m_parent_sub_directory_type, m_possible_data.property_type);
  401. metadata.uid = traits->owner_user();
  402. metadata.gid = traits->owner_group();
  403. metadata.size = 0;
  404. metadata.mtime = traits->modified_time();
  405. return metadata;
  406. }
  407. KResult ProcFSProcessPropertyInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const
  408. {
  409. VERIFY_NOT_REACHED();
  410. }
  411. KResultOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
  412. {
  413. dbgln_if(PROCFS_DEBUG, "ProcFS ProcessInformation: read_bytes offset: {} count: {}", offset, count);
  414. VERIFY(offset >= 0);
  415. VERIFY(buffer.user_or_kernel_ptr());
  416. if (!description) {
  417. auto builder = TRY(KBufferBuilder::try_create());
  418. auto process = Process::from_pid(associated_pid());
  419. if (!process)
  420. return KResult(ESRCH);
  421. TRY(try_to_acquire_data(*process, builder));
  422. auto data_buffer = builder.build();
  423. if (!data_buffer)
  424. return KResult(EFAULT);
  425. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  426. TRY(buffer.write(data_buffer->data() + offset, nread));
  427. return nread;
  428. }
  429. if (!description->data()) {
  430. dbgln("ProcFS Process Information: Do not have cached data!");
  431. return KResult(EIO);
  432. }
  433. MutexLocker locker(m_refresh_lock);
  434. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  435. auto& data_buffer = typed_cached_data.buffer;
  436. if (!data_buffer || (size_t)offset >= data_buffer->size())
  437. return 0;
  438. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  439. TRY(buffer.write(data_buffer->data() + offset, nread));
  440. return nread;
  441. }
  442. KResultOr<NonnullRefPtr<Inode>> ProcFSProcessPropertyInode::lookup(StringView)
  443. {
  444. return EINVAL;
  445. }
  446. static KResult build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data)
  447. {
  448. cached_data.buffer = builder.build();
  449. if (!cached_data.buffer)
  450. return ENOMEM;
  451. return KSuccess;
  452. }
  453. KResult ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBufferBuilder& builder) const
  454. {
  455. // FIXME: Verify process is already ref-counted
  456. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions) {
  457. TRY(process.procfs_get_file_description_link(m_possible_data.property_index, builder));
  458. return KSuccess;
  459. }
  460. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks) {
  461. TRY(process.procfs_get_thread_stack(m_possible_data.property_index, builder));
  462. return KSuccess;
  463. }
  464. VERIFY(m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  465. switch (m_possible_data.property_type) {
  466. case SegmentedProcFSIndex::MainProcessProperty::Unveil:
  467. return process.procfs_get_unveil_stats(builder);
  468. case SegmentedProcFSIndex::MainProcessProperty::Pledge:
  469. return process.procfs_get_pledge_stats(builder);
  470. case SegmentedProcFSIndex::MainProcessProperty::OpenFileDescriptions:
  471. return process.procfs_get_fds_stats(builder);
  472. case SegmentedProcFSIndex::MainProcessProperty::BinaryLink:
  473. return process.procfs_get_binary_link(builder);
  474. case SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink:
  475. return process.procfs_get_current_work_directory_link(builder);
  476. case SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents:
  477. return process.procfs_get_perf_events(builder);
  478. case SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats:
  479. return process.procfs_get_virtual_memory_stats(builder);
  480. default:
  481. VERIFY_NOT_REACHED();
  482. }
  483. }
  484. KResult ProcFSProcessPropertyInode::refresh_data(OpenFileDescription& description)
  485. {
  486. // For process-specific inodes, hold the process's ptrace lock across refresh
  487. // and refuse to load data if the process is not dumpable.
  488. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  489. auto process = Process::from_pid(associated_pid());
  490. if (!process)
  491. return KResult(ESRCH);
  492. process->ptrace_lock().lock();
  493. if (!process->is_dumpable()) {
  494. process->ptrace_lock().unlock();
  495. return EPERM;
  496. }
  497. ScopeGuard guard = [&] {
  498. process->ptrace_lock().unlock();
  499. };
  500. MutexLocker locker(m_refresh_lock);
  501. auto& cached_data = description.data();
  502. if (!cached_data) {
  503. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  504. if (!cached_data)
  505. return ENOMEM;
  506. }
  507. auto builder = TRY(KBufferBuilder::try_create());
  508. TRY(try_to_acquire_data(*process, builder));
  509. return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
  510. }
  511. }