ProcFS.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. #include "ProcFS.h"
  2. #include "Console.h"
  3. #include "KSyms.h"
  4. #include "Process.h"
  5. #include "Scheduler.h"
  6. #include "StdLib.h"
  7. #include <AK/JsonArray.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/JsonValue.h>
  10. #include <Kernel/Arch/i386/CPU.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/FileDescription.h>
  13. #include <Kernel/FileSystem/VirtualFileSystem.h>
  14. #include <Kernel/KBufferBuilder.h>
  15. #include <Kernel/KParams.h>
  16. #include <Kernel/Net/NetworkAdapter.h>
  17. #include <Kernel/Net/TCPSocket.h>
  18. #include <Kernel/Net/UDPSocket.h>
  19. #include <Kernel/Net/LocalSocket.h>
  20. #include <Kernel/PCI.h>
  21. #include <Kernel/VM/MemoryManager.h>
  22. #include <Kernel/kmalloc.h>
  23. #include <LibC/errno_numbers.h>
  24. enum ProcParentDirectory {
  25. PDI_AbstractRoot = 0,
  26. PDI_Root,
  27. PDI_Root_sys,
  28. PDI_Root_net,
  29. PDI_PID,
  30. PDI_PID_fd,
  31. };
  32. enum ProcFileType {
  33. FI_Invalid = 0,
  34. FI_Root = 1, // directory
  35. __FI_Root_Start,
  36. FI_Root_mm,
  37. FI_Root_mounts,
  38. FI_Root_df,
  39. FI_Root_all,
  40. FI_Root_memstat,
  41. FI_Root_cpuinfo,
  42. FI_Root_inodes,
  43. FI_Root_dmesg,
  44. FI_Root_pci,
  45. FI_Root_uptime,
  46. FI_Root_cmdline,
  47. FI_Root_self, // symlink
  48. FI_Root_sys, // directory
  49. FI_Root_net, // directory
  50. __FI_Root_End,
  51. FI_Root_net_adapters,
  52. FI_Root_net_tcp,
  53. FI_Root_net_udp,
  54. FI_PID,
  55. __FI_PID_Start,
  56. FI_PID_vm,
  57. FI_PID_vmo,
  58. FI_PID_stack,
  59. FI_PID_regs,
  60. FI_PID_fds,
  61. FI_PID_exe, // symlink
  62. FI_PID_cwd, // symlink
  63. FI_PID_fd, // directory
  64. __FI_PID_End,
  65. FI_MaxStaticFileIndex,
  66. };
  67. static inline pid_t to_pid(const InodeIdentifier& identifier)
  68. {
  69. #ifdef PROCFS_DEBUG
  70. dbgprintf("to_pid, index=%08x -> %u\n", identifier.index(), identifier.index() >> 16);
  71. #endif
  72. return identifier.index() >> 16u;
  73. }
  74. static inline ProcParentDirectory to_proc_parent_directory(const InodeIdentifier& identifier)
  75. {
  76. return (ProcParentDirectory)((identifier.index() >> 12) & 0xf);
  77. }
  78. static inline int to_fd(const InodeIdentifier& identifier)
  79. {
  80. ASSERT(to_proc_parent_directory(identifier) == PDI_PID_fd);
  81. return (identifier.index() & 0xff) - FI_MaxStaticFileIndex;
  82. }
  83. static inline int to_sys_index(const InodeIdentifier& identifier)
  84. {
  85. ASSERT(to_proc_parent_directory(identifier) == PDI_Root_sys);
  86. return identifier.index() & 0xff;
  87. }
  88. static inline InodeIdentifier to_identifier(unsigned fsid, ProcParentDirectory parent, pid_t pid, ProcFileType proc_file_type)
  89. {
  90. return { fsid, ((unsigned)parent << 12u) | ((unsigned)pid << 16u) | (unsigned)proc_file_type };
  91. }
  92. static inline InodeIdentifier to_identifier_with_fd(unsigned fsid, pid_t pid, int fd)
  93. {
  94. return { fsid, (PDI_PID_fd << 12u) | ((unsigned)pid << 16u) | (FI_MaxStaticFileIndex + fd) };
  95. }
  96. static inline InodeIdentifier sys_var_to_identifier(unsigned fsid, unsigned index)
  97. {
  98. ASSERT(index < 256);
  99. return { fsid, (PDI_Root_sys << 12u) | index };
  100. }
  101. static inline InodeIdentifier to_parent_id(const InodeIdentifier& identifier)
  102. {
  103. switch (to_proc_parent_directory(identifier)) {
  104. case PDI_AbstractRoot:
  105. case PDI_Root:
  106. return { identifier.fsid(), FI_Root };
  107. case PDI_Root_sys:
  108. return { identifier.fsid(), FI_Root_sys };
  109. case PDI_Root_net:
  110. return { identifier.fsid(), FI_Root_net };
  111. case PDI_PID:
  112. return to_identifier(identifier.fsid(), PDI_Root, to_pid(identifier), FI_PID);
  113. case PDI_PID_fd:
  114. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_fd);
  115. }
  116. ASSERT_NOT_REACHED();
  117. }
  118. #if 0
  119. static inline u8 to_unused_metadata(const InodeIdentifier& identifier)
  120. {
  121. return (identifier.index() >> 8) & 0xf;
  122. }
  123. #endif
  124. static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
  125. {
  126. return (ProcFileType)(identifier.index() & 0xff);
  127. }
  128. static inline bool is_process_related_file(const InodeIdentifier& identifier)
  129. {
  130. if (to_proc_file_type(identifier) == FI_PID)
  131. return true;
  132. auto proc_parent_directory = to_proc_parent_directory(identifier);
  133. switch (proc_parent_directory) {
  134. case PDI_PID:
  135. case PDI_PID_fd:
  136. return true;
  137. default:
  138. return false;
  139. }
  140. }
  141. static inline bool is_directory(const InodeIdentifier& identifier)
  142. {
  143. auto proc_file_type = to_proc_file_type(identifier);
  144. switch (proc_file_type) {
  145. case FI_Root:
  146. case FI_Root_sys:
  147. case FI_Root_net:
  148. case FI_PID:
  149. case FI_PID_fd:
  150. return true;
  151. default:
  152. return false;
  153. }
  154. }
  155. static inline bool is_persistent_inode(const InodeIdentifier& identifier)
  156. {
  157. return to_proc_parent_directory(identifier) == PDI_Root_sys;
  158. }
  159. static ProcFS* s_the;
  160. ProcFS& ProcFS::the()
  161. {
  162. ASSERT(s_the);
  163. return *s_the;
  164. }
  165. NonnullRefPtr<ProcFS> ProcFS::create()
  166. {
  167. return adopt(*new ProcFS);
  168. }
  169. ProcFS::~ProcFS()
  170. {
  171. }
  172. Optional<KBuffer> procfs$pid_fds(InodeIdentifier identifier)
  173. {
  174. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  175. if (!handle)
  176. return {};
  177. auto& process = handle->process();
  178. if (process.number_of_open_file_descriptors() == 0)
  179. return {};
  180. JsonArray array;
  181. for (int i = 0; i < process.max_open_file_descriptors(); ++i) {
  182. auto* description = process.file_description(i);
  183. if (!description)
  184. continue;
  185. JsonObject description_object;
  186. description_object.set("fd", i);
  187. description_object.set("absolute_path", description->absolute_path());
  188. description_object.set("seekable", description->file().is_seekable());
  189. description_object.set("class", description->file().class_name());
  190. description_object.set("offset", description->offset());
  191. array.append(move(description_object));
  192. }
  193. return array.serialized<KBufferBuilder>();
  194. }
  195. Optional<KBuffer> procfs$pid_fd_entry(InodeIdentifier identifier)
  196. {
  197. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  198. if (!handle)
  199. return {};
  200. auto& process = handle->process();
  201. int fd = to_fd(identifier);
  202. auto* description = process.file_description(fd);
  203. if (!description)
  204. return {};
  205. return description->absolute_path().to_byte_buffer();
  206. }
  207. Optional<KBuffer> procfs$pid_vm(InodeIdentifier identifier)
  208. {
  209. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  210. if (!handle)
  211. return {};
  212. auto& process = handle->process();
  213. JsonArray array;
  214. for (auto& region : process.regions()) {
  215. JsonObject region_object;
  216. region_object.set("readable", region.is_readable());
  217. region_object.set("writable", region.is_writable());
  218. region_object.set("address", region.vaddr().get());
  219. region_object.set("size", region.size());
  220. region_object.set("amount_resident", region.amount_resident());
  221. region_object.set("name", region.name());
  222. array.append(move(region_object));
  223. }
  224. return array.serialized<KBufferBuilder>();
  225. }
  226. Optional<KBuffer> procfs$pci(InodeIdentifier)
  227. {
  228. KBufferBuilder builder;
  229. PCI::enumerate_all([&builder](PCI::Address address, PCI::ID id) {
  230. builder.appendf("%b:%b.%b %w:%w\n", address.bus(), address.slot(), address.function(), id.vendor_id, id.device_id);
  231. });
  232. return builder.build();
  233. }
  234. Optional<KBuffer> procfs$uptime(InodeIdentifier)
  235. {
  236. KBufferBuilder builder;
  237. builder.appendf("%u\n", (u32)(g_uptime / 1000));
  238. return builder.build();
  239. }
  240. Optional<KBuffer> procfs$cmdline(InodeIdentifier)
  241. {
  242. KBufferBuilder builder;
  243. builder.appendf("%s\n", KParams::the().cmdline().characters());
  244. return builder.build();
  245. }
  246. Optional<KBuffer> procfs$net_adapters(InodeIdentifier)
  247. {
  248. JsonArray json;
  249. NetworkAdapter::for_each([&json](auto& adapter) {
  250. JsonObject obj;
  251. obj.set("name", adapter.name());
  252. obj.set("class_name", adapter.class_name());
  253. obj.set("mac_address", adapter.mac_address().to_string());
  254. obj.set("ipv4_address", adapter.ipv4_address().to_string());
  255. obj.set("packets_in", adapter.packets_in());
  256. obj.set("bytes_in", adapter.bytes_in());
  257. obj.set("packets_out", adapter.packets_out());
  258. obj.set("bytes_out", adapter.bytes_out());
  259. json.append(obj);
  260. });
  261. return json.serialized<KBufferBuilder>();
  262. }
  263. Optional<KBuffer> procfs$net_tcp(InodeIdentifier)
  264. {
  265. JsonArray json;
  266. TCPSocket::for_each([&json](auto& socket) {
  267. JsonObject obj;
  268. obj.set("local_address", socket.local_address().to_string());
  269. obj.set("local_port", socket.local_port());
  270. obj.set("peer_address", socket.peer_address().to_string());
  271. obj.set("peer_port", socket.peer_port());
  272. obj.set("state", TCPSocket::to_string(socket.state()));
  273. obj.set("ack_number", socket.ack_number());
  274. obj.set("sequence_number", socket.sequence_number());
  275. obj.set("packets_in", socket.packets_in());
  276. obj.set("bytes_in", socket.bytes_in());
  277. obj.set("packets_out", socket.packets_out());
  278. obj.set("bytes_out", socket.bytes_out());
  279. json.append(obj);
  280. });
  281. return json.serialized<KBufferBuilder>();
  282. }
  283. Optional<KBuffer> procfs$net_udp(InodeIdentifier)
  284. {
  285. JsonArray json;
  286. UDPSocket::for_each([&json](auto& socket) {
  287. JsonObject obj;
  288. obj.set("local_address", socket.local_address().to_string());
  289. obj.set("local_port", socket.local_port());
  290. obj.set("peer_address", socket.peer_address().to_string());
  291. obj.set("peer_port", socket.peer_port());
  292. json.append(obj);
  293. });
  294. return json.serialized<KBufferBuilder>();
  295. }
  296. Optional<KBuffer> procfs$pid_vmo(InodeIdentifier identifier)
  297. {
  298. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  299. if (!handle)
  300. return {};
  301. auto& process = handle->process();
  302. KBufferBuilder builder;
  303. builder.appendf("BEGIN END SIZE NAME\n");
  304. for (auto& region : process.regions()) {
  305. builder.appendf("%x -- %x %x %s\n",
  306. region.vaddr().get(),
  307. region.vaddr().offset(region.size() - 1).get(),
  308. region.size(),
  309. region.name().characters());
  310. builder.appendf("VMO: %s @ %x(%u)\n",
  311. region.vmo().is_anonymous() ? "anonymous" : "file-backed",
  312. &region.vmo(),
  313. region.vmo().ref_count());
  314. for (size_t i = 0; i < region.vmo().page_count(); ++i) {
  315. auto& physical_page = region.vmo().physical_pages()[i];
  316. builder.appendf("P%x%s(%u) ",
  317. physical_page ? physical_page->paddr().get() : 0,
  318. region.should_cow(i) ? "!" : "",
  319. physical_page ? physical_page->ref_count() : 0);
  320. }
  321. builder.appendf("\n");
  322. }
  323. return builder.build();
  324. }
  325. Optional<KBuffer> procfs$pid_stack(InodeIdentifier identifier)
  326. {
  327. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  328. if (!handle)
  329. return {};
  330. auto& process = handle->process();
  331. return process.backtrace(*handle);
  332. }
  333. Optional<KBuffer> procfs$pid_regs(InodeIdentifier identifier)
  334. {
  335. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  336. if (!handle)
  337. return {};
  338. auto& process = handle->process();
  339. KBufferBuilder builder;
  340. process.for_each_thread([&](Thread& thread) {
  341. builder.appendf("Thread %d:\n", thread.tid());
  342. auto& tss = thread.tss();
  343. builder.appendf("eax: %x\n", tss.eax);
  344. builder.appendf("ebx: %x\n", tss.ebx);
  345. builder.appendf("ecx: %x\n", tss.ecx);
  346. builder.appendf("edx: %x\n", tss.edx);
  347. builder.appendf("esi: %x\n", tss.esi);
  348. builder.appendf("edi: %x\n", tss.edi);
  349. builder.appendf("ebp: %x\n", tss.ebp);
  350. builder.appendf("cr3: %x\n", tss.cr3);
  351. builder.appendf("flg: %x\n", tss.eflags);
  352. builder.appendf("sp: %w:%x\n", tss.ss, tss.esp);
  353. builder.appendf("pc: %w:%x\n", tss.cs, tss.eip);
  354. return IterationDecision::Continue;
  355. });
  356. return builder.build();
  357. }
  358. Optional<KBuffer> procfs$pid_exe(InodeIdentifier identifier)
  359. {
  360. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  361. if (!handle)
  362. return {};
  363. auto& process = handle->process();
  364. auto* custody = process.executable();
  365. ASSERT(custody);
  366. return custody->absolute_path().to_byte_buffer();
  367. }
  368. Optional<KBuffer> procfs$pid_cwd(InodeIdentifier identifier)
  369. {
  370. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  371. if (!handle)
  372. return {};
  373. return handle->process().current_directory().absolute_path().to_byte_buffer();
  374. }
  375. Optional<KBuffer> procfs$self(InodeIdentifier)
  376. {
  377. char buffer[16];
  378. ksprintf(buffer, "%u", current->pid());
  379. return KBuffer::copy((const u8*)buffer, strlen(buffer));
  380. }
  381. Optional<KBuffer> procfs$mm(InodeIdentifier)
  382. {
  383. InterruptDisabler disabler;
  384. KBufferBuilder builder;
  385. u32 vmobject_count = 0;
  386. MemoryManager::for_each_vmobject([&](auto& vmobject) {
  387. ++vmobject_count;
  388. builder.appendf("VMObject: %p %s(%u): p:%4u\n",
  389. &vmobject,
  390. vmobject.is_anonymous() ? "anon" : "file",
  391. vmobject.ref_count(),
  392. vmobject.page_count());
  393. return IterationDecision::Continue;
  394. });
  395. builder.appendf("VMO count: %u\n", vmobject_count);
  396. builder.appendf("Free physical pages: %u\n", MM.user_physical_pages() - MM.user_physical_pages_used());
  397. builder.appendf("Free supervisor physical pages: %u\n", MM.super_physical_pages() - MM.super_physical_pages_used());
  398. return builder.build();
  399. }
  400. Optional<KBuffer> procfs$dmesg(InodeIdentifier)
  401. {
  402. InterruptDisabler disabler;
  403. KBufferBuilder builder;
  404. for (char ch : Console::the().logbuffer())
  405. builder.append(ch);
  406. return builder.build();
  407. }
  408. Optional<KBuffer> procfs$mounts(InodeIdentifier)
  409. {
  410. // FIXME: This is obviously racy against the VFS mounts changing.
  411. KBufferBuilder builder;
  412. VFS::the().for_each_mount([&builder](auto& mount) {
  413. auto& fs = mount.guest_fs();
  414. builder.appendf("%s @ ", fs.class_name());
  415. if (!mount.host().is_valid())
  416. builder.appendf("/");
  417. else {
  418. builder.appendf("%u:%u", mount.host().fsid(), mount.host().index());
  419. builder.append(' ');
  420. builder.append(mount.absolute_path());
  421. }
  422. builder.append('\n');
  423. });
  424. return builder.build();
  425. }
  426. Optional<KBuffer> procfs$df(InodeIdentifier)
  427. {
  428. // FIXME: This is obviously racy against the VFS mounts changing.
  429. JsonArray json;
  430. VFS::the().for_each_mount([&json](auto& mount) {
  431. auto& fs = mount.guest_fs();
  432. JsonObject fs_object;
  433. fs_object.set("class_name", fs.class_name());
  434. fs_object.set("total_block_count", fs.total_block_count());
  435. fs_object.set("free_block_count", fs.free_block_count());
  436. fs_object.set("total_inode_count", fs.total_inode_count());
  437. fs_object.set("free_inode_count", fs.free_inode_count());
  438. fs_object.set("mount_point", mount.absolute_path());
  439. fs_object.set("block_size", fs.block_size());
  440. fs_object.set("readonly", fs.is_readonly());
  441. json.append(fs_object);
  442. });
  443. return json.serialized<KBufferBuilder>();
  444. }
  445. Optional<KBuffer> procfs$cpuinfo(InodeIdentifier)
  446. {
  447. KBufferBuilder builder;
  448. {
  449. CPUID cpuid(0);
  450. builder.appendf("cpuid: ");
  451. auto emit_u32 = [&](u32 value) {
  452. builder.appendf("%c%c%c%c",
  453. value & 0xff,
  454. (value >> 8) & 0xff,
  455. (value >> 16) & 0xff,
  456. (value >> 24) & 0xff);
  457. };
  458. emit_u32(cpuid.ebx());
  459. emit_u32(cpuid.edx());
  460. emit_u32(cpuid.ecx());
  461. builder.appendf("\n");
  462. }
  463. {
  464. CPUID cpuid(1);
  465. u32 stepping = cpuid.eax() & 0xf;
  466. u32 model = (cpuid.eax() >> 4) & 0xf;
  467. u32 family = (cpuid.eax() >> 8) & 0xf;
  468. u32 type = (cpuid.eax() >> 12) & 0x3;
  469. u32 extended_model = (cpuid.eax() >> 16) & 0xf;
  470. u32 extended_family = (cpuid.eax() >> 20) & 0xff;
  471. u32 display_model;
  472. u32 display_family;
  473. if (family == 15) {
  474. display_family = family + extended_family;
  475. display_model = model + (extended_model << 4);
  476. } else if (family == 6) {
  477. display_family = family;
  478. display_model = model + (extended_model << 4);
  479. } else {
  480. display_family = family;
  481. display_model = model;
  482. }
  483. builder.appendf("family: %u\n", display_family);
  484. builder.appendf("model: %u\n", display_model);
  485. builder.appendf("stepping: %u\n", stepping);
  486. builder.appendf("type: %u\n", type);
  487. }
  488. {
  489. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  490. // and verifying that the returned eax>=0x80000004.
  491. alignas(u32) char buffer[48];
  492. u32* bufptr = reinterpret_cast<u32*>(buffer);
  493. auto copy_brand_string_part_to_buffer = [&](u32 i) {
  494. CPUID cpuid(0x80000002 + i);
  495. *bufptr++ = cpuid.eax();
  496. *bufptr++ = cpuid.ebx();
  497. *bufptr++ = cpuid.ecx();
  498. *bufptr++ = cpuid.edx();
  499. };
  500. copy_brand_string_part_to_buffer(0);
  501. copy_brand_string_part_to_buffer(1);
  502. copy_brand_string_part_to_buffer(2);
  503. builder.appendf("brandstr: \"%s\"\n", buffer);
  504. }
  505. return builder.build();
  506. }
  507. Optional<KBuffer> procfs$memstat(InodeIdentifier)
  508. {
  509. InterruptDisabler disabler;
  510. JsonObject json;
  511. json.set("kmalloc_allocated", sum_alloc);
  512. json.set("kmalloc_available", sum_free);
  513. json.set("kmalloc_eternal_allocated", kmalloc_sum_eternal);
  514. json.set("user_physical_allocated", MM.user_physical_pages_used());
  515. json.set("user_physical_available", MM.user_physical_pages());
  516. json.set("super_physical_allocated", MM.super_physical_pages_used());
  517. json.set("super_physical_available", MM.super_physical_pages());
  518. json.set("kmalloc_call_count", g_kmalloc_call_count);
  519. json.set("kfree_call_count", g_kfree_call_count);
  520. return json.serialized<KBufferBuilder>();;
  521. }
  522. Optional<KBuffer> procfs$all(InodeIdentifier)
  523. {
  524. InterruptDisabler disabler;
  525. auto processes = Process::all_processes();
  526. JsonArray array;
  527. // Keep this in sync with CProcessStatistics.
  528. auto build_process = [&](const Process& process) {
  529. JsonObject process_object;
  530. process_object.set("pid", process.pid());
  531. process_object.set("times_scheduled", process.main_thread().times_scheduled());
  532. process_object.set("pgid", process.tty() ? process.tty()->pgid() : 0);
  533. process_object.set("pgp", process.pgid());
  534. process_object.set("sid", process.sid());
  535. process_object.set("uid", process.uid());
  536. process_object.set("gid", process.gid());
  537. process_object.set("state", process.main_thread().state_string());
  538. process_object.set("ppid", process.ppid());
  539. process_object.set("nfds", process.number_of_open_file_descriptors());
  540. process_object.set("name", process.name());
  541. process_object.set("tty", process.tty() ? process.tty()->tty_name() : "notty");
  542. process_object.set("amount_virtual", process.amount_virtual());
  543. process_object.set("amount_resident", process.amount_resident());
  544. process_object.set("amount_shared", process.amount_shared());
  545. process_object.set("ticks", process.main_thread().ticks());
  546. process_object.set("priority", to_string(process.priority()));
  547. process_object.set("syscall_count", process.syscall_count());
  548. process_object.set("icon_id", process.icon_id());
  549. array.append(process_object);
  550. };
  551. build_process(*Scheduler::colonel());
  552. for (auto* process : processes)
  553. build_process(*process);
  554. return array.serialized<KBufferBuilder>();
  555. }
  556. Optional<KBuffer> procfs$inodes(InodeIdentifier)
  557. {
  558. extern InlineLinkedList<Inode>& all_inodes();
  559. KBufferBuilder builder;
  560. InterruptDisabler disabler;
  561. for (auto& inode : all_inodes()) {
  562. builder.appendf("Inode{K%x} %02u:%08u (%u)\n", &inode, inode.fsid(), inode.index(), inode.ref_count());
  563. }
  564. return builder.build();
  565. }
  566. struct SysVariableData final : public ProcFSInodeCustomData {
  567. virtual ~SysVariableData() override {}
  568. enum Type {
  569. Invalid,
  570. Boolean,
  571. String,
  572. };
  573. Type type { Invalid };
  574. Function<void()> notify_callback;
  575. void* address;
  576. };
  577. static ByteBuffer read_sys_bool(InodeIdentifier inode_id)
  578. {
  579. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  580. if (!inode_ptr)
  581. return {};
  582. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  583. ASSERT(inode.custom_data());
  584. auto buffer = ByteBuffer::create_uninitialized(2);
  585. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  586. ASSERT(custom_data.type == SysVariableData::Boolean);
  587. ASSERT(custom_data.address);
  588. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(custom_data.address);
  589. {
  590. LOCKER(lockable_bool->lock());
  591. buffer[0] = lockable_bool->resource() ? '1' : '0';
  592. }
  593. buffer[1] = '\n';
  594. return buffer;
  595. }
  596. static ssize_t write_sys_bool(InodeIdentifier inode_id, const ByteBuffer& data)
  597. {
  598. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  599. if (!inode_ptr)
  600. return {};
  601. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  602. ASSERT(inode.custom_data());
  603. if (data.is_empty() || !(data[0] == '0' || data[0] == '1'))
  604. return data.size();
  605. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  606. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(custom_data.address);
  607. {
  608. LOCKER(lockable_bool->lock());
  609. lockable_bool->resource() = data[0] == '1';
  610. }
  611. if (custom_data.notify_callback)
  612. custom_data.notify_callback();
  613. return data.size();
  614. }
  615. static ByteBuffer read_sys_string(InodeIdentifier inode_id)
  616. {
  617. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  618. if (!inode_ptr)
  619. return {};
  620. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  621. ASSERT(inode.custom_data());
  622. auto buffer = ByteBuffer::create_uninitialized(2);
  623. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  624. ASSERT(custom_data.type == SysVariableData::String);
  625. ASSERT(custom_data.address);
  626. auto* lockable_string = reinterpret_cast<Lockable<String>*>(custom_data.address);
  627. LOCKER(lockable_string->lock());
  628. return lockable_string->resource().to_byte_buffer();
  629. }
  630. static ssize_t write_sys_string(InodeIdentifier inode_id, const ByteBuffer& data)
  631. {
  632. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  633. if (!inode_ptr)
  634. return {};
  635. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  636. ASSERT(inode.custom_data());
  637. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  638. ASSERT(custom_data.address);
  639. {
  640. auto* lockable_string = reinterpret_cast<Lockable<String>*>(custom_data.address);
  641. LOCKER(lockable_string->lock());
  642. lockable_string->resource() = String((const char*)data.pointer(), data.size());
  643. }
  644. if (custom_data.notify_callback)
  645. custom_data.notify_callback();
  646. return data.size();
  647. }
  648. void ProcFS::add_sys_bool(String&& name, Lockable<bool>& var, Function<void()>&& notify_callback)
  649. {
  650. InterruptDisabler disabler;
  651. int index = m_sys_entries.size();
  652. auto inode = adopt(*new ProcFSInode(*this, sys_var_to_identifier(fsid(), index).index()));
  653. auto data = make<SysVariableData>();
  654. data->type = SysVariableData::Boolean;
  655. data->notify_callback = move(notify_callback);
  656. data->address = &var;
  657. inode->set_custom_data(move(data));
  658. m_sys_entries.empend(strdup(name.characters()), 0, read_sys_bool, write_sys_bool, move(inode));
  659. }
  660. void ProcFS::add_sys_string(String&& name, Lockable<String>& var, Function<void()>&& notify_callback)
  661. {
  662. InterruptDisabler disabler;
  663. int index = m_sys_entries.size();
  664. auto inode = adopt(*new ProcFSInode(*this, sys_var_to_identifier(fsid(), index).index()));
  665. auto data = make<SysVariableData>();
  666. data->type = SysVariableData::String;
  667. data->notify_callback = move(notify_callback);
  668. data->address = &var;
  669. inode->set_custom_data(move(data));
  670. m_sys_entries.empend(strdup(name.characters()), 0, read_sys_string, write_sys_string, move(inode));
  671. }
  672. bool ProcFS::initialize()
  673. {
  674. return true;
  675. }
  676. const char* ProcFS::class_name() const
  677. {
  678. return "ProcFS";
  679. }
  680. RefPtr<Inode> ProcFS::create_inode(InodeIdentifier, const String&, mode_t, off_t, dev_t, int&)
  681. {
  682. kprintf("FIXME: Implement ProcFS::create_inode()?\n");
  683. return {};
  684. }
  685. RefPtr<Inode> ProcFS::create_directory(InodeIdentifier, const String&, mode_t, int& error)
  686. {
  687. error = -EROFS;
  688. return nullptr;
  689. }
  690. InodeIdentifier ProcFS::root_inode() const
  691. {
  692. return { fsid(), FI_Root };
  693. }
  694. RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  695. {
  696. #ifdef PROCFS_DEBUG
  697. dbgprintf("ProcFS::get_inode(%u)\n", inode_id.index());
  698. #endif
  699. if (inode_id == root_inode())
  700. return m_root_inode;
  701. if (to_proc_parent_directory(inode_id) == ProcParentDirectory::PDI_Root_sys) {
  702. auto sys_index = to_sys_index(inode_id);
  703. if (sys_index < m_sys_entries.size())
  704. return m_sys_entries[sys_index].inode;
  705. }
  706. LOCKER(m_inodes_lock);
  707. auto it = m_inodes.find(inode_id.index());
  708. if (it == m_inodes.end()) {
  709. auto inode = adopt(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  710. m_inodes.set(inode_id.index(), inode.ptr());
  711. return inode;
  712. }
  713. return (*it).value;
  714. }
  715. ProcFSInode::ProcFSInode(ProcFS& fs, unsigned index)
  716. : Inode(fs, index)
  717. {
  718. }
  719. ProcFSInode::~ProcFSInode()
  720. {
  721. LOCKER(fs().m_inodes_lock);
  722. fs().m_inodes.remove(index());
  723. }
  724. InodeMetadata ProcFSInode::metadata() const
  725. {
  726. #ifdef PROCFS_DEBUG
  727. dbgprintf("ProcFSInode::metadata(%u)\n", index());
  728. #endif
  729. InodeMetadata metadata;
  730. metadata.inode = identifier();
  731. metadata.ctime = mepoch;
  732. metadata.atime = mepoch;
  733. metadata.mtime = mepoch;
  734. auto proc_parent_directory = to_proc_parent_directory(identifier());
  735. auto pid = to_pid(identifier());
  736. auto proc_file_type = to_proc_file_type(identifier());
  737. #ifdef PROCFS_DEBUG
  738. dbgprintf(" -> pid: %d, fi: %u, pdi: %u\n", pid, proc_file_type, proc_parent_directory);
  739. #endif
  740. if (is_process_related_file(identifier())) {
  741. auto handle = ProcessInspectionHandle::from_pid(pid);
  742. metadata.uid = handle->process().sys$getuid();
  743. metadata.gid = handle->process().sys$getgid();
  744. }
  745. if (proc_parent_directory == PDI_PID_fd) {
  746. metadata.mode = 00120777;
  747. return metadata;
  748. }
  749. if (proc_parent_directory == PDI_Root_sys) {
  750. metadata.mode = 00100644;
  751. return metadata;
  752. }
  753. switch (proc_file_type) {
  754. case FI_Root_self:
  755. case FI_PID_cwd:
  756. case FI_PID_exe:
  757. metadata.mode = 0120777;
  758. break;
  759. case FI_Root:
  760. case FI_Root_sys:
  761. case FI_Root_net:
  762. case FI_PID:
  763. case FI_PID_fd:
  764. metadata.mode = 040777;
  765. break;
  766. default:
  767. metadata.mode = 0100644;
  768. break;
  769. }
  770. #ifdef PROCFS_DEBUG
  771. dbgprintf("Returning mode %o\n", metadata.mode);
  772. #endif
  773. return metadata;
  774. }
  775. ssize_t ProcFSInode::read_bytes(off_t offset, ssize_t count, u8* buffer, FileDescription* description) const
  776. {
  777. #ifdef PROCFS_DEBUG
  778. dbgprintf("ProcFS: read_bytes %u\n", index());
  779. #endif
  780. ASSERT(offset >= 0);
  781. ASSERT(buffer);
  782. auto* directory_entry = fs().get_directory_entry(identifier());
  783. Function<Optional<KBuffer>(InodeIdentifier)> callback_tmp;
  784. Function<Optional<KBuffer>(InodeIdentifier)>* read_callback { nullptr };
  785. if (directory_entry) {
  786. read_callback = &directory_entry->read_callback;
  787. } else {
  788. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  789. callback_tmp = procfs$pid_fd_entry;
  790. read_callback = &callback_tmp;
  791. }
  792. }
  793. ASSERT(read_callback);
  794. Optional<KBuffer> generated_data;
  795. if (!description) {
  796. generated_data = (*read_callback)(identifier());
  797. } else {
  798. if (!description->generator_cache())
  799. description->generator_cache() = (*read_callback)(identifier());
  800. generated_data = description->generator_cache();
  801. }
  802. auto& data = generated_data;
  803. ssize_t nread = min(static_cast<off_t>(data.value().size() - offset), static_cast<off_t>(count));
  804. memcpy(buffer, data.value().data() + offset, nread);
  805. if (nread == 0 && description && description->generator_cache())
  806. description->generator_cache().clear();
  807. return nread;
  808. }
  809. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  810. {
  811. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  812. }
  813. bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  814. {
  815. #ifdef PROCFS_DEBUG
  816. dbgprintf("ProcFS: traverse_as_directory %u\n", index());
  817. #endif
  818. if (!::is_directory(identifier()))
  819. return false;
  820. auto pid = to_pid(identifier());
  821. auto proc_file_type = to_proc_file_type(identifier());
  822. auto parent_id = to_parent_id(identifier());
  823. callback({ ".", 1, identifier(), 2 });
  824. callback({ "..", 2, parent_id, 2 });
  825. switch (proc_file_type) {
  826. case FI_Root:
  827. for (auto& entry : fs().m_entries) {
  828. // FIXME: strlen() here is sad.
  829. if (!entry.name)
  830. continue;
  831. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  832. callback({ entry.name, (int)strlen(entry.name), to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  833. }
  834. for (auto pid_child : Process::all_pids()) {
  835. char name[16];
  836. int name_length = ksprintf(name, "%u", pid_child);
  837. callback({ name, name_length, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  838. }
  839. break;
  840. case FI_Root_sys:
  841. for (int i = 0; i < fs().m_sys_entries.size(); ++i) {
  842. auto& entry = fs().m_sys_entries[i];
  843. callback({ entry.name, (int)strlen(entry.name), sys_var_to_identifier(fsid(), i), 0 });
  844. }
  845. break;
  846. case FI_Root_net:
  847. callback({ "adapters", 8, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_adapters), 0 });
  848. callback({ "tcp", 3, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_tcp), 0 });
  849. callback({ "udp", 3, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_udp), 0 });
  850. break;
  851. case FI_PID: {
  852. auto handle = ProcessInspectionHandle::from_pid(pid);
  853. if (!handle)
  854. return false;
  855. auto& process = handle->process();
  856. for (auto& entry : fs().m_entries) {
  857. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  858. if (entry.proc_file_type == FI_PID_exe && !process.executable())
  859. continue;
  860. // FIXME: strlen() here is sad.
  861. callback({ entry.name, (int)strlen(entry.name), to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  862. }
  863. }
  864. } break;
  865. case FI_PID_fd: {
  866. auto handle = ProcessInspectionHandle::from_pid(pid);
  867. if (!handle)
  868. return false;
  869. auto& process = handle->process();
  870. for (int i = 0; i < process.max_open_file_descriptors(); ++i) {
  871. auto* description = process.file_description(i);
  872. if (!description)
  873. continue;
  874. char name[16];
  875. int name_length = ksprintf(name, "%u", i);
  876. callback({ name, name_length, to_identifier_with_fd(fsid(), pid, i), 0 });
  877. }
  878. } break;
  879. default:
  880. return true;
  881. }
  882. return true;
  883. }
  884. InodeIdentifier ProcFSInode::lookup(StringView name)
  885. {
  886. ASSERT(is_directory());
  887. if (name == ".")
  888. return identifier();
  889. if (name == "..")
  890. return to_parent_id(identifier());
  891. auto proc_file_type = to_proc_file_type(identifier());
  892. if (proc_file_type == FI_Root) {
  893. for (auto& entry : fs().m_entries) {
  894. if (entry.name == nullptr)
  895. continue;
  896. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  897. if (name == entry.name) {
  898. return to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type);
  899. }
  900. }
  901. }
  902. bool ok;
  903. unsigned name_as_number = name.to_uint(ok);
  904. if (ok) {
  905. bool process_exists = false;
  906. {
  907. InterruptDisabler disabler;
  908. process_exists = Process::from_pid(name_as_number);
  909. }
  910. if (process_exists)
  911. return to_identifier(fsid(), PDI_Root, name_as_number, FI_PID);
  912. }
  913. return {};
  914. }
  915. if (proc_file_type == FI_Root_sys) {
  916. for (int i = 0; i < fs().m_sys_entries.size(); ++i) {
  917. auto& entry = fs().m_sys_entries[i];
  918. if (name == entry.name)
  919. return sys_var_to_identifier(fsid(), i);
  920. }
  921. return {};
  922. }
  923. if (proc_file_type == FI_Root_net) {
  924. if (name == "adapters")
  925. return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_adapters);
  926. if (name == "tcp")
  927. return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_tcp);
  928. if (name == "udp")
  929. return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_udp);
  930. return {};
  931. }
  932. if (proc_file_type == FI_PID) {
  933. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier()));
  934. if (!handle)
  935. return {};
  936. auto& process = handle->process();
  937. for (auto& entry : fs().m_entries) {
  938. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  939. if (entry.proc_file_type == FI_PID_exe && !process.executable())
  940. continue;
  941. if (entry.name == nullptr)
  942. continue;
  943. if (name == entry.name) {
  944. return to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type);
  945. }
  946. }
  947. }
  948. return {};
  949. }
  950. if (proc_file_type == FI_PID_fd) {
  951. bool ok;
  952. unsigned name_as_number = name.to_uint(ok);
  953. if (ok) {
  954. bool fd_exists = false;
  955. {
  956. InterruptDisabler disabler;
  957. if (auto* process = Process::from_pid(to_pid(identifier())))
  958. fd_exists = process->file_description(name_as_number);
  959. }
  960. if (fd_exists)
  961. return to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number);
  962. }
  963. }
  964. return {};
  965. }
  966. void ProcFSInode::flush_metadata()
  967. {
  968. }
  969. ssize_t ProcFSInode::write_bytes(off_t offset, ssize_t size, const u8* buffer, FileDescription*)
  970. {
  971. auto* directory_entry = fs().get_directory_entry(identifier());
  972. if (!directory_entry || !directory_entry->write_callback)
  973. return -EPERM;
  974. ASSERT(is_persistent_inode(identifier()));
  975. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  976. ASSERT(offset == 0);
  977. bool success = directory_entry->write_callback(identifier(), ByteBuffer::wrap(buffer, size));
  978. ASSERT(success);
  979. return 0;
  980. }
  981. KResult ProcFSInode::add_child(InodeIdentifier child_id, const StringView& name, mode_t)
  982. {
  983. (void)child_id;
  984. (void)name;
  985. return KResult(-EPERM);
  986. }
  987. KResult ProcFSInode::remove_child(const StringView& name)
  988. {
  989. (void)name;
  990. return KResult(-EPERM);
  991. }
  992. ProcFSInodeCustomData::~ProcFSInodeCustomData()
  993. {
  994. }
  995. size_t ProcFSInode::directory_entry_count() const
  996. {
  997. ASSERT(is_directory());
  998. size_t count = 0;
  999. traverse_as_directory([&count](const FS::DirectoryEntry&) {
  1000. ++count;
  1001. return true;
  1002. });
  1003. return count;
  1004. }
  1005. KResult ProcFSInode::chmod(mode_t)
  1006. {
  1007. return KResult(-EPERM);
  1008. }
  1009. ProcFS::ProcFS()
  1010. {
  1011. s_the = this;
  1012. m_root_inode = adopt(*new ProcFSInode(*this, 1));
  1013. m_entries.resize(FI_MaxStaticFileIndex);
  1014. m_entries[FI_Root_mm] = { "mm", FI_Root_mm, procfs$mm };
  1015. m_entries[FI_Root_mounts] = { "mounts", FI_Root_mounts, procfs$mounts };
  1016. m_entries[FI_Root_df] = { "df", FI_Root_df, procfs$df };
  1017. m_entries[FI_Root_all] = { "all", FI_Root_all, procfs$all };
  1018. m_entries[FI_Root_memstat] = { "memstat", FI_Root_memstat, procfs$memstat };
  1019. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, procfs$cpuinfo };
  1020. m_entries[FI_Root_inodes] = { "inodes", FI_Root_inodes, procfs$inodes };
  1021. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };
  1022. m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self };
  1023. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, procfs$pci };
  1024. m_entries[FI_Root_uptime] = { "uptime", FI_Root_uptime, procfs$uptime };
  1025. m_entries[FI_Root_cmdline] = { "cmdline", FI_Root_cmdline, procfs$cmdline };
  1026. m_entries[FI_Root_sys] = { "sys", FI_Root_sys };
  1027. m_entries[FI_Root_net] = { "net", FI_Root_net };
  1028. m_entries[FI_Root_net_adapters] = { "adapters", FI_Root_net_adapters, procfs$net_adapters };
  1029. m_entries[FI_Root_net_tcp] = { "tcp", FI_Root_net_tcp, procfs$net_tcp };
  1030. m_entries[FI_Root_net_udp] = { "udp", FI_Root_net_udp, procfs$net_udp };
  1031. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
  1032. m_entries[FI_PID_vmo] = { "vmo", FI_PID_vmo, procfs$pid_vmo };
  1033. m_entries[FI_PID_stack] = { "stack", FI_PID_stack, procfs$pid_stack };
  1034. m_entries[FI_PID_regs] = { "regs", FI_PID_regs, procfs$pid_regs };
  1035. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, procfs$pid_fds };
  1036. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, procfs$pid_exe };
  1037. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, procfs$pid_cwd };
  1038. m_entries[FI_PID_fd] = { "fd", FI_PID_fd };
  1039. m_kmalloc_stack_helper.resource() = g_dump_kmalloc_stacks;
  1040. add_sys_bool("kmalloc_stacks", m_kmalloc_stack_helper, [this] {
  1041. g_dump_kmalloc_stacks = m_kmalloc_stack_helper.resource();
  1042. });
  1043. }
  1044. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  1045. {
  1046. if (to_proc_parent_directory(identifier) == PDI_Root_sys) {
  1047. auto sys_index = to_sys_index(identifier);
  1048. if (sys_index < m_sys_entries.size())
  1049. return const_cast<ProcFSDirectoryEntry*>(&m_sys_entries[sys_index]);
  1050. return nullptr;
  1051. }
  1052. auto proc_file_type = to_proc_file_type(identifier);
  1053. if (proc_file_type != FI_Invalid && proc_file_type < FI_MaxStaticFileIndex)
  1054. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1055. return nullptr;
  1056. }
  1057. KResult ProcFSInode::chown(uid_t, gid_t)
  1058. {
  1059. return KResult(-EPERM);
  1060. }