ProcFS.cpp 43 KB

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