ProcFS.cpp 60 KB

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