ProcFS.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. }
  98. #if 0
  99. static inline byte to_unused_metadata(const InodeIdentifier& identifier)
  100. {
  101. return (identifier.index() >> 8) & 0xf;
  102. }
  103. #endif
  104. static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
  105. {
  106. return (ProcFileType)(identifier.index() & 0xff);
  107. }
  108. static inline bool is_process_related_file(const InodeIdentifier& identifier)
  109. {
  110. if (to_proc_file_type(identifier) == FI_PID)
  111. return true;
  112. auto proc_parent_directory = to_proc_parent_directory(identifier);
  113. switch (proc_parent_directory) {
  114. case PDI_PID:
  115. case PDI_PID_fd:
  116. return true;
  117. default:
  118. return false;
  119. }
  120. }
  121. static inline bool is_directory(const InodeIdentifier& identifier)
  122. {
  123. auto proc_file_type = to_proc_file_type(identifier);
  124. switch (proc_file_type) {
  125. case FI_Root:
  126. case FI_Root_sys:
  127. case FI_PID:
  128. case FI_PID_fd:
  129. return true;
  130. default:
  131. return false;
  132. }
  133. }
  134. static inline bool is_persistent_inode(const InodeIdentifier& identifier)
  135. {
  136. return to_proc_parent_directory(identifier) == PDI_Root_sys;
  137. }
  138. static ProcFS* s_the;
  139. ProcFS& ProcFS::the()
  140. {
  141. ASSERT(s_the);
  142. return *s_the;
  143. }
  144. RetainPtr<ProcFS> ProcFS::create()
  145. {
  146. return adopt(*new ProcFS);
  147. }
  148. ProcFS::~ProcFS()
  149. {
  150. }
  151. ByteBuffer procfs$pid_fds(InodeIdentifier identifier)
  152. {
  153. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  154. if (!handle)
  155. return { };
  156. auto& process = handle->process();
  157. if (process.number_of_open_file_descriptors() == 0)
  158. return { };
  159. StringBuilder builder;
  160. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  161. auto* descriptor = process.file_descriptor(i);
  162. if (!descriptor)
  163. continue;
  164. builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters());
  165. }
  166. return builder.to_byte_buffer();
  167. }
  168. ByteBuffer procfs$pid_fd_entry(InodeIdentifier identifier)
  169. {
  170. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  171. if (!handle)
  172. return { };
  173. auto& process = handle->process();
  174. int fd = to_fd(identifier);
  175. auto* descriptor = process.file_descriptor(fd);
  176. if (!descriptor)
  177. return { };
  178. return descriptor->absolute_path().to_byte_buffer();
  179. }
  180. ByteBuffer procfs$pid_vm(InodeIdentifier identifier)
  181. {
  182. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  183. if (!handle)
  184. return { };
  185. auto& process = handle->process();
  186. StringBuilder builder;
  187. builder.appendf("BEGIN END SIZE COMMIT FLAGS NAME\n");
  188. for (auto& region : process.regions()) {
  189. StringBuilder flags_builder;
  190. if (region->is_readable())
  191. flags_builder.append('R');
  192. if (region->is_writable())
  193. flags_builder.append('W');
  194. if (region->is_bitmap())
  195. flags_builder.append('B');
  196. builder.appendf("%x -- %x %x %x % 4s %s\n",
  197. region->laddr().get(),
  198. region->laddr().offset(region->size() - 1).get(),
  199. region->size(),
  200. region->amount_resident(),
  201. flags_builder.to_string().characters(),
  202. region->name().characters());
  203. }
  204. return builder.to_byte_buffer();
  205. }
  206. ByteBuffer procfs$pci(InodeIdentifier)
  207. {
  208. StringBuilder builder;
  209. PCI::enumerate_all([&builder] (PCI::Address address, PCI::ID id) {
  210. builder.appendf("%b:%b.%b %w:%w\n", address.bus(), address.slot(), address.function(), id.vendor_id, id.device_id);
  211. });
  212. return builder.to_byte_buffer();
  213. }
  214. ByteBuffer procfs$pid_vmo(InodeIdentifier identifier)
  215. {
  216. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  217. if (!handle)
  218. return { };
  219. auto& process = handle->process();
  220. StringBuilder builder;
  221. builder.appendf("BEGIN END SIZE NAME\n");
  222. for (auto& region : process.regions()) {
  223. builder.appendf("%x -- %x %x %s\n",
  224. region->laddr().get(),
  225. region->laddr().offset(region->size() - 1).get(),
  226. region->size(),
  227. region->name().characters());
  228. builder.appendf("VMO: %s \"%s\" @ %x(%u)\n",
  229. region->vmo().is_anonymous() ? "anonymous" : "file-backed",
  230. region->vmo().name().characters(),
  231. &region->vmo(),
  232. region->vmo().retain_count());
  233. for (size_t i = 0; i < region->vmo().page_count(); ++i) {
  234. auto& physical_page = region->vmo().physical_pages()[i];
  235. builder.appendf("P%x%s(%u) ",
  236. physical_page ? physical_page->paddr().get() : 0,
  237. region->cow_map().get(i) ? "!" : "",
  238. physical_page ? physical_page->retain_count() : 0
  239. );
  240. }
  241. builder.appendf("\n");
  242. }
  243. return builder.to_byte_buffer();
  244. }
  245. ByteBuffer procfs$pid_stack(InodeIdentifier identifier)
  246. {
  247. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  248. if (!handle)
  249. return { };
  250. auto& process = handle->process();
  251. ProcessPagingScope paging_scope(process);
  252. struct RecognizedSymbol {
  253. dword address;
  254. const KSym* ksym;
  255. };
  256. Vector<RecognizedSymbol> recognized_symbols;
  257. if (auto* eip_ksym = ksymbolicate(process.tss().eip))
  258. recognized_symbols.append({ process.tss().eip, eip_ksym });
  259. for (dword* stack_ptr = (dword*)process.frame_ptr(); process.validate_read_from_kernel(LinearAddress((dword)stack_ptr)); stack_ptr = (dword*)*stack_ptr) {
  260. dword retaddr = stack_ptr[1];
  261. if (auto* ksym = ksymbolicate(retaddr))
  262. recognized_symbols.append({ retaddr, ksym });
  263. }
  264. StringBuilder builder;
  265. for (auto& symbol : recognized_symbols) {
  266. unsigned offset = symbol.address - symbol.ksym->address;
  267. builder.appendf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
  268. }
  269. return builder.to_byte_buffer();
  270. }
  271. ByteBuffer procfs$pid_regs(InodeIdentifier identifier)
  272. {
  273. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  274. if (!handle)
  275. return { };
  276. auto& process = handle->process();
  277. auto& tss = process.tss();
  278. StringBuilder builder;
  279. builder.appendf("eax: %x\n", tss.eax);
  280. builder.appendf("ebx: %x\n", tss.ebx);
  281. builder.appendf("ecx: %x\n", tss.ecx);
  282. builder.appendf("edx: %x\n", tss.edx);
  283. builder.appendf("esi: %x\n", tss.esi);
  284. builder.appendf("edi: %x\n", tss.edi);
  285. builder.appendf("ebp: %x\n", tss.ebp);
  286. builder.appendf("cr3: %x\n", tss.cr3);
  287. builder.appendf("flg: %x\n", tss.eflags);
  288. builder.appendf("sp: %w:%x\n", tss.ss, tss.esp);
  289. builder.appendf("pc: %w:%x\n", tss.cs, tss.eip);
  290. return builder.to_byte_buffer();
  291. }
  292. ByteBuffer procfs$pid_exe(InodeIdentifier identifier)
  293. {
  294. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  295. if (!handle)
  296. return { };
  297. auto& process = handle->process();
  298. auto inode = process.executable_inode();
  299. ASSERT(inode);
  300. return VFS::the().absolute_path(*inode).to_byte_buffer();
  301. }
  302. ByteBuffer procfs$pid_cwd(InodeIdentifier identifier)
  303. {
  304. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
  305. if (!handle)
  306. return { };
  307. auto& process = handle->process();
  308. auto inode = process.cwd_inode();
  309. ASSERT(inode);
  310. return VFS::the().absolute_path(*inode).to_byte_buffer();
  311. }
  312. ByteBuffer procfs$self(InodeIdentifier)
  313. {
  314. char buffer[16];
  315. ksprintf(buffer, "%u", current->pid());
  316. return ByteBuffer::copy((const byte*)buffer, strlen(buffer));
  317. }
  318. ByteBuffer procfs$mm(InodeIdentifier)
  319. {
  320. // FIXME: Implement
  321. InterruptDisabler disabler;
  322. StringBuilder builder;
  323. for (auto* vmo : MM.m_vmos) {
  324. builder.appendf("VMO: %p %s(%u): p:%4u %s\n",
  325. vmo,
  326. vmo->is_anonymous() ? "anon" : "file",
  327. vmo->retain_count(),
  328. vmo->page_count(),
  329. vmo->name().characters());
  330. }
  331. builder.appendf("VMO count: %u\n", MM.m_vmos.size());
  332. builder.appendf("Free physical pages: %u\n", MM.m_free_physical_pages.size());
  333. builder.appendf("Free supervisor physical pages: %u\n", MM.m_free_supervisor_physical_pages.size());
  334. return builder.to_byte_buffer();
  335. }
  336. ByteBuffer procfs$dmesg(InodeIdentifier)
  337. {
  338. InterruptDisabler disabler;
  339. StringBuilder builder;
  340. for (char ch : Console::the().logbuffer())
  341. builder.append(ch);
  342. return builder.to_byte_buffer();
  343. }
  344. ByteBuffer procfs$mounts(InodeIdentifier)
  345. {
  346. // FIXME: This is obviously racy against the VFS mounts changing.
  347. StringBuilder builder;
  348. VFS::the().for_each_mount([&builder] (auto& mount) {
  349. auto& fs = mount.guest_fs();
  350. builder.appendf("%s @ ", fs.class_name());
  351. if (!mount.host().is_valid())
  352. builder.appendf("/");
  353. else {
  354. builder.appendf("%u:%u", mount.host().fsid(), mount.host().index());
  355. auto path = VFS::the().absolute_path(mount.host());
  356. builder.append(' ');
  357. builder.append(path);
  358. }
  359. builder.append('\n');
  360. });
  361. return builder.to_byte_buffer();
  362. }
  363. ByteBuffer procfs$cpuinfo(InodeIdentifier)
  364. {
  365. StringBuilder builder;
  366. {
  367. CPUID cpuid(0);
  368. builder.appendf("cpuid: ");
  369. auto emit_dword = [&] (dword value) {
  370. builder.appendf("%c%c%c%c",
  371. value & 0xff,
  372. (value >> 8) & 0xff,
  373. (value >> 16) & 0xff,
  374. (value >> 24) & 0xff);
  375. };
  376. emit_dword(cpuid.ebx());
  377. emit_dword(cpuid.edx());
  378. emit_dword(cpuid.ecx());
  379. builder.appendf("\n");
  380. }
  381. {
  382. CPUID cpuid(1);
  383. dword stepping = cpuid.eax() & 0xf;
  384. dword model = (cpuid.eax() >> 4) & 0xf;
  385. dword family = (cpuid.eax() >> 8) & 0xf;
  386. dword type = (cpuid.eax() >> 12) & 0x3;
  387. dword extended_model = (cpuid.eax() >> 16) & 0xf;
  388. dword extended_family = (cpuid.eax() >> 20) & 0xff;
  389. dword display_model;
  390. dword display_family;
  391. if (family == 15) {
  392. display_family = family + extended_family;
  393. display_model = model + (extended_model << 4);
  394. } else if (family == 6) {
  395. display_family = family;
  396. display_model = model + (extended_model << 4);
  397. } else {
  398. display_family = family;
  399. display_model = model;
  400. }
  401. builder.appendf("family: %u\n", display_family);
  402. builder.appendf("model: %u\n", display_model);
  403. builder.appendf("stepping: %u\n", stepping);
  404. builder.appendf("type: %u\n", type);
  405. }
  406. {
  407. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  408. // and verifying that the returned eax>=0x80000004.
  409. char buffer[48];
  410. dword* bufptr = reinterpret_cast<dword*>(buffer);
  411. auto copy_brand_string_part_to_buffer = [&] (dword i) {
  412. CPUID cpuid(0x80000002 + i);
  413. *bufptr++ = cpuid.eax();
  414. *bufptr++ = cpuid.ebx();
  415. *bufptr++ = cpuid.ecx();
  416. *bufptr++ = cpuid.edx();
  417. };
  418. copy_brand_string_part_to_buffer(0);
  419. copy_brand_string_part_to_buffer(1);
  420. copy_brand_string_part_to_buffer(2);
  421. builder.appendf("brandstr: \"%s\"\n", buffer);
  422. }
  423. return builder.to_byte_buffer();
  424. }
  425. ByteBuffer procfs$kmalloc(InodeIdentifier)
  426. {
  427. StringBuilder builder;
  428. builder.appendf(
  429. "eternal: %u\n"
  430. "allocated: %u\n"
  431. "free: %u\n",
  432. kmalloc_sum_eternal,
  433. sum_alloc,
  434. sum_free
  435. );
  436. return builder.to_byte_buffer();
  437. }
  438. ByteBuffer procfs$summary(InodeIdentifier)
  439. {
  440. InterruptDisabler disabler;
  441. auto processes = Process::all_processes();
  442. StringBuilder builder;
  443. builder.appendf("PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
  444. for (auto* process : processes) {
  445. builder.appendf("% 3u % 3u % 3u % 3u % 4u % 8s % 3u % 9u % 3u % 4s %s\n",
  446. process->pid(),
  447. process->tty() ? process->tty()->pgid() : 0,
  448. process->pgid(),
  449. process->sid(),
  450. process->uid(),
  451. to_string(process->state()),
  452. process->ppid(),
  453. process->times_scheduled(),
  454. process->number_of_open_file_descriptors(),
  455. process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
  456. process->name().characters());
  457. }
  458. return builder.to_byte_buffer();
  459. }
  460. ByteBuffer procfs$all(InodeIdentifier)
  461. {
  462. InterruptDisabler disabler;
  463. auto processes = Process::all_processes();
  464. StringBuilder builder;
  465. auto build_process_line = [&builder] (Process* process) {
  466. builder.appendf("%u,%u,%u,%u,%u,%u,%u,%s,%u,%u,%s,%s,%u,%u,%u,%u,%u,%s\n",
  467. process->pid(),
  468. process->times_scheduled(),
  469. process->tty() ? process->tty()->pgid() : 0,
  470. process->pgid(),
  471. process->sid(),
  472. process->uid(),
  473. process->gid(),
  474. to_string(process->state()),
  475. process->ppid(),
  476. process->number_of_open_file_descriptors(),
  477. process->tty() ? process->tty()->tty_name().characters() : "notty",
  478. process->name().characters(),
  479. process->amount_virtual(),
  480. process->amount_resident(),
  481. process->amount_shared(),
  482. process->amount_in_bitmaps(),
  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. (void) child_id;
  945. (void) name;
  946. (void) file_type;
  947. (void) error;
  948. ASSERT_NOT_REACHED();
  949. return false;
  950. }
  951. bool ProcFSInode::remove_child(const String& name, int& error)
  952. {
  953. (void) name;
  954. (void) error;
  955. ASSERT_NOT_REACHED();
  956. return false;
  957. }
  958. ProcFSInodeCustomData::~ProcFSInodeCustomData()
  959. {
  960. }
  961. size_t ProcFSInode::directory_entry_count() const
  962. {
  963. ASSERT(is_directory());
  964. size_t count = 0;
  965. traverse_as_directory([&count] (const FS::DirectoryEntry&) {
  966. ++count;
  967. return true;
  968. });
  969. return count;
  970. }
  971. bool ProcFSInode::chmod(mode_t, int& error)
  972. {
  973. error = -EPERM;
  974. return false;
  975. }
  976. ProcFS::ProcFS()
  977. {
  978. s_the = this;
  979. m_root_inode = adopt(*new ProcFSInode(*this, 1));
  980. m_entries.resize(FI_MaxStaticFileIndex);
  981. m_entries[FI_Root_mm] = { "mm", FI_Root_mm, procfs$mm };
  982. m_entries[FI_Root_mounts] = { "mounts", FI_Root_mounts, procfs$mounts };
  983. m_entries[FI_Root_kmalloc] = { "kmalloc", FI_Root_kmalloc, procfs$kmalloc };
  984. m_entries[FI_Root_all] = { "all", FI_Root_all, procfs$all };
  985. m_entries[FI_Root_summary] = { "summary", FI_Root_summary, procfs$summary };
  986. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, procfs$cpuinfo};
  987. m_entries[FI_Root_inodes] = { "inodes", FI_Root_inodes, procfs$inodes };
  988. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };
  989. m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self };
  990. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, procfs$pci };
  991. m_entries[FI_Root_sys] = { "sys", FI_Root_sys };
  992. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
  993. m_entries[FI_PID_vmo] = { "vmo", FI_PID_vmo, procfs$pid_vmo };
  994. m_entries[FI_PID_stack] = { "stack", FI_PID_stack, procfs$pid_stack };
  995. m_entries[FI_PID_regs] = { "regs", FI_PID_regs, procfs$pid_regs };
  996. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, procfs$pid_fds };
  997. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, procfs$pid_exe };
  998. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, procfs$pid_cwd };
  999. m_entries[FI_PID_fd] = { "fd", FI_PID_fd };
  1000. }
  1001. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  1002. {
  1003. if (to_proc_parent_directory(identifier) == PDI_Root_sys) {
  1004. auto sys_index = to_sys_index(identifier);
  1005. if (sys_index < m_sys_entries.size())
  1006. return const_cast<ProcFSDirectoryEntry*>(&m_sys_entries[sys_index]);
  1007. return nullptr;
  1008. }
  1009. auto proc_file_type = to_proc_file_type(identifier);
  1010. if (proc_file_type != FI_Invalid && proc_file_type < FI_MaxStaticFileIndex)
  1011. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1012. return nullptr;
  1013. }