ProcFS.cpp 43 KB

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