ProcFS.cpp 43 KB

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