ProcFS.cpp 22 KB

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