ProcFS.cpp 22 KB

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