ProcFS.cpp 40 KB

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