ProcFS.cpp 42 KB

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