ProcFS.cpp 33 KB

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