ProcFS.cpp 43 KB

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