ProcFS.cpp 41 KB

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