ProcFS.cpp 43 KB

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