ProcFS.cpp 60 KB

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