ProcFS.cpp 36 KB

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