Inode.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. * Copyright (c) 2021-2022, Liav A. <liavalb@hotmail.co.il>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <Kernel/FileSystem/ProcFS/Inode.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/ProcessExposed.h>
  11. namespace Kernel {
  12. ProcFSInode::~ProcFSInode() = default;
  13. ErrorOr<void> ProcFSInode::flush_metadata()
  14. {
  15. return {};
  16. }
  17. ErrorOr<void> ProcFSInode::add_child(Inode&, StringView, mode_t)
  18. {
  19. return EROFS;
  20. }
  21. ErrorOr<NonnullLockRefPtr<Inode>> ProcFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
  22. {
  23. return EROFS;
  24. }
  25. ErrorOr<void> ProcFSInode::remove_child(StringView)
  26. {
  27. return EROFS;
  28. }
  29. ErrorOr<void> ProcFSInode::chmod(mode_t)
  30. {
  31. return EPERM;
  32. }
  33. ErrorOr<void> ProcFSInode::chown(UserID, GroupID)
  34. {
  35. return EPERM;
  36. }
  37. ErrorOr<void> ProcFSInode::replace_child(StringView, Inode&)
  38. {
  39. return EROFS;
  40. }
  41. ErrorOr<size_t> ProcFSInode::write_bytes_locked(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*)
  42. {
  43. return EROFS;
  44. }
  45. ErrorOr<void> ProcFSInode::truncate(u64)
  46. {
  47. return EROFS;
  48. }
  49. ErrorOr<void> ProcFSInode::update_timestamps(Optional<Time>, Optional<Time>, Optional<Time>)
  50. {
  51. // Note: Silently ignore the update request.
  52. return {};
  53. }
  54. static mode_t determine_procfs_process_inode_mode(SegmentedProcFSIndex::ProcessSubDirectory parent_subdirectory_type, Optional<SegmentedProcFSIndex::MainProcessProperty> main_property)
  55. {
  56. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  57. return S_IFLNK | 0400;
  58. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  59. return S_IFREG | 0400;
  60. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Children)
  61. return S_IFLNK | 0400;
  62. VERIFY(parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  63. if (main_property == SegmentedProcFSIndex::MainProcessProperty::BinaryLink)
  64. return S_IFLNK | 0777;
  65. if (main_property == SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink)
  66. return S_IFLNK | 0777;
  67. return S_IFREG | 0400;
  68. }
  69. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_file_description_link_inode(ProcFS const& procfs_instance, unsigned fd_number, ProcessID pid)
  70. {
  71. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, fd_number, pid));
  72. }
  73. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, unsigned file_description_index, ProcessID pid)
  74. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_file_description(pid, file_description_index))
  75. , m_type(Type::FileDescriptionLink)
  76. , m_parent_subdirectory_type(SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions)
  77. , m_associated_pid(pid)
  78. {
  79. m_possible_data.property_index = file_description_index;
  80. }
  81. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_thread_stack_inode(ProcFS const& procfs_instance, ThreadID stack_thread_index, ProcessID pid)
  82. {
  83. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, stack_thread_index, pid));
  84. }
  85. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, ThreadID thread_stack_index, ProcessID pid)
  86. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid, thread_stack_index))
  87. , m_type(Type::ThreadStack)
  88. , m_parent_subdirectory_type(SegmentedProcFSIndex::ProcessSubDirectory::Stacks)
  89. , m_associated_pid(pid)
  90. {
  91. m_possible_data.property_index = thread_stack_index.value();
  92. }
  93. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_pid_property_inode(ProcFS const& procfs_instance, SegmentedProcFSIndex::MainProcessProperty process_property, ProcessID pid)
  94. {
  95. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, process_property, pid));
  96. }
  97. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, SegmentedProcFSIndex::MainProcessProperty main_property_type, ProcessID pid)
  98. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(pid, main_property_type))
  99. , m_type(Type::ProcessProperty)
  100. , m_parent_subdirectory_type(SegmentedProcFSIndex::ProcessSubDirectory::Reserved)
  101. , m_associated_pid(pid)
  102. {
  103. m_possible_data.property_type = main_property_type;
  104. }
  105. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_child_process_link_inode(ProcFS const& procfs_instance, ProcessID child_pid, ProcessID pid)
  106. {
  107. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, child_pid, pid));
  108. }
  109. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, ProcessID child_pid, ProcessID pid)
  110. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_children(pid, child_pid))
  111. , m_type(Type::ChildProcessLink)
  112. , m_parent_subdirectory_type(SegmentedProcFSIndex::ProcessSubDirectory::Children)
  113. , m_associated_pid(pid)
  114. {
  115. m_possible_data.property_index = child_pid.value();
  116. }
  117. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_process_directory_inode(ProcFS const& procfs_instance, ProcessID pid)
  118. {
  119. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, pid));
  120. }
  121. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, ProcessID pid)
  122. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_pid_directory(pid))
  123. , m_type(Type::ProcessDirectory)
  124. , m_associated_pid(pid)
  125. {
  126. }
  127. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_process_subdirectory_inode(ProcFS const& procfs_instance, SegmentedProcFSIndex::ProcessSubDirectory subdirectory_type, ProcessID pid)
  128. {
  129. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, subdirectory_type, pid));
  130. }
  131. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, SegmentedProcFSIndex::ProcessSubDirectory subdirectory_type, ProcessID pid)
  132. : Inode(const_cast<ProcFS&>(procfs_instance), SegmentedProcFSIndex::build_segmented_index_for_sub_directory(pid, subdirectory_type))
  133. , m_type(Type::ProcessSubdirectory)
  134. , m_subdirectory_type(subdirectory_type)
  135. , m_associated_pid(pid)
  136. {
  137. }
  138. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_global_link_inode(ProcFS const& procfs_instance, ProcFSExposedLink const& link_component)
  139. {
  140. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, link_component));
  141. }
  142. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, ProcFSExposedLink const& link_component)
  143. : Inode(const_cast<ProcFS&>(procfs_instance), link_component.component_index())
  144. , m_type(Type::GlobalLink)
  145. , m_associated_component(link_component)
  146. {
  147. }
  148. ErrorOr<NonnullLockRefPtr<ProcFSInode>> ProcFSInode::try_create_as_directory_inode(ProcFS const& procfs_instance, ProcFSExposedDirectory const& directory_component)
  149. {
  150. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcFSInode(procfs_instance, directory_component));
  151. }
  152. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, ProcFSExposedDirectory const& directory_component)
  153. : Inode(const_cast<ProcFS&>(procfs_instance), directory_component.component_index())
  154. , m_type(Type::GlobalDirectory)
  155. , m_associated_component(directory_component)
  156. {
  157. }
  158. ErrorOr<void> ProcFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  159. {
  160. MutexLocker locker(procfs().m_lock);
  161. if (m_type == Type::ProcessSubdirectory) {
  162. VERIFY(m_associated_pid.has_value());
  163. VERIFY(m_subdirectory_type.has_value());
  164. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  165. if (!process)
  166. return EINVAL;
  167. switch (m_subdirectory_type.value()) {
  168. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  169. return process->traverse_file_descriptions_directory(procfs().fsid(), move(callback));
  170. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  171. return process->traverse_stacks_directory(procfs().fsid(), move(callback));
  172. case SegmentedProcFSIndex::ProcessSubDirectory::Children:
  173. return process->traverse_children_directory(procfs().fsid(), move(callback));
  174. default:
  175. VERIFY_NOT_REACHED();
  176. }
  177. VERIFY_NOT_REACHED();
  178. }
  179. if (m_type == Type::GlobalDirectory) {
  180. VERIFY(m_associated_component);
  181. return m_associated_component->traverse_as_directory(procfs().fsid(), move(callback));
  182. }
  183. VERIFY(m_type == Type::ProcessDirectory);
  184. VERIFY(m_associated_pid.has_value());
  185. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  186. if (!process)
  187. return EINVAL;
  188. return process->procfs_traits()->traverse_as_directory(procfs().fsid(), move(callback));
  189. }
  190. ErrorOr<NonnullLockRefPtr<Inode>> ProcFSInode::lookup(StringView name)
  191. {
  192. MutexLocker locker(procfs().m_lock);
  193. if (m_type == Type::ProcessSubdirectory) {
  194. VERIFY(m_associated_pid.has_value());
  195. VERIFY(m_subdirectory_type.has_value());
  196. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  197. if (!process)
  198. return ESRCH;
  199. switch (m_subdirectory_type.value()) {
  200. case SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions:
  201. return process->lookup_file_descriptions_directory(procfs(), name);
  202. case SegmentedProcFSIndex::ProcessSubDirectory::Stacks:
  203. return process->lookup_stacks_directory(procfs(), name);
  204. case SegmentedProcFSIndex::ProcessSubDirectory::Children:
  205. return process->lookup_children_directory(procfs(), name);
  206. default:
  207. VERIFY_NOT_REACHED();
  208. }
  209. VERIFY_NOT_REACHED();
  210. }
  211. if (m_type == Type::GlobalDirectory) {
  212. VERIFY(m_associated_component);
  213. auto component = TRY(m_associated_component->lookup(name));
  214. return TRY(component->to_inode(procfs()));
  215. }
  216. VERIFY(m_type == Type::ProcessDirectory);
  217. VERIFY(m_associated_pid.has_value());
  218. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  219. if (!process)
  220. return ESRCH;
  221. if (name == "fd"sv)
  222. return TRY(ProcFSInode::try_create_as_process_subdirectory_inode(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions, m_associated_pid.value()));
  223. if (name == "stacks"sv)
  224. return TRY(ProcFSInode::try_create_as_process_subdirectory_inode(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, m_associated_pid.value()));
  225. if (name == "children"sv)
  226. return TRY(ProcFSInode::try_create_as_process_subdirectory_inode(procfs(), SegmentedProcFSIndex::ProcessSubDirectory::Children, m_associated_pid.value()));
  227. if (name == "unveil"sv)
  228. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::Unveil, m_associated_pid.value()));
  229. if (name == "pledge"sv)
  230. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::Pledge, m_associated_pid.value()));
  231. if (name == "fds"sv)
  232. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::OpenFileDescriptions, m_associated_pid.value()));
  233. if (name == "exe"sv)
  234. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::BinaryLink, m_associated_pid.value()));
  235. if (name == "cwd"sv)
  236. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink, m_associated_pid.value()));
  237. if (name == "perf_events"sv)
  238. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents, m_associated_pid.value()));
  239. if (name == "vm"sv)
  240. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats, m_associated_pid.value()));
  241. if (name == "cmdline"sv)
  242. return TRY(ProcFSInode::try_create_as_pid_property_inode(procfs(), SegmentedProcFSIndex::MainProcessProperty::CommandLine, m_associated_pid.value()));
  243. return ENOENT;
  244. }
  245. ErrorOr<void> ProcFSInode::attach(OpenFileDescription& description)
  246. {
  247. if (m_type == Type::GlobalDirectory || m_type == Type::ProcessDirectory || m_type == Type::ProcessSubdirectory)
  248. return {};
  249. if (m_type == Type::GlobalLink)
  250. return m_associated_component->refresh_data(description);
  251. VERIFY(m_type == Type::ProcessProperty || m_type == Type::FileDescriptionLink || m_type == Type::ThreadStack || m_type == Type::ChildProcessLink);
  252. return refresh_process_property_data(description);
  253. }
  254. void ProcFSInode::did_seek(OpenFileDescription& description, off_t offset)
  255. {
  256. if (m_type == Type::GlobalLink) {
  257. if (offset != 0)
  258. return;
  259. auto result = m_associated_component->refresh_data(description);
  260. if (result.is_error()) {
  261. // Subsequent calls to read will return EIO!
  262. dbgln("ProcFS: Could not refresh contents: {}", result.error());
  263. }
  264. }
  265. VERIFY(m_type == Type::ProcessProperty || m_type == Type::FileDescriptionLink || m_type == Type::ThreadStack || m_type == Type::ChildProcessLink);
  266. if (offset != 0)
  267. return;
  268. (void)refresh_process_property_data(description);
  269. }
  270. ErrorOr<size_t> ProcFSInode::read_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
  271. {
  272. dbgln_if(PROCFS_DEBUG, "ProcFSInode: read_bytes_locked offset: {} count: {}", offset, count);
  273. if (m_type == Type::GlobalLink) {
  274. VERIFY(m_associated_component);
  275. return m_associated_component->read_bytes(offset, count, buffer, description);
  276. }
  277. VERIFY(m_type == Type::ProcessProperty || m_type == Type::FileDescriptionLink || m_type == Type::ThreadStack || m_type == Type::ChildProcessLink);
  278. VERIFY(offset >= 0);
  279. VERIFY(buffer.user_or_kernel_ptr());
  280. if (!description) {
  281. auto builder = TRY(KBufferBuilder::try_create());
  282. VERIFY(m_associated_pid.has_value());
  283. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  284. if (!process)
  285. return Error::from_errno(ESRCH);
  286. TRY(try_fetch_process_property_data(*process, builder));
  287. auto data_buffer = builder.build();
  288. if (!data_buffer)
  289. return Error::from_errno(EFAULT);
  290. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  291. TRY(buffer.write(data_buffer->data() + offset, nread));
  292. return nread;
  293. }
  294. if (!description->data()) {
  295. dbgln("ProcFS Process Information: Do not have cached data!");
  296. return Error::from_errno(EIO);
  297. }
  298. MutexLocker locker(m_refresh_lock);
  299. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  300. auto& data_buffer = typed_cached_data.buffer;
  301. if (!data_buffer || (size_t)offset >= data_buffer->size())
  302. return 0;
  303. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  304. TRY(buffer.write(data_buffer->data() + offset, nread));
  305. return nread;
  306. }
  307. static ErrorOr<void> build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data)
  308. {
  309. cached_data.buffer = builder.build();
  310. if (!cached_data.buffer)
  311. return ENOMEM;
  312. return {};
  313. }
  314. ErrorOr<void> ProcFSInode::try_fetch_process_property_data(NonnullLockRefPtr<Process> process, KBufferBuilder& builder) const
  315. {
  316. VERIFY(m_type == Type::ProcessProperty || m_type == Type::FileDescriptionLink || m_type == Type::ThreadStack || m_type == Type::ChildProcessLink);
  317. VERIFY(m_parent_subdirectory_type.has_value());
  318. auto parent_subdirectory_type = m_parent_subdirectory_type.value();
  319. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::OpenFileDescriptions) {
  320. TRY(process->procfs_get_file_description_link(m_possible_data.property_index, builder));
  321. return {};
  322. }
  323. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Stacks) {
  324. TRY(process->procfs_get_thread_stack(m_possible_data.property_index, builder));
  325. return {};
  326. }
  327. if (parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Children) {
  328. TRY(process->procfs_get_child_proccess_link(m_possible_data.property_index, builder));
  329. return {};
  330. }
  331. VERIFY(m_type == Type::ProcessProperty);
  332. VERIFY(parent_subdirectory_type == SegmentedProcFSIndex::ProcessSubDirectory::Reserved);
  333. switch (m_possible_data.property_type) {
  334. case SegmentedProcFSIndex::MainProcessProperty::Unveil:
  335. return process->procfs_get_unveil_stats(builder);
  336. case SegmentedProcFSIndex::MainProcessProperty::Pledge:
  337. return process->procfs_get_pledge_stats(builder);
  338. case SegmentedProcFSIndex::MainProcessProperty::OpenFileDescriptions:
  339. return process->procfs_get_fds_stats(builder);
  340. case SegmentedProcFSIndex::MainProcessProperty::BinaryLink:
  341. return process->procfs_get_binary_link(builder);
  342. case SegmentedProcFSIndex::MainProcessProperty::CurrentWorkDirectoryLink:
  343. return process->procfs_get_current_work_directory_link(builder);
  344. case SegmentedProcFSIndex::MainProcessProperty::PerformanceEvents:
  345. return process->procfs_get_perf_events(builder);
  346. case SegmentedProcFSIndex::MainProcessProperty::VirtualMemoryStats:
  347. return process->procfs_get_virtual_memory_stats(builder);
  348. case SegmentedProcFSIndex::MainProcessProperty::CommandLine:
  349. return process->procfs_get_command_line(builder);
  350. default:
  351. VERIFY_NOT_REACHED();
  352. }
  353. }
  354. ErrorOr<void> ProcFSInode::refresh_process_property_data(OpenFileDescription& description)
  355. {
  356. // For process-specific inodes, hold the process's ptrace lock across refresh
  357. // and refuse to load data if the process is not dumpable.
  358. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  359. VERIFY(m_type == Type::ProcessProperty || m_type == Type::FileDescriptionLink || m_type == Type::ThreadStack || m_type == Type::ChildProcessLink);
  360. VERIFY(m_associated_pid.has_value());
  361. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  362. if (!process)
  363. return Error::from_errno(ESRCH);
  364. process->ptrace_lock().lock();
  365. if (!process->is_dumpable()) {
  366. process->ptrace_lock().unlock();
  367. return EPERM;
  368. }
  369. ScopeGuard guard = [&] {
  370. process->ptrace_lock().unlock();
  371. };
  372. MutexLocker locker(m_refresh_lock);
  373. auto& cached_data = description.data();
  374. if (!cached_data) {
  375. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  376. if (!cached_data)
  377. return ENOMEM;
  378. }
  379. auto builder = TRY(KBufferBuilder::try_create());
  380. TRY(try_fetch_process_property_data(*process, builder));
  381. return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
  382. }
  383. InodeMetadata ProcFSInode::metadata() const
  384. {
  385. InodeMetadata metadata;
  386. switch (m_type) {
  387. case Type::GlobalLink: {
  388. metadata.inode = { fsid(), m_associated_component->component_index() };
  389. metadata.mode = S_IFLNK | m_associated_component->required_mode();
  390. metadata.uid = m_associated_component->owner_user();
  391. metadata.gid = m_associated_component->owner_group();
  392. metadata.size = 0;
  393. metadata.mtime = m_associated_component->modified_time();
  394. break;
  395. }
  396. case Type::GlobalDirectory: {
  397. metadata.inode = { fsid(), m_associated_component->component_index() };
  398. metadata.mode = S_IFDIR | m_associated_component->required_mode();
  399. metadata.uid = m_associated_component->owner_user();
  400. metadata.gid = m_associated_component->owner_group();
  401. metadata.size = 0;
  402. metadata.mtime = m_associated_component->modified_time();
  403. break;
  404. }
  405. case Type::FileDescriptionLink: {
  406. VERIFY(m_associated_pid.has_value());
  407. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  408. if (!process)
  409. return {};
  410. auto traits = process->procfs_traits();
  411. metadata.inode = { fsid(), SegmentedProcFSIndex::build_segmented_index_for_file_description(m_associated_pid.value(), m_possible_data.property_index) };
  412. metadata.mode = determine_procfs_process_inode_mode(m_parent_subdirectory_type.value(), {});
  413. metadata.uid = traits->owner_user();
  414. metadata.gid = traits->owner_group();
  415. metadata.size = 0;
  416. metadata.mtime = traits->modified_time();
  417. break;
  418. }
  419. case Type::ThreadStack: {
  420. VERIFY(m_associated_pid.has_value());
  421. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  422. if (!process)
  423. return {};
  424. auto traits = process->procfs_traits();
  425. metadata.inode = { fsid(), SegmentedProcFSIndex::build_segmented_index_for_thread_stack(m_associated_pid.value(), m_possible_data.property_index) };
  426. metadata.mode = determine_procfs_process_inode_mode(m_parent_subdirectory_type.value(), {});
  427. metadata.uid = traits->owner_user();
  428. metadata.gid = traits->owner_group();
  429. metadata.size = 0;
  430. metadata.mtime = traits->modified_time();
  431. break;
  432. }
  433. case Type::ProcessProperty: {
  434. VERIFY(m_associated_pid.has_value());
  435. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  436. if (!process)
  437. return {};
  438. auto traits = process->procfs_traits();
  439. metadata.inode = { fsid(), SegmentedProcFSIndex::build_segmented_index_for_main_property_in_pid_directory(m_associated_pid.value(), m_possible_data.property_type) };
  440. metadata.mode = determine_procfs_process_inode_mode(m_parent_subdirectory_type.value(), m_possible_data.property_type);
  441. metadata.uid = traits->owner_user();
  442. metadata.gid = traits->owner_group();
  443. metadata.size = 0;
  444. metadata.mtime = traits->modified_time();
  445. break;
  446. }
  447. case Type::ChildProcessLink: {
  448. VERIFY(m_associated_pid.has_value());
  449. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  450. if (!process)
  451. return {};
  452. auto traits = process->procfs_traits();
  453. metadata.inode = { fsid(), SegmentedProcFSIndex::build_segmented_index_for_children(m_associated_pid.value(), m_possible_data.property_index) };
  454. metadata.mode = determine_procfs_process_inode_mode(m_parent_subdirectory_type.value(), {});
  455. metadata.uid = traits->owner_user();
  456. metadata.gid = traits->owner_group();
  457. metadata.size = 0;
  458. metadata.mtime = traits->modified_time();
  459. break;
  460. }
  461. case Type::ProcessDirectory: {
  462. VERIFY(m_associated_pid.has_value());
  463. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  464. if (!process)
  465. return {};
  466. auto traits = process->procfs_traits();
  467. metadata.inode = { fsid(), traits->component_index() };
  468. metadata.mode = S_IFDIR | traits->required_mode();
  469. metadata.uid = traits->owner_user();
  470. metadata.gid = traits->owner_group();
  471. metadata.size = 0;
  472. metadata.mtime = traits->modified_time();
  473. break;
  474. }
  475. case Type::ProcessSubdirectory: {
  476. VERIFY(m_associated_pid.has_value());
  477. VERIFY(m_subdirectory_type.has_value());
  478. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  479. if (!process)
  480. return {};
  481. auto traits = process->procfs_traits();
  482. metadata.inode = { fsid(), SegmentedProcFSIndex::build_segmented_index_for_sub_directory(m_associated_pid.value(), m_subdirectory_type.value()) };
  483. metadata.mode = S_IFDIR | traits->required_mode();
  484. metadata.uid = traits->owner_user();
  485. metadata.gid = traits->owner_group();
  486. metadata.size = 0;
  487. metadata.mtime = traits->modified_time();
  488. break;
  489. }
  490. }
  491. return metadata;
  492. }
  493. }