ProcFS.cpp 24 KB

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