Process.cpp 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636
  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/Demangle.h>
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/Time.h>
  32. #include <AK/Types.h>
  33. #include <Kernel/Arch/i386/CPU.h>
  34. #include <Kernel/Arch/i386/PIT.h>
  35. #include <Kernel/Console.h>
  36. #include <Kernel/Devices/KeyboardDevice.h>
  37. #include <Kernel/Devices/NullDevice.h>
  38. #include <Kernel/Devices/PCSpeaker.h>
  39. #include <Kernel/Devices/RandomDevice.h>
  40. #include <Kernel/FileSystem/Custody.h>
  41. #include <Kernel/FileSystem/DevPtsFS.h>
  42. #include <Kernel/FileSystem/Ext2FileSystem.h>
  43. #include <Kernel/FileSystem/FIFO.h>
  44. #include <Kernel/FileSystem/FileDescription.h>
  45. #include <Kernel/FileSystem/InodeWatcher.h>
  46. #include <Kernel/FileSystem/ProcFS.h>
  47. #include <Kernel/FileSystem/TmpFS.h>
  48. #include <Kernel/FileSystem/VirtualFileSystem.h>
  49. #include <Kernel/Heap/kmalloc.h>
  50. #include <Kernel/IO.h>
  51. #include <Kernel/KBufferBuilder.h>
  52. #include <Kernel/KSyms.h>
  53. #include <Kernel/KernelInfoPage.h>
  54. #include <Kernel/Module.h>
  55. #include <Kernel/Multiboot.h>
  56. #include <Kernel/Net/Socket.h>
  57. #include <Kernel/Process.h>
  58. #include <Kernel/ProcessTracer.h>
  59. #include <Kernel/Profiling.h>
  60. #include <Kernel/RTC.h>
  61. #include <Kernel/Random.h>
  62. #include <Kernel/Scheduler.h>
  63. #include <Kernel/SharedBuffer.h>
  64. #include <Kernel/StdLib.h>
  65. #include <Kernel/Syscall.h>
  66. #include <Kernel/TTY/MasterPTY.h>
  67. #include <Kernel/Thread.h>
  68. #include <Kernel/VM/InodeVMObject.h>
  69. #include <Kernel/VM/PurgeableVMObject.h>
  70. #include <LibC/errno_numbers.h>
  71. #include <LibC/limits.h>
  72. #include <LibC/signal_numbers.h>
  73. #include <LibELF/ELFLoader.h>
  74. //#define DEBUG_POLL_SELECT
  75. //#define DEBUG_IO
  76. //#define TASK_DEBUG
  77. //#define FORK_DEBUG
  78. //#define EXEC_DEBUG
  79. //#define SIGNAL_DEBUG
  80. //#define SHARED_BUFFER_DEBUG
  81. static void create_signal_trampolines();
  82. static void create_kernel_info_page();
  83. static pid_t next_pid;
  84. InlineLinkedList<Process>* g_processes;
  85. static String* s_hostname;
  86. static Lock* s_hostname_lock;
  87. static VirtualAddress s_info_page_address_for_userspace;
  88. static VirtualAddress s_info_page_address_for_kernel;
  89. VirtualAddress g_return_to_ring3_from_signal_trampoline;
  90. HashMap<String, OwnPtr<Module>>* g_modules;
  91. pid_t Process::allocate_pid()
  92. {
  93. InterruptDisabler disabler;
  94. return next_pid++;
  95. }
  96. void Process::initialize()
  97. {
  98. g_modules = new HashMap<String, OwnPtr<Module>>;
  99. next_pid = 0;
  100. g_processes = new InlineLinkedList<Process>;
  101. s_hostname = new String("courage");
  102. s_hostname_lock = new Lock;
  103. create_signal_trampolines();
  104. create_kernel_info_page();
  105. }
  106. void Process::update_info_page_timestamp(const timeval& tv)
  107. {
  108. auto* info_page = (KernelInfoPage*)s_info_page_address_for_kernel.as_ptr();
  109. info_page->serial++;
  110. const_cast<timeval&>(info_page->now) = tv;
  111. }
  112. Vector<pid_t> Process::all_pids()
  113. {
  114. Vector<pid_t> pids;
  115. InterruptDisabler disabler;
  116. pids.ensure_capacity((int)g_processes->size_slow());
  117. for (auto& process : *g_processes)
  118. pids.append(process.pid());
  119. return pids;
  120. }
  121. Vector<Process*> Process::all_processes()
  122. {
  123. Vector<Process*> processes;
  124. InterruptDisabler disabler;
  125. processes.ensure_capacity((int)g_processes->size_slow());
  126. for (auto& process : *g_processes)
  127. processes.append(&process);
  128. return processes;
  129. }
  130. bool Process::in_group(gid_t gid) const
  131. {
  132. return m_gid == gid || m_extra_gids.contains(gid);
  133. }
  134. Range Process::allocate_range(VirtualAddress vaddr, size_t size)
  135. {
  136. vaddr.mask(PAGE_MASK);
  137. size = PAGE_ROUND_UP(size);
  138. if (vaddr.is_null())
  139. return page_directory().range_allocator().allocate_anywhere(size);
  140. return page_directory().range_allocator().allocate_specific(vaddr, size);
  141. }
  142. static unsigned prot_to_region_access_flags(int prot)
  143. {
  144. unsigned access = 0;
  145. if (prot & PROT_READ)
  146. access |= Region::Access::Read;
  147. if (prot & PROT_WRITE)
  148. access |= Region::Access::Write;
  149. if (prot & PROT_EXEC)
  150. access |= Region::Access::Execute;
  151. return access;
  152. }
  153. Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
  154. {
  155. auto& region = add_region(Region::create_user_accessible(range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
  156. size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
  157. for (size_t i = 0; i < region.page_count(); ++i) {
  158. if (source_region.should_cow(page_offset_in_source_region + i))
  159. region.set_should_cow(i, true);
  160. }
  161. return region;
  162. }
  163. Region* Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, bool commit)
  164. {
  165. auto range = allocate_range(vaddr, size);
  166. if (!range.is_valid())
  167. return nullptr;
  168. auto& region = add_region(Region::create_user_accessible(range, name, prot_to_region_access_flags(prot)));
  169. region.map(page_directory());
  170. if (commit)
  171. region.commit();
  172. return &region;
  173. }
  174. Region* Process::allocate_file_backed_region(VirtualAddress vaddr, size_t size, NonnullRefPtr<Inode> inode, const String& name, int prot)
  175. {
  176. auto range = allocate_range(vaddr, size);
  177. if (!range.is_valid())
  178. return nullptr;
  179. auto& region = add_region(Region::create_user_accessible(range, inode, name, prot_to_region_access_flags(prot)));
  180. region.map(page_directory());
  181. return &region;
  182. }
  183. Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot, bool user_accessible)
  184. {
  185. size_t end_in_vmobject = offset_in_vmobject + size;
  186. if (end_in_vmobject < offset_in_vmobject) {
  187. dbgprintf("allocate_region_with_vmobject: Overflow (offset + size)\n");
  188. return nullptr;
  189. }
  190. if (offset_in_vmobject >= vmobject->size()) {
  191. dbgprintf("allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.\n");
  192. return nullptr;
  193. }
  194. if (end_in_vmobject > vmobject->size()) {
  195. dbgprintf("allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.\n");
  196. return nullptr;
  197. }
  198. auto range = allocate_range(vaddr, size);
  199. if (!range.is_valid())
  200. return nullptr;
  201. offset_in_vmobject &= PAGE_MASK;
  202. Region* region;
  203. if (user_accessible)
  204. region = &add_region(Region::create_user_accessible(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  205. else
  206. region = &add_region(Region::create_kernel_only(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  207. region->map(page_directory());
  208. return region;
  209. }
  210. bool Process::deallocate_region(Region& region)
  211. {
  212. InterruptDisabler disabler;
  213. if (m_region_lookup_cache.region == &region)
  214. m_region_lookup_cache.region = nullptr;
  215. for (int i = 0; i < m_regions.size(); ++i) {
  216. if (&m_regions[i] == &region) {
  217. m_regions.remove(i);
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. Region* Process::region_from_range(const Range& range)
  224. {
  225. if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
  226. return m_region_lookup_cache.region;
  227. size_t size = PAGE_ROUND_UP(range.size());
  228. for (auto& region : m_regions) {
  229. if (region.vaddr() == range.base() && region.size() == size) {
  230. m_region_lookup_cache.range = range;
  231. m_region_lookup_cache.region = &region;
  232. return &region;
  233. }
  234. }
  235. return nullptr;
  236. }
  237. Region* Process::region_containing(const Range& range)
  238. {
  239. for (auto& region : m_regions) {
  240. if (region.contains(range))
  241. return &region;
  242. }
  243. return nullptr;
  244. }
  245. int Process::sys$set_mmap_name(const Syscall::SC_set_mmap_name_params* user_params)
  246. {
  247. REQUIRE_PROMISE(stdio);
  248. Syscall::SC_set_mmap_name_params params;
  249. if (!validate_read_and_copy_typed(&params, user_params))
  250. return -EFAULT;
  251. if (params.name.length > PATH_MAX)
  252. return -ENAMETOOLONG;
  253. auto name = validate_and_copy_string_from_user(params.name);
  254. if (name.is_null())
  255. return -EFAULT;
  256. auto* region = region_from_range({ VirtualAddress(params.addr), params.size });
  257. if (!region)
  258. return -EINVAL;
  259. if (!region->is_mmap())
  260. return -EPERM;
  261. region->set_name(name);
  262. return 0;
  263. }
  264. static bool validate_mmap_prot(int prot, bool map_stack)
  265. {
  266. bool readable = prot & PROT_READ;
  267. bool writable = prot & PROT_WRITE;
  268. bool executable = prot & PROT_EXEC;
  269. if (writable && executable)
  270. return false;
  271. if (map_stack) {
  272. if (executable)
  273. return false;
  274. if (!readable || !writable)
  275. return false;
  276. }
  277. return true;
  278. }
  279. static bool validate_inode_mmap_prot(const Process& process, int prot, const Inode& inode)
  280. {
  281. auto metadata = inode.metadata();
  282. if ((prot & PROT_WRITE) && !metadata.may_write(process))
  283. return false;
  284. if ((prot & PROT_READ) && !metadata.may_read(process))
  285. return false;
  286. InterruptDisabler disabler;
  287. if (inode.vmobject()) {
  288. if ((prot & PROT_EXEC) && inode.vmobject()->writable_mappings())
  289. return false;
  290. if ((prot & PROT_WRITE) && inode.vmobject()->executable_mappings())
  291. return false;
  292. }
  293. return true;
  294. }
  295. // Carve out a virtual address range from a region and return the two regions on either side
  296. Vector<Region*, 2> Process::split_region_around_range(const Region& source_region, const Range& desired_range)
  297. {
  298. Range old_region_range = source_region.range();
  299. auto remaining_ranges_after_unmap = old_region_range.carve(desired_range);
  300. ASSERT(!remaining_ranges_after_unmap.is_empty());
  301. auto make_replacement_region = [&](const Range& new_range) -> Region& {
  302. ASSERT(new_range.base() >= old_region_range.base());
  303. ASSERT(new_range.end() <= old_region_range.end());
  304. size_t new_range_offset_in_vmobject = source_region.offset_in_vmobject() + (new_range.base().get() - old_region_range.base().get());
  305. return allocate_split_region(source_region, new_range, new_range_offset_in_vmobject);
  306. };
  307. Vector<Region*, 2> new_regions;
  308. for (auto& new_range : remaining_ranges_after_unmap) {
  309. new_regions.unchecked_append(&make_replacement_region(new_range));
  310. }
  311. return new_regions;
  312. }
  313. void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params)
  314. {
  315. REQUIRE_PROMISE(stdio);
  316. Syscall::SC_mmap_params params;
  317. if (!validate_read_and_copy_typed(&params, user_params))
  318. return (void*)-EFAULT;
  319. void* addr = (void*)params.addr;
  320. size_t size = params.size;
  321. int prot = params.prot;
  322. int flags = params.flags;
  323. int fd = params.fd;
  324. int offset = params.offset;
  325. String name;
  326. if (params.name.characters) {
  327. if (params.name.length > PATH_MAX)
  328. return (void*)-ENAMETOOLONG;
  329. name = validate_and_copy_string_from_user(params.name);
  330. if (name.is_null())
  331. return (void*)-EFAULT;
  332. }
  333. if (size == 0)
  334. return (void*)-EINVAL;
  335. if ((uintptr_t)addr & ~PAGE_MASK)
  336. return (void*)-EINVAL;
  337. bool map_shared = flags & MAP_SHARED;
  338. bool map_anonymous = flags & MAP_ANONYMOUS;
  339. bool map_purgeable = flags & MAP_PURGEABLE;
  340. bool map_private = flags & MAP_PRIVATE;
  341. bool map_stack = flags & MAP_STACK;
  342. bool map_fixed = flags & MAP_FIXED;
  343. if (map_shared && map_private)
  344. return (void*)-EINVAL;
  345. if (!map_shared && !map_private)
  346. return (void*)-EINVAL;
  347. if (!validate_mmap_prot(prot, map_stack))
  348. return (void*)-EINVAL;
  349. if (map_stack && (!map_private || !map_anonymous))
  350. return (void*)-EINVAL;
  351. Region* region = nullptr;
  352. if (map_purgeable) {
  353. auto vmobject = PurgeableVMObject::create_with_size(size);
  354. region = allocate_region_with_vmobject(VirtualAddress(addr), size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  355. if (!region && (!map_fixed && addr != 0))
  356. region = allocate_region_with_vmobject({}, size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  357. } else if (map_anonymous) {
  358. region = allocate_region(VirtualAddress(addr), size, !name.is_null() ? name : "mmap", prot, false);
  359. if (!region && (!map_fixed && addr != 0))
  360. region = allocate_region({}, size, !name.is_null() ? name : "mmap", prot, false);
  361. } else {
  362. if (offset < 0)
  363. return (void*)-EINVAL;
  364. if (static_cast<size_t>(offset) & ~PAGE_MASK)
  365. return (void*)-EINVAL;
  366. // FIXME: Implement MAP_PRIVATE for FileDescription-backed mmap
  367. if (map_private)
  368. return (void*)-ENOTSUP;
  369. auto description = file_description(fd);
  370. if (!description)
  371. return (void*)-EBADF;
  372. if (description->is_directory())
  373. return (void*)-ENODEV;
  374. if ((prot & PROT_READ) && !description->is_readable())
  375. return (void*)-EACCES;
  376. if ((prot & PROT_WRITE) && !description->is_writable())
  377. return (void*)-EACCES;
  378. if (description->inode()) {
  379. if (!validate_inode_mmap_prot(*this, prot, *description->inode()))
  380. return (void*)-EACCES;
  381. }
  382. auto region_or_error = description->mmap(*this, VirtualAddress(addr), static_cast<size_t>(offset), size, prot);
  383. if (region_or_error.is_error()) {
  384. // Fail if MAP_FIXED or address is 0, retry otherwise
  385. if (map_fixed || addr == 0)
  386. return (void*)(int)region_or_error.error();
  387. region_or_error = description->mmap(*this, {}, static_cast<size_t>(offset), size, prot);
  388. }
  389. if (region_or_error.is_error())
  390. return (void*)(int)region_or_error.error();
  391. region = region_or_error.value();
  392. }
  393. if (!region)
  394. return (void*)-ENOMEM;
  395. region->set_mmap(true);
  396. if (map_shared)
  397. region->set_shared(true);
  398. if (map_stack)
  399. region->set_stack(true);
  400. if (!name.is_null())
  401. region->set_name(name);
  402. return region->vaddr().as_ptr();
  403. }
  404. int Process::sys$munmap(void* addr, size_t size)
  405. {
  406. REQUIRE_PROMISE(stdio);
  407. Range range_to_unmap { VirtualAddress(addr), size };
  408. if (auto* whole_region = region_from_range(range_to_unmap)) {
  409. if (!whole_region->is_mmap())
  410. return -EPERM;
  411. bool success = deallocate_region(*whole_region);
  412. ASSERT(success);
  413. return 0;
  414. }
  415. if (auto* old_region = region_containing(range_to_unmap)) {
  416. if (!old_region->is_mmap())
  417. return -EPERM;
  418. auto new_regions = split_region_around_range(*old_region, range_to_unmap);
  419. // We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
  420. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  421. deallocate_region(*old_region);
  422. // Instead we give back the unwanted VM manually.
  423. page_directory().range_allocator().deallocate(range_to_unmap);
  424. // And finally we map the new region(s) using our page directory (they were just allocated and don't have one).
  425. for (auto* new_region : new_regions) {
  426. new_region->map(page_directory());
  427. }
  428. return 0;
  429. }
  430. // FIXME: We should also support munmap() across multiple regions. (#175)
  431. return -EINVAL;
  432. }
  433. int Process::sys$mprotect(void* addr, size_t size, int prot)
  434. {
  435. REQUIRE_PROMISE(stdio);
  436. Range range_to_mprotect = { VirtualAddress(addr), size };
  437. if (auto* whole_region = region_from_range(range_to_mprotect)) {
  438. if (!whole_region->is_mmap())
  439. return -EPERM;
  440. if (!validate_mmap_prot(prot, whole_region->is_stack()))
  441. return -EINVAL;
  442. if (whole_region->access() == prot_to_region_access_flags(prot))
  443. return 0;
  444. if (whole_region->vmobject().is_inode()
  445. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(whole_region->vmobject()).inode())) {
  446. return -EACCES;
  447. }
  448. whole_region->set_readable(prot & PROT_READ);
  449. whole_region->set_writable(prot & PROT_WRITE);
  450. whole_region->set_executable(prot & PROT_EXEC);
  451. whole_region->remap();
  452. return 0;
  453. }
  454. // Check if we can carve out the desired range from an existing region
  455. if (auto* old_region = region_containing(range_to_mprotect)) {
  456. if (!old_region->is_mmap())
  457. return -EPERM;
  458. if (!validate_mmap_prot(prot, old_region->is_stack()))
  459. return -EINVAL;
  460. if (old_region->access() == prot_to_region_access_flags(prot))
  461. return 0;
  462. if (old_region->vmobject().is_inode()
  463. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(old_region->vmobject()).inode())) {
  464. return -EACCES;
  465. }
  466. // This vector is the region(s) adjacent to our range.
  467. // We need to allocate a new region for the range we wanted to change permission bits on.
  468. auto adjacent_regions = split_region_around_range(*old_region, range_to_mprotect);
  469. size_t new_range_offset_in_vmobject = old_region->offset_in_vmobject() + (range_to_mprotect.base().get() - old_region->range().base().get());
  470. auto& new_region = allocate_split_region(*old_region, range_to_mprotect, new_range_offset_in_vmobject);
  471. new_region.set_readable(prot & PROT_READ);
  472. new_region.set_writable(prot & PROT_WRITE);
  473. new_region.set_executable(prot & PROT_EXEC);
  474. // Unmap the old region here, specifying that we *don't* want the VM deallocated.
  475. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  476. deallocate_region(*old_region);
  477. // Map the new regions using our page directory (they were just allocated and don't have one).
  478. for (auto* adjacent_region : adjacent_regions) {
  479. adjacent_region->map(page_directory());
  480. }
  481. new_region.map(page_directory());
  482. return 0;
  483. }
  484. // FIXME: We should also support mprotect() across multiple regions. (#175) (#964)
  485. return -EINVAL;
  486. }
  487. int Process::sys$madvise(void* address, size_t size, int advice)
  488. {
  489. REQUIRE_PROMISE(stdio);
  490. auto* region = region_from_range({ VirtualAddress(address), size });
  491. if (!region)
  492. return -EINVAL;
  493. if (!region->is_mmap())
  494. return -EPERM;
  495. if ((advice & MADV_SET_VOLATILE) && (advice & MADV_SET_NONVOLATILE))
  496. return -EINVAL;
  497. if (advice & MADV_SET_VOLATILE) {
  498. if (!region->vmobject().is_purgeable())
  499. return -EPERM;
  500. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  501. vmobject.set_volatile(true);
  502. return 0;
  503. }
  504. if (advice & MADV_SET_NONVOLATILE) {
  505. if (!region->vmobject().is_purgeable())
  506. return -EPERM;
  507. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  508. if (!vmobject.is_volatile())
  509. return 0;
  510. vmobject.set_volatile(false);
  511. bool was_purged = vmobject.was_purged();
  512. vmobject.set_was_purged(false);
  513. return was_purged ? 1 : 0;
  514. }
  515. if (advice & MADV_GET_VOLATILE) {
  516. if (!region->vmobject().is_purgeable())
  517. return -EPERM;
  518. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  519. return vmobject.is_volatile() ? 0 : 1;
  520. }
  521. return -EINVAL;
  522. }
  523. int Process::sys$purge(int mode)
  524. {
  525. REQUIRE_NO_PROMISES;
  526. if (!is_superuser())
  527. return -EPERM;
  528. int purged_page_count = 0;
  529. if (mode & PURGE_ALL_VOLATILE) {
  530. NonnullRefPtrVector<PurgeableVMObject> vmobjects;
  531. {
  532. InterruptDisabler disabler;
  533. MM.for_each_vmobject([&](auto& vmobject) {
  534. if (vmobject.is_purgeable())
  535. vmobjects.append(static_cast<PurgeableVMObject&>(vmobject));
  536. return IterationDecision::Continue;
  537. });
  538. }
  539. for (auto& vmobject : vmobjects) {
  540. purged_page_count += vmobject.purge();
  541. }
  542. }
  543. if (mode & PURGE_ALL_CLEAN_INODE) {
  544. NonnullRefPtrVector<InodeVMObject> vmobjects;
  545. {
  546. InterruptDisabler disabler;
  547. MM.for_each_vmobject([&](auto& vmobject) {
  548. if (vmobject.is_inode())
  549. vmobjects.append(static_cast<InodeVMObject&>(vmobject));
  550. return IterationDecision::Continue;
  551. });
  552. }
  553. for (auto& vmobject : vmobjects) {
  554. purged_page_count += vmobject.release_all_clean_pages();
  555. }
  556. }
  557. return purged_page_count;
  558. }
  559. int Process::sys$gethostname(char* buffer, ssize_t size)
  560. {
  561. REQUIRE_PROMISE(stdio);
  562. if (size < 0)
  563. return -EINVAL;
  564. if (!validate_write(buffer, size))
  565. return -EFAULT;
  566. LOCKER(*s_hostname_lock);
  567. if ((size_t)size < (s_hostname->length() + 1))
  568. return -ENAMETOOLONG;
  569. copy_to_user(buffer, s_hostname->characters(), s_hostname->length() + 1);
  570. return 0;
  571. }
  572. pid_t Process::sys$fork(RegisterDump& regs)
  573. {
  574. REQUIRE_PROMISE(proc);
  575. Thread* child_first_thread = nullptr;
  576. auto* child = new Process(child_first_thread, m_name, m_uid, m_gid, m_pid, m_ring, m_cwd, m_executable, m_tty, this);
  577. child->m_root_directory = m_root_directory;
  578. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  579. child->m_promises = m_promises;
  580. child->m_execpromises = m_execpromises;
  581. child->m_veil_state = m_veil_state;
  582. child->m_unveiled_paths = m_unveiled_paths;
  583. child->m_fds = m_fds;
  584. child->m_sid = m_sid;
  585. child->m_pgid = m_pgid;
  586. child->m_umask = m_umask;
  587. #ifdef FORK_DEBUG
  588. dbgprintf("fork: child=%p\n", child);
  589. #endif
  590. for (auto& region : m_regions) {
  591. #ifdef FORK_DEBUG
  592. dbg() << "fork: cloning Region{" << &region << "} '" << region.name() << "' @ " << region.vaddr();
  593. #endif
  594. auto& child_region = child->add_region(region.clone());
  595. child_region.map(child->page_directory());
  596. if (&region == m_master_tls_region)
  597. child->m_master_tls_region = &child_region;
  598. }
  599. child->m_extra_gids = m_extra_gids;
  600. auto& child_tss = child_first_thread->m_tss;
  601. child_tss.eax = 0; // fork() returns 0 in the child :^)
  602. child_tss.ebx = regs.ebx;
  603. child_tss.ecx = regs.ecx;
  604. child_tss.edx = regs.edx;
  605. child_tss.ebp = regs.ebp;
  606. child_tss.esp = regs.userspace_esp;
  607. child_tss.esi = regs.esi;
  608. child_tss.edi = regs.edi;
  609. child_tss.eflags = regs.eflags;
  610. child_tss.eip = regs.eip;
  611. child_tss.cs = regs.cs;
  612. child_tss.ds = regs.ds;
  613. child_tss.es = regs.es;
  614. child_tss.fs = regs.fs;
  615. child_tss.gs = regs.gs;
  616. child_tss.ss = regs.userspace_ss;
  617. #ifdef FORK_DEBUG
  618. dbgprintf("fork: child will begin executing at %w:%x with stack %w:%x, kstack %w:%x\n", child_tss.cs, child_tss.eip, child_tss.ss, child_tss.esp, child_tss.ss0, child_tss.esp0);
  619. #endif
  620. {
  621. InterruptDisabler disabler;
  622. g_processes->prepend(child);
  623. }
  624. #ifdef TASK_DEBUG
  625. kprintf("Process %u (%s) forked from %u @ %p\n", child->pid(), child->name().characters(), m_pid, child_tss.eip);
  626. #endif
  627. child_first_thread->set_state(Thread::State::Skip1SchedulerPass);
  628. return child->pid();
  629. }
  630. int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description)
  631. {
  632. ASSERT(is_ring3());
  633. auto path = main_program_description->absolute_path();
  634. dbg() << "do_exec(" << path << ")";
  635. // FIXME(Thread): Kill any threads the moment we commit to the exec().
  636. if (thread_count() != 1) {
  637. dbgprintf("Gonna die because I have many threads! These are the threads:\n");
  638. for_each_thread([](Thread& thread) {
  639. dbgprintf("Thread{%p}: TID=%d, PID=%d\n", &thread, thread.tid(), thread.pid());
  640. return IterationDecision::Continue;
  641. });
  642. ASSERT(thread_count() == 1);
  643. ASSERT_NOT_REACHED();
  644. }
  645. size_t total_blob_size = 0;
  646. for (auto& a : arguments)
  647. total_blob_size += a.length() + 1;
  648. for (auto& e : environment)
  649. total_blob_size += e.length() + 1;
  650. size_t total_meta_size = sizeof(char*) * (arguments.size() + 1) + sizeof(char*) * (environment.size() + 1);
  651. // FIXME: How much stack space does process startup need?
  652. if ((total_blob_size + total_meta_size) >= Thread::default_userspace_stack_size)
  653. return -E2BIG;
  654. auto parts = path.split('/');
  655. if (parts.is_empty())
  656. return -ENOENT;
  657. RefPtr<InodeVMObject> vmobject;
  658. if (interpreter_description) {
  659. vmobject = InodeVMObject::create_with_inode(*interpreter_description->inode());
  660. } else {
  661. vmobject = InodeVMObject::create_with_inode(*main_program_description->inode());
  662. }
  663. if (static_cast<const InodeVMObject&>(*vmobject).writable_mappings()) {
  664. dbg() << "Refusing to execute a write-mapped program";
  665. return -ETXTBSY;
  666. }
  667. auto old_page_directory = move(m_page_directory);
  668. auto old_regions = move(m_regions);
  669. m_page_directory = PageDirectory::create_for_userspace(*this);
  670. #ifdef MM_DEBUG
  671. dbgprintf("Process %u exec: PD=%x created\n", pid(), m_page_directory.ptr());
  672. #endif
  673. MM.enter_process_paging_scope(*this);
  674. Region* region { nullptr };
  675. InodeMetadata loader_metadata;
  676. // FIXME: Hoooo boy this is a hack if I ever saw one.
  677. // This is the 'random' offset we're giving to our ET_DYN exectuables to start as.
  678. // It also happens to be the static Virtual Addresss offset every static exectuable gets :)
  679. // Without this, some assumptions by the ELF loading hooks below are severely broken.
  680. // 0x08000000 is a verified random number chosen by random dice roll https://xkcd.com/221/
  681. u32 totally_random_offset = interpreter_description ? 0x08000000 : 0;
  682. // FIXME: We should be able to load both the PT_INTERP interpreter and the main program... once the RTLD is smart enough
  683. if (interpreter_description) {
  684. loader_metadata = interpreter_description->metadata();
  685. region = allocate_region_with_vmobject(VirtualAddress(), loader_metadata.size, *vmobject, 0, interpreter_description->absolute_path(), PROT_READ, false);
  686. // we don't need the interpreter file desciption after we've loaded (or not) it into memory
  687. interpreter_description = nullptr;
  688. } else {
  689. loader_metadata = main_program_description->metadata();
  690. region = allocate_region_with_vmobject(VirtualAddress(), loader_metadata.size, *vmobject, 0, main_program_description->absolute_path(), PROT_READ, false);
  691. }
  692. ASSERT(region);
  693. Region* master_tls_region { nullptr };
  694. size_t master_tls_size = 0;
  695. size_t master_tls_alignment = 0;
  696. u32 entry_eip = 0;
  697. OwnPtr<ELFLoader> loader;
  698. {
  699. ArmedScopeGuard rollback_regions_guard([&]() {
  700. ASSERT(&current->process() == this);
  701. m_page_directory = move(old_page_directory);
  702. m_regions = move(old_regions);
  703. MM.enter_process_paging_scope(*this);
  704. });
  705. loader = make<ELFLoader>(region->vaddr().as_ptr(), loader_metadata.size);
  706. // Load the correct executable -- either interp or main program.
  707. // FIXME: Once we actually load both interp and main, we'll need to be more clever about this.
  708. // In that case, both will be ET_DYN objects, so they'll both be completely relocatable.
  709. // That means, we can put them literally anywhere in User VM space (ASLR anyone?).
  710. // ALSO FIXME: Reminder to really really fix that 'totally random offset' business.
  711. loader->map_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable, bool is_executable, const String& name) -> u8* {
  712. ASSERT(size);
  713. ASSERT(alignment == PAGE_SIZE);
  714. int prot = 0;
  715. if (is_readable)
  716. prot |= PROT_READ;
  717. if (is_writable)
  718. prot |= PROT_WRITE;
  719. if (is_executable)
  720. prot |= PROT_EXEC;
  721. if (auto* region = allocate_region_with_vmobject(vaddr.offset(totally_random_offset), size, *vmobject, offset_in_image, String(name), prot))
  722. return region->vaddr().as_ptr();
  723. return nullptr;
  724. };
  725. loader->alloc_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) -> u8* {
  726. ASSERT(size);
  727. ASSERT(alignment == PAGE_SIZE);
  728. int prot = 0;
  729. if (is_readable)
  730. prot |= PROT_READ;
  731. if (is_writable)
  732. prot |= PROT_WRITE;
  733. if (auto* region = allocate_region(vaddr.offset(totally_random_offset), size, String(name), prot))
  734. return region->vaddr().as_ptr();
  735. return nullptr;
  736. };
  737. // FIXME: Move TLS region allocation to userspace: LibC and the dynamic loader.
  738. // LibC if we end up with a statically linked executable, and the
  739. // dynamic loader so that it can create new TLS blocks for each shared libarary
  740. // that gets loaded as part of DT_NEEDED processing, and via dlopen()
  741. // If that doesn't happen quickly, at least pass the location of the TLS region
  742. // some ELF Auxilliary Vector so the loader can use it/create new ones as necessary.
  743. loader->tls_section_hook = [&](size_t size, size_t alignment) {
  744. ASSERT(size);
  745. master_tls_region = allocate_region({}, size, String(), PROT_READ | PROT_WRITE);
  746. master_tls_size = size;
  747. master_tls_alignment = alignment;
  748. return master_tls_region->vaddr().as_ptr();
  749. };
  750. bool success = loader->load();
  751. if (!success) {
  752. kprintf("do_exec: Failure loading %s\n", path.characters());
  753. return -ENOEXEC;
  754. }
  755. // FIXME: Validate that this virtual address is within executable region,
  756. // instead of just non-null. You could totally have a DSO with entry point of
  757. // the beginning of the text segement.
  758. if (!loader->entry().offset(totally_random_offset).get()) {
  759. kprintf("do_exec: Failure loading %s, entry pointer is invalid! (%p)\n", path.characters(), loader->entry().offset(totally_random_offset).get());
  760. return -ENOEXEC;
  761. }
  762. rollback_regions_guard.disarm();
  763. // NOTE: At this point, we've committed to the new executable.
  764. entry_eip = loader->entry().offset(totally_random_offset).get();
  765. #ifdef EXEC_DEBUG
  766. kprintf("Memory layout after ELF load:");
  767. dump_regions();
  768. #endif
  769. }
  770. m_elf_loader = move(loader);
  771. m_executable = main_program_description->custody();
  772. m_promises = m_execpromises;
  773. m_veil_state = VeilState::None;
  774. m_unveiled_paths.clear();
  775. // Copy of the master TLS region that we will clone for new threads
  776. m_master_tls_region = master_tls_region;
  777. auto main_program_metadata = main_program_description->metadata();
  778. if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
  779. if (main_program_metadata.is_setuid())
  780. m_euid = main_program_metadata.uid;
  781. if (main_program_metadata.is_setgid())
  782. m_egid = main_program_metadata.gid;
  783. }
  784. current->set_default_signal_dispositions();
  785. current->m_signal_mask = 0;
  786. current->m_pending_signals = 0;
  787. m_futex_queues.clear();
  788. for (int i = 0; i < m_fds.size(); ++i) {
  789. auto& daf = m_fds[i];
  790. if (daf.description && daf.flags & FD_CLOEXEC) {
  791. daf.description->close();
  792. daf = {};
  793. }
  794. }
  795. // FIXME: Should we just make a new Thread here instead?
  796. Thread* new_main_thread = nullptr;
  797. if (&current->process() == this) {
  798. new_main_thread = current;
  799. } else {
  800. for_each_thread([&](auto& thread) {
  801. new_main_thread = &thread;
  802. return IterationDecision::Break;
  803. });
  804. }
  805. ASSERT(new_main_thread);
  806. // NOTE: We create the new stack before disabling interrupts since it will zero-fault
  807. // and we don't want to deal with faults after this point.
  808. u32 new_userspace_esp = new_main_thread->make_userspace_stack_for_main_thread(move(arguments), move(environment));
  809. // We cli() manually here because we don't want to get interrupted between do_exec() and Schedule::yield().
  810. // The reason is that the task redirection we've set up above will be clobbered by the timer IRQ.
  811. // If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
  812. if (&current->process() == this)
  813. cli();
  814. // NOTE: Be careful to not trigger any page faults below!
  815. Scheduler::prepare_to_modify_tss(*new_main_thread);
  816. m_name = parts.take_last();
  817. new_main_thread->set_name(m_name);
  818. auto& tss = new_main_thread->m_tss;
  819. u32 old_esp0 = tss.esp0;
  820. m_master_tls_size = master_tls_size;
  821. m_master_tls_alignment = master_tls_alignment;
  822. new_main_thread->make_thread_specific_region({});
  823. memset(&tss, 0, sizeof(TSS32));
  824. tss.iomapbase = sizeof(TSS32);
  825. tss.eflags = 0x0202;
  826. tss.eip = entry_eip;
  827. tss.cs = 0x1b;
  828. tss.ds = 0x23;
  829. tss.es = 0x23;
  830. tss.fs = 0x23;
  831. tss.gs = thread_specific_selector() | 3;
  832. tss.ss = 0x23;
  833. tss.cr3 = page_directory().cr3();
  834. tss.esp = new_userspace_esp;
  835. tss.ss0 = 0x10;
  836. tss.esp0 = old_esp0;
  837. tss.ss2 = m_pid;
  838. #ifdef TASK_DEBUG
  839. kprintf("Process %u (%s) exec'd %s @ %p\n", pid(), name().characters(), path.characters(), tss.eip);
  840. #endif
  841. new_main_thread->set_state(Thread::State::Skip1SchedulerPass);
  842. big_lock().force_unlock_if_locked();
  843. return 0;
  844. }
  845. static KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const char first_page[], int nread)
  846. {
  847. int word_start = 2;
  848. int word_length = 0;
  849. if (nread > 2 && first_page[0] == '#' && first_page[1] == '!') {
  850. Vector<String> interpreter_words;
  851. for (int i = 2; i < nread; ++i) {
  852. if (first_page[i] == '\n') {
  853. break;
  854. }
  855. if (first_page[i] != ' ') {
  856. ++word_length;
  857. }
  858. if (first_page[i] == ' ') {
  859. if (word_length > 0) {
  860. interpreter_words.append(String(&first_page[word_start], word_length));
  861. }
  862. word_length = 0;
  863. word_start = i + 1;
  864. }
  865. }
  866. if (word_length > 0)
  867. interpreter_words.append(String(&first_page[word_start], word_length));
  868. if (!interpreter_words.is_empty())
  869. return interpreter_words;
  870. }
  871. return KResult(-ENOEXEC);
  872. }
  873. KResultOr<NonnullRefPtr<FileDescription>> Process::find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size)
  874. {
  875. if (nread < (int)sizeof(Elf32_Ehdr))
  876. return KResult(-ENOEXEC);
  877. auto elf_header = (Elf32_Ehdr*)first_page;
  878. if (!ELFImage::validate_elf_header(*elf_header, file_size)) {
  879. dbgprintf("%s(%d) exec(%s): File has invalid ELF header\n", m_name.characters(), m_pid, path.characters());
  880. return KResult(-ENOEXEC);
  881. }
  882. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  883. String interpreter_path;
  884. if (!ELFImage::validate_program_headers(*elf_header, file_size, (u8*)first_page, nread, interpreter_path)) {
  885. dbgprintf("%s(%d) exec(%s): File has invalid ELF Program headers\n", m_name.characters(), m_pid, path.characters());
  886. return KResult(-ENOEXEC);
  887. }
  888. if (!interpreter_path.is_empty()) {
  889. // Programs with an interpreter better be relocatable executables or we don't know what to do...
  890. if (elf_header->e_type != ET_DYN)
  891. return KResult(-ENOEXEC);
  892. dbgprintf("%s(%d) exec(%s): Using program interpreter %s\n", m_name.characters(), m_pid, path.characters(), interpreter_path.characters());
  893. auto interp_result = VFS::the().open(interpreter_path, O_EXEC, 0, current_directory());
  894. if (interp_result.is_error()) {
  895. dbgprintf("%s(%d) exec(%s): Unable to open program interpreter %s\n", m_name.characters(), m_pid, path.characters(), interpreter_path.characters());
  896. return interp_result.error();
  897. }
  898. auto interpreter_description = interp_result.value();
  899. auto interp_metadata = interpreter_description->metadata();
  900. ASSERT(interpreter_description->inode());
  901. // Validate the program interpreter as a valid elf binary.
  902. // If your program interpreter is a #! file or something, it's time to stop playing games :)
  903. if (interp_metadata.size < (int)sizeof(Elf32_Ehdr))
  904. return KResult(-ENOEXEC);
  905. memset(first_page, 0, sizeof(first_page));
  906. nread = interpreter_description->read((u8*)&first_page, sizeof(first_page));
  907. if (nread < (int)sizeof(Elf32_Ehdr))
  908. return KResult(-ENOEXEC);
  909. elf_header = (Elf32_Ehdr*)first_page;
  910. if (!ELFImage::validate_elf_header(*elf_header, interp_metadata.size)) {
  911. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has invalid ELF header\n", m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters());
  912. return KResult(-ENOEXEC);
  913. }
  914. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  915. String interpreter_interpreter_path;
  916. if (!ELFImage::validate_program_headers(*elf_header, interp_metadata.size, (u8*)first_page, nread, interpreter_interpreter_path)) {
  917. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has invalid ELF Program headers\n", m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters());
  918. return KResult(-ENOEXEC);
  919. }
  920. if (!interpreter_interpreter_path.is_empty()) {
  921. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has its own interpreter (%s)! No thank you!\n",
  922. m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters(), interpreter_interpreter_path.characters());
  923. return KResult(-ELOOP);
  924. }
  925. return interpreter_description;
  926. }
  927. if (elf_header->e_type != ET_EXEC) {
  928. // We can't exec an ET_REL, that's just an object file from the compiler
  929. // If it's ET_DYN with no PT_INTERP, then we can't load it properly either
  930. return KResult(-ENOEXEC);
  931. }
  932. // No interpreter, but, path refers to a valid elf image
  933. return KResult(KSuccess);
  934. }
  935. int Process::exec(String path, Vector<String> arguments, Vector<String> environment, int recursion_depth)
  936. {
  937. if (recursion_depth > 2) {
  938. dbgprintf("%s(%d) exec(%s): SHENANIGANS! recursed too far trying to find #! interpreter\n", m_name.characters(), m_pid, path.characters());
  939. return -ELOOP;
  940. }
  941. // Open the file to check what kind of binary format it is
  942. // Currently supported formats:
  943. // - #! interpreted file
  944. // - ELF32
  945. // * ET_EXEC binary that just gets loaded
  946. // * ET_DYN binary that requires a program interpreter
  947. //
  948. auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
  949. if (result.is_error())
  950. return result.error();
  951. auto description = result.value();
  952. auto metadata = description->metadata();
  953. // Always gonna need at least 3 bytes. these are for #!X
  954. if (metadata.size < 3)
  955. return -ENOEXEC;
  956. ASSERT(description->inode());
  957. // Read the first page of the program into memory so we can validate the binfmt of it
  958. char first_page[PAGE_SIZE];
  959. int nread = description->read((u8*)&first_page, sizeof(first_page));
  960. // 1) #! interpreted file
  961. auto shebang_result = find_shebang_interpreter_for_executable(first_page, nread);
  962. if (!shebang_result.is_error()) {
  963. Vector<String> new_arguments(shebang_result.value());
  964. new_arguments.append(path);
  965. arguments.remove(0);
  966. new_arguments.append(move(arguments));
  967. return exec(shebang_result.value().first(), move(new_arguments), move(environment), ++recursion_depth);
  968. }
  969. // #2) ELF32 for i386
  970. auto elf_result = find_elf_interpreter_for_executable(path, first_page, nread, metadata.size);
  971. RefPtr<FileDescription> interpreter_description;
  972. // We're getting either an interpreter, an error, or KSuccess (i.e. no interpreter but file checks out)
  973. if (!elf_result.is_error())
  974. interpreter_description = elf_result.value();
  975. else if (elf_result.error().is_error())
  976. return elf_result.error();
  977. // The bulk of exec() is done by do_exec(), which ensures that all locals
  978. // are cleaned up by the time we yield-teleport below.
  979. int rc = do_exec(move(description), move(arguments), move(environment), move(interpreter_description));
  980. if (rc < 0)
  981. return rc;
  982. if (&current->process() == this) {
  983. Scheduler::yield();
  984. ASSERT_NOT_REACHED();
  985. }
  986. return 0;
  987. }
  988. int Process::sys$execve(const Syscall::SC_execve_params* user_params)
  989. {
  990. REQUIRE_PROMISE(exec);
  991. // NOTE: Be extremely careful with allocating any kernel memory in exec().
  992. // On success, the kernel stack will be lost.
  993. Syscall::SC_execve_params params;
  994. if (!validate_read_and_copy_typed(&params, user_params))
  995. return -EFAULT;
  996. if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
  997. return -E2BIG;
  998. String path;
  999. {
  1000. auto path_arg = get_syscall_path_argument(params.path);
  1001. if (path_arg.is_error())
  1002. return path_arg.error();
  1003. path = path_arg.value();
  1004. }
  1005. auto copy_user_strings = [&](const auto& list, auto& output) {
  1006. if (!list.length)
  1007. return true;
  1008. if (!validate_read_typed(list.strings, list.length))
  1009. return false;
  1010. Vector<Syscall::StringArgument, 32> strings;
  1011. strings.resize(list.length);
  1012. copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument));
  1013. for (size_t i = 0; i < list.length; ++i) {
  1014. auto string = validate_and_copy_string_from_user(strings[i]);
  1015. if (string.is_null())
  1016. return false;
  1017. output.append(move(string));
  1018. }
  1019. return true;
  1020. };
  1021. Vector<String> arguments;
  1022. if (!copy_user_strings(params.arguments, arguments))
  1023. return -EFAULT;
  1024. Vector<String> environment;
  1025. if (!copy_user_strings(params.environment, environment))
  1026. return -EFAULT;
  1027. int rc = exec(move(path), move(arguments), move(environment));
  1028. ASSERT(rc < 0); // We should never continue after a successful exec!
  1029. return rc;
  1030. }
  1031. Process* Process::create_user_process(Thread*& first_thread, const String& path, uid_t uid, gid_t gid, pid_t parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  1032. {
  1033. // FIXME: Don't split() the path twice (sys$spawn also does it...)
  1034. auto parts = path.split('/');
  1035. if (arguments.is_empty()) {
  1036. arguments.append(parts.last());
  1037. }
  1038. RefPtr<Custody> cwd;
  1039. RefPtr<Custody> root;
  1040. {
  1041. InterruptDisabler disabler;
  1042. if (auto* parent = Process::from_pid(parent_pid)) {
  1043. cwd = parent->m_cwd;
  1044. root = parent->m_root_directory;
  1045. }
  1046. }
  1047. if (!cwd)
  1048. cwd = VFS::the().root_custody();
  1049. if (!root)
  1050. root = VFS::the().root_custody();
  1051. auto* process = new Process(first_thread, parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty);
  1052. process->m_fds.resize(m_max_open_file_descriptors);
  1053. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  1054. auto description = device_to_use_as_tty.open(O_RDWR).value();
  1055. process->m_fds[0].set(*description);
  1056. process->m_fds[1].set(*description);
  1057. process->m_fds[2].set(*description);
  1058. error = process->exec(path, move(arguments), move(environment));
  1059. if (error != 0) {
  1060. delete process;
  1061. return nullptr;
  1062. }
  1063. {
  1064. InterruptDisabler disabler;
  1065. g_processes->prepend(process);
  1066. }
  1067. #ifdef TASK_DEBUG
  1068. kprintf("Process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), first_thread->tss().eip);
  1069. #endif
  1070. error = 0;
  1071. return process;
  1072. }
  1073. Process* Process::create_kernel_process(Thread*& first_thread, String&& name, void (*e)())
  1074. {
  1075. auto* process = new Process(first_thread, move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  1076. first_thread->tss().eip = (uintptr_t)e;
  1077. if (process->pid() != 0) {
  1078. InterruptDisabler disabler;
  1079. g_processes->prepend(process);
  1080. #ifdef TASK_DEBUG
  1081. kprintf("Kernel process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), first_thread->tss().eip);
  1082. #endif
  1083. }
  1084. first_thread->set_state(Thread::State::Runnable);
  1085. return process;
  1086. }
  1087. Process::Process(Thread*& first_thread, const String& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
  1088. : m_name(move(name))
  1089. , m_pid(allocate_pid())
  1090. , m_uid(uid)
  1091. , m_gid(gid)
  1092. , m_euid(uid)
  1093. , m_egid(gid)
  1094. , m_ring(ring)
  1095. , m_executable(move(executable))
  1096. , m_cwd(move(cwd))
  1097. , m_tty(tty)
  1098. , m_ppid(ppid)
  1099. {
  1100. dbg() << "Created new process " << m_name << "(" << m_pid << ")";
  1101. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  1102. #ifdef MM_DEBUG
  1103. dbgprintf("Process %u ctor: PD=%x created\n", pid(), m_page_directory.ptr());
  1104. #endif
  1105. if (fork_parent) {
  1106. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  1107. first_thread = current->clone(*this);
  1108. } else {
  1109. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  1110. first_thread = new Thread(*this);
  1111. }
  1112. }
  1113. Process::~Process()
  1114. {
  1115. ASSERT(thread_count() == 0);
  1116. }
  1117. void Process::dump_regions()
  1118. {
  1119. kprintf("Process %s(%u) regions:\n", name().characters(), pid());
  1120. kprintf("BEGIN END SIZE ACCESS NAME\n");
  1121. for (auto& region : m_regions) {
  1122. kprintf("%08x -- %08x %08x %c%c%c%c%c%c %s\n",
  1123. region.vaddr().get(),
  1124. region.vaddr().offset(region.size() - 1).get(),
  1125. region.size(),
  1126. region.is_readable() ? 'R' : ' ',
  1127. region.is_writable() ? 'W' : ' ',
  1128. region.is_executable() ? 'X' : ' ',
  1129. region.is_shared() ? 'S' : ' ',
  1130. region.is_stack() ? 'T' : ' ',
  1131. region.vmobject().is_purgeable() ? 'P' : ' ',
  1132. region.name().characters());
  1133. }
  1134. MM.dump_kernel_regions();
  1135. }
  1136. void Process::sys$exit(int status)
  1137. {
  1138. cli();
  1139. #ifdef TASK_DEBUG
  1140. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  1141. #endif
  1142. if (status != 0)
  1143. dump_backtrace();
  1144. m_termination_status = status;
  1145. m_termination_signal = 0;
  1146. die();
  1147. current->die_if_needed();
  1148. ASSERT_NOT_REACHED();
  1149. }
  1150. void signal_trampoline_dummy(void)
  1151. {
  1152. // The trampoline preserves the current eax, pushes the signal code and
  1153. // then calls the signal handler. We do this because, when interrupting a
  1154. // blocking syscall, that syscall may return some special error code in eax;
  1155. // This error code would likely be overwritten by the signal handler, so it's
  1156. // neccessary to preserve it here.
  1157. asm(
  1158. ".intel_syntax noprefix\n"
  1159. "asm_signal_trampoline:\n"
  1160. "push ebp\n"
  1161. "mov ebp, esp\n"
  1162. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  1163. "sub esp, 4\n" // align the stack to 16 bytes
  1164. "mov eax, [ebp+12]\n" // push the signal code
  1165. "push eax\n"
  1166. "call [ebp+8]\n" // call the signal handler
  1167. "add esp, 8\n"
  1168. "mov eax, %P0\n"
  1169. "int 0x82\n" // sigreturn syscall
  1170. "asm_signal_trampoline_end:\n"
  1171. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  1172. }
  1173. extern "C" void asm_signal_trampoline(void);
  1174. extern "C" void asm_signal_trampoline_end(void);
  1175. void create_signal_trampolines()
  1176. {
  1177. InterruptDisabler disabler;
  1178. // NOTE: We leak this region.
  1179. auto* trampoline_region = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write | Region::Access::Execute, false).leak_ptr();
  1180. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  1181. u8* trampoline = (u8*)asm_signal_trampoline;
  1182. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  1183. size_t trampoline_size = trampoline_end - trampoline;
  1184. {
  1185. SmapDisabler disabler;
  1186. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  1187. memcpy(code_ptr, trampoline, trampoline_size);
  1188. }
  1189. trampoline_region->set_writable(false);
  1190. trampoline_region->remap();
  1191. }
  1192. void create_kernel_info_page()
  1193. {
  1194. auto* info_page_region_for_userspace = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Kernel info page", Region::Access::Read).leak_ptr();
  1195. auto* info_page_region_for_kernel = MM.allocate_kernel_region_with_vmobject(info_page_region_for_userspace->vmobject(), PAGE_SIZE, "Kernel info page", Region::Access::Read | Region::Access::Write).leak_ptr();
  1196. s_info_page_address_for_userspace = info_page_region_for_userspace->vaddr();
  1197. s_info_page_address_for_kernel = info_page_region_for_kernel->vaddr();
  1198. memset(s_info_page_address_for_kernel.as_ptr(), 0, PAGE_SIZE);
  1199. }
  1200. int Process::sys$sigreturn(RegisterDump& registers)
  1201. {
  1202. REQUIRE_PROMISE(stdio);
  1203. SmapDisabler disabler;
  1204. //Here, we restore the state pushed by dispatch signal and asm_signal_trampoline.
  1205. u32* stack_ptr = (u32*)registers.userspace_esp;
  1206. u32 smuggled_eax = *stack_ptr;
  1207. //pop the stored eax, ebp, return address, handler and signal code
  1208. stack_ptr += 5;
  1209. current->m_signal_mask = *stack_ptr;
  1210. stack_ptr++;
  1211. //pop edi, esi, ebp, esp, ebx, edx, ecx and eax
  1212. memcpy(&registers.edi, stack_ptr, 8 * sizeof(uintptr_t));
  1213. stack_ptr += 8;
  1214. registers.eip = *stack_ptr;
  1215. stack_ptr++;
  1216. registers.eflags = *stack_ptr;
  1217. stack_ptr++;
  1218. registers.userspace_esp = registers.esp;
  1219. return smuggled_eax;
  1220. }
  1221. void Process::crash(int signal, u32 eip)
  1222. {
  1223. ASSERT_INTERRUPTS_DISABLED();
  1224. ASSERT(!is_dead());
  1225. ASSERT(&current->process() == this);
  1226. if (eip >= 0xc0000000 && ksyms_ready) {
  1227. auto* ksym = ksymbolicate(eip);
  1228. dbgprintf("\033[31;1m%p %s +%d\033[0m\n", eip, ksym ? demangle(ksym->name).characters() : "(k?)", ksym ? eip - ksym->address : 0);
  1229. } else if (m_elf_loader) {
  1230. dbgprintf("\033[31;1m%p %s\033[0m\n", eip, m_elf_loader->symbolicate(eip).characters());
  1231. } else {
  1232. dbgprintf("\033[31;1m%p (?)\033[0m\n", eip);
  1233. }
  1234. dump_backtrace();
  1235. m_termination_signal = signal;
  1236. dump_regions();
  1237. ASSERT(is_ring3());
  1238. die();
  1239. // We can not return from here, as there is nowhere
  1240. // to unwind to, so die right away.
  1241. current->die_if_needed();
  1242. ASSERT_NOT_REACHED();
  1243. }
  1244. Process* Process::from_pid(pid_t pid)
  1245. {
  1246. ASSERT_INTERRUPTS_DISABLED();
  1247. for (auto& process : *g_processes) {
  1248. if (process.pid() == pid)
  1249. return &process;
  1250. }
  1251. return nullptr;
  1252. }
  1253. RefPtr<FileDescription> Process::file_description(int fd) const
  1254. {
  1255. if (fd < 0)
  1256. return nullptr;
  1257. if (fd < m_fds.size())
  1258. return m_fds[fd].description.ptr();
  1259. return nullptr;
  1260. }
  1261. int Process::fd_flags(int fd) const
  1262. {
  1263. if (fd < 0)
  1264. return -1;
  1265. if (fd < m_fds.size())
  1266. return m_fds[fd].flags;
  1267. return -1;
  1268. }
  1269. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, ssize_t size)
  1270. {
  1271. REQUIRE_PROMISE(stdio);
  1272. if (size < 0)
  1273. return -EINVAL;
  1274. if (!validate_write(buffer, size))
  1275. return -EFAULT;
  1276. auto description = file_description(fd);
  1277. if (!description)
  1278. return -EBADF;
  1279. return description->get_dir_entries((u8*)buffer, size);
  1280. }
  1281. int Process::sys$lseek(int fd, off_t offset, int whence)
  1282. {
  1283. REQUIRE_PROMISE(stdio);
  1284. auto description = file_description(fd);
  1285. if (!description)
  1286. return -EBADF;
  1287. return description->seek(offset, whence);
  1288. }
  1289. int Process::sys$ttyname_r(int fd, char* buffer, ssize_t size)
  1290. {
  1291. REQUIRE_PROMISE(tty);
  1292. if (size < 0)
  1293. return -EINVAL;
  1294. if (!validate_write(buffer, size))
  1295. return -EFAULT;
  1296. auto description = file_description(fd);
  1297. if (!description)
  1298. return -EBADF;
  1299. if (!description->is_tty())
  1300. return -ENOTTY;
  1301. String tty_name = description->tty()->tty_name();
  1302. if ((size_t)size < tty_name.length() + 1)
  1303. return -ERANGE;
  1304. copy_to_user(buffer, tty_name.characters(), tty_name.length() + 1);
  1305. return 0;
  1306. }
  1307. int Process::sys$ptsname_r(int fd, char* buffer, ssize_t size)
  1308. {
  1309. REQUIRE_PROMISE(tty);
  1310. if (size < 0)
  1311. return -EINVAL;
  1312. if (!validate_write(buffer, size))
  1313. return -EFAULT;
  1314. auto description = file_description(fd);
  1315. if (!description)
  1316. return -EBADF;
  1317. auto* master_pty = description->master_pty();
  1318. if (!master_pty)
  1319. return -ENOTTY;
  1320. auto pts_name = master_pty->pts_name();
  1321. if ((size_t)size < pts_name.length() + 1)
  1322. return -ERANGE;
  1323. copy_to_user(buffer, pts_name.characters(), pts_name.length() + 1);
  1324. return 0;
  1325. }
  1326. ssize_t Process::sys$writev(int fd, const struct iovec* iov, int iov_count)
  1327. {
  1328. REQUIRE_PROMISE(stdio);
  1329. if (iov_count < 0)
  1330. return -EINVAL;
  1331. if (!validate_read_typed(iov, iov_count))
  1332. return -EFAULT;
  1333. u64 total_length = 0;
  1334. Vector<iovec, 32> vecs;
  1335. vecs.resize(iov_count);
  1336. copy_from_user(vecs.data(), iov, iov_count * sizeof(iovec));
  1337. for (auto& vec : vecs) {
  1338. if (!validate_read(vec.iov_base, vec.iov_len))
  1339. return -EFAULT;
  1340. total_length += vec.iov_len;
  1341. if (total_length > INT32_MAX)
  1342. return -EINVAL;
  1343. }
  1344. auto description = file_description(fd);
  1345. if (!description)
  1346. return -EBADF;
  1347. if (!description->is_writable())
  1348. return -EBADF;
  1349. int nwritten = 0;
  1350. for (auto& vec : vecs) {
  1351. int rc = do_write(*description, (const u8*)vec.iov_base, vec.iov_len);
  1352. if (rc < 0) {
  1353. if (nwritten == 0)
  1354. return rc;
  1355. return nwritten;
  1356. }
  1357. nwritten += rc;
  1358. }
  1359. return nwritten;
  1360. }
  1361. ssize_t Process::do_write(FileDescription& description, const u8* data, int data_size)
  1362. {
  1363. ssize_t nwritten = 0;
  1364. if (!description.is_blocking()) {
  1365. if (!description.can_write())
  1366. return -EAGAIN;
  1367. }
  1368. if (description.should_append()) {
  1369. #ifdef IO_DEBUG
  1370. dbgprintf("seeking to end (O_APPEND)\n");
  1371. #endif
  1372. description.seek(0, SEEK_END);
  1373. }
  1374. while (nwritten < data_size) {
  1375. #ifdef IO_DEBUG
  1376. dbgprintf("while %u < %u\n", nwritten, size);
  1377. #endif
  1378. if (!description.can_write()) {
  1379. #ifdef IO_DEBUG
  1380. dbgprintf("block write on %d\n", fd);
  1381. #endif
  1382. if (current->block<Thread::WriteBlocker>(description) != Thread::BlockResult::WokeNormally) {
  1383. if (nwritten == 0)
  1384. return -EINTR;
  1385. }
  1386. }
  1387. ssize_t rc = description.write(data + nwritten, data_size - nwritten);
  1388. #ifdef IO_DEBUG
  1389. dbgprintf(" -> write returned %d\n", rc);
  1390. #endif
  1391. if (rc < 0) {
  1392. // FIXME: Support returning partial nwritten with errno.
  1393. ASSERT(nwritten == 0);
  1394. return rc;
  1395. }
  1396. if (rc == 0)
  1397. break;
  1398. nwritten += rc;
  1399. }
  1400. return nwritten;
  1401. }
  1402. ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
  1403. {
  1404. REQUIRE_PROMISE(stdio);
  1405. if (size < 0)
  1406. return -EINVAL;
  1407. if (size == 0)
  1408. return 0;
  1409. if (!validate_read(data, size))
  1410. return -EFAULT;
  1411. #ifdef DEBUG_IO
  1412. dbgprintf("%s(%u): sys$write(%d, %p, %u)\n", name().characters(), pid(), fd, data, size);
  1413. #endif
  1414. auto description = file_description(fd);
  1415. if (!description)
  1416. return -EBADF;
  1417. if (!description->is_writable())
  1418. return -EBADF;
  1419. return do_write(*description, data, size);
  1420. }
  1421. ssize_t Process::sys$read(int fd, u8* buffer, ssize_t size)
  1422. {
  1423. REQUIRE_PROMISE(stdio);
  1424. if (size < 0)
  1425. return -EINVAL;
  1426. if (size == 0)
  1427. return 0;
  1428. if (!validate_write(buffer, size))
  1429. return -EFAULT;
  1430. #ifdef DEBUG_IO
  1431. dbgprintf("%s(%u) sys$read(%d, %p, %u)\n", name().characters(), pid(), fd, buffer, size);
  1432. #endif
  1433. auto description = file_description(fd);
  1434. if (!description)
  1435. return -EBADF;
  1436. if (!description->is_readable())
  1437. return -EBADF;
  1438. if (description->is_directory())
  1439. return -EISDIR;
  1440. if (description->is_blocking()) {
  1441. if (!description->can_read()) {
  1442. if (current->block<Thread::ReadBlocker>(*description) != Thread::BlockResult::WokeNormally)
  1443. return -EINTR;
  1444. if (!description->can_read())
  1445. return -EAGAIN;
  1446. }
  1447. }
  1448. return description->read(buffer, size);
  1449. }
  1450. int Process::sys$close(int fd)
  1451. {
  1452. REQUIRE_PROMISE(stdio);
  1453. auto description = file_description(fd);
  1454. #ifdef DEBUG_IO
  1455. dbgprintf("%s(%u) sys$close(%d) %p\n", name().characters(), pid(), fd, description.ptr());
  1456. #endif
  1457. if (!description)
  1458. return -EBADF;
  1459. int rc = description->close();
  1460. m_fds[fd] = {};
  1461. return rc;
  1462. }
  1463. int Process::sys$utime(const char* user_path, size_t path_length, const utimbuf* user_buf)
  1464. {
  1465. REQUIRE_PROMISE(fattr);
  1466. if (user_buf && !validate_read_typed(user_buf))
  1467. return -EFAULT;
  1468. auto path = get_syscall_path_argument(user_path, path_length);
  1469. if (path.is_error())
  1470. return path.error();
  1471. utimbuf buf;
  1472. if (user_buf) {
  1473. copy_from_user(&buf, user_buf);
  1474. } else {
  1475. auto now = kgettimeofday();
  1476. buf = { now.tv_sec, now.tv_sec };
  1477. }
  1478. return VFS::the().utime(path.value(), current_directory(), buf.actime, buf.modtime);
  1479. }
  1480. int Process::sys$access(const char* user_path, size_t path_length, int mode)
  1481. {
  1482. REQUIRE_PROMISE(rpath);
  1483. auto path = get_syscall_path_argument(user_path, path_length);
  1484. if (path.is_error())
  1485. return path.error();
  1486. return VFS::the().access(path.value(), mode, current_directory());
  1487. }
  1488. int Process::sys$fcntl(int fd, int cmd, u32 arg)
  1489. {
  1490. REQUIRE_PROMISE(stdio);
  1491. #ifdef DEBUG_IO
  1492. dbgprintf("sys$fcntl: fd=%d, cmd=%d, arg=%u\n", fd, cmd, arg);
  1493. #endif
  1494. auto description = file_description(fd);
  1495. if (!description)
  1496. return -EBADF;
  1497. // NOTE: The FD flags are not shared between FileDescription objects.
  1498. // This means that dup() doesn't copy the FD_CLOEXEC flag!
  1499. switch (cmd) {
  1500. case F_DUPFD: {
  1501. int arg_fd = (int)arg;
  1502. if (arg_fd < 0)
  1503. return -EINVAL;
  1504. int new_fd = alloc_fd(arg_fd);
  1505. if (new_fd < 0)
  1506. return new_fd;
  1507. m_fds[new_fd].set(*description);
  1508. return new_fd;
  1509. }
  1510. case F_GETFD:
  1511. return m_fds[fd].flags;
  1512. case F_SETFD:
  1513. m_fds[fd].flags = arg;
  1514. break;
  1515. case F_GETFL:
  1516. return description->file_flags();
  1517. case F_SETFL:
  1518. description->set_file_flags(arg);
  1519. break;
  1520. default:
  1521. ASSERT_NOT_REACHED();
  1522. }
  1523. return 0;
  1524. }
  1525. int Process::sys$fstat(int fd, stat* statbuf)
  1526. {
  1527. REQUIRE_PROMISE(stdio);
  1528. if (!validate_write_typed(statbuf))
  1529. return -EFAULT;
  1530. auto description = file_description(fd);
  1531. if (!description)
  1532. return -EBADF;
  1533. return description->fstat(*statbuf);
  1534. }
  1535. int Process::sys$lstat(const char* user_path, size_t path_length, stat* user_statbuf)
  1536. {
  1537. REQUIRE_PROMISE(rpath);
  1538. if (!validate_write_typed(user_statbuf))
  1539. return -EFAULT;
  1540. auto path = get_syscall_path_argument(user_path, path_length);
  1541. if (path.is_error())
  1542. return path.error();
  1543. auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), O_NOFOLLOW_NOERROR);
  1544. if (metadata_or_error.is_error())
  1545. return metadata_or_error.error();
  1546. stat statbuf;
  1547. auto result = metadata_or_error.value().stat(statbuf);
  1548. if (result.is_error())
  1549. return result;
  1550. copy_to_user(user_statbuf, &statbuf);
  1551. return 0;
  1552. }
  1553. int Process::sys$stat(const char* user_path, size_t path_length, stat* user_statbuf)
  1554. {
  1555. REQUIRE_PROMISE(rpath);
  1556. if (!validate_write_typed(user_statbuf))
  1557. return -EFAULT;
  1558. auto path = get_syscall_path_argument(user_path, path_length);
  1559. if (path.is_error())
  1560. return path.error();
  1561. auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory());
  1562. if (metadata_or_error.is_error())
  1563. return metadata_or_error.error();
  1564. stat statbuf;
  1565. auto result = metadata_or_error.value().stat(statbuf);
  1566. if (result.is_error())
  1567. return result;
  1568. copy_to_user(user_statbuf, &statbuf);
  1569. return 0;
  1570. }
  1571. template<typename DataType, typename SizeType>
  1572. bool Process::validate(const Syscall::MutableBufferArgument<DataType, SizeType>& buffer)
  1573. {
  1574. return validate_write(buffer.data, buffer.size);
  1575. }
  1576. template<typename DataType, typename SizeType>
  1577. bool Process::validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>& buffer)
  1578. {
  1579. return validate_read(buffer.data, buffer.size);
  1580. }
  1581. String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
  1582. {
  1583. if (user_length == 0)
  1584. return String::empty();
  1585. if (!user_characters)
  1586. return {};
  1587. if (!validate_read(user_characters, user_length))
  1588. return {};
  1589. SmapDisabler disabler;
  1590. size_t measured_length = strnlen(user_characters, user_length);
  1591. return String(user_characters, measured_length);
  1592. }
  1593. String Process::validate_and_copy_string_from_user(const Syscall::StringArgument& string) const
  1594. {
  1595. return validate_and_copy_string_from_user(string.characters, string.length);
  1596. }
  1597. int Process::sys$readlink(const Syscall::SC_readlink_params* user_params)
  1598. {
  1599. REQUIRE_PROMISE(rpath);
  1600. Syscall::SC_readlink_params params;
  1601. if (!validate_read_and_copy_typed(&params, user_params))
  1602. return -EFAULT;
  1603. if (!validate(params.buffer))
  1604. return -EFAULT;
  1605. auto path = get_syscall_path_argument(params.path);
  1606. if (path.is_error())
  1607. return path.error();
  1608. auto result = VFS::the().open(path.value(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory());
  1609. if (result.is_error())
  1610. return result.error();
  1611. auto description = result.value();
  1612. if (!description->metadata().is_symlink())
  1613. return -EINVAL;
  1614. auto contents = description->read_entire_file();
  1615. if (!contents)
  1616. return -EIO; // FIXME: Get a more detailed error from VFS.
  1617. auto link_target = String::copy(contents);
  1618. if (link_target.length() + 1 > params.buffer.size)
  1619. return -ENAMETOOLONG;
  1620. copy_to_user(params.buffer.data, link_target.characters(), link_target.length() + 1);
  1621. return 0;
  1622. }
  1623. int Process::sys$chdir(const char* user_path, size_t path_length)
  1624. {
  1625. REQUIRE_PROMISE(rpath);
  1626. auto path = get_syscall_path_argument(user_path, path_length);
  1627. if (path.is_error())
  1628. return path.error();
  1629. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  1630. if (directory_or_error.is_error())
  1631. return directory_or_error.error();
  1632. m_cwd = *directory_or_error.value();
  1633. return 0;
  1634. }
  1635. int Process::sys$fchdir(int fd)
  1636. {
  1637. REQUIRE_PROMISE(stdio);
  1638. auto description = file_description(fd);
  1639. if (!description)
  1640. return -EBADF;
  1641. if (!description->is_directory())
  1642. return -ENOTDIR;
  1643. if (!description->metadata().may_execute(*this))
  1644. return -EACCES;
  1645. m_cwd = description->custody();
  1646. return 0;
  1647. }
  1648. int Process::sys$getcwd(char* buffer, ssize_t size)
  1649. {
  1650. REQUIRE_PROMISE(rpath);
  1651. if (size < 0)
  1652. return -EINVAL;
  1653. if (!validate_write(buffer, size))
  1654. return -EFAULT;
  1655. auto path = current_directory().absolute_path();
  1656. if ((size_t)size < path.length() + 1)
  1657. return -ERANGE;
  1658. copy_to_user(buffer, path.characters(), path.length() + 1);
  1659. return 0;
  1660. }
  1661. int Process::number_of_open_file_descriptors() const
  1662. {
  1663. int count = 0;
  1664. for (auto& description : m_fds) {
  1665. if (description)
  1666. ++count;
  1667. }
  1668. return count;
  1669. }
  1670. int Process::sys$open(const Syscall::SC_open_params* user_params)
  1671. {
  1672. Syscall::SC_open_params params;
  1673. if (!validate_read_and_copy_typed(&params, user_params))
  1674. return -EFAULT;
  1675. int dirfd = params.dirfd;
  1676. int options = params.options;
  1677. u16 mode = params.mode;
  1678. if (options & O_NOFOLLOW_NOERROR)
  1679. return -EINVAL;
  1680. if (options & O_UNLINK_INTERNAL)
  1681. return -EINVAL;
  1682. if (options & O_WRONLY)
  1683. REQUIRE_PROMISE(wpath);
  1684. else if (options & O_RDONLY)
  1685. REQUIRE_PROMISE(rpath);
  1686. if (options & O_CREAT)
  1687. REQUIRE_PROMISE(cpath);
  1688. // Ignore everything except permission bits.
  1689. mode &= 04777;
  1690. auto path = get_syscall_path_argument(params.path);
  1691. if (path.is_error())
  1692. return path.error();
  1693. #ifdef DEBUG_IO
  1694. dbgprintf("%s(%u) sys$open(%d, \"%s\")\n", dirfd, name().characters(), pid(), path.value().characters());
  1695. #endif
  1696. int fd = alloc_fd();
  1697. if (fd < 0)
  1698. return fd;
  1699. RefPtr<Custody> base;
  1700. if (dirfd == AT_FDCWD) {
  1701. base = current_directory();
  1702. } else {
  1703. auto base_description = file_description(dirfd);
  1704. if (!base_description)
  1705. return -EBADF;
  1706. if (!base_description->is_directory())
  1707. return -ENOTDIR;
  1708. if (!base_description->custody())
  1709. return -EINVAL;
  1710. base = base_description->custody();
  1711. }
  1712. auto result = VFS::the().open(path.value(), options, mode & ~umask(), *base);
  1713. if (result.is_error())
  1714. return result.error();
  1715. auto description = result.value();
  1716. u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1717. m_fds[fd].set(move(description), fd_flags);
  1718. return fd;
  1719. }
  1720. int Process::alloc_fd(int first_candidate_fd)
  1721. {
  1722. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  1723. if (!m_fds[i])
  1724. return i;
  1725. }
  1726. return -EMFILE;
  1727. }
  1728. int Process::sys$pipe(int pipefd[2], int flags)
  1729. {
  1730. REQUIRE_PROMISE(stdio);
  1731. if (!validate_write_typed(pipefd))
  1732. return -EFAULT;
  1733. if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors())
  1734. return -EMFILE;
  1735. // Reject flags other than O_CLOEXEC.
  1736. if ((flags & O_CLOEXEC) != flags)
  1737. return -EINVAL;
  1738. u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1739. auto fifo = FIFO::create(m_uid);
  1740. int reader_fd = alloc_fd();
  1741. m_fds[reader_fd].set(fifo->open_direction(FIFO::Direction::Reader), fd_flags);
  1742. m_fds[reader_fd].description->set_readable(true);
  1743. copy_to_user(&pipefd[0], &reader_fd);
  1744. int writer_fd = alloc_fd();
  1745. m_fds[writer_fd].set(fifo->open_direction(FIFO::Direction::Writer), fd_flags);
  1746. m_fds[writer_fd].description->set_writable(true);
  1747. copy_to_user(&pipefd[1], &writer_fd);
  1748. return 0;
  1749. }
  1750. int Process::sys$killpg(int pgrp, int signum)
  1751. {
  1752. REQUIRE_PROMISE(proc);
  1753. if (signum < 1 || signum >= 32)
  1754. return -EINVAL;
  1755. if (pgrp < 0)
  1756. return -EINVAL;
  1757. InterruptDisabler disabler;
  1758. return do_killpg(pgrp, signum);
  1759. }
  1760. int Process::sys$setuid(uid_t uid)
  1761. {
  1762. REQUIRE_PROMISE(id);
  1763. if (uid != m_uid && !is_superuser())
  1764. return -EPERM;
  1765. m_uid = uid;
  1766. m_euid = uid;
  1767. return 0;
  1768. }
  1769. int Process::sys$setgid(gid_t gid)
  1770. {
  1771. REQUIRE_PROMISE(id);
  1772. if (gid != m_gid && !is_superuser())
  1773. return -EPERM;
  1774. m_gid = gid;
  1775. m_egid = gid;
  1776. return 0;
  1777. }
  1778. unsigned Process::sys$alarm(unsigned seconds)
  1779. {
  1780. REQUIRE_PROMISE(stdio);
  1781. unsigned previous_alarm_remaining = 0;
  1782. if (m_alarm_deadline && m_alarm_deadline > g_uptime) {
  1783. previous_alarm_remaining = (m_alarm_deadline - g_uptime) / TICKS_PER_SECOND;
  1784. }
  1785. if (!seconds) {
  1786. m_alarm_deadline = 0;
  1787. return previous_alarm_remaining;
  1788. }
  1789. m_alarm_deadline = g_uptime + seconds * TICKS_PER_SECOND;
  1790. return previous_alarm_remaining;
  1791. }
  1792. int Process::sys$uname(utsname* buf)
  1793. {
  1794. REQUIRE_PROMISE(stdio);
  1795. if (!validate_write_typed(buf))
  1796. return -EFAULT;
  1797. LOCKER(*s_hostname_lock);
  1798. if (s_hostname->length() + 1 > sizeof(utsname::nodename))
  1799. return -ENAMETOOLONG;
  1800. copy_to_user(buf->sysname, "SerenityOS", 11);
  1801. copy_to_user(buf->release, "1.0-dev", 8);
  1802. copy_to_user(buf->version, "FIXME", 6);
  1803. copy_to_user(buf->machine, "i686", 5);
  1804. copy_to_user(buf->nodename, s_hostname->characters(), s_hostname->length() + 1);
  1805. return 0;
  1806. }
  1807. KResult Process::do_kill(Process& process, int signal)
  1808. {
  1809. // FIXME: Allow sending SIGCONT to everyone in the process group.
  1810. // FIXME: Should setuid processes have some special treatment here?
  1811. if (!is_superuser() && m_euid != process.m_uid && m_uid != process.m_uid)
  1812. return KResult(-EPERM);
  1813. if (process.is_ring0() && signal == SIGKILL) {
  1814. kprintf("%s(%u) attempted to send SIGKILL to ring 0 process %s(%u)\n", name().characters(), m_pid, process.name().characters(), process.pid());
  1815. return KResult(-EPERM);
  1816. }
  1817. if (signal != 0)
  1818. process.send_signal(signal, this);
  1819. return KSuccess;
  1820. }
  1821. KResult Process::do_killpg(pid_t pgrp, int signal)
  1822. {
  1823. ASSERT(pgrp >= 0);
  1824. // Send the signal to all processes in the given group.
  1825. if (pgrp == 0) {
  1826. // Send the signal to our own pgrp.
  1827. pgrp = pgid();
  1828. }
  1829. bool group_was_empty = true;
  1830. bool any_succeeded = false;
  1831. KResult error = KSuccess;
  1832. Process::for_each_in_pgrp(pgrp, [&](auto& process) {
  1833. group_was_empty = false;
  1834. KResult res = do_kill(process, signal);
  1835. if (res.is_success())
  1836. any_succeeded = true;
  1837. else
  1838. error = res;
  1839. return IterationDecision::Continue;
  1840. });
  1841. if (group_was_empty)
  1842. return KResult(-ESRCH);
  1843. if (any_succeeded)
  1844. return KSuccess;
  1845. return error;
  1846. }
  1847. int Process::sys$kill(pid_t pid, int signal)
  1848. {
  1849. if (pid == m_pid)
  1850. REQUIRE_PROMISE(stdio);
  1851. else
  1852. REQUIRE_PROMISE(proc);
  1853. if (signal < 0 || signal >= 32)
  1854. return -EINVAL;
  1855. if (pid <= 0)
  1856. return do_killpg(-pid, signal);
  1857. if (pid == -1) {
  1858. // FIXME: Send to all processes.
  1859. return -ENOTIMPL;
  1860. }
  1861. if (pid == m_pid) {
  1862. if (signal == 0)
  1863. return 0;
  1864. if (!current->should_ignore_signal(signal)) {
  1865. current->send_signal(signal, this);
  1866. (void)current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
  1867. }
  1868. return 0;
  1869. }
  1870. InterruptDisabler disabler;
  1871. auto* peer = Process::from_pid(pid);
  1872. if (!peer)
  1873. return -ESRCH;
  1874. return do_kill(*peer, signal);
  1875. }
  1876. int Process::sys$usleep(useconds_t usec)
  1877. {
  1878. REQUIRE_PROMISE(stdio);
  1879. if (!usec)
  1880. return 0;
  1881. u64 wakeup_time = current->sleep(usec / 1000);
  1882. if (wakeup_time > g_uptime)
  1883. return -EINTR;
  1884. return 0;
  1885. }
  1886. int Process::sys$sleep(unsigned seconds)
  1887. {
  1888. REQUIRE_PROMISE(stdio);
  1889. if (!seconds)
  1890. return 0;
  1891. u64 wakeup_time = current->sleep(seconds * TICKS_PER_SECOND);
  1892. if (wakeup_time > g_uptime) {
  1893. u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
  1894. return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
  1895. }
  1896. return 0;
  1897. }
  1898. timeval kgettimeofday()
  1899. {
  1900. return const_cast<const timeval&>(((KernelInfoPage*)s_info_page_address_for_kernel.as_ptr())->now);
  1901. }
  1902. void kgettimeofday(timeval& tv)
  1903. {
  1904. tv = kgettimeofday();
  1905. }
  1906. int Process::sys$gettimeofday(timeval* tv)
  1907. {
  1908. REQUIRE_PROMISE(stdio);
  1909. if (!validate_write_typed(tv))
  1910. return -EFAULT;
  1911. *tv = kgettimeofday();
  1912. return 0;
  1913. }
  1914. uid_t Process::sys$getuid()
  1915. {
  1916. REQUIRE_PROMISE(stdio);
  1917. return m_uid;
  1918. }
  1919. gid_t Process::sys$getgid()
  1920. {
  1921. REQUIRE_PROMISE(stdio);
  1922. return m_gid;
  1923. }
  1924. uid_t Process::sys$geteuid()
  1925. {
  1926. REQUIRE_PROMISE(stdio);
  1927. return m_euid;
  1928. }
  1929. gid_t Process::sys$getegid()
  1930. {
  1931. REQUIRE_PROMISE(stdio);
  1932. return m_egid;
  1933. }
  1934. pid_t Process::sys$getpid()
  1935. {
  1936. REQUIRE_PROMISE(stdio);
  1937. return m_pid;
  1938. }
  1939. pid_t Process::sys$getppid()
  1940. {
  1941. REQUIRE_PROMISE(stdio);
  1942. return m_ppid;
  1943. }
  1944. mode_t Process::sys$umask(mode_t mask)
  1945. {
  1946. REQUIRE_PROMISE(stdio);
  1947. auto old_mask = m_umask;
  1948. m_umask = mask & 0777;
  1949. return old_mask;
  1950. }
  1951. int Process::reap(Process& process)
  1952. {
  1953. int exit_status;
  1954. {
  1955. InterruptDisabler disabler;
  1956. exit_status = (process.m_termination_status << 8) | process.m_termination_signal;
  1957. if (process.ppid()) {
  1958. auto* parent = Process::from_pid(process.ppid());
  1959. if (parent) {
  1960. parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
  1961. parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
  1962. }
  1963. }
  1964. dbg() << "Reaping process " << process;
  1965. ASSERT(process.is_dead());
  1966. g_processes->remove(&process);
  1967. }
  1968. delete &process;
  1969. return exit_status;
  1970. }
  1971. pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
  1972. {
  1973. REQUIRE_PROMISE(stdio);
  1974. dbg() << "sys$waitpid(" << waitee << ", " << wstatus << ", " << options << ")";
  1975. if (!options) {
  1976. // FIXME: This can't be right.. can it? Figure out how this should actually work.
  1977. options = WEXITED;
  1978. }
  1979. if (wstatus && !validate_write_typed(wstatus))
  1980. return -EFAULT;
  1981. int exit_status = 0;
  1982. {
  1983. InterruptDisabler disabler;
  1984. if (waitee != -1 && !Process::from_pid(waitee))
  1985. return -ECHILD;
  1986. }
  1987. if (options & WNOHANG) {
  1988. // FIXME: Figure out what WNOHANG should do with stopped children.
  1989. if (waitee == -1) {
  1990. pid_t reaped_pid = 0;
  1991. InterruptDisabler disabler;
  1992. for_each_child([&reaped_pid, &exit_status](Process& process) {
  1993. if (process.is_dead()) {
  1994. reaped_pid = process.pid();
  1995. exit_status = reap(process);
  1996. }
  1997. return IterationDecision::Continue;
  1998. });
  1999. return reaped_pid;
  2000. } else {
  2001. ASSERT(waitee > 0); // FIXME: Implement other PID specs.
  2002. InterruptDisabler disabler;
  2003. auto* waitee_process = Process::from_pid(waitee);
  2004. if (!waitee_process)
  2005. return -ECHILD;
  2006. if (waitee_process->is_dead()) {
  2007. exit_status = reap(*waitee_process);
  2008. return waitee;
  2009. }
  2010. return 0;
  2011. }
  2012. }
  2013. pid_t waitee_pid = waitee;
  2014. if (current->block<Thread::WaitBlocker>(options, waitee_pid) != Thread::BlockResult::WokeNormally)
  2015. return -EINTR;
  2016. InterruptDisabler disabler;
  2017. // NOTE: If waitee was -1, m_waitee_pid will have been filled in by the scheduler.
  2018. Process* waitee_process = Process::from_pid(waitee_pid);
  2019. if (!waitee_process)
  2020. return -ECHILD;
  2021. auto* waitee_thread = Thread::from_tid(waitee_pid);
  2022. if (!waitee_thread)
  2023. return -ECHILD;
  2024. ASSERT(waitee_process);
  2025. if (waitee_process->is_dead()) {
  2026. exit_status = reap(*waitee_process);
  2027. } else {
  2028. ASSERT(waitee_thread->state() == Thread::State::Stopped);
  2029. exit_status = (waitee_thread->m_stop_signal << 8) | 0x7f;
  2030. }
  2031. if (wstatus)
  2032. copy_to_user(wstatus, &exit_status);
  2033. return waitee_pid;
  2034. }
  2035. bool Process::validate_read_from_kernel(VirtualAddress vaddr, ssize_t size) const
  2036. {
  2037. if (vaddr.is_null())
  2038. return false;
  2039. return MM.validate_kernel_read(*this, vaddr, size);
  2040. }
  2041. bool Process::validate_read(const void* address, ssize_t size) const
  2042. {
  2043. if (!size)
  2044. return false;
  2045. return MM.validate_user_read(*this, VirtualAddress(address), size);
  2046. }
  2047. bool Process::validate_write(void* address, ssize_t size) const
  2048. {
  2049. if (!size)
  2050. return false;
  2051. return MM.validate_user_write(*this, VirtualAddress(address), size);
  2052. }
  2053. pid_t Process::sys$getsid(pid_t pid)
  2054. {
  2055. REQUIRE_PROMISE(stdio);
  2056. if (pid == 0)
  2057. return m_sid;
  2058. InterruptDisabler disabler;
  2059. auto* process = Process::from_pid(pid);
  2060. if (!process)
  2061. return -ESRCH;
  2062. if (m_sid != process->m_sid)
  2063. return -EPERM;
  2064. return process->m_sid;
  2065. }
  2066. pid_t Process::sys$setsid()
  2067. {
  2068. REQUIRE_PROMISE(proc);
  2069. InterruptDisabler disabler;
  2070. bool found_process_with_same_pgid_as_my_pid = false;
  2071. Process::for_each_in_pgrp(pid(), [&](auto&) {
  2072. found_process_with_same_pgid_as_my_pid = true;
  2073. return IterationDecision::Break;
  2074. });
  2075. if (found_process_with_same_pgid_as_my_pid)
  2076. return -EPERM;
  2077. m_sid = m_pid;
  2078. m_pgid = m_pid;
  2079. m_tty = nullptr;
  2080. return m_sid;
  2081. }
  2082. pid_t Process::sys$getpgid(pid_t pid)
  2083. {
  2084. REQUIRE_PROMISE(stdio);
  2085. if (pid == 0)
  2086. return m_pgid;
  2087. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2088. auto* process = Process::from_pid(pid);
  2089. if (!process)
  2090. return -ESRCH;
  2091. return process->m_pgid;
  2092. }
  2093. pid_t Process::sys$getpgrp()
  2094. {
  2095. REQUIRE_PROMISE(stdio);
  2096. return m_pgid;
  2097. }
  2098. static pid_t get_sid_from_pgid(pid_t pgid)
  2099. {
  2100. InterruptDisabler disabler;
  2101. auto* group_leader = Process::from_pid(pgid);
  2102. if (!group_leader)
  2103. return -1;
  2104. return group_leader->sid();
  2105. }
  2106. int Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  2107. {
  2108. REQUIRE_PROMISE(proc);
  2109. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2110. pid_t pid = specified_pid ? specified_pid : m_pid;
  2111. if (specified_pgid < 0) {
  2112. // The value of the pgid argument is less than 0, or is not a value supported by the implementation.
  2113. return -EINVAL;
  2114. }
  2115. auto* process = Process::from_pid(pid);
  2116. if (!process)
  2117. return -ESRCH;
  2118. if (process != this && process->ppid() != m_pid) {
  2119. // The value of the pid argument does not match the process ID
  2120. // of the calling process or of a child process of the calling process.
  2121. return -ESRCH;
  2122. }
  2123. if (process->pid() == process->sid()) {
  2124. // The process indicated by the pid argument is a session leader.
  2125. return -EPERM;
  2126. }
  2127. if (process->ppid() == m_pid && process->sid() != sid()) {
  2128. // The value of the pid argument matches the process ID of a child
  2129. // process of the calling process and the child process is not in
  2130. // the same session as the calling process.
  2131. return -EPERM;
  2132. }
  2133. pid_t new_pgid = specified_pgid ? specified_pgid : process->m_pid;
  2134. pid_t current_sid = get_sid_from_pgid(process->m_pgid);
  2135. pid_t new_sid = get_sid_from_pgid(new_pgid);
  2136. if (current_sid != new_sid) {
  2137. // Can't move a process between sessions.
  2138. return -EPERM;
  2139. }
  2140. // FIXME: There are more EPERM conditions to check for here..
  2141. process->m_pgid = new_pgid;
  2142. return 0;
  2143. }
  2144. int Process::sys$ioctl(int fd, unsigned request, unsigned arg)
  2145. {
  2146. auto description = file_description(fd);
  2147. if (!description)
  2148. return -EBADF;
  2149. SmapDisabler disabler;
  2150. return description->file().ioctl(*description, request, arg);
  2151. }
  2152. int Process::sys$getdtablesize()
  2153. {
  2154. REQUIRE_PROMISE(stdio);
  2155. return m_max_open_file_descriptors;
  2156. }
  2157. int Process::sys$dup(int old_fd)
  2158. {
  2159. REQUIRE_PROMISE(stdio);
  2160. auto description = file_description(old_fd);
  2161. if (!description)
  2162. return -EBADF;
  2163. int new_fd = alloc_fd();
  2164. if (new_fd < 0)
  2165. return new_fd;
  2166. m_fds[new_fd].set(*description);
  2167. return new_fd;
  2168. }
  2169. int Process::sys$dup2(int old_fd, int new_fd)
  2170. {
  2171. REQUIRE_PROMISE(stdio);
  2172. auto description = file_description(old_fd);
  2173. if (!description)
  2174. return -EBADF;
  2175. if (new_fd < 0 || new_fd >= m_max_open_file_descriptors)
  2176. return -EINVAL;
  2177. m_fds[new_fd].set(*description);
  2178. return new_fd;
  2179. }
  2180. int Process::sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
  2181. {
  2182. REQUIRE_PROMISE(stdio);
  2183. if (old_set) {
  2184. if (!validate_write_typed(old_set))
  2185. return -EFAULT;
  2186. copy_to_user(old_set, &current->m_signal_mask);
  2187. }
  2188. if (set) {
  2189. if (!validate_read_typed(set))
  2190. return -EFAULT;
  2191. sigset_t set_value;
  2192. copy_from_user(&set_value, set);
  2193. switch (how) {
  2194. case SIG_BLOCK:
  2195. current->m_signal_mask &= ~set_value;
  2196. break;
  2197. case SIG_UNBLOCK:
  2198. current->m_signal_mask |= set_value;
  2199. break;
  2200. case SIG_SETMASK:
  2201. current->m_signal_mask = set_value;
  2202. break;
  2203. default:
  2204. return -EINVAL;
  2205. }
  2206. }
  2207. return 0;
  2208. }
  2209. int Process::sys$sigpending(sigset_t* set)
  2210. {
  2211. REQUIRE_PROMISE(stdio);
  2212. if (!validate_write_typed(set))
  2213. return -EFAULT;
  2214. copy_to_user(set, &current->m_pending_signals);
  2215. return 0;
  2216. }
  2217. int Process::sys$sigaction(int signum, const sigaction* act, sigaction* old_act)
  2218. {
  2219. REQUIRE_PROMISE(stdio);
  2220. if (signum < 1 || signum >= 32 || signum == SIGKILL || signum == SIGSTOP)
  2221. return -EINVAL;
  2222. if (!validate_read_typed(act))
  2223. return -EFAULT;
  2224. InterruptDisabler disabler; // FIXME: This should use a narrower lock. Maybe a way to ignore signals temporarily?
  2225. auto& action = current->m_signal_action_data[signum];
  2226. if (old_act) {
  2227. if (!validate_write_typed(old_act))
  2228. return -EFAULT;
  2229. copy_to_user(&old_act->sa_flags, &action.flags);
  2230. copy_to_user(&old_act->sa_sigaction, &action.handler_or_sigaction, sizeof(action.handler_or_sigaction));
  2231. }
  2232. copy_from_user(&action.flags, &act->sa_flags);
  2233. copy_from_user(&action.handler_or_sigaction, &act->sa_sigaction, sizeof(action.handler_or_sigaction));
  2234. return 0;
  2235. }
  2236. int Process::sys$getgroups(ssize_t count, gid_t* gids)
  2237. {
  2238. REQUIRE_PROMISE(stdio);
  2239. if (count < 0)
  2240. return -EINVAL;
  2241. if (!count)
  2242. return m_extra_gids.size();
  2243. if (count != (int)m_extra_gids.size())
  2244. return -EINVAL;
  2245. if (!validate_write_typed(gids, m_extra_gids.size()))
  2246. return -EFAULT;
  2247. size_t i = 0;
  2248. SmapDisabler disabler;
  2249. for (auto gid : m_extra_gids)
  2250. gids[i++] = gid;
  2251. return 0;
  2252. }
  2253. int Process::sys$setgroups(ssize_t count, const gid_t* gids)
  2254. {
  2255. REQUIRE_PROMISE(id);
  2256. if (count < 0)
  2257. return -EINVAL;
  2258. if (!is_superuser())
  2259. return -EPERM;
  2260. if (count && !validate_read(gids, count))
  2261. return -EFAULT;
  2262. m_extra_gids.clear();
  2263. SmapDisabler disabler;
  2264. for (int i = 0; i < count; ++i) {
  2265. if (gids[i] == m_gid)
  2266. continue;
  2267. m_extra_gids.set(gids[i]);
  2268. }
  2269. return 0;
  2270. }
  2271. int Process::sys$mkdir(const char* user_path, size_t path_length, mode_t mode)
  2272. {
  2273. REQUIRE_PROMISE(cpath);
  2274. auto path = get_syscall_path_argument(user_path, path_length);
  2275. if (path.is_error())
  2276. return path.error();
  2277. return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory());
  2278. }
  2279. int Process::sys$realpath(const Syscall::SC_realpath_params* user_params)
  2280. {
  2281. REQUIRE_PROMISE(rpath);
  2282. Syscall::SC_realpath_params params;
  2283. if (!validate_read_and_copy_typed(&params, user_params))
  2284. return -EFAULT;
  2285. if (!validate_write(params.buffer.data, params.buffer.size))
  2286. return -EFAULT;
  2287. auto path = get_syscall_path_argument(params.path);
  2288. if (path.is_error())
  2289. return path.error();
  2290. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  2291. if (custody_or_error.is_error())
  2292. return custody_or_error.error();
  2293. auto& custody = custody_or_error.value();
  2294. auto absolute_path = custody->absolute_path();
  2295. if (absolute_path.length() + 1 > params.buffer.size)
  2296. return -ENAMETOOLONG;
  2297. copy_to_user(params.buffer.data, absolute_path.characters(), absolute_path.length() + 1);
  2298. return 0;
  2299. };
  2300. clock_t Process::sys$times(tms* times)
  2301. {
  2302. REQUIRE_PROMISE(stdio);
  2303. if (!validate_write_typed(times))
  2304. return -EFAULT;
  2305. copy_to_user(&times->tms_utime, &m_ticks_in_user);
  2306. copy_to_user(&times->tms_stime, &m_ticks_in_kernel);
  2307. copy_to_user(&times->tms_cutime, &m_ticks_in_user_for_dead_children);
  2308. copy_to_user(&times->tms_cstime, &m_ticks_in_kernel_for_dead_children);
  2309. return g_uptime & 0x7fffffff;
  2310. }
  2311. int Process::sys$select(const Syscall::SC_select_params* params)
  2312. {
  2313. REQUIRE_PROMISE(stdio);
  2314. // FIXME: Return -EINVAL if timeout is invalid.
  2315. if (!validate_read_typed(params))
  2316. return -EFAULT;
  2317. SmapDisabler disabler;
  2318. int nfds = params->nfds;
  2319. fd_set* readfds = params->readfds;
  2320. fd_set* writefds = params->writefds;
  2321. fd_set* exceptfds = params->exceptfds;
  2322. timeval* timeout = params->timeout;
  2323. if (writefds && !validate_write_typed(writefds))
  2324. return -EFAULT;
  2325. if (readfds && !validate_write_typed(readfds))
  2326. return -EFAULT;
  2327. if (exceptfds && !validate_write_typed(exceptfds))
  2328. return -EFAULT;
  2329. if (timeout && !validate_read_typed(timeout))
  2330. return -EFAULT;
  2331. if (nfds < 0)
  2332. return -EINVAL;
  2333. timeval computed_timeout;
  2334. bool select_has_timeout = false;
  2335. if (timeout && (timeout->tv_sec || timeout->tv_usec)) {
  2336. timeval_add(kgettimeofday(), *timeout, computed_timeout);
  2337. select_has_timeout = true;
  2338. }
  2339. Thread::SelectBlocker::FDVector rfds;
  2340. Thread::SelectBlocker::FDVector wfds;
  2341. Thread::SelectBlocker::FDVector efds;
  2342. auto transfer_fds = [&](auto* fds, auto& vector) -> int {
  2343. vector.clear_with_capacity();
  2344. if (!fds)
  2345. return 0;
  2346. for (int fd = 0; fd < nfds; ++fd) {
  2347. if (FD_ISSET(fd, fds)) {
  2348. if (!file_description(fd)) {
  2349. dbg() << "sys$select: Bad fd number " << fd;
  2350. return -EBADF;
  2351. }
  2352. vector.append(fd);
  2353. }
  2354. }
  2355. return 0;
  2356. };
  2357. if (int error = transfer_fds(writefds, wfds))
  2358. return error;
  2359. if (int error = transfer_fds(readfds, rfds))
  2360. return error;
  2361. if (int error = transfer_fds(exceptfds, efds))
  2362. return error;
  2363. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2364. dbgprintf("%s<%u> selecting on (read:%u, write:%u), timeout=%p\n", name().characters(), pid(), rfds.size(), wfds.size(), timeout);
  2365. #endif
  2366. if (!timeout || select_has_timeout) {
  2367. if (current->block<Thread::SelectBlocker>(computed_timeout, select_has_timeout, rfds, wfds, efds) != Thread::BlockResult::WokeNormally)
  2368. return -EINTR;
  2369. }
  2370. int marked_fd_count = 0;
  2371. auto mark_fds = [&](auto* fds, auto& vector, auto should_mark) {
  2372. if (!fds)
  2373. return;
  2374. FD_ZERO(fds);
  2375. for (int fd : vector) {
  2376. if (auto description = file_description(fd); description && should_mark(*description)) {
  2377. FD_SET(fd, fds);
  2378. ++marked_fd_count;
  2379. }
  2380. }
  2381. };
  2382. mark_fds(readfds, rfds, [](auto& description) { return description.can_read(); });
  2383. mark_fds(writefds, wfds, [](auto& description) { return description.can_write(); });
  2384. // FIXME: We should also mark exceptfds as appropriate.
  2385. return marked_fd_count;
  2386. }
  2387. int Process::sys$poll(pollfd* fds, int nfds, int timeout)
  2388. {
  2389. REQUIRE_PROMISE(stdio);
  2390. if (!validate_read_typed(fds))
  2391. return -EFAULT;
  2392. SmapDisabler disabler;
  2393. Thread::SelectBlocker::FDVector rfds;
  2394. Thread::SelectBlocker::FDVector wfds;
  2395. for (int i = 0; i < nfds; ++i) {
  2396. if (fds[i].events & POLLIN)
  2397. rfds.append(fds[i].fd);
  2398. if (fds[i].events & POLLOUT)
  2399. wfds.append(fds[i].fd);
  2400. }
  2401. timeval actual_timeout;
  2402. bool has_timeout = false;
  2403. if (timeout >= 0) {
  2404. // poll is in ms, we want s/us.
  2405. struct timeval tvtimeout;
  2406. tvtimeout.tv_sec = 0;
  2407. while (timeout >= 1000) {
  2408. tvtimeout.tv_sec += 1;
  2409. timeout -= 1000;
  2410. }
  2411. tvtimeout.tv_usec = timeout * 1000;
  2412. timeval_add(kgettimeofday(), tvtimeout, actual_timeout);
  2413. has_timeout = true;
  2414. }
  2415. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2416. dbgprintf("%s<%u> polling on (read:%u, write:%u), timeout=%d\n", name().characters(), pid(), rfds.size(), wfds.size(), timeout);
  2417. #endif
  2418. if (has_timeout || timeout < 0) {
  2419. if (current->block<Thread::SelectBlocker>(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector()) != Thread::BlockResult::WokeNormally)
  2420. return -EINTR;
  2421. }
  2422. int fds_with_revents = 0;
  2423. for (int i = 0; i < nfds; ++i) {
  2424. auto description = file_description(fds[i].fd);
  2425. if (!description) {
  2426. fds[i].revents = POLLNVAL;
  2427. continue;
  2428. }
  2429. fds[i].revents = 0;
  2430. if (fds[i].events & POLLIN && description->can_read())
  2431. fds[i].revents |= POLLIN;
  2432. if (fds[i].events & POLLOUT && description->can_write())
  2433. fds[i].revents |= POLLOUT;
  2434. if (fds[i].revents)
  2435. ++fds_with_revents;
  2436. }
  2437. return fds_with_revents;
  2438. }
  2439. Custody& Process::current_directory()
  2440. {
  2441. if (!m_cwd)
  2442. m_cwd = VFS::the().root_custody();
  2443. return *m_cwd;
  2444. }
  2445. int Process::sys$link(const Syscall::SC_link_params* user_params)
  2446. {
  2447. REQUIRE_PROMISE(cpath);
  2448. Syscall::SC_link_params params;
  2449. if (!validate_read_and_copy_typed(&params, user_params))
  2450. return -EFAULT;
  2451. auto old_path = validate_and_copy_string_from_user(params.old_path);
  2452. auto new_path = validate_and_copy_string_from_user(params.new_path);
  2453. if (old_path.is_null() || new_path.is_null())
  2454. return -EFAULT;
  2455. return VFS::the().link(old_path, new_path, current_directory());
  2456. }
  2457. int Process::sys$unlink(const char* user_path, size_t path_length)
  2458. {
  2459. REQUIRE_PROMISE(cpath);
  2460. if (!validate_read(user_path, path_length))
  2461. return -EFAULT;
  2462. auto path = get_syscall_path_argument(user_path, path_length);
  2463. if (path.is_error())
  2464. return path.error();
  2465. return VFS::the().unlink(path.value(), current_directory());
  2466. }
  2467. int Process::sys$symlink(const Syscall::SC_symlink_params* user_params)
  2468. {
  2469. REQUIRE_PROMISE(cpath);
  2470. Syscall::SC_symlink_params params;
  2471. if (!validate_read_and_copy_typed(&params, user_params))
  2472. return -EFAULT;
  2473. auto target = get_syscall_path_argument(params.target);
  2474. if (target.is_error())
  2475. return target.error();
  2476. auto linkpath = get_syscall_path_argument(params.linkpath);
  2477. if (linkpath.is_error())
  2478. return linkpath.error();
  2479. return VFS::the().symlink(target.value(), linkpath.value(), current_directory());
  2480. }
  2481. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  2482. {
  2483. if (path_length == 0)
  2484. return KResult(-EINVAL);
  2485. if (path_length > PATH_MAX)
  2486. return KResult(-ENAMETOOLONG);
  2487. if (!validate_read(user_path, path_length))
  2488. return KResult(-EFAULT);
  2489. return copy_string_from_user(user_path, path_length);
  2490. }
  2491. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  2492. {
  2493. return get_syscall_path_argument(path.characters, path.length);
  2494. }
  2495. int Process::sys$rmdir(const char* user_path, size_t path_length)
  2496. {
  2497. REQUIRE_PROMISE(cpath);
  2498. auto path = get_syscall_path_argument(user_path, path_length);
  2499. if (path.is_error())
  2500. return path.error();
  2501. return VFS::the().rmdir(path.value(), current_directory());
  2502. }
  2503. int Process::sys$chmod(const char* user_path, size_t path_length, mode_t mode)
  2504. {
  2505. REQUIRE_PROMISE(fattr);
  2506. auto path = get_syscall_path_argument(user_path, path_length);
  2507. if (path.is_error())
  2508. return path.error();
  2509. return VFS::the().chmod(path.value(), mode, current_directory());
  2510. }
  2511. int Process::sys$fchmod(int fd, mode_t mode)
  2512. {
  2513. REQUIRE_PROMISE(fattr);
  2514. auto description = file_description(fd);
  2515. if (!description)
  2516. return -EBADF;
  2517. return description->chmod(mode);
  2518. }
  2519. int Process::sys$fchown(int fd, uid_t uid, gid_t gid)
  2520. {
  2521. REQUIRE_PROMISE(chown);
  2522. auto description = file_description(fd);
  2523. if (!description)
  2524. return -EBADF;
  2525. return description->chown(uid, gid);
  2526. }
  2527. int Process::sys$chown(const Syscall::SC_chown_params* user_params)
  2528. {
  2529. REQUIRE_PROMISE(chown);
  2530. Syscall::SC_chown_params params;
  2531. if (!validate_read_and_copy_typed(&params, user_params))
  2532. return -EFAULT;
  2533. auto path = get_syscall_path_argument(params.path);
  2534. if (path.is_error())
  2535. return path.error();
  2536. return VFS::the().chown(path.value(), params.uid, params.gid, current_directory());
  2537. }
  2538. void Process::finalize()
  2539. {
  2540. ASSERT(current == g_finalizer);
  2541. dbg() << "Finalizing process " << *this;
  2542. m_fds.clear();
  2543. m_tty = nullptr;
  2544. m_executable = nullptr;
  2545. m_cwd = nullptr;
  2546. m_root_directory = nullptr;
  2547. m_root_directory_relative_to_global_root = nullptr;
  2548. m_elf_loader = nullptr;
  2549. disown_all_shared_buffers();
  2550. {
  2551. InterruptDisabler disabler;
  2552. if (auto* parent_thread = Thread::from_tid(m_ppid)) {
  2553. if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
  2554. // NOTE: If the parent doesn't care about this process, let it go.
  2555. m_ppid = 0;
  2556. } else {
  2557. parent_thread->send_signal(SIGCHLD, this);
  2558. }
  2559. }
  2560. }
  2561. m_dead = true;
  2562. }
  2563. void Process::die()
  2564. {
  2565. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  2566. // getting an EOF when the last process using the slave PTY dies.
  2567. // If the master PTY owner relies on an EOF to know when to wait() on a
  2568. // slave owner, we have to allow the PTY pair to be torn down.
  2569. m_tty = nullptr;
  2570. if (m_tracer)
  2571. m_tracer->set_dead();
  2572. {
  2573. // Tell the threads to unwind and die.
  2574. InterruptDisabler disabler;
  2575. for_each_thread([](Thread& thread) {
  2576. thread.set_should_die();
  2577. return IterationDecision::Continue;
  2578. });
  2579. }
  2580. }
  2581. size_t Process::amount_dirty_private() const
  2582. {
  2583. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  2584. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  2585. // That's probably a situation that needs to be looked at in general.
  2586. size_t amount = 0;
  2587. for (auto& region : m_regions) {
  2588. if (!region.is_shared())
  2589. amount += region.amount_dirty();
  2590. }
  2591. return amount;
  2592. }
  2593. size_t Process::amount_clean_inode() const
  2594. {
  2595. HashTable<const InodeVMObject*> vmobjects;
  2596. for (auto& region : m_regions) {
  2597. if (region.vmobject().is_inode())
  2598. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  2599. }
  2600. size_t amount = 0;
  2601. for (auto& vmobject : vmobjects)
  2602. amount += vmobject->amount_clean();
  2603. return amount;
  2604. }
  2605. size_t Process::amount_virtual() const
  2606. {
  2607. size_t amount = 0;
  2608. for (auto& region : m_regions) {
  2609. amount += region.size();
  2610. }
  2611. return amount;
  2612. }
  2613. size_t Process::amount_resident() const
  2614. {
  2615. // FIXME: This will double count if multiple regions use the same physical page.
  2616. size_t amount = 0;
  2617. for (auto& region : m_regions) {
  2618. amount += region.amount_resident();
  2619. }
  2620. return amount;
  2621. }
  2622. size_t Process::amount_shared() const
  2623. {
  2624. // FIXME: This will double count if multiple regions use the same physical page.
  2625. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  2626. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  2627. // so that every Region contributes +1 ref to each of its PhysicalPages.
  2628. size_t amount = 0;
  2629. for (auto& region : m_regions) {
  2630. amount += region.amount_shared();
  2631. }
  2632. return amount;
  2633. }
  2634. size_t Process::amount_purgeable_volatile() const
  2635. {
  2636. size_t amount = 0;
  2637. for (auto& region : m_regions) {
  2638. if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2639. amount += region.amount_resident();
  2640. }
  2641. return amount;
  2642. }
  2643. size_t Process::amount_purgeable_nonvolatile() const
  2644. {
  2645. size_t amount = 0;
  2646. for (auto& region : m_regions) {
  2647. if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2648. amount += region.amount_resident();
  2649. }
  2650. return amount;
  2651. }
  2652. #define REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain) \
  2653. do { \
  2654. if (domain == AF_INET) \
  2655. REQUIRE_PROMISE(inet); \
  2656. else if (domain == AF_LOCAL) \
  2657. REQUIRE_PROMISE(unix); \
  2658. } while (0)
  2659. int Process::sys$socket(int domain, int type, int protocol)
  2660. {
  2661. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain);
  2662. if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
  2663. return -EACCES;
  2664. int fd = alloc_fd();
  2665. if (fd < 0)
  2666. return fd;
  2667. auto result = Socket::create(domain, type, protocol);
  2668. if (result.is_error())
  2669. return result.error();
  2670. auto description = FileDescription::create(*result.value());
  2671. description->set_readable(true);
  2672. description->set_writable(true);
  2673. unsigned flags = 0;
  2674. if (type & SOCK_CLOEXEC)
  2675. flags |= FD_CLOEXEC;
  2676. if (type & SOCK_NONBLOCK)
  2677. description->set_blocking(false);
  2678. m_fds[fd].set(move(description), flags);
  2679. return fd;
  2680. }
  2681. int Process::sys$bind(int sockfd, const sockaddr* address, socklen_t address_length)
  2682. {
  2683. if (!validate_read(address, address_length))
  2684. return -EFAULT;
  2685. auto description = file_description(sockfd);
  2686. if (!description)
  2687. return -EBADF;
  2688. if (!description->is_socket())
  2689. return -ENOTSOCK;
  2690. auto& socket = *description->socket();
  2691. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2692. return socket.bind(address, address_length);
  2693. }
  2694. int Process::sys$listen(int sockfd, int backlog)
  2695. {
  2696. auto description = file_description(sockfd);
  2697. if (!description)
  2698. return -EBADF;
  2699. if (!description->is_socket())
  2700. return -ENOTSOCK;
  2701. auto& socket = *description->socket();
  2702. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2703. if (socket.is_connected())
  2704. return -EINVAL;
  2705. return socket.listen(backlog);
  2706. }
  2707. int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* address_size)
  2708. {
  2709. REQUIRE_PROMISE(accept);
  2710. if (!validate_write_typed(address_size))
  2711. return -EFAULT;
  2712. SmapDisabler disabler;
  2713. if (!validate_write(address, *address_size))
  2714. return -EFAULT;
  2715. int accepted_socket_fd = alloc_fd();
  2716. if (accepted_socket_fd < 0)
  2717. return accepted_socket_fd;
  2718. auto accepting_socket_description = file_description(accepting_socket_fd);
  2719. if (!accepting_socket_description)
  2720. return -EBADF;
  2721. if (!accepting_socket_description->is_socket())
  2722. return -ENOTSOCK;
  2723. auto& socket = *accepting_socket_description->socket();
  2724. if (!socket.can_accept()) {
  2725. if (accepting_socket_description->is_blocking()) {
  2726. if (current->block<Thread::AcceptBlocker>(*accepting_socket_description) != Thread::BlockResult::WokeNormally)
  2727. return -EINTR;
  2728. } else {
  2729. return -EAGAIN;
  2730. }
  2731. }
  2732. auto accepted_socket = socket.accept();
  2733. ASSERT(accepted_socket);
  2734. bool success = accepted_socket->get_peer_address(address, address_size);
  2735. ASSERT(success);
  2736. auto accepted_socket_description = FileDescription::create(*accepted_socket);
  2737. accepted_socket_description->set_readable(true);
  2738. accepted_socket_description->set_writable(true);
  2739. // NOTE: The accepted socket inherits fd flags from the accepting socket.
  2740. // I'm not sure if this matches other systems but it makes sense to me.
  2741. accepted_socket_description->set_blocking(accepting_socket_description->is_blocking());
  2742. m_fds[accepted_socket_fd].set(move(accepted_socket_description), m_fds[accepting_socket_fd].flags);
  2743. // NOTE: Moving this state to Completed is what causes connect() to unblock on the client side.
  2744. accepted_socket->set_setup_state(Socket::SetupState::Completed);
  2745. return accepted_socket_fd;
  2746. }
  2747. int Process::sys$connect(int sockfd, const sockaddr* address, socklen_t address_size)
  2748. {
  2749. if (!validate_read(address, address_size))
  2750. return -EFAULT;
  2751. int fd = alloc_fd();
  2752. if (fd < 0)
  2753. return fd;
  2754. auto description = file_description(sockfd);
  2755. if (!description)
  2756. return -EBADF;
  2757. if (!description->is_socket())
  2758. return -ENOTSOCK;
  2759. auto& socket = *description->socket();
  2760. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2761. SmapDisabler disabler;
  2762. return socket.connect(*description, address, address_size, description->is_blocking() ? ShouldBlock::Yes : ShouldBlock::No);
  2763. }
  2764. ssize_t Process::sys$sendto(const Syscall::SC_sendto_params* user_params)
  2765. {
  2766. REQUIRE_PROMISE(stdio);
  2767. Syscall::SC_sendto_params params;
  2768. if (!validate_read_and_copy_typed(&params, user_params))
  2769. return -EFAULT;
  2770. int flags = params.flags;
  2771. const sockaddr* addr = params.addr;
  2772. socklen_t addr_length = params.addr_length;
  2773. if (!validate(params.data))
  2774. return -EFAULT;
  2775. if (addr && !validate_read(addr, addr_length))
  2776. return -EFAULT;
  2777. auto description = file_description(params.sockfd);
  2778. if (!description)
  2779. return -EBADF;
  2780. if (!description->is_socket())
  2781. return -ENOTSOCK;
  2782. SmapDisabler disabler;
  2783. auto& socket = *description->socket();
  2784. return socket.sendto(*description, params.data.data, params.data.size, flags, addr, addr_length);
  2785. }
  2786. ssize_t Process::sys$recvfrom(const Syscall::SC_recvfrom_params* user_params)
  2787. {
  2788. REQUIRE_PROMISE(stdio);
  2789. Syscall::SC_recvfrom_params params;
  2790. if (!validate_read_and_copy_typed(&params, user_params))
  2791. return -EFAULT;
  2792. int flags = params.flags;
  2793. sockaddr* addr = params.addr;
  2794. socklen_t* addr_length = params.addr_length;
  2795. SmapDisabler disabler;
  2796. if (!validate(params.buffer))
  2797. return -EFAULT;
  2798. if (addr_length) {
  2799. if (!validate_write_typed(addr_length))
  2800. return -EFAULT;
  2801. if (!validate_write(addr, *addr_length))
  2802. return -EFAULT;
  2803. } else if (addr) {
  2804. return -EINVAL;
  2805. }
  2806. auto description = file_description(params.sockfd);
  2807. if (!description)
  2808. return -EBADF;
  2809. if (!description->is_socket())
  2810. return -ENOTSOCK;
  2811. auto& socket = *description->socket();
  2812. bool original_blocking = description->is_blocking();
  2813. if (flags & MSG_DONTWAIT)
  2814. description->set_blocking(false);
  2815. auto nrecv = socket.recvfrom(*description, params.buffer.data, params.buffer.size, flags, addr, addr_length);
  2816. if (flags & MSG_DONTWAIT)
  2817. description->set_blocking(original_blocking);
  2818. return nrecv;
  2819. }
  2820. template<bool sockname, typename Params>
  2821. int Process::get_sock_or_peer_name(const Params& params)
  2822. {
  2823. socklen_t addrlen_value;
  2824. if (!validate_read_and_copy_typed(&addrlen_value, params.addrlen))
  2825. return -EFAULT;
  2826. if (addrlen_value <= 0)
  2827. return -EINVAL;
  2828. if (!validate_write(params.addr, addrlen_value))
  2829. return -EFAULT;
  2830. if (!validate_write_typed(params.addrlen))
  2831. return -EFAULT;
  2832. auto description = file_description(params.sockfd);
  2833. if (!description)
  2834. return -EBADF;
  2835. if (!description->is_socket())
  2836. return -ENOTSOCK;
  2837. auto& socket = *description->socket();
  2838. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2839. u8 address_buffer[sizeof(sockaddr_un)];
  2840. addrlen_value = min(sizeof(sockaddr_un), static_cast<size_t>(addrlen_value));
  2841. if (!socket.get_local_address((sockaddr*)address_buffer, &addrlen_value))
  2842. return -EINVAL;
  2843. copy_to_user(params.addr, address_buffer, addrlen_value);
  2844. return 0;
  2845. }
  2846. int Process::sys$getsockname(const Syscall::SC_getsockname_params* user_params)
  2847. {
  2848. Syscall::SC_getsockname_params params;
  2849. if (!validate_read_and_copy_typed(&params, user_params))
  2850. return -EFAULT;
  2851. return get_sock_or_peer_name<true>(params);
  2852. }
  2853. int Process::sys$getpeername(const Syscall::SC_getpeername_params* user_params)
  2854. {
  2855. Syscall::SC_getpeername_params params;
  2856. if (!validate_read_and_copy_typed(&params, user_params))
  2857. return -EFAULT;
  2858. return get_sock_or_peer_name<false>(params);
  2859. }
  2860. int Process::sys$sched_setparam(int tid, const struct sched_param* param)
  2861. {
  2862. REQUIRE_PROMISE(proc);
  2863. if (!validate_read_typed(param))
  2864. return -EFAULT;
  2865. int desired_priority;
  2866. copy_from_user(&desired_priority, &param->sched_priority);
  2867. InterruptDisabler disabler;
  2868. auto* peer = current;
  2869. if (tid != 0)
  2870. peer = Thread::from_tid(tid);
  2871. if (!peer)
  2872. return -ESRCH;
  2873. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  2874. return -EPERM;
  2875. if (desired_priority < THREAD_PRIORITY_MIN || desired_priority > THREAD_PRIORITY_MAX)
  2876. return -EINVAL;
  2877. peer->set_priority((u32)desired_priority);
  2878. return 0;
  2879. }
  2880. int Process::sys$sched_getparam(pid_t pid, struct sched_param* param)
  2881. {
  2882. REQUIRE_PROMISE(proc);
  2883. if (!validate_write_typed(param))
  2884. return -EFAULT;
  2885. InterruptDisabler disabler;
  2886. auto* peer = current;
  2887. if (pid != 0)
  2888. peer = Thread::from_tid(pid);
  2889. if (!peer)
  2890. return -ESRCH;
  2891. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  2892. return -EPERM;
  2893. int priority = peer->priority();
  2894. copy_to_user(&param->sched_priority, &priority);
  2895. return 0;
  2896. }
  2897. int Process::sys$getsockopt(const Syscall::SC_getsockopt_params* params)
  2898. {
  2899. if (!validate_read_typed(params))
  2900. return -EFAULT;
  2901. SmapDisabler disabler;
  2902. int sockfd = params->sockfd;
  2903. int level = params->level;
  2904. int option = params->option;
  2905. void* value = params->value;
  2906. socklen_t* value_size = params->value_size;
  2907. if (!validate_write_typed(value_size))
  2908. return -EFAULT;
  2909. if (!validate_write(value, *value_size))
  2910. return -EFAULT;
  2911. auto description = file_description(sockfd);
  2912. if (!description)
  2913. return -EBADF;
  2914. if (!description->is_socket())
  2915. return -ENOTSOCK;
  2916. auto& socket = *description->socket();
  2917. if (has_promised(Pledge::accept) && socket.is_local() && level == SOL_SOCKET && option == SO_PEERCRED) {
  2918. // We make an exception for SOL_SOCKET::SO_PEERCRED on local sockets if you've pledged "accept"
  2919. } else {
  2920. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2921. }
  2922. return socket.getsockopt(*description, level, option, value, value_size);
  2923. }
  2924. int Process::sys$setsockopt(const Syscall::SC_setsockopt_params* params)
  2925. {
  2926. if (!validate_read_typed(params))
  2927. return -EFAULT;
  2928. SmapDisabler disabler;
  2929. int sockfd = params->sockfd;
  2930. int level = params->level;
  2931. int option = params->option;
  2932. const void* value = params->value;
  2933. socklen_t value_size = params->value_size;
  2934. if (!validate_read(value, value_size))
  2935. return -EFAULT;
  2936. auto description = file_description(sockfd);
  2937. if (!description)
  2938. return -EBADF;
  2939. if (!description->is_socket())
  2940. return -ENOTSOCK;
  2941. auto& socket = *description->socket();
  2942. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2943. return socket.setsockopt(level, option, value, value_size);
  2944. }
  2945. void Process::disown_all_shared_buffers()
  2946. {
  2947. LOCKER(shared_buffers().lock());
  2948. Vector<SharedBuffer*, 32> buffers_to_disown;
  2949. for (auto& it : shared_buffers().resource())
  2950. buffers_to_disown.append(it.value.ptr());
  2951. for (auto* shared_buffer : buffers_to_disown)
  2952. shared_buffer->disown(m_pid);
  2953. }
  2954. int Process::sys$create_shared_buffer(int size, void** buffer)
  2955. {
  2956. REQUIRE_PROMISE(shared_buffer);
  2957. if (!size || size < 0)
  2958. return -EINVAL;
  2959. size = PAGE_ROUND_UP(size);
  2960. if (!validate_write_typed(buffer))
  2961. return -EFAULT;
  2962. LOCKER(shared_buffers().lock());
  2963. static int s_next_shared_buffer_id;
  2964. int shared_buffer_id = ++s_next_shared_buffer_id;
  2965. auto shared_buffer = make<SharedBuffer>(shared_buffer_id, size);
  2966. shared_buffer->share_with(m_pid);
  2967. void* address = shared_buffer->ref_for_process_and_get_address(*this);
  2968. {
  2969. SmapDisabler disabler;
  2970. *buffer = address;
  2971. }
  2972. ASSERT((int)shared_buffer->size() >= size);
  2973. #ifdef SHARED_BUFFER_DEBUG
  2974. kprintf("%s(%u): Created shared buffer %d @ %p (%u bytes, vmobject is %u)\n", name().characters(), pid(), shared_buffer_id, *buffer, size, shared_buffer->size());
  2975. #endif
  2976. shared_buffers().resource().set(shared_buffer_id, move(shared_buffer));
  2977. return shared_buffer_id;
  2978. }
  2979. int Process::sys$share_buffer_with(int shared_buffer_id, pid_t peer_pid)
  2980. {
  2981. REQUIRE_PROMISE(shared_buffer);
  2982. if (!peer_pid || peer_pid < 0 || peer_pid == m_pid)
  2983. return -EINVAL;
  2984. LOCKER(shared_buffers().lock());
  2985. auto it = shared_buffers().resource().find(shared_buffer_id);
  2986. if (it == shared_buffers().resource().end())
  2987. return -EINVAL;
  2988. auto& shared_buffer = *(*it).value;
  2989. if (!shared_buffer.is_shared_with(m_pid))
  2990. return -EPERM;
  2991. {
  2992. InterruptDisabler disabler;
  2993. auto* peer = Process::from_pid(peer_pid);
  2994. if (!peer)
  2995. return -ESRCH;
  2996. }
  2997. shared_buffer.share_with(peer_pid);
  2998. return 0;
  2999. }
  3000. int Process::sys$share_buffer_globally(int shared_buffer_id)
  3001. {
  3002. REQUIRE_PROMISE(shared_buffer);
  3003. LOCKER(shared_buffers().lock());
  3004. auto it = shared_buffers().resource().find(shared_buffer_id);
  3005. if (it == shared_buffers().resource().end())
  3006. return -EINVAL;
  3007. auto& shared_buffer = *(*it).value;
  3008. if (!shared_buffer.is_shared_with(m_pid))
  3009. return -EPERM;
  3010. shared_buffer.share_globally();
  3011. return 0;
  3012. }
  3013. int Process::sys$release_shared_buffer(int shared_buffer_id)
  3014. {
  3015. REQUIRE_PROMISE(shared_buffer);
  3016. LOCKER(shared_buffers().lock());
  3017. auto it = shared_buffers().resource().find(shared_buffer_id);
  3018. if (it == shared_buffers().resource().end())
  3019. return -EINVAL;
  3020. auto& shared_buffer = *(*it).value;
  3021. if (!shared_buffer.is_shared_with(m_pid))
  3022. return -EPERM;
  3023. #ifdef SHARED_BUFFER_DEBUG
  3024. kprintf("%s(%u): Releasing shared buffer %d, buffer count: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3025. #endif
  3026. shared_buffer.deref_for_process(*this);
  3027. return 0;
  3028. }
  3029. void* Process::sys$get_shared_buffer(int shared_buffer_id)
  3030. {
  3031. REQUIRE_PROMISE(shared_buffer);
  3032. LOCKER(shared_buffers().lock());
  3033. auto it = shared_buffers().resource().find(shared_buffer_id);
  3034. if (it == shared_buffers().resource().end())
  3035. return (void*)-EINVAL;
  3036. auto& shared_buffer = *(*it).value;
  3037. if (!shared_buffer.is_shared_with(m_pid))
  3038. return (void*)-EPERM;
  3039. #ifdef SHARED_BUFFER_DEBUG
  3040. kprintf("%s(%u): Retaining shared buffer %d, buffer count: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3041. #endif
  3042. return shared_buffer.ref_for_process_and_get_address(*this);
  3043. }
  3044. int Process::sys$seal_shared_buffer(int shared_buffer_id)
  3045. {
  3046. REQUIRE_PROMISE(shared_buffer);
  3047. LOCKER(shared_buffers().lock());
  3048. auto it = shared_buffers().resource().find(shared_buffer_id);
  3049. if (it == shared_buffers().resource().end())
  3050. return -EINVAL;
  3051. auto& shared_buffer = *(*it).value;
  3052. if (!shared_buffer.is_shared_with(m_pid))
  3053. return -EPERM;
  3054. #ifdef SHARED_BUFFER_DEBUG
  3055. kprintf("%s(%u): Sealing shared buffer %d\n", name().characters(), pid(), shared_buffer_id);
  3056. #endif
  3057. shared_buffer.seal();
  3058. return 0;
  3059. }
  3060. int Process::sys$get_shared_buffer_size(int shared_buffer_id)
  3061. {
  3062. REQUIRE_PROMISE(shared_buffer);
  3063. LOCKER(shared_buffers().lock());
  3064. auto it = shared_buffers().resource().find(shared_buffer_id);
  3065. if (it == shared_buffers().resource().end())
  3066. return -EINVAL;
  3067. auto& shared_buffer = *(*it).value;
  3068. if (!shared_buffer.is_shared_with(m_pid))
  3069. return -EPERM;
  3070. #ifdef SHARED_BUFFER_DEBUG
  3071. kprintf("%s(%u): Get shared buffer %d size: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3072. #endif
  3073. return shared_buffer.size();
  3074. }
  3075. int Process::sys$set_shared_buffer_volatile(int shared_buffer_id, bool state)
  3076. {
  3077. REQUIRE_PROMISE(shared_buffer);
  3078. LOCKER(shared_buffers().lock());
  3079. auto it = shared_buffers().resource().find(shared_buffer_id);
  3080. if (it == shared_buffers().resource().end())
  3081. return -EINVAL;
  3082. auto& shared_buffer = *(*it).value;
  3083. if (!shared_buffer.is_shared_with(m_pid))
  3084. return -EPERM;
  3085. #ifdef SHARED_BUFFER_DEBUG
  3086. kprintf("%s(%u): Set shared buffer %d volatile: %u\n", name().characters(), pid(), shared_buffer_id, state);
  3087. #endif
  3088. if (!state) {
  3089. bool was_purged = shared_buffer.vmobject().was_purged();
  3090. shared_buffer.vmobject().set_volatile(state);
  3091. shared_buffer.vmobject().set_was_purged(false);
  3092. return was_purged ? 1 : 0;
  3093. }
  3094. shared_buffer.vmobject().set_volatile(true);
  3095. return 0;
  3096. }
  3097. void Process::terminate_due_to_signal(u8 signal)
  3098. {
  3099. ASSERT_INTERRUPTS_DISABLED();
  3100. ASSERT(signal < 32);
  3101. dbgprintf("terminate_due_to_signal %s(%u) <- %u\n", name().characters(), pid(), signal);
  3102. m_termination_status = 0;
  3103. m_termination_signal = signal;
  3104. die();
  3105. }
  3106. void Process::send_signal(u8 signal, Process* sender)
  3107. {
  3108. InterruptDisabler disabler;
  3109. auto* thread = Thread::from_tid(m_pid);
  3110. if (!thread)
  3111. thread = &any_thread();
  3112. thread->send_signal(signal, sender);
  3113. }
  3114. int Process::sys$create_thread(void* (*entry)(void*), void* argument, const Syscall::SC_create_thread_params* user_params)
  3115. {
  3116. REQUIRE_PROMISE(thread);
  3117. if (!validate_read((const void*)entry, sizeof(void*)))
  3118. return -EFAULT;
  3119. Syscall::SC_create_thread_params params;
  3120. if (!validate_read_and_copy_typed(&params, user_params))
  3121. return -EFAULT;
  3122. unsigned detach_state = params.m_detach_state;
  3123. int schedule_priority = params.m_schedule_priority;
  3124. void* stack_location = params.m_stack_location;
  3125. unsigned stack_size = params.m_stack_size;
  3126. if (!validate_write(stack_location, stack_size))
  3127. return -EFAULT;
  3128. u32 user_stack_address = reinterpret_cast<u32>(stack_location) + stack_size;
  3129. if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
  3130. return -EFAULT;
  3131. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  3132. int requested_thread_priority = schedule_priority;
  3133. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  3134. return -EINVAL;
  3135. bool is_thread_joinable = (0 == detach_state);
  3136. // FIXME: Do something with guard pages?
  3137. auto* thread = new Thread(*this);
  3138. // We know this thread is not the main_thread,
  3139. // So give it a unique name until the user calls $set_thread_name on it
  3140. // length + 4 to give space for our extra junk at the end
  3141. StringBuilder builder(m_name.length() + 4);
  3142. builder.append(m_name);
  3143. builder.appendf("[%d]", thread->tid());
  3144. thread->set_name(builder.to_string());
  3145. thread->set_priority(requested_thread_priority);
  3146. thread->set_joinable(is_thread_joinable);
  3147. auto& tss = thread->tss();
  3148. tss.eip = (uintptr_t)entry;
  3149. tss.eflags = 0x0202;
  3150. tss.cr3 = page_directory().cr3();
  3151. tss.esp = user_stack_address;
  3152. // NOTE: The stack needs to be 16-byte aligned.
  3153. thread->push_value_on_stack((uintptr_t)argument);
  3154. thread->push_value_on_stack(0);
  3155. thread->make_thread_specific_region({});
  3156. thread->set_state(Thread::State::Runnable);
  3157. return thread->tid();
  3158. }
  3159. void Process::sys$exit_thread(void* exit_value)
  3160. {
  3161. REQUIRE_PROMISE(thread);
  3162. cli();
  3163. current->m_exit_value = exit_value;
  3164. current->set_should_die();
  3165. big_lock().force_unlock_if_locked();
  3166. current->die_if_needed();
  3167. ASSERT_NOT_REACHED();
  3168. }
  3169. int Process::sys$detach_thread(int tid)
  3170. {
  3171. REQUIRE_PROMISE(thread);
  3172. InterruptDisabler disabler;
  3173. auto* thread = Thread::from_tid(tid);
  3174. if (!thread || thread->pid() != pid())
  3175. return -ESRCH;
  3176. if (!thread->is_joinable())
  3177. return -EINVAL;
  3178. thread->set_joinable(false);
  3179. return 0;
  3180. }
  3181. int Process::sys$join_thread(int tid, void** exit_value)
  3182. {
  3183. REQUIRE_PROMISE(thread);
  3184. if (exit_value && !validate_write_typed(exit_value))
  3185. return -EFAULT;
  3186. InterruptDisabler disabler;
  3187. auto* thread = Thread::from_tid(tid);
  3188. if (!thread || thread->pid() != pid())
  3189. return -ESRCH;
  3190. if (thread == current)
  3191. return -EDEADLK;
  3192. if (thread->m_joinee == current)
  3193. return -EDEADLK;
  3194. ASSERT(thread->m_joiner != current);
  3195. if (thread->m_joiner)
  3196. return -EINVAL;
  3197. if (!thread->is_joinable())
  3198. return -EINVAL;
  3199. void* joinee_exit_value = nullptr;
  3200. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  3201. for (;;) {
  3202. auto result = current->block<Thread::JoinBlocker>(*thread, joinee_exit_value);
  3203. if (result == Thread::BlockResult::InterruptedByDeath) {
  3204. // NOTE: This cleans things up so that Thread::finalize() won't
  3205. // get confused about a missing joiner when finalizing the joinee.
  3206. InterruptDisabler disabler;
  3207. current->m_joinee->m_joiner = nullptr;
  3208. current->m_joinee = nullptr;
  3209. return 0;
  3210. }
  3211. }
  3212. // NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
  3213. thread = nullptr;
  3214. if (exit_value)
  3215. copy_to_user(exit_value, &joinee_exit_value);
  3216. return 0;
  3217. }
  3218. int Process::sys$set_thread_name(int tid, const char* user_name, size_t user_name_length)
  3219. {
  3220. REQUIRE_PROMISE(thread);
  3221. auto name = validate_and_copy_string_from_user(user_name, user_name_length);
  3222. if (name.is_null())
  3223. return -EFAULT;
  3224. const size_t max_thread_name_size = 64;
  3225. if (name.length() > max_thread_name_size)
  3226. return -EINVAL;
  3227. InterruptDisabler disabler;
  3228. auto* thread = Thread::from_tid(tid);
  3229. if (!thread || thread->pid() != pid())
  3230. return -ESRCH;
  3231. thread->set_name(name);
  3232. return 0;
  3233. }
  3234. int Process::sys$get_thread_name(int tid, char* buffer, size_t buffer_size)
  3235. {
  3236. REQUIRE_PROMISE(thread);
  3237. if (buffer_size == 0)
  3238. return -EINVAL;
  3239. if (!validate_write(buffer, buffer_size))
  3240. return -EFAULT;
  3241. InterruptDisabler disabler;
  3242. auto* thread = Thread::from_tid(tid);
  3243. if (!thread || thread->pid() != pid())
  3244. return -ESRCH;
  3245. if (thread->name().length() + 1 > (size_t)buffer_size)
  3246. return -ENAMETOOLONG;
  3247. copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1);
  3248. return 0;
  3249. }
  3250. int Process::sys$gettid()
  3251. {
  3252. REQUIRE_PROMISE(stdio);
  3253. return current->tid();
  3254. }
  3255. int Process::sys$donate(int tid)
  3256. {
  3257. REQUIRE_PROMISE(stdio);
  3258. if (tid < 0)
  3259. return -EINVAL;
  3260. InterruptDisabler disabler;
  3261. auto* thread = Thread::from_tid(tid);
  3262. if (!thread || thread->pid() != pid())
  3263. return -ESRCH;
  3264. Scheduler::donate_to(thread, "sys$donate");
  3265. return 0;
  3266. }
  3267. int Process::sys$rename(const Syscall::SC_rename_params* user_params)
  3268. {
  3269. REQUIRE_PROMISE(cpath);
  3270. Syscall::SC_rename_params params;
  3271. if (!validate_read_and_copy_typed(&params, user_params))
  3272. return -EFAULT;
  3273. auto old_path = get_syscall_path_argument(params.old_path);
  3274. if (old_path.is_error())
  3275. return old_path.error();
  3276. auto new_path = get_syscall_path_argument(params.new_path);
  3277. if (new_path.is_error())
  3278. return new_path.error();
  3279. return VFS::the().rename(old_path.value(), new_path.value(), current_directory());
  3280. }
  3281. int Process::sys$ftruncate(int fd, off_t length)
  3282. {
  3283. REQUIRE_PROMISE(stdio);
  3284. if (length < 0)
  3285. return -EINVAL;
  3286. auto description = file_description(fd);
  3287. if (!description)
  3288. return -EBADF;
  3289. if (!description->is_writable())
  3290. return -EBADF;
  3291. return description->truncate(length);
  3292. }
  3293. int Process::sys$watch_file(const char* user_path, size_t path_length)
  3294. {
  3295. REQUIRE_PROMISE(rpath);
  3296. auto path = get_syscall_path_argument(user_path, path_length);
  3297. if (path.is_error())
  3298. return path.error();
  3299. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  3300. if (custody_or_error.is_error())
  3301. return custody_or_error.error();
  3302. auto& custody = custody_or_error.value();
  3303. auto& inode = custody->inode();
  3304. if (!inode.fs().supports_watchers())
  3305. return -ENOTSUP;
  3306. int fd = alloc_fd();
  3307. if (fd < 0)
  3308. return fd;
  3309. m_fds[fd].set(FileDescription::create(*InodeWatcher::create(inode)));
  3310. m_fds[fd].description->set_readable(true);
  3311. return fd;
  3312. }
  3313. int Process::sys$systrace(pid_t pid)
  3314. {
  3315. REQUIRE_PROMISE(proc);
  3316. InterruptDisabler disabler;
  3317. auto* peer = Process::from_pid(pid);
  3318. if (!peer)
  3319. return -ESRCH;
  3320. if (peer->uid() != m_euid)
  3321. return -EACCES;
  3322. int fd = alloc_fd();
  3323. if (fd < 0)
  3324. return fd;
  3325. auto description = FileDescription::create(peer->ensure_tracer());
  3326. description->set_readable(true);
  3327. m_fds[fd].set(move(description), 0);
  3328. return fd;
  3329. }
  3330. int Process::sys$halt()
  3331. {
  3332. if (!is_superuser())
  3333. return -EPERM;
  3334. REQUIRE_NO_PROMISES;
  3335. dbgprintf("acquiring FS locks...\n");
  3336. FS::lock_all();
  3337. dbgprintf("syncing mounted filesystems...\n");
  3338. FS::sync();
  3339. dbgprintf("attempting system shutdown...\n");
  3340. IO::out16(0x604, 0x2000);
  3341. return ESUCCESS;
  3342. }
  3343. int Process::sys$reboot()
  3344. {
  3345. if (!is_superuser())
  3346. return -EPERM;
  3347. REQUIRE_NO_PROMISES;
  3348. dbgprintf("acquiring FS locks...\n");
  3349. FS::lock_all();
  3350. dbgprintf("syncing mounted filesystems...\n");
  3351. FS::sync();
  3352. dbgprintf("attempting reboot via KB Controller...\n");
  3353. IO::out8(0x64, 0xFE);
  3354. return ESUCCESS;
  3355. }
  3356. int Process::sys$mount(const Syscall::SC_mount_params* user_params)
  3357. {
  3358. if (!is_superuser())
  3359. return -EPERM;
  3360. REQUIRE_NO_PROMISES;
  3361. Syscall::SC_mount_params params;
  3362. if (!validate_read_and_copy_typed(&params, user_params))
  3363. return -EFAULT;
  3364. auto source = validate_and_copy_string_from_user(params.source);
  3365. auto target = validate_and_copy_string_from_user(params.target);
  3366. auto fs_type = validate_and_copy_string_from_user(params.fs_type);
  3367. if (source.is_null() || target.is_null() || fs_type.is_null())
  3368. return -EFAULT;
  3369. dbg() << "mount " << fs_type << ": source " << source << " @ " << target;
  3370. auto custody_or_error = VFS::the().resolve_path(target, current_directory());
  3371. if (custody_or_error.is_error())
  3372. return custody_or_error.error();
  3373. auto& target_custody = custody_or_error.value();
  3374. RefPtr<FS> fs;
  3375. if (params.flags & MS_BIND) {
  3376. // We're doing a bind mount.
  3377. auto source_or_error = VFS::the().resolve_path(source, current_directory());
  3378. if (source_or_error.is_error())
  3379. return source_or_error.error();
  3380. auto& source_custody = source_or_error.value();
  3381. return VFS::the().bind_mount(source_custody, target_custody, params.flags);
  3382. }
  3383. if (fs_type == "ext2" || fs_type == "Ext2FS") {
  3384. auto source_or_error = VFS::the().open(source, O_RDWR, 0, current_directory());
  3385. if (source_or_error.is_error())
  3386. return source_or_error.error();
  3387. auto* device = source_or_error.value()->device();
  3388. if (!device || !device->is_disk_device()) {
  3389. dbg() << "mount: this is not a DiskDevice";
  3390. return -ENODEV;
  3391. }
  3392. auto& disk_device = static_cast<DiskDevice&>(*device);
  3393. dbg() << "mount: attempting to mount " << disk_device.absolute_path() << " on " << target;
  3394. fs = Ext2FS::create(disk_device);
  3395. } else if (fs_type == "proc" || fs_type == "ProcFS") {
  3396. fs = ProcFS::create();
  3397. } else if (fs_type == "devpts" || fs_type == "DevPtsFS") {
  3398. fs = DevPtsFS::create();
  3399. } else if (fs_type == "tmp" || fs_type == "TmpFS") {
  3400. fs = TmpFS::create();
  3401. } else {
  3402. return -ENODEV;
  3403. }
  3404. if (!fs->initialize()) {
  3405. dbg() << "mount: failed to initialize " << fs_type << " filesystem on " << source;
  3406. return -ENODEV;
  3407. }
  3408. auto result = VFS::the().mount(fs.release_nonnull(), target_custody, params.flags);
  3409. dbg() << "mount: successfully mounted " << source << " on " << target;
  3410. return result;
  3411. }
  3412. int Process::sys$umount(const char* user_mountpoint, size_t mountpoint_length)
  3413. {
  3414. if (!is_superuser())
  3415. return -EPERM;
  3416. REQUIRE_NO_PROMISES;
  3417. if (!validate_read(user_mountpoint, mountpoint_length))
  3418. return -EFAULT;
  3419. auto mountpoint = get_syscall_path_argument(user_mountpoint, mountpoint_length);
  3420. if (mountpoint.is_error())
  3421. return mountpoint.error();
  3422. auto metadata_or_error = VFS::the().lookup_metadata(mountpoint.value(), current_directory());
  3423. if (metadata_or_error.is_error())
  3424. return metadata_or_error.error();
  3425. auto guest_inode_id = metadata_or_error.value().inode;
  3426. return VFS::the().unmount(guest_inode_id);
  3427. }
  3428. ProcessTracer& Process::ensure_tracer()
  3429. {
  3430. if (!m_tracer)
  3431. m_tracer = ProcessTracer::create(m_pid);
  3432. return *m_tracer;
  3433. }
  3434. void Process::FileDescriptionAndFlags::clear()
  3435. {
  3436. description = nullptr;
  3437. flags = 0;
  3438. }
  3439. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& d, u32 f)
  3440. {
  3441. description = move(d);
  3442. flags = f;
  3443. }
  3444. int Process::sys$mknod(const Syscall::SC_mknod_params* user_params)
  3445. {
  3446. REQUIRE_PROMISE(dpath);
  3447. Syscall::SC_mknod_params params;
  3448. if (!validate_read_and_copy_typed(&params, user_params))
  3449. return -EFAULT;
  3450. if (!is_superuser() && !is_regular_file(params.mode) && !is_fifo(params.mode) && !is_socket(params.mode))
  3451. return -EPERM;
  3452. auto path = get_syscall_path_argument(params.path);
  3453. if (path.is_error())
  3454. return path.error();
  3455. return VFS::the().mknod(path.value(), params.mode & ~umask(), params.dev, current_directory());
  3456. }
  3457. int Process::sys$dump_backtrace()
  3458. {
  3459. dump_backtrace();
  3460. return 0;
  3461. }
  3462. int Process::sys$dbgputch(u8 ch)
  3463. {
  3464. IO::out8(0xe9, ch);
  3465. return 0;
  3466. }
  3467. int Process::sys$dbgputstr(const u8* characters, int length)
  3468. {
  3469. if (!length)
  3470. return 0;
  3471. if (!validate_read(characters, length))
  3472. return -EFAULT;
  3473. SmapDisabler disabler;
  3474. for (int i = 0; i < length; ++i)
  3475. IO::out8(0xe9, characters[i]);
  3476. return 0;
  3477. }
  3478. KBuffer Process::backtrace(ProcessInspectionHandle& handle) const
  3479. {
  3480. KBufferBuilder builder;
  3481. for_each_thread([&](Thread& thread) {
  3482. builder.appendf("Thread %d (%s):\n", thread.tid(), thread.name().characters());
  3483. builder.append(thread.backtrace(handle));
  3484. return IterationDecision::Continue;
  3485. });
  3486. return builder.build();
  3487. }
  3488. int Process::sys$set_process_icon(int icon_id)
  3489. {
  3490. REQUIRE_PROMISE(shared_buffer);
  3491. LOCKER(shared_buffers().lock());
  3492. auto it = shared_buffers().resource().find(icon_id);
  3493. if (it == shared_buffers().resource().end())
  3494. return -EINVAL;
  3495. auto& shared_buffer = *(*it).value;
  3496. if (!shared_buffer.is_shared_with(m_pid))
  3497. return -EPERM;
  3498. m_icon_id = icon_id;
  3499. return 0;
  3500. }
  3501. int Process::sys$get_process_name(char* buffer, int buffer_size)
  3502. {
  3503. REQUIRE_PROMISE(stdio);
  3504. if (buffer_size <= 0)
  3505. return -EINVAL;
  3506. if (!validate_write(buffer, buffer_size))
  3507. return -EFAULT;
  3508. if (m_name.length() + 1 > (size_t)buffer_size)
  3509. return -ENAMETOOLONG;
  3510. copy_to_user(buffer, m_name.characters(), m_name.length() + 1);
  3511. return 0;
  3512. }
  3513. // We don't use the flag yet, but we could use it for distinguishing
  3514. // random source like Linux, unlike the OpenBSD equivalent. However, if we
  3515. // do, we should be able of the caveats that Linux has dealt with.
  3516. int Process::sys$getrandom(void* buffer, size_t buffer_size, unsigned int flags __attribute__((unused)))
  3517. {
  3518. REQUIRE_PROMISE(stdio);
  3519. if (buffer_size <= 0)
  3520. return -EINVAL;
  3521. if (!validate_write(buffer, buffer_size))
  3522. return -EFAULT;
  3523. SmapDisabler disabler;
  3524. get_good_random_bytes((u8*)buffer, buffer_size);
  3525. return 0;
  3526. }
  3527. int Process::sys$setkeymap(const Syscall::SC_setkeymap_params* user_params)
  3528. {
  3529. if (!is_superuser())
  3530. return -EPERM;
  3531. REQUIRE_NO_PROMISES;
  3532. Syscall::SC_setkeymap_params params;
  3533. if (!validate_read_and_copy_typed(&params, user_params))
  3534. return -EFAULT;
  3535. const char* map = params.map;
  3536. const char* shift_map = params.shift_map;
  3537. const char* alt_map = params.alt_map;
  3538. const char* altgr_map = params.altgr_map;
  3539. if (!validate_read(map, 0x80))
  3540. return -EFAULT;
  3541. if (!validate_read(shift_map, 0x80))
  3542. return -EFAULT;
  3543. if (!validate_read(alt_map, 0x80))
  3544. return -EFAULT;
  3545. if (!validate_read(altgr_map, 0x80))
  3546. return -EFAULT;
  3547. SmapDisabler disabler;
  3548. KeyboardDevice::the().set_maps(map, shift_map, alt_map, altgr_map);
  3549. return 0;
  3550. }
  3551. int Process::sys$clock_gettime(clockid_t clock_id, timespec* ts)
  3552. {
  3553. REQUIRE_PROMISE(stdio);
  3554. if (!validate_write_typed(ts))
  3555. return -EFAULT;
  3556. SmapDisabler disabler;
  3557. switch (clock_id) {
  3558. case CLOCK_MONOTONIC:
  3559. ts->tv_sec = g_uptime / TICKS_PER_SECOND;
  3560. ts->tv_nsec = (g_uptime % TICKS_PER_SECOND) * 1000000;
  3561. break;
  3562. default:
  3563. return -EINVAL;
  3564. }
  3565. return 0;
  3566. }
  3567. int Process::sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params* user_params)
  3568. {
  3569. REQUIRE_PROMISE(stdio);
  3570. Syscall::SC_clock_nanosleep_params params;
  3571. if (!validate_read_and_copy_typed(&params, user_params))
  3572. return -EFAULT;
  3573. if (params.requested_sleep && !validate_read_typed(params.requested_sleep))
  3574. return -EFAULT;
  3575. timespec requested_sleep;
  3576. copy_from_user(&requested_sleep, params.requested_sleep);
  3577. if (params.remaining_sleep && !validate_write_typed(params.remaining_sleep))
  3578. return -EFAULT;
  3579. bool is_absolute = params.flags & TIMER_ABSTIME;
  3580. switch (params.clock_id) {
  3581. case CLOCK_MONOTONIC: {
  3582. u64 wakeup_time;
  3583. if (is_absolute) {
  3584. u64 time_to_wake = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3585. wakeup_time = current->sleep_until(time_to_wake);
  3586. } else {
  3587. u32 ticks_to_sleep = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3588. if (!ticks_to_sleep)
  3589. return 0;
  3590. wakeup_time = current->sleep(ticks_to_sleep);
  3591. }
  3592. if (wakeup_time > g_uptime) {
  3593. u32 ticks_left = wakeup_time - g_uptime;
  3594. if (!is_absolute && params.remaining_sleep) {
  3595. timespec remaining_sleep;
  3596. memset(&remaining_sleep, 0, sizeof(timespec));
  3597. remaining_sleep.tv_sec = ticks_left / TICKS_PER_SECOND;
  3598. ticks_left -= remaining_sleep.tv_sec * TICKS_PER_SECOND;
  3599. remaining_sleep.tv_nsec = ticks_left * 1000000;
  3600. copy_to_user(params.remaining_sleep, &remaining_sleep);
  3601. }
  3602. return -EINTR;
  3603. }
  3604. return 0;
  3605. }
  3606. default:
  3607. return -EINVAL;
  3608. }
  3609. }
  3610. int Process::sys$sync()
  3611. {
  3612. REQUIRE_PROMISE(stdio);
  3613. VFS::the().sync();
  3614. return 0;
  3615. }
  3616. int Process::sys$yield()
  3617. {
  3618. REQUIRE_PROMISE(stdio);
  3619. current->yield_without_holding_big_lock();
  3620. return 0;
  3621. }
  3622. int Process::sys$beep()
  3623. {
  3624. PCSpeaker::tone_on(440);
  3625. u64 wakeup_time = current->sleep(100);
  3626. PCSpeaker::tone_off();
  3627. if (wakeup_time > g_uptime)
  3628. return -EINTR;
  3629. return 0;
  3630. }
  3631. int Process::sys$module_load(const char* user_path, size_t path_length)
  3632. {
  3633. if (!is_superuser())
  3634. return -EPERM;
  3635. REQUIRE_NO_PROMISES;
  3636. auto path = get_syscall_path_argument(user_path, path_length);
  3637. if (path.is_error())
  3638. return path.error();
  3639. auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
  3640. if (description_or_error.is_error())
  3641. return description_or_error.error();
  3642. auto& description = description_or_error.value();
  3643. auto payload = description->read_entire_file();
  3644. auto storage = KBuffer::create_with_size(payload.size());
  3645. memcpy(storage.data(), payload.data(), payload.size());
  3646. payload.clear();
  3647. auto elf_image = make<ELFImage>(storage.data(), storage.size());
  3648. if (!elf_image->parse())
  3649. return -ENOEXEC;
  3650. HashMap<String, u8*> section_storage_by_name;
  3651. auto module = make<Module>();
  3652. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELFImage::Section& section) {
  3653. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  3654. section_storage_by_name.set(section.name(), section_storage.data());
  3655. module->sections.append(move(section_storage));
  3656. return IterationDecision::Continue;
  3657. });
  3658. bool missing_symbols = false;
  3659. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELFImage::Section& section) {
  3660. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  3661. ASSERT(section_storage);
  3662. section.relocations().for_each_relocation([&](const ELFImage::Relocation& relocation) {
  3663. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  3664. switch (relocation.type()) {
  3665. case R_386_PC32: {
  3666. // PC-relative relocation
  3667. dbg() << "PC-relative relocation: " << relocation.symbol().name();
  3668. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3669. if (symbol_address == 0)
  3670. missing_symbols = true;
  3671. dbg() << " Symbol address: " << (void*)symbol_address;
  3672. ptrdiff_t relative_offset = (char*)symbol_address - ((char*)&patch_ptr + 4);
  3673. patch_ptr = relative_offset;
  3674. break;
  3675. }
  3676. case R_386_32: // Absolute relocation
  3677. dbg() << "Absolute relocation: '" << relocation.symbol().name() << "' value:" << relocation.symbol().value() << ", index:" << relocation.symbol_index();
  3678. if (relocation.symbol().bind() == STB_LOCAL) {
  3679. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  3680. ASSERT(section_storage_containing_symbol);
  3681. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  3682. if (symbol_address == 0)
  3683. missing_symbols = true;
  3684. dbg() << " Symbol address: " << (void*)symbol_address;
  3685. patch_ptr += symbol_address;
  3686. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  3687. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3688. if (symbol_address == 0)
  3689. missing_symbols = true;
  3690. dbg() << " Symbol address: " << (void*)symbol_address;
  3691. patch_ptr += symbol_address;
  3692. } else {
  3693. ASSERT_NOT_REACHED();
  3694. }
  3695. break;
  3696. }
  3697. return IterationDecision::Continue;
  3698. });
  3699. return IterationDecision::Continue;
  3700. });
  3701. if (missing_symbols)
  3702. return -EINVAL;
  3703. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  3704. if (!text_base) {
  3705. dbg() << "No .text section found in module!";
  3706. return -EINVAL;
  3707. }
  3708. elf_image->for_each_symbol([&](const ELFImage::Symbol& symbol) {
  3709. dbg() << " - " << symbol.type() << " '" << symbol.name() << "' @ " << (void*)symbol.value() << ", size=" << symbol.size();
  3710. if (symbol.name() == "module_init") {
  3711. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  3712. } else if (symbol.name() == "module_fini") {
  3713. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  3714. } else if (symbol.name() == "module_name") {
  3715. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  3716. if (storage)
  3717. module->name = String((const char*)(storage + symbol.value()));
  3718. }
  3719. return IterationDecision::Continue;
  3720. });
  3721. if (!module->module_init)
  3722. return -EINVAL;
  3723. if (g_modules->contains(module->name)) {
  3724. dbg() << "a module with the name " << module->name << " is already loaded; please unload it first";
  3725. return -EEXIST;
  3726. }
  3727. module->module_init();
  3728. auto name = module->name;
  3729. g_modules->set(name, move(module));
  3730. return 0;
  3731. }
  3732. int Process::sys$module_unload(const char* user_name, size_t name_length)
  3733. {
  3734. if (!is_superuser())
  3735. return -EPERM;
  3736. REQUIRE_NO_PROMISES;
  3737. auto module_name = validate_and_copy_string_from_user(user_name, name_length);
  3738. if (module_name.is_null())
  3739. return -EFAULT;
  3740. auto it = g_modules->find(module_name);
  3741. if (it == g_modules->end())
  3742. return -ENOENT;
  3743. if (it->value->module_fini)
  3744. it->value->module_fini();
  3745. g_modules->remove(it);
  3746. return 0;
  3747. }
  3748. int Process::sys$profiling_enable(pid_t pid)
  3749. {
  3750. REQUIRE_NO_PROMISES;
  3751. InterruptDisabler disabler;
  3752. auto* process = Process::from_pid(pid);
  3753. if (!process)
  3754. return -ESRCH;
  3755. if (!is_superuser() && process->uid() != m_uid)
  3756. return -EPERM;
  3757. Profiling::start(*process);
  3758. process->set_profiling(true);
  3759. return 0;
  3760. }
  3761. int Process::sys$profiling_disable(pid_t pid)
  3762. {
  3763. InterruptDisabler disabler;
  3764. auto* process = Process::from_pid(pid);
  3765. if (!process)
  3766. return -ESRCH;
  3767. if (!is_superuser() && process->uid() != m_uid)
  3768. return -EPERM;
  3769. process->set_profiling(false);
  3770. Profiling::stop();
  3771. return 0;
  3772. }
  3773. void* Process::sys$get_kernel_info_page()
  3774. {
  3775. REQUIRE_PROMISE(stdio);
  3776. return s_info_page_address_for_userspace.as_ptr();
  3777. }
  3778. Thread& Process::any_thread()
  3779. {
  3780. Thread* found_thread = nullptr;
  3781. for_each_thread([&](auto& thread) {
  3782. found_thread = &thread;
  3783. return IterationDecision::Break;
  3784. });
  3785. ASSERT(found_thread);
  3786. return *found_thread;
  3787. }
  3788. WaitQueue& Process::futex_queue(i32* userspace_address)
  3789. {
  3790. auto& queue = m_futex_queues.ensure((uintptr_t)userspace_address);
  3791. if (!queue)
  3792. queue = make<WaitQueue>();
  3793. return *queue;
  3794. }
  3795. int Process::sys$futex(const Syscall::SC_futex_params* user_params)
  3796. {
  3797. REQUIRE_PROMISE(thread);
  3798. Syscall::SC_futex_params params;
  3799. if (!validate_read_and_copy_typed(&params, user_params))
  3800. return -EFAULT;
  3801. i32* userspace_address = params.userspace_address;
  3802. int futex_op = params.futex_op;
  3803. i32 value = params.val;
  3804. const timespec* user_timeout = params.timeout;
  3805. if (!validate_read_typed(userspace_address))
  3806. return -EFAULT;
  3807. if (user_timeout && !validate_read_typed(user_timeout))
  3808. return -EFAULT;
  3809. timespec timeout { 0, 0 };
  3810. if (user_timeout)
  3811. copy_from_user(&timeout, user_timeout);
  3812. i32 user_value;
  3813. switch (futex_op) {
  3814. case FUTEX_WAIT:
  3815. copy_from_user(&user_value, userspace_address);
  3816. if (user_value != value)
  3817. return -EAGAIN;
  3818. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  3819. // FIXME: Support timeout!
  3820. current->wait_on(futex_queue(userspace_address));
  3821. break;
  3822. case FUTEX_WAKE:
  3823. if (value == 0)
  3824. return 0;
  3825. if (value == 1) {
  3826. futex_queue(userspace_address).wake_one();
  3827. } else {
  3828. // FIXME: Wake exactly (value) waiters.
  3829. futex_queue(userspace_address).wake_all();
  3830. }
  3831. break;
  3832. }
  3833. return 0;
  3834. }
  3835. int Process::sys$set_thread_boost(int tid, int amount)
  3836. {
  3837. REQUIRE_PROMISE(proc);
  3838. if (amount < 0 || amount > 20)
  3839. return -EINVAL;
  3840. InterruptDisabler disabler;
  3841. auto* thread = Thread::from_tid(tid);
  3842. if (!thread)
  3843. return -ESRCH;
  3844. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  3845. return -ESRCH;
  3846. if (!is_superuser() && thread->process().uid() != euid())
  3847. return -EPERM;
  3848. thread->set_priority_boost(amount);
  3849. return 0;
  3850. }
  3851. int Process::sys$set_process_boost(pid_t pid, int amount)
  3852. {
  3853. REQUIRE_PROMISE(proc);
  3854. if (amount < 0 || amount > 20)
  3855. return -EINVAL;
  3856. InterruptDisabler disabler;
  3857. auto* process = Process::from_pid(pid);
  3858. if (!process || process->is_dead())
  3859. return -ESRCH;
  3860. if (!is_superuser() && process->uid() != euid())
  3861. return -EPERM;
  3862. process->m_priority_boost = amount;
  3863. return 0;
  3864. }
  3865. int Process::sys$chroot(const char* user_path, size_t path_length, int mount_flags)
  3866. {
  3867. if (!is_superuser())
  3868. return -EPERM;
  3869. REQUIRE_PROMISE(chroot);
  3870. auto path = get_syscall_path_argument(user_path, path_length);
  3871. if (path.is_error())
  3872. return path.error();
  3873. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  3874. if (directory_or_error.is_error())
  3875. return directory_or_error.error();
  3876. auto directory = directory_or_error.value();
  3877. m_root_directory_relative_to_global_root = directory;
  3878. int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
  3879. set_root_directory(Custody::create(nullptr, "", directory->inode(), chroot_mount_flags));
  3880. return 0;
  3881. }
  3882. Custody& Process::root_directory()
  3883. {
  3884. if (!m_root_directory)
  3885. m_root_directory = VFS::the().root_custody();
  3886. return *m_root_directory;
  3887. }
  3888. Custody& Process::root_directory_relative_to_global_root()
  3889. {
  3890. if (!m_root_directory_relative_to_global_root)
  3891. m_root_directory_relative_to_global_root = root_directory();
  3892. return *m_root_directory_relative_to_global_root;
  3893. }
  3894. void Process::set_root_directory(const Custody& root)
  3895. {
  3896. m_root_directory = root;
  3897. }
  3898. int Process::sys$pledge(const Syscall::SC_pledge_params* user_params)
  3899. {
  3900. Syscall::SC_pledge_params params;
  3901. if (!validate_read_and_copy_typed(&params, user_params))
  3902. return -EFAULT;
  3903. if (params.promises.length > 1024 || params.execpromises.length > 1024)
  3904. return -E2BIG;
  3905. String promises;
  3906. if (params.promises.characters) {
  3907. promises = validate_and_copy_string_from_user(params.promises);
  3908. if (promises.is_null())
  3909. return -EFAULT;
  3910. }
  3911. String execpromises;
  3912. if (params.execpromises.characters) {
  3913. execpromises = validate_and_copy_string_from_user(params.execpromises);
  3914. if (execpromises.is_null())
  3915. return -EFAULT;
  3916. }
  3917. auto parse_pledge = [&](auto& pledge_spec, u32& mask) {
  3918. auto parts = pledge_spec.split_view(' ');
  3919. for (auto& part : parts) {
  3920. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  3921. if (part == #x) { \
  3922. mask |= (1u << (u32)Pledge::x); \
  3923. continue; \
  3924. }
  3925. ENUMERATE_PLEDGE_PROMISES
  3926. #undef __ENUMERATE_PLEDGE_PROMISE
  3927. if (part == "dns") {
  3928. // "dns" is an alias for "unix" since DNS queries go via LookupServer
  3929. mask |= (1u << (u32)Pledge::unix);
  3930. continue;
  3931. }
  3932. return false;
  3933. }
  3934. return true;
  3935. };
  3936. if (!promises.is_null()) {
  3937. u32 new_promises = 0;
  3938. if (!parse_pledge(promises, new_promises))
  3939. return -EINVAL;
  3940. if (m_promises && (!new_promises || new_promises & ~m_promises))
  3941. return -EPERM;
  3942. m_promises = new_promises;
  3943. }
  3944. if (!execpromises.is_null()) {
  3945. u32 new_execpromises = 0;
  3946. if (!parse_pledge(execpromises, new_execpromises))
  3947. return -EINVAL;
  3948. if (m_execpromises && (!new_execpromises || new_execpromises & ~m_execpromises))
  3949. return -EPERM;
  3950. m_execpromises = new_execpromises;
  3951. }
  3952. return 0;
  3953. }
  3954. Region& Process::add_region(NonnullOwnPtr<Region> region)
  3955. {
  3956. auto* ptr = region.ptr();
  3957. m_regions.append(move(region));
  3958. return *ptr;
  3959. }
  3960. int Process::sys$unveil(const Syscall::SC_unveil_params* user_params)
  3961. {
  3962. Syscall::SC_unveil_params params;
  3963. if (!validate_read_and_copy_typed(&params, user_params))
  3964. return -EFAULT;
  3965. if (!params.path.characters && !params.permissions.characters) {
  3966. m_veil_state = VeilState::Locked;
  3967. return 0;
  3968. }
  3969. if (m_veil_state == VeilState::Locked)
  3970. return -EPERM;
  3971. if (!params.path.characters || !params.permissions.characters)
  3972. return -EINVAL;
  3973. if (params.permissions.length > 4)
  3974. return -EINVAL;
  3975. auto path = get_syscall_path_argument(params.path);
  3976. if (path.is_error())
  3977. return path.error();
  3978. if (path.value().is_empty() || path.value().characters()[0] != '/')
  3979. return -EINVAL;
  3980. auto permissions = validate_and_copy_string_from_user(params.permissions);
  3981. if (permissions.is_null())
  3982. return -EFAULT;
  3983. unsigned new_permissions = 0;
  3984. for (size_t i = 0; i < permissions.length(); ++i) {
  3985. switch (permissions[i]) {
  3986. case 'r':
  3987. new_permissions |= UnveiledPath::Access::Read;
  3988. break;
  3989. case 'w':
  3990. new_permissions |= UnveiledPath::Access::Write;
  3991. break;
  3992. case 'x':
  3993. new_permissions |= UnveiledPath::Access::Execute;
  3994. break;
  3995. case 'c':
  3996. new_permissions |= UnveiledPath::Access::CreateOrRemove;
  3997. break;
  3998. default:
  3999. return -EINVAL;
  4000. }
  4001. }
  4002. for (int i = 0; i < m_unveiled_paths.size(); ++i) {
  4003. auto& unveiled_path = m_unveiled_paths[i];
  4004. if (unveiled_path.path == path.value()) {
  4005. if (new_permissions & ~unveiled_path.permissions)
  4006. return -EPERM;
  4007. unveiled_path.permissions = new_permissions;
  4008. return 0;
  4009. }
  4010. }
  4011. m_unveiled_paths.append({ path.value(), new_permissions });
  4012. ASSERT(m_veil_state != VeilState::Locked);
  4013. m_veil_state = VeilState::Dropped;
  4014. return 0;
  4015. }