ProcFS.cpp 59 KB

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