ProcFS.cpp 60 KB

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