ProcFS.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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,%u,%s\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. process->ticks(),
  476. to_string(process->priority())
  477. );
  478. };
  479. build_process_line(Scheduler::colonel());
  480. for (auto* process : processes)
  481. build_process_line(process);
  482. return builder.to_byte_buffer();
  483. }
  484. ByteBuffer procfs$inodes(InodeIdentifier)
  485. {
  486. extern HashTable<Inode*>& all_inodes();
  487. auto& vfs = VFS::the();
  488. StringBuilder builder;
  489. for (auto it : all_inodes()) {
  490. RetainPtr<Inode> inode = *it;
  491. String path = vfs.absolute_path(*inode);
  492. builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters());
  493. }
  494. return builder.to_byte_buffer();
  495. }
  496. struct SysVariableData final : public ProcFSInodeCustomData {
  497. virtual ~SysVariableData() override { }
  498. enum Type {
  499. Invalid,
  500. Boolean,
  501. String,
  502. };
  503. Type type { Invalid };
  504. Function<void()> notify_callback;
  505. void* address;
  506. };
  507. static ByteBuffer read_sys_bool(InodeIdentifier inode_id)
  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. auto buffer = ByteBuffer::create_uninitialized(2);
  515. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  516. ASSERT(custom_data.type == SysVariableData::Boolean);
  517. ASSERT(custom_data.address);
  518. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(custom_data.address);
  519. {
  520. LOCKER(lockable_bool->lock());
  521. buffer[0] = lockable_bool->resource() ? '1' : '0';
  522. }
  523. buffer[1] = '\n';
  524. return buffer;
  525. }
  526. static ssize_t write_sys_bool(InodeIdentifier inode_id, const ByteBuffer& data)
  527. {
  528. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  529. if (!inode_ptr)
  530. return { };
  531. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  532. ASSERT(inode.custom_data());
  533. if (data.is_empty() || !(data[0] == '0' || data[0] == '1'))
  534. return data.size();
  535. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  536. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(custom_data.address);
  537. {
  538. LOCKER(lockable_bool->lock());
  539. lockable_bool->resource() = data[0] == '1';
  540. }
  541. if (custom_data.notify_callback)
  542. custom_data.notify_callback();
  543. return data.size();
  544. }
  545. static ByteBuffer read_sys_string(InodeIdentifier inode_id)
  546. {
  547. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  548. if (!inode_ptr)
  549. return { };
  550. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  551. ASSERT(inode.custom_data());
  552. auto buffer = ByteBuffer::create_uninitialized(2);
  553. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  554. ASSERT(custom_data.type == SysVariableData::String);
  555. ASSERT(custom_data.address);
  556. auto* lockable_string = reinterpret_cast<Lockable<String>*>(custom_data.address);
  557. LOCKER(lockable_string->lock());
  558. return lockable_string->resource().to_byte_buffer();
  559. }
  560. static ssize_t write_sys_string(InodeIdentifier inode_id, const ByteBuffer& data)
  561. {
  562. auto inode_ptr = ProcFS::the().get_inode(inode_id);
  563. if (!inode_ptr)
  564. return { };
  565. auto& inode = static_cast<ProcFSInode&>(*inode_ptr);
  566. ASSERT(inode.custom_data());
  567. auto& custom_data = *static_cast<const SysVariableData*>(inode.custom_data());
  568. ASSERT(custom_data.address);
  569. {
  570. auto* lockable_string = reinterpret_cast<Lockable<String>*>(custom_data.address);
  571. LOCKER(lockable_string->lock());
  572. lockable_string->resource() = String((const char*)data.pointer(), data.size());
  573. }
  574. if (custom_data.notify_callback)
  575. custom_data.notify_callback();
  576. return data.size();
  577. }
  578. void ProcFS::add_sys_bool(String&& name, Lockable<bool>& var, Function<void()>&& notify_callback)
  579. {
  580. InterruptDisabler disabler;
  581. unsigned index = m_sys_entries.size();
  582. auto inode = adopt(*new ProcFSInode(*this, sys_var_to_identifier(fsid(), index).index()));
  583. auto data = make<SysVariableData>();
  584. data->type = SysVariableData::Boolean;
  585. data->notify_callback = move(notify_callback);
  586. data->address = &var;
  587. inode->set_custom_data(move(data));
  588. m_sys_entries.append({ strdup(name.characters()), name.length(), read_sys_bool, write_sys_bool, move(inode) });
  589. }
  590. void ProcFS::add_sys_string(String&& name, Lockable<String>& var, Function<void()>&& notify_callback)
  591. {
  592. InterruptDisabler disabler;
  593. unsigned index = m_sys_entries.size();
  594. auto inode = adopt(*new ProcFSInode(*this, sys_var_to_identifier(fsid(), index).index()));
  595. auto data = make<SysVariableData>();
  596. data->type = SysVariableData::String;
  597. data->notify_callback = move(notify_callback);
  598. data->address = &var;
  599. inode->set_custom_data(move(data));
  600. m_sys_entries.append({ strdup(name.characters()), name.length(), read_sys_string, write_sys_string, move(inode) });
  601. }
  602. bool ProcFS::initialize()
  603. {
  604. return true;
  605. }
  606. const char* ProcFS::class_name() const
  607. {
  608. return "ProcFS";
  609. }
  610. RetainPtr<Inode> ProcFS::create_inode(InodeIdentifier parentInode, const String& name, mode_t mode, unsigned size, int& error)
  611. {
  612. (void) parentInode;
  613. (void) name;
  614. (void) mode;
  615. (void) size;
  616. (void) error;
  617. kprintf("FIXME: Implement ProcFS::create_inode()?\n");
  618. return { };
  619. }
  620. RetainPtr<Inode> ProcFS::create_directory(InodeIdentifier, const String&, mode_t, int& error)
  621. {
  622. error = -EROFS;
  623. return nullptr;
  624. }
  625. RetainPtr<Inode> ProcFSInode::parent() const
  626. {
  627. return fs().get_inode(to_parent_id(identifier()));
  628. }
  629. InodeIdentifier ProcFS::root_inode() const
  630. {
  631. return { fsid(), FI_Root };
  632. }
  633. RetainPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  634. {
  635. #ifdef PROCFS_DEBUG
  636. dbgprintf("ProcFS::get_inode(%u)\n", inode_id.index());
  637. #endif
  638. if (inode_id == root_inode())
  639. return m_root_inode;
  640. if (to_proc_parent_directory(inode_id) == ProcParentDirectory::PDI_Root_sys) {
  641. auto sys_index = to_sys_index(inode_id);
  642. if (sys_index < m_sys_entries.size())
  643. return m_sys_entries[sys_index].inode;
  644. }
  645. LOCKER(m_inodes_lock);
  646. auto it = m_inodes.find(inode_id.index());
  647. if (it == m_inodes.end()) {
  648. auto inode = adopt(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  649. m_inodes.set(inode_id.index(), inode.ptr());
  650. return inode;
  651. }
  652. return (*it).value;
  653. }
  654. ProcFSInode::ProcFSInode(ProcFS& fs, unsigned index)
  655. : Inode(fs, index)
  656. {
  657. }
  658. ProcFSInode::~ProcFSInode()
  659. {
  660. LOCKER(fs().m_inodes_lock);
  661. fs().m_inodes.remove(index());
  662. }
  663. InodeMetadata ProcFSInode::metadata() const
  664. {
  665. #ifdef PROCFS_DEBUG
  666. dbgprintf("ProcFSInode::metadata(%u)\n", index());
  667. #endif
  668. InodeMetadata metadata;
  669. metadata.inode = identifier();
  670. metadata.ctime = mepoch;
  671. metadata.atime = mepoch;
  672. metadata.mtime = mepoch;
  673. auto proc_parent_directory = to_proc_parent_directory(identifier());
  674. auto pid = to_pid(identifier());
  675. auto proc_file_type = to_proc_file_type(identifier());
  676. #ifdef PROCFS_DEBUG
  677. dbgprintf(" -> pid: %d, fi: %u, pdi: %u\n", pid, proc_file_type, proc_parent_directory);
  678. #endif
  679. if (is_process_related_file(identifier())) {
  680. auto handle = ProcessInspectionHandle::from_pid(pid);
  681. metadata.uid = handle->process().sys$getuid();
  682. metadata.gid = handle->process().sys$getgid();
  683. }
  684. if (proc_parent_directory == PDI_PID_fd) {
  685. metadata.mode = 00120777;
  686. return metadata;
  687. }
  688. if (proc_parent_directory == PDI_Root_sys) {
  689. metadata.mode = 00100644;
  690. return metadata;
  691. }
  692. switch (proc_file_type) {
  693. case FI_Root_self:
  694. case FI_PID_cwd:
  695. case FI_PID_exe:
  696. metadata.mode = 0120777;
  697. break;
  698. case FI_Root:
  699. case FI_Root_sys:
  700. case FI_PID:
  701. case FI_PID_fd:
  702. metadata.mode = 040777;
  703. break;
  704. default:
  705. metadata.mode = 0100644;
  706. break;
  707. }
  708. #ifdef PROCFS_DEBUG
  709. dbgprintf("Returning mode %o\n", metadata.mode);
  710. #endif
  711. return metadata;
  712. }
  713. ssize_t ProcFSInode::read_bytes(off_t offset, size_t count, byte* buffer, FileDescriptor* descriptor) const
  714. {
  715. #ifdef PROCFS_DEBUG
  716. dbgprintf("ProcFS: read_bytes %u\n", index());
  717. #endif
  718. ASSERT(offset >= 0);
  719. ASSERT(buffer);
  720. auto* directory_entry = fs().get_directory_entry(identifier());
  721. Function<ByteBuffer(InodeIdentifier)> callback_tmp;
  722. Function<ByteBuffer(InodeIdentifier)>* read_callback { nullptr };
  723. if (directory_entry) {
  724. read_callback = &directory_entry->read_callback;
  725. } else {
  726. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  727. callback_tmp = procfs$pid_fd_entry;
  728. read_callback = &callback_tmp;
  729. }
  730. }
  731. ASSERT(read_callback);
  732. ByteBuffer generated_data;
  733. if (!descriptor) {
  734. generated_data = (*read_callback)(identifier());
  735. } else {
  736. if (!descriptor->generator_cache())
  737. descriptor->generator_cache() = (*read_callback)(identifier());
  738. generated_data = descriptor->generator_cache();
  739. }
  740. auto& data = generated_data;
  741. ssize_t nread = min(static_cast<off_t>(data.size() - offset), static_cast<off_t>(count));
  742. memcpy(buffer, data.pointer() + offset, nread);
  743. if (nread == 0 && descriptor && descriptor->generator_cache())
  744. descriptor->generator_cache().clear();
  745. return nread;
  746. }
  747. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  748. {
  749. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  750. }
  751. bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  752. {
  753. #ifdef PROCFS_DEBUG
  754. dbgprintf("ProcFS: traverse_as_directory %u\n", index());
  755. #endif
  756. if (!::is_directory(identifier()))
  757. return false;
  758. auto pid = to_pid(identifier());
  759. auto proc_file_type = to_proc_file_type(identifier());
  760. auto parent_id = to_parent_id(identifier());
  761. callback({ ".", 1, identifier(), 2 });
  762. callback({ "..", 2, parent_id, 2 });
  763. switch (proc_file_type) {
  764. case FI_Root:
  765. for (auto& entry : fs().m_entries) {
  766. // FIXME: strlen() here is sad.
  767. if (!entry.name)
  768. continue;
  769. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  770. callback({ entry.name, strlen(entry.name), to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  771. }
  772. for (auto pid_child : Process::all_pids()) {
  773. char name[16];
  774. size_t name_length = ksprintf(name, "%u", pid_child);
  775. callback({ name, name_length, to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  776. }
  777. break;
  778. case FI_Root_sys:
  779. for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
  780. auto& entry = fs().m_sys_entries[i];
  781. callback({ entry.name, strlen(entry.name), sys_var_to_identifier(fsid(), i), 0 });
  782. }
  783. break;
  784. case FI_PID: {
  785. auto handle = ProcessInspectionHandle::from_pid(pid);
  786. if (!handle)
  787. return false;
  788. auto& process = handle->process();
  789. for (auto& entry : fs().m_entries) {
  790. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  791. if (entry.proc_file_type == FI_PID_exe || entry.proc_file_type == FI_PID_cwd) {
  792. if (entry.proc_file_type == FI_PID_exe && !process.executable_inode())
  793. continue;
  794. if (entry.proc_file_type == FI_PID_cwd && !process.cwd_inode())
  795. continue;
  796. }
  797. // FIXME: strlen() here is sad.
  798. callback({ entry.name, strlen(entry.name), to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  799. }
  800. }
  801. }
  802. break;
  803. case FI_PID_fd: {
  804. auto handle = ProcessInspectionHandle::from_pid(pid);
  805. if (!handle)
  806. return false;
  807. auto& process = handle->process();
  808. for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
  809. auto* descriptor = process.file_descriptor(i);
  810. if (!descriptor)
  811. continue;
  812. char name[16];
  813. size_t name_length = ksprintf(name, "%u", i);
  814. callback({ name, name_length, to_identifier_with_fd(fsid(), pid, i), 0 });
  815. }
  816. }
  817. break;
  818. default:
  819. return true;
  820. }
  821. return true;
  822. }
  823. InodeIdentifier ProcFSInode::lookup(const String& name)
  824. {
  825. ASSERT(is_directory());
  826. if (name == ".")
  827. return identifier();
  828. if (name == "..")
  829. return to_parent_id(identifier());
  830. auto proc_file_type = to_proc_file_type(identifier());
  831. if (proc_file_type == FI_Root) {
  832. for (auto& entry : fs().m_entries) {
  833. if (entry.name == nullptr)
  834. continue;
  835. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  836. if (!strcmp(entry.name, name.characters())) {
  837. return to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type);
  838. }
  839. }
  840. }
  841. bool ok;
  842. unsigned name_as_number = name.to_uint(ok);
  843. if (ok) {
  844. bool process_exists = false;
  845. {
  846. InterruptDisabler disabler;
  847. process_exists = Process::from_pid(name_as_number);
  848. }
  849. if (process_exists)
  850. return to_identifier(fsid(), PDI_Root, name_as_number, FI_PID);
  851. }
  852. return { };
  853. }
  854. if (proc_file_type == FI_Root_sys) {
  855. for (size_t i = 0; i < fs().m_sys_entries.size(); ++i) {
  856. auto& entry = fs().m_sys_entries[i];
  857. if (!strcmp(entry.name, name.characters()))
  858. return sys_var_to_identifier(fsid(), i);
  859. }
  860. return { };
  861. }
  862. if (proc_file_type == FI_PID) {
  863. auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier()));
  864. if (!handle)
  865. return { };
  866. auto& process = handle->process();
  867. for (auto& entry : fs().m_entries) {
  868. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  869. if (entry.proc_file_type == FI_PID_exe || entry.proc_file_type == FI_PID_cwd) {
  870. if (entry.proc_file_type == FI_PID_exe && !process.executable_inode())
  871. continue;
  872. if (entry.proc_file_type == FI_PID_cwd && !process.cwd_inode())
  873. continue;
  874. }
  875. if (entry.name == nullptr)
  876. continue;
  877. if (!strcmp(entry.name, name.characters())) {
  878. return to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type);
  879. }
  880. }
  881. }
  882. return { };
  883. }
  884. if (proc_file_type == FI_PID_fd) {
  885. bool ok;
  886. unsigned name_as_number = name.to_uint(ok);
  887. if (ok) {
  888. bool fd_exists = false;
  889. {
  890. InterruptDisabler disabler;
  891. if (auto* process = Process::from_pid(to_pid(identifier())))
  892. fd_exists = process->file_descriptor(name_as_number);
  893. }
  894. if (fd_exists)
  895. return to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number);
  896. }
  897. }
  898. return { };
  899. }
  900. String ProcFSInode::reverse_lookup(InodeIdentifier child_id)
  901. {
  902. ASSERT(is_directory());
  903. auto proc_file_type = to_proc_file_type(identifier());
  904. if (proc_file_type == FI_Root) {
  905. for (auto& entry : fs().m_entries) {
  906. if (child_id == to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type)) {
  907. return entry.name;
  908. }
  909. }
  910. auto child_proc_file_type = to_proc_file_type(child_id);
  911. if (child_proc_file_type == FI_PID)
  912. return String::format("%u", to_pid(child_id));
  913. return { };
  914. }
  915. // FIXME: Implement
  916. ASSERT_NOT_REACHED();
  917. return { };
  918. }
  919. void ProcFSInode::flush_metadata()
  920. {
  921. }
  922. ssize_t ProcFSInode::write_bytes(off_t offset, size_t size, const byte* buffer, FileDescriptor*)
  923. {
  924. auto* directory_entry = fs().get_directory_entry(identifier());
  925. if (!directory_entry || !directory_entry->write_callback)
  926. return -EPERM;
  927. ASSERT(is_persistent_inode(identifier()));
  928. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  929. ASSERT(offset == 0);
  930. bool success = directory_entry->write_callback(identifier(), ByteBuffer::wrap((byte*)buffer, size));
  931. ASSERT(success);
  932. return 0;
  933. }
  934. bool ProcFSInode::add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error)
  935. {
  936. (void) child_id;
  937. (void) name;
  938. (void) file_type;
  939. (void) error;
  940. ASSERT_NOT_REACHED();
  941. return false;
  942. }
  943. bool ProcFSInode::remove_child(const String& name, int& error)
  944. {
  945. (void) name;
  946. (void) error;
  947. ASSERT_NOT_REACHED();
  948. return false;
  949. }
  950. ProcFSInodeCustomData::~ProcFSInodeCustomData()
  951. {
  952. }
  953. size_t ProcFSInode::directory_entry_count() const
  954. {
  955. ASSERT(is_directory());
  956. size_t count = 0;
  957. traverse_as_directory([&count] (const FS::DirectoryEntry&) {
  958. ++count;
  959. return true;
  960. });
  961. return count;
  962. }
  963. bool ProcFSInode::chmod(mode_t, int& error)
  964. {
  965. error = -EPERM;
  966. return false;
  967. }
  968. ProcFS::ProcFS()
  969. {
  970. s_the = this;
  971. m_root_inode = adopt(*new ProcFSInode(*this, 1));
  972. m_entries.resize(FI_MaxStaticFileIndex);
  973. m_entries[FI_Root_mm] = { "mm", FI_Root_mm, procfs$mm };
  974. m_entries[FI_Root_mounts] = { "mounts", FI_Root_mounts, procfs$mounts };
  975. m_entries[FI_Root_kmalloc] = { "kmalloc", FI_Root_kmalloc, procfs$kmalloc };
  976. m_entries[FI_Root_all] = { "all", FI_Root_all, procfs$all };
  977. m_entries[FI_Root_summary] = { "summary", FI_Root_summary, procfs$summary };
  978. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, procfs$cpuinfo};
  979. m_entries[FI_Root_inodes] = { "inodes", FI_Root_inodes, procfs$inodes };
  980. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };
  981. m_entries[FI_Root_self] = { "self", FI_Root_self, procfs$self };
  982. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, procfs$pci };
  983. m_entries[FI_Root_sys] = { "sys", FI_Root_sys };
  984. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
  985. m_entries[FI_PID_vmo] = { "vmo", FI_PID_vmo, procfs$pid_vmo };
  986. m_entries[FI_PID_stack] = { "stack", FI_PID_stack, procfs$pid_stack };
  987. m_entries[FI_PID_regs] = { "regs", FI_PID_regs, procfs$pid_regs };
  988. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, procfs$pid_fds };
  989. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, procfs$pid_exe };
  990. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, procfs$pid_cwd };
  991. m_entries[FI_PID_fd] = { "fd", FI_PID_fd };
  992. }
  993. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  994. {
  995. if (to_proc_parent_directory(identifier) == PDI_Root_sys) {
  996. auto sys_index = to_sys_index(identifier);
  997. if (sys_index < m_sys_entries.size())
  998. return const_cast<ProcFSDirectoryEntry*>(&m_sys_entries[sys_index]);
  999. return nullptr;
  1000. }
  1001. auto proc_file_type = to_proc_file_type(identifier);
  1002. if (proc_file_type != FI_Invalid && proc_file_type < FI_MaxStaticFileIndex)
  1003. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1004. return nullptr;
  1005. }