Process.cpp 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335
  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. int Process::sys$set_process_name(const char* user_name, size_t user_name_length)
  3852. {
  3853. REQUIRE_PROMISE(proc);
  3854. if (user_name_length > 256)
  3855. return -ENAMETOOLONG;
  3856. auto name = validate_and_copy_string_from_user(user_name, user_name_length);
  3857. if (name.is_null())
  3858. return -EFAULT;
  3859. m_name = move(name);
  3860. return 0;
  3861. }
  3862. // We don't use the flag yet, but we could use it for distinguishing
  3863. // random source like Linux, unlike the OpenBSD equivalent. However, if we
  3864. // do, we should be able of the caveats that Linux has dealt with.
  3865. ssize_t Process::sys$getrandom(void* buffer, size_t buffer_size, unsigned int flags __attribute__((unused)))
  3866. {
  3867. REQUIRE_PROMISE(stdio);
  3868. if (buffer_size <= 0)
  3869. return -EINVAL;
  3870. if (!validate_write(buffer, buffer_size))
  3871. return -EFAULT;
  3872. SmapDisabler disabler;
  3873. get_good_random_bytes((u8*)buffer, buffer_size);
  3874. return 0;
  3875. }
  3876. int Process::sys$setkeymap(const Syscall::SC_setkeymap_params* user_params)
  3877. {
  3878. REQUIRE_PROMISE(setkeymap);
  3879. if (!is_superuser())
  3880. return -EPERM;
  3881. Syscall::SC_setkeymap_params params;
  3882. if (!validate_read_and_copy_typed(&params, user_params))
  3883. return -EFAULT;
  3884. Keyboard::CharacterMapData character_map_data;
  3885. if (!validate_read(params.map, CHAR_MAP_SIZE))
  3886. return -EFAULT;
  3887. if (!validate_read(params.shift_map, CHAR_MAP_SIZE))
  3888. return -EFAULT;
  3889. if (!validate_read(params.alt_map, CHAR_MAP_SIZE))
  3890. return -EFAULT;
  3891. if (!validate_read(params.altgr_map, CHAR_MAP_SIZE))
  3892. return -EFAULT;
  3893. copy_from_user(character_map_data.map, params.map, CHAR_MAP_SIZE * sizeof(u32));
  3894. copy_from_user(character_map_data.shift_map, params.shift_map, CHAR_MAP_SIZE * sizeof(u32));
  3895. copy_from_user(character_map_data.alt_map, params.alt_map, CHAR_MAP_SIZE * sizeof(u32));
  3896. copy_from_user(character_map_data.altgr_map, params.altgr_map, CHAR_MAP_SIZE * sizeof(u32));
  3897. KeyboardDevice::the().set_maps(character_map_data);
  3898. return 0;
  3899. }
  3900. int Process::sys$clock_gettime(clockid_t clock_id, timespec* user_ts)
  3901. {
  3902. REQUIRE_PROMISE(stdio);
  3903. if (!validate_write_typed(user_ts))
  3904. return -EFAULT;
  3905. timespec ts;
  3906. memset(&ts, 0, sizeof(ts));
  3907. switch (clock_id) {
  3908. case CLOCK_MONOTONIC:
  3909. ts.tv_sec = TimeManagement::the().seconds_since_boot();
  3910. ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
  3911. break;
  3912. case CLOCK_REALTIME:
  3913. ts.tv_sec = TimeManagement::the().epoch_time();
  3914. ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
  3915. break;
  3916. default:
  3917. return -EINVAL;
  3918. }
  3919. copy_to_user(user_ts, &ts);
  3920. return 0;
  3921. }
  3922. int Process::sys$clock_settime(clockid_t clock_id, timespec* user_ts)
  3923. {
  3924. REQUIRE_PROMISE(settime);
  3925. if (!is_superuser())
  3926. return -EPERM;
  3927. timespec ts;
  3928. if (!validate_read_and_copy_typed(&ts, user_ts))
  3929. return -EFAULT;
  3930. switch (clock_id) {
  3931. case CLOCK_REALTIME:
  3932. TimeManagement::the().set_epoch_time(ts.tv_sec);
  3933. break;
  3934. default:
  3935. return -EINVAL;
  3936. }
  3937. return 0;
  3938. }
  3939. int Process::sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params* user_params)
  3940. {
  3941. REQUIRE_PROMISE(stdio);
  3942. Syscall::SC_clock_nanosleep_params params;
  3943. if (!validate_read_and_copy_typed(&params, user_params))
  3944. return -EFAULT;
  3945. if (params.requested_sleep && !validate_read_typed(params.requested_sleep))
  3946. return -EFAULT;
  3947. timespec requested_sleep;
  3948. copy_from_user(&requested_sleep, params.requested_sleep);
  3949. if (params.remaining_sleep && !validate_write_typed(params.remaining_sleep))
  3950. return -EFAULT;
  3951. bool is_absolute = params.flags & TIMER_ABSTIME;
  3952. switch (params.clock_id) {
  3953. case CLOCK_MONOTONIC: {
  3954. u64 wakeup_time;
  3955. if (is_absolute) {
  3956. u64 time_to_wake = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3957. wakeup_time = Thread::current()->sleep_until(time_to_wake);
  3958. } else {
  3959. u64 ticks_to_sleep = requested_sleep.tv_sec * TimeManagement::the().ticks_per_second();
  3960. ticks_to_sleep += requested_sleep.tv_nsec * TimeManagement::the().ticks_per_second() / 1000000000;
  3961. if (!ticks_to_sleep)
  3962. return 0;
  3963. wakeup_time = Thread::current()->sleep(ticks_to_sleep);
  3964. }
  3965. if (wakeup_time > g_uptime) {
  3966. u64 ticks_left = wakeup_time - g_uptime;
  3967. if (!is_absolute && params.remaining_sleep) {
  3968. if (!validate_write_typed(params.remaining_sleep)) {
  3969. // This can happen because the lock is dropped while
  3970. // sleeping, thus giving other threads the opportunity
  3971. // to make the region unwritable.
  3972. return -EFAULT;
  3973. }
  3974. timespec remaining_sleep;
  3975. memset(&remaining_sleep, 0, sizeof(timespec));
  3976. remaining_sleep.tv_sec = ticks_left / TimeManagement::the().ticks_per_second();
  3977. ticks_left -= remaining_sleep.tv_sec * TimeManagement::the().ticks_per_second();
  3978. remaining_sleep.tv_nsec = ticks_left * 1000000000 / TimeManagement::the().ticks_per_second();
  3979. copy_to_user(params.remaining_sleep, &remaining_sleep);
  3980. }
  3981. return -EINTR;
  3982. }
  3983. return 0;
  3984. }
  3985. default:
  3986. return -EINVAL;
  3987. }
  3988. }
  3989. int Process::sys$sync()
  3990. {
  3991. REQUIRE_PROMISE(stdio);
  3992. VFS::the().sync();
  3993. return 0;
  3994. }
  3995. int Process::sys$yield()
  3996. {
  3997. REQUIRE_PROMISE(stdio);
  3998. Thread::current()->yield_without_holding_big_lock();
  3999. return 0;
  4000. }
  4001. int Process::sys$beep()
  4002. {
  4003. PCSpeaker::tone_on(440);
  4004. u64 wakeup_time = Thread::current()->sleep(100);
  4005. PCSpeaker::tone_off();
  4006. if (wakeup_time > g_uptime)
  4007. return -EINTR;
  4008. return 0;
  4009. }
  4010. int Process::sys$module_load(const char* user_path, size_t path_length)
  4011. {
  4012. if (!is_superuser())
  4013. return -EPERM;
  4014. REQUIRE_NO_PROMISES;
  4015. auto path = get_syscall_path_argument(user_path, path_length);
  4016. if (path.is_error())
  4017. return path.error();
  4018. auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
  4019. if (description_or_error.is_error())
  4020. return description_or_error.error();
  4021. auto& description = description_or_error.value();
  4022. auto payload_or_error = description->read_entire_file();
  4023. if (payload_or_error.is_error())
  4024. return payload_or_error.error();
  4025. auto payload = payload_or_error.value();
  4026. auto storage = KBuffer::create_with_size(payload.size());
  4027. memcpy(storage.data(), payload.data(), payload.size());
  4028. payload.clear();
  4029. auto elf_image = make<ELF::Image>(storage.data(), storage.size());
  4030. if (!elf_image->parse())
  4031. return -ENOEXEC;
  4032. HashMap<String, u8*> section_storage_by_name;
  4033. auto module = make<Module>();
  4034. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  4035. if (!section.size())
  4036. return IterationDecision::Continue;
  4037. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  4038. section_storage_by_name.set(section.name(), section_storage.data());
  4039. module->sections.append(move(section_storage));
  4040. return IterationDecision::Continue;
  4041. });
  4042. bool missing_symbols = false;
  4043. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  4044. if (!section.size())
  4045. return IterationDecision::Continue;
  4046. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  4047. ASSERT(section_storage);
  4048. section.relocations().for_each_relocation([&](const ELF::Image::Relocation& relocation) {
  4049. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  4050. switch (relocation.type()) {
  4051. case R_386_PC32: {
  4052. // PC-relative relocation
  4053. dbg() << "PC-relative relocation: " << relocation.symbol().name();
  4054. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  4055. if (symbol_address == 0)
  4056. missing_symbols = true;
  4057. dbg() << " Symbol address: " << (void*)symbol_address;
  4058. ptrdiff_t relative_offset = (char*)symbol_address - ((char*)&patch_ptr + 4);
  4059. patch_ptr = relative_offset;
  4060. break;
  4061. }
  4062. case R_386_32: // Absolute relocation
  4063. dbg() << "Absolute relocation: '" << relocation.symbol().name() << "' value:" << relocation.symbol().value() << ", index:" << relocation.symbol_index();
  4064. if (relocation.symbol().bind() == STB_LOCAL) {
  4065. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  4066. ASSERT(section_storage_containing_symbol);
  4067. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  4068. if (symbol_address == 0)
  4069. missing_symbols = true;
  4070. dbg() << " Symbol address: " << (void*)symbol_address;
  4071. patch_ptr += symbol_address;
  4072. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  4073. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  4074. if (symbol_address == 0)
  4075. missing_symbols = true;
  4076. dbg() << " Symbol address: " << (void*)symbol_address;
  4077. patch_ptr += symbol_address;
  4078. } else {
  4079. ASSERT_NOT_REACHED();
  4080. }
  4081. break;
  4082. }
  4083. return IterationDecision::Continue;
  4084. });
  4085. return IterationDecision::Continue;
  4086. });
  4087. if (missing_symbols)
  4088. return -EINVAL;
  4089. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  4090. if (!text_base) {
  4091. dbg() << "No .text section found in module!";
  4092. return -EINVAL;
  4093. }
  4094. elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
  4095. dbg() << " - " << symbol.type() << " '" << symbol.name() << "' @ " << (void*)symbol.value() << ", size=" << symbol.size();
  4096. if (symbol.name() == "module_init") {
  4097. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  4098. } else if (symbol.name() == "module_fini") {
  4099. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  4100. } else if (symbol.name() == "module_name") {
  4101. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  4102. if (storage)
  4103. module->name = String((const char*)(storage + symbol.value()));
  4104. }
  4105. return IterationDecision::Continue;
  4106. });
  4107. if (!module->module_init)
  4108. return -EINVAL;
  4109. if (g_modules->contains(module->name)) {
  4110. dbg() << "a module with the name " << module->name << " is already loaded; please unload it first";
  4111. return -EEXIST;
  4112. }
  4113. module->module_init();
  4114. auto name = module->name;
  4115. g_modules->set(name, move(module));
  4116. return 0;
  4117. }
  4118. int Process::sys$module_unload(const char* user_name, size_t name_length)
  4119. {
  4120. if (!is_superuser())
  4121. return -EPERM;
  4122. REQUIRE_NO_PROMISES;
  4123. auto module_name = validate_and_copy_string_from_user(user_name, name_length);
  4124. if (module_name.is_null())
  4125. return -EFAULT;
  4126. auto it = g_modules->find(module_name);
  4127. if (it == g_modules->end())
  4128. return -ENOENT;
  4129. if (it->value->module_fini)
  4130. it->value->module_fini();
  4131. g_modules->remove(it);
  4132. return 0;
  4133. }
  4134. int Process::sys$profiling_enable(pid_t pid)
  4135. {
  4136. REQUIRE_NO_PROMISES;
  4137. ScopedSpinLock lock(g_processes_lock);
  4138. auto* process = Process::from_pid(pid);
  4139. if (!process)
  4140. return -ESRCH;
  4141. if (process->is_dead())
  4142. return -ESRCH;
  4143. if (!is_superuser() && process->uid() != m_uid)
  4144. return -EPERM;
  4145. Profiling::start(*process);
  4146. process->set_profiling(true);
  4147. return 0;
  4148. }
  4149. int Process::sys$profiling_disable(pid_t pid)
  4150. {
  4151. ScopedSpinLock lock(g_processes_lock);
  4152. auto* process = Process::from_pid(pid);
  4153. if (!process)
  4154. return -ESRCH;
  4155. if (!is_superuser() && process->uid() != m_uid)
  4156. return -EPERM;
  4157. process->set_profiling(false);
  4158. Profiling::stop();
  4159. return 0;
  4160. }
  4161. WaitQueue& Process::futex_queue(i32* userspace_address)
  4162. {
  4163. auto& queue = m_futex_queues.ensure((FlatPtr)userspace_address);
  4164. if (!queue)
  4165. queue = make<WaitQueue>();
  4166. return *queue;
  4167. }
  4168. int Process::sys$futex(const Syscall::SC_futex_params* user_params)
  4169. {
  4170. REQUIRE_PROMISE(thread);
  4171. Syscall::SC_futex_params params;
  4172. if (!validate_read_and_copy_typed(&params, user_params))
  4173. return -EFAULT;
  4174. i32* userspace_address = params.userspace_address;
  4175. int futex_op = params.futex_op;
  4176. i32 value = params.val;
  4177. const timespec* user_timeout = params.timeout;
  4178. if (!validate_read_typed(userspace_address))
  4179. return -EFAULT;
  4180. if (user_timeout && !validate_read_typed(user_timeout))
  4181. return -EFAULT;
  4182. switch (futex_op) {
  4183. case FUTEX_WAIT: {
  4184. i32 user_value;
  4185. copy_from_user(&user_value, userspace_address);
  4186. if (user_value != value)
  4187. return -EAGAIN;
  4188. timespec ts_abstimeout { 0, 0 };
  4189. if (user_timeout && !validate_read_and_copy_typed(&ts_abstimeout, user_timeout))
  4190. return -EFAULT;
  4191. WaitQueue& wait_queue = futex_queue(userspace_address);
  4192. timeval* optional_timeout = nullptr;
  4193. timeval relative_timeout { 0, 0 };
  4194. if (user_timeout) {
  4195. compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout);
  4196. optional_timeout = &relative_timeout;
  4197. }
  4198. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  4199. Thread::BlockResult result = Thread::current()->wait_on(wait_queue, "Futex", optional_timeout);
  4200. if (result == Thread::BlockResult::InterruptedByTimeout) {
  4201. return -ETIMEDOUT;
  4202. }
  4203. break;
  4204. }
  4205. case FUTEX_WAKE:
  4206. if (value == 0)
  4207. return 0;
  4208. if (value == 1) {
  4209. futex_queue(userspace_address).wake_one();
  4210. } else {
  4211. futex_queue(userspace_address).wake_n(value);
  4212. }
  4213. break;
  4214. }
  4215. return 0;
  4216. }
  4217. int Process::sys$set_thread_boost(int tid, int amount)
  4218. {
  4219. REQUIRE_PROMISE(proc);
  4220. if (amount < 0 || amount > 20)
  4221. return -EINVAL;
  4222. InterruptDisabler disabler;
  4223. auto* thread = Thread::from_tid(tid);
  4224. if (!thread)
  4225. return -ESRCH;
  4226. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  4227. return -ESRCH;
  4228. if (!is_superuser() && thread->process().uid() != euid())
  4229. return -EPERM;
  4230. thread->set_priority_boost(amount);
  4231. return 0;
  4232. }
  4233. int Process::sys$set_process_boost(pid_t pid, int amount)
  4234. {
  4235. REQUIRE_PROMISE(proc);
  4236. if (amount < 0 || amount > 20)
  4237. return -EINVAL;
  4238. ScopedSpinLock lock(g_processes_lock);
  4239. auto* process = Process::from_pid(pid);
  4240. if (!process || process->is_dead())
  4241. return -ESRCH;
  4242. if (!is_superuser() && process->uid() != euid())
  4243. return -EPERM;
  4244. process->m_priority_boost = amount;
  4245. return 0;
  4246. }
  4247. int Process::sys$chroot(const char* user_path, size_t path_length, int mount_flags)
  4248. {
  4249. if (!is_superuser())
  4250. return -EPERM;
  4251. REQUIRE_PROMISE(chroot);
  4252. auto path = get_syscall_path_argument(user_path, path_length);
  4253. if (path.is_error())
  4254. return path.error();
  4255. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  4256. if (directory_or_error.is_error())
  4257. return directory_or_error.error();
  4258. auto directory = directory_or_error.value();
  4259. m_root_directory_relative_to_global_root = directory;
  4260. int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
  4261. set_root_directory(Custody::create(nullptr, "", directory->inode(), chroot_mount_flags));
  4262. return 0;
  4263. }
  4264. Custody& Process::root_directory()
  4265. {
  4266. if (!m_root_directory)
  4267. m_root_directory = VFS::the().root_custody();
  4268. return *m_root_directory;
  4269. }
  4270. Custody& Process::root_directory_relative_to_global_root()
  4271. {
  4272. if (!m_root_directory_relative_to_global_root)
  4273. m_root_directory_relative_to_global_root = root_directory();
  4274. return *m_root_directory_relative_to_global_root;
  4275. }
  4276. void Process::set_root_directory(const Custody& root)
  4277. {
  4278. m_root_directory = root;
  4279. }
  4280. int Process::sys$pledge(const Syscall::SC_pledge_params* user_params)
  4281. {
  4282. Syscall::SC_pledge_params params;
  4283. if (!validate_read_and_copy_typed(&params, user_params))
  4284. return -EFAULT;
  4285. if (params.promises.length > 1024 || params.execpromises.length > 1024)
  4286. return -E2BIG;
  4287. String promises;
  4288. if (params.promises.characters) {
  4289. promises = validate_and_copy_string_from_user(params.promises);
  4290. if (promises.is_null())
  4291. return -EFAULT;
  4292. }
  4293. String execpromises;
  4294. if (params.execpromises.characters) {
  4295. execpromises = validate_and_copy_string_from_user(params.execpromises);
  4296. if (execpromises.is_null())
  4297. return -EFAULT;
  4298. }
  4299. auto parse_pledge = [&](auto& pledge_spec, u32& mask) {
  4300. auto parts = pledge_spec.split_view(' ');
  4301. for (auto& part : parts) {
  4302. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  4303. if (part == #x) { \
  4304. mask |= (1u << (u32)Pledge::x); \
  4305. continue; \
  4306. }
  4307. ENUMERATE_PLEDGE_PROMISES
  4308. #undef __ENUMERATE_PLEDGE_PROMISE
  4309. if (part == "dns") {
  4310. // "dns" is an alias for "unix" since DNS queries go via LookupServer
  4311. mask |= (1u << (u32)Pledge::unix);
  4312. continue;
  4313. }
  4314. return false;
  4315. }
  4316. return true;
  4317. };
  4318. u32 new_promises;
  4319. u32 new_execpromises;
  4320. if (!promises.is_null()) {
  4321. new_promises = 0;
  4322. if (!parse_pledge(promises, new_promises))
  4323. return -EINVAL;
  4324. if (m_promises && (!new_promises || new_promises & ~m_promises))
  4325. return -EPERM;
  4326. } else {
  4327. new_promises = m_promises;
  4328. }
  4329. if (!execpromises.is_null()) {
  4330. new_execpromises = 0;
  4331. if (!parse_pledge(execpromises, new_execpromises))
  4332. return -EINVAL;
  4333. if (m_execpromises && (!new_execpromises || new_execpromises & ~m_execpromises))
  4334. return -EPERM;
  4335. } else {
  4336. new_execpromises = m_execpromises;
  4337. }
  4338. m_promises = new_promises;
  4339. m_execpromises = new_execpromises;
  4340. return 0;
  4341. }
  4342. Region& Process::add_region(NonnullOwnPtr<Region> region)
  4343. {
  4344. auto* ptr = region.ptr();
  4345. ScopedSpinLock lock(m_lock);
  4346. m_regions.append(move(region));
  4347. return *ptr;
  4348. }
  4349. int Process::sys$unveil(const Syscall::SC_unveil_params* user_params)
  4350. {
  4351. Syscall::SC_unveil_params params;
  4352. if (!validate_read_and_copy_typed(&params, user_params))
  4353. return -EFAULT;
  4354. if (!params.path.characters && !params.permissions.characters) {
  4355. m_veil_state = VeilState::Locked;
  4356. return 0;
  4357. }
  4358. if (m_veil_state == VeilState::Locked)
  4359. return -EPERM;
  4360. if (!params.path.characters || !params.permissions.characters)
  4361. return -EINVAL;
  4362. if (params.permissions.length > 4)
  4363. return -EINVAL;
  4364. auto path = get_syscall_path_argument(params.path);
  4365. if (path.is_error())
  4366. return path.error();
  4367. if (path.value().is_empty() || path.value().characters()[0] != '/')
  4368. return -EINVAL;
  4369. auto custody_or_error = VFS::the().resolve_path_without_veil(path.value(), root_directory());
  4370. if (custody_or_error.is_error())
  4371. // FIXME Should this be EINVAL?
  4372. return custody_or_error.error();
  4373. auto& custody = custody_or_error.value();
  4374. auto new_unveiled_path = custody->absolute_path();
  4375. auto permissions = validate_and_copy_string_from_user(params.permissions);
  4376. if (permissions.is_null())
  4377. return -EFAULT;
  4378. unsigned new_permissions = 0;
  4379. for (size_t i = 0; i < permissions.length(); ++i) {
  4380. switch (permissions[i]) {
  4381. case 'r':
  4382. new_permissions |= UnveiledPath::Access::Read;
  4383. break;
  4384. case 'w':
  4385. new_permissions |= UnveiledPath::Access::Write;
  4386. break;
  4387. case 'x':
  4388. new_permissions |= UnveiledPath::Access::Execute;
  4389. break;
  4390. case 'c':
  4391. new_permissions |= UnveiledPath::Access::CreateOrRemove;
  4392. break;
  4393. default:
  4394. return -EINVAL;
  4395. }
  4396. }
  4397. for (size_t i = 0; i < m_unveiled_paths.size(); ++i) {
  4398. auto& unveiled_path = m_unveiled_paths[i];
  4399. if (unveiled_path.path == new_unveiled_path) {
  4400. if (new_permissions & ~unveiled_path.permissions)
  4401. return -EPERM;
  4402. unveiled_path.permissions = new_permissions;
  4403. return 0;
  4404. }
  4405. }
  4406. m_unveiled_paths.append({ new_unveiled_path, new_permissions });
  4407. ASSERT(m_veil_state != VeilState::Locked);
  4408. m_veil_state = VeilState::Dropped;
  4409. return 0;
  4410. }
  4411. int Process::sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2)
  4412. {
  4413. if (!m_perf_event_buffer)
  4414. m_perf_event_buffer = make<PerformanceEventBuffer>();
  4415. return m_perf_event_buffer->append(type, arg1, arg2);
  4416. }
  4417. void Process::set_tty(TTY* tty)
  4418. {
  4419. m_tty = tty;
  4420. }
  4421. OwnPtr<Process::ELFBundle> Process::elf_bundle() const
  4422. {
  4423. if (!m_executable)
  4424. return nullptr;
  4425. auto bundle = make<ELFBundle>();
  4426. if (!m_executable->inode().shared_vmobject()) {
  4427. return nullptr;
  4428. }
  4429. ASSERT(m_executable->inode().shared_vmobject());
  4430. auto& vmobject = *m_executable->inode().shared_vmobject();
  4431. bundle->region = MM.allocate_kernel_region_with_vmobject(const_cast<SharedInodeVMObject&>(vmobject), vmobject.size(), "ELF bundle", Region::Access::Read);
  4432. if (!bundle->region)
  4433. return nullptr;
  4434. bundle->elf_loader = ELF::Loader::create(bundle->region->vaddr().as_ptr(), bundle->region->size());
  4435. return bundle;
  4436. }
  4437. int Process::sys$get_stack_bounds(FlatPtr* user_stack_base, size_t* user_stack_size)
  4438. {
  4439. if (!validate_write_typed(user_stack_base))
  4440. return -EFAULT;
  4441. if (!validate_write_typed(user_stack_size))
  4442. return -EFAULT;
  4443. FlatPtr stack_pointer = Thread::current()->get_register_dump_from_stack().userspace_esp;
  4444. auto* stack_region = MM.region_from_vaddr(*this, VirtualAddress(stack_pointer));
  4445. if (!stack_region) {
  4446. ASSERT_NOT_REACHED();
  4447. return -EINVAL;
  4448. }
  4449. FlatPtr stack_base = stack_region->range().base().get();
  4450. size_t stack_size = stack_region->size();
  4451. copy_to_user(user_stack_base, &stack_base);
  4452. copy_to_user(user_stack_size, &stack_size);
  4453. return 0;
  4454. }
  4455. int Process::sys$ptrace(const Syscall::SC_ptrace_params* user_params)
  4456. {
  4457. REQUIRE_PROMISE(proc);
  4458. Syscall::SC_ptrace_params params;
  4459. if (!validate_read_and_copy_typed(&params, user_params))
  4460. return -EFAULT;
  4461. auto result = Ptrace::handle_syscall(params, *this);
  4462. return result.is_error() ? result.error() : result.value();
  4463. }
  4464. bool Process::has_tracee_thread(int tracer_pid) const
  4465. {
  4466. bool has_tracee = false;
  4467. for_each_thread([&](Thread& t) {
  4468. if (t.tracer() && t.tracer()->tracer_pid() == tracer_pid) {
  4469. has_tracee = true;
  4470. return IterationDecision::Break;
  4471. }
  4472. return IterationDecision::Continue;
  4473. });
  4474. return has_tracee;
  4475. }
  4476. KResultOr<u32> Process::peek_user_data(u32* address)
  4477. {
  4478. if (!MM.validate_user_read(*this, VirtualAddress(address), sizeof(u32))) {
  4479. dbg() << "Invalid address for peek_user_data: " << address;
  4480. return KResult(-EFAULT);
  4481. }
  4482. uint32_t result;
  4483. // This function can be called from the context of another
  4484. // process that called PT_PEEK
  4485. ProcessPagingScope scope(*this);
  4486. copy_from_user(&result, address);
  4487. return result;
  4488. }
  4489. KResult Process::poke_user_data(u32* address, u32 data)
  4490. {
  4491. // We validate for read (rather than write) because PT_POKE can write to readonly pages.
  4492. // So we effectively only care that the poke operation is trying to write to user pages.
  4493. if (!MM.validate_user_read(*this, VirtualAddress(address), sizeof(u32))) {
  4494. dbg() << "Invalid address for poke_user_data: " << address;
  4495. return KResult(-EFAULT);
  4496. }
  4497. ProcessPagingScope scope(*this);
  4498. Range range = { VirtualAddress(address), sizeof(u32) };
  4499. auto* region = region_containing(range);
  4500. ASSERT(region != nullptr);
  4501. if (region->is_shared()) {
  4502. // If the region is shared, we change its vmobject to a PrivateInodeVMObject
  4503. // to prevent the write operation from chaning any shared inode data
  4504. ASSERT(region->vmobject().is_shared_inode());
  4505. region->set_vmobject(PrivateInodeVMObject::create_with_inode(static_cast<SharedInodeVMObject&>(region->vmobject()).inode()));
  4506. region->set_shared(false);
  4507. }
  4508. const bool was_writable = region->is_writable();
  4509. if (!was_writable) //TODO refactor into scopeguard
  4510. {
  4511. region->set_writable(true);
  4512. region->remap();
  4513. }
  4514. copy_to_user(address, &data);
  4515. if (!was_writable) {
  4516. region->set_writable(false);
  4517. region->remap();
  4518. }
  4519. return KResult(KSuccess);
  4520. }
  4521. int Process::sys$sendfd(int sockfd, int fd)
  4522. {
  4523. REQUIRE_PROMISE(sendfd);
  4524. auto socket_description = file_description(sockfd);
  4525. if (!socket_description)
  4526. return -EBADF;
  4527. if (!socket_description->is_socket())
  4528. return -ENOTSOCK;
  4529. auto& socket = *socket_description->socket();
  4530. if (!socket.is_local())
  4531. return -EAFNOSUPPORT;
  4532. if (!socket.is_connected())
  4533. return -ENOTCONN;
  4534. auto passing_descriptor = file_description(fd);
  4535. if (!passing_descriptor)
  4536. return -EBADF;
  4537. auto& local_socket = static_cast<LocalSocket&>(socket);
  4538. return local_socket.sendfd(*socket_description, *passing_descriptor);
  4539. }
  4540. int Process::sys$recvfd(int sockfd)
  4541. {
  4542. REQUIRE_PROMISE(recvfd);
  4543. auto socket_description = file_description(sockfd);
  4544. if (!socket_description)
  4545. return -EBADF;
  4546. if (!socket_description->is_socket())
  4547. return -ENOTSOCK;
  4548. auto& socket = *socket_description->socket();
  4549. if (!socket.is_local())
  4550. return -EAFNOSUPPORT;
  4551. int new_fd = alloc_fd();
  4552. if (new_fd < 0)
  4553. return new_fd;
  4554. auto& local_socket = static_cast<LocalSocket&>(socket);
  4555. auto received_descriptor_or_error = local_socket.recvfd(*socket_description);
  4556. if (received_descriptor_or_error.is_error())
  4557. return received_descriptor_or_error.error();
  4558. m_fds[new_fd].set(*received_descriptor_or_error.value(), 0);
  4559. return new_fd;
  4560. }
  4561. long Process::sys$sysconf(int name)
  4562. {
  4563. switch (name) {
  4564. case _SC_NPROCESSORS_CONF:
  4565. case _SC_NPROCESSORS_ONLN:
  4566. return Processor::processor_count();
  4567. default:
  4568. return -EINVAL;
  4569. }
  4570. }
  4571. }