ProcFS.cpp 61 KB

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