ProcFS.cpp 37 KB

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