ProcFS.cpp 44 KB

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