ProcFS.cpp 35 KB

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