ProcFS.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <AK/JsonObjectSerializer.h>
  8. #include <AK/JsonValue.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <Kernel/Arch/x86/CPU.h>
  11. #include <Kernel/Arch/x86/ProcessorInfo.h>
  12. #include <Kernel/CommandLine.h>
  13. #include <Kernel/Console.h>
  14. #include <Kernel/DMI.h>
  15. #include <Kernel/Debug.h>
  16. #include <Kernel/Devices/BlockDevice.h>
  17. #include <Kernel/Devices/HID/HIDManagement.h>
  18. #include <Kernel/FileSystem/Custody.h>
  19. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  20. #include <Kernel/FileSystem/FileDescription.h>
  21. #include <Kernel/FileSystem/ProcFS.h>
  22. #include <Kernel/FileSystem/VirtualFileSystem.h>
  23. #include <Kernel/Heap/kmalloc.h>
  24. #include <Kernel/Interrupts/GenericInterruptHandler.h>
  25. #include <Kernel/Interrupts/InterruptManagement.h>
  26. #include <Kernel/KBufferBuilder.h>
  27. #include <Kernel/Module.h>
  28. #include <Kernel/Net/LocalSocket.h>
  29. #include <Kernel/Net/NetworkAdapter.h>
  30. #include <Kernel/Net/Routing.h>
  31. #include <Kernel/Net/TCPSocket.h>
  32. #include <Kernel/Net/UDPSocket.h>
  33. #include <Kernel/PCI/Access.h>
  34. #include <Kernel/PerformanceEventBuffer.h>
  35. #include <Kernel/Process.h>
  36. #include <Kernel/Scheduler.h>
  37. #include <Kernel/StdLib.h>
  38. #include <Kernel/TTY/TTY.h>
  39. #include <Kernel/UBSanitizer.h>
  40. #include <Kernel/VM/AnonymousVMObject.h>
  41. #include <Kernel/VM/MemoryManager.h>
  42. #include <LibC/errno_numbers.h>
  43. namespace Kernel {
  44. enum ProcParentDirectory {
  45. PDI_AbstractRoot = 0,
  46. PDI_Root,
  47. PDI_Root_sys,
  48. PDI_Root_net,
  49. PDI_PID,
  50. PDI_PID_fd,
  51. PDI_PID_stacks,
  52. };
  53. static_assert(PDI_PID_stacks < 16, "Too many directories for identifier scheme");
  54. enum ProcFileType {
  55. FI_Invalid = 0,
  56. FI_Root = 1, // directory
  57. __FI_Root_Start,
  58. FI_Root_df,
  59. FI_Root_all,
  60. FI_Root_memstat,
  61. FI_Root_cpuinfo,
  62. FI_Root_dmesg,
  63. FI_Root_interrupts,
  64. FI_Root_dmi,
  65. FI_Root_smbios_entry_point,
  66. FI_Root_keymap,
  67. FI_Root_pci,
  68. FI_Root_devices,
  69. FI_Root_uptime,
  70. FI_Root_cmdline,
  71. FI_Root_modules,
  72. FI_Root_profile,
  73. FI_Root_self, // symlink
  74. FI_Root_sys, // directory
  75. FI_Root_net, // directory
  76. __FI_Root_End,
  77. FI_Root_sys_variable,
  78. FI_Root_net_adapters,
  79. FI_Root_net_arp,
  80. FI_Root_net_tcp,
  81. FI_Root_net_udp,
  82. FI_Root_net_local,
  83. FI_PID,
  84. __FI_PID_Start,
  85. FI_PID_perf_events,
  86. FI_PID_vm,
  87. FI_PID_stacks, // directory
  88. FI_PID_fds,
  89. FI_PID_unveil,
  90. FI_PID_exe, // symlink
  91. FI_PID_cwd, // symlink
  92. FI_PID_root, // symlink
  93. FI_PID_fd, // directory
  94. __FI_PID_End,
  95. FI_MaxStaticFileIndex,
  96. };
  97. static inline ProcessID to_pid(const InodeIdentifier& identifier)
  98. {
  99. return identifier.index().value() >> 16u;
  100. }
  101. static inline ThreadID to_tid(const InodeIdentifier& identifier)
  102. {
  103. // Sneakily, use the exact same mechanism.
  104. return to_pid(identifier).value();
  105. }
  106. static inline ProcParentDirectory to_proc_parent_directory(const InodeIdentifier& identifier)
  107. {
  108. return (ProcParentDirectory)((identifier.index().value() >> 12) & 0xf);
  109. }
  110. static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
  111. {
  112. return (ProcFileType)(identifier.index().value() & 0xff);
  113. }
  114. static inline int to_fd(const InodeIdentifier& identifier)
  115. {
  116. VERIFY(to_proc_parent_directory(identifier) == PDI_PID_fd);
  117. return (identifier.index().value() & 0xff) - FI_MaxStaticFileIndex;
  118. }
  119. static inline size_t to_sys_index(const InodeIdentifier& identifier)
  120. {
  121. VERIFY(to_proc_parent_directory(identifier) == PDI_Root_sys);
  122. VERIFY(to_proc_file_type(identifier) == FI_Root_sys_variable);
  123. return identifier.index().value() >> 16u;
  124. }
  125. static inline InodeIdentifier to_identifier(unsigned fsid, ProcParentDirectory parent, ProcessID pid, ProcFileType proc_file_type)
  126. {
  127. return { fsid, ((unsigned)parent << 12u) | ((unsigned)pid.value() << 16u) | (unsigned)proc_file_type };
  128. }
  129. static inline InodeIdentifier to_identifier_with_fd(unsigned fsid, ProcessID pid, int fd)
  130. {
  131. return { fsid, (PDI_PID_fd << 12u) | ((unsigned)pid.value() << 16u) | (FI_MaxStaticFileIndex + fd) };
  132. }
  133. static inline InodeIdentifier to_identifier_with_stack(unsigned fsid, ThreadID tid)
  134. {
  135. return { fsid, (PDI_PID_stacks << 12u) | ((unsigned)tid.value() << 16u) | FI_MaxStaticFileIndex };
  136. }
  137. static inline InodeIdentifier sys_var_to_identifier(unsigned fsid, unsigned index)
  138. {
  139. VERIFY(index < 256);
  140. return { fsid, (PDI_Root_sys << 12u) | (index << 16u) | FI_Root_sys_variable };
  141. }
  142. static inline InodeIdentifier to_parent_id(const InodeIdentifier& identifier)
  143. {
  144. switch (to_proc_parent_directory(identifier)) {
  145. case PDI_AbstractRoot:
  146. case PDI_Root:
  147. return { identifier.fsid(), FI_Root };
  148. case PDI_Root_sys:
  149. return { identifier.fsid(), FI_Root_sys };
  150. case PDI_Root_net:
  151. return { identifier.fsid(), FI_Root_net };
  152. case PDI_PID:
  153. return to_identifier(identifier.fsid(), PDI_Root, to_pid(identifier), FI_PID);
  154. case PDI_PID_fd:
  155. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_fd);
  156. case PDI_PID_stacks:
  157. return to_identifier(identifier.fsid(), PDI_PID, to_pid(identifier), FI_PID_stacks);
  158. }
  159. VERIFY_NOT_REACHED();
  160. }
  161. #if 0
  162. static inline u8 to_unused_metadata(const InodeIdentifier& identifier)
  163. {
  164. return (identifier.index() >> 8) & 0xf;
  165. }
  166. #endif
  167. static inline bool is_process_related_file(const InodeIdentifier& identifier)
  168. {
  169. if (to_proc_file_type(identifier) == FI_PID)
  170. return true;
  171. auto proc_parent_directory = to_proc_parent_directory(identifier);
  172. switch (proc_parent_directory) {
  173. case PDI_PID:
  174. case PDI_PID_fd:
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. static inline bool is_thread_related_file(const InodeIdentifier& identifier)
  181. {
  182. auto proc_parent_directory = to_proc_parent_directory(identifier);
  183. return proc_parent_directory == PDI_PID_stacks;
  184. }
  185. static inline bool is_directory(const InodeIdentifier& identifier)
  186. {
  187. auto proc_file_type = to_proc_file_type(identifier);
  188. switch (proc_file_type) {
  189. case FI_Root:
  190. case FI_Root_sys:
  191. case FI_Root_net:
  192. case FI_PID:
  193. case FI_PID_fd:
  194. case FI_PID_stacks:
  195. return true;
  196. default:
  197. return false;
  198. }
  199. }
  200. static inline bool is_persistent_inode(const InodeIdentifier& identifier)
  201. {
  202. return to_proc_parent_directory(identifier) == PDI_Root_sys;
  203. }
  204. struct ProcFSInodeData : public FileDescriptionData {
  205. RefPtr<KBufferImpl> buffer;
  206. };
  207. NonnullRefPtr<ProcFS> ProcFS::create()
  208. {
  209. return adopt_ref(*new ProcFS);
  210. }
  211. ProcFS::~ProcFS()
  212. {
  213. }
  214. static bool procfs$pid_fds(InodeIdentifier identifier, KBufferBuilder& builder)
  215. {
  216. JsonArraySerializer array { builder };
  217. auto process = Process::from_pid(to_pid(identifier));
  218. if (!process) {
  219. array.finish();
  220. return true;
  221. }
  222. if (process->number_of_open_file_descriptors() == 0) {
  223. array.finish();
  224. return true;
  225. }
  226. for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
  227. auto description = process->file_description(i);
  228. if (!description)
  229. continue;
  230. bool cloexec = process->fd_flags(i) & FD_CLOEXEC;
  231. auto description_object = array.add_object();
  232. description_object.add("fd", i);
  233. description_object.add("absolute_path", description->absolute_path());
  234. description_object.add("seekable", description->file().is_seekable());
  235. description_object.add("class", description->file().class_name());
  236. description_object.add("offset", description->offset());
  237. description_object.add("cloexec", cloexec);
  238. description_object.add("blocking", description->is_blocking());
  239. description_object.add("can_read", description->can_read());
  240. description_object.add("can_write", description->can_write());
  241. }
  242. array.finish();
  243. return true;
  244. }
  245. static bool procfs$pid_fd_entry(InodeIdentifier identifier, KBufferBuilder& builder)
  246. {
  247. auto process = Process::from_pid(to_pid(identifier));
  248. if (!process)
  249. return false;
  250. int fd = to_fd(identifier);
  251. auto description = process->file_description(fd);
  252. if (!description)
  253. return false;
  254. builder.append_bytes(description->absolute_path().bytes());
  255. return true;
  256. }
  257. static bool procfs$pid_vm(InodeIdentifier identifier, KBufferBuilder& builder)
  258. {
  259. auto process = Process::from_pid(to_pid(identifier));
  260. if (!process)
  261. return false;
  262. JsonArraySerializer array { builder };
  263. {
  264. ScopedSpinLock lock(process->space().get_lock());
  265. for (auto& region : process->space().regions()) {
  266. if (!region->is_user() && !Process::current()->is_superuser())
  267. continue;
  268. auto region_object = array.add_object();
  269. region_object.add("readable", region->is_readable());
  270. region_object.add("writable", region->is_writable());
  271. region_object.add("executable", region->is_executable());
  272. region_object.add("stack", region->is_stack());
  273. region_object.add("shared", region->is_shared());
  274. region_object.add("syscall", region->is_syscall_region());
  275. region_object.add("purgeable", region->vmobject().is_anonymous());
  276. if (region->vmobject().is_anonymous()) {
  277. region_object.add("volatile", static_cast<const AnonymousVMObject&>(region->vmobject()).is_any_volatile());
  278. }
  279. region_object.add("cacheable", region->is_cacheable());
  280. region_object.add("address", region->vaddr().get());
  281. region_object.add("size", region->size());
  282. region_object.add("amount_resident", region->amount_resident());
  283. region_object.add("amount_dirty", region->amount_dirty());
  284. region_object.add("cow_pages", region->cow_pages());
  285. region_object.add("name", region->name());
  286. region_object.add("vmobject", region->vmobject().class_name());
  287. StringBuilder pagemap_builder;
  288. for (size_t i = 0; i < region->page_count(); ++i) {
  289. auto* page = region->physical_page(i);
  290. if (!page)
  291. pagemap_builder.append('N');
  292. else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
  293. pagemap_builder.append('Z');
  294. else
  295. pagemap_builder.append('P');
  296. }
  297. region_object.add("pagemap", pagemap_builder.to_string());
  298. }
  299. }
  300. array.finish();
  301. return true;
  302. }
  303. static bool procfs$pci(InodeIdentifier, KBufferBuilder& builder)
  304. {
  305. JsonArraySerializer array { builder };
  306. PCI::enumerate([&array](PCI::Address address, PCI::ID id) {
  307. auto obj = array.add_object();
  308. obj.add("seg", address.seg());
  309. obj.add("bus", address.bus());
  310. obj.add("device", address.device());
  311. obj.add("function", address.function());
  312. obj.add("vendor_id", id.vendor_id);
  313. obj.add("device_id", id.device_id);
  314. obj.add("revision_id", PCI::get_revision_id(address));
  315. obj.add("subclass", PCI::get_subclass(address));
  316. obj.add("class", PCI::get_class(address));
  317. obj.add("subsystem_id", PCI::get_subsystem_id(address));
  318. obj.add("subsystem_vendor_id", PCI::get_subsystem_vendor_id(address));
  319. });
  320. array.finish();
  321. return true;
  322. }
  323. static bool procfs$dmi(InodeIdentifier, KBufferBuilder& builder)
  324. {
  325. if (!DMIExpose::the().is_available())
  326. return false;
  327. auto structures_ptr = DMIExpose::the().structure_table();
  328. builder.append_bytes(ReadonlyBytes { structures_ptr->data(), structures_ptr->size() });
  329. return true;
  330. }
  331. static bool procfs$smbios_entry_point(InodeIdentifier, KBufferBuilder& builder)
  332. {
  333. if (!DMIExpose::the().is_available())
  334. return false;
  335. auto structures_ptr = DMIExpose::the().entry_point();
  336. builder.append_bytes(ReadonlyBytes { structures_ptr->data(), structures_ptr->size() });
  337. return true;
  338. }
  339. static bool procfs$interrupts(InodeIdentifier, KBufferBuilder& builder)
  340. {
  341. JsonArraySerializer array { builder };
  342. InterruptManagement::the().enumerate_interrupt_handlers([&array](GenericInterruptHandler& handler) {
  343. auto obj = array.add_object();
  344. obj.add("purpose", handler.purpose());
  345. obj.add("interrupt_line", handler.interrupt_number());
  346. obj.add("controller", handler.controller());
  347. obj.add("cpu_handler", 0); // FIXME: Determine the responsible CPU for each interrupt handler.
  348. obj.add("device_sharing", (unsigned)handler.sharing_devices_count());
  349. obj.add("call_count", (unsigned)handler.get_invoking_count());
  350. });
  351. array.finish();
  352. return true;
  353. }
  354. static bool procfs$keymap(InodeIdentifier, KBufferBuilder& builder)
  355. {
  356. JsonObjectSerializer<KBufferBuilder> json { builder };
  357. json.add("keymap", HIDManagement::the().keymap_name());
  358. json.finish();
  359. return true;
  360. }
  361. static bool procfs$devices(InodeIdentifier, KBufferBuilder& builder)
  362. {
  363. JsonArraySerializer array { builder };
  364. Device::for_each([&array](auto& device) {
  365. auto obj = array.add_object();
  366. obj.add("major", device.major());
  367. obj.add("minor", device.minor());
  368. obj.add("class_name", device.class_name());
  369. if (device.is_block_device())
  370. obj.add("type", "block");
  371. else if (device.is_character_device())
  372. obj.add("type", "character");
  373. else
  374. VERIFY_NOT_REACHED();
  375. });
  376. array.finish();
  377. return true;
  378. }
  379. static bool procfs$uptime(InodeIdentifier, KBufferBuilder& builder)
  380. {
  381. builder.appendff("{}\n", TimeManagement::the().uptime_ms() / 1000);
  382. return true;
  383. }
  384. static bool procfs$cmdline(InodeIdentifier, KBufferBuilder& builder)
  385. {
  386. builder.append(kernel_command_line().string());
  387. builder.append('\n');
  388. return true;
  389. }
  390. static bool procfs$modules(InodeIdentifier, KBufferBuilder& builder)
  391. {
  392. extern HashMap<String, OwnPtr<Module>>* g_modules;
  393. JsonArraySerializer array { builder };
  394. for (auto& it : *g_modules) {
  395. auto obj = array.add_object();
  396. obj.add("name", it.value->name);
  397. obj.add("module_init", it.value->module_init);
  398. obj.add("module_fini", it.value->module_fini);
  399. u32 size = 0;
  400. for (auto& section : it.value->sections) {
  401. size += section.capacity();
  402. }
  403. obj.add("size", size);
  404. }
  405. array.finish();
  406. return true;
  407. }
  408. static bool procfs$profile(InodeIdentifier, KBufferBuilder& builder)
  409. {
  410. extern PerformanceEventBuffer* g_global_perf_events;
  411. if (!g_global_perf_events)
  412. return false;
  413. return g_global_perf_events->to_json(builder);
  414. }
  415. static bool procfs$pid_perf_events(InodeIdentifier identifier, KBufferBuilder& builder)
  416. {
  417. auto process = Process::from_pid(to_pid(identifier));
  418. if (!process)
  419. return false;
  420. InterruptDisabler disabler;
  421. if (!process->perf_events())
  422. return false;
  423. return process->perf_events()->to_json(builder);
  424. }
  425. static bool procfs$net_adapters(InodeIdentifier, KBufferBuilder& builder)
  426. {
  427. JsonArraySerializer array { builder };
  428. NetworkAdapter::for_each([&array](auto& adapter) {
  429. auto obj = array.add_object();
  430. obj.add("name", adapter.name());
  431. obj.add("class_name", adapter.class_name());
  432. obj.add("mac_address", adapter.mac_address().to_string());
  433. if (!adapter.ipv4_address().is_zero()) {
  434. obj.add("ipv4_address", adapter.ipv4_address().to_string());
  435. obj.add("ipv4_netmask", adapter.ipv4_netmask().to_string());
  436. }
  437. if (!adapter.ipv4_gateway().is_zero())
  438. obj.add("ipv4_gateway", adapter.ipv4_gateway().to_string());
  439. obj.add("packets_in", adapter.packets_in());
  440. obj.add("bytes_in", adapter.bytes_in());
  441. obj.add("packets_out", adapter.packets_out());
  442. obj.add("bytes_out", adapter.bytes_out());
  443. obj.add("link_up", adapter.link_up());
  444. obj.add("mtu", adapter.mtu());
  445. });
  446. array.finish();
  447. return true;
  448. }
  449. static bool procfs$net_arp(InodeIdentifier, KBufferBuilder& builder)
  450. {
  451. JsonArraySerializer array { builder };
  452. Locker locker(arp_table().lock(), Lock::Mode::Shared);
  453. for (auto& it : arp_table().resource()) {
  454. auto obj = array.add_object();
  455. obj.add("mac_address", it.value.to_string());
  456. obj.add("ip_address", it.key.to_string());
  457. }
  458. array.finish();
  459. return true;
  460. }
  461. static bool procfs$net_tcp(InodeIdentifier, KBufferBuilder& builder)
  462. {
  463. JsonArraySerializer array { builder };
  464. TCPSocket::for_each([&array](auto& socket) {
  465. auto obj = array.add_object();
  466. obj.add("local_address", socket.local_address().to_string());
  467. obj.add("local_port", socket.local_port());
  468. obj.add("peer_address", socket.peer_address().to_string());
  469. obj.add("peer_port", socket.peer_port());
  470. obj.add("state", TCPSocket::to_string(socket.state()));
  471. obj.add("ack_number", socket.ack_number());
  472. obj.add("sequence_number", socket.sequence_number());
  473. obj.add("packets_in", socket.packets_in());
  474. obj.add("bytes_in", socket.bytes_in());
  475. obj.add("packets_out", socket.packets_out());
  476. obj.add("bytes_out", socket.bytes_out());
  477. });
  478. array.finish();
  479. return true;
  480. }
  481. static bool procfs$net_udp(InodeIdentifier, KBufferBuilder& builder)
  482. {
  483. JsonArraySerializer array { builder };
  484. UDPSocket::for_each([&array](auto& socket) {
  485. auto obj = array.add_object();
  486. obj.add("local_address", socket.local_address().to_string());
  487. obj.add("local_port", socket.local_port());
  488. obj.add("peer_address", socket.peer_address().to_string());
  489. obj.add("peer_port", socket.peer_port());
  490. });
  491. array.finish();
  492. return true;
  493. }
  494. static bool procfs$net_local(InodeIdentifier, KBufferBuilder& builder)
  495. {
  496. JsonArraySerializer array { builder };
  497. LocalSocket::for_each([&array](auto& socket) {
  498. auto obj = array.add_object();
  499. obj.add("path", String(socket.socket_path()));
  500. obj.add("origin_pid", socket.origin_pid());
  501. obj.add("origin_uid", socket.origin_uid());
  502. obj.add("origin_gid", socket.origin_gid());
  503. obj.add("acceptor_pid", socket.acceptor_pid());
  504. obj.add("acceptor_uid", socket.acceptor_uid());
  505. obj.add("acceptor_gid", socket.acceptor_gid());
  506. });
  507. array.finish();
  508. return true;
  509. }
  510. static bool procfs$pid_unveil(InodeIdentifier identifier, KBufferBuilder& builder)
  511. {
  512. auto process = Process::from_pid(to_pid(identifier));
  513. if (!process)
  514. return false;
  515. JsonArraySerializer array { builder };
  516. for (auto& unveiled_path : process->unveiled_paths()) {
  517. if (!unveiled_path.was_explicitly_unveiled())
  518. continue;
  519. auto obj = array.add_object();
  520. obj.add("path", unveiled_path.path());
  521. StringBuilder permissions_builder;
  522. if (unveiled_path.permissions() & UnveilAccess::Read)
  523. permissions_builder.append('r');
  524. if (unveiled_path.permissions() & UnveilAccess::Write)
  525. permissions_builder.append('w');
  526. if (unveiled_path.permissions() & UnveilAccess::Execute)
  527. permissions_builder.append('x');
  528. if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
  529. permissions_builder.append('c');
  530. if (unveiled_path.permissions() & UnveilAccess::Browse)
  531. permissions_builder.append('b');
  532. obj.add("permissions", permissions_builder.to_string());
  533. }
  534. array.finish();
  535. return true;
  536. }
  537. static bool procfs$tid_stack(InodeIdentifier identifier, KBufferBuilder& builder)
  538. {
  539. auto thread = Thread::from_tid(to_tid(identifier));
  540. if (!thread)
  541. return false;
  542. JsonArraySerializer array { builder };
  543. bool show_kernel_addresses = Process::current()->is_superuser();
  544. bool kernel_address_added = false;
  545. for (auto address : Processor::capture_stack_trace(*thread, 1024)) {
  546. if (!show_kernel_addresses && !is_user_address(VirtualAddress { address })) {
  547. if (kernel_address_added)
  548. continue;
  549. address = 0xdeadc0de;
  550. kernel_address_added = true;
  551. }
  552. array.add(JsonValue(address));
  553. }
  554. array.finish();
  555. return true;
  556. }
  557. static bool procfs$pid_exe(InodeIdentifier identifier, KBufferBuilder& builder)
  558. {
  559. auto process = Process::from_pid(to_pid(identifier));
  560. if (!process)
  561. return false;
  562. auto* custody = process->executable();
  563. VERIFY(custody);
  564. builder.append(custody->absolute_path().bytes());
  565. return true;
  566. }
  567. static bool procfs$pid_cwd(InodeIdentifier identifier, KBufferBuilder& builder)
  568. {
  569. auto process = Process::from_pid(to_pid(identifier));
  570. if (!process)
  571. return false;
  572. builder.append_bytes(process->current_directory().absolute_path().bytes());
  573. return true;
  574. }
  575. static bool procfs$pid_root(InodeIdentifier identifier, KBufferBuilder& builder)
  576. {
  577. auto process = Process::from_pid(to_pid(identifier));
  578. if (!process)
  579. return false;
  580. builder.append_bytes(process->root_directory_relative_to_global_root().absolute_path().to_byte_buffer());
  581. return true;
  582. }
  583. static bool procfs$self(InodeIdentifier, KBufferBuilder& builder)
  584. {
  585. builder.appendff("{}", Process::current()->pid().value());
  586. return true;
  587. }
  588. static bool procfs$dmesg(InodeIdentifier, KBufferBuilder& builder)
  589. {
  590. InterruptDisabler disabler;
  591. for (char ch : Console::the().logbuffer())
  592. builder.append(ch);
  593. return true;
  594. }
  595. static bool procfs$df(InodeIdentifier, KBufferBuilder& builder)
  596. {
  597. // FIXME: This is obviously racy against the VFS mounts changing.
  598. JsonArraySerializer array { builder };
  599. VFS::the().for_each_mount([&array](auto& mount) {
  600. auto& fs = mount.guest_fs();
  601. auto fs_object = array.add_object();
  602. fs_object.add("class_name", fs.class_name());
  603. fs_object.add("total_block_count", fs.total_block_count());
  604. fs_object.add("free_block_count", fs.free_block_count());
  605. fs_object.add("total_inode_count", fs.total_inode_count());
  606. fs_object.add("free_inode_count", fs.free_inode_count());
  607. fs_object.add("mount_point", mount.absolute_path());
  608. fs_object.add("block_size", static_cast<u64>(fs.block_size()));
  609. fs_object.add("readonly", fs.is_readonly());
  610. fs_object.add("mount_flags", mount.flags());
  611. if (fs.is_file_backed())
  612. fs_object.add("source", static_cast<const FileBackedFS&>(fs).file_description().absolute_path());
  613. else
  614. fs_object.add("source", "none");
  615. });
  616. array.finish();
  617. return true;
  618. }
  619. static bool procfs$cpuinfo(InodeIdentifier, KBufferBuilder& builder)
  620. {
  621. JsonArraySerializer array { builder };
  622. Processor::for_each(
  623. [&](Processor& proc) -> IterationDecision {
  624. auto& info = proc.info();
  625. auto obj = array.add_object();
  626. JsonArray features;
  627. for (auto& feature : info.features().split(' '))
  628. features.append(feature);
  629. obj.add("processor", proc.get_id());
  630. obj.add("cpuid", info.cpuid());
  631. obj.add("family", info.display_family());
  632. obj.add("features", features);
  633. obj.add("model", info.display_model());
  634. obj.add("stepping", info.stepping());
  635. obj.add("type", info.type());
  636. obj.add("brandstr", info.brandstr());
  637. return IterationDecision::Continue;
  638. });
  639. array.finish();
  640. return true;
  641. }
  642. static bool procfs$memstat(InodeIdentifier, KBufferBuilder& builder)
  643. {
  644. InterruptDisabler disabler;
  645. kmalloc_stats stats;
  646. get_kmalloc_stats(stats);
  647. ScopedSpinLock mm_lock(s_mm_lock);
  648. auto user_physical_pages_total = MM.user_physical_pages();
  649. auto user_physical_pages_used = MM.user_physical_pages_used();
  650. auto user_physical_pages_committed = MM.user_physical_pages_committed();
  651. auto user_physical_pages_uncommitted = MM.user_physical_pages_uncommitted();
  652. auto super_physical_total = MM.super_physical_pages();
  653. auto super_physical_used = MM.super_physical_pages_used();
  654. mm_lock.unlock();
  655. JsonObjectSerializer<KBufferBuilder> json { builder };
  656. json.add("kmalloc_allocated", stats.bytes_allocated);
  657. json.add("kmalloc_available", stats.bytes_free);
  658. json.add("kmalloc_eternal_allocated", stats.bytes_eternal);
  659. json.add("user_physical_allocated", user_physical_pages_used);
  660. json.add("user_physical_available", user_physical_pages_total - user_physical_pages_used);
  661. json.add("user_physical_committed", user_physical_pages_committed);
  662. json.add("user_physical_uncommitted", user_physical_pages_uncommitted);
  663. json.add("super_physical_allocated", super_physical_used);
  664. json.add("super_physical_available", super_physical_total - super_physical_used);
  665. json.add("kmalloc_call_count", stats.kmalloc_call_count);
  666. json.add("kfree_call_count", stats.kfree_call_count);
  667. slab_alloc_stats([&json](size_t slab_size, size_t num_allocated, size_t num_free) {
  668. auto prefix = String::formatted("slab_{}", slab_size);
  669. json.add(String::formatted("{}_num_allocated", prefix), num_allocated);
  670. json.add(String::formatted("{}_num_free", prefix), num_free);
  671. });
  672. json.finish();
  673. return true;
  674. }
  675. static bool procfs$all(InodeIdentifier, KBufferBuilder& builder)
  676. {
  677. JsonArraySerializer array { builder };
  678. // Keep this in sync with CProcessStatistics.
  679. auto build_process = [&](const Process& process) {
  680. auto process_object = array.add_object();
  681. if (process.is_user_process()) {
  682. StringBuilder pledge_builder;
  683. #define __ENUMERATE_PLEDGE_PROMISE(promise) \
  684. if (process.has_promised(Pledge::promise)) { \
  685. pledge_builder.append(#promise " "); \
  686. }
  687. ENUMERATE_PLEDGE_PROMISES
  688. #undef __ENUMERATE_PLEDGE_PROMISE
  689. process_object.add("pledge", pledge_builder.to_string());
  690. switch (process.veil_state()) {
  691. case VeilState::None:
  692. process_object.add("veil", "None");
  693. break;
  694. case VeilState::Dropped:
  695. process_object.add("veil", "Dropped");
  696. break;
  697. case VeilState::Locked:
  698. process_object.add("veil", "Locked");
  699. break;
  700. }
  701. } else {
  702. process_object.add("pledge", String());
  703. process_object.add("veil", String());
  704. }
  705. process_object.add("pid", process.pid().value());
  706. process_object.add("pgid", process.tty() ? process.tty()->pgid().value() : 0);
  707. process_object.add("pgp", process.pgid().value());
  708. process_object.add("sid", process.sid().value());
  709. process_object.add("uid", process.uid());
  710. process_object.add("gid", process.gid());
  711. process_object.add("ppid", process.ppid().value());
  712. process_object.add("nfds", process.number_of_open_file_descriptors());
  713. process_object.add("name", process.name());
  714. process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
  715. process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
  716. process_object.add("amount_virtual", process.space().amount_virtual());
  717. process_object.add("amount_resident", process.space().amount_resident());
  718. process_object.add("amount_dirty_private", process.space().amount_dirty_private());
  719. process_object.add("amount_clean_inode", process.space().amount_clean_inode());
  720. process_object.add("amount_shared", process.space().amount_shared());
  721. process_object.add("amount_purgeable_volatile", process.space().amount_purgeable_volatile());
  722. process_object.add("amount_purgeable_nonvolatile", process.space().amount_purgeable_nonvolatile());
  723. process_object.add("dumpable", process.is_dumpable());
  724. process_object.add("kernel", process.is_kernel_process());
  725. auto thread_array = process_object.add_array("threads");
  726. process.for_each_thread([&](const Thread& thread) {
  727. auto thread_object = thread_array.add_object();
  728. #if LOCK_DEBUG
  729. thread_object.add("lock_count", thread.lock_count());
  730. #endif
  731. thread_object.add("tid", thread.tid().value());
  732. thread_object.add("name", thread.name());
  733. thread_object.add("times_scheduled", thread.times_scheduled());
  734. thread_object.add("ticks_user", thread.ticks_in_user());
  735. thread_object.add("ticks_kernel", thread.ticks_in_kernel());
  736. thread_object.add("state", thread.state_string());
  737. thread_object.add("cpu", thread.cpu());
  738. thread_object.add("priority", thread.priority());
  739. thread_object.add("syscall_count", thread.syscall_count());
  740. thread_object.add("inode_faults", thread.inode_faults());
  741. thread_object.add("zero_faults", thread.zero_faults());
  742. thread_object.add("cow_faults", thread.cow_faults());
  743. thread_object.add("file_read_bytes", thread.file_read_bytes());
  744. thread_object.add("file_write_bytes", thread.file_write_bytes());
  745. thread_object.add("unix_socket_read_bytes", thread.unix_socket_read_bytes());
  746. thread_object.add("unix_socket_write_bytes", thread.unix_socket_write_bytes());
  747. thread_object.add("ipv4_socket_read_bytes", thread.ipv4_socket_read_bytes());
  748. thread_object.add("ipv4_socket_write_bytes", thread.ipv4_socket_write_bytes());
  749. return IterationDecision::Continue;
  750. });
  751. };
  752. ScopedSpinLock lock(g_scheduler_lock);
  753. auto processes = Process::all_processes();
  754. build_process(*Scheduler::colonel());
  755. for (auto& process : processes)
  756. build_process(process);
  757. array.finish();
  758. return true;
  759. }
  760. struct SysVariable {
  761. String name;
  762. enum class Type : u8 {
  763. Invalid,
  764. Boolean,
  765. String,
  766. };
  767. Type type { Type::Invalid };
  768. Function<void()> notify_callback;
  769. void* address { nullptr };
  770. static SysVariable& for_inode(InodeIdentifier);
  771. void notify()
  772. {
  773. if (notify_callback)
  774. notify_callback();
  775. }
  776. };
  777. static Vector<SysVariable, 16>* s_sys_variables;
  778. static inline Vector<SysVariable, 16>& sys_variables()
  779. {
  780. if (s_sys_variables == nullptr) {
  781. s_sys_variables = new Vector<SysVariable, 16>;
  782. s_sys_variables->append({ "", SysVariable::Type::Invalid, nullptr, nullptr });
  783. }
  784. return *s_sys_variables;
  785. }
  786. SysVariable& SysVariable::for_inode(InodeIdentifier id)
  787. {
  788. auto index = to_sys_index(id);
  789. if (index >= sys_variables().size())
  790. return sys_variables()[0];
  791. auto& variable = sys_variables()[index];
  792. VERIFY(variable.address);
  793. return variable;
  794. }
  795. static bool read_sys_bool(InodeIdentifier inode_id, KBufferBuilder& builder)
  796. {
  797. auto& variable = SysVariable::for_inode(inode_id);
  798. VERIFY(variable.type == SysVariable::Type::Boolean);
  799. u8 buffer[2];
  800. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  801. {
  802. Locker locker(lockable_bool->lock(), Lock::Mode::Shared);
  803. buffer[0] = lockable_bool->resource() ? '1' : '0';
  804. }
  805. buffer[1] = '\n';
  806. builder.append_bytes(ReadonlyBytes { buffer, sizeof(buffer) });
  807. return true;
  808. }
  809. static ssize_t write_sys_bool(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  810. {
  811. auto& variable = SysVariable::for_inode(inode_id);
  812. VERIFY(variable.type == SysVariable::Type::Boolean);
  813. char value = 0;
  814. bool did_read = false;
  815. ssize_t nread = buffer.read_buffered<1>(1, [&](const u8* data, size_t) {
  816. if (did_read)
  817. return 0;
  818. value = (char)data[0];
  819. did_read = true;
  820. return 1;
  821. });
  822. if (nread < 0)
  823. return nread;
  824. VERIFY(nread == 0 || (nread == 1 && did_read));
  825. if (nread == 0 || !(value == '0' || value == '1'))
  826. return (ssize_t)size;
  827. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  828. {
  829. Locker locker(lockable_bool->lock());
  830. lockable_bool->resource() = value == '1';
  831. }
  832. variable.notify();
  833. return (ssize_t)size;
  834. }
  835. static bool read_sys_string(InodeIdentifier inode_id, KBufferBuilder& builder)
  836. {
  837. auto& variable = SysVariable::for_inode(inode_id);
  838. VERIFY(variable.type == SysVariable::Type::String);
  839. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  840. Locker locker(lockable_string->lock(), Lock::Mode::Shared);
  841. builder.append_bytes(lockable_string->resource().bytes());
  842. return true;
  843. }
  844. static ssize_t write_sys_string(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  845. {
  846. auto& variable = SysVariable::for_inode(inode_id);
  847. VERIFY(variable.type == SysVariable::Type::String);
  848. auto string_copy = buffer.copy_into_string(size);
  849. if (string_copy.is_null())
  850. return -EFAULT;
  851. {
  852. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  853. Locker locker(lockable_string->lock());
  854. lockable_string->resource() = move(string_copy);
  855. }
  856. variable.notify();
  857. return (ssize_t)size;
  858. }
  859. void ProcFS::add_sys_bool(String&& name, Lockable<bool>& var, Function<void()>&& notify_callback)
  860. {
  861. InterruptDisabler disabler;
  862. SysVariable variable;
  863. variable.name = move(name);
  864. variable.type = SysVariable::Type::Boolean;
  865. variable.notify_callback = move(notify_callback);
  866. variable.address = &var;
  867. sys_variables().append(move(variable));
  868. }
  869. bool ProcFS::initialize()
  870. {
  871. static Lockable<bool>* kmalloc_stack_helper;
  872. static Lockable<bool>* ubsan_deadly_helper;
  873. if (kmalloc_stack_helper == nullptr) {
  874. kmalloc_stack_helper = new Lockable<bool>();
  875. kmalloc_stack_helper->resource() = g_dump_kmalloc_stacks;
  876. ProcFS::add_sys_bool("kmalloc_stacks", *kmalloc_stack_helper, [] {
  877. g_dump_kmalloc_stacks = kmalloc_stack_helper->resource();
  878. });
  879. ubsan_deadly_helper = new Lockable<bool>();
  880. ubsan_deadly_helper->resource() = UBSanitizer::g_ubsan_is_deadly;
  881. ProcFS::add_sys_bool("ubsan_is_deadly", *ubsan_deadly_helper, [] {
  882. UBSanitizer::g_ubsan_is_deadly = ubsan_deadly_helper->resource();
  883. });
  884. }
  885. return true;
  886. }
  887. const char* ProcFS::class_name() const
  888. {
  889. return "ProcFS";
  890. }
  891. NonnullRefPtr<Inode> ProcFS::root_inode() const
  892. {
  893. return *m_root_inode;
  894. }
  895. RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  896. {
  897. dbgln_if(PROCFS_DEBUG, "ProcFS::get_inode({})", inode_id.index());
  898. if (inode_id == root_inode()->identifier())
  899. return m_root_inode;
  900. Locker locker(m_inodes_lock);
  901. auto it = m_inodes.find(inode_id.index().value());
  902. if (it != m_inodes.end()) {
  903. // It's possible that the ProcFSInode ref count was dropped to 0 or
  904. // the ~ProcFSInode destructor is even running already, but blocked
  905. // from removing it from this map. So we need to *try* to ref it,
  906. // and if that fails we cannot return this instance anymore and just
  907. // create a new one.
  908. if (it->value->try_ref())
  909. return adopt_ref(*it->value);
  910. // We couldn't ref it, so just create a new one and replace the entry
  911. }
  912. auto inode = adopt_ref(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  913. auto result = m_inodes.set(inode_id.index().value(), inode.ptr());
  914. VERIFY(result == ((it == m_inodes.end()) ? AK::HashSetResult::InsertedNewEntry : AK::HashSetResult::ReplacedExistingEntry));
  915. return inode;
  916. }
  917. ProcFSInode::ProcFSInode(ProcFS& fs, InodeIndex index)
  918. : Inode(fs, index)
  919. {
  920. }
  921. ProcFSInode::~ProcFSInode()
  922. {
  923. Locker locker(fs().m_inodes_lock);
  924. auto it = fs().m_inodes.find(index().value());
  925. if (it != fs().m_inodes.end() && it->value == this)
  926. fs().m_inodes.remove(it);
  927. }
  928. RefPtr<Process> ProcFSInode::process() const
  929. {
  930. auto parent = to_proc_parent_directory(identifier());
  931. if (parent == PDI_PID || parent == PDI_PID_fd || parent == PDI_PID_stacks)
  932. return Process::from_pid(to_pid(identifier()));
  933. return nullptr;
  934. }
  935. KResult ProcFSInode::refresh_data(FileDescription& description) const
  936. {
  937. if (Kernel::is_directory(identifier()))
  938. return KSuccess;
  939. // For process-specific inodes, hold the process's ptrace lock across refresh
  940. // and refuse to load data if the process is not dumpable.
  941. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  942. auto process = this->process();
  943. if (process) {
  944. process->ptrace_lock().lock();
  945. if (!process->is_dumpable()) {
  946. process->ptrace_lock().unlock();
  947. return EPERM;
  948. }
  949. }
  950. ScopeGuard guard = [&] {
  951. if (process)
  952. process->ptrace_lock().unlock();
  953. };
  954. auto& cached_data = description.data();
  955. auto* directory_entry = fs().get_directory_entry(identifier());
  956. bool (*read_callback)(InodeIdentifier, KBufferBuilder&) = nullptr;
  957. if (directory_entry) {
  958. read_callback = directory_entry->read_callback;
  959. VERIFY(read_callback);
  960. } else {
  961. switch (to_proc_parent_directory(identifier())) {
  962. case PDI_PID_fd:
  963. read_callback = procfs$pid_fd_entry;
  964. break;
  965. case PDI_PID_stacks:
  966. read_callback = procfs$tid_stack;
  967. break;
  968. case PDI_Root_sys:
  969. switch (SysVariable::for_inode(identifier()).type) {
  970. case SysVariable::Type::Invalid:
  971. VERIFY_NOT_REACHED();
  972. case SysVariable::Type::Boolean:
  973. read_callback = read_sys_bool;
  974. break;
  975. case SysVariable::Type::String:
  976. read_callback = read_sys_string;
  977. break;
  978. }
  979. break;
  980. default:
  981. VERIFY_NOT_REACHED();
  982. }
  983. VERIFY(read_callback);
  984. }
  985. if (!cached_data)
  986. cached_data = new ProcFSInodeData;
  987. auto& buffer = static_cast<ProcFSInodeData&>(*cached_data).buffer;
  988. if (buffer) {
  989. // If we're reusing the buffer, reset the size to 0 first. This
  990. // ensures we don't accidentally leak previously written data.
  991. buffer->set_size(0);
  992. }
  993. KBufferBuilder builder(buffer, true);
  994. if (!read_callback(identifier(), builder))
  995. return ENOENT;
  996. // We don't use builder.build() here, which would steal our buffer
  997. // and turn it into an OwnPtr. Instead, just flush to the buffer so
  998. // that we can read all the data that was written.
  999. if (!builder.flush())
  1000. return ENOMEM;
  1001. if (!buffer)
  1002. return ENOMEM;
  1003. return KSuccess;
  1004. }
  1005. KResult ProcFSInode::attach(FileDescription& description)
  1006. {
  1007. return refresh_data(description);
  1008. }
  1009. void ProcFSInode::did_seek(FileDescription& description, off_t new_offset)
  1010. {
  1011. if (new_offset != 0)
  1012. return;
  1013. auto result = refresh_data(description);
  1014. if (result.is_error()) {
  1015. // Subsequent calls to read will return EIO!
  1016. dbgln("ProcFS: Could not refresh contents: {}", result.error());
  1017. }
  1018. }
  1019. InodeMetadata ProcFSInode::metadata() const
  1020. {
  1021. dbgln_if(PROCFS_DEBUG, "ProcFSInode::metadata({})", index());
  1022. InodeMetadata metadata;
  1023. metadata.inode = identifier();
  1024. metadata.ctime = mepoch;
  1025. metadata.atime = mepoch;
  1026. metadata.mtime = mepoch;
  1027. auto proc_parent_directory = to_proc_parent_directory(identifier());
  1028. auto proc_file_type = to_proc_file_type(identifier());
  1029. dbgln_if(PROCFS_DEBUG, " -> pid={}, fi={}, pdi={}", to_pid(identifier()).value(), (int)proc_file_type, (int)proc_parent_directory);
  1030. if (is_process_related_file(identifier())) {
  1031. ProcessID pid = to_pid(identifier());
  1032. auto process = Process::from_pid(pid);
  1033. if (process && process->is_dumpable()) {
  1034. metadata.uid = process->euid();
  1035. metadata.gid = process->egid();
  1036. } else {
  1037. metadata.uid = 0;
  1038. metadata.gid = 0;
  1039. }
  1040. } else if (is_thread_related_file(identifier())) {
  1041. ThreadID tid = to_tid(identifier());
  1042. auto thread = Thread::from_tid(tid);
  1043. if (thread && thread->process().is_dumpable()) {
  1044. metadata.uid = thread->process().euid();
  1045. metadata.gid = thread->process().egid();
  1046. } else {
  1047. metadata.uid = 0;
  1048. metadata.gid = 0;
  1049. }
  1050. }
  1051. if (proc_parent_directory == PDI_PID_fd) {
  1052. metadata.mode = S_IFLNK | S_IRUSR | S_IWUSR | S_IXUSR;
  1053. return metadata;
  1054. }
  1055. switch (proc_file_type) {
  1056. case FI_Root_self:
  1057. metadata.mode = S_IFLNK | S_IRUSR | S_IRGRP | S_IROTH;
  1058. break;
  1059. case FI_PID_cwd:
  1060. case FI_PID_exe:
  1061. case FI_PID_root:
  1062. metadata.mode = S_IFLNK | S_IRUSR;
  1063. break;
  1064. case FI_Root:
  1065. case FI_Root_sys:
  1066. case FI_Root_net:
  1067. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
  1068. break;
  1069. case FI_PID:
  1070. case FI_PID_fd:
  1071. case FI_PID_stacks:
  1072. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR;
  1073. break;
  1074. case FI_Root_smbios_entry_point:
  1075. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1076. metadata.size = DMIExpose::the().entry_point_length();
  1077. break;
  1078. case FI_Root_dmi:
  1079. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1080. metadata.size = DMIExpose::the().structure_table_length();
  1081. break;
  1082. default:
  1083. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1084. break;
  1085. }
  1086. if (proc_file_type > FI_Invalid && proc_file_type < FI_MaxStaticFileIndex) {
  1087. if (fs().m_entries[proc_file_type].supervisor_only) {
  1088. metadata.uid = 0;
  1089. metadata.gid = 0;
  1090. metadata.mode &= ~077;
  1091. }
  1092. }
  1093. return metadata;
  1094. }
  1095. KResultOr<ssize_t> ProcFSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  1096. {
  1097. dbgln_if(PROCFS_DEBUG, "ProcFS: read_bytes offset: {} count: {}", offset, count);
  1098. VERIFY(offset >= 0);
  1099. VERIFY(buffer.user_or_kernel_ptr());
  1100. if (!description)
  1101. return EIO;
  1102. if (!description->data()) {
  1103. dbgln_if(PROCFS_DEBUG, "ProcFS: Do not have cached data!");
  1104. return EIO;
  1105. }
  1106. // Be sure to keep a reference to data_buffer while we use it!
  1107. RefPtr<KBufferImpl> data_buffer = static_cast<ProcFSInodeData&>(*description->data()).buffer;
  1108. if (!data_buffer || (size_t)offset >= data_buffer->size())
  1109. return 0;
  1110. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  1111. if (!buffer.write(data_buffer->data() + offset, nread))
  1112. return EFAULT;
  1113. return nread;
  1114. }
  1115. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  1116. {
  1117. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  1118. }
  1119. KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  1120. {
  1121. dbgln_if(PROCFS_DEBUG, "ProcFS: traverse_as_directory {}", index());
  1122. if (!Kernel::is_directory(identifier()))
  1123. return ENOTDIR;
  1124. auto proc_file_type = to_proc_file_type(identifier());
  1125. auto parent_id = to_parent_id(identifier());
  1126. callback({ ".", identifier(), 2 });
  1127. callback({ "..", parent_id, 2 });
  1128. switch (proc_file_type) {
  1129. case FI_Root:
  1130. for (auto& entry : fs().m_entries) {
  1131. // FIXME: strlen() here is sad.
  1132. if (!entry.name)
  1133. continue;
  1134. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  1135. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  1136. }
  1137. for (auto pid_child : Process::all_pids()) {
  1138. callback({ String::number(pid_child.value()), to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  1139. }
  1140. break;
  1141. case FI_Root_sys:
  1142. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1143. auto& variable = sys_variables()[i];
  1144. callback({ variable.name, sys_var_to_identifier(fsid(), i), 0 });
  1145. }
  1146. break;
  1147. case FI_Root_net:
  1148. callback({ "adapters", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_adapters), 0 });
  1149. callback({ "arp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_arp), 0 });
  1150. callback({ "tcp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_tcp), 0 });
  1151. callback({ "udp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_udp), 0 });
  1152. callback({ "local", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_local), 0 });
  1153. break;
  1154. case FI_PID: {
  1155. auto pid = to_pid(identifier());
  1156. auto process = Process::from_pid(pid);
  1157. if (!process)
  1158. return ENOENT;
  1159. for (auto& entry : fs().m_entries) {
  1160. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1161. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1162. continue;
  1163. // FIXME: strlen() here is sad.
  1164. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  1165. }
  1166. }
  1167. } break;
  1168. case FI_PID_fd: {
  1169. auto pid = to_pid(identifier());
  1170. auto process = Process::from_pid(pid);
  1171. if (!process)
  1172. return ENOENT;
  1173. for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
  1174. auto description = process->file_description(i);
  1175. if (!description)
  1176. continue;
  1177. callback({ String::number(i), to_identifier_with_fd(fsid(), pid, i), 0 });
  1178. }
  1179. } break;
  1180. case FI_PID_stacks: {
  1181. auto pid = to_pid(identifier());
  1182. auto process = Process::from_pid(pid);
  1183. if (!process)
  1184. return ENOENT;
  1185. process->for_each_thread([&](const Thread& thread) -> IterationDecision {
  1186. int tid = thread.tid().value();
  1187. callback({ String::number(tid), to_identifier_with_stack(fsid(), tid), 0 });
  1188. return IterationDecision::Continue;
  1189. });
  1190. } break;
  1191. default:
  1192. return KSuccess;
  1193. }
  1194. return KSuccess;
  1195. }
  1196. RefPtr<Inode> ProcFSInode::lookup(StringView name)
  1197. {
  1198. VERIFY(is_directory());
  1199. if (name == ".")
  1200. return this;
  1201. if (name == "..")
  1202. return fs().get_inode(to_parent_id(identifier()));
  1203. auto proc_file_type = to_proc_file_type(identifier());
  1204. if (proc_file_type == FI_Root) {
  1205. for (auto& entry : fs().m_entries) {
  1206. if (entry.name == nullptr)
  1207. continue;
  1208. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  1209. if (name == entry.name) {
  1210. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type));
  1211. }
  1212. }
  1213. }
  1214. auto name_as_number = name.to_uint();
  1215. if (!name_as_number.has_value())
  1216. return {};
  1217. bool process_exists = false;
  1218. {
  1219. InterruptDisabler disabler;
  1220. process_exists = Process::from_pid(name_as_number.value());
  1221. }
  1222. if (process_exists)
  1223. return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number.value(), FI_PID));
  1224. return {};
  1225. }
  1226. if (proc_file_type == FI_Root_sys) {
  1227. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1228. auto& variable = sys_variables()[i];
  1229. if (name == variable.name)
  1230. return fs().get_inode(sys_var_to_identifier(fsid(), i));
  1231. }
  1232. return {};
  1233. }
  1234. if (proc_file_type == FI_Root_net) {
  1235. if (name == "adapters")
  1236. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_adapters));
  1237. if (name == "arp")
  1238. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_arp));
  1239. if (name == "tcp")
  1240. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_tcp));
  1241. if (name == "udp")
  1242. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_udp));
  1243. if (name == "local")
  1244. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_local));
  1245. return {};
  1246. }
  1247. if (proc_file_type == FI_PID) {
  1248. auto process = Process::from_pid(to_pid(identifier()));
  1249. if (!process)
  1250. return {};
  1251. for (auto& entry : fs().m_entries) {
  1252. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1253. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1254. continue;
  1255. if (entry.name == nullptr)
  1256. continue;
  1257. if (name == entry.name) {
  1258. return fs().get_inode(to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type));
  1259. }
  1260. }
  1261. }
  1262. return {};
  1263. }
  1264. if (proc_file_type == FI_PID_fd) {
  1265. auto name_as_number = name.to_uint();
  1266. if (!name_as_number.has_value())
  1267. return {};
  1268. bool fd_exists = false;
  1269. {
  1270. if (auto process = Process::from_pid(to_pid(identifier())))
  1271. fd_exists = process->file_description(name_as_number.value());
  1272. }
  1273. if (fd_exists)
  1274. return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number.value()));
  1275. }
  1276. if (proc_file_type == FI_PID_stacks) {
  1277. auto name_as_number = name.to_int();
  1278. if (!name_as_number.has_value())
  1279. return {};
  1280. int tid = name_as_number.value();
  1281. if (tid <= 0) {
  1282. return {};
  1283. }
  1284. bool thread_exists = false;
  1285. {
  1286. auto process = Process::from_pid(to_pid(identifier()));
  1287. auto thread = Thread::from_tid(tid);
  1288. thread_exists = process && thread && process->pid() == thread->pid();
  1289. }
  1290. if (thread_exists)
  1291. return fs().get_inode(to_identifier_with_stack(fsid(), tid));
  1292. }
  1293. return {};
  1294. }
  1295. void ProcFSInode::flush_metadata()
  1296. {
  1297. }
  1298. KResultOr<ssize_t> ProcFSInode::write_bytes(off_t offset, ssize_t size, const UserOrKernelBuffer& buffer, FileDescription*)
  1299. {
  1300. // For process-specific inodes, hold the process's ptrace lock across the write
  1301. // and refuse to write at all data if the process is not dumpable.
  1302. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  1303. auto process = this->process();
  1304. if (process) {
  1305. process->ptrace_lock().lock();
  1306. if (!process->is_dumpable()) {
  1307. process->ptrace_lock().unlock();
  1308. return EPERM;
  1309. }
  1310. }
  1311. ScopeGuard guard = [&] {
  1312. if (process)
  1313. process->ptrace_lock().unlock();
  1314. };
  1315. auto result = prepare_to_write_data();
  1316. if (result.is_error())
  1317. return result;
  1318. auto* directory_entry = fs().get_directory_entry(identifier());
  1319. ssize_t (*write_callback)(InodeIdentifier, const UserOrKernelBuffer&, size_t) = nullptr;
  1320. if (directory_entry == nullptr) {
  1321. if (to_proc_parent_directory(identifier()) == PDI_Root_sys) {
  1322. switch (SysVariable::for_inode(identifier()).type) {
  1323. case SysVariable::Type::Invalid:
  1324. VERIFY_NOT_REACHED();
  1325. case SysVariable::Type::Boolean:
  1326. write_callback = write_sys_bool;
  1327. break;
  1328. case SysVariable::Type::String:
  1329. write_callback = write_sys_string;
  1330. break;
  1331. }
  1332. } else
  1333. return EPERM;
  1334. } else {
  1335. if (!directory_entry->write_callback)
  1336. return EPERM;
  1337. write_callback = directory_entry->write_callback;
  1338. }
  1339. VERIFY(is_persistent_inode(identifier()));
  1340. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  1341. VERIFY(offset == 0);
  1342. ssize_t nwritten = write_callback(identifier(), buffer, (size_t)size);
  1343. if (nwritten < 0)
  1344. dbgln("ProcFS: Writing {} bytes failed: {}", size, nwritten);
  1345. return nwritten;
  1346. }
  1347. KResultOr<NonnullRefPtr<Custody>> ProcFSInode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  1348. {
  1349. if (FI_Root_self == to_proc_file_type(identifier())) {
  1350. return VFS::the().resolve_path(String::number(Process::current()->pid().value()), base, out_parent, options, symlink_recursion_level);
  1351. }
  1352. // The only other links are in pid directories, so it's safe to ignore
  1353. // unrelated files and the thread-specific stacks/ directory.
  1354. if (!is_process_related_file(identifier()))
  1355. return Inode::resolve_as_link(base, out_parent, options, symlink_recursion_level);
  1356. // FIXME: We should return a custody for FI_PID or FI_PID_fd here
  1357. // for correctness. It's impossible to create files in ProcFS,
  1358. // so returning null shouldn't break much.
  1359. if (out_parent)
  1360. *out_parent = nullptr;
  1361. auto pid = to_pid(identifier());
  1362. auto proc_file_type = to_proc_file_type(identifier());
  1363. auto process = Process::from_pid(pid);
  1364. if (!process)
  1365. return ENOENT;
  1366. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  1367. if (out_parent)
  1368. *out_parent = base;
  1369. int fd = to_fd(identifier());
  1370. auto description = process->file_description(fd);
  1371. if (!description)
  1372. return ENOENT;
  1373. auto proxy_inode = ProcFSProxyInode::create(const_cast<ProcFS&>(fs()), *description);
  1374. return Custody::create(&base, "", proxy_inode, base.mount_flags());
  1375. }
  1376. Custody* res = nullptr;
  1377. switch (proc_file_type) {
  1378. case FI_PID_cwd:
  1379. res = &process->current_directory();
  1380. break;
  1381. case FI_PID_exe:
  1382. res = process->executable();
  1383. break;
  1384. case FI_PID_root:
  1385. // Note: we open root_directory() here, not
  1386. // root_directory_relative_to_global_root().
  1387. // This seems more useful.
  1388. res = &process->root_directory();
  1389. break;
  1390. default:
  1391. VERIFY_NOT_REACHED();
  1392. }
  1393. if (!res)
  1394. return ENOENT;
  1395. return *res;
  1396. }
  1397. ProcFSProxyInode::ProcFSProxyInode(ProcFS& fs, FileDescription& fd)
  1398. : Inode(fs, 0)
  1399. , m_fd(fd)
  1400. {
  1401. }
  1402. ProcFSProxyInode::~ProcFSProxyInode()
  1403. {
  1404. }
  1405. KResult ProcFSProxyInode::attach(FileDescription& fd)
  1406. {
  1407. return m_fd->inode()->attach(fd);
  1408. }
  1409. void ProcFSProxyInode::did_seek(FileDescription& fd, off_t new_offset)
  1410. {
  1411. return m_fd->inode()->did_seek(fd, new_offset);
  1412. }
  1413. InodeMetadata ProcFSProxyInode::metadata() const
  1414. {
  1415. InodeMetadata metadata = m_fd->metadata();
  1416. if (m_fd->is_readable())
  1417. metadata.mode |= 0444;
  1418. else
  1419. metadata.mode &= ~0444;
  1420. if (m_fd->is_writable())
  1421. metadata.mode |= 0222;
  1422. else
  1423. metadata.mode &= ~0222;
  1424. if (!metadata.is_directory())
  1425. metadata.mode &= ~0111;
  1426. return metadata;
  1427. }
  1428. KResultOr<NonnullRefPtr<Inode>> ProcFSProxyInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  1429. {
  1430. if (!m_fd->inode())
  1431. return EINVAL;
  1432. return m_fd->inode()->create_child(name, mode, dev, uid, gid);
  1433. }
  1434. KResult ProcFSProxyInode::add_child(Inode& child, const StringView& name, mode_t mode)
  1435. {
  1436. if (!m_fd->inode())
  1437. return EINVAL;
  1438. return m_fd->inode()->add_child(child, name, mode);
  1439. }
  1440. KResult ProcFSProxyInode::remove_child(const StringView& name)
  1441. {
  1442. if (!m_fd->inode())
  1443. return EINVAL;
  1444. return m_fd->inode()->remove_child(name);
  1445. }
  1446. RefPtr<Inode> ProcFSProxyInode::lookup(StringView name)
  1447. {
  1448. if (!m_fd->inode())
  1449. return {};
  1450. return m_fd->inode()->lookup(name);
  1451. }
  1452. KResultOr<size_t> ProcFSProxyInode::directory_entry_count() const
  1453. {
  1454. if (!m_fd->inode())
  1455. return EINVAL;
  1456. return m_fd->inode()->directory_entry_count();
  1457. }
  1458. KResultOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  1459. {
  1460. return EPERM;
  1461. }
  1462. KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t)
  1463. {
  1464. return EPERM;
  1465. }
  1466. KResult ProcFSInode::remove_child([[maybe_unused]] const StringView& name)
  1467. {
  1468. return EPERM;
  1469. }
  1470. KResultOr<size_t> ProcFSInode::directory_entry_count() const
  1471. {
  1472. VERIFY(is_directory());
  1473. size_t count = 0;
  1474. KResult result = traverse_as_directory([&count](auto&) {
  1475. ++count;
  1476. return true;
  1477. });
  1478. if (result.is_error())
  1479. return result;
  1480. return count;
  1481. }
  1482. KResult ProcFSInode::chmod(mode_t)
  1483. {
  1484. return EPERM;
  1485. }
  1486. ProcFS::ProcFS()
  1487. {
  1488. m_root_inode = adopt_ref(*new ProcFSInode(*this, 1));
  1489. m_entries.resize(FI_MaxStaticFileIndex);
  1490. m_entries[FI_Root_df] = { "df", FI_Root_df, false, procfs$df };
  1491. m_entries[FI_Root_all] = { "all", FI_Root_all, false, procfs$all };
  1492. m_entries[FI_Root_memstat] = { "memstat", FI_Root_memstat, false, procfs$memstat };
  1493. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, false, procfs$cpuinfo };
  1494. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, true, procfs$dmesg };
  1495. m_entries[FI_Root_self] = { "self", FI_Root_self, false, procfs$self };
  1496. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, false, procfs$pci };
  1497. m_entries[FI_Root_interrupts] = { "interrupts", FI_Root_interrupts, false, procfs$interrupts };
  1498. m_entries[FI_Root_dmi] = { "DMI", FI_Root_dmi, false, procfs$dmi };
  1499. m_entries[FI_Root_smbios_entry_point] = { "smbios_entry_point", FI_Root_smbios_entry_point, false, procfs$smbios_entry_point };
  1500. m_entries[FI_Root_keymap] = { "keymap", FI_Root_keymap, false, procfs$keymap };
  1501. m_entries[FI_Root_devices] = { "devices", FI_Root_devices, false, procfs$devices };
  1502. m_entries[FI_Root_uptime] = { "uptime", FI_Root_uptime, false, procfs$uptime };
  1503. m_entries[FI_Root_cmdline] = { "cmdline", FI_Root_cmdline, true, procfs$cmdline };
  1504. m_entries[FI_Root_modules] = { "modules", FI_Root_modules, true, procfs$modules };
  1505. m_entries[FI_Root_profile] = { "profile", FI_Root_profile, true, procfs$profile };
  1506. m_entries[FI_Root_sys] = { "sys", FI_Root_sys, true };
  1507. m_entries[FI_Root_net] = { "net", FI_Root_net, false };
  1508. m_entries[FI_Root_net_adapters] = { "adapters", FI_Root_net_adapters, false, procfs$net_adapters };
  1509. m_entries[FI_Root_net_arp] = { "arp", FI_Root_net_arp, true, procfs$net_arp };
  1510. m_entries[FI_Root_net_tcp] = { "tcp", FI_Root_net_tcp, false, procfs$net_tcp };
  1511. m_entries[FI_Root_net_udp] = { "udp", FI_Root_net_udp, false, procfs$net_udp };
  1512. m_entries[FI_Root_net_local] = { "local", FI_Root_net_local, false, procfs$net_local };
  1513. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, false, procfs$pid_vm };
  1514. m_entries[FI_PID_stacks] = { "stacks", FI_PID_stacks, false };
  1515. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, false, procfs$pid_fds };
  1516. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, false, procfs$pid_exe };
  1517. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, false, procfs$pid_cwd };
  1518. m_entries[FI_PID_unveil] = { "unveil", FI_PID_unveil, false, procfs$pid_unveil };
  1519. m_entries[FI_PID_root] = { "root", FI_PID_root, false, procfs$pid_root };
  1520. m_entries[FI_PID_perf_events] = { "perf_events", FI_PID_perf_events, false, procfs$pid_perf_events };
  1521. m_entries[FI_PID_fd] = { "fd", FI_PID_fd, false };
  1522. }
  1523. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  1524. {
  1525. auto proc_file_type = to_proc_file_type(identifier);
  1526. if (proc_file_type != FI_Invalid && proc_file_type != FI_Root_sys_variable && proc_file_type < FI_MaxStaticFileIndex)
  1527. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1528. return nullptr;
  1529. }
  1530. KResult ProcFSInode::chown(uid_t, gid_t)
  1531. {
  1532. return EPERM;
  1533. }
  1534. }