ProcFS.cpp 32 KB

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