ProcFS.cpp 37 KB

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