ProcFS.cpp 40 KB

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