ProcFS.cpp 45 KB

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