ProcFS.cpp 48 KB

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