ProcFS.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/API/POSIX/errno.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/OpenFileDescription.h>
  13. #include <Kernel/FileSystem/ProcFS.h>
  14. #include <Kernel/FileSystem/VirtualFileSystem.h>
  15. #include <Kernel/Heap/kmalloc.h>
  16. #include <Kernel/Process.h>
  17. #include <Kernel/Sections.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. ErrorOr<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. ErrorOr<void> ProcFS::initialize()
  44. {
  45. m_root_inode = static_ptr_cast<ProcFSDirectoryInode>(TRY(ProcFSComponentRegistry::the().root_directory().to_inode(*this)));
  46. return {};
  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. ErrorOr<void> ProcFSInode::flush_metadata()
  60. {
  61. return {};
  62. }
  63. ErrorOr<void> ProcFSInode::add_child(Inode&, StringView, mode_t)
  64. {
  65. return EROFS;
  66. }
  67. ErrorOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
  68. {
  69. return EROFS;
  70. }
  71. ErrorOr<void> ProcFSInode::remove_child(StringView)
  72. {
  73. return EROFS;
  74. }
  75. ErrorOr<void> ProcFSInode::chmod(mode_t)
  76. {
  77. return EPERM;
  78. }
  79. ErrorOr<void> ProcFSInode::chown(UserID, GroupID)
  80. {
  81. return EPERM;
  82. }
  83. ErrorOr<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. ErrorOr<void> ProcFSGlobalInode::attach(OpenFileDescription& description)
  103. {
  104. return m_associated_component->refresh_data(description);
  105. }
  106. ErrorOr<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. ErrorOr<void> ProcFSGlobalInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const
  115. {
  116. VERIFY_NOT_REACHED();
  117. }
  118. ErrorOr<NonnullRefPtr<Inode>> ProcFSGlobalInode::lookup(StringView)
  119. {
  120. VERIFY_NOT_REACHED();
  121. }
  122. ErrorOr<void> ProcFSGlobalInode::truncate(u64 size)
  123. {
  124. return m_associated_component->truncate(size);
  125. }
  126. ErrorOr<void> 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. ErrorOr<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. ErrorOr<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. ErrorOr<void> ProcFSDirectoryInode::traverse_as_directory(Function<ErrorOr<void>(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. ErrorOr<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. ErrorOr<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. ErrorOr<size_t> ProcFSProcessAssociatedInode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, OpenFileDescription*)
  206. {
  207. return ENOTSUP;
  208. }
  209. ErrorOr<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. ErrorOr<void> ProcFSProcessDirectoryInode::attach(OpenFileDescription&)
  218. {
  219. return {};
  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. ErrorOr<size_t> ProcFSProcessDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const
  238. {
  239. VERIFY_NOT_REACHED();
  240. }
  241. ErrorOr<void> ProcFSProcessDirectoryInode::traverse_as_directory(Function<ErrorOr<void>(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. ErrorOr<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. if (name == "tty"sv)
  274. return TRY(ProcFSProcessPropertyInode::try_create_for_pid_property(procfs(), SegmentedProcFSIndex::MainProcessProperty::TTYLink, associated_pid()));
  275. return ENOENT;
  276. }
  277. ErrorOr<NonnullRefPtr<ProcFSProcessSubDirectoryInode>> ProcFSProcessSubDirectoryInode::try_create(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  278. {
  279. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessSubDirectoryInode(procfs, sub_directory_type, pid));
  280. }
  281. ProcFSProcessSubDirectoryInode::ProcFSProcessSubDirectoryInode(const ProcFS& procfs, SegmentedProcFSIndex::ProcessSubDirectory sub_directory_type, ProcessID pid)
  282. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_sub_directory(pid, sub_directory_type))
  283. , m_sub_directory_type(sub_directory_type)
  284. {
  285. }
  286. ErrorOr<size_t> ProcFSProcessSubDirectoryInode::read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const
  287. {
  288. VERIFY_NOT_REACHED();
  289. }
  290. ErrorOr<void> ProcFSProcessSubDirectoryInode::attach(OpenFileDescription&)
  291. {
  292. return {};
  293. }
  294. void ProcFSProcessSubDirectoryInode::did_seek(OpenFileDescription&, off_t)
  295. {
  296. VERIFY_NOT_REACHED();
  297. }
  298. InodeMetadata ProcFSProcessSubDirectoryInode::metadata() const
  299. {
  300. MutexLocker locker(m_inode_lock);
  301. auto process = Process::from_pid(associated_pid());
  302. if (!process)
  303. return {};
  304. auto traits = process->procfs_traits();
  305. InodeMetadata metadata;
  306. metadata.inode = { fsid(), traits->component_index() };
  307. metadata.mode = S_IFDIR | traits->required_mode();
  308. metadata.uid = traits->owner_user();
  309. metadata.gid = traits->owner_group();
  310. metadata.size = 0;
  311. metadata.mtime = traits->modified_time();
  312. return metadata;
  313. }
  314. ErrorOr<void> ProcFSProcessSubDirectoryInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  315. {
  316. MutexLocker locker(procfs().m_lock);
  317. auto process = Process::from_pid(associated_pid());
  318. if (!process)
  319. return EINVAL;
  320. switch (m_sub_directory_type) {
  321. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  322. return process->traverse_file_descriptions_directory(procfs().fsid(), move(callback));
  323. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  324. return process->traverse_stacks_directory(procfs().fsid(), move(callback));
  325. default:
  326. VERIFY_NOT_REACHED();
  327. }
  328. VERIFY_NOT_REACHED();
  329. }
  330. ErrorOr<NonnullRefPtr<Inode>> ProcFSProcessSubDirectoryInode::lookup(StringView name)
  331. {
  332. MutexLocker locker(procfs().m_lock);
  333. auto process = Process::from_pid(associated_pid());
  334. if (!process)
  335. return ESRCH;
  336. switch (m_sub_directory_type) {
  337. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  338. return process->lookup_file_descriptions_directory(procfs(), name);
  339. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  340. return process->lookup_stacks_directory(procfs(), name);
  341. default:
  342. VERIFY_NOT_REACHED();
  343. }
  344. }
  345. ErrorOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_file_description_link(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  346. {
  347. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, file_description_index, pid));
  348. }
  349. ErrorOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_thread_stack(const ProcFS& procfs, ThreadID stack_thread_index, ProcessID pid)
  350. {
  351. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, stack_thread_index, pid));
  352. }
  353. ErrorOr<NonnullRefPtr<ProcFSProcessPropertyInode>> ProcFSProcessPropertyInode::try_create_for_pid_property(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  354. {
  355. return adopt_nonnull_ref_or_enomem(new (nothrow) ProcFSProcessPropertyInode(procfs, main_property_type, pid));
  356. }
  357. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  358. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(pid, main_property_type))
  359. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Reserved)
  360. {
  361. m_possible_data.property_type = main_property_type;
  362. }
  363. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, unsigned file_description_index, ProcessID pid)
  364. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_file_description(pid, file_description_index))
  365. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  366. {
  367. m_possible_data.property_index = file_description_index;
  368. }
  369. ProcFSProcessPropertyInode::ProcFSProcessPropertyInode(const ProcFS& procfs, ThreadID thread_stack_index, ProcessID pid)
  370. : ProcFSProcessAssociatedInode(procfs, pid, SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid, thread_stack_index))
  371. , m_parent_sub_directory_type(SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  372. {
  373. m_possible_data.property_index = thread_stack_index.value();
  374. }
  375. ErrorOr<void> ProcFSProcessPropertyInode::attach(OpenFileDescription& description)
  376. {
  377. return refresh_data(description);
  378. }
  379. void ProcFSProcessPropertyInode::did_seek(OpenFileDescription& description, off_t offset)
  380. {
  381. if (offset != 0)
  382. return;
  383. (void)refresh_data(description);
  384. }
  385. static mode_t determine_procfs_process_inode_mode(SegmentedProcFSIndex::ProcessSubDirectory parent_sub_directory_type, SegmentedProcFSIndex::MainProcessProperty main_property)
  386. {
  387. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  388. return S_IFLNK | 0400;
  389. if (parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  390. return S_IFREG | 0400;
  391. VERIFY(parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  392. if (main_property == SegmentedProcFSIndex::MainProcessProperty::BinaryLink)
  393. return S_IFLNK | 0777;
  394. if (main_property == SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink)
  395. return S_IFLNK | 0777;
  396. if (main_property == SegmentedProcFSIndex::MainProcessProperty::TTYLink)
  397. return S_IFLNK | 0777;
  398. return S_IFREG | 0400;
  399. }
  400. InodeMetadata ProcFSProcessPropertyInode::metadata() const
  401. {
  402. MutexLocker locker(m_inode_lock);
  403. auto process = Process::from_pid(associated_pid());
  404. if (!process)
  405. return {};
  406. auto traits = process->procfs_traits();
  407. InodeMetadata metadata;
  408. metadata.inode = { fsid(), traits->component_index() };
  409. metadata.mode = determine_procfs_process_inode_mode(m_parent_sub_directory_type, m_possible_data.property_type);
  410. metadata.uid = traits->owner_user();
  411. metadata.gid = traits->owner_group();
  412. metadata.size = 0;
  413. metadata.mtime = traits->modified_time();
  414. return metadata;
  415. }
  416. ErrorOr<void> ProcFSProcessPropertyInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const
  417. {
  418. VERIFY_NOT_REACHED();
  419. }
  420. ErrorOr<size_t> ProcFSProcessPropertyInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* 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. auto builder = TRY(KBufferBuilder::try_create());
  427. auto process = Process::from_pid(associated_pid());
  428. if (!process)
  429. return Error::from_errno(ESRCH);
  430. TRY(try_to_acquire_data(*process, builder));
  431. auto data_buffer = builder.build();
  432. if (!data_buffer)
  433. return Error::from_errno(EFAULT);
  434. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  435. TRY(buffer.write(data_buffer->data() + offset, nread));
  436. return nread;
  437. }
  438. if (!description->data()) {
  439. dbgln("ProcFS Process Information: Do not have cached data!");
  440. return Error::from_errno(EIO);
  441. }
  442. MutexLocker locker(m_refresh_lock);
  443. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  444. auto& data_buffer = typed_cached_data.buffer;
  445. if (!data_buffer || (size_t)offset >= data_buffer->size())
  446. return 0;
  447. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  448. TRY(buffer.write(data_buffer->data() + offset, nread));
  449. return nread;
  450. }
  451. ErrorOr<NonnullRefPtr<Inode>> ProcFSProcessPropertyInode::lookup(StringView)
  452. {
  453. return EINVAL;
  454. }
  455. static ErrorOr<void> build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data)
  456. {
  457. cached_data.buffer = builder.build();
  458. if (!cached_data.buffer)
  459. return ENOMEM;
  460. return {};
  461. }
  462. ErrorOr<void> ProcFSProcessPropertyInode::try_to_acquire_data(Process& process, KBufferBuilder& builder) const
  463. {
  464. // FIXME: Verify process is already ref-counted
  465. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions) {
  466. TRY(process.procfs_get_file_description_link(m_possible_data.property_index, builder));
  467. return {};
  468. }
  469. if (m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks) {
  470. TRY(process.procfs_get_thread_stack(m_possible_data.property_index, builder));
  471. return {};
  472. }
  473. VERIFY(m_parent_sub_directory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  474. switch (m_possible_data.property_type) {
  475. case SegmentedProcFSIndex::MainProcessProperty::Unveil:
  476. return process.procfs_get_unveil_stats(builder);
  477. case SegmentedProcFSIndex::MainProcessProperty::Pledge:
  478. return process.procfs_get_pledge_stats(builder);
  479. case SegmentedProcFSIndex::MainProcessProperty::OpenFileDescriptions:
  480. return process.procfs_get_fds_stats(builder);
  481. case SegmentedProcFSIndex::MainProcessProperty::BinaryLink:
  482. return process.procfs_get_binary_link(builder);
  483. case SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink:
  484. return process.procfs_get_current_work_directory_link(builder);
  485. case SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents:
  486. return process.procfs_get_perf_events(builder);
  487. case SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats:
  488. return process.procfs_get_virtual_memory_stats(builder);
  489. case SegmentedProcFSIndex::MainProcessProperty::TTYLink:
  490. return process.procfs_get_tty_link(builder);
  491. default:
  492. VERIFY_NOT_REACHED();
  493. }
  494. }
  495. ErrorOr<void> ProcFSProcessPropertyInode::refresh_data(OpenFileDescription& description)
  496. {
  497. // For process-specific inodes, hold the process's ptrace lock across refresh
  498. // and refuse to load data if the process is not dumpable.
  499. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  500. auto process = Process::from_pid(associated_pid());
  501. if (!process)
  502. return Error::from_errno(ESRCH);
  503. process->ptrace_lock().lock();
  504. if (!process->is_dumpable()) {
  505. process->ptrace_lock().unlock();
  506. return EPERM;
  507. }
  508. ScopeGuard guard = [&] {
  509. process->ptrace_lock().unlock();
  510. };
  511. MutexLocker locker(m_refresh_lock);
  512. auto& cached_data = description.data();
  513. if (!cached_data) {
  514. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  515. if (!cached_data)
  516. return ENOMEM;
  517. }
  518. auto builder = TRY(KBufferBuilder::try_create());
  519. TRY(try_to_acquire_data(*process, builder));
  520. return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
  521. }
  522. }