Process.cpp 166 KB

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