ProcFS.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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/KeyboardDevice.h>
  39. #include <Kernel/FileSystem/Custody.h>
  40. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  41. #include <Kernel/FileSystem/FileDescription.h>
  42. #include <Kernel/FileSystem/ProcFS.h>
  43. #include <Kernel/FileSystem/VirtualFileSystem.h>
  44. #include <Kernel/Heap/kmalloc.h>
  45. #include <Kernel/Interrupts/GenericInterruptHandler.h>
  46. #include <Kernel/Interrupts/InterruptManagement.h>
  47. #include <Kernel/KBufferBuilder.h>
  48. #include <Kernel/KSyms.h>
  49. #include <Kernel/Module.h>
  50. #include <Kernel/Net/LocalSocket.h>
  51. #include <Kernel/Net/NetworkAdapter.h>
  52. #include <Kernel/Net/Routing.h>
  53. #include <Kernel/Net/TCPSocket.h>
  54. #include <Kernel/Net/UDPSocket.h>
  55. #include <Kernel/PCI/Access.h>
  56. #include <Kernel/PerformanceEventBuffer.h>
  57. #include <Kernel/Process.h>
  58. #include <Kernel/Scheduler.h>
  59. #include <Kernel/StdLib.h>
  60. #include <Kernel/TTY/TTY.h>
  61. #include <Kernel/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", KeyboardDevice::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. auto thread_array = process_object.add_array("threads");
  742. process.for_each_thread([&](const Thread& thread) {
  743. auto thread_object = thread_array.add_object();
  744. thread_object.add("tid", thread.tid().value());
  745. thread_object.add("name", thread.name());
  746. thread_object.add("times_scheduled", thread.times_scheduled());
  747. thread_object.add("ticks_user", thread.ticks_in_user());
  748. thread_object.add("ticks_kernel", thread.ticks_in_kernel());
  749. thread_object.add("state", thread.state_string());
  750. thread_object.add("cpu", thread.cpu());
  751. thread_object.add("priority", thread.priority());
  752. thread_object.add("syscall_count", thread.syscall_count());
  753. thread_object.add("inode_faults", thread.inode_faults());
  754. thread_object.add("zero_faults", thread.zero_faults());
  755. thread_object.add("cow_faults", thread.cow_faults());
  756. thread_object.add("file_read_bytes", thread.file_read_bytes());
  757. thread_object.add("file_write_bytes", thread.file_write_bytes());
  758. thread_object.add("unix_socket_read_bytes", thread.unix_socket_read_bytes());
  759. thread_object.add("unix_socket_write_bytes", thread.unix_socket_write_bytes());
  760. thread_object.add("ipv4_socket_read_bytes", thread.ipv4_socket_read_bytes());
  761. thread_object.add("ipv4_socket_write_bytes", thread.ipv4_socket_write_bytes());
  762. return IterationDecision::Continue;
  763. });
  764. };
  765. ScopedSpinLock lock(g_scheduler_lock);
  766. auto processes = Process::all_processes();
  767. build_process(*Scheduler::colonel());
  768. for (auto& process : processes)
  769. build_process(process);
  770. array.finish();
  771. return true;
  772. }
  773. struct SysVariable {
  774. String name;
  775. enum class Type : u8 {
  776. Invalid,
  777. Boolean,
  778. String,
  779. };
  780. Type type { Type::Invalid };
  781. Function<void()> notify_callback;
  782. void* address { nullptr };
  783. static SysVariable& for_inode(InodeIdentifier);
  784. void notify()
  785. {
  786. if (notify_callback)
  787. notify_callback();
  788. }
  789. };
  790. static Vector<SysVariable, 16>* s_sys_variables;
  791. static inline Vector<SysVariable, 16>& sys_variables()
  792. {
  793. if (s_sys_variables == nullptr) {
  794. s_sys_variables = new Vector<SysVariable, 16>;
  795. s_sys_variables->append({ "", SysVariable::Type::Invalid, nullptr, nullptr });
  796. }
  797. return *s_sys_variables;
  798. }
  799. SysVariable& SysVariable::for_inode(InodeIdentifier id)
  800. {
  801. auto index = to_sys_index(id);
  802. if (index >= sys_variables().size())
  803. return sys_variables()[0];
  804. auto& variable = sys_variables()[index];
  805. VERIFY(variable.address);
  806. return variable;
  807. }
  808. static bool read_sys_bool(InodeIdentifier inode_id, KBufferBuilder& builder)
  809. {
  810. auto& variable = SysVariable::for_inode(inode_id);
  811. VERIFY(variable.type == SysVariable::Type::Boolean);
  812. u8 buffer[2];
  813. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  814. {
  815. LOCKER(lockable_bool->lock(), Lock::Mode::Shared);
  816. buffer[0] = lockable_bool->resource() ? '1' : '0';
  817. }
  818. buffer[1] = '\n';
  819. builder.append_bytes(ReadonlyBytes { buffer, sizeof(buffer) });
  820. return true;
  821. }
  822. static ssize_t write_sys_bool(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  823. {
  824. auto& variable = SysVariable::for_inode(inode_id);
  825. VERIFY(variable.type == SysVariable::Type::Boolean);
  826. char value = 0;
  827. bool did_read = false;
  828. ssize_t nread = buffer.read_buffered<1>(1, [&](const u8* data, size_t) {
  829. if (did_read)
  830. return 0;
  831. value = (char)data[0];
  832. did_read = true;
  833. return 1;
  834. });
  835. if (nread < 0)
  836. return nread;
  837. VERIFY(nread == 0 || (nread == 1 && did_read));
  838. if (nread == 0 || !(value == '0' || value == '1'))
  839. return (ssize_t)size;
  840. auto* lockable_bool = reinterpret_cast<Lockable<bool>*>(variable.address);
  841. {
  842. LOCKER(lockable_bool->lock());
  843. lockable_bool->resource() = value == '1';
  844. }
  845. variable.notify();
  846. return (ssize_t)size;
  847. }
  848. static bool read_sys_string(InodeIdentifier inode_id, KBufferBuilder& builder)
  849. {
  850. auto& variable = SysVariable::for_inode(inode_id);
  851. VERIFY(variable.type == SysVariable::Type::String);
  852. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  853. LOCKER(lockable_string->lock(), Lock::Mode::Shared);
  854. builder.append_bytes(lockable_string->resource().bytes());
  855. return true;
  856. }
  857. static ssize_t write_sys_string(InodeIdentifier inode_id, const UserOrKernelBuffer& buffer, size_t size)
  858. {
  859. auto& variable = SysVariable::for_inode(inode_id);
  860. VERIFY(variable.type == SysVariable::Type::String);
  861. auto string_copy = buffer.copy_into_string(size);
  862. if (string_copy.is_null())
  863. return -EFAULT;
  864. {
  865. auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
  866. LOCKER(lockable_string->lock());
  867. lockable_string->resource() = move(string_copy);
  868. }
  869. variable.notify();
  870. return (ssize_t)size;
  871. }
  872. void ProcFS::add_sys_bool(String&& name, Lockable<bool>& var, Function<void()>&& notify_callback)
  873. {
  874. InterruptDisabler disabler;
  875. SysVariable variable;
  876. variable.name = move(name);
  877. variable.type = SysVariable::Type::Boolean;
  878. variable.notify_callback = move(notify_callback);
  879. variable.address = &var;
  880. sys_variables().append(move(variable));
  881. }
  882. void ProcFS::add_sys_string(String&& name, Lockable<String>& var, Function<void()>&& notify_callback)
  883. {
  884. InterruptDisabler disabler;
  885. SysVariable variable;
  886. variable.name = move(name);
  887. variable.type = SysVariable::Type::String;
  888. variable.notify_callback = move(notify_callback);
  889. variable.address = &var;
  890. sys_variables().append(move(variable));
  891. }
  892. bool ProcFS::initialize()
  893. {
  894. static Lockable<bool>* kmalloc_stack_helper;
  895. static Lockable<bool>* ubsan_deadly_helper;
  896. if (kmalloc_stack_helper == nullptr) {
  897. kmalloc_stack_helper = new Lockable<bool>();
  898. kmalloc_stack_helper->resource() = g_dump_kmalloc_stacks;
  899. ProcFS::add_sys_bool("kmalloc_stacks", *kmalloc_stack_helper, [] {
  900. g_dump_kmalloc_stacks = kmalloc_stack_helper->resource();
  901. });
  902. ubsan_deadly_helper = new Lockable<bool>();
  903. ubsan_deadly_helper->resource() = UBSanitizer::g_ubsan_is_deadly;
  904. ProcFS::add_sys_bool("ubsan_is_deadly", *ubsan_deadly_helper, [] {
  905. UBSanitizer::g_ubsan_is_deadly = ubsan_deadly_helper->resource();
  906. });
  907. }
  908. return true;
  909. }
  910. const char* ProcFS::class_name() const
  911. {
  912. return "ProcFS";
  913. }
  914. NonnullRefPtr<Inode> ProcFS::root_inode() const
  915. {
  916. return *m_root_inode;
  917. }
  918. RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
  919. {
  920. dbgln_if(PROCFS_DEBUG, "ProcFS::get_inode({})", inode_id.index());
  921. if (inode_id == root_inode()->identifier())
  922. return m_root_inode;
  923. LOCKER(m_inodes_lock);
  924. auto it = m_inodes.find(inode_id.index().value());
  925. if (it != m_inodes.end()) {
  926. // It's possible that the ProcFSInode ref count was dropped to 0 or
  927. // the ~ProcFSInode destructor is even running already, but blocked
  928. // from removing it from this map. So we need to *try* to ref it,
  929. // and if that fails we cannot return this instance anymore and just
  930. // create a new one.
  931. if (it->value->try_ref())
  932. return adopt(*it->value);
  933. // We couldn't ref it, so just create a new one and replace the entry
  934. }
  935. auto inode = adopt(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
  936. auto result = m_inodes.set(inode_id.index().value(), inode.ptr());
  937. VERIFY(result == ((it == m_inodes.end()) ? AK::HashSetResult::InsertedNewEntry : AK::HashSetResult::ReplacedExistingEntry));
  938. return inode;
  939. }
  940. ProcFSInode::ProcFSInode(ProcFS& fs, InodeIndex index)
  941. : Inode(fs, index)
  942. {
  943. }
  944. ProcFSInode::~ProcFSInode()
  945. {
  946. LOCKER(fs().m_inodes_lock);
  947. auto it = fs().m_inodes.find(index().value());
  948. if (it != fs().m_inodes.end() && it->value == this)
  949. fs().m_inodes.remove(it);
  950. }
  951. RefPtr<Process> ProcFSInode::process() const
  952. {
  953. return Process::from_pid(to_pid(identifier()));
  954. }
  955. KResult ProcFSInode::refresh_data(FileDescription& description) const
  956. {
  957. if (Kernel::is_directory(identifier()))
  958. return KSuccess;
  959. // For process-specific inodes, hold the process's ptrace lock across refresh
  960. // and refuse to load data if the process is not dumpable.
  961. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  962. auto process = this->process();
  963. if (process) {
  964. process->ptrace_lock().lock();
  965. if (!process->is_dumpable()) {
  966. process->ptrace_lock().unlock();
  967. return EPERM;
  968. }
  969. }
  970. ScopeGuard guard = [&] {
  971. if (process)
  972. process->ptrace_lock().unlock();
  973. };
  974. auto& cached_data = description.data();
  975. auto* directory_entry = fs().get_directory_entry(identifier());
  976. bool (*read_callback)(InodeIdentifier, KBufferBuilder&) = nullptr;
  977. if (directory_entry) {
  978. read_callback = directory_entry->read_callback;
  979. VERIFY(read_callback);
  980. } else {
  981. switch (to_proc_parent_directory(identifier())) {
  982. case PDI_PID_fd:
  983. read_callback = procfs$pid_fd_entry;
  984. break;
  985. case PDI_PID_stacks:
  986. read_callback = procfs$tid_stack;
  987. break;
  988. case PDI_Root_sys:
  989. switch (SysVariable::for_inode(identifier()).type) {
  990. case SysVariable::Type::Invalid:
  991. VERIFY_NOT_REACHED();
  992. case SysVariable::Type::Boolean:
  993. read_callback = read_sys_bool;
  994. break;
  995. case SysVariable::Type::String:
  996. read_callback = read_sys_string;
  997. break;
  998. }
  999. break;
  1000. default:
  1001. VERIFY_NOT_REACHED();
  1002. }
  1003. VERIFY(read_callback);
  1004. }
  1005. if (!cached_data)
  1006. cached_data = new ProcFSInodeData;
  1007. auto& buffer = static_cast<ProcFSInodeData&>(*cached_data).buffer;
  1008. if (buffer) {
  1009. // If we're reusing the buffer, reset the size to 0 first. This
  1010. // ensures we don't accidentally leak previously written data.
  1011. buffer->set_size(0);
  1012. }
  1013. KBufferBuilder builder(buffer, true);
  1014. if (!read_callback(identifier(), builder))
  1015. return ENOENT;
  1016. // We don't use builder.build() here, which would steal our buffer
  1017. // and turn it into an OwnPtr. Instead, just flush to the buffer so
  1018. // that we can read all the data that was written.
  1019. if (!builder.flush())
  1020. return ENOMEM;
  1021. if (!buffer)
  1022. return ENOMEM;
  1023. return KSuccess;
  1024. }
  1025. KResult ProcFSInode::attach(FileDescription& description)
  1026. {
  1027. return refresh_data(description);
  1028. }
  1029. void ProcFSInode::did_seek(FileDescription& description, off_t new_offset)
  1030. {
  1031. if (new_offset != 0)
  1032. return;
  1033. auto result = refresh_data(description);
  1034. if (result.is_error()) {
  1035. // Subsequent calls to read will return EIO!
  1036. dbgln("ProcFS: Could not refresh contents: {}", result.error());
  1037. }
  1038. }
  1039. InodeMetadata ProcFSInode::metadata() const
  1040. {
  1041. dbgln_if(PROCFS_DEBUG, "ProcFSInode::metadata({})", index());
  1042. InodeMetadata metadata;
  1043. metadata.inode = identifier();
  1044. metadata.ctime = mepoch;
  1045. metadata.atime = mepoch;
  1046. metadata.mtime = mepoch;
  1047. auto proc_parent_directory = to_proc_parent_directory(identifier());
  1048. auto proc_file_type = to_proc_file_type(identifier());
  1049. dbgln_if(PROCFS_DEBUG, " -> pid={}, fi={}, pdi={}", to_pid(identifier()).value(), (int)proc_file_type, (int)proc_parent_directory);
  1050. if (is_process_related_file(identifier())) {
  1051. ProcessID pid = to_pid(identifier());
  1052. auto process = Process::from_pid(pid);
  1053. if (process && process->is_dumpable()) {
  1054. metadata.uid = process->euid();
  1055. metadata.gid = process->egid();
  1056. } else {
  1057. metadata.uid = 0;
  1058. metadata.gid = 0;
  1059. }
  1060. } else if (is_thread_related_file(identifier())) {
  1061. ThreadID tid = to_tid(identifier());
  1062. auto thread = Thread::from_tid(tid);
  1063. if (thread && thread->process().is_dumpable()) {
  1064. metadata.uid = thread->process().euid();
  1065. metadata.gid = thread->process().egid();
  1066. } else {
  1067. metadata.uid = 0;
  1068. metadata.gid = 0;
  1069. }
  1070. }
  1071. if (proc_parent_directory == PDI_PID_fd) {
  1072. metadata.mode = S_IFLNK | S_IRUSR | S_IWUSR | S_IXUSR;
  1073. return metadata;
  1074. }
  1075. switch (proc_file_type) {
  1076. case FI_Root_self:
  1077. metadata.mode = S_IFLNK | S_IRUSR | S_IRGRP | S_IROTH;
  1078. break;
  1079. case FI_PID_cwd:
  1080. case FI_PID_exe:
  1081. case FI_PID_root:
  1082. metadata.mode = S_IFLNK | S_IRUSR;
  1083. break;
  1084. case FI_Root:
  1085. case FI_Root_sys:
  1086. case FI_Root_net:
  1087. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
  1088. break;
  1089. case FI_PID:
  1090. case FI_PID_fd:
  1091. case FI_PID_stacks:
  1092. metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR;
  1093. break;
  1094. case FI_Root_smbios_entry_point:
  1095. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1096. metadata.size = DMIExpose::the().entry_point_length();
  1097. break;
  1098. case FI_Root_dmi:
  1099. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1100. metadata.size = DMIExpose::the().structure_table_length();
  1101. break;
  1102. default:
  1103. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  1104. break;
  1105. }
  1106. if (proc_file_type > FI_Invalid && proc_file_type < FI_MaxStaticFileIndex) {
  1107. if (fs().m_entries[proc_file_type].supervisor_only) {
  1108. metadata.uid = 0;
  1109. metadata.gid = 0;
  1110. metadata.mode &= ~077;
  1111. }
  1112. }
  1113. return metadata;
  1114. }
  1115. ssize_t ProcFSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  1116. {
  1117. dbgln_if(PROCFS_DEBUG, "ProcFS: read_bytes offset: {} count: {}", offset, count);
  1118. VERIFY(offset >= 0);
  1119. VERIFY(buffer.user_or_kernel_ptr());
  1120. if (!description)
  1121. return -EIO;
  1122. if (!description->data()) {
  1123. dbgln_if(PROCFS_DEBUG, "ProcFS: Do not have cached data!");
  1124. return -EIO;
  1125. }
  1126. // Be sure to keep a reference to data_buffer while we use it!
  1127. RefPtr<KBufferImpl> data_buffer = static_cast<ProcFSInodeData&>(*description->data()).buffer;
  1128. if (!data_buffer || (size_t)offset >= data_buffer->size())
  1129. return 0;
  1130. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  1131. if (!buffer.write(data_buffer->data() + offset, nread))
  1132. return -EFAULT;
  1133. return nread;
  1134. }
  1135. InodeIdentifier ProcFS::ProcFSDirectoryEntry::identifier(unsigned fsid) const
  1136. {
  1137. return to_identifier(fsid, PDI_Root, 0, (ProcFileType)proc_file_type);
  1138. }
  1139. KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  1140. {
  1141. dbgln_if(PROCFS_DEBUG, "ProcFS: traverse_as_directory {}", index());
  1142. if (!Kernel::is_directory(identifier()))
  1143. return ENOTDIR;
  1144. auto proc_file_type = to_proc_file_type(identifier());
  1145. auto parent_id = to_parent_id(identifier());
  1146. callback({ ".", identifier(), 2 });
  1147. callback({ "..", parent_id, 2 });
  1148. switch (proc_file_type) {
  1149. case FI_Root:
  1150. for (auto& entry : fs().m_entries) {
  1151. // FIXME: strlen() here is sad.
  1152. if (!entry.name)
  1153. continue;
  1154. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End)
  1155. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type), 0 });
  1156. }
  1157. for (auto pid_child : Process::all_pids()) {
  1158. callback({ String::number(pid_child.value()), to_identifier(fsid(), PDI_Root, pid_child, FI_PID), 0 });
  1159. }
  1160. break;
  1161. case FI_Root_sys:
  1162. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1163. auto& variable = sys_variables()[i];
  1164. callback({ variable.name, sys_var_to_identifier(fsid(), i), 0 });
  1165. }
  1166. break;
  1167. case FI_Root_net:
  1168. callback({ "adapters", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_adapters), 0 });
  1169. callback({ "arp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_arp), 0 });
  1170. callback({ "tcp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_tcp), 0 });
  1171. callback({ "udp", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_udp), 0 });
  1172. callback({ "local", to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_local), 0 });
  1173. break;
  1174. case FI_PID: {
  1175. auto pid = to_pid(identifier());
  1176. auto process = Process::from_pid(pid);
  1177. if (!process)
  1178. return ENOENT;
  1179. for (auto& entry : fs().m_entries) {
  1180. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1181. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1182. continue;
  1183. // FIXME: strlen() here is sad.
  1184. callback({ { entry.name, strlen(entry.name) }, to_identifier(fsid(), PDI_PID, pid, (ProcFileType)entry.proc_file_type), 0 });
  1185. }
  1186. }
  1187. } break;
  1188. case FI_PID_fd: {
  1189. auto pid = to_pid(identifier());
  1190. auto process = Process::from_pid(pid);
  1191. if (!process)
  1192. return ENOENT;
  1193. for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
  1194. auto description = process->file_description(i);
  1195. if (!description)
  1196. continue;
  1197. callback({ String::number(i), to_identifier_with_fd(fsid(), pid, i), 0 });
  1198. }
  1199. } break;
  1200. case FI_PID_stacks: {
  1201. auto pid = to_pid(identifier());
  1202. auto process = Process::from_pid(pid);
  1203. if (!process)
  1204. return ENOENT;
  1205. process->for_each_thread([&](const Thread& thread) -> IterationDecision {
  1206. int tid = thread.tid().value();
  1207. callback({ String::number(tid), to_identifier_with_stack(fsid(), tid), 0 });
  1208. return IterationDecision::Continue;
  1209. });
  1210. } break;
  1211. default:
  1212. return KSuccess;
  1213. }
  1214. return KSuccess;
  1215. }
  1216. RefPtr<Inode> ProcFSInode::lookup(StringView name)
  1217. {
  1218. VERIFY(is_directory());
  1219. if (name == ".")
  1220. return this;
  1221. if (name == "..")
  1222. return fs().get_inode(to_parent_id(identifier()));
  1223. auto proc_file_type = to_proc_file_type(identifier());
  1224. if (proc_file_type == FI_Root) {
  1225. for (auto& entry : fs().m_entries) {
  1226. if (entry.name == nullptr)
  1227. continue;
  1228. if (entry.proc_file_type > __FI_Root_Start && entry.proc_file_type < __FI_Root_End) {
  1229. if (name == entry.name) {
  1230. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, (ProcFileType)entry.proc_file_type));
  1231. }
  1232. }
  1233. }
  1234. auto name_as_number = name.to_uint();
  1235. if (!name_as_number.has_value())
  1236. return {};
  1237. bool process_exists = false;
  1238. {
  1239. InterruptDisabler disabler;
  1240. process_exists = Process::from_pid(name_as_number.value());
  1241. }
  1242. if (process_exists)
  1243. return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number.value(), FI_PID));
  1244. return {};
  1245. }
  1246. if (proc_file_type == FI_Root_sys) {
  1247. for (size_t i = 1; i < sys_variables().size(); ++i) {
  1248. auto& variable = sys_variables()[i];
  1249. if (name == variable.name)
  1250. return fs().get_inode(sys_var_to_identifier(fsid(), i));
  1251. }
  1252. return {};
  1253. }
  1254. if (proc_file_type == FI_Root_net) {
  1255. if (name == "adapters")
  1256. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_adapters));
  1257. if (name == "arp")
  1258. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_arp));
  1259. if (name == "tcp")
  1260. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_tcp));
  1261. if (name == "udp")
  1262. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_udp));
  1263. if (name == "local")
  1264. return fs().get_inode(to_identifier(fsid(), PDI_Root, 0, FI_Root_net_local));
  1265. return {};
  1266. }
  1267. if (proc_file_type == FI_PID) {
  1268. auto process = Process::from_pid(to_pid(identifier()));
  1269. if (!process)
  1270. return {};
  1271. for (auto& entry : fs().m_entries) {
  1272. if (entry.proc_file_type > __FI_PID_Start && entry.proc_file_type < __FI_PID_End) {
  1273. if (entry.proc_file_type == FI_PID_exe && !process->executable())
  1274. continue;
  1275. if (entry.name == nullptr)
  1276. continue;
  1277. if (name == entry.name) {
  1278. return fs().get_inode(to_identifier(fsid(), PDI_PID, to_pid(identifier()), (ProcFileType)entry.proc_file_type));
  1279. }
  1280. }
  1281. }
  1282. return {};
  1283. }
  1284. if (proc_file_type == FI_PID_fd) {
  1285. auto name_as_number = name.to_uint();
  1286. if (!name_as_number.has_value())
  1287. return {};
  1288. bool fd_exists = false;
  1289. {
  1290. if (auto process = Process::from_pid(to_pid(identifier())))
  1291. fd_exists = process->file_description(name_as_number.value());
  1292. }
  1293. if (fd_exists)
  1294. return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number.value()));
  1295. }
  1296. if (proc_file_type == FI_PID_stacks) {
  1297. auto name_as_number = name.to_int();
  1298. if (!name_as_number.has_value())
  1299. return {};
  1300. int tid = name_as_number.value();
  1301. if (tid <= 0) {
  1302. return {};
  1303. }
  1304. bool thread_exists = false;
  1305. {
  1306. auto process = Process::from_pid(to_pid(identifier()));
  1307. auto thread = Thread::from_tid(tid);
  1308. thread_exists = process && thread && process->pid() == thread->pid();
  1309. }
  1310. if (thread_exists)
  1311. return fs().get_inode(to_identifier_with_stack(fsid(), tid));
  1312. }
  1313. return {};
  1314. }
  1315. void ProcFSInode::flush_metadata()
  1316. {
  1317. }
  1318. ssize_t ProcFSInode::write_bytes(off_t offset, ssize_t size, const UserOrKernelBuffer& buffer, FileDescription*)
  1319. {
  1320. // For process-specific inodes, hold the process's ptrace lock across the write
  1321. // and refuse to write at all data if the process is not dumpable.
  1322. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  1323. auto process = this->process();
  1324. if (process) {
  1325. process->ptrace_lock().lock();
  1326. if (!process->is_dumpable()) {
  1327. process->ptrace_lock().unlock();
  1328. return EPERM;
  1329. }
  1330. }
  1331. ScopeGuard guard = [&] {
  1332. if (process)
  1333. process->ptrace_lock().unlock();
  1334. };
  1335. auto result = prepare_to_write_data();
  1336. if (result.is_error())
  1337. return result;
  1338. auto* directory_entry = fs().get_directory_entry(identifier());
  1339. ssize_t (*write_callback)(InodeIdentifier, const UserOrKernelBuffer&, size_t) = nullptr;
  1340. if (directory_entry == nullptr) {
  1341. if (to_proc_parent_directory(identifier()) == PDI_Root_sys) {
  1342. switch (SysVariable::for_inode(identifier()).type) {
  1343. case SysVariable::Type::Invalid:
  1344. VERIFY_NOT_REACHED();
  1345. case SysVariable::Type::Boolean:
  1346. write_callback = write_sys_bool;
  1347. break;
  1348. case SysVariable::Type::String:
  1349. write_callback = write_sys_string;
  1350. break;
  1351. }
  1352. } else
  1353. return -EPERM;
  1354. } else {
  1355. if (!directory_entry->write_callback)
  1356. return -EPERM;
  1357. write_callback = directory_entry->write_callback;
  1358. }
  1359. VERIFY(is_persistent_inode(identifier()));
  1360. // FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
  1361. VERIFY(offset == 0);
  1362. ssize_t nwritten = write_callback(identifier(), buffer, (size_t)size);
  1363. if (nwritten < 0)
  1364. dbgln("ProcFS: Writing {} bytes failed: {}", size, nwritten);
  1365. return nwritten;
  1366. }
  1367. KResultOr<NonnullRefPtr<Custody>> ProcFSInode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  1368. {
  1369. if (FI_Root_self == to_proc_file_type(identifier())) {
  1370. return VFS::the().resolve_path(String::number(Process::current()->pid().value()), base, out_parent, options, symlink_recursion_level);
  1371. }
  1372. // The only other links are in pid directories, so it's safe to ignore
  1373. // unrelated files and the thread-specific stacks/ directory.
  1374. if (!is_process_related_file(identifier()))
  1375. return Inode::resolve_as_link(base, out_parent, options, symlink_recursion_level);
  1376. // FIXME: We should return a custody for FI_PID or FI_PID_fd here
  1377. // for correctness. It's impossible to create files in ProcFS,
  1378. // so returning null shouldn't break much.
  1379. if (out_parent)
  1380. *out_parent = nullptr;
  1381. auto pid = to_pid(identifier());
  1382. auto proc_file_type = to_proc_file_type(identifier());
  1383. auto process = Process::from_pid(pid);
  1384. if (!process)
  1385. return ENOENT;
  1386. if (to_proc_parent_directory(identifier()) == PDI_PID_fd) {
  1387. if (out_parent)
  1388. *out_parent = base;
  1389. int fd = to_fd(identifier());
  1390. auto description = process->file_description(fd);
  1391. if (!description)
  1392. return ENOENT;
  1393. auto proxy_inode = ProcFSProxyInode::create(const_cast<ProcFS&>(fs()), *description);
  1394. return Custody::create(&base, "", proxy_inode, base.mount_flags());
  1395. }
  1396. Custody* res = nullptr;
  1397. switch (proc_file_type) {
  1398. case FI_PID_cwd:
  1399. res = &process->current_directory();
  1400. break;
  1401. case FI_PID_exe:
  1402. res = process->executable();
  1403. break;
  1404. case FI_PID_root:
  1405. // Note: we open root_directory() here, not
  1406. // root_directory_relative_to_global_root().
  1407. // This seems more useful.
  1408. res = &process->root_directory();
  1409. break;
  1410. default:
  1411. VERIFY_NOT_REACHED();
  1412. }
  1413. if (!res)
  1414. return ENOENT;
  1415. return *res;
  1416. }
  1417. ProcFSProxyInode::ProcFSProxyInode(ProcFS& fs, FileDescription& fd)
  1418. : Inode(fs, 0)
  1419. , m_fd(fd)
  1420. {
  1421. }
  1422. ProcFSProxyInode::~ProcFSProxyInode()
  1423. {
  1424. }
  1425. KResult ProcFSProxyInode::attach(FileDescription& fd)
  1426. {
  1427. return m_fd->inode()->attach(fd);
  1428. }
  1429. void ProcFSProxyInode::did_seek(FileDescription& fd, off_t new_offset)
  1430. {
  1431. return m_fd->inode()->did_seek(fd, new_offset);
  1432. }
  1433. InodeMetadata ProcFSProxyInode::metadata() const
  1434. {
  1435. InodeMetadata metadata = m_fd->metadata();
  1436. if (m_fd->is_readable())
  1437. metadata.mode |= 0444;
  1438. else
  1439. metadata.mode &= ~0444;
  1440. if (m_fd->is_writable())
  1441. metadata.mode |= 0222;
  1442. else
  1443. metadata.mode &= ~0222;
  1444. if (!metadata.is_directory())
  1445. metadata.mode &= ~0111;
  1446. return metadata;
  1447. }
  1448. KResultOr<NonnullRefPtr<Inode>> ProcFSProxyInode::create_child(const String& name, mode_t mode, dev_t dev, uid_t uid, gid_t gid)
  1449. {
  1450. if (!m_fd->inode())
  1451. return EINVAL;
  1452. return m_fd->inode()->create_child(name, mode, dev, uid, gid);
  1453. }
  1454. KResult ProcFSProxyInode::add_child(Inode& child, const StringView& name, mode_t mode)
  1455. {
  1456. if (!m_fd->inode())
  1457. return EINVAL;
  1458. return m_fd->inode()->add_child(child, name, mode);
  1459. }
  1460. KResult ProcFSProxyInode::remove_child(const StringView& name)
  1461. {
  1462. if (!m_fd->inode())
  1463. return EINVAL;
  1464. return m_fd->inode()->remove_child(name);
  1465. }
  1466. RefPtr<Inode> ProcFSProxyInode::lookup(StringView name)
  1467. {
  1468. if (!m_fd->inode())
  1469. return {};
  1470. return m_fd->inode()->lookup(name);
  1471. }
  1472. KResultOr<size_t> ProcFSProxyInode::directory_entry_count() const
  1473. {
  1474. if (!m_fd->inode())
  1475. return EINVAL;
  1476. return m_fd->inode()->directory_entry_count();
  1477. }
  1478. KResultOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  1479. {
  1480. return EPERM;
  1481. }
  1482. KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t)
  1483. {
  1484. return EPERM;
  1485. }
  1486. KResult ProcFSInode::remove_child([[maybe_unused]] const StringView& name)
  1487. {
  1488. return EPERM;
  1489. }
  1490. KResultOr<size_t> ProcFSInode::directory_entry_count() const
  1491. {
  1492. VERIFY(is_directory());
  1493. size_t count = 0;
  1494. KResult result = traverse_as_directory([&count](auto&) {
  1495. ++count;
  1496. return true;
  1497. });
  1498. if (result.is_error())
  1499. return result;
  1500. return count;
  1501. }
  1502. KResult ProcFSInode::chmod(mode_t)
  1503. {
  1504. return EPERM;
  1505. }
  1506. ProcFS::ProcFS()
  1507. {
  1508. m_root_inode = adopt(*new ProcFSInode(*this, 1));
  1509. m_entries.resize(FI_MaxStaticFileIndex);
  1510. m_entries[FI_Root_df] = { "df", FI_Root_df, false, procfs$df };
  1511. m_entries[FI_Root_all] = { "all", FI_Root_all, false, procfs$all };
  1512. m_entries[FI_Root_memstat] = { "memstat", FI_Root_memstat, false, procfs$memstat };
  1513. m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, false, procfs$cpuinfo };
  1514. m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, true, procfs$dmesg };
  1515. m_entries[FI_Root_self] = { "self", FI_Root_self, false, procfs$self };
  1516. m_entries[FI_Root_pci] = { "pci", FI_Root_pci, false, procfs$pci };
  1517. m_entries[FI_Root_interrupts] = { "interrupts", FI_Root_interrupts, false, procfs$interrupts };
  1518. m_entries[FI_Root_dmi] = { "DMI", FI_Root_dmi, false, procfs$dmi };
  1519. m_entries[FI_Root_smbios_entry_point] = { "smbios_entry_point", FI_Root_smbios_entry_point, false, procfs$smbios_entry_point };
  1520. m_entries[FI_Root_keymap] = { "keymap", FI_Root_keymap, false, procfs$keymap };
  1521. m_entries[FI_Root_devices] = { "devices", FI_Root_devices, false, procfs$devices };
  1522. m_entries[FI_Root_uptime] = { "uptime", FI_Root_uptime, false, procfs$uptime };
  1523. m_entries[FI_Root_cmdline] = { "cmdline", FI_Root_cmdline, true, procfs$cmdline };
  1524. m_entries[FI_Root_modules] = { "modules", FI_Root_modules, true, procfs$modules };
  1525. m_entries[FI_Root_profile] = { "profile", FI_Root_profile, true, procfs$profile };
  1526. m_entries[FI_Root_sys] = { "sys", FI_Root_sys, true };
  1527. m_entries[FI_Root_net] = { "net", FI_Root_net, false };
  1528. m_entries[FI_Root_net_adapters] = { "adapters", FI_Root_net_adapters, false, procfs$net_adapters };
  1529. m_entries[FI_Root_net_arp] = { "arp", FI_Root_net_arp, true, procfs$net_arp };
  1530. m_entries[FI_Root_net_tcp] = { "tcp", FI_Root_net_tcp, false, procfs$net_tcp };
  1531. m_entries[FI_Root_net_udp] = { "udp", FI_Root_net_udp, false, procfs$net_udp };
  1532. m_entries[FI_Root_net_local] = { "local", FI_Root_net_local, false, procfs$net_local };
  1533. m_entries[FI_PID_vm] = { "vm", FI_PID_vm, false, procfs$pid_vm };
  1534. m_entries[FI_PID_stacks] = { "stacks", FI_PID_stacks, false };
  1535. m_entries[FI_PID_fds] = { "fds", FI_PID_fds, false, procfs$pid_fds };
  1536. m_entries[FI_PID_exe] = { "exe", FI_PID_exe, false, procfs$pid_exe };
  1537. m_entries[FI_PID_cwd] = { "cwd", FI_PID_cwd, false, procfs$pid_cwd };
  1538. m_entries[FI_PID_unveil] = { "unveil", FI_PID_unveil, false, procfs$pid_unveil };
  1539. m_entries[FI_PID_root] = { "root", FI_PID_root, false, procfs$pid_root };
  1540. m_entries[FI_PID_perf_events] = { "perf_events", FI_PID_perf_events, false, procfs$pid_perf_events };
  1541. m_entries[FI_PID_fd] = { "fd", FI_PID_fd, false };
  1542. }
  1543. ProcFS::ProcFSDirectoryEntry* ProcFS::get_directory_entry(InodeIdentifier identifier) const
  1544. {
  1545. auto proc_file_type = to_proc_file_type(identifier);
  1546. if (proc_file_type != FI_Invalid && proc_file_type != FI_Root_sys_variable && proc_file_type < FI_MaxStaticFileIndex)
  1547. return const_cast<ProcFSDirectoryEntry*>(&m_entries[proc_file_type]);
  1548. return nullptr;
  1549. }
  1550. KResult ProcFSInode::chown(uid_t, gid_t)
  1551. {
  1552. return EPERM;
  1553. }
  1554. }