ProcFS.cpp 45 KB

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