ProcFS.cpp 42 KB

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