posixmodule.c 450 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126
  1. /* POSIX module implementation */
  2. /* This file is also used for Windows NT/MS-Win. In that case the
  3. module actually calls itself 'nt', not 'posix', and a few
  4. functions are either unimplemented or implemented differently. The source
  5. assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
  6. of the compiler used. Different compilers define their own feature
  7. test macro, e.g. '_MSC_VER'. */
  8. #define PY_SSIZE_T_CLEAN
  9. #include "Python.h"
  10. #ifdef __VXWORKS__
  11. # include "pycore_bitutils.h" // _Py_popcount32()
  12. #endif
  13. #include "pycore_call.h" // _PyObject_CallNoArgs()
  14. #include "pycore_ceval.h" // _PyEval_ReInitThreads()
  15. #include "pycore_fileutils.h" // _Py_closerange()
  16. #include "pycore_import.h" // _PyImport_ReInitLock()
  17. #include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
  18. #include "pycore_long.h" // _PyLong_IsNegative()
  19. #include "pycore_moduleobject.h" // _PyModule_GetState()
  20. #include "pycore_object.h" // _PyObject_LookupSpecial()
  21. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  22. #include "pycore_signal.h" // Py_NSIG
  23. #include "pycore_typeobject.h" // _PyType_AddMethod()
  24. #ifdef MS_WINDOWS
  25. # include <windows.h>
  26. # if !defined(MS_WINDOWS_GAMES) || defined(MS_WINDOWS_DESKTOP)
  27. # include <pathcch.h>
  28. # endif
  29. # include <winioctl.h>
  30. # include <lmcons.h> // UNLEN
  31. # include "osdefs.h" // SEP
  32. # include <aclapi.h> // SetEntriesInAcl
  33. # include <sddl.h> // SDDL_REVISION_1
  34. # if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
  35. # define HAVE_SYMLINK
  36. # endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
  37. #endif
  38. #include "structmember.h" // PyMemberDef
  39. #ifndef MS_WINDOWS
  40. # include "posixmodule.h"
  41. #else
  42. # include "pycore_fileutils_windows.h"
  43. # include "winreparse.h"
  44. #endif
  45. #if !defined(EX_OK) && defined(EXIT_SUCCESS)
  46. # define EX_OK EXIT_SUCCESS
  47. #endif
  48. /* On android API level 21, 'AT_EACCESS' is not declared although
  49. * HAVE_FACCESSAT is defined. */
  50. #ifdef __ANDROID__
  51. # undef HAVE_FACCESSAT
  52. #endif
  53. #include <stdio.h> // ctermid()
  54. #include <stdlib.h> // system()
  55. /*
  56. * A number of APIs are available on macOS from a certain macOS version.
  57. * To support building with a new SDK while deploying to older versions
  58. * the availability test is split into two:
  59. * - HAVE_<FUNCTION>: The configure check for compile time availability
  60. * - HAVE_<FUNCTION>_RUNTIME: Runtime check for availability
  61. *
  62. * The latter is always true when not on macOS, or when using a compiler
  63. * that does not support __has_builtin (older versions of Xcode).
  64. *
  65. * Due to compiler restrictions there is one valid use of HAVE_<FUNCTION>_RUNTIME:
  66. * if (HAVE_<FUNCTION>_RUNTIME) { ... }
  67. *
  68. * In mixing the test with other tests or using negations will result in compile
  69. * errors.
  70. */
  71. #if defined(__APPLE__)
  72. #include <mach/mach.h>
  73. #if defined(__has_builtin)
  74. #if __has_builtin(__builtin_available)
  75. #define HAVE_BUILTIN_AVAILABLE 1
  76. #endif
  77. #endif
  78. #ifdef HAVE_BUILTIN_AVAILABLE
  79. # define HAVE_FSTATAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  80. # define HAVE_FACCESSAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  81. # define HAVE_FCHMODAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  82. # define HAVE_FCHOWNAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  83. # define HAVE_LINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  84. # define HAVE_FDOPENDIR_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  85. # define HAVE_MKDIRAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  86. # define HAVE_RENAMEAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  87. # define HAVE_UNLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  88. # define HAVE_OPENAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  89. # define HAVE_READLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  90. # define HAVE_SYMLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
  91. # define HAVE_FUTIMENS_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
  92. # define HAVE_UTIMENSAT_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
  93. # define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
  94. # define HAVE_MKFIFOAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
  95. # define HAVE_MKNODAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
  96. # define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *)
  97. #else /* Xcode 8 or earlier */
  98. /* __builtin_available is not present in these compilers, but
  99. * some of the symbols might be weak linked (10.10 SDK or later
  100. * deploying on 10.9.
  101. *
  102. * Fall back to the older style of availability checking for
  103. * symbols introduced in macOS 10.10.
  104. */
  105. # ifdef HAVE_FSTATAT
  106. # define HAVE_FSTATAT_RUNTIME (fstatat != NULL)
  107. # endif
  108. # ifdef HAVE_FACCESSAT
  109. # define HAVE_FACCESSAT_RUNTIME (faccessat != NULL)
  110. # endif
  111. # ifdef HAVE_FCHMODAT
  112. # define HAVE_FCHMODAT_RUNTIME (fchmodat != NULL)
  113. # endif
  114. # ifdef HAVE_FCHOWNAT
  115. # define HAVE_FCHOWNAT_RUNTIME (fchownat != NULL)
  116. # endif
  117. # ifdef HAVE_LINKAT
  118. # define HAVE_LINKAT_RUNTIME (linkat != NULL)
  119. # endif
  120. # ifdef HAVE_FDOPENDIR
  121. # define HAVE_FDOPENDIR_RUNTIME (fdopendir != NULL)
  122. # endif
  123. # ifdef HAVE_MKDIRAT
  124. # define HAVE_MKDIRAT_RUNTIME (mkdirat != NULL)
  125. # endif
  126. # ifdef HAVE_RENAMEAT
  127. # define HAVE_RENAMEAT_RUNTIME (renameat != NULL)
  128. # endif
  129. # ifdef HAVE_UNLINKAT
  130. # define HAVE_UNLINKAT_RUNTIME (unlinkat != NULL)
  131. # endif
  132. # ifdef HAVE_OPENAT
  133. # define HAVE_OPENAT_RUNTIME (openat != NULL)
  134. # endif
  135. # ifdef HAVE_READLINKAT
  136. # define HAVE_READLINKAT_RUNTIME (readlinkat != NULL)
  137. # endif
  138. # ifdef HAVE_SYMLINKAT
  139. # define HAVE_SYMLINKAT_RUNTIME (symlinkat != NULL)
  140. # endif
  141. # ifdef HAVE_UTIMENSAT
  142. # define HAVE_UTIMENSAT_RUNTIME (utimensat != NULL)
  143. # endif
  144. # ifdef HAVE_FUTIMENS
  145. # define HAVE_FUTIMENS_RUNTIME (futimens != NULL)
  146. # endif
  147. # ifdef HAVE_PWRITEV
  148. # define HAVE_PWRITEV_RUNTIME (pwritev != NULL)
  149. # endif
  150. # ifdef HAVE_MKFIFOAT
  151. # define HAVE_MKFIFOAT_RUNTIME (mkfifoat != NULL)
  152. # endif
  153. # ifdef HAVE_MKNODAT
  154. # define HAVE_MKNODAT_RUNTIME (mknodat != NULL)
  155. # endif
  156. #endif
  157. #ifdef HAVE_FUTIMESAT
  158. /* Some of the logic for weak linking depends on this assertion */
  159. # error "HAVE_FUTIMESAT unexpectedly defined"
  160. #endif
  161. #else
  162. # define HAVE_FSTATAT_RUNTIME 1
  163. # define HAVE_FACCESSAT_RUNTIME 1
  164. # define HAVE_FCHMODAT_RUNTIME 1
  165. # define HAVE_FCHOWNAT_RUNTIME 1
  166. # define HAVE_LINKAT_RUNTIME 1
  167. # define HAVE_FDOPENDIR_RUNTIME 1
  168. # define HAVE_MKDIRAT_RUNTIME 1
  169. # define HAVE_RENAMEAT_RUNTIME 1
  170. # define HAVE_UNLINKAT_RUNTIME 1
  171. # define HAVE_OPENAT_RUNTIME 1
  172. # define HAVE_READLINKAT_RUNTIME 1
  173. # define HAVE_SYMLINKAT_RUNTIME 1
  174. # define HAVE_FUTIMENS_RUNTIME 1
  175. # define HAVE_UTIMENSAT_RUNTIME 1
  176. # define HAVE_PWRITEV_RUNTIME 1
  177. # define HAVE_MKFIFOAT_RUNTIME 1
  178. # define HAVE_MKNODAT_RUNTIME 1
  179. #endif
  180. #ifdef __cplusplus
  181. extern "C" {
  182. #endif
  183. PyDoc_STRVAR(posix__doc__,
  184. "This module provides access to operating system functionality that is\n\
  185. standardized by the C Standard and the POSIX standard (a thinly\n\
  186. disguised Unix interface). Refer to the library manual and\n\
  187. corresponding Unix manual entries for more information on calls.");
  188. #ifdef HAVE_SYS_UIO_H
  189. # include <sys/uio.h>
  190. #endif
  191. #ifdef HAVE_SYS_TYPES_H
  192. /* Should be included before <sys/sysmacros.h> on HP-UX v3 */
  193. # include <sys/types.h>
  194. #endif /* HAVE_SYS_TYPES_H */
  195. #ifdef HAVE_SYS_SYSMACROS_H
  196. /* GNU C Library: major(), minor(), makedev() */
  197. # include <sys/sysmacros.h>
  198. #endif
  199. #ifdef HAVE_SYS_STAT_H
  200. # include <sys/stat.h>
  201. #endif /* HAVE_SYS_STAT_H */
  202. #ifdef HAVE_SYS_WAIT_H
  203. # include <sys/wait.h> // WNOHANG
  204. #endif
  205. #ifdef HAVE_LINUX_WAIT_H
  206. # include <linux/wait.h> // P_PIDFD
  207. #endif
  208. #ifdef HAVE_SIGNAL_H
  209. # include <signal.h>
  210. #endif
  211. #ifdef HAVE_FCNTL_H
  212. # include <fcntl.h>
  213. #endif
  214. #ifdef HAVE_GRP_H
  215. # include <grp.h>
  216. #endif
  217. #ifdef HAVE_SYSEXITS_H
  218. # include <sysexits.h>
  219. #endif
  220. #ifdef HAVE_SYS_LOADAVG_H
  221. # include <sys/loadavg.h>
  222. #endif
  223. #ifdef HAVE_SYS_SENDFILE_H
  224. # include <sys/sendfile.h>
  225. #endif
  226. #if defined(__APPLE__)
  227. # include <copyfile.h>
  228. #endif
  229. #ifdef HAVE_SCHED_H
  230. # include <sched.h>
  231. #endif
  232. #ifdef HAVE_COPY_FILE_RANGE
  233. # include <unistd.h>
  234. #endif
  235. #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
  236. # undef HAVE_SCHED_SETAFFINITY
  237. #endif
  238. #if defined(HAVE_SYS_XATTR_H) && defined(HAVE_LINUX_LIMITS_H) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
  239. # define USE_XATTRS
  240. # include <linux/limits.h> // Needed for XATTR_SIZE_MAX on musl libc.
  241. #endif
  242. #ifdef USE_XATTRS
  243. # include <sys/xattr.h>
  244. #endif
  245. #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
  246. # ifdef HAVE_SYS_SOCKET_H
  247. # include <sys/socket.h>
  248. # endif
  249. #endif
  250. #ifdef HAVE_DLFCN_H
  251. # include <dlfcn.h>
  252. #endif
  253. #ifdef __hpux
  254. # include <sys/mpctl.h>
  255. #endif
  256. #if defined(__DragonFly__) || \
  257. defined(__OpenBSD__) || \
  258. defined(__FreeBSD__) || \
  259. defined(__NetBSD__) || \
  260. defined(__APPLE__)
  261. # include <sys/sysctl.h>
  262. #endif
  263. #ifdef HAVE_LINUX_RANDOM_H
  264. # include <linux/random.h>
  265. #endif
  266. #ifdef HAVE_GETRANDOM_SYSCALL
  267. # include <sys/syscall.h>
  268. # include <sys/random.h>
  269. #endif
  270. #ifdef HAVE_WINDOWS_CONSOLE_IO
  271. # define TERMSIZE_USE_CONIO
  272. #elif defined(HAVE_SYS_IOCTL_H)
  273. # include <sys/ioctl.h>
  274. # if defined(HAVE_TERMIOS_H)
  275. # include <termios.h>
  276. # endif
  277. # if defined(TIOCGWINSZ)
  278. # define TERMSIZE_USE_IOCTL
  279. # endif
  280. #endif /* HAVE_WINDOWS_CONSOLE_IO */
  281. /* Various compilers have only certain posix functions */
  282. /* XXX Gosh I wish these were all moved into pyconfig.h */
  283. #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
  284. # define HAVE_OPENDIR 1
  285. # define HAVE_SYSTEM 1
  286. # include <process.h>
  287. #elif defined( _MSC_VER)
  288. /* Microsoft compiler */
  289. # if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
  290. # define HAVE_GETPPID 1
  291. # endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_APP | MS_WINDOWS_SYSTEM */
  292. # if defined(MS_WINDOWS_DESKTOP)
  293. # define HAVE_GETLOGIN 1
  294. # endif /* MS_WINDOWS_DESKTOP */
  295. # if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
  296. # define HAVE_SPAWNV 1
  297. # define HAVE_EXECV 1
  298. # define HAVE_WSPAWNV 1
  299. # define HAVE_WEXECV 1
  300. # define HAVE_SYSTEM 1
  301. # define HAVE_CWAIT 1
  302. # endif /* MS_WINDOWS_DESKTOP | MS_WINDOWS_SYSTEM */
  303. # define HAVE_PIPE 1
  304. # define HAVE_FSYNC 1
  305. # define fsync _commit
  306. #endif /* ! __WATCOMC__ || __QNX__ */
  307. /*[clinic input]
  308. # one of the few times we lie about this name!
  309. module os
  310. [clinic start generated code]*/
  311. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
  312. #ifndef _MSC_VER
  313. #if defined(__sgi)&&_COMPILER_VERSION>=700
  314. /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
  315. (default) */
  316. extern char *ctermid_r(char *);
  317. #endif
  318. #endif /* !_MSC_VER */
  319. #if defined(__VXWORKS__)
  320. # error #include <vxCpuLib.h>
  321. # error #include <rtpLib.h>
  322. # include <wait.h>
  323. # error #include <taskLib.h>
  324. # ifndef _P_WAIT
  325. # define _P_WAIT 0
  326. # define _P_NOWAIT 1
  327. # define _P_NOWAITO 1
  328. # endif
  329. #endif /* __VXWORKS__ */
  330. #ifdef HAVE_POSIX_SPAWN
  331. # include <spawn.h>
  332. #endif
  333. #ifdef HAVE_UTIME_H
  334. # include <utime.h>
  335. #endif /* HAVE_UTIME_H */
  336. #ifdef HAVE_SYS_UTIME_H
  337. # include <sys/utime.h>
  338. # define HAVE_UTIME_H /* pretend we do for the rest of this file */
  339. #endif /* HAVE_SYS_UTIME_H */
  340. #ifdef HAVE_SYS_TIMES_H
  341. # include <sys/times.h>
  342. #endif /* HAVE_SYS_TIMES_H */
  343. #ifdef HAVE_SYS_PARAM_H
  344. # include <sys/param.h>
  345. #endif /* HAVE_SYS_PARAM_H */
  346. #ifdef HAVE_SYS_UTSNAME_H
  347. # include <sys/utsname.h>
  348. #endif /* HAVE_SYS_UTSNAME_H */
  349. #ifdef HAVE_DIRENT_H
  350. # include <dirent.h>
  351. # define NAMLEN(dirent) strlen((dirent)->d_name)
  352. #else
  353. # if defined(__WATCOMC__) && !defined(__QNX__)
  354. # include <direct.h>
  355. # define NAMLEN(dirent) strlen((dirent)->d_name)
  356. # else
  357. # define dirent direct
  358. # define NAMLEN(dirent) (dirent)->d_namlen
  359. # endif
  360. # ifdef HAVE_SYS_NDIR_H
  361. # include <sys/ndir.h>
  362. # endif
  363. # ifdef HAVE_SYS_DIR_H
  364. # include <sys/dir.h>
  365. # endif
  366. # ifdef HAVE_NDIR_H
  367. # include <ndir.h>
  368. # endif
  369. #endif
  370. #ifdef _MSC_VER
  371. # ifdef HAVE_DIRECT_H
  372. # include <direct.h>
  373. # endif
  374. # ifdef HAVE_IO_H
  375. # include <io.h>
  376. # endif
  377. # ifdef HAVE_PROCESS_H
  378. # include <process.h>
  379. # endif
  380. # include <malloc.h>
  381. #endif /* _MSC_VER */
  382. #ifndef MAXPATHLEN
  383. # if defined(PATH_MAX) && PATH_MAX > 1024
  384. # define MAXPATHLEN PATH_MAX
  385. # else
  386. # define MAXPATHLEN 1024
  387. # endif
  388. #endif /* MAXPATHLEN */
  389. #ifdef UNION_WAIT
  390. /* Emulate some macros on systems that have a union instead of macros */
  391. # ifndef WIFEXITED
  392. # define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
  393. # endif
  394. # ifndef WEXITSTATUS
  395. # define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
  396. # endif
  397. # ifndef WTERMSIG
  398. # define WTERMSIG(u_wait) ((u_wait).w_termsig)
  399. # endif
  400. # define WAIT_TYPE union wait
  401. # define WAIT_STATUS_INT(s) (s.w_status)
  402. #else
  403. /* !UNION_WAIT */
  404. # define WAIT_TYPE int
  405. # define WAIT_STATUS_INT(s) (s)
  406. #endif /* UNION_WAIT */
  407. /* Don't use the "_r" form if we don't need it (also, won't have a
  408. prototype for it, at least on Solaris -- maybe others as well?). */
  409. #if defined(HAVE_CTERMID_R)
  410. # define USE_CTERMID_R
  411. #endif
  412. /* choose the appropriate stat and fstat functions and return structs */
  413. #undef STAT
  414. #undef FSTAT
  415. #undef STRUCT_STAT
  416. #ifdef MS_WINDOWS
  417. # define STAT win32_stat
  418. # define LSTAT win32_lstat
  419. # define FSTAT _Py_fstat_noraise
  420. # define STRUCT_STAT struct _Py_stat_struct
  421. #else
  422. # define STAT stat
  423. # define LSTAT lstat
  424. # define FSTAT fstat
  425. # define STRUCT_STAT struct stat
  426. #endif
  427. #if defined(MAJOR_IN_MKDEV)
  428. # include <sys/mkdev.h>
  429. #else
  430. # if defined(MAJOR_IN_SYSMACROS)
  431. # include <sys/sysmacros.h>
  432. # endif
  433. # if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
  434. # include <sys/mkdev.h>
  435. # endif
  436. #endif
  437. #ifdef MS_WINDOWS
  438. # define INITFUNC PyInit_nt
  439. # define MODNAME "nt"
  440. # define MODNAME_OBJ &_Py_ID(nt)
  441. #else
  442. # define INITFUNC PyInit_posix
  443. # define MODNAME "posix"
  444. # define MODNAME_OBJ &_Py_ID(posix)
  445. #endif
  446. #if defined(__sun)
  447. /* Something to implement in autoconf, not present in autoconf 2.69 */
  448. # define HAVE_STRUCT_STAT_ST_FSTYPE 1
  449. #endif
  450. /* memfd_create is either defined in sys/mman.h or sys/memfd.h
  451. * linux/memfd.h defines additional flags
  452. */
  453. #ifdef HAVE_SYS_MMAN_H
  454. # include <sys/mman.h>
  455. #endif
  456. #ifdef HAVE_SYS_MEMFD_H
  457. # error #include <sys/memfd.h>
  458. #endif
  459. #ifdef HAVE_LINUX_MEMFD_H
  460. # include <linux/memfd.h>
  461. #endif
  462. /* eventfd() */
  463. #ifdef HAVE_SYS_EVENTFD_H
  464. # include <sys/eventfd.h>
  465. #endif
  466. #ifdef _Py_MEMORY_SANITIZER
  467. # include <sanitizer/msan_interface.h>
  468. #endif
  469. #ifdef HAVE_FORK
  470. static void
  471. run_at_forkers(PyObject *lst, int reverse)
  472. {
  473. Py_ssize_t i;
  474. PyObject *cpy;
  475. if (lst != NULL) {
  476. assert(PyList_CheckExact(lst));
  477. /* Use a list copy in case register_at_fork() is called from
  478. * one of the callbacks.
  479. */
  480. cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
  481. if (cpy == NULL)
  482. PyErr_WriteUnraisable(lst);
  483. else {
  484. if (reverse)
  485. PyList_Reverse(cpy);
  486. for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
  487. PyObject *func, *res;
  488. func = PyList_GET_ITEM(cpy, i);
  489. res = _PyObject_CallNoArgs(func);
  490. if (res == NULL)
  491. PyErr_WriteUnraisable(func);
  492. else
  493. Py_DECREF(res);
  494. }
  495. Py_DECREF(cpy);
  496. }
  497. }
  498. }
  499. void
  500. PyOS_BeforeFork(void)
  501. {
  502. PyInterpreterState *interp = _PyInterpreterState_GET();
  503. run_at_forkers(interp->before_forkers, 1);
  504. _PyImport_AcquireLock(interp);
  505. }
  506. void
  507. PyOS_AfterFork_Parent(void)
  508. {
  509. PyInterpreterState *interp = _PyInterpreterState_GET();
  510. if (_PyImport_ReleaseLock(interp) <= 0) {
  511. Py_FatalError("failed releasing import lock after fork");
  512. }
  513. run_at_forkers(interp->after_forkers_parent, 0);
  514. }
  515. void
  516. PyOS_AfterFork_Child(void)
  517. {
  518. PyStatus status;
  519. _PyRuntimeState *runtime = &_PyRuntime;
  520. status = _PyRuntimeState_ReInitThreads(runtime);
  521. if (_PyStatus_EXCEPTION(status)) {
  522. goto fatal_error;
  523. }
  524. PyThreadState *tstate = _PyThreadState_GET();
  525. _Py_EnsureTstateNotNULL(tstate);
  526. #ifdef PY_HAVE_THREAD_NATIVE_ID
  527. tstate->native_thread_id = PyThread_get_thread_native_id();
  528. #endif
  529. status = _PyEval_ReInitThreads(tstate);
  530. if (_PyStatus_EXCEPTION(status)) {
  531. goto fatal_error;
  532. }
  533. status = _PyImport_ReInitLock(tstate->interp);
  534. if (_PyStatus_EXCEPTION(status)) {
  535. goto fatal_error;
  536. }
  537. _PySignal_AfterFork();
  538. status = _PyInterpreterState_DeleteExceptMain(runtime);
  539. if (_PyStatus_EXCEPTION(status)) {
  540. goto fatal_error;
  541. }
  542. assert(_PyThreadState_GET() == tstate);
  543. status = _PyPerfTrampoline_AfterFork_Child();
  544. if (_PyStatus_EXCEPTION(status)) {
  545. goto fatal_error;
  546. }
  547. run_at_forkers(tstate->interp->after_forkers_child, 0);
  548. return;
  549. fatal_error:
  550. Py_ExitStatusException(status);
  551. }
  552. static int
  553. register_at_forker(PyObject **lst, PyObject *func)
  554. {
  555. if (func == NULL) /* nothing to register? do nothing. */
  556. return 0;
  557. if (*lst == NULL) {
  558. *lst = PyList_New(0);
  559. if (*lst == NULL)
  560. return -1;
  561. }
  562. return PyList_Append(*lst, func);
  563. }
  564. #endif /* HAVE_FORK */
  565. /* Legacy wrapper */
  566. void
  567. PyOS_AfterFork(void)
  568. {
  569. #ifdef HAVE_FORK
  570. PyOS_AfterFork_Child();
  571. #endif
  572. }
  573. #ifdef MS_WINDOWS
  574. /* defined in fileutils.c */
  575. void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
  576. void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, ULONG,
  577. FILE_BASIC_INFO *, FILE_ID_INFO *,
  578. struct _Py_stat_struct *);
  579. void _Py_stat_basic_info_to_stat(FILE_STAT_BASIC_INFORMATION *,
  580. struct _Py_stat_struct *);
  581. #endif
  582. #ifndef MS_WINDOWS
  583. PyObject *
  584. _PyLong_FromUid(uid_t uid)
  585. {
  586. if (uid == (uid_t)-1)
  587. return PyLong_FromLong(-1);
  588. return PyLong_FromUnsignedLong(uid);
  589. }
  590. PyObject *
  591. _PyLong_FromGid(gid_t gid)
  592. {
  593. if (gid == (gid_t)-1)
  594. return PyLong_FromLong(-1);
  595. return PyLong_FromUnsignedLong(gid);
  596. }
  597. int
  598. _Py_Uid_Converter(PyObject *obj, uid_t *p)
  599. {
  600. uid_t uid;
  601. PyObject *index;
  602. int overflow;
  603. long result;
  604. unsigned long uresult;
  605. index = _PyNumber_Index(obj);
  606. if (index == NULL) {
  607. PyErr_Format(PyExc_TypeError,
  608. "uid should be integer, not %.200s",
  609. _PyType_Name(Py_TYPE(obj)));
  610. return 0;
  611. }
  612. /*
  613. * Handling uid_t is complicated for two reasons:
  614. * * Although uid_t is (always?) unsigned, it still
  615. * accepts -1.
  616. * * We don't know its size in advance--it may be
  617. * bigger than an int, or it may be smaller than
  618. * a long.
  619. *
  620. * So a bit of defensive programming is in order.
  621. * Start with interpreting the value passed
  622. * in as a signed long and see if it works.
  623. */
  624. result = PyLong_AsLongAndOverflow(index, &overflow);
  625. if (!overflow) {
  626. uid = (uid_t)result;
  627. if (result == -1) {
  628. if (PyErr_Occurred())
  629. goto fail;
  630. /* It's a legitimate -1, we're done. */
  631. goto success;
  632. }
  633. /* Any other negative number is disallowed. */
  634. if (result < 0)
  635. goto underflow;
  636. /* Ensure the value wasn't truncated. */
  637. if (sizeof(uid_t) < sizeof(long) &&
  638. (long)uid != result)
  639. goto underflow;
  640. goto success;
  641. }
  642. if (overflow < 0)
  643. goto underflow;
  644. /*
  645. * Okay, the value overflowed a signed long. If it
  646. * fits in an *unsigned* long, it may still be okay,
  647. * as uid_t may be unsigned long on this platform.
  648. */
  649. uresult = PyLong_AsUnsignedLong(index);
  650. if (PyErr_Occurred()) {
  651. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  652. goto overflow;
  653. goto fail;
  654. }
  655. uid = (uid_t)uresult;
  656. /*
  657. * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
  658. * but this value would get interpreted as (uid_t)-1 by chown
  659. * and its siblings. That's not what the user meant! So we
  660. * throw an overflow exception instead. (We already
  661. * handled a real -1 with PyLong_AsLongAndOverflow() above.)
  662. */
  663. if (uid == (uid_t)-1)
  664. goto overflow;
  665. /* Ensure the value wasn't truncated. */
  666. if (sizeof(uid_t) < sizeof(long) &&
  667. (unsigned long)uid != uresult)
  668. goto overflow;
  669. /* fallthrough */
  670. success:
  671. Py_DECREF(index);
  672. *p = uid;
  673. return 1;
  674. underflow:
  675. PyErr_SetString(PyExc_OverflowError,
  676. "uid is less than minimum");
  677. goto fail;
  678. overflow:
  679. PyErr_SetString(PyExc_OverflowError,
  680. "uid is greater than maximum");
  681. /* fallthrough */
  682. fail:
  683. Py_DECREF(index);
  684. return 0;
  685. }
  686. int
  687. _Py_Gid_Converter(PyObject *obj, gid_t *p)
  688. {
  689. gid_t gid;
  690. PyObject *index;
  691. int overflow;
  692. long result;
  693. unsigned long uresult;
  694. index = _PyNumber_Index(obj);
  695. if (index == NULL) {
  696. PyErr_Format(PyExc_TypeError,
  697. "gid should be integer, not %.200s",
  698. _PyType_Name(Py_TYPE(obj)));
  699. return 0;
  700. }
  701. /*
  702. * Handling gid_t is complicated for two reasons:
  703. * * Although gid_t is (always?) unsigned, it still
  704. * accepts -1.
  705. * * We don't know its size in advance--it may be
  706. * bigger than an int, or it may be smaller than
  707. * a long.
  708. *
  709. * So a bit of defensive programming is in order.
  710. * Start with interpreting the value passed
  711. * in as a signed long and see if it works.
  712. */
  713. result = PyLong_AsLongAndOverflow(index, &overflow);
  714. if (!overflow) {
  715. gid = (gid_t)result;
  716. if (result == -1) {
  717. if (PyErr_Occurred())
  718. goto fail;
  719. /* It's a legitimate -1, we're done. */
  720. goto success;
  721. }
  722. /* Any other negative number is disallowed. */
  723. if (result < 0) {
  724. goto underflow;
  725. }
  726. /* Ensure the value wasn't truncated. */
  727. if (sizeof(gid_t) < sizeof(long) &&
  728. (long)gid != result)
  729. goto underflow;
  730. goto success;
  731. }
  732. if (overflow < 0)
  733. goto underflow;
  734. /*
  735. * Okay, the value overflowed a signed long. If it
  736. * fits in an *unsigned* long, it may still be okay,
  737. * as gid_t may be unsigned long on this platform.
  738. */
  739. uresult = PyLong_AsUnsignedLong(index);
  740. if (PyErr_Occurred()) {
  741. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  742. goto overflow;
  743. goto fail;
  744. }
  745. gid = (gid_t)uresult;
  746. /*
  747. * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
  748. * but this value would get interpreted as (gid_t)-1 by chown
  749. * and its siblings. That's not what the user meant! So we
  750. * throw an overflow exception instead. (We already
  751. * handled a real -1 with PyLong_AsLongAndOverflow() above.)
  752. */
  753. if (gid == (gid_t)-1)
  754. goto overflow;
  755. /* Ensure the value wasn't truncated. */
  756. if (sizeof(gid_t) < sizeof(long) &&
  757. (unsigned long)gid != uresult)
  758. goto overflow;
  759. /* fallthrough */
  760. success:
  761. Py_DECREF(index);
  762. *p = gid;
  763. return 1;
  764. underflow:
  765. PyErr_SetString(PyExc_OverflowError,
  766. "gid is less than minimum");
  767. goto fail;
  768. overflow:
  769. PyErr_SetString(PyExc_OverflowError,
  770. "gid is greater than maximum");
  771. /* fallthrough */
  772. fail:
  773. Py_DECREF(index);
  774. return 0;
  775. }
  776. #endif /* MS_WINDOWS */
  777. static PyObject *
  778. _PyLong_FromDev(dev_t dev)
  779. {
  780. #ifdef NODEV
  781. if (dev == NODEV) {
  782. return PyLong_FromLongLong((long long)dev);
  783. }
  784. #endif
  785. return PyLong_FromUnsignedLongLong((unsigned long long)dev);
  786. }
  787. #if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) || defined(HAVE_DEVICE_MACROS)
  788. static int
  789. _Py_Dev_Converter(PyObject *obj, void *p)
  790. {
  791. #ifdef NODEV
  792. if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
  793. int overflow;
  794. long long result = PyLong_AsLongLongAndOverflow(obj, &overflow);
  795. if (result == -1 && PyErr_Occurred()) {
  796. return 0;
  797. }
  798. if (!overflow && result == (long long)NODEV) {
  799. *((dev_t *)p) = NODEV;
  800. return 1;
  801. }
  802. }
  803. #endif
  804. unsigned long long result = PyLong_AsUnsignedLongLong(obj);
  805. if (result == (unsigned long long)-1 && PyErr_Occurred()) {
  806. return 0;
  807. }
  808. if ((unsigned long long)(dev_t)result != result) {
  809. PyErr_SetString(PyExc_OverflowError,
  810. "Python int too large to convert to C dev_t");
  811. return 0;
  812. }
  813. *((dev_t *)p) = (dev_t)result;
  814. return 1;
  815. }
  816. #endif /* (HAVE_MKNOD && HAVE_MAKEDEV) || HAVE_DEVICE_MACROS */
  817. #ifdef AT_FDCWD
  818. /*
  819. * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
  820. * without the int cast, the value gets interpreted as uint (4291925331),
  821. * which doesn't play nicely with all the initializer lines in this file that
  822. * look like this:
  823. * int dir_fd = DEFAULT_DIR_FD;
  824. */
  825. #define DEFAULT_DIR_FD (int)AT_FDCWD
  826. #else
  827. #define DEFAULT_DIR_FD (-100)
  828. #endif
  829. static int
  830. _fd_converter(PyObject *o, int *p)
  831. {
  832. int overflow;
  833. long long_value;
  834. PyObject *index = _PyNumber_Index(o);
  835. if (index == NULL) {
  836. return 0;
  837. }
  838. assert(PyLong_Check(index));
  839. long_value = PyLong_AsLongAndOverflow(index, &overflow);
  840. Py_DECREF(index);
  841. assert(!PyErr_Occurred());
  842. if (overflow > 0 || long_value > INT_MAX) {
  843. PyErr_SetString(PyExc_OverflowError,
  844. "fd is greater than maximum");
  845. return 0;
  846. }
  847. if (overflow < 0 || long_value < INT_MIN) {
  848. PyErr_SetString(PyExc_OverflowError,
  849. "fd is less than minimum");
  850. return 0;
  851. }
  852. *p = (int)long_value;
  853. return 1;
  854. }
  855. static int
  856. dir_fd_converter(PyObject *o, void *p)
  857. {
  858. if (o == Py_None) {
  859. *(int *)p = DEFAULT_DIR_FD;
  860. return 1;
  861. }
  862. else if (PyIndex_Check(o)) {
  863. return _fd_converter(o, (int *)p);
  864. }
  865. else {
  866. PyErr_Format(PyExc_TypeError,
  867. "argument should be integer or None, not %.200s",
  868. _PyType_Name(Py_TYPE(o)));
  869. return 0;
  870. }
  871. }
  872. typedef struct {
  873. PyObject *billion;
  874. PyObject *DirEntryType;
  875. PyObject *ScandirIteratorType;
  876. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  877. PyObject *SchedParamType;
  878. #endif
  879. newfunc statresult_new_orig;
  880. PyObject *StatResultType;
  881. PyObject *StatVFSResultType;
  882. PyObject *TerminalSizeType;
  883. PyObject *TimesResultType;
  884. PyObject *UnameResultType;
  885. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  886. PyObject *WaitidResultType;
  887. #endif
  888. #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
  889. PyObject *struct_rusage;
  890. #endif
  891. PyObject *st_mode;
  892. } _posixstate;
  893. static inline _posixstate*
  894. get_posix_state(PyObject *module)
  895. {
  896. void *state = _PyModule_GetState(module);
  897. assert(state != NULL);
  898. return (_posixstate *)state;
  899. }
  900. /*
  901. * A PyArg_ParseTuple "converter" function
  902. * that handles filesystem paths in the manner
  903. * preferred by the os module.
  904. *
  905. * path_converter accepts (Unicode) strings and their
  906. * subclasses, and bytes and their subclasses. What
  907. * it does with the argument depends on path.make_wide:
  908. *
  909. * * If path.make_wide is nonzero, if we get a (Unicode)
  910. * string we extract the wchar_t * and return it; if we
  911. * get bytes we decode to wchar_t * and return that.
  912. *
  913. * * If path.make_wide is zero, if we get bytes we extract
  914. * the char_t * and return it; if we get a (Unicode)
  915. * string we encode to char_t * and return that.
  916. *
  917. * path_converter also optionally accepts signed
  918. * integers (representing open file descriptors) instead
  919. * of path strings.
  920. *
  921. * Input fields:
  922. * path.nullable
  923. * If nonzero, the path is permitted to be None.
  924. * path.nonstrict
  925. * If nonzero, the path is permitted to contain
  926. * embedded null characters and have any length.
  927. * path.make_wide
  928. * If nonzero, the converter always uses wide, decoding if necessary, else
  929. * it always uses narrow, encoding if necessary. The default value is
  930. * nonzero on Windows, else zero.
  931. * path.suppress_value_error
  932. * If nonzero, raising ValueError is suppressed.
  933. * path.allow_fd
  934. * If nonzero, the path is permitted to be a file handle
  935. * (a signed int) instead of a string.
  936. * path.function_name
  937. * If non-NULL, path_converter will use that as the name
  938. * of the function in error messages.
  939. * (If path.function_name is NULL it omits the function name.)
  940. * path.argument_name
  941. * If non-NULL, path_converter will use that as the name
  942. * of the parameter in error messages.
  943. * (If path.argument_name is NULL it uses "path".)
  944. *
  945. * Output fields:
  946. * path.wide
  947. * Points to the path if it was expressed as Unicode
  948. * or if it was bytes and decoded to Unicode.
  949. * path.narrow
  950. * Points to the path if it was expressed as bytes,
  951. * or if it was Unicode and encoded to bytes.
  952. * path.fd
  953. * Contains a file descriptor if path.accept_fd was true
  954. * and the caller provided a signed integer instead of any
  955. * sort of string.
  956. *
  957. * WARNING: if your "path" parameter is optional, and is
  958. * unspecified, path_converter will never get called.
  959. * So if you set allow_fd, you *MUST* initialize path.fd = -1
  960. * yourself!
  961. * path.value_error
  962. * If nonzero, then suppress_value_error was specified and a ValueError
  963. * occurred.
  964. * path.length
  965. * The length of the path in characters, if specified as
  966. * a string.
  967. * path.object
  968. * The original object passed in (if get a PathLike object,
  969. * the result of PyOS_FSPath() is treated as the original object).
  970. * Own a reference to the object.
  971. * path.cleanup
  972. * For internal use only. May point to a temporary object.
  973. * (Pay no attention to the man behind the curtain.)
  974. *
  975. * At most one of path.wide or path.narrow will be non-NULL.
  976. * If path was None and path.nullable was set,
  977. * or if path was an integer and path.allow_fd was set,
  978. * both path.wide and path.narrow will be NULL
  979. * and path.length will be 0.
  980. *
  981. * path_converter takes care to not write to the path_t
  982. * unless it's successful. However it must reset the
  983. * "cleanup" field each time it's called.
  984. *
  985. * Use as follows:
  986. * path_t path;
  987. * memset(&path, 0, sizeof(path));
  988. * PyArg_ParseTuple(args, "O&", path_converter, &path);
  989. * // ... use values from path ...
  990. * path_cleanup(&path);
  991. *
  992. * (Note that if PyArg_Parse fails you don't need to call
  993. * path_cleanup(). However it is safe to do so.)
  994. */
  995. typedef struct {
  996. // Input fields
  997. const char *function_name;
  998. const char *argument_name;
  999. int nullable;
  1000. int nonstrict;
  1001. int make_wide;
  1002. int suppress_value_error;
  1003. int allow_fd;
  1004. // Output fields
  1005. const wchar_t *wide;
  1006. const char *narrow;
  1007. int fd;
  1008. int value_error;
  1009. Py_ssize_t length;
  1010. PyObject *object;
  1011. PyObject *cleanup;
  1012. } path_t;
  1013. #define PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, \
  1014. make_wide, suppress_value_error, allow_fd) \
  1015. {function_name, argument_name, nullable, nonstrict, make_wide, \
  1016. suppress_value_error, allow_fd, NULL, NULL, -1, 0, 0, NULL, NULL}
  1017. #ifdef MS_WINDOWS
  1018. #define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
  1019. nonstrict, suppress_value_error, allow_fd) \
  1020. PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 1, \
  1021. suppress_value_error, allow_fd)
  1022. #else
  1023. #define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
  1024. nonstrict, suppress_value_error, allow_fd) \
  1025. PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 0, \
  1026. suppress_value_error, allow_fd)
  1027. #endif
  1028. static void
  1029. path_cleanup(path_t *path)
  1030. {
  1031. wchar_t *wide = (wchar_t *)path->wide;
  1032. path->wide = NULL;
  1033. PyMem_Free(wide);
  1034. Py_CLEAR(path->object);
  1035. Py_CLEAR(path->cleanup);
  1036. }
  1037. static int
  1038. path_converter(PyObject *o, void *p)
  1039. {
  1040. path_t *path = (path_t *)p;
  1041. PyObject *bytes = NULL;
  1042. Py_ssize_t length = 0;
  1043. int is_index, is_bytes, is_unicode;
  1044. const char *narrow;
  1045. PyObject *wo = NULL;
  1046. wchar_t *wide = NULL;
  1047. #define FORMAT_EXCEPTION(exc, fmt) \
  1048. PyErr_Format(exc, "%s%s" fmt, \
  1049. path->function_name ? path->function_name : "", \
  1050. path->function_name ? ": " : "", \
  1051. path->argument_name ? path->argument_name : "path")
  1052. /* Py_CLEANUP_SUPPORTED support */
  1053. if (o == NULL) {
  1054. path_cleanup(path);
  1055. return 1;
  1056. }
  1057. /* Ensure it's always safe to call path_cleanup(). */
  1058. path->object = path->cleanup = NULL;
  1059. /* path->object owns a reference to the original object */
  1060. Py_INCREF(o);
  1061. if ((o == Py_None) && path->nullable) {
  1062. path->wide = NULL;
  1063. path->narrow = NULL;
  1064. path->fd = -1;
  1065. goto success_exit;
  1066. }
  1067. /* Only call this here so that we don't treat the return value of
  1068. os.fspath() as an fd or buffer. */
  1069. is_index = path->allow_fd && PyIndex_Check(o);
  1070. is_bytes = PyBytes_Check(o);
  1071. is_unicode = PyUnicode_Check(o);
  1072. if (!is_index && !is_unicode && !is_bytes) {
  1073. /* Inline PyOS_FSPath() for better error messages. */
  1074. PyObject *func, *res;
  1075. func = _PyObject_LookupSpecial(o, &_Py_ID(__fspath__));
  1076. if (NULL == func) {
  1077. goto error_format;
  1078. }
  1079. res = _PyObject_CallNoArgs(func);
  1080. Py_DECREF(func);
  1081. if (NULL == res) {
  1082. goto error_exit;
  1083. }
  1084. else if (PyUnicode_Check(res)) {
  1085. is_unicode = 1;
  1086. }
  1087. else if (PyBytes_Check(res)) {
  1088. is_bytes = 1;
  1089. }
  1090. else {
  1091. PyErr_Format(PyExc_TypeError,
  1092. "expected %.200s.__fspath__() to return str or bytes, "
  1093. "not %.200s", _PyType_Name(Py_TYPE(o)),
  1094. _PyType_Name(Py_TYPE(res)));
  1095. Py_DECREF(res);
  1096. goto error_exit;
  1097. }
  1098. /* still owns a reference to the original object */
  1099. Py_SETREF(o, res);
  1100. }
  1101. if (is_unicode) {
  1102. if (path->make_wide) {
  1103. wide = PyUnicode_AsWideCharString(o, &length);
  1104. if (!wide) {
  1105. goto error_exit;
  1106. }
  1107. #ifdef MS_WINDOWS
  1108. if (!path->nonstrict && length > 32767) {
  1109. FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
  1110. goto error_exit;
  1111. }
  1112. #endif
  1113. if (!path->nonstrict && wcslen(wide) != (size_t)length) {
  1114. FORMAT_EXCEPTION(PyExc_ValueError,
  1115. "embedded null character in %s");
  1116. goto error_exit;
  1117. }
  1118. path->wide = wide;
  1119. path->narrow = NULL;
  1120. path->fd = -1;
  1121. wide = NULL;
  1122. goto success_exit;
  1123. }
  1124. bytes = PyUnicode_EncodeFSDefault(o);
  1125. if (!bytes) {
  1126. goto error_exit;
  1127. }
  1128. }
  1129. else if (is_bytes) {
  1130. bytes = Py_NewRef(o);
  1131. }
  1132. else if (is_index) {
  1133. if (!_fd_converter(o, &path->fd)) {
  1134. goto error_exit;
  1135. }
  1136. path->wide = NULL;
  1137. path->narrow = NULL;
  1138. goto success_exit;
  1139. }
  1140. else {
  1141. error_format:
  1142. PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
  1143. path->function_name ? path->function_name : "",
  1144. path->function_name ? ": " : "",
  1145. path->argument_name ? path->argument_name : "path",
  1146. path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
  1147. "integer or None" :
  1148. path->allow_fd ? "string, bytes, os.PathLike or integer" :
  1149. path->nullable ? "string, bytes, os.PathLike or None" :
  1150. "string, bytes or os.PathLike",
  1151. _PyType_Name(Py_TYPE(o)));
  1152. goto error_exit;
  1153. }
  1154. length = PyBytes_GET_SIZE(bytes);
  1155. narrow = PyBytes_AS_STRING(bytes);
  1156. if (!path->nonstrict && strlen(narrow) != (size_t)length) {
  1157. FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
  1158. goto error_exit;
  1159. }
  1160. if (path->make_wide) {
  1161. wo = PyUnicode_DecodeFSDefaultAndSize(narrow, length);
  1162. if (!wo) {
  1163. goto error_exit;
  1164. }
  1165. wide = PyUnicode_AsWideCharString(wo, &length);
  1166. Py_DECREF(wo);
  1167. if (!wide) {
  1168. goto error_exit;
  1169. }
  1170. #ifdef MS_WINDOWS
  1171. if (!path->nonstrict && length > 32767) {
  1172. FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
  1173. goto error_exit;
  1174. }
  1175. #endif
  1176. if (!path->nonstrict && wcslen(wide) != (size_t)length) {
  1177. FORMAT_EXCEPTION(PyExc_ValueError,
  1178. "embedded null character in %s");
  1179. goto error_exit;
  1180. }
  1181. path->wide = wide;
  1182. path->narrow = NULL;
  1183. Py_DECREF(bytes);
  1184. wide = NULL;
  1185. }
  1186. else {
  1187. path->wide = NULL;
  1188. path->narrow = narrow;
  1189. if (bytes == o) {
  1190. /* Still a reference owned by path->object, don't have to
  1191. worry about path->narrow is used after free. */
  1192. Py_DECREF(bytes);
  1193. }
  1194. else {
  1195. path->cleanup = bytes;
  1196. }
  1197. }
  1198. path->fd = -1;
  1199. success_exit:
  1200. path->value_error = 0;
  1201. path->length = length;
  1202. path->object = o;
  1203. return Py_CLEANUP_SUPPORTED;
  1204. error_exit:
  1205. Py_XDECREF(o);
  1206. Py_XDECREF(bytes);
  1207. PyMem_Free(wide);
  1208. if (!path->suppress_value_error ||
  1209. !PyErr_ExceptionMatches(PyExc_ValueError))
  1210. {
  1211. return 0;
  1212. }
  1213. PyErr_Clear();
  1214. path->wide = NULL;
  1215. path->narrow = NULL;
  1216. path->fd = -1;
  1217. path->value_error = 1;
  1218. path->length = 0;
  1219. path->object = NULL;
  1220. return Py_CLEANUP_SUPPORTED;
  1221. }
  1222. static void
  1223. argument_unavailable_error(const char *function_name, const char *argument_name)
  1224. {
  1225. PyErr_Format(PyExc_NotImplementedError,
  1226. "%s%s%s unavailable on this platform",
  1227. (function_name != NULL) ? function_name : "",
  1228. (function_name != NULL) ? ": ": "",
  1229. argument_name);
  1230. }
  1231. static int
  1232. dir_fd_unavailable(PyObject *o, void *p)
  1233. {
  1234. int dir_fd;
  1235. if (!dir_fd_converter(o, &dir_fd))
  1236. return 0;
  1237. if (dir_fd != DEFAULT_DIR_FD) {
  1238. argument_unavailable_error(NULL, "dir_fd");
  1239. return 0;
  1240. }
  1241. *(int *)p = dir_fd;
  1242. return 1;
  1243. }
  1244. static int
  1245. fd_specified(const char *function_name, int fd)
  1246. {
  1247. if (fd == -1)
  1248. return 0;
  1249. argument_unavailable_error(function_name, "fd");
  1250. return 1;
  1251. }
  1252. static int
  1253. follow_symlinks_specified(const char *function_name, int follow_symlinks)
  1254. {
  1255. if (follow_symlinks)
  1256. return 0;
  1257. argument_unavailable_error(function_name, "follow_symlinks");
  1258. return 1;
  1259. }
  1260. static int
  1261. path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
  1262. {
  1263. if (!path->wide && (dir_fd != DEFAULT_DIR_FD) && !path->narrow) {
  1264. PyErr_Format(PyExc_ValueError,
  1265. "%s: can't specify dir_fd without matching path",
  1266. function_name);
  1267. return 1;
  1268. }
  1269. return 0;
  1270. }
  1271. static int
  1272. dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
  1273. {
  1274. if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
  1275. PyErr_Format(PyExc_ValueError,
  1276. "%s: can't specify both dir_fd and fd",
  1277. function_name);
  1278. return 1;
  1279. }
  1280. return 0;
  1281. }
  1282. static int
  1283. fd_and_follow_symlinks_invalid(const char *function_name, int fd,
  1284. int follow_symlinks)
  1285. {
  1286. if ((fd > 0) && (!follow_symlinks)) {
  1287. PyErr_Format(PyExc_ValueError,
  1288. "%s: cannot use fd and follow_symlinks together",
  1289. function_name);
  1290. return 1;
  1291. }
  1292. return 0;
  1293. }
  1294. static int
  1295. dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
  1296. int follow_symlinks)
  1297. {
  1298. if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
  1299. PyErr_Format(PyExc_ValueError,
  1300. "%s: cannot use dir_fd and follow_symlinks together",
  1301. function_name);
  1302. return 1;
  1303. }
  1304. return 0;
  1305. }
  1306. #ifdef MS_WINDOWS
  1307. typedef long long Py_off_t;
  1308. #else
  1309. typedef off_t Py_off_t;
  1310. #endif
  1311. static int
  1312. Py_off_t_converter(PyObject *arg, void *addr)
  1313. {
  1314. #ifdef HAVE_LARGEFILE_SUPPORT
  1315. *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
  1316. #else
  1317. *((Py_off_t *)addr) = PyLong_AsLong(arg);
  1318. #endif
  1319. if (PyErr_Occurred())
  1320. return 0;
  1321. return 1;
  1322. }
  1323. static PyObject *
  1324. PyLong_FromPy_off_t(Py_off_t offset)
  1325. {
  1326. #ifdef HAVE_LARGEFILE_SUPPORT
  1327. return PyLong_FromLongLong(offset);
  1328. #else
  1329. return PyLong_FromLong(offset);
  1330. #endif
  1331. }
  1332. #ifdef HAVE_SIGSET_T
  1333. /* Convert an iterable of integers to a sigset.
  1334. Return 1 on success, return 0 and raise an exception on error. */
  1335. int
  1336. _Py_Sigset_Converter(PyObject *obj, void *addr)
  1337. {
  1338. sigset_t *mask = (sigset_t *)addr;
  1339. PyObject *iterator, *item;
  1340. long signum;
  1341. int overflow;
  1342. // The extra parens suppress the unreachable-code warning with clang on MacOS
  1343. if (sigemptyset(mask) < (0)) {
  1344. /* Probably only if mask == NULL. */
  1345. PyErr_SetFromErrno(PyExc_OSError);
  1346. return 0;
  1347. }
  1348. iterator = PyObject_GetIter(obj);
  1349. if (iterator == NULL) {
  1350. return 0;
  1351. }
  1352. while ((item = PyIter_Next(iterator)) != NULL) {
  1353. signum = PyLong_AsLongAndOverflow(item, &overflow);
  1354. Py_DECREF(item);
  1355. if (signum <= 0 || signum >= Py_NSIG) {
  1356. if (overflow || signum != -1 || !PyErr_Occurred()) {
  1357. PyErr_Format(PyExc_ValueError,
  1358. "signal number %ld out of range [1; %i]",
  1359. signum, Py_NSIG - 1);
  1360. }
  1361. goto error;
  1362. }
  1363. if (sigaddset(mask, (int)signum)) {
  1364. if (errno != EINVAL) {
  1365. /* Probably impossible */
  1366. PyErr_SetFromErrno(PyExc_OSError);
  1367. goto error;
  1368. }
  1369. /* For backwards compatibility, allow idioms such as
  1370. * `range(1, NSIG)` but warn about invalid signal numbers
  1371. */
  1372. const char msg[] =
  1373. "invalid signal number %ld, please use valid_signals()";
  1374. if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
  1375. goto error;
  1376. }
  1377. }
  1378. }
  1379. if (!PyErr_Occurred()) {
  1380. Py_DECREF(iterator);
  1381. return 1;
  1382. }
  1383. error:
  1384. Py_DECREF(iterator);
  1385. return 0;
  1386. }
  1387. #endif /* HAVE_SIGSET_T */
  1388. /* Return a dictionary corresponding to the POSIX environment table */
  1389. #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
  1390. /* On Darwin/MacOSX a shared library or framework has no access to
  1391. ** environ directly, we must obtain it with _NSGetEnviron(). See also
  1392. ** man environ(7).
  1393. */
  1394. #include <crt_externs.h>
  1395. #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
  1396. extern char **environ;
  1397. #endif /* !_MSC_VER */
  1398. static PyObject *
  1399. convertenviron(void)
  1400. {
  1401. PyObject *d;
  1402. #ifdef MS_WINDOWS
  1403. wchar_t **e;
  1404. #else
  1405. char **e;
  1406. #endif
  1407. d = PyDict_New();
  1408. if (d == NULL)
  1409. return NULL;
  1410. #ifdef MS_WINDOWS
  1411. /* _wenviron must be initialized in this way if the program is started
  1412. through main() instead of wmain(). */
  1413. (void)_wgetenv(L"");
  1414. e = _wenviron;
  1415. #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
  1416. /* environ is not accessible as an extern in a shared object on OSX; use
  1417. _NSGetEnviron to resolve it. The value changes if you add environment
  1418. variables between calls to Py_Initialize, so don't cache the value. */
  1419. e = *_NSGetEnviron();
  1420. #else
  1421. e = environ;
  1422. #endif
  1423. if (e == NULL)
  1424. return d;
  1425. for (; *e != NULL; e++) {
  1426. PyObject *k;
  1427. PyObject *v;
  1428. #ifdef MS_WINDOWS
  1429. const wchar_t *p = wcschr(*e, L'=');
  1430. #else
  1431. const char *p = strchr(*e, '=');
  1432. #endif
  1433. if (p == NULL)
  1434. continue;
  1435. #ifdef MS_WINDOWS
  1436. k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
  1437. #else
  1438. k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
  1439. #endif
  1440. if (k == NULL) {
  1441. Py_DECREF(d);
  1442. return NULL;
  1443. }
  1444. #ifdef MS_WINDOWS
  1445. v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
  1446. #else
  1447. v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
  1448. #endif
  1449. if (v == NULL) {
  1450. Py_DECREF(k);
  1451. Py_DECREF(d);
  1452. return NULL;
  1453. }
  1454. if (PyDict_SetDefault(d, k, v) == NULL) {
  1455. Py_DECREF(v);
  1456. Py_DECREF(k);
  1457. Py_DECREF(d);
  1458. return NULL;
  1459. }
  1460. Py_DECREF(k);
  1461. Py_DECREF(v);
  1462. }
  1463. return d;
  1464. }
  1465. /* Set a POSIX-specific error from errno, and return NULL */
  1466. static PyObject *
  1467. posix_error(void)
  1468. {
  1469. return PyErr_SetFromErrno(PyExc_OSError);
  1470. }
  1471. #ifdef MS_WINDOWS
  1472. static PyObject *
  1473. win32_error(const char* function, const char* filename)
  1474. {
  1475. /* XXX We should pass the function name along in the future.
  1476. (winreg.c also wants to pass the function name.)
  1477. This would however require an additional param to the
  1478. Windows error object, which is non-trivial.
  1479. */
  1480. errno = GetLastError();
  1481. if (filename)
  1482. return PyErr_SetFromWindowsErrWithFilename(errno, filename);
  1483. else
  1484. return PyErr_SetFromWindowsErr(errno);
  1485. }
  1486. static PyObject *
  1487. win32_error_object_err(const char* function, PyObject* filename, DWORD err)
  1488. {
  1489. /* XXX - see win32_error for comments on 'function' */
  1490. if (filename)
  1491. return PyErr_SetExcFromWindowsErrWithFilenameObject(
  1492. PyExc_OSError,
  1493. err,
  1494. filename);
  1495. else
  1496. return PyErr_SetFromWindowsErr(err);
  1497. }
  1498. static PyObject *
  1499. win32_error_object(const char* function, PyObject* filename)
  1500. {
  1501. errno = GetLastError();
  1502. return win32_error_object_err(function, filename, errno);
  1503. }
  1504. #endif /* MS_WINDOWS */
  1505. static PyObject *
  1506. posix_path_object_error(PyObject *path)
  1507. {
  1508. return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
  1509. }
  1510. static PyObject *
  1511. path_object_error(PyObject *path)
  1512. {
  1513. #ifdef MS_WINDOWS
  1514. return PyErr_SetExcFromWindowsErrWithFilenameObject(
  1515. PyExc_OSError, 0, path);
  1516. #else
  1517. return posix_path_object_error(path);
  1518. #endif
  1519. }
  1520. static PyObject *
  1521. path_object_error2(PyObject *path, PyObject *path2)
  1522. {
  1523. #ifdef MS_WINDOWS
  1524. return PyErr_SetExcFromWindowsErrWithFilenameObjects(
  1525. PyExc_OSError, 0, path, path2);
  1526. #else
  1527. return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
  1528. #endif
  1529. }
  1530. static PyObject *
  1531. path_error(path_t *path)
  1532. {
  1533. return path_object_error(path->object);
  1534. }
  1535. static PyObject *
  1536. posix_path_error(path_t *path)
  1537. {
  1538. return posix_path_object_error(path->object);
  1539. }
  1540. static PyObject *
  1541. path_error2(path_t *path, path_t *path2)
  1542. {
  1543. return path_object_error2(path->object, path2->object);
  1544. }
  1545. /* POSIX generic methods */
  1546. static PyObject *
  1547. posix_fildes_fd(int fd, int (*func)(int))
  1548. {
  1549. int res;
  1550. int async_err = 0;
  1551. do {
  1552. Py_BEGIN_ALLOW_THREADS
  1553. _Py_BEGIN_SUPPRESS_IPH
  1554. res = (*func)(fd);
  1555. _Py_END_SUPPRESS_IPH
  1556. Py_END_ALLOW_THREADS
  1557. } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  1558. if (res != 0)
  1559. return (!async_err) ? posix_error() : NULL;
  1560. Py_RETURN_NONE;
  1561. }
  1562. #ifdef MS_WINDOWS
  1563. /* This is a reimplementation of the C library's chdir function,
  1564. but one that produces Win32 errors instead of DOS error codes.
  1565. chdir is essentially a wrapper around SetCurrentDirectory; however,
  1566. it also needs to set "magic" environment variables indicating
  1567. the per-drive current directory, which are of the form =<drive>: */
  1568. static BOOL __stdcall
  1569. win32_wchdir(LPCWSTR path)
  1570. {
  1571. wchar_t path_buf[MAX_PATH], *new_path = path_buf;
  1572. int result;
  1573. wchar_t env[4] = L"=x:";
  1574. if(!SetCurrentDirectoryW(path))
  1575. return FALSE;
  1576. result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
  1577. if (!result)
  1578. return FALSE;
  1579. if (result > Py_ARRAY_LENGTH(path_buf)) {
  1580. new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
  1581. if (!new_path) {
  1582. SetLastError(ERROR_OUTOFMEMORY);
  1583. return FALSE;
  1584. }
  1585. result = GetCurrentDirectoryW(result, new_path);
  1586. if (!result) {
  1587. PyMem_RawFree(new_path);
  1588. return FALSE;
  1589. }
  1590. }
  1591. int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
  1592. wcsncmp(new_path, L"//", 2) == 0);
  1593. if (!is_unc_like_path) {
  1594. env[1] = new_path[0];
  1595. result = SetEnvironmentVariableW(env, new_path);
  1596. }
  1597. if (new_path != path_buf)
  1598. PyMem_RawFree(new_path);
  1599. return result ? TRUE : FALSE;
  1600. }
  1601. #endif
  1602. #ifdef MS_WINDOWS
  1603. /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
  1604. - time stamps are restricted to second resolution
  1605. - file modification times suffer from forth-and-back conversions between
  1606. UTC and local time
  1607. Therefore, we implement our own stat, based on the Win32 API directly.
  1608. */
  1609. #define HAVE_STAT_NSEC 1
  1610. #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
  1611. #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
  1612. static void
  1613. find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
  1614. BY_HANDLE_FILE_INFORMATION *info,
  1615. ULONG *reparse_tag)
  1616. {
  1617. memset(info, 0, sizeof(*info));
  1618. info->dwFileAttributes = pFileData->dwFileAttributes;
  1619. info->ftCreationTime = pFileData->ftCreationTime;
  1620. info->ftLastAccessTime = pFileData->ftLastAccessTime;
  1621. info->ftLastWriteTime = pFileData->ftLastWriteTime;
  1622. info->nFileSizeHigh = pFileData->nFileSizeHigh;
  1623. info->nFileSizeLow = pFileData->nFileSizeLow;
  1624. /* info->nNumberOfLinks = 1; */
  1625. if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  1626. *reparse_tag = pFileData->dwReserved0;
  1627. else
  1628. *reparse_tag = 0;
  1629. }
  1630. static BOOL
  1631. attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
  1632. {
  1633. HANDLE hFindFile;
  1634. WIN32_FIND_DATAW FileData;
  1635. LPCWSTR filename = pszFile;
  1636. size_t n = wcslen(pszFile);
  1637. if (n && (pszFile[n - 1] == L'\\' || pszFile[n - 1] == L'/')) {
  1638. // cannot use PyMem_Malloc here because we do not hold the GIL
  1639. filename = (LPCWSTR)malloc((n + 1) * sizeof(filename[0]));
  1640. if(!filename) {
  1641. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  1642. return FALSE;
  1643. }
  1644. wcsncpy_s((LPWSTR)filename, n + 1, pszFile, n);
  1645. while (--n > 0 && (filename[n] == L'\\' || filename[n] == L'/')) {
  1646. ((LPWSTR)filename)[n] = L'\0';
  1647. }
  1648. if (!n || (n == 1 && filename[1] == L':')) {
  1649. // Nothing left to query
  1650. free((void *)filename);
  1651. return FALSE;
  1652. }
  1653. }
  1654. hFindFile = FindFirstFileW(filename, &FileData);
  1655. if (pszFile != filename) {
  1656. free((void *)filename);
  1657. }
  1658. if (hFindFile == INVALID_HANDLE_VALUE) {
  1659. return FALSE;
  1660. }
  1661. FindClose(hFindFile);
  1662. find_data_to_file_info(&FileData, info, reparse_tag);
  1663. return TRUE;
  1664. }
  1665. static void
  1666. update_st_mode_from_path(const wchar_t *path, DWORD attr,
  1667. struct _Py_stat_struct *result)
  1668. {
  1669. if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  1670. /* Fix the file execute permissions. This hack sets S_IEXEC if
  1671. the filename has an extension that is commonly used by files
  1672. that CreateProcessW can execute. A real implementation calls
  1673. GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
  1674. AccessCheck to check for generic read, write, and execute
  1675. access. */
  1676. const wchar_t *fileExtension = wcsrchr(path, '.');
  1677. if (fileExtension) {
  1678. if (_wcsicmp(fileExtension, L".exe") == 0 ||
  1679. _wcsicmp(fileExtension, L".bat") == 0 ||
  1680. _wcsicmp(fileExtension, L".cmd") == 0 ||
  1681. _wcsicmp(fileExtension, L".com") == 0) {
  1682. result->st_mode |= 0111;
  1683. }
  1684. }
  1685. }
  1686. }
  1687. static int
  1688. win32_xstat_slow_impl(const wchar_t *path, struct _Py_stat_struct *result,
  1689. BOOL traverse)
  1690. {
  1691. HANDLE hFile;
  1692. BY_HANDLE_FILE_INFORMATION fileInfo;
  1693. FILE_BASIC_INFO basicInfo;
  1694. FILE_BASIC_INFO *pBasicInfo = NULL;
  1695. FILE_ID_INFO idInfo;
  1696. FILE_ID_INFO *pIdInfo = NULL;
  1697. FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
  1698. DWORD fileType, error;
  1699. BOOL isUnhandledTag = FALSE;
  1700. int retval = 0;
  1701. DWORD access = FILE_READ_ATTRIBUTES;
  1702. DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
  1703. if (!traverse) {
  1704. flags |= FILE_FLAG_OPEN_REPARSE_POINT;
  1705. }
  1706. hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
  1707. if (hFile == INVALID_HANDLE_VALUE) {
  1708. /* Either the path doesn't exist, or the caller lacks access. */
  1709. error = GetLastError();
  1710. switch (error) {
  1711. case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
  1712. case ERROR_SHARING_VIOLATION: /* It's a paging file. */
  1713. /* Try reading the parent directory. */
  1714. if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
  1715. /* Cannot read the parent directory. */
  1716. switch (GetLastError()) {
  1717. case ERROR_FILE_NOT_FOUND: /* File cannot be found */
  1718. case ERROR_PATH_NOT_FOUND: /* File parent directory cannot be found */
  1719. case ERROR_NOT_READY: /* Drive exists but unavailable */
  1720. case ERROR_BAD_NET_NAME: /* Remote drive unavailable */
  1721. break;
  1722. /* Restore the error from CreateFileW(). */
  1723. default:
  1724. SetLastError(error);
  1725. }
  1726. return -1;
  1727. }
  1728. if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  1729. if (traverse ||
  1730. !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
  1731. /* The stat call has to traverse but cannot, so fail. */
  1732. SetLastError(error);
  1733. return -1;
  1734. }
  1735. }
  1736. break;
  1737. case ERROR_INVALID_PARAMETER:
  1738. /* \\.\con requires read or write access. */
  1739. hFile = CreateFileW(path, access | GENERIC_READ,
  1740. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  1741. OPEN_EXISTING, flags, NULL);
  1742. if (hFile == INVALID_HANDLE_VALUE) {
  1743. SetLastError(error);
  1744. return -1;
  1745. }
  1746. break;
  1747. case ERROR_CANT_ACCESS_FILE:
  1748. /* bpo37834: open unhandled reparse points if traverse fails. */
  1749. if (traverse) {
  1750. traverse = FALSE;
  1751. isUnhandledTag = TRUE;
  1752. hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
  1753. flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
  1754. }
  1755. if (hFile == INVALID_HANDLE_VALUE) {
  1756. SetLastError(error);
  1757. return -1;
  1758. }
  1759. break;
  1760. default:
  1761. return -1;
  1762. }
  1763. }
  1764. if (hFile != INVALID_HANDLE_VALUE) {
  1765. /* Handle types other than files on disk. */
  1766. fileType = GetFileType(hFile);
  1767. if (fileType != FILE_TYPE_DISK) {
  1768. if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
  1769. retval = -1;
  1770. goto cleanup;
  1771. }
  1772. DWORD fileAttributes = GetFileAttributesW(path);
  1773. memset(result, 0, sizeof(*result));
  1774. if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
  1775. fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  1776. /* \\.\pipe\ or \\.\mailslot\ */
  1777. result->st_mode = _S_IFDIR;
  1778. } else if (fileType == FILE_TYPE_CHAR) {
  1779. /* \\.\nul */
  1780. result->st_mode = _S_IFCHR;
  1781. } else if (fileType == FILE_TYPE_PIPE) {
  1782. /* \\.\pipe\spam */
  1783. result->st_mode = _S_IFIFO;
  1784. }
  1785. /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
  1786. goto cleanup;
  1787. }
  1788. /* Query the reparse tag, and traverse a non-link. */
  1789. if (!traverse) {
  1790. if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
  1791. &tagInfo, sizeof(tagInfo))) {
  1792. /* Allow devices that do not support FileAttributeTagInfo. */
  1793. switch (GetLastError()) {
  1794. case ERROR_INVALID_PARAMETER:
  1795. case ERROR_INVALID_FUNCTION:
  1796. case ERROR_NOT_SUPPORTED:
  1797. tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
  1798. tagInfo.ReparseTag = 0;
  1799. break;
  1800. default:
  1801. retval = -1;
  1802. goto cleanup;
  1803. }
  1804. } else if (tagInfo.FileAttributes &
  1805. FILE_ATTRIBUTE_REPARSE_POINT) {
  1806. if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
  1807. if (isUnhandledTag) {
  1808. /* Traversing previously failed for either this link
  1809. or its target. */
  1810. SetLastError(ERROR_CANT_ACCESS_FILE);
  1811. retval = -1;
  1812. goto cleanup;
  1813. }
  1814. /* Traverse a non-link, but not if traversing already failed
  1815. for an unhandled tag. */
  1816. } else if (!isUnhandledTag) {
  1817. CloseHandle(hFile);
  1818. return win32_xstat_slow_impl(path, result, TRUE);
  1819. }
  1820. }
  1821. }
  1822. if (!GetFileInformationByHandle(hFile, &fileInfo) ||
  1823. !GetFileInformationByHandleEx(hFile, FileBasicInfo,
  1824. &basicInfo, sizeof(basicInfo))) {
  1825. switch (GetLastError()) {
  1826. case ERROR_INVALID_PARAMETER:
  1827. case ERROR_INVALID_FUNCTION:
  1828. case ERROR_NOT_SUPPORTED:
  1829. /* Volumes and physical disks are block devices, e.g.
  1830. \\.\C: and \\.\PhysicalDrive0. */
  1831. memset(result, 0, sizeof(*result));
  1832. result->st_mode = 0x6000; /* S_IFBLK */
  1833. goto cleanup;
  1834. }
  1835. retval = -1;
  1836. goto cleanup;
  1837. }
  1838. /* Successfully got FileBasicInfo, so we'll pass it along */
  1839. pBasicInfo = &basicInfo;
  1840. if (GetFileInformationByHandleEx(hFile, FileIdInfo, &idInfo, sizeof(idInfo))) {
  1841. /* Successfully got FileIdInfo, so pass it along */
  1842. pIdInfo = &idInfo;
  1843. }
  1844. }
  1845. _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, pBasicInfo, pIdInfo, result);
  1846. update_st_mode_from_path(path, fileInfo.dwFileAttributes, result);
  1847. cleanup:
  1848. if (hFile != INVALID_HANDLE_VALUE) {
  1849. /* Preserve last error if we are failing */
  1850. error = retval ? GetLastError() : 0;
  1851. if (!CloseHandle(hFile)) {
  1852. retval = -1;
  1853. } else if (retval) {
  1854. /* Restore last error */
  1855. SetLastError(error);
  1856. }
  1857. }
  1858. return retval;
  1859. }
  1860. static int
  1861. win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
  1862. BOOL traverse)
  1863. {
  1864. FILE_STAT_BASIC_INFORMATION statInfo;
  1865. if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo,
  1866. &statInfo, sizeof(statInfo))) {
  1867. if (// Cannot use fast path for reparse points ...
  1868. !(statInfo.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  1869. // ... unless it's a name surrogate (symlink) and we're not following
  1870. || (!traverse && IsReparseTagNameSurrogate(statInfo.ReparseTag))
  1871. ) {
  1872. _Py_stat_basic_info_to_stat(&statInfo, result);
  1873. update_st_mode_from_path(path, statInfo.FileAttributes, result);
  1874. return 0;
  1875. }
  1876. } else {
  1877. switch(GetLastError()) {
  1878. case ERROR_FILE_NOT_FOUND:
  1879. case ERROR_PATH_NOT_FOUND:
  1880. case ERROR_NOT_READY:
  1881. case ERROR_BAD_NET_NAME:
  1882. /* These errors aren't worth retrying with the slow path */
  1883. return -1;
  1884. case ERROR_NOT_SUPPORTED:
  1885. /* indicates the API couldn't be loaded */
  1886. break;
  1887. }
  1888. }
  1889. return win32_xstat_slow_impl(path, result, traverse);
  1890. }
  1891. static int
  1892. win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
  1893. {
  1894. /* Protocol violation: we explicitly clear errno, instead of
  1895. setting it to a POSIX error. Callers should use GetLastError. */
  1896. int code = win32_xstat_impl(path, result, traverse);
  1897. errno = 0;
  1898. /* ctime is only deprecated from 3.12, so we copy birthtime across */
  1899. result->st_ctime = result->st_birthtime;
  1900. result->st_ctime_nsec = result->st_birthtime_nsec;
  1901. return code;
  1902. }
  1903. /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
  1904. In Posix, stat automatically traverses symlinks and returns the stat
  1905. structure for the target. In Windows, the equivalent GetFileAttributes by
  1906. default does not traverse symlinks and instead returns attributes for
  1907. the symlink.
  1908. Instead, we will open the file (which *does* traverse symlinks by default)
  1909. and GetFileInformationByHandle(). */
  1910. static int
  1911. win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
  1912. {
  1913. return win32_xstat(path, result, FALSE);
  1914. }
  1915. static int
  1916. win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
  1917. {
  1918. return win32_xstat(path, result, TRUE);
  1919. }
  1920. #endif /* MS_WINDOWS */
  1921. PyDoc_STRVAR(stat_result__doc__,
  1922. "stat_result: Result from stat, fstat, or lstat.\n\n\
  1923. This object may be accessed either as a tuple of\n\
  1924. (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
  1925. or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
  1926. \n\
  1927. Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
  1928. or st_flags, they are available as attributes only.\n\
  1929. \n\
  1930. See os.stat for more information.");
  1931. static PyStructSequence_Field stat_result_fields[] = {
  1932. {"st_mode", "protection bits"},
  1933. {"st_ino", "inode"},
  1934. {"st_dev", "device"},
  1935. {"st_nlink", "number of hard links"},
  1936. {"st_uid", "user ID of owner"},
  1937. {"st_gid", "group ID of owner"},
  1938. {"st_size", "total size, in bytes"},
  1939. /* The NULL is replaced with PyStructSequence_UnnamedField later. */
  1940. {NULL, "integer time of last access"},
  1941. {NULL, "integer time of last modification"},
  1942. {NULL, "integer time of last change"},
  1943. {"st_atime", "time of last access"},
  1944. {"st_mtime", "time of last modification"},
  1945. {"st_ctime", "time of last change"},
  1946. {"st_atime_ns", "time of last access in nanoseconds"},
  1947. {"st_mtime_ns", "time of last modification in nanoseconds"},
  1948. {"st_ctime_ns", "time of last change in nanoseconds"},
  1949. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  1950. {"st_blksize", "blocksize for filesystem I/O"},
  1951. #endif
  1952. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  1953. {"st_blocks", "number of blocks allocated"},
  1954. #endif
  1955. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  1956. {"st_rdev", "device type (if inode device)"},
  1957. #endif
  1958. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  1959. {"st_flags", "user defined flags for file"},
  1960. #endif
  1961. #ifdef HAVE_STRUCT_STAT_ST_GEN
  1962. {"st_gen", "generation number"},
  1963. #endif
  1964. #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) || defined(MS_WINDOWS)
  1965. {"st_birthtime", "time of creation"},
  1966. #endif
  1967. #ifdef MS_WINDOWS
  1968. {"st_birthtime_ns", "time of creation in nanoseconds"},
  1969. #endif
  1970. #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
  1971. {"st_file_attributes", "Windows file attribute bits"},
  1972. #endif
  1973. #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
  1974. {"st_fstype", "Type of filesystem"},
  1975. #endif
  1976. #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
  1977. {"st_reparse_tag", "Windows reparse tag"},
  1978. #endif
  1979. {0}
  1980. };
  1981. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  1982. #define ST_BLKSIZE_IDX 16
  1983. #else
  1984. #define ST_BLKSIZE_IDX 15
  1985. #endif
  1986. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  1987. #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
  1988. #else
  1989. #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
  1990. #endif
  1991. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  1992. #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
  1993. #else
  1994. #define ST_RDEV_IDX ST_BLOCKS_IDX
  1995. #endif
  1996. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  1997. #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
  1998. #else
  1999. #define ST_FLAGS_IDX ST_RDEV_IDX
  2000. #endif
  2001. #ifdef HAVE_STRUCT_STAT_ST_GEN
  2002. #define ST_GEN_IDX (ST_FLAGS_IDX+1)
  2003. #else
  2004. #define ST_GEN_IDX ST_FLAGS_IDX
  2005. #endif
  2006. #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) || defined(MS_WINDOWS)
  2007. #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
  2008. #else
  2009. #define ST_BIRTHTIME_IDX ST_GEN_IDX
  2010. #endif
  2011. #ifdef MS_WINDOWS
  2012. #define ST_BIRTHTIME_NS_IDX (ST_BIRTHTIME_IDX+1)
  2013. #else
  2014. #define ST_BIRTHTIME_NS_IDX ST_BIRTHTIME_IDX
  2015. #endif
  2016. #if defined(HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES) || defined(MS_WINDOWS)
  2017. #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_NS_IDX+1)
  2018. #else
  2019. #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_NS_IDX
  2020. #endif
  2021. #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
  2022. #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
  2023. #else
  2024. #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
  2025. #endif
  2026. #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
  2027. #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
  2028. #else
  2029. #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
  2030. #endif
  2031. static PyStructSequence_Desc stat_result_desc = {
  2032. "stat_result", /* name */
  2033. stat_result__doc__, /* doc */
  2034. stat_result_fields,
  2035. 10
  2036. };
  2037. PyDoc_STRVAR(statvfs_result__doc__,
  2038. "statvfs_result: Result from statvfs or fstatvfs.\n\n\
  2039. This object may be accessed either as a tuple of\n\
  2040. (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
  2041. or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
  2042. \n\
  2043. See os.statvfs for more information.");
  2044. static PyStructSequence_Field statvfs_result_fields[] = {
  2045. {"f_bsize", },
  2046. {"f_frsize", },
  2047. {"f_blocks", },
  2048. {"f_bfree", },
  2049. {"f_bavail", },
  2050. {"f_files", },
  2051. {"f_ffree", },
  2052. {"f_favail", },
  2053. {"f_flag", },
  2054. {"f_namemax",},
  2055. {"f_fsid", },
  2056. {0}
  2057. };
  2058. static PyStructSequence_Desc statvfs_result_desc = {
  2059. "statvfs_result", /* name */
  2060. statvfs_result__doc__, /* doc */
  2061. statvfs_result_fields,
  2062. 10
  2063. };
  2064. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  2065. PyDoc_STRVAR(waitid_result__doc__,
  2066. "waitid_result: Result from waitid.\n\n\
  2067. This object may be accessed either as a tuple of\n\
  2068. (si_pid, si_uid, si_signo, si_status, si_code),\n\
  2069. or via the attributes si_pid, si_uid, and so on.\n\
  2070. \n\
  2071. See os.waitid for more information.");
  2072. static PyStructSequence_Field waitid_result_fields[] = {
  2073. {"si_pid", },
  2074. {"si_uid", },
  2075. {"si_signo", },
  2076. {"si_status", },
  2077. {"si_code", },
  2078. {0}
  2079. };
  2080. static PyStructSequence_Desc waitid_result_desc = {
  2081. "waitid_result", /* name */
  2082. waitid_result__doc__, /* doc */
  2083. waitid_result_fields,
  2084. 5
  2085. };
  2086. #endif
  2087. static PyObject *
  2088. statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  2089. {
  2090. PyStructSequence *result;
  2091. int i;
  2092. // ht_module doesn't get set in PyStructSequence_NewType(),
  2093. // so we can't use PyType_GetModule().
  2094. PyObject *mod = PyImport_GetModule(MODNAME_OBJ);
  2095. if (mod == NULL) {
  2096. return NULL;
  2097. }
  2098. _posixstate *state = get_posix_state(mod);
  2099. Py_DECREF(mod);
  2100. if (state == NULL) {
  2101. return NULL;
  2102. }
  2103. #define structseq_new state->statresult_new_orig
  2104. result = (PyStructSequence*)structseq_new(type, args, kwds);
  2105. if (!result)
  2106. return NULL;
  2107. /* If we have been initialized from a tuple,
  2108. st_?time might be set to None. Initialize it
  2109. from the int slots. */
  2110. for (i = 7; i <= 9; i++) {
  2111. if (result->ob_item[i+3] == Py_None) {
  2112. Py_DECREF(Py_None);
  2113. result->ob_item[i+3] = Py_NewRef(result->ob_item[i]);
  2114. }
  2115. }
  2116. return (PyObject*)result;
  2117. }
  2118. static int
  2119. _posix_clear(PyObject *module)
  2120. {
  2121. _posixstate *state = get_posix_state(module);
  2122. Py_CLEAR(state->billion);
  2123. Py_CLEAR(state->DirEntryType);
  2124. Py_CLEAR(state->ScandirIteratorType);
  2125. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  2126. Py_CLEAR(state->SchedParamType);
  2127. #endif
  2128. Py_CLEAR(state->StatResultType);
  2129. Py_CLEAR(state->StatVFSResultType);
  2130. Py_CLEAR(state->TerminalSizeType);
  2131. Py_CLEAR(state->TimesResultType);
  2132. Py_CLEAR(state->UnameResultType);
  2133. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  2134. Py_CLEAR(state->WaitidResultType);
  2135. #endif
  2136. #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
  2137. Py_CLEAR(state->struct_rusage);
  2138. #endif
  2139. Py_CLEAR(state->st_mode);
  2140. return 0;
  2141. }
  2142. static int
  2143. _posix_traverse(PyObject *module, visitproc visit, void *arg)
  2144. {
  2145. _posixstate *state = get_posix_state(module);
  2146. Py_VISIT(state->billion);
  2147. Py_VISIT(state->DirEntryType);
  2148. Py_VISIT(state->ScandirIteratorType);
  2149. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  2150. Py_VISIT(state->SchedParamType);
  2151. #endif
  2152. Py_VISIT(state->StatResultType);
  2153. Py_VISIT(state->StatVFSResultType);
  2154. Py_VISIT(state->TerminalSizeType);
  2155. Py_VISIT(state->TimesResultType);
  2156. Py_VISIT(state->UnameResultType);
  2157. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  2158. Py_VISIT(state->WaitidResultType);
  2159. #endif
  2160. #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
  2161. Py_VISIT(state->struct_rusage);
  2162. #endif
  2163. Py_VISIT(state->st_mode);
  2164. return 0;
  2165. }
  2166. static void
  2167. _posix_free(void *module)
  2168. {
  2169. _posix_clear((PyObject *)module);
  2170. }
  2171. static int
  2172. fill_time(PyObject *module, PyObject *v, int s_index, int f_index, int ns_index, time_t sec, unsigned long nsec)
  2173. {
  2174. assert(!PyErr_Occurred());
  2175. int res = -1;
  2176. PyObject *s_in_ns = NULL;
  2177. PyObject *ns_total = NULL;
  2178. PyObject *float_s = NULL;
  2179. PyObject *s = _PyLong_FromTime_t(sec);
  2180. PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
  2181. if (!(s && ns_fractional)) {
  2182. goto exit;
  2183. }
  2184. s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
  2185. if (!s_in_ns) {
  2186. goto exit;
  2187. }
  2188. ns_total = PyNumber_Add(s_in_ns, ns_fractional);
  2189. if (!ns_total)
  2190. goto exit;
  2191. float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
  2192. if (!float_s) {
  2193. goto exit;
  2194. }
  2195. if (s_index >= 0) {
  2196. PyStructSequence_SET_ITEM(v, s_index, s);
  2197. s = NULL;
  2198. }
  2199. if (f_index >= 0) {
  2200. PyStructSequence_SET_ITEM(v, f_index, float_s);
  2201. float_s = NULL;
  2202. }
  2203. if (ns_index >= 0) {
  2204. PyStructSequence_SET_ITEM(v, ns_index, ns_total);
  2205. ns_total = NULL;
  2206. }
  2207. assert(!PyErr_Occurred());
  2208. res = 0;
  2209. exit:
  2210. Py_XDECREF(s);
  2211. Py_XDECREF(ns_fractional);
  2212. Py_XDECREF(s_in_ns);
  2213. Py_XDECREF(ns_total);
  2214. Py_XDECREF(float_s);
  2215. return res;
  2216. }
  2217. #ifdef MS_WINDOWS
  2218. static PyObject*
  2219. _pystat_l128_from_l64_l64(uint64_t low, uint64_t high)
  2220. {
  2221. PyObject *o_low = PyLong_FromUnsignedLongLong(low);
  2222. if (!o_low || !high) {
  2223. return o_low;
  2224. }
  2225. PyObject *o_high = PyLong_FromUnsignedLongLong(high);
  2226. PyObject *l64 = o_high ? PyLong_FromLong(64) : NULL;
  2227. if (!l64) {
  2228. Py_XDECREF(o_high);
  2229. Py_DECREF(o_low);
  2230. return NULL;
  2231. }
  2232. Py_SETREF(o_high, PyNumber_Lshift(o_high, l64));
  2233. Py_DECREF(l64);
  2234. if (!o_high) {
  2235. Py_DECREF(o_low);
  2236. return NULL;
  2237. }
  2238. Py_SETREF(o_low, PyNumber_Add(o_low, o_high));
  2239. Py_DECREF(o_high);
  2240. return o_low;
  2241. }
  2242. #endif
  2243. /* pack a system stat C structure into the Python stat tuple
  2244. (used by posix_stat() and posix_fstat()) */
  2245. static PyObject*
  2246. _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
  2247. {
  2248. assert(!PyErr_Occurred());
  2249. PyObject *StatResultType = get_posix_state(module)->StatResultType;
  2250. PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
  2251. if (v == NULL) {
  2252. return NULL;
  2253. }
  2254. #define SET_ITEM(pos, expr) \
  2255. do { \
  2256. PyObject *obj = (expr); \
  2257. if (obj == NULL) { \
  2258. goto error; \
  2259. } \
  2260. PyStructSequence_SET_ITEM(v, (pos), obj); \
  2261. } while (0)
  2262. SET_ITEM(0, PyLong_FromLong((long)st->st_mode));
  2263. #ifdef MS_WINDOWS
  2264. SET_ITEM(1, _pystat_l128_from_l64_l64(st->st_ino, st->st_ino_high));
  2265. SET_ITEM(2, PyLong_FromUnsignedLongLong(st->st_dev));
  2266. #else
  2267. static_assert(sizeof(unsigned long long) >= sizeof(st->st_ino),
  2268. "stat.st_ino is larger than unsigned long long");
  2269. SET_ITEM(1, PyLong_FromUnsignedLongLong(st->st_ino));
  2270. SET_ITEM(2, _PyLong_FromDev(st->st_dev));
  2271. #endif
  2272. SET_ITEM(3, PyLong_FromLong((long)st->st_nlink));
  2273. #if defined(MS_WINDOWS)
  2274. SET_ITEM(4, PyLong_FromLong(0));
  2275. SET_ITEM(5, PyLong_FromLong(0));
  2276. #else
  2277. SET_ITEM(4, _PyLong_FromUid(st->st_uid));
  2278. SET_ITEM(5, _PyLong_FromGid(st->st_gid));
  2279. #endif
  2280. static_assert(sizeof(long long) >= sizeof(st->st_size),
  2281. "stat.st_size is larger than long long");
  2282. SET_ITEM(6, PyLong_FromLongLong(st->st_size));
  2283. // Set st_atime, st_mtime and st_ctime
  2284. unsigned long ansec, mnsec, cnsec;
  2285. #if defined(HAVE_STAT_TV_NSEC)
  2286. ansec = st->st_atim.tv_nsec;
  2287. mnsec = st->st_mtim.tv_nsec;
  2288. cnsec = st->st_ctim.tv_nsec;
  2289. #elif defined(HAVE_STAT_TV_NSEC2)
  2290. ansec = st->st_atimespec.tv_nsec;
  2291. mnsec = st->st_mtimespec.tv_nsec;
  2292. cnsec = st->st_ctimespec.tv_nsec;
  2293. #elif defined(HAVE_STAT_NSEC)
  2294. ansec = st->st_atime_nsec;
  2295. mnsec = st->st_mtime_nsec;
  2296. cnsec = st->st_ctime_nsec;
  2297. #else
  2298. ansec = mnsec = cnsec = 0;
  2299. #endif
  2300. if (fill_time(module, v, 7, 10, 13, st->st_atime, ansec) < 0) {
  2301. goto error;
  2302. }
  2303. if (fill_time(module, v, 8, 11, 14, st->st_mtime, mnsec) < 0) {
  2304. goto error;
  2305. }
  2306. if (fill_time(module, v, 9, 12, 15, st->st_ctime, cnsec) < 0) {
  2307. goto error;
  2308. }
  2309. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  2310. SET_ITEM(ST_BLKSIZE_IDX, PyLong_FromLong((long)st->st_blksize));
  2311. #endif
  2312. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  2313. SET_ITEM(ST_BLOCKS_IDX, PyLong_FromLong((long)st->st_blocks));
  2314. #endif
  2315. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  2316. SET_ITEM(ST_RDEV_IDX, PyLong_FromLong((long)st->st_rdev));
  2317. #endif
  2318. #ifdef HAVE_STRUCT_STAT_ST_GEN
  2319. SET_ITEM(ST_GEN_IDX, PyLong_FromLong((long)st->st_gen));
  2320. #endif
  2321. #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
  2322. {
  2323. unsigned long bsec, bnsec;
  2324. bsec = (long)st->st_birthtime;
  2325. #ifdef HAVE_STAT_TV_NSEC2
  2326. bnsec = st->st_birthtimespec.tv_nsec;
  2327. #else
  2328. bnsec = 0;
  2329. #endif
  2330. SET_ITEM(ST_BIRTHTIME_IDX, PyFloat_FromDouble(bsec + bnsec * 1e-9));
  2331. }
  2332. #elif defined(MS_WINDOWS)
  2333. if (fill_time(module, v, -1, ST_BIRTHTIME_IDX, ST_BIRTHTIME_NS_IDX,
  2334. st->st_birthtime, st->st_birthtime_nsec) < 0) {
  2335. goto error;
  2336. }
  2337. #endif
  2338. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  2339. SET_ITEM(ST_FLAGS_IDX, PyLong_FromLong((long)st->st_flags));
  2340. #endif
  2341. #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
  2342. SET_ITEM(ST_FILE_ATTRIBUTES_IDX,
  2343. PyLong_FromUnsignedLong(st->st_file_attributes));
  2344. #endif
  2345. #ifdef HAVE_STRUCT_STAT_ST_FSTYPE
  2346. SET_ITEM(ST_FSTYPE_IDX, PyUnicode_FromString(st->st_fstype));
  2347. #endif
  2348. #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
  2349. SET_ITEM(ST_REPARSE_TAG_IDX, PyLong_FromUnsignedLong(st->st_reparse_tag));
  2350. #endif
  2351. assert(!PyErr_Occurred());
  2352. return v;
  2353. error:
  2354. Py_DECREF(v);
  2355. return NULL;
  2356. #undef SET_ITEM
  2357. }
  2358. /* POSIX methods */
  2359. static PyObject *
  2360. posix_do_stat(PyObject *module, const char *function_name, path_t *path,
  2361. int dir_fd, int follow_symlinks)
  2362. {
  2363. STRUCT_STAT st;
  2364. int result;
  2365. #ifdef HAVE_FSTATAT
  2366. int fstatat_unavailable = 0;
  2367. #endif
  2368. #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
  2369. if (follow_symlinks_specified(function_name, follow_symlinks))
  2370. return NULL;
  2371. #endif
  2372. if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
  2373. dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
  2374. fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
  2375. return NULL;
  2376. Py_BEGIN_ALLOW_THREADS
  2377. if (path->fd != -1)
  2378. result = FSTAT(path->fd, &st);
  2379. #ifdef MS_WINDOWS
  2380. else if (follow_symlinks)
  2381. result = win32_stat(path->wide, &st);
  2382. else
  2383. result = win32_lstat(path->wide, &st);
  2384. #else
  2385. else
  2386. #if defined(HAVE_LSTAT)
  2387. if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
  2388. result = LSTAT(path->narrow, &st);
  2389. else
  2390. #endif /* HAVE_LSTAT */
  2391. #ifdef HAVE_FSTATAT
  2392. if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
  2393. if (HAVE_FSTATAT_RUNTIME) {
  2394. result = fstatat(dir_fd, path->narrow, &st,
  2395. follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
  2396. } else {
  2397. fstatat_unavailable = 1;
  2398. }
  2399. } else
  2400. #endif /* HAVE_FSTATAT */
  2401. result = STAT(path->narrow, &st);
  2402. #endif /* MS_WINDOWS */
  2403. Py_END_ALLOW_THREADS
  2404. #ifdef HAVE_FSTATAT
  2405. if (fstatat_unavailable) {
  2406. argument_unavailable_error("stat", "dir_fd");
  2407. return NULL;
  2408. }
  2409. #endif
  2410. if (result != 0) {
  2411. return path_error(path);
  2412. }
  2413. return _pystat_fromstructstat(module, &st);
  2414. }
  2415. /*[python input]
  2416. for s in """
  2417. FACCESSAT
  2418. FCHMODAT
  2419. FCHOWNAT
  2420. FSTATAT
  2421. LINKAT
  2422. MKDIRAT
  2423. MKFIFOAT
  2424. MKNODAT
  2425. OPENAT
  2426. READLINKAT
  2427. SYMLINKAT
  2428. UNLINKAT
  2429. """.strip().split():
  2430. s = s.strip()
  2431. print("""
  2432. #ifdef HAVE_{s}
  2433. #define {s}_DIR_FD_CONVERTER dir_fd_converter
  2434. #else
  2435. #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
  2436. #endif
  2437. """.rstrip().format(s=s))
  2438. for s in """
  2439. FCHDIR
  2440. FCHMOD
  2441. FCHOWN
  2442. FDOPENDIR
  2443. FEXECVE
  2444. FPATHCONF
  2445. FSTATVFS
  2446. FTRUNCATE
  2447. """.strip().split():
  2448. s = s.strip()
  2449. print("""
  2450. #ifdef HAVE_{s}
  2451. #define PATH_HAVE_{s} 1
  2452. #else
  2453. #define PATH_HAVE_{s} 0
  2454. #endif
  2455. """.rstrip().format(s=s))
  2456. [python start generated code]*/
  2457. #ifdef HAVE_FACCESSAT
  2458. #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
  2459. #else
  2460. #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
  2461. #endif
  2462. #ifdef HAVE_FCHMODAT
  2463. #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
  2464. #else
  2465. #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
  2466. #endif
  2467. #ifdef HAVE_FCHOWNAT
  2468. #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
  2469. #else
  2470. #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
  2471. #endif
  2472. #ifdef HAVE_FSTATAT
  2473. #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
  2474. #else
  2475. #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
  2476. #endif
  2477. #ifdef HAVE_LINKAT
  2478. #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
  2479. #else
  2480. #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
  2481. #endif
  2482. #ifdef HAVE_MKDIRAT
  2483. #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
  2484. #else
  2485. #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
  2486. #endif
  2487. #ifdef HAVE_MKFIFOAT
  2488. #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
  2489. #else
  2490. #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
  2491. #endif
  2492. #ifdef HAVE_MKNODAT
  2493. #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
  2494. #else
  2495. #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
  2496. #endif
  2497. #ifdef HAVE_OPENAT
  2498. #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
  2499. #else
  2500. #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
  2501. #endif
  2502. #ifdef HAVE_READLINKAT
  2503. #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
  2504. #else
  2505. #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
  2506. #endif
  2507. #ifdef HAVE_SYMLINKAT
  2508. #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
  2509. #else
  2510. #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
  2511. #endif
  2512. #ifdef HAVE_UNLINKAT
  2513. #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
  2514. #else
  2515. #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
  2516. #endif
  2517. #ifdef HAVE_FCHDIR
  2518. #define PATH_HAVE_FCHDIR 1
  2519. #else
  2520. #define PATH_HAVE_FCHDIR 0
  2521. #endif
  2522. #ifdef HAVE_FCHMOD
  2523. #define PATH_HAVE_FCHMOD 1
  2524. #else
  2525. #define PATH_HAVE_FCHMOD 0
  2526. #endif
  2527. #ifdef HAVE_FCHOWN
  2528. #define PATH_HAVE_FCHOWN 1
  2529. #else
  2530. #define PATH_HAVE_FCHOWN 0
  2531. #endif
  2532. #ifdef HAVE_FDOPENDIR
  2533. #define PATH_HAVE_FDOPENDIR 1
  2534. #else
  2535. #define PATH_HAVE_FDOPENDIR 0
  2536. #endif
  2537. #ifdef HAVE_FEXECVE
  2538. #define PATH_HAVE_FEXECVE 1
  2539. #else
  2540. #define PATH_HAVE_FEXECVE 0
  2541. #endif
  2542. #ifdef HAVE_FPATHCONF
  2543. #define PATH_HAVE_FPATHCONF 1
  2544. #else
  2545. #define PATH_HAVE_FPATHCONF 0
  2546. #endif
  2547. #ifdef HAVE_FSTATVFS
  2548. #define PATH_HAVE_FSTATVFS 1
  2549. #else
  2550. #define PATH_HAVE_FSTATVFS 0
  2551. #endif
  2552. #ifdef HAVE_FTRUNCATE
  2553. #define PATH_HAVE_FTRUNCATE 1
  2554. #else
  2555. #define PATH_HAVE_FTRUNCATE 0
  2556. #endif
  2557. /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
  2558. #ifdef MS_WINDOWS
  2559. #undef PATH_HAVE_FTRUNCATE
  2560. #define PATH_HAVE_FTRUNCATE 1
  2561. #endif
  2562. /*[python input]
  2563. class path_t_converter(CConverter):
  2564. type = "path_t"
  2565. impl_by_reference = True
  2566. parse_by_reference = True
  2567. converter = 'path_converter'
  2568. def converter_init(self, *, allow_fd=False, make_wide=None,
  2569. nonstrict=False, nullable=False,
  2570. suppress_value_error=False):
  2571. # right now path_t doesn't support default values.
  2572. # to support a default value, you'll need to override initialize().
  2573. if self.default not in (unspecified, None):
  2574. fail("Can't specify a default to the path_t converter!")
  2575. if self.c_default not in (None, 'Py_None'):
  2576. raise RuntimeError("Can't specify a c_default to the path_t converter!")
  2577. self.nullable = nullable
  2578. self.nonstrict = nonstrict
  2579. self.make_wide = make_wide
  2580. self.suppress_value_error = suppress_value_error
  2581. self.allow_fd = allow_fd
  2582. def pre_render(self):
  2583. def strify(value):
  2584. if isinstance(value, str):
  2585. return value
  2586. return str(int(bool(value)))
  2587. # add self.py_name here when merging with posixmodule conversion
  2588. if self.make_wide is None:
  2589. self.c_default = 'PATH_T_INITIALIZE_P("{}", "{}", {}, {}, {}, {})'.format(
  2590. self.function.name,
  2591. self.name,
  2592. strify(self.nullable),
  2593. strify(self.nonstrict),
  2594. strify(self.suppress_value_error),
  2595. strify(self.allow_fd),
  2596. )
  2597. else:
  2598. self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {}, {}, {}, {})'.format(
  2599. self.function.name,
  2600. self.name,
  2601. strify(self.nullable),
  2602. strify(self.nonstrict),
  2603. strify(self.make_wide),
  2604. strify(self.suppress_value_error),
  2605. strify(self.allow_fd),
  2606. )
  2607. def cleanup(self):
  2608. return "path_cleanup(&" + self.name + ");\n"
  2609. class dir_fd_converter(CConverter):
  2610. type = 'int'
  2611. def converter_init(self, requires=None):
  2612. if self.default in (unspecified, None):
  2613. self.c_default = 'DEFAULT_DIR_FD'
  2614. if isinstance(requires, str):
  2615. self.converter = requires.upper() + '_DIR_FD_CONVERTER'
  2616. else:
  2617. self.converter = 'dir_fd_converter'
  2618. class uid_t_converter(CConverter):
  2619. type = "uid_t"
  2620. converter = '_Py_Uid_Converter'
  2621. class gid_t_converter(CConverter):
  2622. type = "gid_t"
  2623. converter = '_Py_Gid_Converter'
  2624. class dev_t_converter(CConverter):
  2625. type = 'dev_t'
  2626. converter = '_Py_Dev_Converter'
  2627. class dev_t_return_converter(unsigned_long_return_converter):
  2628. type = 'dev_t'
  2629. conversion_fn = '_PyLong_FromDev'
  2630. unsigned_cast = '(dev_t)'
  2631. class FSConverter_converter(CConverter):
  2632. type = 'PyObject *'
  2633. converter = 'PyUnicode_FSConverter'
  2634. def converter_init(self):
  2635. if self.default is not unspecified:
  2636. fail("FSConverter_converter does not support default values")
  2637. self.c_default = 'NULL'
  2638. def cleanup(self):
  2639. return "Py_XDECREF(" + self.name + ");\n"
  2640. class pid_t_converter(CConverter):
  2641. type = 'pid_t'
  2642. format_unit = '" _Py_PARSE_PID "'
  2643. class idtype_t_converter(int_converter):
  2644. type = 'idtype_t'
  2645. class id_t_converter(CConverter):
  2646. type = 'id_t'
  2647. format_unit = '" _Py_PARSE_PID "'
  2648. class intptr_t_converter(CConverter):
  2649. type = 'intptr_t'
  2650. format_unit = '" _Py_PARSE_INTPTR "'
  2651. class Py_off_t_converter(CConverter):
  2652. type = 'Py_off_t'
  2653. converter = 'Py_off_t_converter'
  2654. class Py_off_t_return_converter(long_return_converter):
  2655. type = 'Py_off_t'
  2656. conversion_fn = 'PyLong_FromPy_off_t'
  2657. class path_confname_converter(CConverter):
  2658. type="int"
  2659. converter="conv_path_confname"
  2660. class confstr_confname_converter(path_confname_converter):
  2661. converter='conv_confstr_confname'
  2662. class sysconf_confname_converter(path_confname_converter):
  2663. converter="conv_sysconf_confname"
  2664. [python start generated code]*/
  2665. /*[python end generated code: output=da39a3ee5e6b4b0d input=577cb476e5d64960]*/
  2666. /*[clinic input]
  2667. os.stat
  2668. path : path_t(allow_fd=True)
  2669. Path to be examined; can be string, bytes, a path-like object or
  2670. open-file-descriptor int.
  2671. *
  2672. dir_fd : dir_fd(requires='fstatat') = None
  2673. If not None, it should be a file descriptor open to a directory,
  2674. and path should be a relative string; path will then be relative to
  2675. that directory.
  2676. follow_symlinks: bool = True
  2677. If False, and the last element of the path is a symbolic link,
  2678. stat will examine the symbolic link itself instead of the file
  2679. the link points to.
  2680. Perform a stat system call on the given path.
  2681. dir_fd and follow_symlinks may not be implemented
  2682. on your platform. If they are unavailable, using them will raise a
  2683. NotImplementedError.
  2684. It's an error to use dir_fd or follow_symlinks when specifying path as
  2685. an open file descriptor.
  2686. [clinic start generated code]*/
  2687. static PyObject *
  2688. os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
  2689. /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
  2690. {
  2691. return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks);
  2692. }
  2693. /*[clinic input]
  2694. os.lstat
  2695. path : path_t
  2696. *
  2697. dir_fd : dir_fd(requires='fstatat') = None
  2698. Perform a stat system call on the given path, without following symbolic links.
  2699. Like stat(), but do not follow symbolic links.
  2700. Equivalent to stat(path, follow_symlinks=False).
  2701. [clinic start generated code]*/
  2702. static PyObject *
  2703. os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
  2704. /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
  2705. {
  2706. int follow_symlinks = 0;
  2707. return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks);
  2708. }
  2709. /*[clinic input]
  2710. os.access -> bool
  2711. path: path_t
  2712. Path to be tested; can be string, bytes, or a path-like object.
  2713. mode: int
  2714. Operating-system mode bitfield. Can be F_OK to test existence,
  2715. or the inclusive-OR of R_OK, W_OK, and X_OK.
  2716. *
  2717. dir_fd : dir_fd(requires='faccessat') = None
  2718. If not None, it should be a file descriptor open to a directory,
  2719. and path should be relative; path will then be relative to that
  2720. directory.
  2721. effective_ids: bool = False
  2722. If True, access will use the effective uid/gid instead of
  2723. the real uid/gid.
  2724. follow_symlinks: bool = True
  2725. If False, and the last element of the path is a symbolic link,
  2726. access will examine the symbolic link itself instead of the file
  2727. the link points to.
  2728. Use the real uid/gid to test for access to a path.
  2729. {parameters}
  2730. dir_fd, effective_ids, and follow_symlinks may not be implemented
  2731. on your platform. If they are unavailable, using them will raise a
  2732. NotImplementedError.
  2733. Note that most operations will use the effective uid/gid, therefore this
  2734. routine can be used in a suid/sgid environment to test if the invoking user
  2735. has the specified access to the path.
  2736. [clinic start generated code]*/
  2737. static int
  2738. os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
  2739. int effective_ids, int follow_symlinks)
  2740. /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
  2741. {
  2742. int return_value;
  2743. #ifdef MS_WINDOWS
  2744. DWORD attr;
  2745. #else
  2746. int result;
  2747. #endif
  2748. #ifdef HAVE_FACCESSAT
  2749. int faccessat_unavailable = 0;
  2750. #endif
  2751. #ifndef HAVE_FACCESSAT
  2752. if (follow_symlinks_specified("access", follow_symlinks))
  2753. return -1;
  2754. if (effective_ids) {
  2755. argument_unavailable_error("access", "effective_ids");
  2756. return -1;
  2757. }
  2758. #endif
  2759. #ifdef MS_WINDOWS
  2760. Py_BEGIN_ALLOW_THREADS
  2761. attr = GetFileAttributesW(path->wide);
  2762. Py_END_ALLOW_THREADS
  2763. /*
  2764. * Access is possible if
  2765. * * we didn't get a -1, and
  2766. * * write access wasn't requested,
  2767. * * or the file isn't read-only,
  2768. * * or it's a directory.
  2769. * (Directories cannot be read-only on Windows.)
  2770. */
  2771. return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
  2772. (!(mode & 2) ||
  2773. !(attr & FILE_ATTRIBUTE_READONLY) ||
  2774. (attr & FILE_ATTRIBUTE_DIRECTORY));
  2775. #else
  2776. Py_BEGIN_ALLOW_THREADS
  2777. #ifdef HAVE_FACCESSAT
  2778. if ((dir_fd != DEFAULT_DIR_FD) ||
  2779. effective_ids ||
  2780. !follow_symlinks) {
  2781. if (HAVE_FACCESSAT_RUNTIME) {
  2782. int flags = 0;
  2783. if (!follow_symlinks)
  2784. flags |= AT_SYMLINK_NOFOLLOW;
  2785. if (effective_ids)
  2786. flags |= AT_EACCESS;
  2787. result = faccessat(dir_fd, path->narrow, mode, flags);
  2788. } else {
  2789. faccessat_unavailable = 1;
  2790. }
  2791. }
  2792. else
  2793. #endif
  2794. result = access(path->narrow, mode);
  2795. Py_END_ALLOW_THREADS
  2796. #ifdef HAVE_FACCESSAT
  2797. if (faccessat_unavailable) {
  2798. if (dir_fd != DEFAULT_DIR_FD) {
  2799. argument_unavailable_error("access", "dir_fd");
  2800. return -1;
  2801. }
  2802. if (follow_symlinks_specified("access", follow_symlinks))
  2803. return -1;
  2804. if (effective_ids) {
  2805. argument_unavailable_error("access", "effective_ids");
  2806. return -1;
  2807. }
  2808. /* should be unreachable */
  2809. return -1;
  2810. }
  2811. #endif
  2812. return_value = !result;
  2813. #endif
  2814. return return_value;
  2815. }
  2816. #ifndef F_OK
  2817. #define F_OK 0
  2818. #endif
  2819. #ifndef R_OK
  2820. #define R_OK 4
  2821. #endif
  2822. #ifndef W_OK
  2823. #define W_OK 2
  2824. #endif
  2825. #ifndef X_OK
  2826. #define X_OK 1
  2827. #endif
  2828. #ifdef HAVE_TTYNAME
  2829. /*[clinic input]
  2830. os.ttyname
  2831. fd: int
  2832. Integer file descriptor handle.
  2833. /
  2834. Return the name of the terminal device connected to 'fd'.
  2835. [clinic start generated code]*/
  2836. static PyObject *
  2837. os_ttyname_impl(PyObject *module, int fd)
  2838. /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
  2839. {
  2840. long size = sysconf(_SC_TTY_NAME_MAX);
  2841. if (size == -1) {
  2842. return posix_error();
  2843. }
  2844. char *buffer = (char *)PyMem_RawMalloc(size);
  2845. if (buffer == NULL) {
  2846. return PyErr_NoMemory();
  2847. }
  2848. int ret = ttyname_r(fd, buffer, size);
  2849. if (ret != 0) {
  2850. PyMem_RawFree(buffer);
  2851. errno = ret;
  2852. return posix_error();
  2853. }
  2854. PyObject *res = PyUnicode_DecodeFSDefault(buffer);
  2855. PyMem_RawFree(buffer);
  2856. return res;
  2857. }
  2858. #endif
  2859. #ifdef HAVE_CTERMID
  2860. /*[clinic input]
  2861. os.ctermid
  2862. Return the name of the controlling terminal for this process.
  2863. [clinic start generated code]*/
  2864. static PyObject *
  2865. os_ctermid_impl(PyObject *module)
  2866. /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
  2867. {
  2868. char *ret;
  2869. char buffer[L_ctermid];
  2870. #ifdef USE_CTERMID_R
  2871. ret = ctermid_r(buffer);
  2872. #else
  2873. ret = ctermid(buffer);
  2874. #endif
  2875. if (ret == NULL)
  2876. return posix_error();
  2877. return PyUnicode_DecodeFSDefault(buffer);
  2878. }
  2879. #endif /* HAVE_CTERMID */
  2880. /*[clinic input]
  2881. os.chdir
  2882. path: path_t(allow_fd='PATH_HAVE_FCHDIR')
  2883. Change the current working directory to the specified path.
  2884. path may always be specified as a string.
  2885. On some platforms, path may also be specified as an open file descriptor.
  2886. If this functionality is unavailable, using it raises an exception.
  2887. [clinic start generated code]*/
  2888. static PyObject *
  2889. os_chdir_impl(PyObject *module, path_t *path)
  2890. /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
  2891. {
  2892. int result;
  2893. if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
  2894. return NULL;
  2895. }
  2896. Py_BEGIN_ALLOW_THREADS
  2897. #ifdef MS_WINDOWS
  2898. /* on unix, success = 0, on windows, success = !0 */
  2899. result = !win32_wchdir(path->wide);
  2900. #else
  2901. #ifdef HAVE_FCHDIR
  2902. if (path->fd != -1)
  2903. result = fchdir(path->fd);
  2904. else
  2905. #endif
  2906. result = chdir(path->narrow);
  2907. #endif
  2908. Py_END_ALLOW_THREADS
  2909. if (result) {
  2910. return path_error(path);
  2911. }
  2912. Py_RETURN_NONE;
  2913. }
  2914. #ifdef HAVE_FCHDIR
  2915. /*[clinic input]
  2916. os.fchdir
  2917. fd: fildes
  2918. Change to the directory of the given file descriptor.
  2919. fd must be opened on a directory, not a file.
  2920. Equivalent to os.chdir(fd).
  2921. [clinic start generated code]*/
  2922. static PyObject *
  2923. os_fchdir_impl(PyObject *module, int fd)
  2924. /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
  2925. {
  2926. if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
  2927. return NULL;
  2928. }
  2929. return posix_fildes_fd(fd, fchdir);
  2930. }
  2931. #endif /* HAVE_FCHDIR */
  2932. /*[clinic input]
  2933. os.chmod
  2934. path: path_t(allow_fd='PATH_HAVE_FCHMOD')
  2935. Path to be modified. May always be specified as a str, bytes, or a path-like object.
  2936. On some platforms, path may also be specified as an open file descriptor.
  2937. If this functionality is unavailable, using it raises an exception.
  2938. mode: int
  2939. Operating-system mode bitfield.
  2940. Be careful when using number literals for *mode*. The conventional UNIX notation for
  2941. numeric modes uses an octal base, which needs to be indicated with a ``0o`` prefix in
  2942. Python.
  2943. *
  2944. dir_fd : dir_fd(requires='fchmodat') = None
  2945. If not None, it should be a file descriptor open to a directory,
  2946. and path should be relative; path will then be relative to that
  2947. directory.
  2948. follow_symlinks: bool = True
  2949. If False, and the last element of the path is a symbolic link,
  2950. chmod will modify the symbolic link itself instead of the file
  2951. the link points to.
  2952. Change the access permissions of a file.
  2953. It is an error to use dir_fd or follow_symlinks when specifying path as
  2954. an open file descriptor.
  2955. dir_fd and follow_symlinks may not be implemented on your platform.
  2956. If they are unavailable, using them will raise a NotImplementedError.
  2957. [clinic start generated code]*/
  2958. static PyObject *
  2959. os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
  2960. int follow_symlinks)
  2961. /*[clinic end generated code: output=5cf6a94915cc7bff input=674a14bc998de09d]*/
  2962. {
  2963. int result;
  2964. #ifdef MS_WINDOWS
  2965. DWORD attr;
  2966. #endif
  2967. #ifdef HAVE_FCHMODAT
  2968. int fchmodat_nofollow_unsupported = 0;
  2969. int fchmodat_unsupported = 0;
  2970. #endif
  2971. #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
  2972. if (follow_symlinks_specified("chmod", follow_symlinks))
  2973. return NULL;
  2974. #endif
  2975. if (PySys_Audit("os.chmod", "Oii", path->object, mode,
  2976. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  2977. return NULL;
  2978. }
  2979. #ifdef MS_WINDOWS
  2980. Py_BEGIN_ALLOW_THREADS
  2981. attr = GetFileAttributesW(path->wide);
  2982. if (attr == INVALID_FILE_ATTRIBUTES)
  2983. result = 0;
  2984. else {
  2985. if (mode & _S_IWRITE)
  2986. attr &= ~FILE_ATTRIBUTE_READONLY;
  2987. else
  2988. attr |= FILE_ATTRIBUTE_READONLY;
  2989. result = SetFileAttributesW(path->wide, attr);
  2990. }
  2991. Py_END_ALLOW_THREADS
  2992. if (!result) {
  2993. return path_error(path);
  2994. }
  2995. #else /* MS_WINDOWS */
  2996. Py_BEGIN_ALLOW_THREADS
  2997. #ifdef HAVE_FCHMOD
  2998. if (path->fd != -1)
  2999. result = fchmod(path->fd, mode);
  3000. else
  3001. #endif /* HAVE_CHMOD */
  3002. #ifdef HAVE_LCHMOD
  3003. if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
  3004. result = lchmod(path->narrow, mode);
  3005. else
  3006. #endif /* HAVE_LCHMOD */
  3007. #ifdef HAVE_FCHMODAT
  3008. if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
  3009. if (HAVE_FCHMODAT_RUNTIME) {
  3010. /*
  3011. * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
  3012. * The documentation specifically shows how to use it,
  3013. * and then says it isn't implemented yet.
  3014. * (true on linux with glibc 2.15, and openindiana 3.x)
  3015. *
  3016. * Once it is supported, os.chmod will automatically
  3017. * support dir_fd and follow_symlinks=False. (Hopefully.)
  3018. * Until then, we need to be careful what exception we raise.
  3019. */
  3020. result = fchmodat(dir_fd, path->narrow, mode,
  3021. follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
  3022. /*
  3023. * But wait! We can't throw the exception without allowing threads,
  3024. * and we can't do that in this nested scope. (Macro trickery, sigh.)
  3025. */
  3026. fchmodat_nofollow_unsupported =
  3027. result &&
  3028. ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
  3029. !follow_symlinks;
  3030. } else {
  3031. fchmodat_unsupported = 1;
  3032. fchmodat_nofollow_unsupported = 1;
  3033. result = -1;
  3034. }
  3035. }
  3036. else
  3037. #endif /* HAVE_FHCMODAT */
  3038. {
  3039. #ifdef HAVE_CHMOD
  3040. result = chmod(path->narrow, mode);
  3041. #elif defined(__wasi__)
  3042. // WASI SDK 15.0 does not support chmod.
  3043. // Ignore missing syscall for now.
  3044. result = 0;
  3045. #else
  3046. result = -1;
  3047. errno = ENOSYS;
  3048. #endif
  3049. }
  3050. Py_END_ALLOW_THREADS
  3051. if (result) {
  3052. #ifdef HAVE_FCHMODAT
  3053. if (fchmodat_unsupported) {
  3054. if (dir_fd != DEFAULT_DIR_FD) {
  3055. argument_unavailable_error("chmod", "dir_fd");
  3056. return NULL;
  3057. }
  3058. }
  3059. if (fchmodat_nofollow_unsupported) {
  3060. if (dir_fd != DEFAULT_DIR_FD)
  3061. dir_fd_and_follow_symlinks_invalid("chmod",
  3062. dir_fd, follow_symlinks);
  3063. else
  3064. follow_symlinks_specified("chmod", follow_symlinks);
  3065. return NULL;
  3066. }
  3067. else
  3068. #endif /* HAVE_FCHMODAT */
  3069. return path_error(path);
  3070. }
  3071. #endif /* MS_WINDOWS */
  3072. Py_RETURN_NONE;
  3073. }
  3074. #ifdef HAVE_FCHMOD
  3075. /*[clinic input]
  3076. os.fchmod
  3077. fd: int
  3078. The file descriptor of the file to be modified.
  3079. mode: int
  3080. Operating-system mode bitfield.
  3081. Be careful when using number literals for *mode*. The conventional UNIX notation for
  3082. numeric modes uses an octal base, which needs to be indicated with a ``0o`` prefix in
  3083. Python.
  3084. Change the access permissions of the file given by file descriptor fd.
  3085. Equivalent to os.chmod(fd, mode).
  3086. [clinic start generated code]*/
  3087. static PyObject *
  3088. os_fchmod_impl(PyObject *module, int fd, int mode)
  3089. /*[clinic end generated code: output=afd9bc05b4e426b3 input=b5594618bbbc22df]*/
  3090. {
  3091. int res;
  3092. int async_err = 0;
  3093. if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
  3094. return NULL;
  3095. }
  3096. do {
  3097. Py_BEGIN_ALLOW_THREADS
  3098. res = fchmod(fd, mode);
  3099. Py_END_ALLOW_THREADS
  3100. } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  3101. if (res != 0)
  3102. return (!async_err) ? posix_error() : NULL;
  3103. Py_RETURN_NONE;
  3104. }
  3105. #endif /* HAVE_FCHMOD */
  3106. #ifdef HAVE_LCHMOD
  3107. /*[clinic input]
  3108. os.lchmod
  3109. path: path_t
  3110. mode: int
  3111. Change the access permissions of a file, without following symbolic links.
  3112. If path is a symlink, this affects the link itself rather than the target.
  3113. Equivalent to chmod(path, mode, follow_symlinks=False)."
  3114. [clinic start generated code]*/
  3115. static PyObject *
  3116. os_lchmod_impl(PyObject *module, path_t *path, int mode)
  3117. /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
  3118. {
  3119. int res;
  3120. if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
  3121. return NULL;
  3122. }
  3123. Py_BEGIN_ALLOW_THREADS
  3124. res = lchmod(path->narrow, mode);
  3125. Py_END_ALLOW_THREADS
  3126. if (res < 0) {
  3127. path_error(path);
  3128. return NULL;
  3129. }
  3130. Py_RETURN_NONE;
  3131. }
  3132. #endif /* HAVE_LCHMOD */
  3133. #ifdef HAVE_CHFLAGS
  3134. /*[clinic input]
  3135. os.chflags
  3136. path: path_t
  3137. flags: unsigned_long(bitwise=True)
  3138. follow_symlinks: bool=True
  3139. Set file flags.
  3140. If follow_symlinks is False, and the last element of the path is a symbolic
  3141. link, chflags will change flags on the symbolic link itself instead of the
  3142. file the link points to.
  3143. follow_symlinks may not be implemented on your platform. If it is
  3144. unavailable, using it will raise a NotImplementedError.
  3145. [clinic start generated code]*/
  3146. static PyObject *
  3147. os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
  3148. int follow_symlinks)
  3149. /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
  3150. {
  3151. int result;
  3152. #ifndef HAVE_LCHFLAGS
  3153. if (follow_symlinks_specified("chflags", follow_symlinks))
  3154. return NULL;
  3155. #endif
  3156. if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
  3157. return NULL;
  3158. }
  3159. Py_BEGIN_ALLOW_THREADS
  3160. #ifdef HAVE_LCHFLAGS
  3161. if (!follow_symlinks)
  3162. result = lchflags(path->narrow, flags);
  3163. else
  3164. #endif
  3165. result = chflags(path->narrow, flags);
  3166. Py_END_ALLOW_THREADS
  3167. if (result)
  3168. return path_error(path);
  3169. Py_RETURN_NONE;
  3170. }
  3171. #endif /* HAVE_CHFLAGS */
  3172. #ifdef HAVE_LCHFLAGS
  3173. /*[clinic input]
  3174. os.lchflags
  3175. path: path_t
  3176. flags: unsigned_long(bitwise=True)
  3177. Set file flags.
  3178. This function will not follow symbolic links.
  3179. Equivalent to chflags(path, flags, follow_symlinks=False).
  3180. [clinic start generated code]*/
  3181. static PyObject *
  3182. os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
  3183. /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
  3184. {
  3185. int res;
  3186. if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
  3187. return NULL;
  3188. }
  3189. Py_BEGIN_ALLOW_THREADS
  3190. res = lchflags(path->narrow, flags);
  3191. Py_END_ALLOW_THREADS
  3192. if (res < 0) {
  3193. return path_error(path);
  3194. }
  3195. Py_RETURN_NONE;
  3196. }
  3197. #endif /* HAVE_LCHFLAGS */
  3198. #ifdef HAVE_CHROOT
  3199. /*[clinic input]
  3200. os.chroot
  3201. path: path_t
  3202. Change root directory to path.
  3203. [clinic start generated code]*/
  3204. static PyObject *
  3205. os_chroot_impl(PyObject *module, path_t *path)
  3206. /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
  3207. {
  3208. int res;
  3209. Py_BEGIN_ALLOW_THREADS
  3210. res = chroot(path->narrow);
  3211. Py_END_ALLOW_THREADS
  3212. if (res < 0)
  3213. return path_error(path);
  3214. Py_RETURN_NONE;
  3215. }
  3216. #endif /* HAVE_CHROOT */
  3217. #ifdef HAVE_FSYNC
  3218. /*[clinic input]
  3219. os.fsync
  3220. fd: fildes
  3221. Force write of fd to disk.
  3222. [clinic start generated code]*/
  3223. static PyObject *
  3224. os_fsync_impl(PyObject *module, int fd)
  3225. /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
  3226. {
  3227. return posix_fildes_fd(fd, fsync);
  3228. }
  3229. #endif /* HAVE_FSYNC */
  3230. #ifdef HAVE_SYNC
  3231. /*[clinic input]
  3232. os.sync
  3233. Force write of everything to disk.
  3234. [clinic start generated code]*/
  3235. static PyObject *
  3236. os_sync_impl(PyObject *module)
  3237. /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
  3238. {
  3239. Py_BEGIN_ALLOW_THREADS
  3240. sync();
  3241. Py_END_ALLOW_THREADS
  3242. Py_RETURN_NONE;
  3243. }
  3244. #endif /* HAVE_SYNC */
  3245. #ifdef HAVE_FDATASYNC
  3246. #ifdef __hpux
  3247. extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
  3248. #endif
  3249. /*[clinic input]
  3250. os.fdatasync
  3251. fd: fildes
  3252. Force write of fd to disk without forcing update of metadata.
  3253. [clinic start generated code]*/
  3254. static PyObject *
  3255. os_fdatasync_impl(PyObject *module, int fd)
  3256. /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
  3257. {
  3258. return posix_fildes_fd(fd, fdatasync);
  3259. }
  3260. #endif /* HAVE_FDATASYNC */
  3261. #ifdef HAVE_CHOWN
  3262. /*[clinic input]
  3263. os.chown
  3264. path : path_t(allow_fd='PATH_HAVE_FCHOWN')
  3265. Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
  3266. uid: uid_t
  3267. gid: gid_t
  3268. *
  3269. dir_fd : dir_fd(requires='fchownat') = None
  3270. If not None, it should be a file descriptor open to a directory,
  3271. and path should be relative; path will then be relative to that
  3272. directory.
  3273. follow_symlinks: bool = True
  3274. If False, and the last element of the path is a symbolic link,
  3275. stat will examine the symbolic link itself instead of the file
  3276. the link points to.
  3277. Change the owner and group id of path to the numeric uid and gid.\
  3278. path may always be specified as a string.
  3279. On some platforms, path may also be specified as an open file descriptor.
  3280. If this functionality is unavailable, using it raises an exception.
  3281. If dir_fd is not None, it should be a file descriptor open to a directory,
  3282. and path should be relative; path will then be relative to that directory.
  3283. If follow_symlinks is False, and the last element of the path is a symbolic
  3284. link, chown will modify the symbolic link itself instead of the file the
  3285. link points to.
  3286. It is an error to use dir_fd or follow_symlinks when specifying path as
  3287. an open file descriptor.
  3288. dir_fd and follow_symlinks may not be implemented on your platform.
  3289. If they are unavailable, using them will raise a NotImplementedError.
  3290. [clinic start generated code]*/
  3291. static PyObject *
  3292. os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
  3293. int dir_fd, int follow_symlinks)
  3294. /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
  3295. {
  3296. int result;
  3297. #if defined(HAVE_FCHOWNAT)
  3298. int fchownat_unsupported = 0;
  3299. #endif
  3300. #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
  3301. if (follow_symlinks_specified("chown", follow_symlinks))
  3302. return NULL;
  3303. #endif
  3304. if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
  3305. fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
  3306. return NULL;
  3307. if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
  3308. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  3309. return NULL;
  3310. }
  3311. Py_BEGIN_ALLOW_THREADS
  3312. #ifdef HAVE_FCHOWN
  3313. if (path->fd != -1)
  3314. result = fchown(path->fd, uid, gid);
  3315. else
  3316. #endif
  3317. #ifdef HAVE_LCHOWN
  3318. if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
  3319. result = lchown(path->narrow, uid, gid);
  3320. else
  3321. #endif
  3322. #ifdef HAVE_FCHOWNAT
  3323. if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
  3324. if (HAVE_FCHOWNAT_RUNTIME) {
  3325. result = fchownat(dir_fd, path->narrow, uid, gid,
  3326. follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
  3327. } else {
  3328. fchownat_unsupported = 1;
  3329. }
  3330. } else
  3331. #endif
  3332. result = chown(path->narrow, uid, gid);
  3333. Py_END_ALLOW_THREADS
  3334. #ifdef HAVE_FCHOWNAT
  3335. if (fchownat_unsupported) {
  3336. /* This would be incorrect if the current platform
  3337. * doesn't support lchown.
  3338. */
  3339. argument_unavailable_error(NULL, "dir_fd");
  3340. return NULL;
  3341. }
  3342. #endif
  3343. if (result)
  3344. return path_error(path);
  3345. Py_RETURN_NONE;
  3346. }
  3347. #endif /* HAVE_CHOWN */
  3348. #ifdef HAVE_FCHOWN
  3349. /*[clinic input]
  3350. os.fchown
  3351. fd: int
  3352. uid: uid_t
  3353. gid: gid_t
  3354. Change the owner and group id of the file specified by file descriptor.
  3355. Equivalent to os.chown(fd, uid, gid).
  3356. [clinic start generated code]*/
  3357. static PyObject *
  3358. os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
  3359. /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
  3360. {
  3361. int res;
  3362. int async_err = 0;
  3363. if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
  3364. return NULL;
  3365. }
  3366. do {
  3367. Py_BEGIN_ALLOW_THREADS
  3368. res = fchown(fd, uid, gid);
  3369. Py_END_ALLOW_THREADS
  3370. } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  3371. if (res != 0)
  3372. return (!async_err) ? posix_error() : NULL;
  3373. Py_RETURN_NONE;
  3374. }
  3375. #endif /* HAVE_FCHOWN */
  3376. #ifdef HAVE_LCHOWN
  3377. /*[clinic input]
  3378. os.lchown
  3379. path : path_t
  3380. uid: uid_t
  3381. gid: gid_t
  3382. Change the owner and group id of path to the numeric uid and gid.
  3383. This function will not follow symbolic links.
  3384. Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
  3385. [clinic start generated code]*/
  3386. static PyObject *
  3387. os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
  3388. /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
  3389. {
  3390. int res;
  3391. if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
  3392. return NULL;
  3393. }
  3394. Py_BEGIN_ALLOW_THREADS
  3395. res = lchown(path->narrow, uid, gid);
  3396. Py_END_ALLOW_THREADS
  3397. if (res < 0) {
  3398. return path_error(path);
  3399. }
  3400. Py_RETURN_NONE;
  3401. }
  3402. #endif /* HAVE_LCHOWN */
  3403. static PyObject *
  3404. posix_getcwd(int use_bytes)
  3405. {
  3406. #ifdef MS_WINDOWS
  3407. wchar_t wbuf[MAXPATHLEN];
  3408. wchar_t *wbuf2 = wbuf;
  3409. DWORD len;
  3410. Py_BEGIN_ALLOW_THREADS
  3411. len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
  3412. /* If the buffer is large enough, len does not include the
  3413. terminating \0. If the buffer is too small, len includes
  3414. the space needed for the terminator. */
  3415. if (len >= Py_ARRAY_LENGTH(wbuf)) {
  3416. if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
  3417. wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
  3418. }
  3419. else {
  3420. wbuf2 = NULL;
  3421. }
  3422. if (wbuf2) {
  3423. len = GetCurrentDirectoryW(len, wbuf2);
  3424. }
  3425. }
  3426. Py_END_ALLOW_THREADS
  3427. if (!wbuf2) {
  3428. PyErr_NoMemory();
  3429. return NULL;
  3430. }
  3431. if (!len) {
  3432. PyErr_SetFromWindowsErr(0);
  3433. if (wbuf2 != wbuf)
  3434. PyMem_RawFree(wbuf2);
  3435. return NULL;
  3436. }
  3437. PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
  3438. if (wbuf2 != wbuf) {
  3439. PyMem_RawFree(wbuf2);
  3440. }
  3441. if (use_bytes) {
  3442. if (resobj == NULL) {
  3443. return NULL;
  3444. }
  3445. Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
  3446. }
  3447. return resobj;
  3448. #else
  3449. const size_t chunk = 1024;
  3450. char *buf = NULL;
  3451. char *cwd = NULL;
  3452. size_t buflen = 0;
  3453. Py_BEGIN_ALLOW_THREADS
  3454. do {
  3455. char *newbuf;
  3456. if (buflen <= PY_SSIZE_T_MAX - chunk) {
  3457. buflen += chunk;
  3458. newbuf = PyMem_RawRealloc(buf, buflen);
  3459. }
  3460. else {
  3461. newbuf = NULL;
  3462. }
  3463. if (newbuf == NULL) {
  3464. PyMem_RawFree(buf);
  3465. buf = NULL;
  3466. break;
  3467. }
  3468. buf = newbuf;
  3469. cwd = getcwd(buf, buflen);
  3470. } while (cwd == NULL && errno == ERANGE);
  3471. Py_END_ALLOW_THREADS
  3472. if (buf == NULL) {
  3473. return PyErr_NoMemory();
  3474. }
  3475. if (cwd == NULL) {
  3476. posix_error();
  3477. PyMem_RawFree(buf);
  3478. return NULL;
  3479. }
  3480. PyObject *obj;
  3481. if (use_bytes) {
  3482. obj = PyBytes_FromStringAndSize(buf, strlen(buf));
  3483. }
  3484. else {
  3485. obj = PyUnicode_DecodeFSDefault(buf);
  3486. }
  3487. PyMem_RawFree(buf);
  3488. return obj;
  3489. #endif /* !MS_WINDOWS */
  3490. }
  3491. /*[clinic input]
  3492. os.getcwd
  3493. Return a unicode string representing the current working directory.
  3494. [clinic start generated code]*/
  3495. static PyObject *
  3496. os_getcwd_impl(PyObject *module)
  3497. /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
  3498. {
  3499. return posix_getcwd(0);
  3500. }
  3501. /*[clinic input]
  3502. os.getcwdb
  3503. Return a bytes string representing the current working directory.
  3504. [clinic start generated code]*/
  3505. static PyObject *
  3506. os_getcwdb_impl(PyObject *module)
  3507. /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
  3508. {
  3509. return posix_getcwd(1);
  3510. }
  3511. #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
  3512. #define HAVE_LINK 1
  3513. #endif
  3514. #ifdef HAVE_LINK
  3515. /*[clinic input]
  3516. os.link
  3517. src : path_t
  3518. dst : path_t
  3519. *
  3520. src_dir_fd : dir_fd = None
  3521. dst_dir_fd : dir_fd = None
  3522. follow_symlinks: bool = True
  3523. Create a hard link to a file.
  3524. If either src_dir_fd or dst_dir_fd is not None, it should be a file
  3525. descriptor open to a directory, and the respective path string (src or dst)
  3526. should be relative; the path will then be relative to that directory.
  3527. If follow_symlinks is False, and the last element of src is a symbolic
  3528. link, link will create a link to the symbolic link itself instead of the
  3529. file the link points to.
  3530. src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
  3531. platform. If they are unavailable, using them will raise a
  3532. NotImplementedError.
  3533. [clinic start generated code]*/
  3534. static PyObject *
  3535. os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
  3536. int dst_dir_fd, int follow_symlinks)
  3537. /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
  3538. {
  3539. #ifdef MS_WINDOWS
  3540. BOOL result = FALSE;
  3541. #else
  3542. int result;
  3543. #endif
  3544. #if defined(HAVE_LINKAT)
  3545. int linkat_unavailable = 0;
  3546. #endif
  3547. #ifndef HAVE_LINKAT
  3548. if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
  3549. argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
  3550. return NULL;
  3551. }
  3552. #endif
  3553. #ifndef MS_WINDOWS
  3554. if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
  3555. PyErr_SetString(PyExc_NotImplementedError,
  3556. "link: src and dst must be the same type");
  3557. return NULL;
  3558. }
  3559. #endif
  3560. if (PySys_Audit("os.link", "OOii", src->object, dst->object,
  3561. src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
  3562. dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
  3563. return NULL;
  3564. }
  3565. #ifdef MS_WINDOWS
  3566. Py_BEGIN_ALLOW_THREADS
  3567. result = CreateHardLinkW(dst->wide, src->wide, NULL);
  3568. Py_END_ALLOW_THREADS
  3569. if (!result)
  3570. return path_error2(src, dst);
  3571. #else
  3572. Py_BEGIN_ALLOW_THREADS
  3573. #ifdef HAVE_LINKAT
  3574. if ((src_dir_fd != DEFAULT_DIR_FD) ||
  3575. (dst_dir_fd != DEFAULT_DIR_FD) ||
  3576. (!follow_symlinks)) {
  3577. if (HAVE_LINKAT_RUNTIME) {
  3578. result = linkat(src_dir_fd, src->narrow,
  3579. dst_dir_fd, dst->narrow,
  3580. follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
  3581. }
  3582. #ifdef __APPLE__
  3583. else {
  3584. if (src_dir_fd == DEFAULT_DIR_FD && dst_dir_fd == DEFAULT_DIR_FD) {
  3585. /* See issue 41355: This matches the behaviour of !HAVE_LINKAT */
  3586. result = link(src->narrow, dst->narrow);
  3587. } else {
  3588. linkat_unavailable = 1;
  3589. }
  3590. }
  3591. #endif
  3592. }
  3593. else
  3594. #endif /* HAVE_LINKAT */
  3595. result = link(src->narrow, dst->narrow);
  3596. Py_END_ALLOW_THREADS
  3597. #ifdef HAVE_LINKAT
  3598. if (linkat_unavailable) {
  3599. /* Either or both dir_fd arguments were specified */
  3600. if (src_dir_fd != DEFAULT_DIR_FD) {
  3601. argument_unavailable_error("link", "src_dir_fd");
  3602. } else {
  3603. argument_unavailable_error("link", "dst_dir_fd");
  3604. }
  3605. return NULL;
  3606. }
  3607. #endif
  3608. if (result)
  3609. return path_error2(src, dst);
  3610. #endif /* MS_WINDOWS */
  3611. Py_RETURN_NONE;
  3612. }
  3613. #endif
  3614. #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
  3615. static PyObject *
  3616. _listdir_windows_no_opendir(path_t *path, PyObject *list)
  3617. {
  3618. PyObject *v;
  3619. HANDLE hFindFile = INVALID_HANDLE_VALUE;
  3620. BOOL result, return_bytes;
  3621. wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
  3622. /* only claim to have space for MAX_PATH */
  3623. Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
  3624. wchar_t *wnamebuf = NULL;
  3625. WIN32_FIND_DATAW wFileData;
  3626. const wchar_t *po_wchars;
  3627. if (!path->wide) { /* Default arg: "." */
  3628. po_wchars = L".";
  3629. len = 1;
  3630. return_bytes = 0;
  3631. } else {
  3632. po_wchars = path->wide;
  3633. len = wcslen(path->wide);
  3634. return_bytes = PyBytes_Check(path->object);
  3635. }
  3636. /* The +5 is so we can append "\\*.*\0" */
  3637. wnamebuf = PyMem_New(wchar_t, len + 5);
  3638. if (!wnamebuf) {
  3639. PyErr_NoMemory();
  3640. goto exit;
  3641. }
  3642. wcscpy(wnamebuf, po_wchars);
  3643. if (len > 0) {
  3644. wchar_t wch = wnamebuf[len-1];
  3645. if (wch != SEP && wch != ALTSEP && wch != L':')
  3646. wnamebuf[len++] = SEP;
  3647. wcscpy(wnamebuf + len, L"*.*");
  3648. }
  3649. if ((list = PyList_New(0)) == NULL) {
  3650. goto exit;
  3651. }
  3652. Py_BEGIN_ALLOW_THREADS
  3653. hFindFile = FindFirstFileW(wnamebuf, &wFileData);
  3654. Py_END_ALLOW_THREADS
  3655. if (hFindFile == INVALID_HANDLE_VALUE) {
  3656. int error = GetLastError();
  3657. if (error == ERROR_FILE_NOT_FOUND)
  3658. goto exit;
  3659. path_error(path);
  3660. Py_CLEAR(list);
  3661. goto exit;
  3662. }
  3663. do {
  3664. /* Skip over . and .. */
  3665. if (wcscmp(wFileData.cFileName, L".") != 0 &&
  3666. wcscmp(wFileData.cFileName, L"..") != 0) {
  3667. v = PyUnicode_FromWideChar(wFileData.cFileName,
  3668. wcslen(wFileData.cFileName));
  3669. if (return_bytes && v) {
  3670. Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
  3671. }
  3672. if (v == NULL) {
  3673. Py_CLEAR(list);
  3674. break;
  3675. }
  3676. if (PyList_Append(list, v) != 0) {
  3677. Py_DECREF(v);
  3678. Py_CLEAR(list);
  3679. break;
  3680. }
  3681. Py_DECREF(v);
  3682. }
  3683. Py_BEGIN_ALLOW_THREADS
  3684. result = FindNextFileW(hFindFile, &wFileData);
  3685. Py_END_ALLOW_THREADS
  3686. /* FindNextFile sets error to ERROR_NO_MORE_FILES if
  3687. it got to the end of the directory. */
  3688. if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
  3689. path_error(path);
  3690. Py_CLEAR(list);
  3691. goto exit;
  3692. }
  3693. } while (result == TRUE);
  3694. exit:
  3695. if (hFindFile != INVALID_HANDLE_VALUE) {
  3696. if (FindClose(hFindFile) == FALSE) {
  3697. if (list != NULL) {
  3698. path_error(path);
  3699. Py_CLEAR(list);
  3700. }
  3701. }
  3702. }
  3703. PyMem_Free(wnamebuf);
  3704. return list;
  3705. } /* end of _listdir_windows_no_opendir */
  3706. #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
  3707. static PyObject *
  3708. _posix_listdir(path_t *path, PyObject *list)
  3709. {
  3710. PyObject *v;
  3711. DIR *dirp = NULL;
  3712. struct dirent *ep;
  3713. int return_str; /* if false, return bytes */
  3714. #ifdef HAVE_FDOPENDIR
  3715. int fd = -1;
  3716. #endif
  3717. errno = 0;
  3718. #ifdef HAVE_FDOPENDIR
  3719. if (path->fd != -1) {
  3720. if (HAVE_FDOPENDIR_RUNTIME) {
  3721. /* closedir() closes the FD, so we duplicate it */
  3722. fd = _Py_dup(path->fd);
  3723. if (fd == -1)
  3724. return NULL;
  3725. return_str = 1;
  3726. Py_BEGIN_ALLOW_THREADS
  3727. dirp = fdopendir(fd);
  3728. Py_END_ALLOW_THREADS
  3729. } else {
  3730. PyErr_SetString(PyExc_TypeError,
  3731. "listdir: path should be string, bytes, os.PathLike or None, not int");
  3732. return NULL;
  3733. }
  3734. }
  3735. else
  3736. #endif
  3737. {
  3738. const char *name;
  3739. if (path->narrow) {
  3740. name = path->narrow;
  3741. /* only return bytes if they specified a bytes object */
  3742. return_str = !PyBytes_Check(path->object);
  3743. }
  3744. else {
  3745. name = ".";
  3746. return_str = 1;
  3747. }
  3748. Py_BEGIN_ALLOW_THREADS
  3749. dirp = opendir(name);
  3750. Py_END_ALLOW_THREADS
  3751. }
  3752. if (dirp == NULL) {
  3753. path_error(path);
  3754. list = NULL;
  3755. #ifdef HAVE_FDOPENDIR
  3756. if (fd != -1) {
  3757. Py_BEGIN_ALLOW_THREADS
  3758. close(fd);
  3759. Py_END_ALLOW_THREADS
  3760. }
  3761. #endif
  3762. goto exit;
  3763. }
  3764. if ((list = PyList_New(0)) == NULL) {
  3765. goto exit;
  3766. }
  3767. for (;;) {
  3768. errno = 0;
  3769. Py_BEGIN_ALLOW_THREADS
  3770. ep = readdir(dirp);
  3771. Py_END_ALLOW_THREADS
  3772. if (ep == NULL) {
  3773. if (errno == 0) {
  3774. break;
  3775. } else {
  3776. path_error(path);
  3777. Py_CLEAR(list);
  3778. goto exit;
  3779. }
  3780. }
  3781. if (ep->d_name[0] == '.' &&
  3782. (NAMLEN(ep) == 1 ||
  3783. (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
  3784. continue;
  3785. if (return_str)
  3786. v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
  3787. else
  3788. v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
  3789. if (v == NULL) {
  3790. Py_CLEAR(list);
  3791. break;
  3792. }
  3793. if (PyList_Append(list, v) != 0) {
  3794. Py_DECREF(v);
  3795. Py_CLEAR(list);
  3796. break;
  3797. }
  3798. Py_DECREF(v);
  3799. }
  3800. exit:
  3801. if (dirp != NULL) {
  3802. Py_BEGIN_ALLOW_THREADS
  3803. #ifdef HAVE_FDOPENDIR
  3804. if (fd > -1)
  3805. rewinddir(dirp);
  3806. #endif
  3807. closedir(dirp);
  3808. Py_END_ALLOW_THREADS
  3809. }
  3810. return list;
  3811. } /* end of _posix_listdir */
  3812. #endif /* which OS */
  3813. /*[clinic input]
  3814. os.listdir
  3815. path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
  3816. Return a list containing the names of the files in the directory.
  3817. path can be specified as either str, bytes, or a path-like object. If path is bytes,
  3818. the filenames returned will also be bytes; in all other circumstances
  3819. the filenames returned will be str.
  3820. If path is None, uses the path='.'.
  3821. On some platforms, path may also be specified as an open file descriptor;\
  3822. the file descriptor must refer to a directory.
  3823. If this functionality is unavailable, using it raises NotImplementedError.
  3824. The list is in arbitrary order. It does not include the special
  3825. entries '.' and '..' even if they are present in the directory.
  3826. [clinic start generated code]*/
  3827. static PyObject *
  3828. os_listdir_impl(PyObject *module, path_t *path)
  3829. /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
  3830. {
  3831. if (PySys_Audit("os.listdir", "O",
  3832. path->object ? path->object : Py_None) < 0) {
  3833. return NULL;
  3834. }
  3835. #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
  3836. return _listdir_windows_no_opendir(path, NULL);
  3837. #else
  3838. return _posix_listdir(path, NULL);
  3839. #endif
  3840. }
  3841. #ifdef MS_WINDOWS
  3842. /*[clinic input]
  3843. os.listdrives
  3844. Return a list containing the names of drives in the system.
  3845. A drive name typically looks like 'C:\\'.
  3846. [clinic start generated code]*/
  3847. static PyObject *
  3848. os_listdrives_impl(PyObject *module)
  3849. /*[clinic end generated code: output=aaece9dacdf682b5 input=1af9ccc9e583798e]*/
  3850. {
  3851. /* Number of possible drives is limited, so 256 should always be enough.
  3852. On the day when it is not, listmounts() will have to be used. */
  3853. wchar_t buffer[256];
  3854. DWORD buflen = Py_ARRAY_LENGTH(buffer);
  3855. PyObject *result = NULL;
  3856. if (PySys_Audit("os.listdrives", NULL) < 0) {
  3857. return NULL;
  3858. }
  3859. Py_BEGIN_ALLOW_THREADS;
  3860. buflen = GetLogicalDriveStringsW(buflen, buffer);
  3861. Py_END_ALLOW_THREADS;
  3862. if (!buflen) {
  3863. PyErr_SetFromWindowsErr(0);
  3864. return NULL;
  3865. } else if (buflen >= Py_ARRAY_LENGTH(buffer)) {
  3866. PyErr_SetFromWindowsErr(ERROR_MORE_DATA);
  3867. return NULL;
  3868. }
  3869. /* buflen includes a null terminator, so remove it */
  3870. PyObject *str = PyUnicode_FromWideChar(buffer, buflen - 1);
  3871. if (str) {
  3872. PyObject *nullchar = PyUnicode_FromStringAndSize("\0", 1);
  3873. if (nullchar) {
  3874. result = PyUnicode_Split(str, nullchar, -1);
  3875. Py_DECREF(nullchar);
  3876. }
  3877. Py_DECREF(str);
  3878. }
  3879. return result;
  3880. }
  3881. /*[clinic input]
  3882. os.listvolumes
  3883. Return a list containing the volumes in the system.
  3884. Volumes are typically represented as a GUID path.
  3885. [clinic start generated code]*/
  3886. static PyObject *
  3887. os_listvolumes_impl(PyObject *module)
  3888. /*[clinic end generated code: output=534e10ea2bf9d386 input=f6e4e70371f11e99]*/
  3889. {
  3890. PyObject *result = PyList_New(0);
  3891. HANDLE find = INVALID_HANDLE_VALUE;
  3892. wchar_t buffer[MAX_PATH + 1];
  3893. if (!result) {
  3894. return NULL;
  3895. }
  3896. if (PySys_Audit("os.listvolumes", NULL) < 0) {
  3897. Py_DECREF(result);
  3898. return NULL;
  3899. }
  3900. int err = 0;
  3901. Py_BEGIN_ALLOW_THREADS;
  3902. find = FindFirstVolumeW(buffer, Py_ARRAY_LENGTH(buffer));
  3903. if (find == INVALID_HANDLE_VALUE) {
  3904. err = GetLastError();
  3905. }
  3906. Py_END_ALLOW_THREADS;
  3907. while (!err) {
  3908. PyObject *s = PyUnicode_FromWideChar(buffer, -1);
  3909. if (!s || PyList_Append(result, s) < 0) {
  3910. Py_XDECREF(s);
  3911. Py_CLEAR(result);
  3912. break;
  3913. }
  3914. Py_DECREF(s);
  3915. Py_BEGIN_ALLOW_THREADS;
  3916. if (!FindNextVolumeW(find, buffer, Py_ARRAY_LENGTH(buffer))) {
  3917. err = GetLastError();
  3918. }
  3919. Py_END_ALLOW_THREADS;
  3920. }
  3921. if (find != INVALID_HANDLE_VALUE) {
  3922. Py_BEGIN_ALLOW_THREADS;
  3923. FindVolumeClose(find);
  3924. Py_END_ALLOW_THREADS;
  3925. }
  3926. if (err && err != ERROR_NO_MORE_FILES) {
  3927. PyErr_SetFromWindowsErr(err);
  3928. Py_XDECREF(result);
  3929. result = NULL;
  3930. }
  3931. return result;
  3932. }
  3933. /*[clinic input]
  3934. os.listmounts
  3935. volume: path_t
  3936. Return a list containing mount points for a particular volume.
  3937. 'volume' should be a GUID path as returned from os.listvolumes.
  3938. [clinic start generated code]*/
  3939. static PyObject *
  3940. os_listmounts_impl(PyObject *module, path_t *volume)
  3941. /*[clinic end generated code: output=06da49679de4512e input=a8a27178e3f67845]*/
  3942. {
  3943. wchar_t default_buffer[MAX_PATH + 1];
  3944. DWORD buflen = Py_ARRAY_LENGTH(default_buffer);
  3945. LPWSTR buffer = default_buffer;
  3946. DWORD attributes;
  3947. PyObject *str = NULL;
  3948. PyObject *nullchar = NULL;
  3949. PyObject *result = NULL;
  3950. /* Ensure we have a valid volume path before continuing */
  3951. Py_BEGIN_ALLOW_THREADS
  3952. attributes = GetFileAttributesW(volume->wide);
  3953. Py_END_ALLOW_THREADS
  3954. if (attributes == INVALID_FILE_ATTRIBUTES &&
  3955. GetLastError() == ERROR_UNRECOGNIZED_VOLUME)
  3956. {
  3957. return PyErr_SetFromWindowsErr(ERROR_UNRECOGNIZED_VOLUME);
  3958. }
  3959. if (PySys_Audit("os.listmounts", "O", volume->object) < 0) {
  3960. return NULL;
  3961. }
  3962. while (1) {
  3963. BOOL success;
  3964. Py_BEGIN_ALLOW_THREADS
  3965. success = GetVolumePathNamesForVolumeNameW(volume->wide, buffer,
  3966. buflen, &buflen);
  3967. Py_END_ALLOW_THREADS
  3968. if (success) {
  3969. break;
  3970. }
  3971. if (GetLastError() != ERROR_MORE_DATA) {
  3972. PyErr_SetFromWindowsErr(0);
  3973. goto exit;
  3974. }
  3975. if (buffer != default_buffer) {
  3976. PyMem_Free((void *)buffer);
  3977. }
  3978. buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * buflen);
  3979. if (!buffer) {
  3980. PyErr_NoMemory();
  3981. goto exit;
  3982. }
  3983. }
  3984. if (buflen < 2) {
  3985. result = PyList_New(0);
  3986. goto exit;
  3987. }
  3988. // buflen includes two null terminators, one for the last string
  3989. // and one for the array of strings.
  3990. str = PyUnicode_FromWideChar(buffer, buflen - 2);
  3991. nullchar = PyUnicode_FromStringAndSize("\0", 1);
  3992. if (str && nullchar) {
  3993. result = PyUnicode_Split(str, nullchar, -1);
  3994. }
  3995. exit:
  3996. if (buffer != default_buffer) {
  3997. PyMem_Free(buffer);
  3998. }
  3999. Py_XDECREF(nullchar);
  4000. Py_XDECREF(str);
  4001. return result;
  4002. }
  4003. /*[clinic input]
  4004. os._path_isdevdrive
  4005. path: path_t
  4006. Determines whether the specified path is on a Windows Dev Drive.
  4007. [clinic start generated code]*/
  4008. static PyObject *
  4009. os__path_isdevdrive_impl(PyObject *module, path_t *path)
  4010. /*[clinic end generated code: output=1f437ea6677433a2 input=ee83e4996a48e23d]*/
  4011. {
  4012. #ifndef PERSISTENT_VOLUME_STATE_DEV_VOLUME
  4013. /* This flag will be documented at
  4014. https://learn.microsoft.com/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_fs_persistent_volume_information
  4015. after release, and will be available in the latest WinSDK.
  4016. We include the flag to avoid a specific version dependency
  4017. on the latest WinSDK. */
  4018. const int PERSISTENT_VOLUME_STATE_DEV_VOLUME = 0x00002000;
  4019. #endif
  4020. int err = 0;
  4021. PyObject *r = NULL;
  4022. wchar_t volume[MAX_PATH];
  4023. Py_BEGIN_ALLOW_THREADS
  4024. if (!GetVolumePathNameW(path->wide, volume, MAX_PATH)) {
  4025. /* invalid path of some kind */
  4026. /* Note that this also includes the case where a volume is mounted
  4027. in a path longer than 260 characters. This is likely to be rare
  4028. and problematic for other reasons, so a (soft) failure in this
  4029. check seems okay. */
  4030. err = GetLastError();
  4031. } else if (GetDriveTypeW(volume) != DRIVE_FIXED) {
  4032. /* only care about local dev drives */
  4033. r = Py_False;
  4034. } else {
  4035. HANDLE hVolume = CreateFileW(
  4036. volume,
  4037. FILE_READ_ATTRIBUTES,
  4038. FILE_SHARE_READ | FILE_SHARE_WRITE,
  4039. NULL,
  4040. OPEN_EXISTING,
  4041. FILE_FLAG_BACKUP_SEMANTICS,
  4042. NULL
  4043. );
  4044. if (hVolume == INVALID_HANDLE_VALUE) {
  4045. err = GetLastError();
  4046. } else {
  4047. FILE_FS_PERSISTENT_VOLUME_INFORMATION volumeState = {0};
  4048. volumeState.Version = 1;
  4049. volumeState.FlagMask = PERSISTENT_VOLUME_STATE_DEV_VOLUME;
  4050. if (!DeviceIoControl(
  4051. hVolume,
  4052. FSCTL_QUERY_PERSISTENT_VOLUME_STATE,
  4053. &volumeState,
  4054. sizeof(volumeState),
  4055. &volumeState,
  4056. sizeof(volumeState),
  4057. NULL,
  4058. NULL
  4059. )) {
  4060. err = GetLastError();
  4061. }
  4062. CloseHandle(hVolume);
  4063. if (err == ERROR_INVALID_PARAMETER) {
  4064. /* not supported on this platform */
  4065. r = Py_False;
  4066. } else if (!err) {
  4067. r = (volumeState.VolumeFlags & PERSISTENT_VOLUME_STATE_DEV_VOLUME)
  4068. ? Py_True : Py_False;
  4069. }
  4070. }
  4071. }
  4072. Py_END_ALLOW_THREADS
  4073. if (err) {
  4074. PyErr_SetFromWindowsErr(err);
  4075. return NULL;
  4076. }
  4077. if (r) {
  4078. return Py_NewRef(r);
  4079. }
  4080. return NULL;
  4081. }
  4082. int
  4083. _PyOS_getfullpathname(const wchar_t *path, wchar_t **abspath_p)
  4084. {
  4085. wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
  4086. DWORD result;
  4087. result = GetFullPathNameW(path,
  4088. Py_ARRAY_LENGTH(woutbuf), woutbuf,
  4089. NULL);
  4090. if (!result) {
  4091. return -1;
  4092. }
  4093. if (result >= Py_ARRAY_LENGTH(woutbuf)) {
  4094. if ((size_t)result <= (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t)) {
  4095. woutbufp = PyMem_RawMalloc((size_t)result * sizeof(wchar_t));
  4096. }
  4097. else {
  4098. woutbufp = NULL;
  4099. }
  4100. if (!woutbufp) {
  4101. *abspath_p = NULL;
  4102. return 0;
  4103. }
  4104. result = GetFullPathNameW(path, result, woutbufp, NULL);
  4105. if (!result) {
  4106. PyMem_RawFree(woutbufp);
  4107. return -1;
  4108. }
  4109. }
  4110. if (woutbufp != woutbuf) {
  4111. *abspath_p = woutbufp;
  4112. return 0;
  4113. }
  4114. *abspath_p = _PyMem_RawWcsdup(woutbufp);
  4115. return 0;
  4116. }
  4117. /* A helper function for abspath on win32 */
  4118. /*[clinic input]
  4119. os._getfullpathname
  4120. path: path_t
  4121. /
  4122. [clinic start generated code]*/
  4123. static PyObject *
  4124. os__getfullpathname_impl(PyObject *module, path_t *path)
  4125. /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
  4126. {
  4127. wchar_t *abspath;
  4128. if (_PyOS_getfullpathname(path->wide, &abspath) < 0) {
  4129. return win32_error_object("GetFullPathNameW", path->object);
  4130. }
  4131. if (abspath == NULL) {
  4132. return PyErr_NoMemory();
  4133. }
  4134. PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
  4135. PyMem_RawFree(abspath);
  4136. if (str == NULL) {
  4137. return NULL;
  4138. }
  4139. if (PyBytes_Check(path->object)) {
  4140. Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
  4141. }
  4142. return str;
  4143. }
  4144. /*[clinic input]
  4145. os._getfinalpathname
  4146. path: path_t
  4147. /
  4148. A helper function for samepath on windows.
  4149. [clinic start generated code]*/
  4150. static PyObject *
  4151. os__getfinalpathname_impl(PyObject *module, path_t *path)
  4152. /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
  4153. {
  4154. HANDLE hFile;
  4155. wchar_t buf[MAXPATHLEN], *target_path = buf;
  4156. int buf_size = Py_ARRAY_LENGTH(buf);
  4157. int result_length;
  4158. PyObject *result;
  4159. Py_BEGIN_ALLOW_THREADS
  4160. hFile = CreateFileW(
  4161. path->wide,
  4162. 0, /* desired access */
  4163. 0, /* share mode */
  4164. NULL, /* security attributes */
  4165. OPEN_EXISTING,
  4166. /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
  4167. FILE_FLAG_BACKUP_SEMANTICS,
  4168. NULL);
  4169. Py_END_ALLOW_THREADS
  4170. if (hFile == INVALID_HANDLE_VALUE) {
  4171. return win32_error_object("CreateFileW", path->object);
  4172. }
  4173. /* We have a good handle to the target, use it to determine the
  4174. target path name. */
  4175. while (1) {
  4176. Py_BEGIN_ALLOW_THREADS
  4177. result_length = GetFinalPathNameByHandleW(hFile, target_path,
  4178. buf_size, VOLUME_NAME_DOS);
  4179. Py_END_ALLOW_THREADS
  4180. if (!result_length) {
  4181. result = win32_error_object("GetFinalPathNameByHandleW",
  4182. path->object);
  4183. goto cleanup;
  4184. }
  4185. if (result_length < buf_size) {
  4186. break;
  4187. }
  4188. wchar_t *tmp;
  4189. tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
  4190. result_length * sizeof(*tmp));
  4191. if (!tmp) {
  4192. result = PyErr_NoMemory();
  4193. goto cleanup;
  4194. }
  4195. buf_size = result_length;
  4196. target_path = tmp;
  4197. }
  4198. result = PyUnicode_FromWideChar(target_path, result_length);
  4199. if (result && PyBytes_Check(path->object)) {
  4200. Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
  4201. }
  4202. cleanup:
  4203. if (target_path != buf) {
  4204. PyMem_Free(target_path);
  4205. }
  4206. CloseHandle(hFile);
  4207. return result;
  4208. }
  4209. /*[clinic input]
  4210. os._getvolumepathname
  4211. path: path_t
  4212. A helper function for ismount on Win32.
  4213. [clinic start generated code]*/
  4214. static PyObject *
  4215. os__getvolumepathname_impl(PyObject *module, path_t *path)
  4216. /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
  4217. {
  4218. PyObject *result;
  4219. wchar_t *mountpath=NULL;
  4220. size_t buflen;
  4221. BOOL ret;
  4222. /* Volume path should be shorter than entire path */
  4223. buflen = Py_MAX(path->length, MAX_PATH);
  4224. if (buflen > PY_DWORD_MAX) {
  4225. PyErr_SetString(PyExc_OverflowError, "path too long");
  4226. return NULL;
  4227. }
  4228. mountpath = PyMem_New(wchar_t, buflen);
  4229. if (mountpath == NULL)
  4230. return PyErr_NoMemory();
  4231. Py_BEGIN_ALLOW_THREADS
  4232. ret = GetVolumePathNameW(path->wide, mountpath,
  4233. Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
  4234. Py_END_ALLOW_THREADS
  4235. if (!ret) {
  4236. result = win32_error_object("_getvolumepathname", path->object);
  4237. goto exit;
  4238. }
  4239. result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
  4240. if (PyBytes_Check(path->object))
  4241. Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
  4242. exit:
  4243. PyMem_Free(mountpath);
  4244. return result;
  4245. }
  4246. /*[clinic input]
  4247. os._path_splitroot
  4248. path: path_t
  4249. Removes everything after the root on Win32.
  4250. [clinic start generated code]*/
  4251. static PyObject *
  4252. os__path_splitroot_impl(PyObject *module, path_t *path)
  4253. /*[clinic end generated code: output=ab7f1a88b654581c input=dc93b1d3984cffb6]*/
  4254. {
  4255. wchar_t *buffer;
  4256. wchar_t *end;
  4257. PyObject *result = NULL;
  4258. HRESULT ret;
  4259. buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * (wcslen(path->wide) + 1));
  4260. if (!buffer) {
  4261. return NULL;
  4262. }
  4263. wcscpy(buffer, path->wide);
  4264. for (wchar_t *p = wcschr(buffer, L'/'); p; p = wcschr(p, L'/')) {
  4265. *p = L'\\';
  4266. }
  4267. Py_BEGIN_ALLOW_THREADS
  4268. ret = PathCchSkipRoot(buffer, &end);
  4269. Py_END_ALLOW_THREADS
  4270. if (FAILED(ret)) {
  4271. result = Py_BuildValue("sO", "", path->object);
  4272. } else if (end != buffer) {
  4273. size_t rootLen = (size_t)(end - buffer);
  4274. result = Py_BuildValue("NN",
  4275. PyUnicode_FromWideChar(path->wide, rootLen),
  4276. PyUnicode_FromWideChar(path->wide + rootLen, -1)
  4277. );
  4278. } else {
  4279. result = Py_BuildValue("Os", path->object, "");
  4280. }
  4281. PyMem_Free(buffer);
  4282. return result;
  4283. }
  4284. #define PY_IFREG 1 // Regular file
  4285. #define PY_IFDIR 2 // Directory
  4286. #define PY_IFLNK 4 // Symlink
  4287. #define PY_IFMNT 8 // Mount Point (junction)
  4288. #define PY_IFLRP 16 // Link Reparse Point (name-surrogate, symlink, junction)
  4289. #define PY_IFRRP 32 // Regular Reparse Point
  4290. static inline BOOL
  4291. _testInfo(DWORD attributes, DWORD reparseTag, BOOL diskDevice, int testedType)
  4292. {
  4293. switch (testedType) {
  4294. case PY_IFREG:
  4295. return diskDevice && attributes &&
  4296. !(attributes & FILE_ATTRIBUTE_DIRECTORY);
  4297. case PY_IFDIR:
  4298. return attributes & FILE_ATTRIBUTE_DIRECTORY;
  4299. case PY_IFLNK:
  4300. return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  4301. reparseTag == IO_REPARSE_TAG_SYMLINK;
  4302. case PY_IFMNT:
  4303. return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  4304. reparseTag == IO_REPARSE_TAG_MOUNT_POINT;
  4305. case PY_IFLRP:
  4306. return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  4307. IsReparseTagNameSurrogate(reparseTag);
  4308. case PY_IFRRP:
  4309. return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  4310. reparseTag && !IsReparseTagNameSurrogate(reparseTag);
  4311. }
  4312. return FALSE;
  4313. }
  4314. static BOOL
  4315. _testFileTypeByHandle(HANDLE hfile, int testedType, BOOL diskOnly)
  4316. {
  4317. assert(testedType == PY_IFREG || testedType == PY_IFDIR ||
  4318. testedType == PY_IFLNK || testedType == PY_IFMNT ||
  4319. testedType == PY_IFLRP || testedType == PY_IFRRP);
  4320. BOOL diskDevice = GetFileType(hfile) == FILE_TYPE_DISK;
  4321. if (diskOnly && !diskDevice) {
  4322. return FALSE;
  4323. }
  4324. if (testedType != PY_IFREG && testedType != PY_IFDIR) {
  4325. FILE_ATTRIBUTE_TAG_INFO info;
  4326. return GetFileInformationByHandleEx(hfile, FileAttributeTagInfo, &info,
  4327. sizeof(info)) &&
  4328. _testInfo(info.FileAttributes, info.ReparseTag, diskDevice,
  4329. testedType);
  4330. }
  4331. FILE_BASIC_INFO info;
  4332. return GetFileInformationByHandleEx(hfile, FileBasicInfo, &info,
  4333. sizeof(info)) &&
  4334. _testInfo(info.FileAttributes, 0, diskDevice, testedType);
  4335. }
  4336. static BOOL
  4337. _testFileTypeByName(LPCWSTR path, int testedType)
  4338. {
  4339. assert(testedType == PY_IFREG || testedType == PY_IFDIR ||
  4340. testedType == PY_IFLNK || testedType == PY_IFMNT ||
  4341. testedType == PY_IFLRP || testedType == PY_IFRRP);
  4342. FILE_STAT_BASIC_INFORMATION info;
  4343. if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo, &info,
  4344. sizeof(info)))
  4345. {
  4346. BOOL diskDevice = info.DeviceType == FILE_DEVICE_DISK ||
  4347. info.DeviceType == FILE_DEVICE_VIRTUAL_DISK ||
  4348. info.DeviceType == FILE_DEVICE_CD_ROM;
  4349. BOOL result = _testInfo(info.FileAttributes, info.ReparseTag,
  4350. diskDevice, testedType);
  4351. if (!result || (testedType != PY_IFREG && testedType != PY_IFDIR) ||
  4352. !(info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
  4353. {
  4354. return result;
  4355. }
  4356. }
  4357. else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
  4358. GetLastError()))
  4359. {
  4360. return FALSE;
  4361. }
  4362. DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
  4363. if (testedType != PY_IFREG && testedType != PY_IFDIR) {
  4364. flags |= FILE_FLAG_OPEN_REPARSE_POINT;
  4365. }
  4366. HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
  4367. OPEN_EXISTING, flags, NULL);
  4368. if (hfile != INVALID_HANDLE_VALUE) {
  4369. BOOL result = _testFileTypeByHandle(hfile, testedType, FALSE);
  4370. CloseHandle(hfile);
  4371. return result;
  4372. }
  4373. switch (GetLastError()) {
  4374. case ERROR_ACCESS_DENIED:
  4375. case ERROR_SHARING_VIOLATION:
  4376. case ERROR_CANT_ACCESS_FILE:
  4377. case ERROR_INVALID_PARAMETER:
  4378. int rc;
  4379. STRUCT_STAT st;
  4380. if (testedType == PY_IFREG || testedType == PY_IFDIR) {
  4381. rc = STAT(path, &st);
  4382. }
  4383. else {
  4384. // PY_IFRRP is not generally supported in this case, except for
  4385. // unhandled reparse points such as IO_REPARSE_TAG_APPEXECLINK.
  4386. rc = LSTAT(path, &st);
  4387. }
  4388. if (!rc) {
  4389. return _testInfo(st.st_file_attributes, st.st_reparse_tag,
  4390. st.st_mode & S_IFREG, testedType);
  4391. }
  4392. }
  4393. return FALSE;
  4394. }
  4395. static BOOL
  4396. _testFileExistsByName(LPCWSTR path, BOOL followLinks)
  4397. {
  4398. FILE_STAT_BASIC_INFORMATION info;
  4399. if (_Py_GetFileInformationByName(path, FileStatBasicByNameInfo, &info,
  4400. sizeof(info)))
  4401. {
  4402. if (!(info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ||
  4403. !followLinks && IsReparseTagNameSurrogate(info.ReparseTag))
  4404. {
  4405. return TRUE;
  4406. }
  4407. }
  4408. else if (_Py_GetFileInformationByName_ErrorIsTrustworthy(
  4409. GetLastError()))
  4410. {
  4411. return FALSE;
  4412. }
  4413. DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
  4414. if (!followLinks) {
  4415. flags |= FILE_FLAG_OPEN_REPARSE_POINT;
  4416. }
  4417. HANDLE hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
  4418. OPEN_EXISTING, flags, NULL);
  4419. if (hfile != INVALID_HANDLE_VALUE) {
  4420. if (followLinks) {
  4421. CloseHandle(hfile);
  4422. return TRUE;
  4423. }
  4424. // Regular Reparse Points (PY_IFRRP) have to be traversed.
  4425. BOOL result = _testFileTypeByHandle(hfile, PY_IFRRP, FALSE);
  4426. CloseHandle(hfile);
  4427. if (!result) {
  4428. return TRUE;
  4429. }
  4430. hfile = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
  4431. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  4432. if (hfile != INVALID_HANDLE_VALUE) {
  4433. CloseHandle(hfile);
  4434. return TRUE;
  4435. }
  4436. }
  4437. switch (GetLastError()) {
  4438. case ERROR_ACCESS_DENIED:
  4439. case ERROR_SHARING_VIOLATION:
  4440. case ERROR_CANT_ACCESS_FILE:
  4441. case ERROR_INVALID_PARAMETER:
  4442. STRUCT_STAT _st;
  4443. return followLinks ? !STAT(path, &_st): !LSTAT(path, &_st);
  4444. }
  4445. return FALSE;
  4446. }
  4447. static BOOL
  4448. _testFileExists(path_t *path, BOOL followLinks)
  4449. {
  4450. BOOL result = FALSE;
  4451. if (path->value_error) {
  4452. return FALSE;
  4453. }
  4454. Py_BEGIN_ALLOW_THREADS
  4455. if (path->fd != -1) {
  4456. HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
  4457. if (hfile != INVALID_HANDLE_VALUE) {
  4458. if (GetFileType(hfile) != FILE_TYPE_UNKNOWN || !GetLastError()) {
  4459. result = TRUE;
  4460. }
  4461. }
  4462. }
  4463. else if (path->wide) {
  4464. result = _testFileExistsByName(path->wide, followLinks);
  4465. }
  4466. Py_END_ALLOW_THREADS
  4467. return result;
  4468. }
  4469. static BOOL
  4470. _testFileType(path_t *path, int testedType)
  4471. {
  4472. BOOL result = FALSE;
  4473. if (path->value_error) {
  4474. return FALSE;
  4475. }
  4476. Py_BEGIN_ALLOW_THREADS
  4477. if (path->fd != -1) {
  4478. HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
  4479. if (hfile != INVALID_HANDLE_VALUE) {
  4480. result = _testFileTypeByHandle(hfile, testedType, TRUE);
  4481. }
  4482. }
  4483. else if (path->wide) {
  4484. result = _testFileTypeByName(path->wide, testedType);
  4485. }
  4486. Py_END_ALLOW_THREADS
  4487. return result;
  4488. }
  4489. /*[clinic input]
  4490. os._path_exists -> bool
  4491. path: path_t(allow_fd=True, suppress_value_error=True)
  4492. Test whether a path exists. Returns False for broken symbolic links.
  4493. [clinic start generated code]*/
  4494. static int
  4495. os__path_exists_impl(PyObject *module, path_t *path)
  4496. /*[clinic end generated code: output=8da13acf666e16ba input=142beabfc66783eb]*/
  4497. {
  4498. return _testFileExists(path, TRUE);
  4499. }
  4500. /*[clinic input]
  4501. os._path_isdir -> bool
  4502. s as path: path_t(allow_fd=True, suppress_value_error=True)
  4503. Return true if the pathname refers to an existing directory.
  4504. [clinic start generated code]*/
  4505. static int
  4506. os__path_isdir_impl(PyObject *module, path_t *path)
  4507. /*[clinic end generated code: output=d5786196f9e2fa7a input=132a3b5301aecf79]*/
  4508. {
  4509. return _testFileType(path, PY_IFDIR);
  4510. }
  4511. /*[clinic input]
  4512. os._path_isfile -> bool
  4513. path: path_t(allow_fd=True, suppress_value_error=True)
  4514. Test whether a path is a regular file
  4515. [clinic start generated code]*/
  4516. static int
  4517. os__path_isfile_impl(PyObject *module, path_t *path)
  4518. /*[clinic end generated code: output=5c3073bc212b9863 input=4ac1fd350b30a39e]*/
  4519. {
  4520. return _testFileType(path, PY_IFREG);
  4521. }
  4522. /*[clinic input]
  4523. os._path_islink -> bool
  4524. path: path_t(allow_fd=True, suppress_value_error=True)
  4525. Test whether a path is a symbolic link
  4526. [clinic start generated code]*/
  4527. static int
  4528. os__path_islink_impl(PyObject *module, path_t *path)
  4529. /*[clinic end generated code: output=30da7bda8296adcc input=7510ce05b547debb]*/
  4530. {
  4531. return _testFileType(path, PY_IFLNK);
  4532. }
  4533. #undef PY_IFREG
  4534. #undef PY_IFDIR
  4535. #undef PY_IFLNK
  4536. #undef PY_IFMNT
  4537. #undef PY_IFLRP
  4538. #undef PY_IFRRP
  4539. #endif /* MS_WINDOWS */
  4540. /*[clinic input]
  4541. os._path_normpath
  4542. path: path_t(make_wide=True, nonstrict=True)
  4543. Normalize path, eliminating double slashes, etc.
  4544. [clinic start generated code]*/
  4545. static PyObject *
  4546. os__path_normpath_impl(PyObject *module, path_t *path)
  4547. /*[clinic end generated code: output=d353e7ed9410c044 input=3d4ac23b06332dcb]*/
  4548. {
  4549. PyObject *result;
  4550. Py_ssize_t norm_len;
  4551. wchar_t *norm_path = _Py_normpath_and_size((wchar_t *)path->wide,
  4552. path->length, &norm_len);
  4553. if (!norm_len) {
  4554. result = PyUnicode_FromOrdinal('.');
  4555. }
  4556. else {
  4557. result = PyUnicode_FromWideChar(norm_path, norm_len);
  4558. }
  4559. if (PyBytes_Check(path->object)) {
  4560. Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
  4561. }
  4562. return result;
  4563. }
  4564. /*[clinic input]
  4565. os.mkdir
  4566. path : path_t
  4567. mode: int = 0o777
  4568. *
  4569. dir_fd : dir_fd(requires='mkdirat') = None
  4570. # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
  4571. Create a directory.
  4572. If dir_fd is not None, it should be a file descriptor open to a directory,
  4573. and path should be relative; path will then be relative to that directory.
  4574. dir_fd may not be implemented on your platform.
  4575. If it is unavailable, using it will raise a NotImplementedError.
  4576. The mode argument is ignored on Windows. Where it is used, the current umask
  4577. value is first masked out.
  4578. [clinic start generated code]*/
  4579. static PyObject *
  4580. os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
  4581. /*[clinic end generated code: output=a70446903abe821f input=a61722e1576fab03]*/
  4582. {
  4583. int result;
  4584. #ifdef MS_WINDOWS
  4585. int error = 0;
  4586. int pathError = 0;
  4587. SECURITY_ATTRIBUTES secAttr = { sizeof(secAttr) };
  4588. SECURITY_ATTRIBUTES *pSecAttr = NULL;
  4589. #endif
  4590. #ifdef HAVE_MKDIRAT
  4591. int mkdirat_unavailable = 0;
  4592. #endif
  4593. if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
  4594. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  4595. return NULL;
  4596. }
  4597. #ifdef MS_WINDOWS
  4598. Py_BEGIN_ALLOW_THREADS
  4599. if (mode == 0700 /* 0o700 */) {
  4600. ULONG sdSize;
  4601. pSecAttr = &secAttr;
  4602. // Set a discretionary ACL (D) that is protected (P) and includes
  4603. // inheritable (OICI) entries that allow (A) full control (FA) to
  4604. // SYSTEM (SY), Administrators (BA), and the owner (OW).
  4605. if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(
  4606. L"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)",
  4607. SDDL_REVISION_1,
  4608. &secAttr.lpSecurityDescriptor,
  4609. &sdSize
  4610. )) {
  4611. error = GetLastError();
  4612. }
  4613. }
  4614. if (!error) {
  4615. result = CreateDirectoryW(path->wide, pSecAttr);
  4616. if (secAttr.lpSecurityDescriptor &&
  4617. // uncommonly, LocalFree returns non-zero on error, but still uses
  4618. // GetLastError() to see what the error code is
  4619. LocalFree(secAttr.lpSecurityDescriptor)) {
  4620. error = GetLastError();
  4621. }
  4622. }
  4623. Py_END_ALLOW_THREADS
  4624. if (error) {
  4625. return PyErr_SetFromWindowsErr(error);
  4626. }
  4627. if (!result) {
  4628. return path_error(path);
  4629. }
  4630. #else
  4631. Py_BEGIN_ALLOW_THREADS
  4632. #if HAVE_MKDIRAT
  4633. if (dir_fd != DEFAULT_DIR_FD) {
  4634. if (HAVE_MKDIRAT_RUNTIME) {
  4635. result = mkdirat(dir_fd, path->narrow, mode);
  4636. } else {
  4637. mkdirat_unavailable = 1;
  4638. }
  4639. } else
  4640. #endif
  4641. #if defined(__WATCOMC__) && !defined(__QNX__)
  4642. result = mkdir(path->narrow);
  4643. #else
  4644. result = mkdir(path->narrow, mode);
  4645. #endif
  4646. Py_END_ALLOW_THREADS
  4647. #if HAVE_MKDIRAT
  4648. if (mkdirat_unavailable) {
  4649. argument_unavailable_error(NULL, "dir_fd");
  4650. return NULL;
  4651. }
  4652. #endif
  4653. if (result < 0)
  4654. return path_error(path);
  4655. #endif /* MS_WINDOWS */
  4656. Py_RETURN_NONE;
  4657. }
  4658. /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
  4659. #if defined(HAVE_SYS_RESOURCE_H)
  4660. #include <sys/resource.h>
  4661. #endif
  4662. #ifdef HAVE_NICE
  4663. /*[clinic input]
  4664. os.nice
  4665. increment: int
  4666. /
  4667. Add increment to the priority of process and return the new priority.
  4668. [clinic start generated code]*/
  4669. static PyObject *
  4670. os_nice_impl(PyObject *module, int increment)
  4671. /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
  4672. {
  4673. int value;
  4674. /* There are two flavours of 'nice': one that returns the new
  4675. priority (as required by almost all standards out there) and the
  4676. Linux/FreeBSD one, which returns '0' on success and advices
  4677. the use of getpriority() to get the new priority.
  4678. If we are of the nice family that returns the new priority, we
  4679. need to clear errno before the call, and check if errno is filled
  4680. before calling posix_error() on a returnvalue of -1, because the
  4681. -1 may be the actual new priority! */
  4682. errno = 0;
  4683. value = nice(increment);
  4684. #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
  4685. if (value == 0)
  4686. value = getpriority(PRIO_PROCESS, 0);
  4687. #endif
  4688. if (value == -1 && errno != 0)
  4689. /* either nice() or getpriority() returned an error */
  4690. return posix_error();
  4691. return PyLong_FromLong((long) value);
  4692. }
  4693. #endif /* HAVE_NICE */
  4694. #ifdef HAVE_GETPRIORITY
  4695. /*[clinic input]
  4696. os.getpriority
  4697. which: int
  4698. who: int
  4699. Return program scheduling priority.
  4700. [clinic start generated code]*/
  4701. static PyObject *
  4702. os_getpriority_impl(PyObject *module, int which, int who)
  4703. /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
  4704. {
  4705. int retval;
  4706. errno = 0;
  4707. retval = getpriority(which, who);
  4708. if (errno != 0)
  4709. return posix_error();
  4710. return PyLong_FromLong((long)retval);
  4711. }
  4712. #endif /* HAVE_GETPRIORITY */
  4713. #ifdef HAVE_SETPRIORITY
  4714. /*[clinic input]
  4715. os.setpriority
  4716. which: int
  4717. who: int
  4718. priority: int
  4719. Set program scheduling priority.
  4720. [clinic start generated code]*/
  4721. static PyObject *
  4722. os_setpriority_impl(PyObject *module, int which, int who, int priority)
  4723. /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
  4724. {
  4725. int retval;
  4726. retval = setpriority(which, who, priority);
  4727. if (retval == -1)
  4728. return posix_error();
  4729. Py_RETURN_NONE;
  4730. }
  4731. #endif /* HAVE_SETPRIORITY */
  4732. static PyObject *
  4733. internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
  4734. {
  4735. const char *function_name = is_replace ? "replace" : "rename";
  4736. int dir_fd_specified;
  4737. #ifdef HAVE_RENAMEAT
  4738. int renameat_unavailable = 0;
  4739. #endif
  4740. #ifdef MS_WINDOWS
  4741. BOOL result;
  4742. int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
  4743. #else
  4744. int result;
  4745. #endif
  4746. dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
  4747. (dst_dir_fd != DEFAULT_DIR_FD);
  4748. #ifndef HAVE_RENAMEAT
  4749. if (dir_fd_specified) {
  4750. argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
  4751. return NULL;
  4752. }
  4753. #endif
  4754. if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
  4755. src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
  4756. dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
  4757. return NULL;
  4758. }
  4759. #ifdef MS_WINDOWS
  4760. Py_BEGIN_ALLOW_THREADS
  4761. result = MoveFileExW(src->wide, dst->wide, flags);
  4762. Py_END_ALLOW_THREADS
  4763. if (!result)
  4764. return path_error2(src, dst);
  4765. #else
  4766. if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
  4767. PyErr_Format(PyExc_ValueError,
  4768. "%s: src and dst must be the same type", function_name);
  4769. return NULL;
  4770. }
  4771. Py_BEGIN_ALLOW_THREADS
  4772. #ifdef HAVE_RENAMEAT
  4773. if (dir_fd_specified) {
  4774. if (HAVE_RENAMEAT_RUNTIME) {
  4775. result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
  4776. } else {
  4777. renameat_unavailable = 1;
  4778. }
  4779. } else
  4780. #endif
  4781. result = rename(src->narrow, dst->narrow);
  4782. Py_END_ALLOW_THREADS
  4783. #ifdef HAVE_RENAMEAT
  4784. if (renameat_unavailable) {
  4785. argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
  4786. return NULL;
  4787. }
  4788. #endif
  4789. if (result)
  4790. return path_error2(src, dst);
  4791. #endif
  4792. Py_RETURN_NONE;
  4793. }
  4794. /*[clinic input]
  4795. os.rename
  4796. src : path_t
  4797. dst : path_t
  4798. *
  4799. src_dir_fd : dir_fd = None
  4800. dst_dir_fd : dir_fd = None
  4801. Rename a file or directory.
  4802. If either src_dir_fd or dst_dir_fd is not None, it should be a file
  4803. descriptor open to a directory, and the respective path string (src or dst)
  4804. should be relative; the path will then be relative to that directory.
  4805. src_dir_fd and dst_dir_fd, may not be implemented on your platform.
  4806. If they are unavailable, using them will raise a NotImplementedError.
  4807. [clinic start generated code]*/
  4808. static PyObject *
  4809. os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
  4810. int dst_dir_fd)
  4811. /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
  4812. {
  4813. return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
  4814. }
  4815. /*[clinic input]
  4816. os.replace = os.rename
  4817. Rename a file or directory, overwriting the destination.
  4818. If either src_dir_fd or dst_dir_fd is not None, it should be a file
  4819. descriptor open to a directory, and the respective path string (src or dst)
  4820. should be relative; the path will then be relative to that directory.
  4821. src_dir_fd and dst_dir_fd, may not be implemented on your platform.
  4822. If they are unavailable, using them will raise a NotImplementedError.
  4823. [clinic start generated code]*/
  4824. static PyObject *
  4825. os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
  4826. int dst_dir_fd)
  4827. /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
  4828. {
  4829. return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
  4830. }
  4831. /*[clinic input]
  4832. os.rmdir
  4833. path: path_t
  4834. *
  4835. dir_fd: dir_fd(requires='unlinkat') = None
  4836. Remove a directory.
  4837. If dir_fd is not None, it should be a file descriptor open to a directory,
  4838. and path should be relative; path will then be relative to that directory.
  4839. dir_fd may not be implemented on your platform.
  4840. If it is unavailable, using it will raise a NotImplementedError.
  4841. [clinic start generated code]*/
  4842. static PyObject *
  4843. os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
  4844. /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
  4845. {
  4846. int result;
  4847. #ifdef HAVE_UNLINKAT
  4848. int unlinkat_unavailable = 0;
  4849. #endif
  4850. if (PySys_Audit("os.rmdir", "Oi", path->object,
  4851. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  4852. return NULL;
  4853. }
  4854. Py_BEGIN_ALLOW_THREADS
  4855. #ifdef MS_WINDOWS
  4856. /* Windows, success=1, UNIX, success=0 */
  4857. result = !RemoveDirectoryW(path->wide);
  4858. #else
  4859. #ifdef HAVE_UNLINKAT
  4860. if (dir_fd != DEFAULT_DIR_FD) {
  4861. if (HAVE_UNLINKAT_RUNTIME) {
  4862. result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
  4863. } else {
  4864. unlinkat_unavailable = 1;
  4865. result = -1;
  4866. }
  4867. } else
  4868. #endif
  4869. result = rmdir(path->narrow);
  4870. #endif
  4871. Py_END_ALLOW_THREADS
  4872. #ifdef HAVE_UNLINKAT
  4873. if (unlinkat_unavailable) {
  4874. argument_unavailable_error("rmdir", "dir_fd");
  4875. return NULL;
  4876. }
  4877. #endif
  4878. if (result)
  4879. return path_error(path);
  4880. Py_RETURN_NONE;
  4881. }
  4882. #ifdef HAVE_SYSTEM
  4883. #ifdef MS_WINDOWS
  4884. /*[clinic input]
  4885. os.system -> long
  4886. command: Py_UNICODE
  4887. Execute the command in a subshell.
  4888. [clinic start generated code]*/
  4889. static long
  4890. os_system_impl(PyObject *module, const Py_UNICODE *command)
  4891. /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
  4892. {
  4893. long result;
  4894. if (PySys_Audit("os.system", "(u)", command) < 0) {
  4895. return -1;
  4896. }
  4897. Py_BEGIN_ALLOW_THREADS
  4898. _Py_BEGIN_SUPPRESS_IPH
  4899. result = _wsystem(command);
  4900. _Py_END_SUPPRESS_IPH
  4901. Py_END_ALLOW_THREADS
  4902. return result;
  4903. }
  4904. #else /* MS_WINDOWS */
  4905. /*[clinic input]
  4906. os.system -> long
  4907. command: FSConverter
  4908. Execute the command in a subshell.
  4909. [clinic start generated code]*/
  4910. static long
  4911. os_system_impl(PyObject *module, PyObject *command)
  4912. /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
  4913. {
  4914. long result;
  4915. const char *bytes = PyBytes_AsString(command);
  4916. if (PySys_Audit("os.system", "(O)", command) < 0) {
  4917. return -1;
  4918. }
  4919. Py_BEGIN_ALLOW_THREADS
  4920. result = system(bytes);
  4921. Py_END_ALLOW_THREADS
  4922. return result;
  4923. }
  4924. #endif
  4925. #endif /* HAVE_SYSTEM */
  4926. #ifdef HAVE_UMASK
  4927. /*[clinic input]
  4928. os.umask
  4929. mask: int
  4930. /
  4931. Set the current numeric umask and return the previous umask.
  4932. [clinic start generated code]*/
  4933. static PyObject *
  4934. os_umask_impl(PyObject *module, int mask)
  4935. /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
  4936. {
  4937. int i = (int)umask(mask);
  4938. if (i < 0)
  4939. return posix_error();
  4940. return PyLong_FromLong((long)i);
  4941. }
  4942. #endif
  4943. #ifdef MS_WINDOWS
  4944. /* override the default DeleteFileW behavior so that directory
  4945. symlinks can be removed with this function, the same as with
  4946. Unix symlinks */
  4947. BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
  4948. {
  4949. WIN32_FILE_ATTRIBUTE_DATA info;
  4950. WIN32_FIND_DATAW find_data;
  4951. HANDLE find_data_handle;
  4952. int is_directory = 0;
  4953. int is_link = 0;
  4954. if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
  4955. is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
  4956. /* Get WIN32_FIND_DATA structure for the path to determine if
  4957. it is a symlink */
  4958. if(is_directory &&
  4959. info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  4960. find_data_handle = FindFirstFileW(lpFileName, &find_data);
  4961. if(find_data_handle != INVALID_HANDLE_VALUE) {
  4962. /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
  4963. IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
  4964. is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
  4965. find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
  4966. FindClose(find_data_handle);
  4967. }
  4968. }
  4969. }
  4970. if (is_directory && is_link)
  4971. return RemoveDirectoryW(lpFileName);
  4972. return DeleteFileW(lpFileName);
  4973. }
  4974. #endif /* MS_WINDOWS */
  4975. /*[clinic input]
  4976. os.unlink
  4977. path: path_t
  4978. *
  4979. dir_fd: dir_fd(requires='unlinkat')=None
  4980. Remove a file (same as remove()).
  4981. If dir_fd is not None, it should be a file descriptor open to a directory,
  4982. and path should be relative; path will then be relative to that directory.
  4983. dir_fd may not be implemented on your platform.
  4984. If it is unavailable, using it will raise a NotImplementedError.
  4985. [clinic start generated code]*/
  4986. static PyObject *
  4987. os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
  4988. /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
  4989. {
  4990. int result;
  4991. #ifdef HAVE_UNLINKAT
  4992. int unlinkat_unavailable = 0;
  4993. #endif
  4994. if (PySys_Audit("os.remove", "Oi", path->object,
  4995. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  4996. return NULL;
  4997. }
  4998. Py_BEGIN_ALLOW_THREADS
  4999. _Py_BEGIN_SUPPRESS_IPH
  5000. #ifdef MS_WINDOWS
  5001. /* Windows, success=1, UNIX, success=0 */
  5002. result = !Py_DeleteFileW(path->wide);
  5003. #else
  5004. #ifdef HAVE_UNLINKAT
  5005. if (dir_fd != DEFAULT_DIR_FD) {
  5006. if (HAVE_UNLINKAT_RUNTIME) {
  5007. result = unlinkat(dir_fd, path->narrow, 0);
  5008. } else {
  5009. unlinkat_unavailable = 1;
  5010. }
  5011. } else
  5012. #endif /* HAVE_UNLINKAT */
  5013. result = unlink(path->narrow);
  5014. #endif
  5015. _Py_END_SUPPRESS_IPH
  5016. Py_END_ALLOW_THREADS
  5017. #ifdef HAVE_UNLINKAT
  5018. if (unlinkat_unavailable) {
  5019. argument_unavailable_error(NULL, "dir_fd");
  5020. return NULL;
  5021. }
  5022. #endif
  5023. if (result)
  5024. return path_error(path);
  5025. Py_RETURN_NONE;
  5026. }
  5027. /*[clinic input]
  5028. os.remove = os.unlink
  5029. Remove a file (same as unlink()).
  5030. If dir_fd is not None, it should be a file descriptor open to a directory,
  5031. and path should be relative; path will then be relative to that directory.
  5032. dir_fd may not be implemented on your platform.
  5033. If it is unavailable, using it will raise a NotImplementedError.
  5034. [clinic start generated code]*/
  5035. static PyObject *
  5036. os_remove_impl(PyObject *module, path_t *path, int dir_fd)
  5037. /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
  5038. {
  5039. return os_unlink_impl(module, path, dir_fd);
  5040. }
  5041. static PyStructSequence_Field uname_result_fields[] = {
  5042. {"sysname", "operating system name"},
  5043. {"nodename", "name of machine on network (implementation-defined)"},
  5044. {"release", "operating system release"},
  5045. {"version", "operating system version"},
  5046. {"machine", "hardware identifier"},
  5047. {NULL}
  5048. };
  5049. PyDoc_STRVAR(uname_result__doc__,
  5050. "uname_result: Result from os.uname().\n\n\
  5051. This object may be accessed either as a tuple of\n\
  5052. (sysname, nodename, release, version, machine),\n\
  5053. or via the attributes sysname, nodename, release, version, and machine.\n\
  5054. \n\
  5055. See os.uname for more information.");
  5056. static PyStructSequence_Desc uname_result_desc = {
  5057. MODNAME ".uname_result", /* name */
  5058. uname_result__doc__, /* doc */
  5059. uname_result_fields,
  5060. 5
  5061. };
  5062. #ifdef HAVE_UNAME
  5063. /*[clinic input]
  5064. os.uname
  5065. Return an object identifying the current operating system.
  5066. The object behaves like a named tuple with the following fields:
  5067. (sysname, nodename, release, version, machine)
  5068. [clinic start generated code]*/
  5069. static PyObject *
  5070. os_uname_impl(PyObject *module)
  5071. /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
  5072. {
  5073. struct utsname u;
  5074. int res;
  5075. PyObject *value;
  5076. Py_BEGIN_ALLOW_THREADS
  5077. res = uname(&u);
  5078. Py_END_ALLOW_THREADS
  5079. if (res < 0)
  5080. return posix_error();
  5081. PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
  5082. value = PyStructSequence_New((PyTypeObject *)UnameResultType);
  5083. if (value == NULL)
  5084. return NULL;
  5085. #define SET(i, field) \
  5086. { \
  5087. PyObject *o = PyUnicode_DecodeFSDefault(field); \
  5088. if (!o) { \
  5089. Py_DECREF(value); \
  5090. return NULL; \
  5091. } \
  5092. PyStructSequence_SET_ITEM(value, i, o); \
  5093. } \
  5094. SET(0, u.sysname);
  5095. SET(1, u.nodename);
  5096. SET(2, u.release);
  5097. SET(3, u.version);
  5098. SET(4, u.machine);
  5099. #undef SET
  5100. return value;
  5101. }
  5102. #endif /* HAVE_UNAME */
  5103. typedef struct {
  5104. int now;
  5105. time_t atime_s;
  5106. long atime_ns;
  5107. time_t mtime_s;
  5108. long mtime_ns;
  5109. } utime_t;
  5110. /*
  5111. * these macros assume that "ut" is a pointer to a utime_t
  5112. * they also intentionally leak the declaration of a pointer named "time"
  5113. */
  5114. #define UTIME_TO_TIMESPEC \
  5115. struct timespec ts[2]; \
  5116. struct timespec *time; \
  5117. if (ut->now) \
  5118. time = NULL; \
  5119. else { \
  5120. ts[0].tv_sec = ut->atime_s; \
  5121. ts[0].tv_nsec = ut->atime_ns; \
  5122. ts[1].tv_sec = ut->mtime_s; \
  5123. ts[1].tv_nsec = ut->mtime_ns; \
  5124. time = ts; \
  5125. } \
  5126. #define UTIME_TO_TIMEVAL \
  5127. struct timeval tv[2]; \
  5128. struct timeval *time; \
  5129. if (ut->now) \
  5130. time = NULL; \
  5131. else { \
  5132. tv[0].tv_sec = ut->atime_s; \
  5133. tv[0].tv_usec = ut->atime_ns / 1000; \
  5134. tv[1].tv_sec = ut->mtime_s; \
  5135. tv[1].tv_usec = ut->mtime_ns / 1000; \
  5136. time = tv; \
  5137. } \
  5138. #define UTIME_TO_UTIMBUF \
  5139. struct utimbuf u; \
  5140. struct utimbuf *time; \
  5141. if (ut->now) \
  5142. time = NULL; \
  5143. else { \
  5144. u.actime = ut->atime_s; \
  5145. u.modtime = ut->mtime_s; \
  5146. time = &u; \
  5147. }
  5148. #define UTIME_TO_TIME_T \
  5149. time_t timet[2]; \
  5150. time_t *time; \
  5151. if (ut->now) \
  5152. time = NULL; \
  5153. else { \
  5154. timet[0] = ut->atime_s; \
  5155. timet[1] = ut->mtime_s; \
  5156. time = timet; \
  5157. } \
  5158. #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
  5159. static int
  5160. utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
  5161. {
  5162. #if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
  5163. if (HAVE_UTIMENSAT_RUNTIME) {
  5164. int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
  5165. UTIME_TO_TIMESPEC;
  5166. return utimensat(dir_fd, path, time, flags);
  5167. } else {
  5168. errno = ENOSYS;
  5169. return -1;
  5170. }
  5171. #elif defined(HAVE_UTIMENSAT)
  5172. int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
  5173. UTIME_TO_TIMESPEC;
  5174. return utimensat(dir_fd, path, time, flags);
  5175. #elif defined(HAVE_FUTIMESAT)
  5176. UTIME_TO_TIMEVAL;
  5177. /*
  5178. * follow_symlinks will never be false here;
  5179. * we only allow !follow_symlinks and dir_fd together
  5180. * if we have utimensat()
  5181. */
  5182. assert(follow_symlinks);
  5183. return futimesat(dir_fd, path, time);
  5184. #endif
  5185. }
  5186. #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
  5187. #else
  5188. #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
  5189. #endif
  5190. #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
  5191. static int
  5192. utime_fd(utime_t *ut, int fd)
  5193. {
  5194. #ifdef HAVE_FUTIMENS
  5195. if (HAVE_FUTIMENS_RUNTIME) {
  5196. UTIME_TO_TIMESPEC;
  5197. return futimens(fd, time);
  5198. } else
  5199. #ifndef HAVE_FUTIMES
  5200. {
  5201. /* Not sure if this can happen */
  5202. PyErr_SetString(
  5203. PyExc_RuntimeError,
  5204. "neither futimens nor futimes are supported"
  5205. " on this system");
  5206. return -1;
  5207. }
  5208. #endif
  5209. #endif
  5210. #ifdef HAVE_FUTIMES
  5211. {
  5212. UTIME_TO_TIMEVAL;
  5213. return futimes(fd, time);
  5214. }
  5215. #endif
  5216. }
  5217. #define PATH_UTIME_HAVE_FD 1
  5218. #else
  5219. #define PATH_UTIME_HAVE_FD 0
  5220. #endif
  5221. #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
  5222. # define UTIME_HAVE_NOFOLLOW_SYMLINKS
  5223. #endif
  5224. #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
  5225. static int
  5226. utime_nofollow_symlinks(utime_t *ut, const char *path)
  5227. {
  5228. #ifdef HAVE_UTIMENSAT
  5229. if (HAVE_UTIMENSAT_RUNTIME) {
  5230. UTIME_TO_TIMESPEC;
  5231. return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
  5232. } else
  5233. #ifndef HAVE_LUTIMES
  5234. {
  5235. /* Not sure if this can happen */
  5236. PyErr_SetString(
  5237. PyExc_RuntimeError,
  5238. "neither utimensat nor lutimes are supported"
  5239. " on this system");
  5240. return -1;
  5241. }
  5242. #endif
  5243. #endif
  5244. #ifdef HAVE_LUTIMES
  5245. {
  5246. UTIME_TO_TIMEVAL;
  5247. return lutimes(path, time);
  5248. }
  5249. #endif
  5250. }
  5251. #endif
  5252. #ifndef MS_WINDOWS
  5253. static int
  5254. utime_default(utime_t *ut, const char *path)
  5255. {
  5256. #if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
  5257. if (HAVE_UTIMENSAT_RUNTIME) {
  5258. UTIME_TO_TIMESPEC;
  5259. return utimensat(DEFAULT_DIR_FD, path, time, 0);
  5260. } else {
  5261. UTIME_TO_TIMEVAL;
  5262. return utimes(path, time);
  5263. }
  5264. #elif defined(HAVE_UTIMENSAT)
  5265. UTIME_TO_TIMESPEC;
  5266. return utimensat(DEFAULT_DIR_FD, path, time, 0);
  5267. #elif defined(HAVE_UTIMES)
  5268. UTIME_TO_TIMEVAL;
  5269. return utimes(path, time);
  5270. #elif defined(HAVE_UTIME_H)
  5271. UTIME_TO_UTIMBUF;
  5272. return utime(path, time);
  5273. #else
  5274. UTIME_TO_TIME_T;
  5275. return utime(path, time);
  5276. #endif
  5277. }
  5278. #endif
  5279. static int
  5280. split_py_long_to_s_and_ns(PyObject *module, PyObject *py_long, time_t *s, long *ns)
  5281. {
  5282. int result = 0;
  5283. PyObject *divmod;
  5284. divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion);
  5285. if (!divmod)
  5286. goto exit;
  5287. if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
  5288. PyErr_Format(PyExc_TypeError,
  5289. "%.200s.__divmod__() must return a 2-tuple, not %.200s",
  5290. _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
  5291. goto exit;
  5292. }
  5293. *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
  5294. if ((*s == -1) && PyErr_Occurred())
  5295. goto exit;
  5296. *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
  5297. if ((*ns == -1) && PyErr_Occurred())
  5298. goto exit;
  5299. result = 1;
  5300. exit:
  5301. Py_XDECREF(divmod);
  5302. return result;
  5303. }
  5304. /*[clinic input]
  5305. os.utime
  5306. path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
  5307. times: object = None
  5308. *
  5309. ns: object = NULL
  5310. dir_fd: dir_fd(requires='futimensat') = None
  5311. follow_symlinks: bool=True
  5312. # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
  5313. Set the access and modified time of path.
  5314. path may always be specified as a string.
  5315. On some platforms, path may also be specified as an open file descriptor.
  5316. If this functionality is unavailable, using it raises an exception.
  5317. If times is not None, it must be a tuple (atime, mtime);
  5318. atime and mtime should be expressed as float seconds since the epoch.
  5319. If ns is specified, it must be a tuple (atime_ns, mtime_ns);
  5320. atime_ns and mtime_ns should be expressed as integer nanoseconds
  5321. since the epoch.
  5322. If times is None and ns is unspecified, utime uses the current time.
  5323. Specifying tuples for both times and ns is an error.
  5324. If dir_fd is not None, it should be a file descriptor open to a directory,
  5325. and path should be relative; path will then be relative to that directory.
  5326. If follow_symlinks is False, and the last element of the path is a symbolic
  5327. link, utime will modify the symbolic link itself instead of the file the
  5328. link points to.
  5329. It is an error to use dir_fd or follow_symlinks when specifying path
  5330. as an open file descriptor.
  5331. dir_fd and follow_symlinks may not be available on your platform.
  5332. If they are unavailable, using them will raise a NotImplementedError.
  5333. [clinic start generated code]*/
  5334. static PyObject *
  5335. os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
  5336. int dir_fd, int follow_symlinks)
  5337. /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
  5338. {
  5339. #ifdef MS_WINDOWS
  5340. HANDLE hFile;
  5341. FILETIME atime, mtime;
  5342. #else
  5343. int result;
  5344. #endif
  5345. utime_t utime;
  5346. memset(&utime, 0, sizeof(utime_t));
  5347. if (times != Py_None && ns) {
  5348. PyErr_SetString(PyExc_ValueError,
  5349. "utime: you may specify either 'times'"
  5350. " or 'ns' but not both");
  5351. return NULL;
  5352. }
  5353. if (times != Py_None) {
  5354. time_t a_sec, m_sec;
  5355. long a_nsec, m_nsec;
  5356. if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
  5357. PyErr_SetString(PyExc_TypeError,
  5358. "utime: 'times' must be either"
  5359. " a tuple of two ints or None");
  5360. return NULL;
  5361. }
  5362. utime.now = 0;
  5363. if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
  5364. &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
  5365. _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
  5366. &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
  5367. return NULL;
  5368. }
  5369. utime.atime_s = a_sec;
  5370. utime.atime_ns = a_nsec;
  5371. utime.mtime_s = m_sec;
  5372. utime.mtime_ns = m_nsec;
  5373. }
  5374. else if (ns) {
  5375. if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
  5376. PyErr_SetString(PyExc_TypeError,
  5377. "utime: 'ns' must be a tuple of two ints");
  5378. return NULL;
  5379. }
  5380. utime.now = 0;
  5381. if (!split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 0),
  5382. &utime.atime_s, &utime.atime_ns) ||
  5383. !split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 1),
  5384. &utime.mtime_s, &utime.mtime_ns)) {
  5385. return NULL;
  5386. }
  5387. }
  5388. else {
  5389. /* times and ns are both None/unspecified. use "now". */
  5390. utime.now = 1;
  5391. }
  5392. #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
  5393. if (follow_symlinks_specified("utime", follow_symlinks))
  5394. return NULL;
  5395. #endif
  5396. if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
  5397. dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
  5398. fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
  5399. return NULL;
  5400. #if !defined(HAVE_UTIMENSAT)
  5401. if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
  5402. PyErr_SetString(PyExc_ValueError,
  5403. "utime: cannot use dir_fd and follow_symlinks "
  5404. "together on this platform");
  5405. return NULL;
  5406. }
  5407. #endif
  5408. if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
  5409. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  5410. return NULL;
  5411. }
  5412. #ifdef MS_WINDOWS
  5413. Py_BEGIN_ALLOW_THREADS
  5414. hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
  5415. NULL, OPEN_EXISTING,
  5416. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  5417. Py_END_ALLOW_THREADS
  5418. if (hFile == INVALID_HANDLE_VALUE) {
  5419. path_error(path);
  5420. return NULL;
  5421. }
  5422. if (utime.now) {
  5423. GetSystemTimeAsFileTime(&mtime);
  5424. atime = mtime;
  5425. }
  5426. else {
  5427. _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
  5428. _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
  5429. }
  5430. if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
  5431. /* Avoid putting the file name into the error here,
  5432. as that may confuse the user into believing that
  5433. something is wrong with the file, when it also
  5434. could be the time stamp that gives a problem. */
  5435. PyErr_SetFromWindowsErr(0);
  5436. CloseHandle(hFile);
  5437. return NULL;
  5438. }
  5439. CloseHandle(hFile);
  5440. #else /* MS_WINDOWS */
  5441. Py_BEGIN_ALLOW_THREADS
  5442. #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
  5443. if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
  5444. result = utime_nofollow_symlinks(&utime, path->narrow);
  5445. else
  5446. #endif
  5447. #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
  5448. if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
  5449. result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
  5450. } else
  5451. #endif
  5452. #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
  5453. if (path->fd != -1)
  5454. result = utime_fd(&utime, path->fd);
  5455. else
  5456. #endif
  5457. result = utime_default(&utime, path->narrow);
  5458. Py_END_ALLOW_THREADS
  5459. #if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
  5460. /* See utime_dir_fd implementation */
  5461. if (result == -1 && errno == ENOSYS) {
  5462. argument_unavailable_error(NULL, "dir_fd");
  5463. return NULL;
  5464. }
  5465. #endif
  5466. if (result < 0) {
  5467. /* see previous comment about not putting filename in error here */
  5468. posix_error();
  5469. return NULL;
  5470. }
  5471. #endif /* MS_WINDOWS */
  5472. Py_RETURN_NONE;
  5473. }
  5474. /* Process operations */
  5475. /*[clinic input]
  5476. os._exit
  5477. status: int
  5478. Exit to the system with specified status, without normal exit processing.
  5479. [clinic start generated code]*/
  5480. static PyObject *
  5481. os__exit_impl(PyObject *module, int status)
  5482. /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
  5483. {
  5484. _exit(status);
  5485. return NULL; /* Make gcc -Wall happy */
  5486. }
  5487. #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
  5488. #define EXECV_CHAR wchar_t
  5489. #else
  5490. #define EXECV_CHAR char
  5491. #endif
  5492. #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
  5493. static void
  5494. free_string_array(EXECV_CHAR **array, Py_ssize_t count)
  5495. {
  5496. Py_ssize_t i;
  5497. for (i = 0; i < count; i++)
  5498. PyMem_Free(array[i]);
  5499. PyMem_Free(array);
  5500. }
  5501. static int
  5502. fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
  5503. {
  5504. Py_ssize_t size;
  5505. PyObject *ub;
  5506. int result = 0;
  5507. #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
  5508. if (!PyUnicode_FSDecoder(o, &ub))
  5509. return 0;
  5510. *out = PyUnicode_AsWideCharString(ub, &size);
  5511. if (*out)
  5512. result = 1;
  5513. #else
  5514. if (!PyUnicode_FSConverter(o, &ub))
  5515. return 0;
  5516. size = PyBytes_GET_SIZE(ub);
  5517. *out = PyMem_Malloc(size + 1);
  5518. if (*out) {
  5519. memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
  5520. result = 1;
  5521. } else
  5522. PyErr_NoMemory();
  5523. #endif
  5524. Py_DECREF(ub);
  5525. return result;
  5526. }
  5527. #endif
  5528. #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
  5529. static EXECV_CHAR**
  5530. parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
  5531. {
  5532. Py_ssize_t i, pos, envc;
  5533. PyObject *keys=NULL, *vals=NULL;
  5534. PyObject *key2, *val2, *keyval;
  5535. EXECV_CHAR **envlist;
  5536. i = PyMapping_Size(env);
  5537. if (i < 0)
  5538. return NULL;
  5539. envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
  5540. if (envlist == NULL) {
  5541. PyErr_NoMemory();
  5542. return NULL;
  5543. }
  5544. envc = 0;
  5545. keys = PyMapping_Keys(env);
  5546. if (!keys)
  5547. goto error;
  5548. vals = PyMapping_Values(env);
  5549. if (!vals)
  5550. goto error;
  5551. if (!PyList_Check(keys) || !PyList_Check(vals)) {
  5552. PyErr_Format(PyExc_TypeError,
  5553. "env.keys() or env.values() is not a list");
  5554. goto error;
  5555. }
  5556. for (pos = 0; pos < i; pos++) {
  5557. PyObject *key = PyList_GetItem(keys, pos); // Borrowed ref.
  5558. if (key == NULL) {
  5559. goto error;
  5560. }
  5561. PyObject *val = PyList_GetItem(vals, pos); // Borrowed ref.
  5562. if (val == NULL) {
  5563. goto error;
  5564. }
  5565. #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
  5566. if (!PyUnicode_FSDecoder(key, &key2))
  5567. goto error;
  5568. if (!PyUnicode_FSDecoder(val, &val2)) {
  5569. Py_DECREF(key2);
  5570. goto error;
  5571. }
  5572. /* Search from index 1 because on Windows starting '=' is allowed for
  5573. defining hidden environment variables. */
  5574. if (PyUnicode_GET_LENGTH(key2) == 0 ||
  5575. PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
  5576. {
  5577. PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
  5578. Py_DECREF(key2);
  5579. Py_DECREF(val2);
  5580. goto error;
  5581. }
  5582. keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
  5583. #else
  5584. if (!PyUnicode_FSConverter(key, &key2))
  5585. goto error;
  5586. if (!PyUnicode_FSConverter(val, &val2)) {
  5587. Py_DECREF(key2);
  5588. goto error;
  5589. }
  5590. if (PyBytes_GET_SIZE(key2) == 0 ||
  5591. strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
  5592. {
  5593. PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
  5594. Py_DECREF(key2);
  5595. Py_DECREF(val2);
  5596. goto error;
  5597. }
  5598. keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
  5599. PyBytes_AS_STRING(val2));
  5600. #endif
  5601. Py_DECREF(key2);
  5602. Py_DECREF(val2);
  5603. if (!keyval)
  5604. goto error;
  5605. if (!fsconvert_strdup(keyval, &envlist[envc++])) {
  5606. Py_DECREF(keyval);
  5607. goto error;
  5608. }
  5609. Py_DECREF(keyval);
  5610. }
  5611. Py_DECREF(vals);
  5612. Py_DECREF(keys);
  5613. envlist[envc] = 0;
  5614. *envc_ptr = envc;
  5615. return envlist;
  5616. error:
  5617. Py_XDECREF(keys);
  5618. Py_XDECREF(vals);
  5619. free_string_array(envlist, envc);
  5620. return NULL;
  5621. }
  5622. static EXECV_CHAR**
  5623. parse_arglist(PyObject* argv, Py_ssize_t *argc)
  5624. {
  5625. int i;
  5626. EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
  5627. if (argvlist == NULL) {
  5628. PyErr_NoMemory();
  5629. return NULL;
  5630. }
  5631. for (i = 0; i < *argc; i++) {
  5632. PyObject* item = PySequence_ITEM(argv, i);
  5633. if (item == NULL)
  5634. goto fail;
  5635. if (!fsconvert_strdup(item, &argvlist[i])) {
  5636. Py_DECREF(item);
  5637. goto fail;
  5638. }
  5639. Py_DECREF(item);
  5640. }
  5641. argvlist[*argc] = NULL;
  5642. return argvlist;
  5643. fail:
  5644. *argc = i;
  5645. free_string_array(argvlist, *argc);
  5646. return NULL;
  5647. }
  5648. #endif
  5649. #ifdef HAVE_EXECV
  5650. /*[clinic input]
  5651. os.execv
  5652. path: path_t
  5653. Path of executable file.
  5654. argv: object
  5655. Tuple or list of strings.
  5656. /
  5657. Execute an executable path with arguments, replacing current process.
  5658. [clinic start generated code]*/
  5659. static PyObject *
  5660. os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
  5661. /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
  5662. {
  5663. EXECV_CHAR **argvlist;
  5664. Py_ssize_t argc;
  5665. PyInterpreterState *interp = _PyInterpreterState_GET();
  5666. if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_EXEC)) {
  5667. PyErr_SetString(PyExc_RuntimeError,
  5668. "exec not supported for isolated subinterpreters");
  5669. return NULL;
  5670. }
  5671. /* execv has two arguments: (path, argv), where
  5672. argv is a list or tuple of strings. */
  5673. if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
  5674. PyErr_SetString(PyExc_TypeError,
  5675. "execv() arg 2 must be a tuple or list");
  5676. return NULL;
  5677. }
  5678. argc = PySequence_Size(argv);
  5679. if (argc < 1) {
  5680. PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
  5681. return NULL;
  5682. }
  5683. argvlist = parse_arglist(argv, &argc);
  5684. if (argvlist == NULL) {
  5685. return NULL;
  5686. }
  5687. if (!argvlist[0][0]) {
  5688. PyErr_SetString(PyExc_ValueError,
  5689. "execv() arg 2 first element cannot be empty");
  5690. free_string_array(argvlist, argc);
  5691. return NULL;
  5692. }
  5693. if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
  5694. free_string_array(argvlist, argc);
  5695. return NULL;
  5696. }
  5697. _Py_BEGIN_SUPPRESS_IPH
  5698. #ifdef HAVE_WEXECV
  5699. _wexecv(path->wide, argvlist);
  5700. #else
  5701. execv(path->narrow, argvlist);
  5702. #endif
  5703. _Py_END_SUPPRESS_IPH
  5704. /* If we get here it's definitely an error */
  5705. posix_error();
  5706. free_string_array(argvlist, argc);
  5707. return NULL;
  5708. }
  5709. /*[clinic input]
  5710. os.execve
  5711. path: path_t(allow_fd='PATH_HAVE_FEXECVE')
  5712. Path of executable file.
  5713. argv: object
  5714. Tuple or list of strings.
  5715. env: object
  5716. Dictionary of strings mapping to strings.
  5717. Execute an executable path with arguments, replacing current process.
  5718. [clinic start generated code]*/
  5719. static PyObject *
  5720. os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
  5721. /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
  5722. {
  5723. EXECV_CHAR **argvlist = NULL;
  5724. EXECV_CHAR **envlist;
  5725. Py_ssize_t argc, envc;
  5726. PyInterpreterState *interp = _PyInterpreterState_GET();
  5727. if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_EXEC)) {
  5728. PyErr_SetString(PyExc_RuntimeError,
  5729. "exec not supported for isolated subinterpreters");
  5730. return NULL;
  5731. }
  5732. /* execve has three arguments: (path, argv, env), where
  5733. argv is a list or tuple of strings and env is a dictionary
  5734. like posix.environ. */
  5735. if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
  5736. PyErr_SetString(PyExc_TypeError,
  5737. "execve: argv must be a tuple or list");
  5738. goto fail_0;
  5739. }
  5740. argc = PySequence_Size(argv);
  5741. if (argc < 1) {
  5742. PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
  5743. return NULL;
  5744. }
  5745. if (!PyMapping_Check(env)) {
  5746. PyErr_SetString(PyExc_TypeError,
  5747. "execve: environment must be a mapping object");
  5748. goto fail_0;
  5749. }
  5750. argvlist = parse_arglist(argv, &argc);
  5751. if (argvlist == NULL) {
  5752. goto fail_0;
  5753. }
  5754. if (!argvlist[0][0]) {
  5755. PyErr_SetString(PyExc_ValueError,
  5756. "execve: argv first element cannot be empty");
  5757. goto fail_0;
  5758. }
  5759. envlist = parse_envlist(env, &envc);
  5760. if (envlist == NULL)
  5761. goto fail_0;
  5762. if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
  5763. goto fail_1;
  5764. }
  5765. _Py_BEGIN_SUPPRESS_IPH
  5766. #ifdef HAVE_FEXECVE
  5767. if (path->fd > -1)
  5768. fexecve(path->fd, argvlist, envlist);
  5769. else
  5770. #endif
  5771. #ifdef HAVE_WEXECV
  5772. _wexecve(path->wide, argvlist, envlist);
  5773. #else
  5774. execve(path->narrow, argvlist, envlist);
  5775. #endif
  5776. _Py_END_SUPPRESS_IPH
  5777. /* If we get here it's definitely an error */
  5778. posix_path_error(path);
  5779. fail_1:
  5780. free_string_array(envlist, envc);
  5781. fail_0:
  5782. if (argvlist)
  5783. free_string_array(argvlist, argc);
  5784. return NULL;
  5785. }
  5786. #endif /* HAVE_EXECV */
  5787. #ifdef HAVE_POSIX_SPAWN
  5788. enum posix_spawn_file_actions_identifier {
  5789. POSIX_SPAWN_OPEN,
  5790. POSIX_SPAWN_CLOSE,
  5791. POSIX_SPAWN_DUP2
  5792. };
  5793. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  5794. static int
  5795. convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res);
  5796. #endif
  5797. static int
  5798. parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup,
  5799. int resetids, int setsid, PyObject *setsigmask,
  5800. PyObject *setsigdef, PyObject *scheduler,
  5801. posix_spawnattr_t *attrp)
  5802. {
  5803. long all_flags = 0;
  5804. errno = posix_spawnattr_init(attrp);
  5805. if (errno) {
  5806. posix_error();
  5807. return -1;
  5808. }
  5809. if (setpgroup) {
  5810. pid_t pgid = PyLong_AsPid(setpgroup);
  5811. if (pgid == (pid_t)-1 && PyErr_Occurred()) {
  5812. goto fail;
  5813. }
  5814. errno = posix_spawnattr_setpgroup(attrp, pgid);
  5815. if (errno) {
  5816. posix_error();
  5817. goto fail;
  5818. }
  5819. all_flags |= POSIX_SPAWN_SETPGROUP;
  5820. }
  5821. if (resetids) {
  5822. all_flags |= POSIX_SPAWN_RESETIDS;
  5823. }
  5824. if (setsid) {
  5825. #ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
  5826. if (HAVE_POSIX_SPAWN_SETSID_RUNTIME) {
  5827. #endif
  5828. #ifdef POSIX_SPAWN_SETSID
  5829. all_flags |= POSIX_SPAWN_SETSID;
  5830. #elif defined(POSIX_SPAWN_SETSID_NP)
  5831. all_flags |= POSIX_SPAWN_SETSID_NP;
  5832. #else
  5833. argument_unavailable_error(func_name, "setsid");
  5834. return -1;
  5835. #endif
  5836. #ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
  5837. } else {
  5838. argument_unavailable_error(func_name, "setsid");
  5839. return -1;
  5840. }
  5841. #endif /* HAVE_POSIX_SPAWN_SETSID_RUNTIME */
  5842. }
  5843. #ifdef HAVE_SIGSET_T
  5844. if (setsigmask) {
  5845. sigset_t set;
  5846. if (!_Py_Sigset_Converter(setsigmask, &set)) {
  5847. goto fail;
  5848. }
  5849. errno = posix_spawnattr_setsigmask(attrp, &set);
  5850. if (errno) {
  5851. posix_error();
  5852. goto fail;
  5853. }
  5854. all_flags |= POSIX_SPAWN_SETSIGMASK;
  5855. }
  5856. if (setsigdef) {
  5857. sigset_t set;
  5858. if (!_Py_Sigset_Converter(setsigdef, &set)) {
  5859. goto fail;
  5860. }
  5861. errno = posix_spawnattr_setsigdefault(attrp, &set);
  5862. if (errno) {
  5863. posix_error();
  5864. goto fail;
  5865. }
  5866. all_flags |= POSIX_SPAWN_SETSIGDEF;
  5867. }
  5868. #else
  5869. if (setsigmask || setsigdef) {
  5870. PyErr_SetString(PyExc_NotImplementedError,
  5871. "sigset is not supported on this platform");
  5872. goto fail;
  5873. }
  5874. #endif
  5875. if (scheduler) {
  5876. #ifdef POSIX_SPAWN_SETSCHEDULER
  5877. PyObject *py_schedpolicy;
  5878. PyObject *schedparam_obj;
  5879. struct sched_param schedparam;
  5880. if (!PyArg_ParseTuple(scheduler, "OO"
  5881. ";A scheduler tuple must have two elements",
  5882. &py_schedpolicy, &schedparam_obj)) {
  5883. goto fail;
  5884. }
  5885. if (!convert_sched_param(module, schedparam_obj, &schedparam)) {
  5886. goto fail;
  5887. }
  5888. if (py_schedpolicy != Py_None) {
  5889. int schedpolicy = _PyLong_AsInt(py_schedpolicy);
  5890. if (schedpolicy == -1 && PyErr_Occurred()) {
  5891. goto fail;
  5892. }
  5893. errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
  5894. if (errno) {
  5895. posix_error();
  5896. goto fail;
  5897. }
  5898. all_flags |= POSIX_SPAWN_SETSCHEDULER;
  5899. }
  5900. errno = posix_spawnattr_setschedparam(attrp, &schedparam);
  5901. if (errno) {
  5902. posix_error();
  5903. goto fail;
  5904. }
  5905. all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
  5906. #else
  5907. PyErr_SetString(PyExc_NotImplementedError,
  5908. "The scheduler option is not supported in this system.");
  5909. goto fail;
  5910. #endif
  5911. }
  5912. errno = posix_spawnattr_setflags(attrp, all_flags);
  5913. if (errno) {
  5914. posix_error();
  5915. goto fail;
  5916. }
  5917. return 0;
  5918. fail:
  5919. (void)posix_spawnattr_destroy(attrp);
  5920. return -1;
  5921. }
  5922. static int
  5923. parse_file_actions(PyObject *file_actions,
  5924. posix_spawn_file_actions_t *file_actionsp,
  5925. PyObject *temp_buffer)
  5926. {
  5927. PyObject *seq;
  5928. PyObject *file_action = NULL;
  5929. PyObject *tag_obj;
  5930. seq = PySequence_Fast(file_actions,
  5931. "file_actions must be a sequence or None");
  5932. if (seq == NULL) {
  5933. return -1;
  5934. }
  5935. errno = posix_spawn_file_actions_init(file_actionsp);
  5936. if (errno) {
  5937. posix_error();
  5938. Py_DECREF(seq);
  5939. return -1;
  5940. }
  5941. for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
  5942. file_action = PySequence_Fast_GET_ITEM(seq, i);
  5943. Py_INCREF(file_action);
  5944. if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
  5945. PyErr_SetString(PyExc_TypeError,
  5946. "Each file_actions element must be a non-empty tuple");
  5947. goto fail;
  5948. }
  5949. long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
  5950. if (tag == -1 && PyErr_Occurred()) {
  5951. goto fail;
  5952. }
  5953. /* Populate the file_actions object */
  5954. switch (tag) {
  5955. case POSIX_SPAWN_OPEN: {
  5956. int fd, oflag;
  5957. PyObject *path;
  5958. unsigned long mode;
  5959. if (!PyArg_ParseTuple(file_action, "OiO&ik"
  5960. ";A open file_action tuple must have 5 elements",
  5961. &tag_obj, &fd, PyUnicode_FSConverter, &path,
  5962. &oflag, &mode))
  5963. {
  5964. goto fail;
  5965. }
  5966. if (PyList_Append(temp_buffer, path)) {
  5967. Py_DECREF(path);
  5968. goto fail;
  5969. }
  5970. errno = posix_spawn_file_actions_addopen(file_actionsp,
  5971. fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
  5972. if (errno) {
  5973. posix_error();
  5974. Py_DECREF(path);
  5975. goto fail;
  5976. }
  5977. Py_DECREF(path);
  5978. break;
  5979. }
  5980. case POSIX_SPAWN_CLOSE: {
  5981. int fd;
  5982. if (!PyArg_ParseTuple(file_action, "Oi"
  5983. ";A close file_action tuple must have 2 elements",
  5984. &tag_obj, &fd))
  5985. {
  5986. goto fail;
  5987. }
  5988. errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
  5989. if (errno) {
  5990. posix_error();
  5991. goto fail;
  5992. }
  5993. break;
  5994. }
  5995. case POSIX_SPAWN_DUP2: {
  5996. int fd1, fd2;
  5997. if (!PyArg_ParseTuple(file_action, "Oii"
  5998. ";A dup2 file_action tuple must have 3 elements",
  5999. &tag_obj, &fd1, &fd2))
  6000. {
  6001. goto fail;
  6002. }
  6003. errno = posix_spawn_file_actions_adddup2(file_actionsp,
  6004. fd1, fd2);
  6005. if (errno) {
  6006. posix_error();
  6007. goto fail;
  6008. }
  6009. break;
  6010. }
  6011. default: {
  6012. PyErr_SetString(PyExc_TypeError,
  6013. "Unknown file_actions identifier");
  6014. goto fail;
  6015. }
  6016. }
  6017. Py_DECREF(file_action);
  6018. }
  6019. Py_DECREF(seq);
  6020. return 0;
  6021. fail:
  6022. Py_DECREF(seq);
  6023. Py_DECREF(file_action);
  6024. (void)posix_spawn_file_actions_destroy(file_actionsp);
  6025. return -1;
  6026. }
  6027. static PyObject *
  6028. py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
  6029. PyObject *env, PyObject *file_actions,
  6030. PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
  6031. PyObject *setsigdef, PyObject *scheduler)
  6032. {
  6033. const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
  6034. EXECV_CHAR **argvlist = NULL;
  6035. EXECV_CHAR **envlist = NULL;
  6036. posix_spawn_file_actions_t file_actions_buf;
  6037. posix_spawn_file_actions_t *file_actionsp = NULL;
  6038. posix_spawnattr_t attr;
  6039. posix_spawnattr_t *attrp = NULL;
  6040. Py_ssize_t argc, envc;
  6041. PyObject *result = NULL;
  6042. PyObject *temp_buffer = NULL;
  6043. pid_t pid;
  6044. int err_code;
  6045. /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
  6046. argv is a list or tuple of strings and env is a dictionary
  6047. like posix.environ. */
  6048. if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
  6049. PyErr_Format(PyExc_TypeError,
  6050. "%s: argv must be a tuple or list", func_name);
  6051. goto exit;
  6052. }
  6053. argc = PySequence_Size(argv);
  6054. if (argc < 1) {
  6055. PyErr_Format(PyExc_ValueError,
  6056. "%s: argv must not be empty", func_name);
  6057. return NULL;
  6058. }
  6059. if (!PyMapping_Check(env)) {
  6060. PyErr_Format(PyExc_TypeError,
  6061. "%s: environment must be a mapping object", func_name);
  6062. goto exit;
  6063. }
  6064. argvlist = parse_arglist(argv, &argc);
  6065. if (argvlist == NULL) {
  6066. goto exit;
  6067. }
  6068. if (!argvlist[0][0]) {
  6069. PyErr_Format(PyExc_ValueError,
  6070. "%s: argv first element cannot be empty", func_name);
  6071. goto exit;
  6072. }
  6073. envlist = parse_envlist(env, &envc);
  6074. if (envlist == NULL) {
  6075. goto exit;
  6076. }
  6077. if (file_actions != NULL && file_actions != Py_None) {
  6078. /* There is a bug in old versions of glibc that makes some of the
  6079. * helper functions for manipulating file actions not copy the provided
  6080. * buffers. The problem is that posix_spawn_file_actions_addopen does not
  6081. * copy the value of path for some old versions of glibc (<2.20).
  6082. * The use of temp_buffer here is a workaround that keeps the
  6083. * python objects that own the buffers alive until posix_spawn gets called.
  6084. * Check https://bugs.python.org/issue33630 and
  6085. * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
  6086. temp_buffer = PyList_New(0);
  6087. if (!temp_buffer) {
  6088. goto exit;
  6089. }
  6090. if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
  6091. goto exit;
  6092. }
  6093. file_actionsp = &file_actions_buf;
  6094. }
  6095. if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid,
  6096. setsigmask, setsigdef, scheduler, &attr)) {
  6097. goto exit;
  6098. }
  6099. attrp = &attr;
  6100. if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
  6101. goto exit;
  6102. }
  6103. _Py_BEGIN_SUPPRESS_IPH
  6104. #ifdef HAVE_POSIX_SPAWNP
  6105. if (use_posix_spawnp) {
  6106. err_code = posix_spawnp(&pid, path->narrow,
  6107. file_actionsp, attrp, argvlist, envlist);
  6108. }
  6109. else
  6110. #endif /* HAVE_POSIX_SPAWNP */
  6111. {
  6112. err_code = posix_spawn(&pid, path->narrow,
  6113. file_actionsp, attrp, argvlist, envlist);
  6114. }
  6115. _Py_END_SUPPRESS_IPH
  6116. if (err_code) {
  6117. errno = err_code;
  6118. PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
  6119. goto exit;
  6120. }
  6121. #ifdef _Py_MEMORY_SANITIZER
  6122. __msan_unpoison(&pid, sizeof(pid));
  6123. #endif
  6124. result = PyLong_FromPid(pid);
  6125. exit:
  6126. if (file_actionsp) {
  6127. (void)posix_spawn_file_actions_destroy(file_actionsp);
  6128. }
  6129. if (attrp) {
  6130. (void)posix_spawnattr_destroy(attrp);
  6131. }
  6132. if (envlist) {
  6133. free_string_array(envlist, envc);
  6134. }
  6135. if (argvlist) {
  6136. free_string_array(argvlist, argc);
  6137. }
  6138. Py_XDECREF(temp_buffer);
  6139. return result;
  6140. }
  6141. /*[clinic input]
  6142. os.posix_spawn
  6143. path: path_t
  6144. Path of executable file.
  6145. argv: object
  6146. Tuple or list of strings.
  6147. env: object
  6148. Dictionary of strings mapping to strings.
  6149. /
  6150. *
  6151. file_actions: object(c_default='NULL') = ()
  6152. A sequence of file action tuples.
  6153. setpgroup: object = NULL
  6154. The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
  6155. resetids: bool = False
  6156. If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
  6157. setsid: bool = False
  6158. If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
  6159. setsigmask: object(c_default='NULL') = ()
  6160. The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
  6161. setsigdef: object(c_default='NULL') = ()
  6162. The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
  6163. scheduler: object = NULL
  6164. A tuple with the scheduler policy (optional) and parameters.
  6165. Execute the program specified by path in a new process.
  6166. [clinic start generated code]*/
  6167. static PyObject *
  6168. os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
  6169. PyObject *env, PyObject *file_actions,
  6170. PyObject *setpgroup, int resetids, int setsid,
  6171. PyObject *setsigmask, PyObject *setsigdef,
  6172. PyObject *scheduler)
  6173. /*[clinic end generated code: output=14a1098c566bc675 input=808aed1090d84e33]*/
  6174. {
  6175. return py_posix_spawn(0, module, path, argv, env, file_actions,
  6176. setpgroup, resetids, setsid, setsigmask, setsigdef,
  6177. scheduler);
  6178. }
  6179. #endif /* HAVE_POSIX_SPAWN */
  6180. #ifdef HAVE_POSIX_SPAWNP
  6181. /*[clinic input]
  6182. os.posix_spawnp
  6183. path: path_t
  6184. Path of executable file.
  6185. argv: object
  6186. Tuple or list of strings.
  6187. env: object
  6188. Dictionary of strings mapping to strings.
  6189. /
  6190. *
  6191. file_actions: object(c_default='NULL') = ()
  6192. A sequence of file action tuples.
  6193. setpgroup: object = NULL
  6194. The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
  6195. resetids: bool = False
  6196. If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
  6197. setsid: bool = False
  6198. If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
  6199. setsigmask: object(c_default='NULL') = ()
  6200. The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
  6201. setsigdef: object(c_default='NULL') = ()
  6202. The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
  6203. scheduler: object = NULL
  6204. A tuple with the scheduler policy (optional) and parameters.
  6205. Execute the program specified by path in a new process.
  6206. [clinic start generated code]*/
  6207. static PyObject *
  6208. os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
  6209. PyObject *env, PyObject *file_actions,
  6210. PyObject *setpgroup, int resetids, int setsid,
  6211. PyObject *setsigmask, PyObject *setsigdef,
  6212. PyObject *scheduler)
  6213. /*[clinic end generated code: output=7b9aaefe3031238d input=9e89e616116752a1]*/
  6214. {
  6215. return py_posix_spawn(1, module, path, argv, env, file_actions,
  6216. setpgroup, resetids, setsid, setsigmask, setsigdef,
  6217. scheduler);
  6218. }
  6219. #endif /* HAVE_POSIX_SPAWNP */
  6220. #ifdef HAVE_RTPSPAWN
  6221. static intptr_t
  6222. _rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
  6223. const char *envp[])
  6224. {
  6225. RTP_ID rtpid;
  6226. int status;
  6227. pid_t res;
  6228. int async_err = 0;
  6229. /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
  6230. uStackSize=0 cannot be used, the default stack size is too small for
  6231. Python. */
  6232. if (envp) {
  6233. rtpid = rtpSpawn(rtpFileName, argv, envp,
  6234. 100, 0x1000000, 0, VX_FP_TASK);
  6235. }
  6236. else {
  6237. rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
  6238. 100, 0x1000000, 0, VX_FP_TASK);
  6239. }
  6240. if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
  6241. do {
  6242. res = waitpid((pid_t)rtpid, &status, 0);
  6243. } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  6244. if (res < 0)
  6245. return RTP_ID_ERROR;
  6246. return ((intptr_t)status);
  6247. }
  6248. return ((intptr_t)rtpid);
  6249. }
  6250. #endif
  6251. #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
  6252. /*[clinic input]
  6253. os.spawnv
  6254. mode: int
  6255. Mode of process creation.
  6256. path: path_t
  6257. Path of executable file.
  6258. argv: object
  6259. Tuple or list of strings.
  6260. /
  6261. Execute the program specified by path in a new process.
  6262. [clinic start generated code]*/
  6263. static PyObject *
  6264. os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
  6265. /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
  6266. {
  6267. EXECV_CHAR **argvlist;
  6268. int i;
  6269. Py_ssize_t argc;
  6270. intptr_t spawnval;
  6271. PyObject *(*getitem)(PyObject *, Py_ssize_t);
  6272. /* spawnv has three arguments: (mode, path, argv), where
  6273. argv is a list or tuple of strings. */
  6274. if (PyList_Check(argv)) {
  6275. argc = PyList_Size(argv);
  6276. getitem = PyList_GetItem;
  6277. }
  6278. else if (PyTuple_Check(argv)) {
  6279. argc = PyTuple_Size(argv);
  6280. getitem = PyTuple_GetItem;
  6281. }
  6282. else {
  6283. PyErr_SetString(PyExc_TypeError,
  6284. "spawnv() arg 2 must be a tuple or list");
  6285. return NULL;
  6286. }
  6287. if (argc == 0) {
  6288. PyErr_SetString(PyExc_ValueError,
  6289. "spawnv() arg 2 cannot be empty");
  6290. return NULL;
  6291. }
  6292. argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
  6293. if (argvlist == NULL) {
  6294. return PyErr_NoMemory();
  6295. }
  6296. for (i = 0; i < argc; i++) {
  6297. if (!fsconvert_strdup((*getitem)(argv, i),
  6298. &argvlist[i])) {
  6299. free_string_array(argvlist, i);
  6300. PyErr_SetString(
  6301. PyExc_TypeError,
  6302. "spawnv() arg 2 must contain only strings");
  6303. return NULL;
  6304. }
  6305. if (i == 0 && !argvlist[0][0]) {
  6306. free_string_array(argvlist, i + 1);
  6307. PyErr_SetString(
  6308. PyExc_ValueError,
  6309. "spawnv() arg 2 first element cannot be empty");
  6310. return NULL;
  6311. }
  6312. }
  6313. argvlist[argc] = NULL;
  6314. #if !defined(HAVE_RTPSPAWN)
  6315. if (mode == _OLD_P_OVERLAY)
  6316. mode = _P_OVERLAY;
  6317. #endif
  6318. if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
  6319. Py_None) < 0) {
  6320. free_string_array(argvlist, argc);
  6321. return NULL;
  6322. }
  6323. Py_BEGIN_ALLOW_THREADS
  6324. _Py_BEGIN_SUPPRESS_IPH
  6325. #ifdef HAVE_WSPAWNV
  6326. spawnval = _wspawnv(mode, path->wide, argvlist);
  6327. #elif defined(HAVE_RTPSPAWN)
  6328. spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
  6329. #else
  6330. spawnval = _spawnv(mode, path->narrow, argvlist);
  6331. #endif
  6332. _Py_END_SUPPRESS_IPH
  6333. Py_END_ALLOW_THREADS
  6334. int saved_errno = errno;
  6335. free_string_array(argvlist, argc);
  6336. if (spawnval == -1) {
  6337. errno = saved_errno;
  6338. posix_error();
  6339. return NULL;
  6340. }
  6341. return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
  6342. }
  6343. /*[clinic input]
  6344. os.spawnve
  6345. mode: int
  6346. Mode of process creation.
  6347. path: path_t
  6348. Path of executable file.
  6349. argv: object
  6350. Tuple or list of strings.
  6351. env: object
  6352. Dictionary of strings mapping to strings.
  6353. /
  6354. Execute the program specified by path in a new process.
  6355. [clinic start generated code]*/
  6356. static PyObject *
  6357. os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
  6358. PyObject *env)
  6359. /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
  6360. {
  6361. EXECV_CHAR **argvlist;
  6362. EXECV_CHAR **envlist;
  6363. PyObject *res = NULL;
  6364. Py_ssize_t argc, i, envc;
  6365. intptr_t spawnval;
  6366. PyObject *(*getitem)(PyObject *, Py_ssize_t);
  6367. Py_ssize_t lastarg = 0;
  6368. /* spawnve has four arguments: (mode, path, argv, env), where
  6369. argv is a list or tuple of strings and env is a dictionary
  6370. like posix.environ. */
  6371. if (PyList_Check(argv)) {
  6372. argc = PyList_Size(argv);
  6373. getitem = PyList_GetItem;
  6374. }
  6375. else if (PyTuple_Check(argv)) {
  6376. argc = PyTuple_Size(argv);
  6377. getitem = PyTuple_GetItem;
  6378. }
  6379. else {
  6380. PyErr_SetString(PyExc_TypeError,
  6381. "spawnve() arg 2 must be a tuple or list");
  6382. goto fail_0;
  6383. }
  6384. if (argc == 0) {
  6385. PyErr_SetString(PyExc_ValueError,
  6386. "spawnve() arg 2 cannot be empty");
  6387. goto fail_0;
  6388. }
  6389. if (!PyMapping_Check(env)) {
  6390. PyErr_SetString(PyExc_TypeError,
  6391. "spawnve() arg 3 must be a mapping object");
  6392. goto fail_0;
  6393. }
  6394. argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
  6395. if (argvlist == NULL) {
  6396. PyErr_NoMemory();
  6397. goto fail_0;
  6398. }
  6399. for (i = 0; i < argc; i++) {
  6400. if (!fsconvert_strdup((*getitem)(argv, i),
  6401. &argvlist[i]))
  6402. {
  6403. lastarg = i;
  6404. goto fail_1;
  6405. }
  6406. if (i == 0 && !argvlist[0][0]) {
  6407. lastarg = i + 1;
  6408. PyErr_SetString(
  6409. PyExc_ValueError,
  6410. "spawnv() arg 2 first element cannot be empty");
  6411. goto fail_1;
  6412. }
  6413. }
  6414. lastarg = argc;
  6415. argvlist[argc] = NULL;
  6416. envlist = parse_envlist(env, &envc);
  6417. if (envlist == NULL)
  6418. goto fail_1;
  6419. #if !defined(HAVE_RTPSPAWN)
  6420. if (mode == _OLD_P_OVERLAY)
  6421. mode = _P_OVERLAY;
  6422. #endif
  6423. if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
  6424. goto fail_2;
  6425. }
  6426. Py_BEGIN_ALLOW_THREADS
  6427. _Py_BEGIN_SUPPRESS_IPH
  6428. #ifdef HAVE_WSPAWNV
  6429. spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
  6430. #elif defined(HAVE_RTPSPAWN)
  6431. spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
  6432. (const char **)envlist);
  6433. #else
  6434. spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
  6435. #endif
  6436. _Py_END_SUPPRESS_IPH
  6437. Py_END_ALLOW_THREADS
  6438. if (spawnval == -1)
  6439. (void) posix_error();
  6440. else
  6441. res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
  6442. fail_2:
  6443. while (--envc >= 0) {
  6444. PyMem_Free(envlist[envc]);
  6445. }
  6446. PyMem_Free(envlist);
  6447. fail_1:
  6448. free_string_array(argvlist, lastarg);
  6449. fail_0:
  6450. return res;
  6451. }
  6452. #endif /* HAVE_SPAWNV */
  6453. #ifdef HAVE_FORK
  6454. /* Helper function to validate arguments.
  6455. Returns 0 on success. non-zero on failure with a TypeError raised.
  6456. If obj is non-NULL it must be callable. */
  6457. static int
  6458. check_null_or_callable(PyObject *obj, const char* obj_name)
  6459. {
  6460. if (obj && !PyCallable_Check(obj)) {
  6461. PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
  6462. obj_name, _PyType_Name(Py_TYPE(obj)));
  6463. return -1;
  6464. }
  6465. return 0;
  6466. }
  6467. /*[clinic input]
  6468. os.register_at_fork
  6469. *
  6470. before: object=NULL
  6471. A callable to be called in the parent before the fork() syscall.
  6472. after_in_child: object=NULL
  6473. A callable to be called in the child after fork().
  6474. after_in_parent: object=NULL
  6475. A callable to be called in the parent after fork().
  6476. Register callables to be called when forking a new process.
  6477. 'before' callbacks are called in reverse order.
  6478. 'after_in_child' and 'after_in_parent' callbacks are called in order.
  6479. [clinic start generated code]*/
  6480. static PyObject *
  6481. os_register_at_fork_impl(PyObject *module, PyObject *before,
  6482. PyObject *after_in_child, PyObject *after_in_parent)
  6483. /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
  6484. {
  6485. PyInterpreterState *interp;
  6486. if (!before && !after_in_child && !after_in_parent) {
  6487. PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
  6488. return NULL;
  6489. }
  6490. if (check_null_or_callable(before, "before") ||
  6491. check_null_or_callable(after_in_child, "after_in_child") ||
  6492. check_null_or_callable(after_in_parent, "after_in_parent")) {
  6493. return NULL;
  6494. }
  6495. interp = _PyInterpreterState_GET();
  6496. if (register_at_forker(&interp->before_forkers, before)) {
  6497. return NULL;
  6498. }
  6499. if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
  6500. return NULL;
  6501. }
  6502. if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
  6503. return NULL;
  6504. }
  6505. Py_RETURN_NONE;
  6506. }
  6507. #endif /* HAVE_FORK */
  6508. #if defined(HAVE_FORK1) || defined(HAVE_FORKPTY) || defined(HAVE_FORK)
  6509. // Common code to raise a warning if we detect there is more than one thread
  6510. // running in the process. Best effort, silent if unable to count threads.
  6511. // Constraint: Quick. Never overcounts. Never leaves an error set.
  6512. //
  6513. // This code might do an import, thus acquiring the import lock, which
  6514. // PyOS_BeforeFork() also does. As this should only be called from
  6515. // the parent process, it is in the same thread so that works.
  6516. static void warn_about_fork_with_threads(const char* name) {
  6517. // TODO: Consider making an `os` module API to return the current number
  6518. // of threads in the process. That'd presumably use this platform code but
  6519. // raise an error rather than using the inaccurate fallback.
  6520. Py_ssize_t num_python_threads = 0;
  6521. #if defined(__APPLE__) && defined(HAVE_GETPID)
  6522. mach_port_t macos_self = mach_task_self();
  6523. mach_port_t macos_task;
  6524. if (task_for_pid(macos_self, getpid(), &macos_task) == KERN_SUCCESS) {
  6525. thread_array_t macos_threads;
  6526. mach_msg_type_number_t macos_n_threads;
  6527. if (task_threads(macos_task, &macos_threads,
  6528. &macos_n_threads) == KERN_SUCCESS) {
  6529. num_python_threads = macos_n_threads;
  6530. }
  6531. }
  6532. #elif defined(__linux__)
  6533. // Linux /proc/self/stat 20th field is the number of threads.
  6534. FILE* proc_stat = fopen("/proc/self/stat", "r");
  6535. if (proc_stat) {
  6536. size_t n;
  6537. // Size chosen arbitrarily. ~60% more bytes than a 20th column index
  6538. // observed on the author's workstation.
  6539. char stat_line[160];
  6540. n = fread(&stat_line, 1, 159, proc_stat);
  6541. stat_line[n] = '\0';
  6542. fclose(proc_stat);
  6543. char *saveptr = NULL;
  6544. char *field = strtok_r(stat_line, " ", &saveptr);
  6545. unsigned int idx;
  6546. for (idx = 19; idx && field; --idx) {
  6547. field = strtok_r(NULL, " ", &saveptr);
  6548. }
  6549. if (idx == 0 && field) { // found the 20th field
  6550. num_python_threads = atoi(field); // 0 on error
  6551. }
  6552. }
  6553. #endif
  6554. if (num_python_threads <= 0) {
  6555. // Fall back to just the number our threading module knows about.
  6556. // An incomplete view of the world, but better than nothing.
  6557. PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
  6558. if (!threading) {
  6559. PyErr_Clear();
  6560. return;
  6561. }
  6562. PyObject *threading_active =
  6563. PyObject_GetAttr(threading, &_Py_ID(_active));
  6564. if (!threading_active) {
  6565. PyErr_Clear();
  6566. Py_DECREF(threading);
  6567. return;
  6568. }
  6569. PyObject *threading_limbo =
  6570. PyObject_GetAttr(threading, &_Py_ID(_limbo));
  6571. if (!threading_limbo) {
  6572. PyErr_Clear();
  6573. Py_DECREF(threading);
  6574. Py_DECREF(threading_active);
  6575. return;
  6576. }
  6577. Py_DECREF(threading);
  6578. // Duplicating what threading.active_count() does but without holding
  6579. // threading._active_limbo_lock so our count could be inaccurate if
  6580. // these dicts are mid-update from another thread. Not a big deal.
  6581. // Worst case if someone replaced threading._active or threading._limbo
  6582. // with non-dicts, we get -1 from *Length() below and undercount.
  6583. // Nobody should, but we're best effort so we clear errors and move on.
  6584. num_python_threads = (PyMapping_Length(threading_active)
  6585. + PyMapping_Length(threading_limbo));
  6586. PyErr_Clear();
  6587. Py_DECREF(threading_active);
  6588. Py_DECREF(threading_limbo);
  6589. }
  6590. if (num_python_threads > 1) {
  6591. PyErr_WarnFormat(
  6592. PyExc_DeprecationWarning, 1,
  6593. #ifdef HAVE_GETPID
  6594. "This process (pid=%d) is multi-threaded, "
  6595. #else
  6596. "This process is multi-threaded, "
  6597. #endif
  6598. "use of %s() may lead to deadlocks in the child.",
  6599. #ifdef HAVE_GETPID
  6600. getpid(),
  6601. #endif
  6602. name);
  6603. PyErr_Clear();
  6604. }
  6605. }
  6606. #endif // HAVE_FORK1 || HAVE_FORKPTY || HAVE_FORK
  6607. #ifdef HAVE_FORK1
  6608. /*[clinic input]
  6609. os.fork1
  6610. Fork a child process with a single multiplexed (i.e., not bound) thread.
  6611. Return 0 to child process and PID of child to parent process.
  6612. [clinic start generated code]*/
  6613. static PyObject *
  6614. os_fork1_impl(PyObject *module)
  6615. /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
  6616. {
  6617. pid_t pid;
  6618. PyInterpreterState *interp = _PyInterpreterState_GET();
  6619. if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
  6620. PyErr_SetString(PyExc_RuntimeError,
  6621. "can't fork at interpreter shutdown");
  6622. return NULL;
  6623. }
  6624. if (!_Py_IsMainInterpreter(interp)) {
  6625. PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
  6626. return NULL;
  6627. }
  6628. PyOS_BeforeFork();
  6629. pid = fork1();
  6630. int saved_errno = errno;
  6631. if (pid == 0) {
  6632. /* child: this clobbers and resets the import lock. */
  6633. PyOS_AfterFork_Child();
  6634. } else {
  6635. warn_about_fork_with_threads("fork1");
  6636. /* parent: release the import lock. */
  6637. PyOS_AfterFork_Parent();
  6638. }
  6639. if (pid == -1) {
  6640. errno = saved_errno;
  6641. return posix_error();
  6642. }
  6643. return PyLong_FromPid(pid);
  6644. }
  6645. #endif /* HAVE_FORK1 */
  6646. #ifdef HAVE_FORK
  6647. /*[clinic input]
  6648. os.fork
  6649. Fork a child process.
  6650. Return 0 to child process and PID of child to parent process.
  6651. [clinic start generated code]*/
  6652. static PyObject *
  6653. os_fork_impl(PyObject *module)
  6654. /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
  6655. {
  6656. pid_t pid;
  6657. PyInterpreterState *interp = _PyInterpreterState_GET();
  6658. if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
  6659. PyErr_SetString(PyExc_RuntimeError,
  6660. "can't fork at interpreter shutdown");
  6661. return NULL;
  6662. }
  6663. if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_FORK)) {
  6664. PyErr_SetString(PyExc_RuntimeError,
  6665. "fork not supported for isolated subinterpreters");
  6666. return NULL;
  6667. }
  6668. if (PySys_Audit("os.fork", NULL) < 0) {
  6669. return NULL;
  6670. }
  6671. PyOS_BeforeFork();
  6672. pid = fork();
  6673. int saved_errno = errno;
  6674. if (pid == 0) {
  6675. /* child: this clobbers and resets the import lock. */
  6676. PyOS_AfterFork_Child();
  6677. } else {
  6678. warn_about_fork_with_threads("fork");
  6679. /* parent: release the import lock. */
  6680. PyOS_AfterFork_Parent();
  6681. }
  6682. if (pid == -1) {
  6683. errno = saved_errno;
  6684. return posix_error();
  6685. }
  6686. return PyLong_FromPid(pid);
  6687. }
  6688. #endif /* HAVE_FORK */
  6689. #ifdef HAVE_SCHED_H
  6690. #ifdef HAVE_SCHED_GET_PRIORITY_MAX
  6691. /*[clinic input]
  6692. os.sched_get_priority_max
  6693. policy: int
  6694. Get the maximum scheduling priority for policy.
  6695. [clinic start generated code]*/
  6696. static PyObject *
  6697. os_sched_get_priority_max_impl(PyObject *module, int policy)
  6698. /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
  6699. {
  6700. int max;
  6701. max = sched_get_priority_max(policy);
  6702. if (max < 0)
  6703. return posix_error();
  6704. return PyLong_FromLong(max);
  6705. }
  6706. /*[clinic input]
  6707. os.sched_get_priority_min
  6708. policy: int
  6709. Get the minimum scheduling priority for policy.
  6710. [clinic start generated code]*/
  6711. static PyObject *
  6712. os_sched_get_priority_min_impl(PyObject *module, int policy)
  6713. /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
  6714. {
  6715. int min = sched_get_priority_min(policy);
  6716. if (min < 0)
  6717. return posix_error();
  6718. return PyLong_FromLong(min);
  6719. }
  6720. #endif /* HAVE_SCHED_GET_PRIORITY_MAX */
  6721. #ifdef HAVE_SCHED_SETSCHEDULER
  6722. /*[clinic input]
  6723. os.sched_getscheduler
  6724. pid: pid_t
  6725. /
  6726. Get the scheduling policy for the process identified by pid.
  6727. Passing 0 for pid returns the scheduling policy for the calling process.
  6728. [clinic start generated code]*/
  6729. static PyObject *
  6730. os_sched_getscheduler_impl(PyObject *module, pid_t pid)
  6731. /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
  6732. {
  6733. int policy;
  6734. policy = sched_getscheduler(pid);
  6735. if (policy < 0)
  6736. return posix_error();
  6737. return PyLong_FromLong(policy);
  6738. }
  6739. #endif /* HAVE_SCHED_SETSCHEDULER */
  6740. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  6741. /*[clinic input]
  6742. class os.sched_param "PyObject *" "SchedParamType"
  6743. @classmethod
  6744. os.sched_param.__new__
  6745. sched_priority: object
  6746. A scheduling parameter.
  6747. Currently has only one field: sched_priority
  6748. [clinic start generated code]*/
  6749. static PyObject *
  6750. os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
  6751. /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
  6752. {
  6753. PyObject *res;
  6754. res = PyStructSequence_New(type);
  6755. if (!res)
  6756. return NULL;
  6757. PyStructSequence_SET_ITEM(res, 0, Py_NewRef(sched_priority));
  6758. return res;
  6759. }
  6760. static PyObject *
  6761. os_sched_param_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
  6762. {
  6763. return Py_BuildValue("(O(N))", Py_TYPE(self), PyStructSequence_GetItem(self, 0));
  6764. }
  6765. static PyMethodDef os_sched_param_reduce_method = {
  6766. "__reduce__", (PyCFunction)os_sched_param_reduce, METH_NOARGS|METH_COEXIST, NULL,
  6767. };
  6768. PyDoc_VAR(os_sched_param__doc__);
  6769. static PyStructSequence_Field sched_param_fields[] = {
  6770. {"sched_priority", "the scheduling priority"},
  6771. {0}
  6772. };
  6773. static PyStructSequence_Desc sched_param_desc = {
  6774. "sched_param", /* name */
  6775. os_sched_param__doc__, /* doc */
  6776. sched_param_fields,
  6777. 1
  6778. };
  6779. static int
  6780. convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res)
  6781. {
  6782. long priority;
  6783. if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) {
  6784. PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
  6785. return 0;
  6786. }
  6787. priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
  6788. if (priority == -1 && PyErr_Occurred())
  6789. return 0;
  6790. if (priority > INT_MAX || priority < INT_MIN) {
  6791. PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
  6792. return 0;
  6793. }
  6794. res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
  6795. return 1;
  6796. }
  6797. #endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
  6798. #ifdef HAVE_SCHED_SETSCHEDULER
  6799. /*[clinic input]
  6800. os.sched_setscheduler
  6801. pid: pid_t
  6802. policy: int
  6803. param as param_obj: object
  6804. /
  6805. Set the scheduling policy for the process identified by pid.
  6806. If pid is 0, the calling process is changed.
  6807. param is an instance of sched_param.
  6808. [clinic start generated code]*/
  6809. static PyObject *
  6810. os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
  6811. PyObject *param_obj)
  6812. /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/
  6813. {
  6814. struct sched_param param;
  6815. if (!convert_sched_param(module, param_obj, &param)) {
  6816. return NULL;
  6817. }
  6818. /*
  6819. ** sched_setscheduler() returns 0 in Linux, but the previous
  6820. ** scheduling policy under Solaris/Illumos, and others.
  6821. ** On error, -1 is returned in all Operating Systems.
  6822. */
  6823. if (sched_setscheduler(pid, policy, &param) == -1)
  6824. return posix_error();
  6825. Py_RETURN_NONE;
  6826. }
  6827. #endif /* HAVE_SCHED_SETSCHEDULER*/
  6828. #ifdef HAVE_SCHED_SETPARAM
  6829. /*[clinic input]
  6830. os.sched_getparam
  6831. pid: pid_t
  6832. /
  6833. Returns scheduling parameters for the process identified by pid.
  6834. If pid is 0, returns parameters for the calling process.
  6835. Return value is an instance of sched_param.
  6836. [clinic start generated code]*/
  6837. static PyObject *
  6838. os_sched_getparam_impl(PyObject *module, pid_t pid)
  6839. /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
  6840. {
  6841. struct sched_param param;
  6842. PyObject *result;
  6843. PyObject *priority;
  6844. if (sched_getparam(pid, &param))
  6845. return posix_error();
  6846. PyObject *SchedParamType = get_posix_state(module)->SchedParamType;
  6847. result = PyStructSequence_New((PyTypeObject *)SchedParamType);
  6848. if (!result)
  6849. return NULL;
  6850. priority = PyLong_FromLong(param.sched_priority);
  6851. if (!priority) {
  6852. Py_DECREF(result);
  6853. return NULL;
  6854. }
  6855. PyStructSequence_SET_ITEM(result, 0, priority);
  6856. return result;
  6857. }
  6858. /*[clinic input]
  6859. os.sched_setparam
  6860. pid: pid_t
  6861. param as param_obj: object
  6862. /
  6863. Set scheduling parameters for the process identified by pid.
  6864. If pid is 0, sets parameters for the calling process.
  6865. param should be an instance of sched_param.
  6866. [clinic start generated code]*/
  6867. static PyObject *
  6868. os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj)
  6869. /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/
  6870. {
  6871. struct sched_param param;
  6872. if (!convert_sched_param(module, param_obj, &param)) {
  6873. return NULL;
  6874. }
  6875. if (sched_setparam(pid, &param))
  6876. return posix_error();
  6877. Py_RETURN_NONE;
  6878. }
  6879. #endif /* HAVE_SCHED_SETPARAM */
  6880. #ifdef HAVE_SCHED_RR_GET_INTERVAL
  6881. /*[clinic input]
  6882. os.sched_rr_get_interval -> double
  6883. pid: pid_t
  6884. /
  6885. Return the round-robin quantum for the process identified by pid, in seconds.
  6886. Value returned is a float.
  6887. [clinic start generated code]*/
  6888. static double
  6889. os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
  6890. /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
  6891. {
  6892. struct timespec interval;
  6893. if (sched_rr_get_interval(pid, &interval)) {
  6894. posix_error();
  6895. return -1.0;
  6896. }
  6897. #ifdef _Py_MEMORY_SANITIZER
  6898. __msan_unpoison(&interval, sizeof(interval));
  6899. #endif
  6900. return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
  6901. }
  6902. #endif /* HAVE_SCHED_RR_GET_INTERVAL */
  6903. /*[clinic input]
  6904. os.sched_yield
  6905. Voluntarily relinquish the CPU.
  6906. [clinic start generated code]*/
  6907. static PyObject *
  6908. os_sched_yield_impl(PyObject *module)
  6909. /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
  6910. {
  6911. int result;
  6912. Py_BEGIN_ALLOW_THREADS
  6913. result = sched_yield();
  6914. Py_END_ALLOW_THREADS
  6915. if (result < 0) {
  6916. return posix_error();
  6917. }
  6918. Py_RETURN_NONE;
  6919. }
  6920. #ifdef HAVE_SCHED_SETAFFINITY
  6921. /* The minimum number of CPUs allocated in a cpu_set_t */
  6922. static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
  6923. /*[clinic input]
  6924. os.sched_setaffinity
  6925. pid: pid_t
  6926. mask : object
  6927. /
  6928. Set the CPU affinity of the process identified by pid to mask.
  6929. mask should be an iterable of integers identifying CPUs.
  6930. [clinic start generated code]*/
  6931. static PyObject *
  6932. os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
  6933. /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
  6934. {
  6935. int ncpus;
  6936. size_t setsize;
  6937. cpu_set_t *cpu_set = NULL;
  6938. PyObject *iterator = NULL, *item;
  6939. iterator = PyObject_GetIter(mask);
  6940. if (iterator == NULL)
  6941. return NULL;
  6942. ncpus = NCPUS_START;
  6943. setsize = CPU_ALLOC_SIZE(ncpus);
  6944. cpu_set = CPU_ALLOC(ncpus);
  6945. if (cpu_set == NULL) {
  6946. PyErr_NoMemory();
  6947. goto error;
  6948. }
  6949. CPU_ZERO_S(setsize, cpu_set);
  6950. while ((item = PyIter_Next(iterator))) {
  6951. long cpu;
  6952. if (!PyLong_Check(item)) {
  6953. PyErr_Format(PyExc_TypeError,
  6954. "expected an iterator of ints, "
  6955. "but iterator yielded %R",
  6956. Py_TYPE(item));
  6957. Py_DECREF(item);
  6958. goto error;
  6959. }
  6960. cpu = PyLong_AsLong(item);
  6961. Py_DECREF(item);
  6962. if (cpu < 0) {
  6963. if (!PyErr_Occurred())
  6964. PyErr_SetString(PyExc_ValueError, "negative CPU number");
  6965. goto error;
  6966. }
  6967. if (cpu > INT_MAX - 1) {
  6968. PyErr_SetString(PyExc_OverflowError, "CPU number too large");
  6969. goto error;
  6970. }
  6971. if (cpu >= ncpus) {
  6972. /* Grow CPU mask to fit the CPU number */
  6973. int newncpus = ncpus;
  6974. cpu_set_t *newmask;
  6975. size_t newsetsize;
  6976. while (newncpus <= cpu) {
  6977. if (newncpus > INT_MAX / 2)
  6978. newncpus = cpu + 1;
  6979. else
  6980. newncpus = newncpus * 2;
  6981. }
  6982. newmask = CPU_ALLOC(newncpus);
  6983. if (newmask == NULL) {
  6984. PyErr_NoMemory();
  6985. goto error;
  6986. }
  6987. newsetsize = CPU_ALLOC_SIZE(newncpus);
  6988. CPU_ZERO_S(newsetsize, newmask);
  6989. memcpy(newmask, cpu_set, setsize);
  6990. CPU_FREE(cpu_set);
  6991. setsize = newsetsize;
  6992. cpu_set = newmask;
  6993. ncpus = newncpus;
  6994. }
  6995. CPU_SET_S(cpu, setsize, cpu_set);
  6996. }
  6997. if (PyErr_Occurred()) {
  6998. goto error;
  6999. }
  7000. Py_CLEAR(iterator);
  7001. if (sched_setaffinity(pid, setsize, cpu_set)) {
  7002. posix_error();
  7003. goto error;
  7004. }
  7005. CPU_FREE(cpu_set);
  7006. Py_RETURN_NONE;
  7007. error:
  7008. if (cpu_set)
  7009. CPU_FREE(cpu_set);
  7010. Py_XDECREF(iterator);
  7011. return NULL;
  7012. }
  7013. /*[clinic input]
  7014. os.sched_getaffinity
  7015. pid: pid_t
  7016. /
  7017. Return the affinity of the process identified by pid (or the current process if zero).
  7018. The affinity is returned as a set of CPU identifiers.
  7019. [clinic start generated code]*/
  7020. static PyObject *
  7021. os_sched_getaffinity_impl(PyObject *module, pid_t pid)
  7022. /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
  7023. {
  7024. int cpu, ncpus, count;
  7025. size_t setsize;
  7026. cpu_set_t *mask = NULL;
  7027. PyObject *res = NULL;
  7028. ncpus = NCPUS_START;
  7029. while (1) {
  7030. setsize = CPU_ALLOC_SIZE(ncpus);
  7031. mask = CPU_ALLOC(ncpus);
  7032. if (mask == NULL)
  7033. return PyErr_NoMemory();
  7034. if (sched_getaffinity(pid, setsize, mask) == 0)
  7035. break;
  7036. CPU_FREE(mask);
  7037. if (errno != EINVAL)
  7038. return posix_error();
  7039. if (ncpus > INT_MAX / 2) {
  7040. PyErr_SetString(PyExc_OverflowError, "could not allocate "
  7041. "a large enough CPU set");
  7042. return NULL;
  7043. }
  7044. ncpus = ncpus * 2;
  7045. }
  7046. res = PySet_New(NULL);
  7047. if (res == NULL)
  7048. goto error;
  7049. for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
  7050. if (CPU_ISSET_S(cpu, setsize, mask)) {
  7051. PyObject *cpu_num = PyLong_FromLong(cpu);
  7052. --count;
  7053. if (cpu_num == NULL)
  7054. goto error;
  7055. if (PySet_Add(res, cpu_num)) {
  7056. Py_DECREF(cpu_num);
  7057. goto error;
  7058. }
  7059. Py_DECREF(cpu_num);
  7060. }
  7061. }
  7062. CPU_FREE(mask);
  7063. return res;
  7064. error:
  7065. if (mask)
  7066. CPU_FREE(mask);
  7067. Py_XDECREF(res);
  7068. return NULL;
  7069. }
  7070. #endif /* HAVE_SCHED_SETAFFINITY */
  7071. #endif /* HAVE_SCHED_H */
  7072. /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
  7073. #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
  7074. # define DEV_PTY_FILE "/dev/ptc"
  7075. # define HAVE_DEV_PTMX
  7076. #else
  7077. # define DEV_PTY_FILE "/dev/ptmx"
  7078. #endif
  7079. #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX)
  7080. #ifdef HAVE_PTY_H
  7081. #include <pty.h>
  7082. #ifdef HAVE_UTMP_H
  7083. #include <utmp.h>
  7084. #endif /* HAVE_UTMP_H */
  7085. #elif defined(HAVE_LIBUTIL_H)
  7086. #include <libutil.h>
  7087. #elif defined(HAVE_UTIL_H)
  7088. #include <util.h>
  7089. #endif /* HAVE_PTY_H */
  7090. #ifdef HAVE_STROPTS_H
  7091. #include <stropts.h>
  7092. #else
  7093. #define __SID ('S' << 8)
  7094. #define I_PUSH (__SID | 2)
  7095. #endif
  7096. #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_LOGIN_TTY) || defined(HAVE_DEV_PTMX) */
  7097. #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
  7098. /*[clinic input]
  7099. os.openpty
  7100. Open a pseudo-terminal.
  7101. Return a tuple of (master_fd, slave_fd) containing open file descriptors
  7102. for both the master and slave ends.
  7103. [clinic start generated code]*/
  7104. static PyObject *
  7105. os_openpty_impl(PyObject *module)
  7106. /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
  7107. {
  7108. int master_fd = -1, slave_fd = -1;
  7109. #ifndef HAVE_OPENPTY
  7110. char * slave_name;
  7111. #endif
  7112. #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
  7113. PyOS_sighandler_t sig_saved;
  7114. #if defined(__sun) && defined(__SVR4)
  7115. extern char *ptsname(int fildes);
  7116. #endif
  7117. #endif
  7118. #ifdef HAVE_OPENPTY
  7119. if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
  7120. goto posix_error;
  7121. if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
  7122. goto error;
  7123. if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
  7124. goto error;
  7125. #elif defined(HAVE__GETPTY)
  7126. slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
  7127. if (slave_name == NULL)
  7128. goto posix_error;
  7129. if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
  7130. goto error;
  7131. slave_fd = _Py_open(slave_name, O_RDWR);
  7132. if (slave_fd < 0)
  7133. goto error;
  7134. #else
  7135. master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
  7136. if (master_fd < 0)
  7137. goto posix_error;
  7138. sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
  7139. /* change permission of slave */
  7140. if (grantpt(master_fd) < 0) {
  7141. int saved_errno = errno;
  7142. PyOS_setsig(SIGCHLD, sig_saved);
  7143. errno = saved_errno;
  7144. goto posix_error;
  7145. }
  7146. /* unlock slave */
  7147. if (unlockpt(master_fd) < 0) {
  7148. int saved_errno = errno;
  7149. PyOS_setsig(SIGCHLD, sig_saved);
  7150. errno = saved_errno;
  7151. goto posix_error;
  7152. }
  7153. PyOS_setsig(SIGCHLD, sig_saved);
  7154. slave_name = ptsname(master_fd); /* get name of slave */
  7155. if (slave_name == NULL)
  7156. goto posix_error;
  7157. slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
  7158. if (slave_fd == -1)
  7159. goto error;
  7160. if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
  7161. goto posix_error;
  7162. #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
  7163. ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
  7164. ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
  7165. #ifndef __hpux
  7166. ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
  7167. #endif /* __hpux */
  7168. #endif /* HAVE_CYGWIN */
  7169. #endif /* HAVE_OPENPTY */
  7170. return Py_BuildValue("(ii)", master_fd, slave_fd);
  7171. posix_error:
  7172. posix_error();
  7173. error:
  7174. if (master_fd != -1)
  7175. close(master_fd);
  7176. if (slave_fd != -1)
  7177. close(slave_fd);
  7178. return NULL;
  7179. }
  7180. #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
  7181. #if defined(HAVE_SETSID) && defined(TIOCSCTTY)
  7182. #define HAVE_FALLBACK_LOGIN_TTY 1
  7183. #endif /* defined(HAVE_SETSID) && defined(TIOCSCTTY) */
  7184. #if defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY)
  7185. /*[clinic input]
  7186. os.login_tty
  7187. fd: fildes
  7188. /
  7189. Prepare the tty of which fd is a file descriptor for a new login session.
  7190. Make the calling process a session leader; make the tty the
  7191. controlling tty, the stdin, the stdout, and the stderr of the
  7192. calling process; close fd.
  7193. [clinic start generated code]*/
  7194. static PyObject *
  7195. os_login_tty_impl(PyObject *module, int fd)
  7196. /*[clinic end generated code: output=495a79911b4cc1bc input=5f298565099903a2]*/
  7197. {
  7198. #ifdef HAVE_LOGIN_TTY
  7199. if (login_tty(fd) == -1) {
  7200. return posix_error();
  7201. }
  7202. #else /* defined(HAVE_FALLBACK_LOGIN_TTY) */
  7203. /* Establish a new session. */
  7204. if (setsid() == -1) {
  7205. return posix_error();
  7206. }
  7207. /* The tty becomes the controlling terminal. */
  7208. if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1) {
  7209. return posix_error();
  7210. }
  7211. /* The tty becomes stdin/stdout/stderr */
  7212. if (dup2(fd, 0) == -1 || dup2(fd, 1) == -1 || dup2(fd, 2) == -1) {
  7213. return posix_error();
  7214. }
  7215. if (fd > 2) {
  7216. close(fd);
  7217. }
  7218. #endif /* HAVE_LOGIN_TTY */
  7219. Py_RETURN_NONE;
  7220. }
  7221. #endif /* defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY) */
  7222. #ifdef HAVE_FORKPTY
  7223. /*[clinic input]
  7224. os.forkpty
  7225. Fork a new process with a new pseudo-terminal as controlling tty.
  7226. Returns a tuple of (pid, master_fd).
  7227. Like fork(), return pid of 0 to the child process,
  7228. and pid of child to the parent process.
  7229. To both, return fd of newly opened pseudo-terminal.
  7230. [clinic start generated code]*/
  7231. static PyObject *
  7232. os_forkpty_impl(PyObject *module)
  7233. /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
  7234. {
  7235. int master_fd = -1;
  7236. pid_t pid;
  7237. PyInterpreterState *interp = _PyInterpreterState_GET();
  7238. if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
  7239. PyErr_SetString(PyExc_RuntimeError,
  7240. "can't fork at interpreter shutdown");
  7241. return NULL;
  7242. }
  7243. if (!_Py_IsMainInterpreter(interp)) {
  7244. PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
  7245. return NULL;
  7246. }
  7247. if (PySys_Audit("os.forkpty", NULL) < 0) {
  7248. return NULL;
  7249. }
  7250. PyOS_BeforeFork();
  7251. pid = forkpty(&master_fd, NULL, NULL, NULL);
  7252. if (pid == 0) {
  7253. /* child: this clobbers and resets the import lock. */
  7254. PyOS_AfterFork_Child();
  7255. } else {
  7256. warn_about_fork_with_threads("forkpty");
  7257. /* parent: release the import lock. */
  7258. PyOS_AfterFork_Parent();
  7259. }
  7260. if (pid == -1) {
  7261. return posix_error();
  7262. }
  7263. return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
  7264. }
  7265. #endif /* HAVE_FORKPTY */
  7266. #ifdef HAVE_GETEGID
  7267. /*[clinic input]
  7268. os.getegid
  7269. Return the current process's effective group id.
  7270. [clinic start generated code]*/
  7271. static PyObject *
  7272. os_getegid_impl(PyObject *module)
  7273. /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
  7274. {
  7275. return _PyLong_FromGid(getegid());
  7276. }
  7277. #endif /* HAVE_GETEGID */
  7278. #ifdef HAVE_GETEUID
  7279. /*[clinic input]
  7280. os.geteuid
  7281. Return the current process's effective user id.
  7282. [clinic start generated code]*/
  7283. static PyObject *
  7284. os_geteuid_impl(PyObject *module)
  7285. /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
  7286. {
  7287. return _PyLong_FromUid(geteuid());
  7288. }
  7289. #endif /* HAVE_GETEUID */
  7290. #ifdef HAVE_GETGID
  7291. /*[clinic input]
  7292. os.getgid
  7293. Return the current process's group id.
  7294. [clinic start generated code]*/
  7295. static PyObject *
  7296. os_getgid_impl(PyObject *module)
  7297. /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
  7298. {
  7299. return _PyLong_FromGid(getgid());
  7300. }
  7301. #endif /* HAVE_GETGID */
  7302. #if defined(HAVE_GETPID)
  7303. /*[clinic input]
  7304. os.getpid
  7305. Return the current process id.
  7306. [clinic start generated code]*/
  7307. static PyObject *
  7308. os_getpid_impl(PyObject *module)
  7309. /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
  7310. {
  7311. #if !defined(MS_WINDOWS) || defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
  7312. return PyLong_FromPid(getpid());
  7313. #else
  7314. return PyLong_FromUnsignedLong(GetCurrentProcessId());
  7315. #endif
  7316. }
  7317. #endif /* defined(HAVE_GETPID) */
  7318. #ifdef NGROUPS_MAX
  7319. #define MAX_GROUPS NGROUPS_MAX
  7320. #else
  7321. /* defined to be 16 on Solaris7, so this should be a small number */
  7322. #define MAX_GROUPS 64
  7323. #endif
  7324. #ifdef HAVE_GETGROUPLIST
  7325. #ifdef __APPLE__
  7326. /*[clinic input]
  7327. os.getgrouplist
  7328. user: str
  7329. username to lookup
  7330. group as basegid: int
  7331. base group id of the user
  7332. /
  7333. Returns a list of groups to which a user belongs.
  7334. [clinic start generated code]*/
  7335. static PyObject *
  7336. os_getgrouplist_impl(PyObject *module, const char *user, int basegid)
  7337. /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/
  7338. #else
  7339. /*[clinic input]
  7340. os.getgrouplist
  7341. user: str
  7342. username to lookup
  7343. group as basegid: gid_t
  7344. base group id of the user
  7345. /
  7346. Returns a list of groups to which a user belongs.
  7347. [clinic start generated code]*/
  7348. static PyObject *
  7349. os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid)
  7350. /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/
  7351. #endif
  7352. {
  7353. int i, ngroups;
  7354. PyObject *list;
  7355. #ifdef __APPLE__
  7356. int *groups;
  7357. #else
  7358. gid_t *groups;
  7359. #endif
  7360. /*
  7361. * NGROUPS_MAX is defined by POSIX.1 as the maximum
  7362. * number of supplimental groups a users can belong to.
  7363. * We have to increment it by one because
  7364. * getgrouplist() returns both the supplemental groups
  7365. * and the primary group, i.e. all of the groups the
  7366. * user belongs to.
  7367. */
  7368. ngroups = 1 + MAX_GROUPS;
  7369. while (1) {
  7370. #ifdef __APPLE__
  7371. groups = PyMem_New(int, ngroups);
  7372. #else
  7373. groups = PyMem_New(gid_t, ngroups);
  7374. #endif
  7375. if (groups == NULL) {
  7376. return PyErr_NoMemory();
  7377. }
  7378. int old_ngroups = ngroups;
  7379. if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
  7380. /* Success */
  7381. break;
  7382. }
  7383. /* getgrouplist() fails if the group list is too small */
  7384. PyMem_Free(groups);
  7385. if (ngroups > old_ngroups) {
  7386. /* If the group list is too small, the glibc implementation of
  7387. getgrouplist() sets ngroups to the total number of groups and
  7388. returns -1. */
  7389. }
  7390. else {
  7391. /* Double the group list size */
  7392. if (ngroups > INT_MAX / 2) {
  7393. return PyErr_NoMemory();
  7394. }
  7395. ngroups *= 2;
  7396. }
  7397. /* Retry getgrouplist() with a larger group list */
  7398. }
  7399. #ifdef _Py_MEMORY_SANITIZER
  7400. /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
  7401. __msan_unpoison(&ngroups, sizeof(ngroups));
  7402. __msan_unpoison(groups, ngroups*sizeof(*groups));
  7403. #endif
  7404. list = PyList_New(ngroups);
  7405. if (list == NULL) {
  7406. PyMem_Free(groups);
  7407. return NULL;
  7408. }
  7409. for (i = 0; i < ngroups; i++) {
  7410. #ifdef __APPLE__
  7411. PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
  7412. #else
  7413. PyObject *o = _PyLong_FromGid(groups[i]);
  7414. #endif
  7415. if (o == NULL) {
  7416. Py_DECREF(list);
  7417. PyMem_Free(groups);
  7418. return NULL;
  7419. }
  7420. PyList_SET_ITEM(list, i, o);
  7421. }
  7422. PyMem_Free(groups);
  7423. return list;
  7424. }
  7425. #endif /* HAVE_GETGROUPLIST */
  7426. #ifdef HAVE_GETGROUPS
  7427. /*[clinic input]
  7428. os.getgroups
  7429. Return list of supplemental group IDs for the process.
  7430. [clinic start generated code]*/
  7431. static PyObject *
  7432. os_getgroups_impl(PyObject *module)
  7433. /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
  7434. {
  7435. // Call getgroups with length 0 to get the actual number of groups
  7436. int n = getgroups(0, NULL);
  7437. if (n < 0) {
  7438. return posix_error();
  7439. }
  7440. if (n == 0) {
  7441. return PyList_New(0);
  7442. }
  7443. gid_t *grouplist = PyMem_New(gid_t, n);
  7444. if (grouplist == NULL) {
  7445. return PyErr_NoMemory();
  7446. }
  7447. n = getgroups(n, grouplist);
  7448. if (n == -1) {
  7449. posix_error();
  7450. PyMem_Free(grouplist);
  7451. return NULL;
  7452. }
  7453. PyObject *result = PyList_New(n);
  7454. if (result == NULL) {
  7455. goto error;
  7456. }
  7457. for (int i = 0; i < n; ++i) {
  7458. PyObject *group = _PyLong_FromGid(grouplist[i]);
  7459. if (group == NULL) {
  7460. goto error;
  7461. }
  7462. PyList_SET_ITEM(result, i, group);
  7463. }
  7464. PyMem_Free(grouplist);
  7465. return result;
  7466. error:
  7467. PyMem_Free(grouplist);
  7468. Py_XDECREF(result);
  7469. return NULL;
  7470. }
  7471. #endif /* HAVE_GETGROUPS */
  7472. #ifdef HAVE_INITGROUPS
  7473. #ifdef __APPLE__
  7474. /*[clinic input]
  7475. os.initgroups
  7476. username as oname: FSConverter
  7477. gid: int
  7478. /
  7479. Initialize the group access list.
  7480. Call the system initgroups() to initialize the group access list with all of
  7481. the groups of which the specified username is a member, plus the specified
  7482. group id.
  7483. [clinic start generated code]*/
  7484. static PyObject *
  7485. os_initgroups_impl(PyObject *module, PyObject *oname, int gid)
  7486. /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/
  7487. #else
  7488. /*[clinic input]
  7489. os.initgroups
  7490. username as oname: FSConverter
  7491. gid: gid_t
  7492. /
  7493. Initialize the group access list.
  7494. Call the system initgroups() to initialize the group access list with all of
  7495. the groups of which the specified username is a member, plus the specified
  7496. group id.
  7497. [clinic start generated code]*/
  7498. static PyObject *
  7499. os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
  7500. /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/
  7501. #endif
  7502. {
  7503. const char *username = PyBytes_AS_STRING(oname);
  7504. if (initgroups(username, gid) == -1)
  7505. return PyErr_SetFromErrno(PyExc_OSError);
  7506. Py_RETURN_NONE;
  7507. }
  7508. #endif /* HAVE_INITGROUPS */
  7509. #ifdef HAVE_GETPGID
  7510. /*[clinic input]
  7511. os.getpgid
  7512. pid: pid_t
  7513. Call the system call getpgid(), and return the result.
  7514. [clinic start generated code]*/
  7515. static PyObject *
  7516. os_getpgid_impl(PyObject *module, pid_t pid)
  7517. /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
  7518. {
  7519. pid_t pgid = getpgid(pid);
  7520. if (pgid < 0)
  7521. return posix_error();
  7522. return PyLong_FromPid(pgid);
  7523. }
  7524. #endif /* HAVE_GETPGID */
  7525. #ifdef HAVE_GETPGRP
  7526. /*[clinic input]
  7527. os.getpgrp
  7528. Return the current process group id.
  7529. [clinic start generated code]*/
  7530. static PyObject *
  7531. os_getpgrp_impl(PyObject *module)
  7532. /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
  7533. {
  7534. #ifdef GETPGRP_HAVE_ARG
  7535. return PyLong_FromPid(getpgrp(0));
  7536. #else /* GETPGRP_HAVE_ARG */
  7537. return PyLong_FromPid(getpgrp());
  7538. #endif /* GETPGRP_HAVE_ARG */
  7539. }
  7540. #endif /* HAVE_GETPGRP */
  7541. #ifdef HAVE_SETPGRP
  7542. /*[clinic input]
  7543. os.setpgrp
  7544. Make the current process the leader of its process group.
  7545. [clinic start generated code]*/
  7546. static PyObject *
  7547. os_setpgrp_impl(PyObject *module)
  7548. /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
  7549. {
  7550. #ifdef SETPGRP_HAVE_ARG
  7551. if (setpgrp(0, 0) < 0)
  7552. #else /* SETPGRP_HAVE_ARG */
  7553. if (setpgrp() < 0)
  7554. #endif /* SETPGRP_HAVE_ARG */
  7555. return posix_error();
  7556. Py_RETURN_NONE;
  7557. }
  7558. #endif /* HAVE_SETPGRP */
  7559. #ifdef HAVE_GETPPID
  7560. #ifdef MS_WINDOWS
  7561. #include <processsnapshot.h>
  7562. static PyObject*
  7563. win32_getppid(void)
  7564. {
  7565. DWORD error;
  7566. PyObject* result = NULL;
  7567. HANDLE process = GetCurrentProcess();
  7568. HPSS snapshot = NULL;
  7569. error = PssCaptureSnapshot(process, PSS_CAPTURE_NONE, 0, &snapshot);
  7570. if (error != ERROR_SUCCESS) {
  7571. return PyErr_SetFromWindowsErr(error);
  7572. }
  7573. PSS_PROCESS_INFORMATION info;
  7574. error = PssQuerySnapshot(snapshot, PSS_QUERY_PROCESS_INFORMATION, &info,
  7575. sizeof(info));
  7576. if (error == ERROR_SUCCESS) {
  7577. result = PyLong_FromUnsignedLong(info.ParentProcessId);
  7578. }
  7579. else {
  7580. result = PyErr_SetFromWindowsErr(error);
  7581. }
  7582. PssFreeSnapshot(process, snapshot);
  7583. return result;
  7584. }
  7585. #endif /*MS_WINDOWS*/
  7586. /*[clinic input]
  7587. os.getppid
  7588. Return the parent's process id.
  7589. If the parent process has already exited, Windows machines will still
  7590. return its id; others systems will return the id of the 'init' process (1).
  7591. [clinic start generated code]*/
  7592. static PyObject *
  7593. os_getppid_impl(PyObject *module)
  7594. /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
  7595. {
  7596. #ifdef MS_WINDOWS
  7597. return win32_getppid();
  7598. #else
  7599. return PyLong_FromPid(getppid());
  7600. #endif
  7601. }
  7602. #endif /* HAVE_GETPPID */
  7603. #ifdef HAVE_GETLOGIN
  7604. /*[clinic input]
  7605. os.getlogin
  7606. Return the actual login name.
  7607. [clinic start generated code]*/
  7608. static PyObject *
  7609. os_getlogin_impl(PyObject *module)
  7610. /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
  7611. {
  7612. PyObject *result = NULL;
  7613. #ifdef MS_WINDOWS
  7614. wchar_t user_name[UNLEN + 1];
  7615. DWORD num_chars = Py_ARRAY_LENGTH(user_name);
  7616. if (GetUserNameW(user_name, &num_chars)) {
  7617. /* num_chars is the number of unicode chars plus null terminator */
  7618. result = PyUnicode_FromWideChar(user_name, num_chars - 1);
  7619. }
  7620. else
  7621. result = PyErr_SetFromWindowsErr(GetLastError());
  7622. #else
  7623. char *name;
  7624. int old_errno = errno;
  7625. errno = 0;
  7626. name = getlogin();
  7627. if (name == NULL) {
  7628. if (errno)
  7629. posix_error();
  7630. else
  7631. PyErr_SetString(PyExc_OSError, "unable to determine login name");
  7632. }
  7633. else
  7634. result = PyUnicode_DecodeFSDefault(name);
  7635. errno = old_errno;
  7636. #endif
  7637. return result;
  7638. }
  7639. #endif /* HAVE_GETLOGIN */
  7640. #ifdef HAVE_GETUID
  7641. /*[clinic input]
  7642. os.getuid
  7643. Return the current process's user id.
  7644. [clinic start generated code]*/
  7645. static PyObject *
  7646. os_getuid_impl(PyObject *module)
  7647. /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
  7648. {
  7649. return _PyLong_FromUid(getuid());
  7650. }
  7651. #endif /* HAVE_GETUID */
  7652. #ifdef MS_WINDOWS
  7653. #define HAVE_KILL
  7654. #endif /* MS_WINDOWS */
  7655. #ifdef HAVE_KILL
  7656. /*[clinic input]
  7657. os.kill
  7658. pid: pid_t
  7659. signal: Py_ssize_t
  7660. /
  7661. Kill a process with a signal.
  7662. [clinic start generated code]*/
  7663. static PyObject *
  7664. os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
  7665. /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
  7666. {
  7667. if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
  7668. return NULL;
  7669. }
  7670. #ifndef MS_WINDOWS
  7671. if (kill(pid, (int)signal) == -1) {
  7672. return posix_error();
  7673. }
  7674. // Check immediately if the signal was sent to the current process.
  7675. // Don't micro-optimize pid == getpid(), since PyErr_SetString() check
  7676. // is cheap.
  7677. if (PyErr_CheckSignals()) {
  7678. return NULL;
  7679. }
  7680. Py_RETURN_NONE;
  7681. #else /* !MS_WINDOWS */
  7682. PyObject *result;
  7683. DWORD sig = (DWORD)signal;
  7684. DWORD err;
  7685. HANDLE handle;
  7686. #ifdef HAVE_WINDOWS_CONSOLE_IO
  7687. /* Console processes which share a common console can be sent CTRL+C or
  7688. CTRL+BREAK events, provided they handle said events. */
  7689. if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
  7690. if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
  7691. err = GetLastError();
  7692. PyErr_SetFromWindowsErr(err);
  7693. }
  7694. else {
  7695. Py_RETURN_NONE;
  7696. }
  7697. }
  7698. #endif /* HAVE_WINDOWS_CONSOLE_IO */
  7699. /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
  7700. attempt to open and terminate the process. */
  7701. handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
  7702. if (handle == NULL) {
  7703. err = GetLastError();
  7704. return PyErr_SetFromWindowsErr(err);
  7705. }
  7706. if (TerminateProcess(handle, sig) == 0) {
  7707. err = GetLastError();
  7708. result = PyErr_SetFromWindowsErr(err);
  7709. } else {
  7710. result = Py_NewRef(Py_None);
  7711. }
  7712. CloseHandle(handle);
  7713. return result;
  7714. #endif /* !MS_WINDOWS */
  7715. }
  7716. #endif /* HAVE_KILL */
  7717. #ifdef HAVE_KILLPG
  7718. /*[clinic input]
  7719. os.killpg
  7720. pgid: pid_t
  7721. signal: int
  7722. /
  7723. Kill a process group with a signal.
  7724. [clinic start generated code]*/
  7725. static PyObject *
  7726. os_killpg_impl(PyObject *module, pid_t pgid, int signal)
  7727. /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
  7728. {
  7729. if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
  7730. return NULL;
  7731. }
  7732. /* XXX some man pages make the `pgid` parameter an int, others
  7733. a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
  7734. take the same type. Moreover, pid_t is always at least as wide as
  7735. int (else compilation of this module fails), which is safe. */
  7736. if (killpg(pgid, signal) == -1)
  7737. return posix_error();
  7738. Py_RETURN_NONE;
  7739. }
  7740. #endif /* HAVE_KILLPG */
  7741. #ifdef HAVE_PLOCK
  7742. #ifdef HAVE_SYS_LOCK_H
  7743. #include <sys/lock.h>
  7744. #endif
  7745. /*[clinic input]
  7746. os.plock
  7747. op: int
  7748. /
  7749. Lock program segments into memory.");
  7750. [clinic start generated code]*/
  7751. static PyObject *
  7752. os_plock_impl(PyObject *module, int op)
  7753. /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
  7754. {
  7755. if (plock(op) == -1)
  7756. return posix_error();
  7757. Py_RETURN_NONE;
  7758. }
  7759. #endif /* HAVE_PLOCK */
  7760. #ifdef HAVE_SETUID
  7761. /*[clinic input]
  7762. os.setuid
  7763. uid: uid_t
  7764. /
  7765. Set the current process's user id.
  7766. [clinic start generated code]*/
  7767. static PyObject *
  7768. os_setuid_impl(PyObject *module, uid_t uid)
  7769. /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
  7770. {
  7771. if (setuid(uid) < 0)
  7772. return posix_error();
  7773. Py_RETURN_NONE;
  7774. }
  7775. #endif /* HAVE_SETUID */
  7776. #ifdef HAVE_SETEUID
  7777. /*[clinic input]
  7778. os.seteuid
  7779. euid: uid_t
  7780. /
  7781. Set the current process's effective user id.
  7782. [clinic start generated code]*/
  7783. static PyObject *
  7784. os_seteuid_impl(PyObject *module, uid_t euid)
  7785. /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
  7786. {
  7787. if (seteuid(euid) < 0)
  7788. return posix_error();
  7789. Py_RETURN_NONE;
  7790. }
  7791. #endif /* HAVE_SETEUID */
  7792. #ifdef HAVE_SETEGID
  7793. /*[clinic input]
  7794. os.setegid
  7795. egid: gid_t
  7796. /
  7797. Set the current process's effective group id.
  7798. [clinic start generated code]*/
  7799. static PyObject *
  7800. os_setegid_impl(PyObject *module, gid_t egid)
  7801. /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
  7802. {
  7803. if (setegid(egid) < 0)
  7804. return posix_error();
  7805. Py_RETURN_NONE;
  7806. }
  7807. #endif /* HAVE_SETEGID */
  7808. #ifdef HAVE_SETREUID
  7809. /*[clinic input]
  7810. os.setreuid
  7811. ruid: uid_t
  7812. euid: uid_t
  7813. /
  7814. Set the current process's real and effective user ids.
  7815. [clinic start generated code]*/
  7816. static PyObject *
  7817. os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
  7818. /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
  7819. {
  7820. if (setreuid(ruid, euid) < 0) {
  7821. return posix_error();
  7822. } else {
  7823. Py_RETURN_NONE;
  7824. }
  7825. }
  7826. #endif /* HAVE_SETREUID */
  7827. #ifdef HAVE_SETREGID
  7828. /*[clinic input]
  7829. os.setregid
  7830. rgid: gid_t
  7831. egid: gid_t
  7832. /
  7833. Set the current process's real and effective group ids.
  7834. [clinic start generated code]*/
  7835. static PyObject *
  7836. os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
  7837. /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
  7838. {
  7839. if (setregid(rgid, egid) < 0)
  7840. return posix_error();
  7841. Py_RETURN_NONE;
  7842. }
  7843. #endif /* HAVE_SETREGID */
  7844. #ifdef HAVE_SETGID
  7845. /*[clinic input]
  7846. os.setgid
  7847. gid: gid_t
  7848. /
  7849. Set the current process's group id.
  7850. [clinic start generated code]*/
  7851. static PyObject *
  7852. os_setgid_impl(PyObject *module, gid_t gid)
  7853. /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
  7854. {
  7855. if (setgid(gid) < 0)
  7856. return posix_error();
  7857. Py_RETURN_NONE;
  7858. }
  7859. #endif /* HAVE_SETGID */
  7860. #ifdef HAVE_SETGROUPS
  7861. /*[clinic input]
  7862. os.setgroups
  7863. groups: object
  7864. /
  7865. Set the groups of the current process to list.
  7866. [clinic start generated code]*/
  7867. static PyObject *
  7868. os_setgroups(PyObject *module, PyObject *groups)
  7869. /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
  7870. {
  7871. if (!PySequence_Check(groups)) {
  7872. PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
  7873. return NULL;
  7874. }
  7875. Py_ssize_t len = PySequence_Size(groups);
  7876. if (len < 0) {
  7877. return NULL;
  7878. }
  7879. if (len > MAX_GROUPS) {
  7880. PyErr_SetString(PyExc_ValueError, "too many groups");
  7881. return NULL;
  7882. }
  7883. gid_t *grouplist = PyMem_New(gid_t, len);
  7884. for (Py_ssize_t i = 0; i < len; i++) {
  7885. PyObject *elem;
  7886. elem = PySequence_GetItem(groups, i);
  7887. if (!elem) {
  7888. PyMem_Free(grouplist);
  7889. return NULL;
  7890. }
  7891. if (!PyLong_Check(elem)) {
  7892. PyErr_SetString(PyExc_TypeError,
  7893. "groups must be integers");
  7894. Py_DECREF(elem);
  7895. PyMem_Free(grouplist);
  7896. return NULL;
  7897. } else {
  7898. if (!_Py_Gid_Converter(elem, &grouplist[i])) {
  7899. Py_DECREF(elem);
  7900. PyMem_Free(grouplist);
  7901. return NULL;
  7902. }
  7903. }
  7904. Py_DECREF(elem);
  7905. }
  7906. if (setgroups(len, grouplist) < 0) {
  7907. posix_error();
  7908. PyMem_Free(grouplist);
  7909. return NULL;
  7910. }
  7911. PyMem_Free(grouplist);
  7912. Py_RETURN_NONE;
  7913. }
  7914. #endif /* HAVE_SETGROUPS */
  7915. #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
  7916. static PyObject *
  7917. wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
  7918. {
  7919. PyObject *result;
  7920. PyObject *struct_rusage;
  7921. if (pid == -1)
  7922. return posix_error();
  7923. // If wait succeeded but no child was ready to report status, ru will not
  7924. // have been populated.
  7925. if (pid == 0) {
  7926. memset(ru, 0, sizeof(*ru));
  7927. }
  7928. struct_rusage = _PyImport_GetModuleAttrString("resource", "struct_rusage");
  7929. if (struct_rusage == NULL)
  7930. return NULL;
  7931. /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
  7932. result = PyStructSequence_New((PyTypeObject*) struct_rusage);
  7933. Py_DECREF(struct_rusage);
  7934. if (!result)
  7935. return NULL;
  7936. int pos = 0;
  7937. #ifndef doubletime
  7938. #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
  7939. #endif
  7940. #define SET_RESULT(CALL) \
  7941. do { \
  7942. PyObject *item = (CALL); \
  7943. if (item == NULL) { \
  7944. Py_DECREF(result); \
  7945. return NULL; \
  7946. } \
  7947. PyStructSequence_SET_ITEM(result, pos++, item); \
  7948. } while(0)
  7949. SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_utime)));
  7950. SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_stime)));
  7951. SET_RESULT(PyLong_FromLong(ru->ru_maxrss));
  7952. SET_RESULT(PyLong_FromLong(ru->ru_ixrss));
  7953. SET_RESULT(PyLong_FromLong(ru->ru_idrss));
  7954. SET_RESULT(PyLong_FromLong(ru->ru_isrss));
  7955. SET_RESULT(PyLong_FromLong(ru->ru_minflt));
  7956. SET_RESULT(PyLong_FromLong(ru->ru_majflt));
  7957. SET_RESULT(PyLong_FromLong(ru->ru_nswap));
  7958. SET_RESULT(PyLong_FromLong(ru->ru_inblock));
  7959. SET_RESULT(PyLong_FromLong(ru->ru_oublock));
  7960. SET_RESULT(PyLong_FromLong(ru->ru_msgsnd));
  7961. SET_RESULT(PyLong_FromLong(ru->ru_msgrcv));
  7962. SET_RESULT(PyLong_FromLong(ru->ru_nsignals));
  7963. SET_RESULT(PyLong_FromLong(ru->ru_nvcsw));
  7964. SET_RESULT(PyLong_FromLong(ru->ru_nivcsw));
  7965. #undef SET_RESULT
  7966. return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
  7967. }
  7968. #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
  7969. #ifdef HAVE_WAIT3
  7970. /*[clinic input]
  7971. os.wait3
  7972. options: int
  7973. Wait for completion of a child process.
  7974. Returns a tuple of information about the child process:
  7975. (pid, status, rusage)
  7976. [clinic start generated code]*/
  7977. static PyObject *
  7978. os_wait3_impl(PyObject *module, int options)
  7979. /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
  7980. {
  7981. pid_t pid;
  7982. struct rusage ru;
  7983. int async_err = 0;
  7984. WAIT_TYPE status;
  7985. WAIT_STATUS_INT(status) = 0;
  7986. do {
  7987. Py_BEGIN_ALLOW_THREADS
  7988. pid = wait3(&status, options, &ru);
  7989. Py_END_ALLOW_THREADS
  7990. } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  7991. if (pid < 0)
  7992. return (!async_err) ? posix_error() : NULL;
  7993. return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru);
  7994. }
  7995. #endif /* HAVE_WAIT3 */
  7996. #ifdef HAVE_WAIT4
  7997. /*[clinic input]
  7998. os.wait4
  7999. pid: pid_t
  8000. options: int
  8001. Wait for completion of a specific child process.
  8002. Returns a tuple of information about the child process:
  8003. (pid, status, rusage)
  8004. [clinic start generated code]*/
  8005. static PyObject *
  8006. os_wait4_impl(PyObject *module, pid_t pid, int options)
  8007. /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
  8008. {
  8009. pid_t res;
  8010. struct rusage ru;
  8011. int async_err = 0;
  8012. WAIT_TYPE status;
  8013. WAIT_STATUS_INT(status) = 0;
  8014. do {
  8015. Py_BEGIN_ALLOW_THREADS
  8016. res = wait4(pid, &status, options, &ru);
  8017. Py_END_ALLOW_THREADS
  8018. } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8019. if (res < 0)
  8020. return (!async_err) ? posix_error() : NULL;
  8021. return wait_helper(module, res, WAIT_STATUS_INT(status), &ru);
  8022. }
  8023. #endif /* HAVE_WAIT4 */
  8024. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  8025. /*[clinic input]
  8026. os.waitid
  8027. idtype: idtype_t
  8028. Must be one of be P_PID, P_PGID or P_ALL.
  8029. id: id_t
  8030. The id to wait on.
  8031. options: int
  8032. Constructed from the ORing of one or more of WEXITED, WSTOPPED
  8033. or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
  8034. /
  8035. Returns the result of waiting for a process or processes.
  8036. Returns either waitid_result or None if WNOHANG is specified and there are
  8037. no children in a waitable state.
  8038. [clinic start generated code]*/
  8039. static PyObject *
  8040. os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
  8041. /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
  8042. {
  8043. PyObject *result;
  8044. int res;
  8045. int async_err = 0;
  8046. siginfo_t si;
  8047. si.si_pid = 0;
  8048. do {
  8049. Py_BEGIN_ALLOW_THREADS
  8050. res = waitid(idtype, id, &si, options);
  8051. Py_END_ALLOW_THREADS
  8052. } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8053. if (res < 0)
  8054. return (!async_err) ? posix_error() : NULL;
  8055. if (si.si_pid == 0)
  8056. Py_RETURN_NONE;
  8057. PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
  8058. result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
  8059. if (!result)
  8060. return NULL;
  8061. int pos = 0;
  8062. #define SET_RESULT(CALL) \
  8063. do { \
  8064. PyObject *item = (CALL); \
  8065. if (item == NULL) { \
  8066. Py_DECREF(result); \
  8067. return NULL; \
  8068. } \
  8069. PyStructSequence_SET_ITEM(result, pos++, item); \
  8070. } while(0)
  8071. SET_RESULT(PyLong_FromPid(si.si_pid));
  8072. SET_RESULT(_PyLong_FromUid(si.si_uid));
  8073. SET_RESULT(PyLong_FromLong((long)(si.si_signo)));
  8074. SET_RESULT(PyLong_FromLong((long)(si.si_status)));
  8075. SET_RESULT(PyLong_FromLong((long)(si.si_code)));
  8076. #undef SET_RESULT
  8077. return result;
  8078. }
  8079. #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
  8080. #if defined(HAVE_WAITPID)
  8081. /*[clinic input]
  8082. os.waitpid
  8083. pid: pid_t
  8084. options: int
  8085. /
  8086. Wait for completion of a given child process.
  8087. Returns a tuple of information regarding the child process:
  8088. (pid, status)
  8089. The options argument is ignored on Windows.
  8090. [clinic start generated code]*/
  8091. static PyObject *
  8092. os_waitpid_impl(PyObject *module, pid_t pid, int options)
  8093. /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
  8094. {
  8095. pid_t res;
  8096. int async_err = 0;
  8097. WAIT_TYPE status;
  8098. WAIT_STATUS_INT(status) = 0;
  8099. do {
  8100. Py_BEGIN_ALLOW_THREADS
  8101. res = waitpid(pid, &status, options);
  8102. Py_END_ALLOW_THREADS
  8103. } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8104. if (res < 0)
  8105. return (!async_err) ? posix_error() : NULL;
  8106. return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
  8107. }
  8108. #elif defined(HAVE_CWAIT)
  8109. /* MS C has a variant of waitpid() that's usable for most purposes. */
  8110. /*[clinic input]
  8111. os.waitpid
  8112. pid: intptr_t
  8113. options: int
  8114. /
  8115. Wait for completion of a given process.
  8116. Returns a tuple of information regarding the process:
  8117. (pid, status << 8)
  8118. The options argument is ignored on Windows.
  8119. [clinic start generated code]*/
  8120. static PyObject *
  8121. os_waitpid_impl(PyObject *module, intptr_t pid, int options)
  8122. /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
  8123. {
  8124. int status;
  8125. intptr_t res;
  8126. int async_err = 0;
  8127. do {
  8128. Py_BEGIN_ALLOW_THREADS
  8129. _Py_BEGIN_SUPPRESS_IPH
  8130. res = _cwait(&status, pid, options);
  8131. _Py_END_SUPPRESS_IPH
  8132. Py_END_ALLOW_THREADS
  8133. } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8134. if (res < 0)
  8135. return (!async_err) ? posix_error() : NULL;
  8136. unsigned long long ustatus = (unsigned int)status;
  8137. /* shift the status left a byte so this is more like the POSIX waitpid */
  8138. return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
  8139. }
  8140. #endif
  8141. #ifdef HAVE_WAIT
  8142. /*[clinic input]
  8143. os.wait
  8144. Wait for completion of a child process.
  8145. Returns a tuple of information about the child process:
  8146. (pid, status)
  8147. [clinic start generated code]*/
  8148. static PyObject *
  8149. os_wait_impl(PyObject *module)
  8150. /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
  8151. {
  8152. pid_t pid;
  8153. int async_err = 0;
  8154. WAIT_TYPE status;
  8155. WAIT_STATUS_INT(status) = 0;
  8156. do {
  8157. Py_BEGIN_ALLOW_THREADS
  8158. pid = wait(&status);
  8159. Py_END_ALLOW_THREADS
  8160. } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8161. if (pid < 0)
  8162. return (!async_err) ? posix_error() : NULL;
  8163. return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
  8164. }
  8165. #endif /* HAVE_WAIT */
  8166. #if defined(__linux__) && defined(__NR_pidfd_open)
  8167. /*[clinic input]
  8168. os.pidfd_open
  8169. pid: pid_t
  8170. flags: unsigned_int = 0
  8171. Return a file descriptor referring to the process *pid*.
  8172. The descriptor can be used to perform process management without races and
  8173. signals.
  8174. [clinic start generated code]*/
  8175. static PyObject *
  8176. os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
  8177. /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
  8178. {
  8179. int fd = syscall(__NR_pidfd_open, pid, flags);
  8180. if (fd < 0) {
  8181. return posix_error();
  8182. }
  8183. return PyLong_FromLong(fd);
  8184. }
  8185. #endif
  8186. #ifdef HAVE_SETNS
  8187. /*[clinic input]
  8188. os.setns
  8189. fd: fildes
  8190. A file descriptor to a namespace.
  8191. nstype: int = 0
  8192. Type of namespace.
  8193. Move the calling thread into different namespaces.
  8194. [clinic start generated code]*/
  8195. static PyObject *
  8196. os_setns_impl(PyObject *module, int fd, int nstype)
  8197. /*[clinic end generated code: output=5dbd055bfb66ecd0 input=42787871226bf3ee]*/
  8198. {
  8199. int res;
  8200. Py_BEGIN_ALLOW_THREADS
  8201. res = setns(fd, nstype);
  8202. Py_END_ALLOW_THREADS
  8203. if (res != 0) {
  8204. return posix_error();
  8205. }
  8206. Py_RETURN_NONE;
  8207. }
  8208. #endif
  8209. #ifdef HAVE_UNSHARE
  8210. /*[clinic input]
  8211. os.unshare
  8212. flags: int
  8213. Namespaces to be unshared.
  8214. Disassociate parts of a process (or thread) execution context.
  8215. [clinic start generated code]*/
  8216. static PyObject *
  8217. os_unshare_impl(PyObject *module, int flags)
  8218. /*[clinic end generated code: output=1b3177906dd237ee input=9e065db3232b8b1b]*/
  8219. {
  8220. int res;
  8221. Py_BEGIN_ALLOW_THREADS
  8222. res = unshare(flags);
  8223. Py_END_ALLOW_THREADS
  8224. if (res != 0) {
  8225. return posix_error();
  8226. }
  8227. Py_RETURN_NONE;
  8228. }
  8229. #endif
  8230. #if defined(HAVE_READLINK) || defined(MS_WINDOWS)
  8231. /*[clinic input]
  8232. os.readlink
  8233. path: path_t
  8234. *
  8235. dir_fd: dir_fd(requires='readlinkat') = None
  8236. Return a string representing the path to which the symbolic link points.
  8237. If dir_fd is not None, it should be a file descriptor open to a directory,
  8238. and path should be relative; path will then be relative to that directory.
  8239. dir_fd may not be implemented on your platform. If it is unavailable,
  8240. using it will raise a NotImplementedError.
  8241. [clinic start generated code]*/
  8242. static PyObject *
  8243. os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
  8244. /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
  8245. {
  8246. #if defined(HAVE_READLINK)
  8247. char buffer[MAXPATHLEN+1];
  8248. ssize_t length;
  8249. #ifdef HAVE_READLINKAT
  8250. int readlinkat_unavailable = 0;
  8251. #endif
  8252. Py_BEGIN_ALLOW_THREADS
  8253. #ifdef HAVE_READLINKAT
  8254. if (dir_fd != DEFAULT_DIR_FD) {
  8255. if (HAVE_READLINKAT_RUNTIME) {
  8256. length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
  8257. } else {
  8258. readlinkat_unavailable = 1;
  8259. }
  8260. } else
  8261. #endif
  8262. length = readlink(path->narrow, buffer, MAXPATHLEN);
  8263. Py_END_ALLOW_THREADS
  8264. #ifdef HAVE_READLINKAT
  8265. if (readlinkat_unavailable) {
  8266. argument_unavailable_error(NULL, "dir_fd");
  8267. return NULL;
  8268. }
  8269. #endif
  8270. if (length < 0) {
  8271. return path_error(path);
  8272. }
  8273. buffer[length] = '\0';
  8274. if (PyUnicode_Check(path->object))
  8275. return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
  8276. else
  8277. return PyBytes_FromStringAndSize(buffer, length);
  8278. #elif defined(MS_WINDOWS)
  8279. DWORD n_bytes_returned;
  8280. DWORD io_result = 0;
  8281. HANDLE reparse_point_handle;
  8282. char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
  8283. _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
  8284. PyObject *result = NULL;
  8285. /* First get a handle to the reparse point */
  8286. Py_BEGIN_ALLOW_THREADS
  8287. reparse_point_handle = CreateFileW(
  8288. path->wide,
  8289. 0,
  8290. 0,
  8291. 0,
  8292. OPEN_EXISTING,
  8293. FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
  8294. 0);
  8295. if (reparse_point_handle != INVALID_HANDLE_VALUE) {
  8296. /* New call DeviceIoControl to read the reparse point */
  8297. io_result = DeviceIoControl(
  8298. reparse_point_handle,
  8299. FSCTL_GET_REPARSE_POINT,
  8300. 0, 0, /* in buffer */
  8301. target_buffer, sizeof(target_buffer),
  8302. &n_bytes_returned,
  8303. 0 /* we're not using OVERLAPPED_IO */
  8304. );
  8305. CloseHandle(reparse_point_handle);
  8306. }
  8307. Py_END_ALLOW_THREADS
  8308. if (io_result == 0) {
  8309. return path_error(path);
  8310. }
  8311. wchar_t *name = NULL;
  8312. Py_ssize_t nameLen = 0;
  8313. if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
  8314. {
  8315. name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
  8316. rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
  8317. nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
  8318. }
  8319. else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
  8320. {
  8321. name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
  8322. rdb->MountPointReparseBuffer.SubstituteNameOffset);
  8323. nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
  8324. }
  8325. else
  8326. {
  8327. PyErr_SetString(PyExc_ValueError, "not a symbolic link");
  8328. }
  8329. if (name) {
  8330. if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
  8331. /* Our buffer is mutable, so this is okay */
  8332. name[1] = L'\\';
  8333. }
  8334. result = PyUnicode_FromWideChar(name, nameLen);
  8335. if (result && PyBytes_Check(path->object)) {
  8336. Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
  8337. }
  8338. }
  8339. return result;
  8340. #endif
  8341. }
  8342. #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
  8343. #if defined(MS_WINDOWS)
  8344. /* Remove the last portion of the path - return 0 on success */
  8345. static int
  8346. _dirnameW(WCHAR *path)
  8347. {
  8348. WCHAR *ptr;
  8349. size_t length = wcsnlen_s(path, MAX_PATH);
  8350. if (length == MAX_PATH) {
  8351. return -1;
  8352. }
  8353. /* walk the path from the end until a backslash is encountered */
  8354. for(ptr = path + length; ptr != path; ptr--) {
  8355. if (*ptr == L'\\' || *ptr == L'/') {
  8356. break;
  8357. }
  8358. }
  8359. *ptr = 0;
  8360. return 0;
  8361. }
  8362. #endif
  8363. #ifdef HAVE_SYMLINK
  8364. #if defined(MS_WINDOWS)
  8365. /* Is this path absolute? */
  8366. static int
  8367. _is_absW(const WCHAR *path)
  8368. {
  8369. return path[0] == L'\\' || path[0] == L'/' ||
  8370. (path[0] && path[1] == L':');
  8371. }
  8372. /* join root and rest with a backslash - return 0 on success */
  8373. static int
  8374. _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
  8375. {
  8376. if (_is_absW(rest)) {
  8377. return wcscpy_s(dest_path, MAX_PATH, rest);
  8378. }
  8379. if (wcscpy_s(dest_path, MAX_PATH, root)) {
  8380. return -1;
  8381. }
  8382. if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
  8383. return -1;
  8384. }
  8385. return wcscat_s(dest_path, MAX_PATH, rest);
  8386. }
  8387. /* Return True if the path at src relative to dest is a directory */
  8388. static int
  8389. _check_dirW(LPCWSTR src, LPCWSTR dest)
  8390. {
  8391. WIN32_FILE_ATTRIBUTE_DATA src_info;
  8392. WCHAR dest_parent[MAX_PATH];
  8393. WCHAR src_resolved[MAX_PATH] = L"";
  8394. /* dest_parent = os.path.dirname(dest) */
  8395. if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
  8396. _dirnameW(dest_parent)) {
  8397. return 0;
  8398. }
  8399. /* src_resolved = os.path.join(dest_parent, src) */
  8400. if (_joinW(src_resolved, dest_parent, src)) {
  8401. return 0;
  8402. }
  8403. return (
  8404. GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
  8405. && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
  8406. );
  8407. }
  8408. #endif
  8409. /*[clinic input]
  8410. os.symlink
  8411. src: path_t
  8412. dst: path_t
  8413. target_is_directory: bool = False
  8414. *
  8415. dir_fd: dir_fd(requires='symlinkat')=None
  8416. # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
  8417. Create a symbolic link pointing to src named dst.
  8418. target_is_directory is required on Windows if the target is to be
  8419. interpreted as a directory. (On Windows, symlink requires
  8420. Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
  8421. target_is_directory is ignored on non-Windows platforms.
  8422. If dir_fd is not None, it should be a file descriptor open to a directory,
  8423. and path should be relative; path will then be relative to that directory.
  8424. dir_fd may not be implemented on your platform.
  8425. If it is unavailable, using it will raise a NotImplementedError.
  8426. [clinic start generated code]*/
  8427. static PyObject *
  8428. os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
  8429. int target_is_directory, int dir_fd)
  8430. /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
  8431. {
  8432. #ifdef MS_WINDOWS
  8433. DWORD result;
  8434. DWORD flags = 0;
  8435. /* Assumed true, set to false if detected to not be available. */
  8436. static int windows_has_symlink_unprivileged_flag = TRUE;
  8437. #else
  8438. int result;
  8439. #ifdef HAVE_SYMLINKAT
  8440. int symlinkat_unavailable = 0;
  8441. #endif
  8442. #endif
  8443. if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
  8444. dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
  8445. return NULL;
  8446. }
  8447. #ifdef MS_WINDOWS
  8448. if (windows_has_symlink_unprivileged_flag) {
  8449. /* Allow non-admin symlinks if system allows it. */
  8450. flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
  8451. }
  8452. Py_BEGIN_ALLOW_THREADS
  8453. _Py_BEGIN_SUPPRESS_IPH
  8454. /* if src is a directory, ensure flags==1 (target_is_directory bit) */
  8455. if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
  8456. flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
  8457. }
  8458. result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
  8459. _Py_END_SUPPRESS_IPH
  8460. Py_END_ALLOW_THREADS
  8461. if (windows_has_symlink_unprivileged_flag && !result &&
  8462. ERROR_INVALID_PARAMETER == GetLastError()) {
  8463. Py_BEGIN_ALLOW_THREADS
  8464. _Py_BEGIN_SUPPRESS_IPH
  8465. /* This error might be caused by
  8466. SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
  8467. Try again, and update windows_has_symlink_unprivileged_flag if we
  8468. are successful this time.
  8469. NOTE: There is a risk of a race condition here if there are other
  8470. conditions than the flag causing ERROR_INVALID_PARAMETER, and
  8471. another process (or thread) changes that condition in between our
  8472. calls to CreateSymbolicLink.
  8473. */
  8474. flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
  8475. result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
  8476. _Py_END_SUPPRESS_IPH
  8477. Py_END_ALLOW_THREADS
  8478. if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
  8479. windows_has_symlink_unprivileged_flag = FALSE;
  8480. }
  8481. }
  8482. if (!result)
  8483. return path_error2(src, dst);
  8484. #else
  8485. if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
  8486. PyErr_SetString(PyExc_ValueError,
  8487. "symlink: src and dst must be the same type");
  8488. return NULL;
  8489. }
  8490. Py_BEGIN_ALLOW_THREADS
  8491. #ifdef HAVE_SYMLINKAT
  8492. if (dir_fd != DEFAULT_DIR_FD) {
  8493. if (HAVE_SYMLINKAT_RUNTIME) {
  8494. result = symlinkat(src->narrow, dir_fd, dst->narrow);
  8495. } else {
  8496. symlinkat_unavailable = 1;
  8497. }
  8498. } else
  8499. #endif
  8500. result = symlink(src->narrow, dst->narrow);
  8501. Py_END_ALLOW_THREADS
  8502. #ifdef HAVE_SYMLINKAT
  8503. if (symlinkat_unavailable) {
  8504. argument_unavailable_error(NULL, "dir_fd");
  8505. return NULL;
  8506. }
  8507. #endif
  8508. if (result)
  8509. return path_error2(src, dst);
  8510. #endif
  8511. Py_RETURN_NONE;
  8512. }
  8513. #endif /* HAVE_SYMLINK */
  8514. static PyStructSequence_Field times_result_fields[] = {
  8515. {"user", "user time"},
  8516. {"system", "system time"},
  8517. {"children_user", "user time of children"},
  8518. {"children_system", "system time of children"},
  8519. {"elapsed", "elapsed time since an arbitrary point in the past"},
  8520. {NULL}
  8521. };
  8522. PyDoc_STRVAR(times_result__doc__,
  8523. "times_result: Result from os.times().\n\n\
  8524. This object may be accessed either as a tuple of\n\
  8525. (user, system, children_user, children_system, elapsed),\n\
  8526. or via the attributes user, system, children_user, children_system,\n\
  8527. and elapsed.\n\
  8528. \n\
  8529. See os.times for more information.");
  8530. static PyStructSequence_Desc times_result_desc = {
  8531. "times_result", /* name */
  8532. times_result__doc__, /* doc */
  8533. times_result_fields,
  8534. 5
  8535. };
  8536. #ifdef MS_WINDOWS
  8537. #define HAVE_TIMES /* mandatory, for the method table */
  8538. #endif
  8539. #ifdef HAVE_TIMES
  8540. static PyObject *
  8541. build_times_result(PyObject *module, double user, double system,
  8542. double children_user, double children_system,
  8543. double elapsed)
  8544. {
  8545. PyObject *TimesResultType = get_posix_state(module)->TimesResultType;
  8546. PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
  8547. if (value == NULL)
  8548. return NULL;
  8549. #define SET(i, field) \
  8550. { \
  8551. PyObject *o = PyFloat_FromDouble(field); \
  8552. if (!o) { \
  8553. Py_DECREF(value); \
  8554. return NULL; \
  8555. } \
  8556. PyStructSequence_SET_ITEM(value, i, o); \
  8557. } \
  8558. SET(0, user);
  8559. SET(1, system);
  8560. SET(2, children_user);
  8561. SET(3, children_system);
  8562. SET(4, elapsed);
  8563. #undef SET
  8564. return value;
  8565. }
  8566. /*[clinic input]
  8567. os.times
  8568. Return a collection containing process timing information.
  8569. The object returned behaves like a named tuple with these fields:
  8570. (utime, stime, cutime, cstime, elapsed_time)
  8571. All fields are floating-point numbers.
  8572. [clinic start generated code]*/
  8573. static PyObject *
  8574. os_times_impl(PyObject *module)
  8575. /*[clinic end generated code: output=35f640503557d32a input=8dbfe33a2dcc3df3]*/
  8576. #ifdef MS_WINDOWS
  8577. {
  8578. FILETIME create, exit, kernel, user;
  8579. HANDLE hProc;
  8580. hProc = GetCurrentProcess();
  8581. GetProcessTimes(hProc, &create, &exit, &kernel, &user);
  8582. /* The fields of a FILETIME structure are the hi and lo part
  8583. of a 64-bit value expressed in 100 nanosecond units.
  8584. 1e7 is one second in such units; 1e-7 the inverse.
  8585. 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
  8586. */
  8587. return build_times_result(module,
  8588. (double)(user.dwHighDateTime*429.4967296 +
  8589. user.dwLowDateTime*1e-7),
  8590. (double)(kernel.dwHighDateTime*429.4967296 +
  8591. kernel.dwLowDateTime*1e-7),
  8592. (double)0,
  8593. (double)0,
  8594. (double)0);
  8595. }
  8596. #else /* MS_WINDOWS */
  8597. {
  8598. struct tms t;
  8599. clock_t c;
  8600. errno = 0;
  8601. c = times(&t);
  8602. if (c == (clock_t) -1) {
  8603. return posix_error();
  8604. }
  8605. assert(_PyRuntime.time.ticks_per_second_initialized);
  8606. #define ticks_per_second _PyRuntime.time.ticks_per_second
  8607. return build_times_result(module,
  8608. (double)t.tms_utime / ticks_per_second,
  8609. (double)t.tms_stime / ticks_per_second,
  8610. (double)t.tms_cutime / ticks_per_second,
  8611. (double)t.tms_cstime / ticks_per_second,
  8612. (double)c / ticks_per_second);
  8613. #undef ticks_per_second
  8614. }
  8615. #endif /* MS_WINDOWS */
  8616. #endif /* HAVE_TIMES */
  8617. #ifdef HAVE_GETSID
  8618. /*[clinic input]
  8619. os.getsid
  8620. pid: pid_t
  8621. /
  8622. Call the system call getsid(pid) and return the result.
  8623. [clinic start generated code]*/
  8624. static PyObject *
  8625. os_getsid_impl(PyObject *module, pid_t pid)
  8626. /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
  8627. {
  8628. int sid;
  8629. sid = getsid(pid);
  8630. if (sid < 0)
  8631. return posix_error();
  8632. return PyLong_FromLong((long)sid);
  8633. }
  8634. #endif /* HAVE_GETSID */
  8635. #ifdef HAVE_SETSID
  8636. /*[clinic input]
  8637. os.setsid
  8638. Call the system call setsid().
  8639. [clinic start generated code]*/
  8640. static PyObject *
  8641. os_setsid_impl(PyObject *module)
  8642. /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
  8643. {
  8644. if (setsid() < 0)
  8645. return posix_error();
  8646. Py_RETURN_NONE;
  8647. }
  8648. #endif /* HAVE_SETSID */
  8649. #ifdef HAVE_SETPGID
  8650. /*[clinic input]
  8651. os.setpgid
  8652. pid: pid_t
  8653. pgrp: pid_t
  8654. /
  8655. Call the system call setpgid(pid, pgrp).
  8656. [clinic start generated code]*/
  8657. static PyObject *
  8658. os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
  8659. /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
  8660. {
  8661. if (setpgid(pid, pgrp) < 0)
  8662. return posix_error();
  8663. Py_RETURN_NONE;
  8664. }
  8665. #endif /* HAVE_SETPGID */
  8666. #ifdef HAVE_TCGETPGRP
  8667. /*[clinic input]
  8668. os.tcgetpgrp
  8669. fd: int
  8670. /
  8671. Return the process group associated with the terminal specified by fd.
  8672. [clinic start generated code]*/
  8673. static PyObject *
  8674. os_tcgetpgrp_impl(PyObject *module, int fd)
  8675. /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
  8676. {
  8677. pid_t pgid = tcgetpgrp(fd);
  8678. if (pgid < 0)
  8679. return posix_error();
  8680. return PyLong_FromPid(pgid);
  8681. }
  8682. #endif /* HAVE_TCGETPGRP */
  8683. #ifdef HAVE_TCSETPGRP
  8684. /*[clinic input]
  8685. os.tcsetpgrp
  8686. fd: int
  8687. pgid: pid_t
  8688. /
  8689. Set the process group associated with the terminal specified by fd.
  8690. [clinic start generated code]*/
  8691. static PyObject *
  8692. os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
  8693. /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
  8694. {
  8695. if (tcsetpgrp(fd, pgid) < 0)
  8696. return posix_error();
  8697. Py_RETURN_NONE;
  8698. }
  8699. #endif /* HAVE_TCSETPGRP */
  8700. /* Functions acting on file descriptors */
  8701. #ifdef O_CLOEXEC
  8702. extern int _Py_open_cloexec_works;
  8703. #endif
  8704. /*[clinic input]
  8705. os.open -> int
  8706. path: path_t
  8707. flags: int
  8708. mode: int = 0o777
  8709. *
  8710. dir_fd: dir_fd(requires='openat') = None
  8711. # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
  8712. Open a file for low level IO. Returns a file descriptor (integer).
  8713. If dir_fd is not None, it should be a file descriptor open to a directory,
  8714. and path should be relative; path will then be relative to that directory.
  8715. dir_fd may not be implemented on your platform.
  8716. If it is unavailable, using it will raise a NotImplementedError.
  8717. [clinic start generated code]*/
  8718. static int
  8719. os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
  8720. /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
  8721. {
  8722. int fd;
  8723. int async_err = 0;
  8724. #ifdef HAVE_OPENAT
  8725. int openat_unavailable = 0;
  8726. #endif
  8727. #ifdef O_CLOEXEC
  8728. int *atomic_flag_works = &_Py_open_cloexec_works;
  8729. #elif !defined(MS_WINDOWS)
  8730. int *atomic_flag_works = NULL;
  8731. #endif
  8732. #ifdef MS_WINDOWS
  8733. flags |= O_NOINHERIT;
  8734. #elif defined(O_CLOEXEC)
  8735. flags |= O_CLOEXEC;
  8736. #endif
  8737. if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
  8738. return -1;
  8739. }
  8740. _Py_BEGIN_SUPPRESS_IPH
  8741. do {
  8742. Py_BEGIN_ALLOW_THREADS
  8743. #ifdef MS_WINDOWS
  8744. fd = _wopen(path->wide, flags, mode);
  8745. #else
  8746. #ifdef HAVE_OPENAT
  8747. if (dir_fd != DEFAULT_DIR_FD) {
  8748. if (HAVE_OPENAT_RUNTIME) {
  8749. fd = openat(dir_fd, path->narrow, flags, mode);
  8750. } else {
  8751. openat_unavailable = 1;
  8752. fd = -1;
  8753. }
  8754. } else
  8755. #endif /* HAVE_OPENAT */
  8756. fd = open(path->narrow, flags, mode);
  8757. #endif /* !MS_WINDOWS */
  8758. Py_END_ALLOW_THREADS
  8759. } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  8760. _Py_END_SUPPRESS_IPH
  8761. #ifdef HAVE_OPENAT
  8762. if (openat_unavailable) {
  8763. argument_unavailable_error(NULL, "dir_fd");
  8764. return -1;
  8765. }
  8766. #endif
  8767. if (fd < 0) {
  8768. if (!async_err)
  8769. PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
  8770. return -1;
  8771. }
  8772. #ifndef MS_WINDOWS
  8773. if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
  8774. close(fd);
  8775. return -1;
  8776. }
  8777. #endif
  8778. return fd;
  8779. }
  8780. /*[clinic input]
  8781. os.close
  8782. fd: int
  8783. Close a file descriptor.
  8784. [clinic start generated code]*/
  8785. static PyObject *
  8786. os_close_impl(PyObject *module, int fd)
  8787. /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
  8788. {
  8789. int res;
  8790. /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
  8791. * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
  8792. * for more details.
  8793. */
  8794. Py_BEGIN_ALLOW_THREADS
  8795. _Py_BEGIN_SUPPRESS_IPH
  8796. res = close(fd);
  8797. _Py_END_SUPPRESS_IPH
  8798. Py_END_ALLOW_THREADS
  8799. if (res < 0)
  8800. return posix_error();
  8801. Py_RETURN_NONE;
  8802. }
  8803. /*[clinic input]
  8804. os.closerange
  8805. fd_low: int
  8806. fd_high: int
  8807. /
  8808. Closes all file descriptors in [fd_low, fd_high), ignoring errors.
  8809. [clinic start generated code]*/
  8810. static PyObject *
  8811. os_closerange_impl(PyObject *module, int fd_low, int fd_high)
  8812. /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
  8813. {
  8814. Py_BEGIN_ALLOW_THREADS
  8815. _Py_closerange(fd_low, fd_high - 1);
  8816. Py_END_ALLOW_THREADS
  8817. Py_RETURN_NONE;
  8818. }
  8819. /*[clinic input]
  8820. os.dup -> int
  8821. fd: int
  8822. /
  8823. Return a duplicate of a file descriptor.
  8824. [clinic start generated code]*/
  8825. static int
  8826. os_dup_impl(PyObject *module, int fd)
  8827. /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
  8828. {
  8829. return _Py_dup(fd);
  8830. }
  8831. // dup2() is either provided by libc or dup2.c with AC_REPLACE_FUNCS().
  8832. // dup2.c provides working dup2() if and only if F_DUPFD is available.
  8833. #if (defined(HAVE_DUP3) || defined(F_DUPFD) || defined(MS_WINDOWS))
  8834. /*[clinic input]
  8835. os.dup2 -> int
  8836. fd: int
  8837. fd2: int
  8838. inheritable: bool=True
  8839. Duplicate file descriptor.
  8840. [clinic start generated code]*/
  8841. static int
  8842. os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
  8843. /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
  8844. {
  8845. int res = 0;
  8846. #if defined(HAVE_DUP3) && \
  8847. !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
  8848. /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
  8849. static int dup3_works = -1;
  8850. #endif
  8851. /* dup2() can fail with EINTR if the target FD is already open, because it
  8852. * then has to be closed. See os_close_impl() for why we don't handle EINTR
  8853. * upon close(), and therefore below.
  8854. */
  8855. #ifdef MS_WINDOWS
  8856. Py_BEGIN_ALLOW_THREADS
  8857. _Py_BEGIN_SUPPRESS_IPH
  8858. res = dup2(fd, fd2);
  8859. _Py_END_SUPPRESS_IPH
  8860. Py_END_ALLOW_THREADS
  8861. if (res < 0) {
  8862. posix_error();
  8863. return -1;
  8864. }
  8865. res = fd2; // msvcrt dup2 returns 0 on success.
  8866. /* Character files like console cannot be make non-inheritable */
  8867. if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
  8868. close(fd2);
  8869. return -1;
  8870. }
  8871. #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
  8872. Py_BEGIN_ALLOW_THREADS
  8873. if (!inheritable)
  8874. res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
  8875. else
  8876. res = dup2(fd, fd2);
  8877. Py_END_ALLOW_THREADS
  8878. if (res < 0) {
  8879. posix_error();
  8880. return -1;
  8881. }
  8882. #else
  8883. #ifdef HAVE_DUP3
  8884. if (!inheritable && dup3_works != 0) {
  8885. Py_BEGIN_ALLOW_THREADS
  8886. res = dup3(fd, fd2, O_CLOEXEC);
  8887. Py_END_ALLOW_THREADS
  8888. if (res < 0) {
  8889. if (dup3_works == -1)
  8890. dup3_works = (errno != ENOSYS);
  8891. if (dup3_works) {
  8892. posix_error();
  8893. return -1;
  8894. }
  8895. }
  8896. }
  8897. if (inheritable || dup3_works == 0)
  8898. {
  8899. #endif
  8900. Py_BEGIN_ALLOW_THREADS
  8901. res = dup2(fd, fd2);
  8902. Py_END_ALLOW_THREADS
  8903. if (res < 0) {
  8904. posix_error();
  8905. return -1;
  8906. }
  8907. if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
  8908. close(fd2);
  8909. return -1;
  8910. }
  8911. #ifdef HAVE_DUP3
  8912. }
  8913. #endif
  8914. #endif
  8915. return res;
  8916. }
  8917. #endif
  8918. #ifdef HAVE_LOCKF
  8919. /*[clinic input]
  8920. os.lockf
  8921. fd: int
  8922. An open file descriptor.
  8923. command: int
  8924. One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
  8925. length: Py_off_t
  8926. The number of bytes to lock, starting at the current position.
  8927. /
  8928. Apply, test or remove a POSIX lock on an open file descriptor.
  8929. [clinic start generated code]*/
  8930. static PyObject *
  8931. os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
  8932. /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
  8933. {
  8934. int res;
  8935. if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
  8936. return NULL;
  8937. }
  8938. Py_BEGIN_ALLOW_THREADS
  8939. res = lockf(fd, command, length);
  8940. Py_END_ALLOW_THREADS
  8941. if (res < 0)
  8942. return posix_error();
  8943. Py_RETURN_NONE;
  8944. }
  8945. #endif /* HAVE_LOCKF */
  8946. /*[clinic input]
  8947. os.lseek -> Py_off_t
  8948. fd: int
  8949. An open file descriptor, as returned by os.open().
  8950. position: Py_off_t
  8951. Position, interpreted relative to 'whence'.
  8952. whence as how: int
  8953. The relative position to seek from. Valid values are:
  8954. - SEEK_SET: seek from the start of the file.
  8955. - SEEK_CUR: seek from the current file position.
  8956. - SEEK_END: seek from the end of the file.
  8957. /
  8958. Set the position of a file descriptor. Return the new position.
  8959. The return value is the number of bytes relative to the beginning of the file.
  8960. [clinic start generated code]*/
  8961. static Py_off_t
  8962. os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
  8963. /*[clinic end generated code: output=971e1efb6b30bd2f input=f096e754c5367504]*/
  8964. {
  8965. Py_off_t result;
  8966. #ifdef SEEK_SET
  8967. /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
  8968. switch (how) {
  8969. case 0: how = SEEK_SET; break;
  8970. case 1: how = SEEK_CUR; break;
  8971. case 2: how = SEEK_END; break;
  8972. }
  8973. #endif /* SEEK_END */
  8974. Py_BEGIN_ALLOW_THREADS
  8975. _Py_BEGIN_SUPPRESS_IPH
  8976. #ifdef MS_WINDOWS
  8977. result = _lseeki64(fd, position, how);
  8978. #else
  8979. result = lseek(fd, position, how);
  8980. #endif
  8981. _Py_END_SUPPRESS_IPH
  8982. Py_END_ALLOW_THREADS
  8983. if (result < 0)
  8984. posix_error();
  8985. return result;
  8986. }
  8987. /*[clinic input]
  8988. os.read
  8989. fd: int
  8990. length: Py_ssize_t
  8991. /
  8992. Read from a file descriptor. Returns a bytes object.
  8993. [clinic start generated code]*/
  8994. static PyObject *
  8995. os_read_impl(PyObject *module, int fd, Py_ssize_t length)
  8996. /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
  8997. {
  8998. Py_ssize_t n;
  8999. PyObject *buffer;
  9000. if (length < 0) {
  9001. errno = EINVAL;
  9002. return posix_error();
  9003. }
  9004. length = Py_MIN(length, _PY_READ_MAX);
  9005. buffer = PyBytes_FromStringAndSize((char *)NULL, length);
  9006. if (buffer == NULL)
  9007. return NULL;
  9008. n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
  9009. if (n == -1) {
  9010. Py_DECREF(buffer);
  9011. return NULL;
  9012. }
  9013. if (n != length)
  9014. _PyBytes_Resize(&buffer, n);
  9015. return buffer;
  9016. }
  9017. #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
  9018. || defined(__APPLE__))) \
  9019. || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
  9020. || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
  9021. static int
  9022. iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
  9023. {
  9024. Py_ssize_t i, j;
  9025. *iov = PyMem_New(struct iovec, cnt);
  9026. if (*iov == NULL) {
  9027. PyErr_NoMemory();
  9028. return -1;
  9029. }
  9030. *buf = PyMem_New(Py_buffer, cnt);
  9031. if (*buf == NULL) {
  9032. PyMem_Free(*iov);
  9033. PyErr_NoMemory();
  9034. return -1;
  9035. }
  9036. for (i = 0; i < cnt; i++) {
  9037. PyObject *item = PySequence_GetItem(seq, i);
  9038. if (item == NULL)
  9039. goto fail;
  9040. if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
  9041. Py_DECREF(item);
  9042. goto fail;
  9043. }
  9044. Py_DECREF(item);
  9045. (*iov)[i].iov_base = (*buf)[i].buf;
  9046. (*iov)[i].iov_len = (*buf)[i].len;
  9047. }
  9048. return 0;
  9049. fail:
  9050. PyMem_Free(*iov);
  9051. for (j = 0; j < i; j++) {
  9052. PyBuffer_Release(&(*buf)[j]);
  9053. }
  9054. PyMem_Free(*buf);
  9055. return -1;
  9056. }
  9057. static void
  9058. iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
  9059. {
  9060. int i;
  9061. PyMem_Free(iov);
  9062. for (i = 0; i < cnt; i++) {
  9063. PyBuffer_Release(&buf[i]);
  9064. }
  9065. PyMem_Free(buf);
  9066. }
  9067. #endif
  9068. #ifdef HAVE_READV
  9069. /*[clinic input]
  9070. os.readv -> Py_ssize_t
  9071. fd: int
  9072. buffers: object
  9073. /
  9074. Read from a file descriptor fd into an iterable of buffers.
  9075. The buffers should be mutable buffers accepting bytes.
  9076. readv will transfer data into each buffer until it is full
  9077. and then move on to the next buffer in the sequence to hold
  9078. the rest of the data.
  9079. readv returns the total number of bytes read,
  9080. which may be less than the total capacity of all the buffers.
  9081. [clinic start generated code]*/
  9082. static Py_ssize_t
  9083. os_readv_impl(PyObject *module, int fd, PyObject *buffers)
  9084. /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
  9085. {
  9086. Py_ssize_t cnt, n;
  9087. int async_err = 0;
  9088. struct iovec *iov;
  9089. Py_buffer *buf;
  9090. if (!PySequence_Check(buffers)) {
  9091. PyErr_SetString(PyExc_TypeError,
  9092. "readv() arg 2 must be a sequence");
  9093. return -1;
  9094. }
  9095. cnt = PySequence_Size(buffers);
  9096. if (cnt < 0)
  9097. return -1;
  9098. if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
  9099. return -1;
  9100. do {
  9101. Py_BEGIN_ALLOW_THREADS
  9102. n = readv(fd, iov, cnt);
  9103. Py_END_ALLOW_THREADS
  9104. } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9105. int saved_errno = errno;
  9106. iov_cleanup(iov, buf, cnt);
  9107. if (n < 0) {
  9108. if (!async_err) {
  9109. errno = saved_errno;
  9110. posix_error();
  9111. }
  9112. return -1;
  9113. }
  9114. return n;
  9115. }
  9116. #endif /* HAVE_READV */
  9117. #ifdef HAVE_PREAD
  9118. /*[clinic input]
  9119. os.pread
  9120. fd: int
  9121. length: Py_ssize_t
  9122. offset: Py_off_t
  9123. /
  9124. Read a number of bytes from a file descriptor starting at a particular offset.
  9125. Read length bytes from file descriptor fd, starting at offset bytes from
  9126. the beginning of the file. The file offset remains unchanged.
  9127. [clinic start generated code]*/
  9128. static PyObject *
  9129. os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
  9130. /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
  9131. {
  9132. Py_ssize_t n;
  9133. int async_err = 0;
  9134. PyObject *buffer;
  9135. if (length < 0) {
  9136. errno = EINVAL;
  9137. return posix_error();
  9138. }
  9139. buffer = PyBytes_FromStringAndSize((char *)NULL, length);
  9140. if (buffer == NULL)
  9141. return NULL;
  9142. do {
  9143. Py_BEGIN_ALLOW_THREADS
  9144. _Py_BEGIN_SUPPRESS_IPH
  9145. n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
  9146. _Py_END_SUPPRESS_IPH
  9147. Py_END_ALLOW_THREADS
  9148. } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9149. if (n < 0) {
  9150. if (!async_err) {
  9151. posix_error();
  9152. }
  9153. Py_DECREF(buffer);
  9154. return NULL;
  9155. }
  9156. if (n != length)
  9157. _PyBytes_Resize(&buffer, n);
  9158. return buffer;
  9159. }
  9160. #endif /* HAVE_PREAD */
  9161. #if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
  9162. /*[clinic input]
  9163. os.preadv -> Py_ssize_t
  9164. fd: int
  9165. buffers: object
  9166. offset: Py_off_t
  9167. flags: int = 0
  9168. /
  9169. Reads from a file descriptor into a number of mutable bytes-like objects.
  9170. Combines the functionality of readv() and pread(). As readv(), it will
  9171. transfer data into each buffer until it is full and then move on to the next
  9172. buffer in the sequence to hold the rest of the data. Its fourth argument,
  9173. specifies the file offset at which the input operation is to be performed. It
  9174. will return the total number of bytes read (which can be less than the total
  9175. capacity of all the objects).
  9176. The flags argument contains a bitwise OR of zero or more of the following flags:
  9177. - RWF_HIPRI
  9178. - RWF_NOWAIT
  9179. Using non-zero flags requires Linux 4.6 or newer.
  9180. [clinic start generated code]*/
  9181. static Py_ssize_t
  9182. os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
  9183. int flags)
  9184. /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
  9185. {
  9186. Py_ssize_t cnt, n;
  9187. int async_err = 0;
  9188. struct iovec *iov;
  9189. Py_buffer *buf;
  9190. if (!PySequence_Check(buffers)) {
  9191. PyErr_SetString(PyExc_TypeError,
  9192. "preadv2() arg 2 must be a sequence");
  9193. return -1;
  9194. }
  9195. cnt = PySequence_Size(buffers);
  9196. if (cnt < 0) {
  9197. return -1;
  9198. }
  9199. #ifndef HAVE_PREADV2
  9200. if(flags != 0) {
  9201. argument_unavailable_error("preadv2", "flags");
  9202. return -1;
  9203. }
  9204. #endif
  9205. if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
  9206. return -1;
  9207. }
  9208. #ifdef HAVE_PREADV2
  9209. do {
  9210. Py_BEGIN_ALLOW_THREADS
  9211. _Py_BEGIN_SUPPRESS_IPH
  9212. n = preadv2(fd, iov, cnt, offset, flags);
  9213. _Py_END_SUPPRESS_IPH
  9214. Py_END_ALLOW_THREADS
  9215. } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9216. #else
  9217. do {
  9218. #if defined(__APPLE__) && defined(__clang__)
  9219. /* This entire function will be removed from the module dict when the API
  9220. * is not available.
  9221. */
  9222. #pragma clang diagnostic push
  9223. #pragma clang diagnostic ignored "-Wunguarded-availability"
  9224. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  9225. #endif
  9226. Py_BEGIN_ALLOW_THREADS
  9227. _Py_BEGIN_SUPPRESS_IPH
  9228. n = preadv(fd, iov, cnt, offset);
  9229. _Py_END_SUPPRESS_IPH
  9230. Py_END_ALLOW_THREADS
  9231. } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9232. #if defined(__APPLE__) && defined(__clang__)
  9233. #pragma clang diagnostic pop
  9234. #endif
  9235. #endif
  9236. int saved_errno = errno;
  9237. iov_cleanup(iov, buf, cnt);
  9238. if (n < 0) {
  9239. if (!async_err) {
  9240. errno = saved_errno;
  9241. posix_error();
  9242. }
  9243. return -1;
  9244. }
  9245. return n;
  9246. }
  9247. #endif /* HAVE_PREADV */
  9248. /*[clinic input]
  9249. os.write -> Py_ssize_t
  9250. fd: int
  9251. data: Py_buffer
  9252. /
  9253. Write a bytes object to a file descriptor.
  9254. [clinic start generated code]*/
  9255. static Py_ssize_t
  9256. os_write_impl(PyObject *module, int fd, Py_buffer *data)
  9257. /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
  9258. {
  9259. return _Py_write(fd, data->buf, data->len);
  9260. }
  9261. #ifdef HAVE_SENDFILE
  9262. #ifdef __APPLE__
  9263. /*[clinic input]
  9264. os.sendfile
  9265. out_fd: int
  9266. in_fd: int
  9267. offset: Py_off_t
  9268. count as sbytes: Py_off_t
  9269. headers: object(c_default="NULL") = ()
  9270. trailers: object(c_default="NULL") = ()
  9271. flags: int = 0
  9272. Copy count bytes from file descriptor in_fd to file descriptor out_fd.
  9273. [clinic start generated code]*/
  9274. static PyObject *
  9275. os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
  9276. Py_off_t sbytes, PyObject *headers, PyObject *trailers,
  9277. int flags)
  9278. /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/
  9279. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  9280. /*[clinic input]
  9281. os.sendfile
  9282. out_fd: int
  9283. in_fd: int
  9284. offset: Py_off_t
  9285. count: Py_ssize_t
  9286. headers: object(c_default="NULL") = ()
  9287. trailers: object(c_default="NULL") = ()
  9288. flags: int = 0
  9289. Copy count bytes from file descriptor in_fd to file descriptor out_fd.
  9290. [clinic start generated code]*/
  9291. static PyObject *
  9292. os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
  9293. Py_ssize_t count, PyObject *headers, PyObject *trailers,
  9294. int flags)
  9295. /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
  9296. #else
  9297. /*[clinic input]
  9298. os.sendfile
  9299. out_fd: int
  9300. in_fd: int
  9301. offset as offobj: object
  9302. count: Py_ssize_t
  9303. Copy count bytes from file descriptor in_fd to file descriptor out_fd.
  9304. [clinic start generated code]*/
  9305. static PyObject *
  9306. os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
  9307. Py_ssize_t count)
  9308. /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
  9309. #endif
  9310. {
  9311. Py_ssize_t ret;
  9312. int async_err = 0;
  9313. #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
  9314. #ifndef __APPLE__
  9315. off_t sbytes;
  9316. #endif
  9317. Py_buffer *hbuf, *tbuf;
  9318. struct sf_hdtr sf;
  9319. sf.headers = NULL;
  9320. sf.trailers = NULL;
  9321. if (headers != NULL) {
  9322. if (!PySequence_Check(headers)) {
  9323. PyErr_SetString(PyExc_TypeError,
  9324. "sendfile() headers must be a sequence");
  9325. return NULL;
  9326. } else {
  9327. Py_ssize_t i = PySequence_Size(headers);
  9328. if (i < 0)
  9329. return NULL;
  9330. if (i > INT_MAX) {
  9331. PyErr_SetString(PyExc_OverflowError,
  9332. "sendfile() header is too large");
  9333. return NULL;
  9334. }
  9335. if (i > 0) {
  9336. sf.hdr_cnt = (int)i;
  9337. if (iov_setup(&(sf.headers), &hbuf,
  9338. headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
  9339. return NULL;
  9340. #ifdef __APPLE__
  9341. for (i = 0; i < sf.hdr_cnt; i++) {
  9342. Py_ssize_t blen = sf.headers[i].iov_len;
  9343. # define OFF_T_MAX 0x7fffffffffffffff
  9344. if (sbytes >= OFF_T_MAX - blen) {
  9345. PyErr_SetString(PyExc_OverflowError,
  9346. "sendfile() header is too large");
  9347. return NULL;
  9348. }
  9349. sbytes += blen;
  9350. }
  9351. #endif
  9352. }
  9353. }
  9354. }
  9355. if (trailers != NULL) {
  9356. if (!PySequence_Check(trailers)) {
  9357. PyErr_SetString(PyExc_TypeError,
  9358. "sendfile() trailers must be a sequence");
  9359. return NULL;
  9360. } else {
  9361. Py_ssize_t i = PySequence_Size(trailers);
  9362. if (i < 0)
  9363. return NULL;
  9364. if (i > INT_MAX) {
  9365. PyErr_SetString(PyExc_OverflowError,
  9366. "sendfile() trailer is too large");
  9367. return NULL;
  9368. }
  9369. if (i > 0) {
  9370. sf.trl_cnt = (int)i;
  9371. if (iov_setup(&(sf.trailers), &tbuf,
  9372. trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
  9373. return NULL;
  9374. }
  9375. }
  9376. }
  9377. _Py_BEGIN_SUPPRESS_IPH
  9378. do {
  9379. Py_BEGIN_ALLOW_THREADS
  9380. #ifdef __APPLE__
  9381. ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
  9382. #else
  9383. ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags);
  9384. #endif
  9385. Py_END_ALLOW_THREADS
  9386. } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9387. _Py_END_SUPPRESS_IPH
  9388. int saved_errno = errno;
  9389. if (sf.headers != NULL)
  9390. iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
  9391. if (sf.trailers != NULL)
  9392. iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
  9393. if (ret < 0) {
  9394. if ((saved_errno == EAGAIN) || (saved_errno == EBUSY)) {
  9395. if (sbytes != 0) {
  9396. // some data has been sent
  9397. goto done;
  9398. }
  9399. // no data has been sent; upper application is supposed
  9400. // to retry on EAGAIN or EBUSY
  9401. }
  9402. if (!async_err) {
  9403. errno = saved_errno;
  9404. posix_error();
  9405. }
  9406. return NULL;
  9407. }
  9408. goto done;
  9409. done:
  9410. #if !defined(HAVE_LARGEFILE_SUPPORT)
  9411. return Py_BuildValue("l", sbytes);
  9412. #else
  9413. return Py_BuildValue("L", sbytes);
  9414. #endif
  9415. #else
  9416. #ifdef __linux__
  9417. if (offobj == Py_None) {
  9418. do {
  9419. Py_BEGIN_ALLOW_THREADS
  9420. ret = sendfile(out_fd, in_fd, NULL, count);
  9421. Py_END_ALLOW_THREADS
  9422. } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9423. if (ret < 0)
  9424. return (!async_err) ? posix_error() : NULL;
  9425. return Py_BuildValue("n", ret);
  9426. }
  9427. #endif
  9428. off_t offset;
  9429. if (!Py_off_t_converter(offobj, &offset))
  9430. return NULL;
  9431. #if defined(__sun) && defined(__SVR4)
  9432. // On Solaris, sendfile raises EINVAL rather than returning 0
  9433. // when the offset is equal or bigger than the in_fd size.
  9434. struct stat st;
  9435. do {
  9436. Py_BEGIN_ALLOW_THREADS
  9437. ret = fstat(in_fd, &st);
  9438. Py_END_ALLOW_THREADS
  9439. } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9440. if (ret < 0)
  9441. return (!async_err) ? posix_error() : NULL;
  9442. if (offset >= st.st_size) {
  9443. return Py_BuildValue("i", 0);
  9444. }
  9445. // On illumos specifically sendfile() may perform a partial write but
  9446. // return -1/an error (in one confirmed case the destination socket
  9447. // had a 5 second timeout set and errno was EAGAIN) and it's on the client
  9448. // code to check if the offset parameter was modified by sendfile().
  9449. //
  9450. // We need this variable to track said change.
  9451. off_t original_offset = offset;
  9452. #endif
  9453. do {
  9454. Py_BEGIN_ALLOW_THREADS
  9455. ret = sendfile(out_fd, in_fd, &offset, count);
  9456. #if defined(__sun) && defined(__SVR4)
  9457. // This handles illumos-specific sendfile() partial write behavior,
  9458. // see a comment above for more details.
  9459. if (ret < 0 && offset != original_offset) {
  9460. ret = offset - original_offset;
  9461. }
  9462. #endif
  9463. Py_END_ALLOW_THREADS
  9464. } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9465. if (ret < 0)
  9466. return (!async_err) ? posix_error() : NULL;
  9467. return Py_BuildValue("n", ret);
  9468. #endif
  9469. }
  9470. #endif /* HAVE_SENDFILE */
  9471. #if defined(__APPLE__)
  9472. /*[clinic input]
  9473. os._fcopyfile
  9474. in_fd: int
  9475. out_fd: int
  9476. flags: int
  9477. /
  9478. Efficiently copy content or metadata of 2 regular file descriptors (macOS).
  9479. [clinic start generated code]*/
  9480. static PyObject *
  9481. os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
  9482. /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
  9483. {
  9484. int ret;
  9485. Py_BEGIN_ALLOW_THREADS
  9486. ret = fcopyfile(in_fd, out_fd, NULL, flags);
  9487. Py_END_ALLOW_THREADS
  9488. if (ret < 0)
  9489. return posix_error();
  9490. Py_RETURN_NONE;
  9491. }
  9492. #endif
  9493. /*[clinic input]
  9494. os.fstat
  9495. fd : int
  9496. Perform a stat system call on the given file descriptor.
  9497. Like stat(), but for an open file descriptor.
  9498. Equivalent to os.stat(fd).
  9499. [clinic start generated code]*/
  9500. static PyObject *
  9501. os_fstat_impl(PyObject *module, int fd)
  9502. /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
  9503. {
  9504. STRUCT_STAT st;
  9505. int res;
  9506. int async_err = 0;
  9507. do {
  9508. Py_BEGIN_ALLOW_THREADS
  9509. res = FSTAT(fd, &st);
  9510. Py_END_ALLOW_THREADS
  9511. } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9512. if (res != 0) {
  9513. #ifdef MS_WINDOWS
  9514. return PyErr_SetFromWindowsErr(0);
  9515. #else
  9516. return (!async_err) ? posix_error() : NULL;
  9517. #endif
  9518. }
  9519. return _pystat_fromstructstat(module, &st);
  9520. }
  9521. /*[clinic input]
  9522. os.isatty -> bool
  9523. fd: int
  9524. /
  9525. Return True if the fd is connected to a terminal.
  9526. Return True if the file descriptor is an open file descriptor
  9527. connected to the slave end of a terminal.
  9528. [clinic start generated code]*/
  9529. static int
  9530. os_isatty_impl(PyObject *module, int fd)
  9531. /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
  9532. {
  9533. int return_value;
  9534. Py_BEGIN_ALLOW_THREADS
  9535. _Py_BEGIN_SUPPRESS_IPH
  9536. return_value = isatty(fd);
  9537. _Py_END_SUPPRESS_IPH
  9538. Py_END_ALLOW_THREADS
  9539. return return_value;
  9540. }
  9541. #ifdef HAVE_PIPE
  9542. /*[clinic input]
  9543. os.pipe
  9544. Create a pipe.
  9545. Returns a tuple of two file descriptors:
  9546. (read_fd, write_fd)
  9547. [clinic start generated code]*/
  9548. static PyObject *
  9549. os_pipe_impl(PyObject *module)
  9550. /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
  9551. {
  9552. int fds[2];
  9553. #ifdef MS_WINDOWS
  9554. HANDLE read, write;
  9555. SECURITY_ATTRIBUTES attr;
  9556. BOOL ok;
  9557. #else
  9558. int res;
  9559. #endif
  9560. #ifdef MS_WINDOWS
  9561. attr.nLength = sizeof(attr);
  9562. attr.lpSecurityDescriptor = NULL;
  9563. attr.bInheritHandle = FALSE;
  9564. Py_BEGIN_ALLOW_THREADS
  9565. ok = CreatePipe(&read, &write, &attr, 0);
  9566. if (ok) {
  9567. fds[0] = _Py_open_osfhandle_noraise(read, _O_RDONLY);
  9568. fds[1] = _Py_open_osfhandle_noraise(write, _O_WRONLY);
  9569. if (fds[0] == -1 || fds[1] == -1) {
  9570. CloseHandle(read);
  9571. CloseHandle(write);
  9572. ok = 0;
  9573. }
  9574. }
  9575. Py_END_ALLOW_THREADS
  9576. if (!ok)
  9577. return PyErr_SetFromWindowsErr(0);
  9578. #else
  9579. #ifdef HAVE_PIPE2
  9580. Py_BEGIN_ALLOW_THREADS
  9581. res = pipe2(fds, O_CLOEXEC);
  9582. Py_END_ALLOW_THREADS
  9583. if (res != 0 && errno == ENOSYS)
  9584. {
  9585. #endif
  9586. Py_BEGIN_ALLOW_THREADS
  9587. res = pipe(fds);
  9588. Py_END_ALLOW_THREADS
  9589. if (res == 0) {
  9590. if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
  9591. close(fds[0]);
  9592. close(fds[1]);
  9593. return NULL;
  9594. }
  9595. if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
  9596. close(fds[0]);
  9597. close(fds[1]);
  9598. return NULL;
  9599. }
  9600. }
  9601. #ifdef HAVE_PIPE2
  9602. }
  9603. #endif
  9604. if (res != 0)
  9605. return PyErr_SetFromErrno(PyExc_OSError);
  9606. #endif /* !MS_WINDOWS */
  9607. return Py_BuildValue("(ii)", fds[0], fds[1]);
  9608. }
  9609. #endif /* HAVE_PIPE */
  9610. #ifdef HAVE_PIPE2
  9611. /*[clinic input]
  9612. os.pipe2
  9613. flags: int
  9614. /
  9615. Create a pipe with flags set atomically.
  9616. Returns a tuple of two file descriptors:
  9617. (read_fd, write_fd)
  9618. flags can be constructed by ORing together one or more of these values:
  9619. O_NONBLOCK, O_CLOEXEC.
  9620. [clinic start generated code]*/
  9621. static PyObject *
  9622. os_pipe2_impl(PyObject *module, int flags)
  9623. /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
  9624. {
  9625. int fds[2];
  9626. int res;
  9627. res = pipe2(fds, flags);
  9628. if (res != 0)
  9629. return posix_error();
  9630. return Py_BuildValue("(ii)", fds[0], fds[1]);
  9631. }
  9632. #endif /* HAVE_PIPE2 */
  9633. #ifdef HAVE_WRITEV
  9634. /*[clinic input]
  9635. os.writev -> Py_ssize_t
  9636. fd: int
  9637. buffers: object
  9638. /
  9639. Iterate over buffers, and write the contents of each to a file descriptor.
  9640. Returns the total number of bytes written.
  9641. buffers must be a sequence of bytes-like objects.
  9642. [clinic start generated code]*/
  9643. static Py_ssize_t
  9644. os_writev_impl(PyObject *module, int fd, PyObject *buffers)
  9645. /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
  9646. {
  9647. Py_ssize_t cnt;
  9648. Py_ssize_t result;
  9649. int async_err = 0;
  9650. struct iovec *iov;
  9651. Py_buffer *buf;
  9652. if (!PySequence_Check(buffers)) {
  9653. PyErr_SetString(PyExc_TypeError,
  9654. "writev() arg 2 must be a sequence");
  9655. return -1;
  9656. }
  9657. cnt = PySequence_Size(buffers);
  9658. if (cnt < 0)
  9659. return -1;
  9660. if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
  9661. return -1;
  9662. }
  9663. do {
  9664. Py_BEGIN_ALLOW_THREADS
  9665. result = writev(fd, iov, cnt);
  9666. Py_END_ALLOW_THREADS
  9667. } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9668. if (result < 0 && !async_err)
  9669. posix_error();
  9670. iov_cleanup(iov, buf, cnt);
  9671. return result;
  9672. }
  9673. #endif /* HAVE_WRITEV */
  9674. #ifdef HAVE_PWRITE
  9675. /*[clinic input]
  9676. os.pwrite -> Py_ssize_t
  9677. fd: int
  9678. buffer: Py_buffer
  9679. offset: Py_off_t
  9680. /
  9681. Write bytes to a file descriptor starting at a particular offset.
  9682. Write buffer to fd, starting at offset bytes from the beginning of
  9683. the file. Returns the number of bytes writte. Does not change the
  9684. current file offset.
  9685. [clinic start generated code]*/
  9686. static Py_ssize_t
  9687. os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
  9688. /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
  9689. {
  9690. Py_ssize_t size;
  9691. int async_err = 0;
  9692. do {
  9693. Py_BEGIN_ALLOW_THREADS
  9694. _Py_BEGIN_SUPPRESS_IPH
  9695. size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
  9696. _Py_END_SUPPRESS_IPH
  9697. Py_END_ALLOW_THREADS
  9698. } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9699. if (size < 0 && !async_err)
  9700. posix_error();
  9701. return size;
  9702. }
  9703. #endif /* HAVE_PWRITE */
  9704. #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
  9705. /*[clinic input]
  9706. os.pwritev -> Py_ssize_t
  9707. fd: int
  9708. buffers: object
  9709. offset: Py_off_t
  9710. flags: int = 0
  9711. /
  9712. Writes the contents of bytes-like objects to a file descriptor at a given offset.
  9713. Combines the functionality of writev() and pwrite(). All buffers must be a sequence
  9714. of bytes-like objects. Buffers are processed in array order. Entire contents of first
  9715. buffer is written before proceeding to second, and so on. The operating system may
  9716. set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
  9717. This function writes the contents of each object to the file descriptor and returns
  9718. the total number of bytes written.
  9719. The flags argument contains a bitwise OR of zero or more of the following flags:
  9720. - RWF_DSYNC
  9721. - RWF_SYNC
  9722. - RWF_APPEND
  9723. Using non-zero flags requires Linux 4.7 or newer.
  9724. [clinic start generated code]*/
  9725. static Py_ssize_t
  9726. os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
  9727. int flags)
  9728. /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/
  9729. {
  9730. Py_ssize_t cnt;
  9731. Py_ssize_t result;
  9732. int async_err = 0;
  9733. struct iovec *iov;
  9734. Py_buffer *buf;
  9735. if (!PySequence_Check(buffers)) {
  9736. PyErr_SetString(PyExc_TypeError,
  9737. "pwritev() arg 2 must be a sequence");
  9738. return -1;
  9739. }
  9740. cnt = PySequence_Size(buffers);
  9741. if (cnt < 0) {
  9742. return -1;
  9743. }
  9744. #ifndef HAVE_PWRITEV2
  9745. if(flags != 0) {
  9746. argument_unavailable_error("pwritev2", "flags");
  9747. return -1;
  9748. }
  9749. #endif
  9750. if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
  9751. return -1;
  9752. }
  9753. #ifdef HAVE_PWRITEV2
  9754. do {
  9755. Py_BEGIN_ALLOW_THREADS
  9756. _Py_BEGIN_SUPPRESS_IPH
  9757. result = pwritev2(fd, iov, cnt, offset, flags);
  9758. _Py_END_SUPPRESS_IPH
  9759. Py_END_ALLOW_THREADS
  9760. } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9761. #else
  9762. #if defined(__APPLE__) && defined(__clang__)
  9763. /* This entire function will be removed from the module dict when the API
  9764. * is not available.
  9765. */
  9766. #pragma clang diagnostic push
  9767. #pragma clang diagnostic ignored "-Wunguarded-availability"
  9768. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  9769. #endif
  9770. do {
  9771. Py_BEGIN_ALLOW_THREADS
  9772. _Py_BEGIN_SUPPRESS_IPH
  9773. result = pwritev(fd, iov, cnt, offset);
  9774. _Py_END_SUPPRESS_IPH
  9775. Py_END_ALLOW_THREADS
  9776. } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9777. #if defined(__APPLE__) && defined(__clang__)
  9778. #pragma clang diagnostic pop
  9779. #endif
  9780. #endif
  9781. if (result < 0) {
  9782. if (!async_err) {
  9783. posix_error();
  9784. }
  9785. result = -1;
  9786. }
  9787. iov_cleanup(iov, buf, cnt);
  9788. return result;
  9789. }
  9790. #endif /* HAVE_PWRITEV */
  9791. #ifdef HAVE_COPY_FILE_RANGE
  9792. /*[clinic input]
  9793. os.copy_file_range
  9794. src: int
  9795. Source file descriptor.
  9796. dst: int
  9797. Destination file descriptor.
  9798. count: Py_ssize_t
  9799. Number of bytes to copy.
  9800. offset_src: object = None
  9801. Starting offset in src.
  9802. offset_dst: object = None
  9803. Starting offset in dst.
  9804. Copy count bytes from one file descriptor to another.
  9805. If offset_src is None, then src is read from the current position;
  9806. respectively for offset_dst.
  9807. [clinic start generated code]*/
  9808. static PyObject *
  9809. os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
  9810. PyObject *offset_src, PyObject *offset_dst)
  9811. /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
  9812. {
  9813. off_t offset_src_val, offset_dst_val;
  9814. off_t *p_offset_src = NULL;
  9815. off_t *p_offset_dst = NULL;
  9816. Py_ssize_t ret;
  9817. int async_err = 0;
  9818. /* The flags argument is provided to allow
  9819. * for future extensions and currently must be to 0. */
  9820. int flags = 0;
  9821. if (count < 0) {
  9822. PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
  9823. return NULL;
  9824. }
  9825. if (offset_src != Py_None) {
  9826. if (!Py_off_t_converter(offset_src, &offset_src_val)) {
  9827. return NULL;
  9828. }
  9829. p_offset_src = &offset_src_val;
  9830. }
  9831. if (offset_dst != Py_None) {
  9832. if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
  9833. return NULL;
  9834. }
  9835. p_offset_dst = &offset_dst_val;
  9836. }
  9837. do {
  9838. Py_BEGIN_ALLOW_THREADS
  9839. ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
  9840. Py_END_ALLOW_THREADS
  9841. } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9842. if (ret < 0) {
  9843. return (!async_err) ? posix_error() : NULL;
  9844. }
  9845. return PyLong_FromSsize_t(ret);
  9846. }
  9847. #endif /* HAVE_COPY_FILE_RANGE*/
  9848. #if (defined(HAVE_SPLICE) && !defined(_AIX))
  9849. /*[clinic input]
  9850. os.splice
  9851. src: int
  9852. Source file descriptor.
  9853. dst: int
  9854. Destination file descriptor.
  9855. count: Py_ssize_t
  9856. Number of bytes to copy.
  9857. offset_src: object = None
  9858. Starting offset in src.
  9859. offset_dst: object = None
  9860. Starting offset in dst.
  9861. flags: unsigned_int = 0
  9862. Flags to modify the semantics of the call.
  9863. Transfer count bytes from one pipe to a descriptor or vice versa.
  9864. If offset_src is None, then src is read from the current position;
  9865. respectively for offset_dst. The offset associated to the file
  9866. descriptor that refers to a pipe must be None.
  9867. [clinic start generated code]*/
  9868. static PyObject *
  9869. os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count,
  9870. PyObject *offset_src, PyObject *offset_dst,
  9871. unsigned int flags)
  9872. /*[clinic end generated code: output=d0386f25a8519dc5 input=047527c66c6d2e0a]*/
  9873. {
  9874. off_t offset_src_val, offset_dst_val;
  9875. off_t *p_offset_src = NULL;
  9876. off_t *p_offset_dst = NULL;
  9877. Py_ssize_t ret;
  9878. int async_err = 0;
  9879. if (count < 0) {
  9880. PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
  9881. return NULL;
  9882. }
  9883. if (offset_src != Py_None) {
  9884. if (!Py_off_t_converter(offset_src, &offset_src_val)) {
  9885. return NULL;
  9886. }
  9887. p_offset_src = &offset_src_val;
  9888. }
  9889. if (offset_dst != Py_None) {
  9890. if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
  9891. return NULL;
  9892. }
  9893. p_offset_dst = &offset_dst_val;
  9894. }
  9895. do {
  9896. Py_BEGIN_ALLOW_THREADS
  9897. ret = splice(src, p_offset_src, dst, p_offset_dst, count, flags);
  9898. Py_END_ALLOW_THREADS
  9899. } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
  9900. if (ret < 0) {
  9901. return (!async_err) ? posix_error() : NULL;
  9902. }
  9903. return PyLong_FromSsize_t(ret);
  9904. }
  9905. #endif /* HAVE_SPLICE*/
  9906. #ifdef HAVE_MKFIFO
  9907. /*[clinic input]
  9908. os.mkfifo
  9909. path: path_t
  9910. mode: int=0o666
  9911. *
  9912. dir_fd: dir_fd(requires='mkfifoat')=None
  9913. Create a "fifo" (a POSIX named pipe).
  9914. If dir_fd is not None, it should be a file descriptor open to a directory,
  9915. and path should be relative; path will then be relative to that directory.
  9916. dir_fd may not be implemented on your platform.
  9917. If it is unavailable, using it will raise a NotImplementedError.
  9918. [clinic start generated code]*/
  9919. static PyObject *
  9920. os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
  9921. /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
  9922. {
  9923. int result;
  9924. int async_err = 0;
  9925. #ifdef HAVE_MKFIFOAT
  9926. int mkfifoat_unavailable = 0;
  9927. #endif
  9928. do {
  9929. Py_BEGIN_ALLOW_THREADS
  9930. #ifdef HAVE_MKFIFOAT
  9931. if (dir_fd != DEFAULT_DIR_FD) {
  9932. if (HAVE_MKFIFOAT_RUNTIME) {
  9933. result = mkfifoat(dir_fd, path->narrow, mode);
  9934. } else {
  9935. mkfifoat_unavailable = 1;
  9936. result = 0;
  9937. }
  9938. } else
  9939. #endif
  9940. result = mkfifo(path->narrow, mode);
  9941. Py_END_ALLOW_THREADS
  9942. } while (result != 0 && errno == EINTR &&
  9943. !(async_err = PyErr_CheckSignals()));
  9944. #ifdef HAVE_MKFIFOAT
  9945. if (mkfifoat_unavailable) {
  9946. argument_unavailable_error(NULL, "dir_fd");
  9947. return NULL;
  9948. }
  9949. #endif
  9950. if (result != 0)
  9951. return (!async_err) ? posix_error() : NULL;
  9952. Py_RETURN_NONE;
  9953. }
  9954. #endif /* HAVE_MKFIFO */
  9955. #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
  9956. /*[clinic input]
  9957. os.mknod
  9958. path: path_t
  9959. mode: int=0o600
  9960. device: dev_t=0
  9961. *
  9962. dir_fd: dir_fd(requires='mknodat')=None
  9963. Create a node in the file system.
  9964. Create a node in the file system (file, device special file or named pipe)
  9965. at path. mode specifies both the permissions to use and the
  9966. type of node to be created, being combined (bitwise OR) with one of
  9967. S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
  9968. device defines the newly created device special file (probably using
  9969. os.makedev()). Otherwise device is ignored.
  9970. If dir_fd is not None, it should be a file descriptor open to a directory,
  9971. and path should be relative; path will then be relative to that directory.
  9972. dir_fd may not be implemented on your platform.
  9973. If it is unavailable, using it will raise a NotImplementedError.
  9974. [clinic start generated code]*/
  9975. static PyObject *
  9976. os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
  9977. int dir_fd)
  9978. /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
  9979. {
  9980. int result;
  9981. int async_err = 0;
  9982. #ifdef HAVE_MKNODAT
  9983. int mknodat_unavailable = 0;
  9984. #endif
  9985. do {
  9986. Py_BEGIN_ALLOW_THREADS
  9987. #ifdef HAVE_MKNODAT
  9988. if (dir_fd != DEFAULT_DIR_FD) {
  9989. if (HAVE_MKNODAT_RUNTIME) {
  9990. result = mknodat(dir_fd, path->narrow, mode, device);
  9991. } else {
  9992. mknodat_unavailable = 1;
  9993. result = 0;
  9994. }
  9995. } else
  9996. #endif
  9997. result = mknod(path->narrow, mode, device);
  9998. Py_END_ALLOW_THREADS
  9999. } while (result != 0 && errno == EINTR &&
  10000. !(async_err = PyErr_CheckSignals()));
  10001. #ifdef HAVE_MKNODAT
  10002. if (mknodat_unavailable) {
  10003. argument_unavailable_error(NULL, "dir_fd");
  10004. return NULL;
  10005. }
  10006. #endif
  10007. if (result != 0)
  10008. return (!async_err) ? posix_error() : NULL;
  10009. Py_RETURN_NONE;
  10010. }
  10011. #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
  10012. #ifdef HAVE_DEVICE_MACROS
  10013. static PyObject *
  10014. major_minor_conv(unsigned int value)
  10015. {
  10016. #ifdef NODEV
  10017. if (value == (unsigned int)NODEV) {
  10018. return PyLong_FromLong((int)NODEV);
  10019. }
  10020. #endif
  10021. return PyLong_FromUnsignedLong(value);
  10022. }
  10023. static int
  10024. major_minor_check(dev_t value)
  10025. {
  10026. #ifdef NODEV
  10027. if (value == NODEV) {
  10028. return 1;
  10029. }
  10030. #endif
  10031. return (dev_t)(unsigned int)value == value;
  10032. }
  10033. /*[clinic input]
  10034. os.major
  10035. device: dev_t
  10036. /
  10037. Extracts a device major number from a raw device number.
  10038. [clinic start generated code]*/
  10039. static PyObject *
  10040. os_major_impl(PyObject *module, dev_t device)
  10041. /*[clinic end generated code: output=4071ffee17647891 input=b1a0a14ec9448229]*/
  10042. {
  10043. return major_minor_conv(major(device));
  10044. }
  10045. /*[clinic input]
  10046. os.minor
  10047. device: dev_t
  10048. /
  10049. Extracts a device minor number from a raw device number.
  10050. [clinic start generated code]*/
  10051. static PyObject *
  10052. os_minor_impl(PyObject *module, dev_t device)
  10053. /*[clinic end generated code: output=306cb78e3bc5004f input=2f686e463682a9da]*/
  10054. {
  10055. return major_minor_conv(minor(device));
  10056. }
  10057. /*[clinic input]
  10058. os.makedev -> dev_t
  10059. major: dev_t
  10060. minor: dev_t
  10061. /
  10062. Composes a raw device number from the major and minor device numbers.
  10063. [clinic start generated code]*/
  10064. static dev_t
  10065. os_makedev_impl(PyObject *module, dev_t major, dev_t minor)
  10066. /*[clinic end generated code: output=cad6125c51f5af80 input=2146126ec02e55c1]*/
  10067. {
  10068. if (!major_minor_check(major) || !major_minor_check(minor)) {
  10069. PyErr_SetString(PyExc_OverflowError,
  10070. "Python int too large to convert to C unsigned int");
  10071. return (dev_t)-1;
  10072. }
  10073. return makedev(major, minor);
  10074. }
  10075. #endif /* HAVE_DEVICE_MACROS */
  10076. #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
  10077. /*[clinic input]
  10078. os.ftruncate
  10079. fd: int
  10080. length: Py_off_t
  10081. /
  10082. Truncate a file, specified by file descriptor, to a specific length.
  10083. [clinic start generated code]*/
  10084. static PyObject *
  10085. os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
  10086. /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
  10087. {
  10088. int result;
  10089. int async_err = 0;
  10090. if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
  10091. return NULL;
  10092. }
  10093. do {
  10094. Py_BEGIN_ALLOW_THREADS
  10095. _Py_BEGIN_SUPPRESS_IPH
  10096. #ifdef MS_WINDOWS
  10097. result = _chsize_s(fd, length);
  10098. #else
  10099. result = ftruncate(fd, length);
  10100. #endif
  10101. _Py_END_SUPPRESS_IPH
  10102. Py_END_ALLOW_THREADS
  10103. } while (result != 0 && errno == EINTR &&
  10104. !(async_err = PyErr_CheckSignals()));
  10105. if (result != 0)
  10106. return (!async_err) ? posix_error() : NULL;
  10107. Py_RETURN_NONE;
  10108. }
  10109. #endif /* HAVE_FTRUNCATE || MS_WINDOWS */
  10110. #if defined HAVE_TRUNCATE || defined MS_WINDOWS
  10111. /*[clinic input]
  10112. os.truncate
  10113. path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
  10114. length: Py_off_t
  10115. Truncate a file, specified by path, to a specific length.
  10116. On some platforms, path may also be specified as an open file descriptor.
  10117. If this functionality is unavailable, using it raises an exception.
  10118. [clinic start generated code]*/
  10119. static PyObject *
  10120. os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
  10121. /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
  10122. {
  10123. int result;
  10124. #ifdef MS_WINDOWS
  10125. int fd;
  10126. #endif
  10127. if (path->fd != -1)
  10128. return os_ftruncate_impl(module, path->fd, length);
  10129. if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
  10130. return NULL;
  10131. }
  10132. Py_BEGIN_ALLOW_THREADS
  10133. _Py_BEGIN_SUPPRESS_IPH
  10134. #ifdef MS_WINDOWS
  10135. fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
  10136. if (fd < 0)
  10137. result = -1;
  10138. else {
  10139. result = _chsize_s(fd, length);
  10140. close(fd);
  10141. if (result < 0)
  10142. errno = result;
  10143. }
  10144. #else
  10145. result = truncate(path->narrow, length);
  10146. #endif
  10147. _Py_END_SUPPRESS_IPH
  10148. Py_END_ALLOW_THREADS
  10149. if (result < 0)
  10150. return posix_path_error(path);
  10151. Py_RETURN_NONE;
  10152. }
  10153. #endif /* HAVE_TRUNCATE || MS_WINDOWS */
  10154. /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
  10155. and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
  10156. defined, which is the case in Python on AIX. AIX bug report:
  10157. http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
  10158. #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
  10159. # define POSIX_FADVISE_AIX_BUG
  10160. #endif
  10161. /* GH-111804: Due to posix_fallocate() not having consistent semantics across
  10162. OSs, support was dropped in WASI preview2. */
  10163. #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) && \
  10164. !defined(__wasi__)
  10165. /*[clinic input]
  10166. os.posix_fallocate
  10167. fd: int
  10168. offset: Py_off_t
  10169. length: Py_off_t
  10170. /
  10171. Ensure a file has allocated at least a particular number of bytes on disk.
  10172. Ensure that the file specified by fd encompasses a range of bytes
  10173. starting at offset bytes from the beginning and continuing for length bytes.
  10174. [clinic start generated code]*/
  10175. static PyObject *
  10176. os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
  10177. Py_off_t length)
  10178. /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
  10179. {
  10180. int result;
  10181. int async_err = 0;
  10182. do {
  10183. Py_BEGIN_ALLOW_THREADS
  10184. result = posix_fallocate(fd, offset, length);
  10185. Py_END_ALLOW_THREADS
  10186. } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
  10187. if (result == 0)
  10188. Py_RETURN_NONE;
  10189. if (async_err)
  10190. return NULL;
  10191. errno = result;
  10192. return posix_error();
  10193. }
  10194. #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG && !defined(__wasi__) */
  10195. #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
  10196. /*[clinic input]
  10197. os.posix_fadvise
  10198. fd: int
  10199. offset: Py_off_t
  10200. length: Py_off_t
  10201. advice: int
  10202. /
  10203. Announce an intention to access data in a specific pattern.
  10204. Announce an intention to access data in a specific pattern, thus allowing
  10205. the kernel to make optimizations.
  10206. The advice applies to the region of the file specified by fd starting at
  10207. offset and continuing for length bytes.
  10208. advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
  10209. POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
  10210. POSIX_FADV_DONTNEED.
  10211. [clinic start generated code]*/
  10212. static PyObject *
  10213. os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
  10214. Py_off_t length, int advice)
  10215. /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
  10216. {
  10217. int result;
  10218. int async_err = 0;
  10219. do {
  10220. Py_BEGIN_ALLOW_THREADS
  10221. result = posix_fadvise(fd, offset, length, advice);
  10222. Py_END_ALLOW_THREADS
  10223. } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
  10224. if (result == 0)
  10225. Py_RETURN_NONE;
  10226. if (async_err)
  10227. return NULL;
  10228. errno = result;
  10229. return posix_error();
  10230. }
  10231. #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
  10232. #ifdef MS_WINDOWS
  10233. static PyObject*
  10234. win32_putenv(PyObject *name, PyObject *value)
  10235. {
  10236. /* Search from index 1 because on Windows starting '=' is allowed for
  10237. defining hidden environment variables. */
  10238. if (PyUnicode_GET_LENGTH(name) == 0 ||
  10239. PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
  10240. {
  10241. PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
  10242. return NULL;
  10243. }
  10244. PyObject *unicode;
  10245. if (value != NULL) {
  10246. unicode = PyUnicode_FromFormat("%U=%U", name, value);
  10247. }
  10248. else {
  10249. unicode = PyUnicode_FromFormat("%U=", name);
  10250. }
  10251. if (unicode == NULL) {
  10252. return NULL;
  10253. }
  10254. Py_ssize_t size;
  10255. wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
  10256. Py_DECREF(unicode);
  10257. if (env == NULL) {
  10258. return NULL;
  10259. }
  10260. if (size > _MAX_ENV) {
  10261. PyErr_Format(PyExc_ValueError,
  10262. "the environment variable is longer than %u characters",
  10263. _MAX_ENV);
  10264. PyMem_Free(env);
  10265. return NULL;
  10266. }
  10267. if (wcslen(env) != (size_t)size) {
  10268. PyErr_SetString(PyExc_ValueError,
  10269. "embedded null character");
  10270. PyMem_Free(env);
  10271. return NULL;
  10272. }
  10273. /* _wputenv() and SetEnvironmentVariableW() update the environment in the
  10274. Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
  10275. and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
  10276. Prefer _wputenv() to be compatible with C libraries using CRT
  10277. variables and CRT functions using these variables (ex: getenv()). */
  10278. int err = _wputenv(env);
  10279. if (err) {
  10280. posix_error();
  10281. PyMem_Free(env);
  10282. return NULL;
  10283. }
  10284. PyMem_Free(env);
  10285. Py_RETURN_NONE;
  10286. }
  10287. #endif
  10288. #ifdef MS_WINDOWS
  10289. /*[clinic input]
  10290. os.putenv
  10291. name: unicode
  10292. value: unicode
  10293. /
  10294. Change or add an environment variable.
  10295. [clinic start generated code]*/
  10296. static PyObject *
  10297. os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
  10298. /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
  10299. {
  10300. if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
  10301. return NULL;
  10302. }
  10303. return win32_putenv(name, value);
  10304. }
  10305. #else
  10306. /*[clinic input]
  10307. os.putenv
  10308. name: FSConverter
  10309. value: FSConverter
  10310. /
  10311. Change or add an environment variable.
  10312. [clinic start generated code]*/
  10313. static PyObject *
  10314. os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
  10315. /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
  10316. {
  10317. const char *name_string = PyBytes_AS_STRING(name);
  10318. const char *value_string = PyBytes_AS_STRING(value);
  10319. if (strchr(name_string, '=') != NULL) {
  10320. PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
  10321. return NULL;
  10322. }
  10323. if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
  10324. return NULL;
  10325. }
  10326. if (setenv(name_string, value_string, 1)) {
  10327. return posix_error();
  10328. }
  10329. Py_RETURN_NONE;
  10330. }
  10331. #endif /* !defined(MS_WINDOWS) */
  10332. #ifdef MS_WINDOWS
  10333. /*[clinic input]
  10334. os.unsetenv
  10335. name: unicode
  10336. /
  10337. Delete an environment variable.
  10338. [clinic start generated code]*/
  10339. static PyObject *
  10340. os_unsetenv_impl(PyObject *module, PyObject *name)
  10341. /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
  10342. {
  10343. if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
  10344. return NULL;
  10345. }
  10346. return win32_putenv(name, NULL);
  10347. }
  10348. #else
  10349. /*[clinic input]
  10350. os.unsetenv
  10351. name: FSConverter
  10352. /
  10353. Delete an environment variable.
  10354. [clinic start generated code]*/
  10355. static PyObject *
  10356. os_unsetenv_impl(PyObject *module, PyObject *name)
  10357. /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
  10358. {
  10359. if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
  10360. return NULL;
  10361. }
  10362. #ifdef HAVE_BROKEN_UNSETENV
  10363. unsetenv(PyBytes_AS_STRING(name));
  10364. #else
  10365. int err = unsetenv(PyBytes_AS_STRING(name));
  10366. if (err) {
  10367. return posix_error();
  10368. }
  10369. #endif
  10370. Py_RETURN_NONE;
  10371. }
  10372. #endif /* !MS_WINDOWS */
  10373. /*[clinic input]
  10374. os.strerror
  10375. code: int
  10376. /
  10377. Translate an error code to a message string.
  10378. [clinic start generated code]*/
  10379. static PyObject *
  10380. os_strerror_impl(PyObject *module, int code)
  10381. /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
  10382. {
  10383. char *message = strerror(code);
  10384. if (message == NULL) {
  10385. PyErr_SetString(PyExc_ValueError,
  10386. "strerror() argument out of range");
  10387. return NULL;
  10388. }
  10389. return PyUnicode_DecodeLocale(message, "surrogateescape");
  10390. }
  10391. #ifdef HAVE_SYS_WAIT_H
  10392. #ifdef WCOREDUMP
  10393. /*[clinic input]
  10394. os.WCOREDUMP -> bool
  10395. status: int
  10396. /
  10397. Return True if the process returning status was dumped to a core file.
  10398. [clinic start generated code]*/
  10399. static int
  10400. os_WCOREDUMP_impl(PyObject *module, int status)
  10401. /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
  10402. {
  10403. WAIT_TYPE wait_status;
  10404. WAIT_STATUS_INT(wait_status) = status;
  10405. return WCOREDUMP(wait_status);
  10406. }
  10407. #endif /* WCOREDUMP */
  10408. #ifdef WIFCONTINUED
  10409. /*[clinic input]
  10410. os.WIFCONTINUED -> bool
  10411. status: int
  10412. Return True if a particular process was continued from a job control stop.
  10413. Return True if the process returning status was continued from a
  10414. job control stop.
  10415. [clinic start generated code]*/
  10416. static int
  10417. os_WIFCONTINUED_impl(PyObject *module, int status)
  10418. /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
  10419. {
  10420. WAIT_TYPE wait_status;
  10421. WAIT_STATUS_INT(wait_status) = status;
  10422. return WIFCONTINUED(wait_status);
  10423. }
  10424. #endif /* WIFCONTINUED */
  10425. #ifdef WIFSTOPPED
  10426. /*[clinic input]
  10427. os.WIFSTOPPED -> bool
  10428. status: int
  10429. Return True if the process returning status was stopped.
  10430. [clinic start generated code]*/
  10431. static int
  10432. os_WIFSTOPPED_impl(PyObject *module, int status)
  10433. /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
  10434. {
  10435. WAIT_TYPE wait_status;
  10436. WAIT_STATUS_INT(wait_status) = status;
  10437. return WIFSTOPPED(wait_status);
  10438. }
  10439. #endif /* WIFSTOPPED */
  10440. #ifdef WIFSIGNALED
  10441. /*[clinic input]
  10442. os.WIFSIGNALED -> bool
  10443. status: int
  10444. Return True if the process returning status was terminated by a signal.
  10445. [clinic start generated code]*/
  10446. static int
  10447. os_WIFSIGNALED_impl(PyObject *module, int status)
  10448. /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
  10449. {
  10450. WAIT_TYPE wait_status;
  10451. WAIT_STATUS_INT(wait_status) = status;
  10452. return WIFSIGNALED(wait_status);
  10453. }
  10454. #endif /* WIFSIGNALED */
  10455. #ifdef WIFEXITED
  10456. /*[clinic input]
  10457. os.WIFEXITED -> bool
  10458. status: int
  10459. Return True if the process returning status exited via the exit() system call.
  10460. [clinic start generated code]*/
  10461. static int
  10462. os_WIFEXITED_impl(PyObject *module, int status)
  10463. /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
  10464. {
  10465. WAIT_TYPE wait_status;
  10466. WAIT_STATUS_INT(wait_status) = status;
  10467. return WIFEXITED(wait_status);
  10468. }
  10469. #endif /* WIFEXITED */
  10470. #ifdef WEXITSTATUS
  10471. /*[clinic input]
  10472. os.WEXITSTATUS -> int
  10473. status: int
  10474. Return the process return code from status.
  10475. [clinic start generated code]*/
  10476. static int
  10477. os_WEXITSTATUS_impl(PyObject *module, int status)
  10478. /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
  10479. {
  10480. WAIT_TYPE wait_status;
  10481. WAIT_STATUS_INT(wait_status) = status;
  10482. return WEXITSTATUS(wait_status);
  10483. }
  10484. #endif /* WEXITSTATUS */
  10485. #ifdef WTERMSIG
  10486. /*[clinic input]
  10487. os.WTERMSIG -> int
  10488. status: int
  10489. Return the signal that terminated the process that provided the status value.
  10490. [clinic start generated code]*/
  10491. static int
  10492. os_WTERMSIG_impl(PyObject *module, int status)
  10493. /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
  10494. {
  10495. WAIT_TYPE wait_status;
  10496. WAIT_STATUS_INT(wait_status) = status;
  10497. return WTERMSIG(wait_status);
  10498. }
  10499. #endif /* WTERMSIG */
  10500. #ifdef WSTOPSIG
  10501. /*[clinic input]
  10502. os.WSTOPSIG -> int
  10503. status: int
  10504. Return the signal that stopped the process that provided the status value.
  10505. [clinic start generated code]*/
  10506. static int
  10507. os_WSTOPSIG_impl(PyObject *module, int status)
  10508. /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
  10509. {
  10510. WAIT_TYPE wait_status;
  10511. WAIT_STATUS_INT(wait_status) = status;
  10512. return WSTOPSIG(wait_status);
  10513. }
  10514. #endif /* WSTOPSIG */
  10515. #endif /* HAVE_SYS_WAIT_H */
  10516. #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
  10517. #ifdef _SCO_DS
  10518. /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
  10519. needed definitions in sys/statvfs.h */
  10520. #define _SVID3
  10521. #endif
  10522. #include <sys/statvfs.h>
  10523. static PyObject*
  10524. _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) {
  10525. PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
  10526. PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
  10527. if (v == NULL)
  10528. return NULL;
  10529. int pos = 0;
  10530. #define SET_RESULT(CALL) \
  10531. do { \
  10532. PyObject *item = (CALL); \
  10533. if (item == NULL) { \
  10534. Py_DECREF(v); \
  10535. return NULL; \
  10536. } \
  10537. PyStructSequence_SET_ITEM(v, pos++, item); \
  10538. } while(0)
  10539. #if !defined(HAVE_LARGEFILE_SUPPORT)
  10540. SET_RESULT(PyLong_FromLong((long) st.f_bsize));
  10541. SET_RESULT(PyLong_FromLong((long) st.f_frsize));
  10542. SET_RESULT(PyLong_FromLong((long) st.f_blocks));
  10543. SET_RESULT(PyLong_FromLong((long) st.f_bfree));
  10544. SET_RESULT(PyLong_FromLong((long) st.f_bavail));
  10545. SET_RESULT(PyLong_FromLong((long) st.f_files));
  10546. SET_RESULT(PyLong_FromLong((long) st.f_ffree));
  10547. SET_RESULT(PyLong_FromLong((long) st.f_favail));
  10548. SET_RESULT(PyLong_FromLong((long) st.f_flag));
  10549. SET_RESULT(PyLong_FromLong((long) st.f_namemax));
  10550. #else
  10551. SET_RESULT(PyLong_FromLong((long) st.f_bsize));
  10552. SET_RESULT(PyLong_FromLong((long) st.f_frsize));
  10553. SET_RESULT(PyLong_FromLongLong((long long) st.f_blocks));
  10554. SET_RESULT(PyLong_FromLongLong((long long) st.f_bfree));
  10555. SET_RESULT(PyLong_FromLongLong((long long) st.f_bavail));
  10556. SET_RESULT(PyLong_FromLongLong((long long) st.f_files));
  10557. SET_RESULT(PyLong_FromLongLong((long long) st.f_ffree));
  10558. SET_RESULT(PyLong_FromLongLong((long long) st.f_favail));
  10559. SET_RESULT(PyLong_FromLong((long) st.f_flag));
  10560. SET_RESULT(PyLong_FromLong((long) st.f_namemax));
  10561. #endif
  10562. /* The _ALL_SOURCE feature test macro defines f_fsid as a structure
  10563. * (issue #32390). */
  10564. #if defined(_AIX) && defined(_ALL_SOURCE)
  10565. SET_RESULT(PyLong_FromUnsignedLong(st.f_fsid.val[0]));
  10566. #else
  10567. SET_RESULT(PyLong_FromUnsignedLong(st.f_fsid));
  10568. #endif
  10569. #undef SET_RESULT
  10570. return v;
  10571. }
  10572. /*[clinic input]
  10573. os.fstatvfs
  10574. fd: int
  10575. /
  10576. Perform an fstatvfs system call on the given fd.
  10577. Equivalent to statvfs(fd).
  10578. [clinic start generated code]*/
  10579. static PyObject *
  10580. os_fstatvfs_impl(PyObject *module, int fd)
  10581. /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
  10582. {
  10583. int result;
  10584. int async_err = 0;
  10585. struct statvfs st;
  10586. do {
  10587. Py_BEGIN_ALLOW_THREADS
  10588. result = fstatvfs(fd, &st);
  10589. Py_END_ALLOW_THREADS
  10590. } while (result != 0 && errno == EINTR &&
  10591. !(async_err = PyErr_CheckSignals()));
  10592. if (result != 0)
  10593. return (!async_err) ? posix_error() : NULL;
  10594. return _pystatvfs_fromstructstatvfs(module, st);
  10595. }
  10596. #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
  10597. #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
  10598. #include <sys/statvfs.h>
  10599. /*[clinic input]
  10600. os.statvfs
  10601. path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
  10602. Perform a statvfs system call on the given path.
  10603. path may always be specified as a string.
  10604. On some platforms, path may also be specified as an open file descriptor.
  10605. If this functionality is unavailable, using it raises an exception.
  10606. [clinic start generated code]*/
  10607. static PyObject *
  10608. os_statvfs_impl(PyObject *module, path_t *path)
  10609. /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
  10610. {
  10611. int result;
  10612. struct statvfs st;
  10613. Py_BEGIN_ALLOW_THREADS
  10614. #ifdef HAVE_FSTATVFS
  10615. if (path->fd != -1) {
  10616. result = fstatvfs(path->fd, &st);
  10617. }
  10618. else
  10619. #endif
  10620. result = statvfs(path->narrow, &st);
  10621. Py_END_ALLOW_THREADS
  10622. if (result) {
  10623. return path_error(path);
  10624. }
  10625. return _pystatvfs_fromstructstatvfs(module, st);
  10626. }
  10627. #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
  10628. #ifdef MS_WINDOWS
  10629. /*[clinic input]
  10630. os._getdiskusage
  10631. path: path_t
  10632. Return disk usage statistics about the given path as a (total, free) tuple.
  10633. [clinic start generated code]*/
  10634. static PyObject *
  10635. os__getdiskusage_impl(PyObject *module, path_t *path)
  10636. /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
  10637. {
  10638. BOOL retval;
  10639. ULARGE_INTEGER _, total, free;
  10640. DWORD err = 0;
  10641. Py_BEGIN_ALLOW_THREADS
  10642. retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
  10643. Py_END_ALLOW_THREADS
  10644. if (retval == 0) {
  10645. if (GetLastError() == ERROR_DIRECTORY) {
  10646. wchar_t *dir_path = NULL;
  10647. dir_path = PyMem_New(wchar_t, path->length + 1);
  10648. if (dir_path == NULL) {
  10649. return PyErr_NoMemory();
  10650. }
  10651. wcscpy_s(dir_path, path->length + 1, path->wide);
  10652. if (_dirnameW(dir_path) != -1) {
  10653. Py_BEGIN_ALLOW_THREADS
  10654. retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
  10655. Py_END_ALLOW_THREADS
  10656. }
  10657. /* Record the last error in case it's modified by PyMem_Free. */
  10658. err = GetLastError();
  10659. PyMem_Free(dir_path);
  10660. if (retval) {
  10661. goto success;
  10662. }
  10663. }
  10664. return PyErr_SetFromWindowsErr(err);
  10665. }
  10666. success:
  10667. return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
  10668. }
  10669. #endif /* MS_WINDOWS */
  10670. /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
  10671. * It maps strings representing configuration variable names to
  10672. * integer values, allowing those functions to be called with the
  10673. * magic names instead of polluting the module's namespace with tons of
  10674. * rarely-used constants. There are three separate tables that use
  10675. * these definitions.
  10676. *
  10677. * This code is always included, even if none of the interfaces that
  10678. * need it are included. The #if hackery needed to avoid it would be
  10679. * sufficiently pervasive that it's not worth the loss of readability.
  10680. */
  10681. struct constdef {
  10682. const char *name;
  10683. int value;
  10684. };
  10685. static int
  10686. conv_confname(PyObject *arg, int *valuep, struct constdef *table,
  10687. size_t tablesize)
  10688. {
  10689. if (PyLong_Check(arg)) {
  10690. int value = _PyLong_AsInt(arg);
  10691. if (value == -1 && PyErr_Occurred())
  10692. return 0;
  10693. *valuep = value;
  10694. return 1;
  10695. }
  10696. else {
  10697. /* look up the value in the table using a binary search */
  10698. size_t lo = 0;
  10699. size_t mid;
  10700. size_t hi = tablesize;
  10701. int cmp;
  10702. const char *confname;
  10703. if (!PyUnicode_Check(arg)) {
  10704. PyErr_SetString(PyExc_TypeError,
  10705. "configuration names must be strings or integers");
  10706. return 0;
  10707. }
  10708. confname = PyUnicode_AsUTF8(arg);
  10709. if (confname == NULL)
  10710. return 0;
  10711. while (lo < hi) {
  10712. mid = (lo + hi) / 2;
  10713. cmp = strcmp(confname, table[mid].name);
  10714. if (cmp < 0)
  10715. hi = mid;
  10716. else if (cmp > 0)
  10717. lo = mid + 1;
  10718. else {
  10719. *valuep = table[mid].value;
  10720. return 1;
  10721. }
  10722. }
  10723. PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
  10724. return 0;
  10725. }
  10726. }
  10727. #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
  10728. static struct constdef posix_constants_pathconf[] = {
  10729. #ifdef _PC_ABI_AIO_XFER_MAX
  10730. {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
  10731. #endif
  10732. #ifdef _PC_ABI_ASYNC_IO
  10733. {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
  10734. #endif
  10735. #ifdef _PC_ASYNC_IO
  10736. {"PC_ASYNC_IO", _PC_ASYNC_IO},
  10737. #endif
  10738. #ifdef _PC_CHOWN_RESTRICTED
  10739. {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
  10740. #endif
  10741. #ifdef _PC_FILESIZEBITS
  10742. {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
  10743. #endif
  10744. #ifdef _PC_LAST
  10745. {"PC_LAST", _PC_LAST},
  10746. #endif
  10747. #ifdef _PC_LINK_MAX
  10748. {"PC_LINK_MAX", _PC_LINK_MAX},
  10749. #endif
  10750. #ifdef _PC_MAX_CANON
  10751. {"PC_MAX_CANON", _PC_MAX_CANON},
  10752. #endif
  10753. #ifdef _PC_MAX_INPUT
  10754. {"PC_MAX_INPUT", _PC_MAX_INPUT},
  10755. #endif
  10756. #ifdef _PC_NAME_MAX
  10757. {"PC_NAME_MAX", _PC_NAME_MAX},
  10758. #endif
  10759. #ifdef _PC_NO_TRUNC
  10760. {"PC_NO_TRUNC", _PC_NO_TRUNC},
  10761. #endif
  10762. #ifdef _PC_PATH_MAX
  10763. {"PC_PATH_MAX", _PC_PATH_MAX},
  10764. #endif
  10765. #ifdef _PC_PIPE_BUF
  10766. {"PC_PIPE_BUF", _PC_PIPE_BUF},
  10767. #endif
  10768. #ifdef _PC_PRIO_IO
  10769. {"PC_PRIO_IO", _PC_PRIO_IO},
  10770. #endif
  10771. #ifdef _PC_SOCK_MAXBUF
  10772. {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
  10773. #endif
  10774. #ifdef _PC_SYNC_IO
  10775. {"PC_SYNC_IO", _PC_SYNC_IO},
  10776. #endif
  10777. #ifdef _PC_VDISABLE
  10778. {"PC_VDISABLE", _PC_VDISABLE},
  10779. #endif
  10780. #ifdef _PC_ACL_ENABLED
  10781. {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
  10782. #endif
  10783. #ifdef _PC_MIN_HOLE_SIZE
  10784. {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
  10785. #endif
  10786. #ifdef _PC_ALLOC_SIZE_MIN
  10787. {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
  10788. #endif
  10789. #ifdef _PC_REC_INCR_XFER_SIZE
  10790. {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
  10791. #endif
  10792. #ifdef _PC_REC_MAX_XFER_SIZE
  10793. {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
  10794. #endif
  10795. #ifdef _PC_REC_MIN_XFER_SIZE
  10796. {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
  10797. #endif
  10798. #ifdef _PC_REC_XFER_ALIGN
  10799. {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
  10800. #endif
  10801. #ifdef _PC_SYMLINK_MAX
  10802. {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
  10803. #endif
  10804. #ifdef _PC_XATTR_ENABLED
  10805. {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
  10806. #endif
  10807. #ifdef _PC_XATTR_EXISTS
  10808. {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
  10809. #endif
  10810. #ifdef _PC_TIMESTAMP_RESOLUTION
  10811. {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
  10812. #endif
  10813. };
  10814. static int
  10815. conv_path_confname(PyObject *arg, int *valuep)
  10816. {
  10817. return conv_confname(arg, valuep, posix_constants_pathconf,
  10818. sizeof(posix_constants_pathconf)
  10819. / sizeof(struct constdef));
  10820. }
  10821. #endif
  10822. #ifdef HAVE_FPATHCONF
  10823. /*[clinic input]
  10824. os.fpathconf -> long
  10825. fd: fildes
  10826. name: path_confname
  10827. /
  10828. Return the configuration limit name for the file descriptor fd.
  10829. If there is no limit, return -1.
  10830. [clinic start generated code]*/
  10831. static long
  10832. os_fpathconf_impl(PyObject *module, int fd, int name)
  10833. /*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/
  10834. {
  10835. long limit;
  10836. errno = 0;
  10837. limit = fpathconf(fd, name);
  10838. if (limit == -1 && errno != 0)
  10839. posix_error();
  10840. return limit;
  10841. }
  10842. #endif /* HAVE_FPATHCONF */
  10843. #ifdef HAVE_PATHCONF
  10844. /*[clinic input]
  10845. os.pathconf -> long
  10846. path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
  10847. name: path_confname
  10848. Return the configuration limit name for the file or directory path.
  10849. If there is no limit, return -1.
  10850. On some platforms, path may also be specified as an open file descriptor.
  10851. If this functionality is unavailable, using it raises an exception.
  10852. [clinic start generated code]*/
  10853. static long
  10854. os_pathconf_impl(PyObject *module, path_t *path, int name)
  10855. /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
  10856. {
  10857. long limit;
  10858. errno = 0;
  10859. #ifdef HAVE_FPATHCONF
  10860. if (path->fd != -1)
  10861. limit = fpathconf(path->fd, name);
  10862. else
  10863. #endif
  10864. limit = pathconf(path->narrow, name);
  10865. if (limit == -1 && errno != 0) {
  10866. if (errno == EINVAL)
  10867. /* could be a path or name problem */
  10868. posix_error();
  10869. else
  10870. path_error(path);
  10871. }
  10872. return limit;
  10873. }
  10874. #endif /* HAVE_PATHCONF */
  10875. #ifdef HAVE_CONFSTR
  10876. static struct constdef posix_constants_confstr[] = {
  10877. #ifdef _CS_ARCHITECTURE
  10878. {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
  10879. #endif
  10880. #ifdef _CS_GNU_LIBC_VERSION
  10881. {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
  10882. #endif
  10883. #ifdef _CS_GNU_LIBPTHREAD_VERSION
  10884. {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
  10885. #endif
  10886. #ifdef _CS_HOSTNAME
  10887. {"CS_HOSTNAME", _CS_HOSTNAME},
  10888. #endif
  10889. #ifdef _CS_HW_PROVIDER
  10890. {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
  10891. #endif
  10892. #ifdef _CS_HW_SERIAL
  10893. {"CS_HW_SERIAL", _CS_HW_SERIAL},
  10894. #endif
  10895. #ifdef _CS_INITTAB_NAME
  10896. {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
  10897. #endif
  10898. #ifdef _CS_LFS64_CFLAGS
  10899. {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
  10900. #endif
  10901. #ifdef _CS_LFS64_LDFLAGS
  10902. {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
  10903. #endif
  10904. #ifdef _CS_LFS64_LIBS
  10905. {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
  10906. #endif
  10907. #ifdef _CS_LFS64_LINTFLAGS
  10908. {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
  10909. #endif
  10910. #ifdef _CS_LFS_CFLAGS
  10911. {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
  10912. #endif
  10913. #ifdef _CS_LFS_LDFLAGS
  10914. {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
  10915. #endif
  10916. #ifdef _CS_LFS_LIBS
  10917. {"CS_LFS_LIBS", _CS_LFS_LIBS},
  10918. #endif
  10919. #ifdef _CS_LFS_LINTFLAGS
  10920. {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
  10921. #endif
  10922. #ifdef _CS_MACHINE
  10923. {"CS_MACHINE", _CS_MACHINE},
  10924. #endif
  10925. #ifdef _CS_PATH
  10926. {"CS_PATH", _CS_PATH},
  10927. #endif
  10928. #ifdef _CS_RELEASE
  10929. {"CS_RELEASE", _CS_RELEASE},
  10930. #endif
  10931. #ifdef _CS_SRPC_DOMAIN
  10932. {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
  10933. #endif
  10934. #ifdef _CS_SYSNAME
  10935. {"CS_SYSNAME", _CS_SYSNAME},
  10936. #endif
  10937. #ifdef _CS_VERSION
  10938. {"CS_VERSION", _CS_VERSION},
  10939. #endif
  10940. #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
  10941. {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
  10942. #endif
  10943. #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
  10944. {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
  10945. #endif
  10946. #ifdef _CS_XBS5_ILP32_OFF32_LIBS
  10947. {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
  10948. #endif
  10949. #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
  10950. {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
  10951. #endif
  10952. #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
  10953. {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
  10954. #endif
  10955. #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
  10956. {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
  10957. #endif
  10958. #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
  10959. {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
  10960. #endif
  10961. #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
  10962. {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
  10963. #endif
  10964. #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
  10965. {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
  10966. #endif
  10967. #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
  10968. {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
  10969. #endif
  10970. #ifdef _CS_XBS5_LP64_OFF64_LIBS
  10971. {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
  10972. #endif
  10973. #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
  10974. {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
  10975. #endif
  10976. #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
  10977. {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
  10978. #endif
  10979. #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
  10980. {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
  10981. #endif
  10982. #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
  10983. {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
  10984. #endif
  10985. #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
  10986. {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
  10987. #endif
  10988. #ifdef _MIPS_CS_AVAIL_PROCESSORS
  10989. {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
  10990. #endif
  10991. #ifdef _MIPS_CS_BASE
  10992. {"MIPS_CS_BASE", _MIPS_CS_BASE},
  10993. #endif
  10994. #ifdef _MIPS_CS_HOSTID
  10995. {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
  10996. #endif
  10997. #ifdef _MIPS_CS_HW_NAME
  10998. {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
  10999. #endif
  11000. #ifdef _MIPS_CS_NUM_PROCESSORS
  11001. {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
  11002. #endif
  11003. #ifdef _MIPS_CS_OSREL_MAJ
  11004. {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
  11005. #endif
  11006. #ifdef _MIPS_CS_OSREL_MIN
  11007. {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
  11008. #endif
  11009. #ifdef _MIPS_CS_OSREL_PATCH
  11010. {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
  11011. #endif
  11012. #ifdef _MIPS_CS_OS_NAME
  11013. {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
  11014. #endif
  11015. #ifdef _MIPS_CS_OS_PROVIDER
  11016. {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
  11017. #endif
  11018. #ifdef _MIPS_CS_PROCESSORS
  11019. {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
  11020. #endif
  11021. #ifdef _MIPS_CS_SERIAL
  11022. {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
  11023. #endif
  11024. #ifdef _MIPS_CS_VENDOR
  11025. {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
  11026. #endif
  11027. };
  11028. static int
  11029. conv_confstr_confname(PyObject *arg, int *valuep)
  11030. {
  11031. return conv_confname(arg, valuep, posix_constants_confstr,
  11032. sizeof(posix_constants_confstr)
  11033. / sizeof(struct constdef));
  11034. }
  11035. /*[clinic input]
  11036. os.confstr
  11037. name: confstr_confname
  11038. /
  11039. Return a string-valued system configuration variable.
  11040. [clinic start generated code]*/
  11041. static PyObject *
  11042. os_confstr_impl(PyObject *module, int name)
  11043. /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
  11044. {
  11045. PyObject *result = NULL;
  11046. char buffer[255];
  11047. size_t len;
  11048. errno = 0;
  11049. len = confstr(name, buffer, sizeof(buffer));
  11050. if (len == 0) {
  11051. if (errno) {
  11052. posix_error();
  11053. return NULL;
  11054. }
  11055. else {
  11056. Py_RETURN_NONE;
  11057. }
  11058. }
  11059. if (len >= sizeof(buffer)) {
  11060. size_t len2;
  11061. char *buf = PyMem_Malloc(len);
  11062. if (buf == NULL)
  11063. return PyErr_NoMemory();
  11064. len2 = confstr(name, buf, len);
  11065. assert(len == len2);
  11066. result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
  11067. PyMem_Free(buf);
  11068. }
  11069. else
  11070. result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
  11071. return result;
  11072. }
  11073. #endif /* HAVE_CONFSTR */
  11074. #ifdef HAVE_SYSCONF
  11075. static struct constdef posix_constants_sysconf[] = {
  11076. #ifdef _SC_2_CHAR_TERM
  11077. {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
  11078. #endif
  11079. #ifdef _SC_2_C_BIND
  11080. {"SC_2_C_BIND", _SC_2_C_BIND},
  11081. #endif
  11082. #ifdef _SC_2_C_DEV
  11083. {"SC_2_C_DEV", _SC_2_C_DEV},
  11084. #endif
  11085. #ifdef _SC_2_C_VERSION
  11086. {"SC_2_C_VERSION", _SC_2_C_VERSION},
  11087. #endif
  11088. #ifdef _SC_2_FORT_DEV
  11089. {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
  11090. #endif
  11091. #ifdef _SC_2_FORT_RUN
  11092. {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
  11093. #endif
  11094. #ifdef _SC_2_LOCALEDEF
  11095. {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
  11096. #endif
  11097. #ifdef _SC_2_SW_DEV
  11098. {"SC_2_SW_DEV", _SC_2_SW_DEV},
  11099. #endif
  11100. #ifdef _SC_2_UPE
  11101. {"SC_2_UPE", _SC_2_UPE},
  11102. #endif
  11103. #ifdef _SC_2_VERSION
  11104. {"SC_2_VERSION", _SC_2_VERSION},
  11105. #endif
  11106. #ifdef _SC_ABI_ASYNCHRONOUS_IO
  11107. {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
  11108. #endif
  11109. #ifdef _SC_ACL
  11110. {"SC_ACL", _SC_ACL},
  11111. #endif
  11112. #ifdef _SC_AIO_LISTIO_MAX
  11113. {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
  11114. #endif
  11115. #ifdef _SC_AIO_MAX
  11116. {"SC_AIO_MAX", _SC_AIO_MAX},
  11117. #endif
  11118. #ifdef _SC_AIO_PRIO_DELTA_MAX
  11119. {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
  11120. #endif
  11121. #ifdef _SC_ARG_MAX
  11122. {"SC_ARG_MAX", _SC_ARG_MAX},
  11123. #endif
  11124. #ifdef _SC_ASYNCHRONOUS_IO
  11125. {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
  11126. #endif
  11127. #ifdef _SC_ATEXIT_MAX
  11128. {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
  11129. #endif
  11130. #ifdef _SC_AUDIT
  11131. {"SC_AUDIT", _SC_AUDIT},
  11132. #endif
  11133. #ifdef _SC_AVPHYS_PAGES
  11134. {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
  11135. #endif
  11136. #ifdef _SC_BC_BASE_MAX
  11137. {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
  11138. #endif
  11139. #ifdef _SC_BC_DIM_MAX
  11140. {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
  11141. #endif
  11142. #ifdef _SC_BC_SCALE_MAX
  11143. {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
  11144. #endif
  11145. #ifdef _SC_BC_STRING_MAX
  11146. {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
  11147. #endif
  11148. #ifdef _SC_CAP
  11149. {"SC_CAP", _SC_CAP},
  11150. #endif
  11151. #ifdef _SC_CHARCLASS_NAME_MAX
  11152. {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
  11153. #endif
  11154. #ifdef _SC_CHAR_BIT
  11155. {"SC_CHAR_BIT", _SC_CHAR_BIT},
  11156. #endif
  11157. #ifdef _SC_CHAR_MAX
  11158. {"SC_CHAR_MAX", _SC_CHAR_MAX},
  11159. #endif
  11160. #ifdef _SC_CHAR_MIN
  11161. {"SC_CHAR_MIN", _SC_CHAR_MIN},
  11162. #endif
  11163. #ifdef _SC_CHILD_MAX
  11164. {"SC_CHILD_MAX", _SC_CHILD_MAX},
  11165. #endif
  11166. #ifdef _SC_CLK_TCK
  11167. {"SC_CLK_TCK", _SC_CLK_TCK},
  11168. #endif
  11169. #ifdef _SC_COHER_BLKSZ
  11170. {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
  11171. #endif
  11172. #ifdef _SC_COLL_WEIGHTS_MAX
  11173. {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
  11174. #endif
  11175. #ifdef _SC_DCACHE_ASSOC
  11176. {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
  11177. #endif
  11178. #ifdef _SC_DCACHE_BLKSZ
  11179. {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
  11180. #endif
  11181. #ifdef _SC_DCACHE_LINESZ
  11182. {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
  11183. #endif
  11184. #ifdef _SC_DCACHE_SZ
  11185. {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
  11186. #endif
  11187. #ifdef _SC_DCACHE_TBLKSZ
  11188. {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
  11189. #endif
  11190. #ifdef _SC_DELAYTIMER_MAX
  11191. {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
  11192. #endif
  11193. #ifdef _SC_EQUIV_CLASS_MAX
  11194. {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
  11195. #endif
  11196. #ifdef _SC_EXPR_NEST_MAX
  11197. {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
  11198. #endif
  11199. #ifdef _SC_FSYNC
  11200. {"SC_FSYNC", _SC_FSYNC},
  11201. #endif
  11202. #ifdef _SC_GETGR_R_SIZE_MAX
  11203. {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
  11204. #endif
  11205. #ifdef _SC_GETPW_R_SIZE_MAX
  11206. {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
  11207. #endif
  11208. #ifdef _SC_ICACHE_ASSOC
  11209. {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
  11210. #endif
  11211. #ifdef _SC_ICACHE_BLKSZ
  11212. {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
  11213. #endif
  11214. #ifdef _SC_ICACHE_LINESZ
  11215. {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
  11216. #endif
  11217. #ifdef _SC_ICACHE_SZ
  11218. {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
  11219. #endif
  11220. #ifdef _SC_INF
  11221. {"SC_INF", _SC_INF},
  11222. #endif
  11223. #ifdef _SC_INT_MAX
  11224. {"SC_INT_MAX", _SC_INT_MAX},
  11225. #endif
  11226. #ifdef _SC_INT_MIN
  11227. {"SC_INT_MIN", _SC_INT_MIN},
  11228. #endif
  11229. #ifdef _SC_IOV_MAX
  11230. {"SC_IOV_MAX", _SC_IOV_MAX},
  11231. #endif
  11232. #ifdef _SC_IP_SECOPTS
  11233. {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
  11234. #endif
  11235. #ifdef _SC_JOB_CONTROL
  11236. {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
  11237. #endif
  11238. #ifdef _SC_KERN_POINTERS
  11239. {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
  11240. #endif
  11241. #ifdef _SC_KERN_SIM
  11242. {"SC_KERN_SIM", _SC_KERN_SIM},
  11243. #endif
  11244. #ifdef _SC_LINE_MAX
  11245. {"SC_LINE_MAX", _SC_LINE_MAX},
  11246. #endif
  11247. #ifdef _SC_LOGIN_NAME_MAX
  11248. {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
  11249. #endif
  11250. #ifdef _SC_LOGNAME_MAX
  11251. {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
  11252. #endif
  11253. #ifdef _SC_LONG_BIT
  11254. {"SC_LONG_BIT", _SC_LONG_BIT},
  11255. #endif
  11256. #ifdef _SC_MAC
  11257. {"SC_MAC", _SC_MAC},
  11258. #endif
  11259. #ifdef _SC_MAPPED_FILES
  11260. {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
  11261. #endif
  11262. #ifdef _SC_MAXPID
  11263. {"SC_MAXPID", _SC_MAXPID},
  11264. #endif
  11265. #ifdef _SC_MB_LEN_MAX
  11266. {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
  11267. #endif
  11268. #ifdef _SC_MEMLOCK
  11269. {"SC_MEMLOCK", _SC_MEMLOCK},
  11270. #endif
  11271. #ifdef _SC_MEMLOCK_RANGE
  11272. {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
  11273. #endif
  11274. #ifdef _SC_MEMORY_PROTECTION
  11275. {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
  11276. #endif
  11277. #ifdef _SC_MESSAGE_PASSING
  11278. {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
  11279. #endif
  11280. #ifdef _SC_MMAP_FIXED_ALIGNMENT
  11281. {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
  11282. #endif
  11283. #ifdef _SC_MQ_OPEN_MAX
  11284. {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
  11285. #endif
  11286. #ifdef _SC_MQ_PRIO_MAX
  11287. {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
  11288. #endif
  11289. #ifdef _SC_NACLS_MAX
  11290. {"SC_NACLS_MAX", _SC_NACLS_MAX},
  11291. #endif
  11292. #ifdef _SC_NGROUPS_MAX
  11293. {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
  11294. #endif
  11295. #ifdef _SC_NL_ARGMAX
  11296. {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
  11297. #endif
  11298. #ifdef _SC_NL_LANGMAX
  11299. {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
  11300. #endif
  11301. #ifdef _SC_NL_MSGMAX
  11302. {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
  11303. #endif
  11304. #ifdef _SC_NL_NMAX
  11305. {"SC_NL_NMAX", _SC_NL_NMAX},
  11306. #endif
  11307. #ifdef _SC_NL_SETMAX
  11308. {"SC_NL_SETMAX", _SC_NL_SETMAX},
  11309. #endif
  11310. #ifdef _SC_NL_TEXTMAX
  11311. {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
  11312. #endif
  11313. #ifdef _SC_NPROCESSORS_CONF
  11314. {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
  11315. #endif
  11316. #ifdef _SC_NPROCESSORS_ONLN
  11317. {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
  11318. #endif
  11319. #ifdef _SC_NPROC_CONF
  11320. {"SC_NPROC_CONF", _SC_NPROC_CONF},
  11321. #endif
  11322. #ifdef _SC_NPROC_ONLN
  11323. {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
  11324. #endif
  11325. #ifdef _SC_NZERO
  11326. {"SC_NZERO", _SC_NZERO},
  11327. #endif
  11328. #ifdef _SC_OPEN_MAX
  11329. {"SC_OPEN_MAX", _SC_OPEN_MAX},
  11330. #endif
  11331. #ifdef _SC_PAGESIZE
  11332. {"SC_PAGESIZE", _SC_PAGESIZE},
  11333. #endif
  11334. #ifdef _SC_PAGE_SIZE
  11335. {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
  11336. #endif
  11337. #ifdef _SC_AIX_REALMEM
  11338. {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
  11339. #endif
  11340. #ifdef _SC_PASS_MAX
  11341. {"SC_PASS_MAX", _SC_PASS_MAX},
  11342. #endif
  11343. #ifdef _SC_PHYS_PAGES
  11344. {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
  11345. #endif
  11346. #ifdef _SC_PII
  11347. {"SC_PII", _SC_PII},
  11348. #endif
  11349. #ifdef _SC_PII_INTERNET
  11350. {"SC_PII_INTERNET", _SC_PII_INTERNET},
  11351. #endif
  11352. #ifdef _SC_PII_INTERNET_DGRAM
  11353. {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
  11354. #endif
  11355. #ifdef _SC_PII_INTERNET_STREAM
  11356. {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
  11357. #endif
  11358. #ifdef _SC_PII_OSI
  11359. {"SC_PII_OSI", _SC_PII_OSI},
  11360. #endif
  11361. #ifdef _SC_PII_OSI_CLTS
  11362. {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
  11363. #endif
  11364. #ifdef _SC_PII_OSI_COTS
  11365. {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
  11366. #endif
  11367. #ifdef _SC_PII_OSI_M
  11368. {"SC_PII_OSI_M", _SC_PII_OSI_M},
  11369. #endif
  11370. #ifdef _SC_PII_SOCKET
  11371. {"SC_PII_SOCKET", _SC_PII_SOCKET},
  11372. #endif
  11373. #ifdef _SC_PII_XTI
  11374. {"SC_PII_XTI", _SC_PII_XTI},
  11375. #endif
  11376. #ifdef _SC_POLL
  11377. {"SC_POLL", _SC_POLL},
  11378. #endif
  11379. #ifdef _SC_PRIORITIZED_IO
  11380. {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
  11381. #endif
  11382. #ifdef _SC_PRIORITY_SCHEDULING
  11383. {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
  11384. #endif
  11385. #ifdef _SC_REALTIME_SIGNALS
  11386. {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
  11387. #endif
  11388. #ifdef _SC_RE_DUP_MAX
  11389. {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
  11390. #endif
  11391. #ifdef _SC_RTSIG_MAX
  11392. {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
  11393. #endif
  11394. #ifdef _SC_SAVED_IDS
  11395. {"SC_SAVED_IDS", _SC_SAVED_IDS},
  11396. #endif
  11397. #ifdef _SC_SCHAR_MAX
  11398. {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
  11399. #endif
  11400. #ifdef _SC_SCHAR_MIN
  11401. {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
  11402. #endif
  11403. #ifdef _SC_SELECT
  11404. {"SC_SELECT", _SC_SELECT},
  11405. #endif
  11406. #ifdef _SC_SEMAPHORES
  11407. {"SC_SEMAPHORES", _SC_SEMAPHORES},
  11408. #endif
  11409. #ifdef _SC_SEM_NSEMS_MAX
  11410. {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
  11411. #endif
  11412. #ifdef _SC_SEM_VALUE_MAX
  11413. {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
  11414. #endif
  11415. #ifdef _SC_SHARED_MEMORY_OBJECTS
  11416. {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
  11417. #endif
  11418. #ifdef _SC_SHRT_MAX
  11419. {"SC_SHRT_MAX", _SC_SHRT_MAX},
  11420. #endif
  11421. #ifdef _SC_SHRT_MIN
  11422. {"SC_SHRT_MIN", _SC_SHRT_MIN},
  11423. #endif
  11424. #ifdef _SC_SIGQUEUE_MAX
  11425. {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
  11426. #endif
  11427. #ifdef _SC_SIGRT_MAX
  11428. {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
  11429. #endif
  11430. #ifdef _SC_SIGRT_MIN
  11431. {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
  11432. #endif
  11433. #ifdef _SC_SOFTPOWER
  11434. {"SC_SOFTPOWER", _SC_SOFTPOWER},
  11435. #endif
  11436. #ifdef _SC_SPLIT_CACHE
  11437. {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
  11438. #endif
  11439. #ifdef _SC_SSIZE_MAX
  11440. {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
  11441. #endif
  11442. #ifdef _SC_STACK_PROT
  11443. {"SC_STACK_PROT", _SC_STACK_PROT},
  11444. #endif
  11445. #ifdef _SC_STREAM_MAX
  11446. {"SC_STREAM_MAX", _SC_STREAM_MAX},
  11447. #endif
  11448. #ifdef _SC_SYNCHRONIZED_IO
  11449. {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
  11450. #endif
  11451. #ifdef _SC_THREADS
  11452. {"SC_THREADS", _SC_THREADS},
  11453. #endif
  11454. #ifdef _SC_THREAD_ATTR_STACKADDR
  11455. {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
  11456. #endif
  11457. #ifdef _SC_THREAD_ATTR_STACKSIZE
  11458. {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
  11459. #endif
  11460. #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
  11461. {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
  11462. #endif
  11463. #ifdef _SC_THREAD_KEYS_MAX
  11464. {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
  11465. #endif
  11466. #ifdef _SC_THREAD_PRIORITY_SCHEDULING
  11467. {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
  11468. #endif
  11469. #ifdef _SC_THREAD_PRIO_INHERIT
  11470. {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
  11471. #endif
  11472. #ifdef _SC_THREAD_PRIO_PROTECT
  11473. {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
  11474. #endif
  11475. #ifdef _SC_THREAD_PROCESS_SHARED
  11476. {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
  11477. #endif
  11478. #ifdef _SC_THREAD_SAFE_FUNCTIONS
  11479. {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
  11480. #endif
  11481. #ifdef _SC_THREAD_STACK_MIN
  11482. {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
  11483. #endif
  11484. #ifdef _SC_THREAD_THREADS_MAX
  11485. {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
  11486. #endif
  11487. #ifdef _SC_TIMERS
  11488. {"SC_TIMERS", _SC_TIMERS},
  11489. #endif
  11490. #ifdef _SC_TIMER_MAX
  11491. {"SC_TIMER_MAX", _SC_TIMER_MAX},
  11492. #endif
  11493. #ifdef _SC_TTY_NAME_MAX
  11494. {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
  11495. #endif
  11496. #ifdef _SC_TZNAME_MAX
  11497. {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
  11498. #endif
  11499. #ifdef _SC_T_IOV_MAX
  11500. {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
  11501. #endif
  11502. #ifdef _SC_UCHAR_MAX
  11503. {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
  11504. #endif
  11505. #ifdef _SC_UINT_MAX
  11506. {"SC_UINT_MAX", _SC_UINT_MAX},
  11507. #endif
  11508. #ifdef _SC_UIO_MAXIOV
  11509. {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
  11510. #endif
  11511. #ifdef _SC_ULONG_MAX
  11512. {"SC_ULONG_MAX", _SC_ULONG_MAX},
  11513. #endif
  11514. #ifdef _SC_USHRT_MAX
  11515. {"SC_USHRT_MAX", _SC_USHRT_MAX},
  11516. #endif
  11517. #ifdef _SC_VERSION
  11518. {"SC_VERSION", _SC_VERSION},
  11519. #endif
  11520. #ifdef _SC_WORD_BIT
  11521. {"SC_WORD_BIT", _SC_WORD_BIT},
  11522. #endif
  11523. #ifdef _SC_XBS5_ILP32_OFF32
  11524. {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
  11525. #endif
  11526. #ifdef _SC_XBS5_ILP32_OFFBIG
  11527. {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
  11528. #endif
  11529. #ifdef _SC_XBS5_LP64_OFF64
  11530. {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
  11531. #endif
  11532. #ifdef _SC_XBS5_LPBIG_OFFBIG
  11533. {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
  11534. #endif
  11535. #ifdef _SC_XOPEN_CRYPT
  11536. {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
  11537. #endif
  11538. #ifdef _SC_XOPEN_ENH_I18N
  11539. {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
  11540. #endif
  11541. #ifdef _SC_XOPEN_LEGACY
  11542. {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
  11543. #endif
  11544. #ifdef _SC_XOPEN_REALTIME
  11545. {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
  11546. #endif
  11547. #ifdef _SC_XOPEN_REALTIME_THREADS
  11548. {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
  11549. #endif
  11550. #ifdef _SC_XOPEN_SHM
  11551. {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
  11552. #endif
  11553. #ifdef _SC_XOPEN_UNIX
  11554. {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
  11555. #endif
  11556. #ifdef _SC_XOPEN_VERSION
  11557. {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
  11558. #endif
  11559. #ifdef _SC_XOPEN_XCU_VERSION
  11560. {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
  11561. #endif
  11562. #ifdef _SC_XOPEN_XPG2
  11563. {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
  11564. #endif
  11565. #ifdef _SC_XOPEN_XPG3
  11566. {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
  11567. #endif
  11568. #ifdef _SC_XOPEN_XPG4
  11569. {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
  11570. #endif
  11571. #ifdef _SC_MINSIGSTKSZ
  11572. {"SC_MINSIGSTKSZ", _SC_MINSIGSTKSZ},
  11573. #endif
  11574. };
  11575. static int
  11576. conv_sysconf_confname(PyObject *arg, int *valuep)
  11577. {
  11578. return conv_confname(arg, valuep, posix_constants_sysconf,
  11579. sizeof(posix_constants_sysconf)
  11580. / sizeof(struct constdef));
  11581. }
  11582. /*[clinic input]
  11583. os.sysconf -> long
  11584. name: sysconf_confname
  11585. /
  11586. Return an integer-valued system configuration variable.
  11587. [clinic start generated code]*/
  11588. static long
  11589. os_sysconf_impl(PyObject *module, int name)
  11590. /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
  11591. {
  11592. long value;
  11593. errno = 0;
  11594. value = sysconf(name);
  11595. if (value == -1 && errno != 0)
  11596. posix_error();
  11597. return value;
  11598. }
  11599. #endif /* HAVE_SYSCONF */
  11600. /* This code is used to ensure that the tables of configuration value names
  11601. * are in sorted order as required by conv_confname(), and also to build
  11602. * the exported dictionaries that are used to publish information about the
  11603. * names available on the host platform.
  11604. *
  11605. * Sorting the table at runtime ensures that the table is properly ordered
  11606. * when used, even for platforms we're not able to test on. It also makes
  11607. * it easier to add additional entries to the tables.
  11608. */
  11609. static int
  11610. cmp_constdefs(const void *v1, const void *v2)
  11611. {
  11612. const struct constdef *c1 =
  11613. (const struct constdef *) v1;
  11614. const struct constdef *c2 =
  11615. (const struct constdef *) v2;
  11616. return strcmp(c1->name, c2->name);
  11617. }
  11618. static int
  11619. setup_confname_table(struct constdef *table, size_t tablesize,
  11620. const char *tablename, PyObject *module)
  11621. {
  11622. PyObject *d = NULL;
  11623. size_t i;
  11624. qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
  11625. d = PyDict_New();
  11626. if (d == NULL)
  11627. return -1;
  11628. for (i=0; i < tablesize; ++i) {
  11629. PyObject *o = PyLong_FromLong(table[i].value);
  11630. if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
  11631. Py_XDECREF(o);
  11632. Py_DECREF(d);
  11633. return -1;
  11634. }
  11635. Py_DECREF(o);
  11636. }
  11637. return _PyModule_Add(module, tablename, d);
  11638. }
  11639. /* Return -1 on failure, 0 on success. */
  11640. static int
  11641. setup_confname_tables(PyObject *module)
  11642. {
  11643. #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
  11644. if (setup_confname_table(posix_constants_pathconf,
  11645. sizeof(posix_constants_pathconf)
  11646. / sizeof(struct constdef),
  11647. "pathconf_names", module))
  11648. return -1;
  11649. #endif
  11650. #ifdef HAVE_CONFSTR
  11651. if (setup_confname_table(posix_constants_confstr,
  11652. sizeof(posix_constants_confstr)
  11653. / sizeof(struct constdef),
  11654. "confstr_names", module))
  11655. return -1;
  11656. #endif
  11657. #ifdef HAVE_SYSCONF
  11658. if (setup_confname_table(posix_constants_sysconf,
  11659. sizeof(posix_constants_sysconf)
  11660. / sizeof(struct constdef),
  11661. "sysconf_names", module))
  11662. return -1;
  11663. #endif
  11664. return 0;
  11665. }
  11666. /*[clinic input]
  11667. os.abort
  11668. Abort the interpreter immediately.
  11669. This function 'dumps core' or otherwise fails in the hardest way possible
  11670. on the hosting operating system. This function never returns.
  11671. [clinic start generated code]*/
  11672. static PyObject *
  11673. os_abort_impl(PyObject *module)
  11674. /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
  11675. {
  11676. abort();
  11677. /*NOTREACHED*/
  11678. #ifndef __clang__
  11679. /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
  11680. GCC emits a warning without "return NULL;" (compiler bug?), but Clang
  11681. is smarter and emits a warning on the return. */
  11682. Py_FatalError("abort() called from Python code didn't abort!");
  11683. return NULL;
  11684. #endif
  11685. }
  11686. #ifdef MS_WINDOWS
  11687. /* Grab ShellExecute dynamically from shell32 */
  11688. static int has_ShellExecute = -1;
  11689. static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
  11690. LPCWSTR, INT);
  11691. static int
  11692. check_ShellExecute(void)
  11693. {
  11694. HINSTANCE hShell32;
  11695. /* only recheck */
  11696. if (-1 == has_ShellExecute) {
  11697. Py_BEGIN_ALLOW_THREADS
  11698. /* Security note: this call is not vulnerable to "DLL hijacking".
  11699. SHELL32 is part of "KnownDLLs" and so Windows always load
  11700. the system SHELL32.DLL, even if there is another SHELL32.DLL
  11701. in the DLL search path. */
  11702. hShell32 = LoadLibraryW(L"SHELL32");
  11703. if (hShell32) {
  11704. *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
  11705. "ShellExecuteW");
  11706. has_ShellExecute = Py_ShellExecuteW != NULL;
  11707. } else {
  11708. has_ShellExecute = 0;
  11709. }
  11710. Py_END_ALLOW_THREADS
  11711. }
  11712. return has_ShellExecute;
  11713. }
  11714. /*[clinic input]
  11715. os.startfile
  11716. filepath: path_t
  11717. operation: Py_UNICODE = NULL
  11718. arguments: Py_UNICODE = NULL
  11719. cwd: path_t(nullable=True) = None
  11720. show_cmd: int = 1
  11721. Start a file with its associated application.
  11722. When "operation" is not specified or "open", this acts like
  11723. double-clicking the file in Explorer, or giving the file name as an
  11724. argument to the DOS "start" command: the file is opened with whatever
  11725. application (if any) its extension is associated.
  11726. When another "operation" is given, it specifies what should be done with
  11727. the file. A typical operation is "print".
  11728. "arguments" is passed to the application, but should be omitted if the
  11729. file is a document.
  11730. "cwd" is the working directory for the operation. If "filepath" is
  11731. relative, it will be resolved against this directory. This argument
  11732. should usually be an absolute path.
  11733. "show_cmd" can be used to override the recommended visibility option.
  11734. See the Windows ShellExecute documentation for values.
  11735. startfile returns as soon as the associated application is launched.
  11736. There is no option to wait for the application to close, and no way
  11737. to retrieve the application's exit status.
  11738. The filepath is relative to the current directory. If you want to use
  11739. an absolute path, make sure the first character is not a slash ("/");
  11740. the underlying Win32 ShellExecute function doesn't work if it is.
  11741. [clinic start generated code]*/
  11742. static PyObject *
  11743. os_startfile_impl(PyObject *module, path_t *filepath,
  11744. const Py_UNICODE *operation, const Py_UNICODE *arguments,
  11745. path_t *cwd, int show_cmd)
  11746. /*[clinic end generated code: output=3baa4f9795841880 input=8248997b80669622]*/
  11747. {
  11748. HINSTANCE rc;
  11749. if(!check_ShellExecute()) {
  11750. /* If the OS doesn't have ShellExecute, return a
  11751. NotImplementedError. */
  11752. return PyErr_Format(PyExc_NotImplementedError,
  11753. "startfile not available on this platform");
  11754. }
  11755. if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
  11756. return NULL;
  11757. }
  11758. if (PySys_Audit("os.startfile/2", "OuuOi", filepath->object, operation,
  11759. arguments, cwd->object ? cwd->object : Py_None,
  11760. show_cmd) < 0) {
  11761. return NULL;
  11762. }
  11763. Py_BEGIN_ALLOW_THREADS
  11764. rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
  11765. arguments, cwd->wide, show_cmd);
  11766. Py_END_ALLOW_THREADS
  11767. if (rc <= (HINSTANCE)32) {
  11768. win32_error_object("startfile", filepath->object);
  11769. return NULL;
  11770. }
  11771. Py_RETURN_NONE;
  11772. }
  11773. #endif /* MS_WINDOWS */
  11774. #ifdef HAVE_GETLOADAVG
  11775. /*[clinic input]
  11776. os.getloadavg
  11777. Return average recent system load information.
  11778. Return the number of processes in the system run queue averaged over
  11779. the last 1, 5, and 15 minutes as a tuple of three floats.
  11780. Raises OSError if the load average was unobtainable.
  11781. [clinic start generated code]*/
  11782. static PyObject *
  11783. os_getloadavg_impl(PyObject *module)
  11784. /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
  11785. {
  11786. double loadavg[3];
  11787. if (getloadavg(loadavg, 3)!=3) {
  11788. PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
  11789. return NULL;
  11790. } else
  11791. return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
  11792. }
  11793. #endif /* HAVE_GETLOADAVG */
  11794. /*[clinic input]
  11795. os.device_encoding
  11796. fd: int
  11797. Return a string describing the encoding of a terminal's file descriptor.
  11798. The file descriptor must be attached to a terminal.
  11799. If the device is not a terminal, return None.
  11800. [clinic start generated code]*/
  11801. static PyObject *
  11802. os_device_encoding_impl(PyObject *module, int fd)
  11803. /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
  11804. {
  11805. return _Py_device_encoding(fd);
  11806. }
  11807. #ifdef HAVE_SETRESUID
  11808. /*[clinic input]
  11809. os.setresuid
  11810. ruid: uid_t
  11811. euid: uid_t
  11812. suid: uid_t
  11813. /
  11814. Set the current process's real, effective, and saved user ids.
  11815. [clinic start generated code]*/
  11816. static PyObject *
  11817. os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
  11818. /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
  11819. {
  11820. if (setresuid(ruid, euid, suid) < 0)
  11821. return posix_error();
  11822. Py_RETURN_NONE;
  11823. }
  11824. #endif /* HAVE_SETRESUID */
  11825. #ifdef HAVE_SETRESGID
  11826. /*[clinic input]
  11827. os.setresgid
  11828. rgid: gid_t
  11829. egid: gid_t
  11830. sgid: gid_t
  11831. /
  11832. Set the current process's real, effective, and saved group ids.
  11833. [clinic start generated code]*/
  11834. static PyObject *
  11835. os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
  11836. /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
  11837. {
  11838. if (setresgid(rgid, egid, sgid) < 0)
  11839. return posix_error();
  11840. Py_RETURN_NONE;
  11841. }
  11842. #endif /* HAVE_SETRESGID */
  11843. #ifdef HAVE_GETRESUID
  11844. /*[clinic input]
  11845. os.getresuid
  11846. Return a tuple of the current process's real, effective, and saved user ids.
  11847. [clinic start generated code]*/
  11848. static PyObject *
  11849. os_getresuid_impl(PyObject *module)
  11850. /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
  11851. {
  11852. uid_t ruid, euid, suid;
  11853. if (getresuid(&ruid, &euid, &suid) < 0)
  11854. return posix_error();
  11855. return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
  11856. _PyLong_FromUid(euid),
  11857. _PyLong_FromUid(suid));
  11858. }
  11859. #endif /* HAVE_GETRESUID */
  11860. #ifdef HAVE_GETRESGID
  11861. /*[clinic input]
  11862. os.getresgid
  11863. Return a tuple of the current process's real, effective, and saved group ids.
  11864. [clinic start generated code]*/
  11865. static PyObject *
  11866. os_getresgid_impl(PyObject *module)
  11867. /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
  11868. {
  11869. gid_t rgid, egid, sgid;
  11870. if (getresgid(&rgid, &egid, &sgid) < 0)
  11871. return posix_error();
  11872. return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
  11873. _PyLong_FromGid(egid),
  11874. _PyLong_FromGid(sgid));
  11875. }
  11876. #endif /* HAVE_GETRESGID */
  11877. #ifdef USE_XATTRS
  11878. /*[clinic input]
  11879. os.getxattr
  11880. path: path_t(allow_fd=True)
  11881. attribute: path_t
  11882. *
  11883. follow_symlinks: bool = True
  11884. Return the value of extended attribute attribute on path.
  11885. path may be either a string, a path-like object, or an open file descriptor.
  11886. If follow_symlinks is False, and the last element of the path is a symbolic
  11887. link, getxattr will examine the symbolic link itself instead of the file
  11888. the link points to.
  11889. [clinic start generated code]*/
  11890. static PyObject *
  11891. os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
  11892. int follow_symlinks)
  11893. /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
  11894. {
  11895. Py_ssize_t i;
  11896. PyObject *buffer = NULL;
  11897. if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
  11898. return NULL;
  11899. if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
  11900. return NULL;
  11901. }
  11902. for (i = 0; ; i++) {
  11903. void *ptr;
  11904. ssize_t result;
  11905. static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
  11906. Py_ssize_t buffer_size = buffer_sizes[i];
  11907. if (!buffer_size) {
  11908. path_error(path);
  11909. return NULL;
  11910. }
  11911. buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
  11912. if (!buffer)
  11913. return NULL;
  11914. ptr = PyBytes_AS_STRING(buffer);
  11915. Py_BEGIN_ALLOW_THREADS;
  11916. if (path->fd >= 0)
  11917. result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
  11918. else if (follow_symlinks)
  11919. result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
  11920. else
  11921. result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
  11922. Py_END_ALLOW_THREADS;
  11923. if (result < 0) {
  11924. if (errno == ERANGE) {
  11925. Py_DECREF(buffer);
  11926. continue;
  11927. }
  11928. path_error(path);
  11929. Py_DECREF(buffer);
  11930. return NULL;
  11931. }
  11932. if (result != buffer_size) {
  11933. /* Can only shrink. */
  11934. _PyBytes_Resize(&buffer, result);
  11935. }
  11936. break;
  11937. }
  11938. return buffer;
  11939. }
  11940. /*[clinic input]
  11941. os.setxattr
  11942. path: path_t(allow_fd=True)
  11943. attribute: path_t
  11944. value: Py_buffer
  11945. flags: int = 0
  11946. *
  11947. follow_symlinks: bool = True
  11948. Set extended attribute attribute on path to value.
  11949. path may be either a string, a path-like object, or an open file descriptor.
  11950. If follow_symlinks is False, and the last element of the path is a symbolic
  11951. link, setxattr will modify the symbolic link itself instead of the file
  11952. the link points to.
  11953. [clinic start generated code]*/
  11954. static PyObject *
  11955. os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
  11956. Py_buffer *value, int flags, int follow_symlinks)
  11957. /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
  11958. {
  11959. ssize_t result;
  11960. if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
  11961. return NULL;
  11962. if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
  11963. value->buf, value->len, flags) < 0) {
  11964. return NULL;
  11965. }
  11966. Py_BEGIN_ALLOW_THREADS;
  11967. if (path->fd > -1)
  11968. result = fsetxattr(path->fd, attribute->narrow,
  11969. value->buf, value->len, flags);
  11970. else if (follow_symlinks)
  11971. result = setxattr(path->narrow, attribute->narrow,
  11972. value->buf, value->len, flags);
  11973. else
  11974. result = lsetxattr(path->narrow, attribute->narrow,
  11975. value->buf, value->len, flags);
  11976. Py_END_ALLOW_THREADS;
  11977. if (result) {
  11978. path_error(path);
  11979. return NULL;
  11980. }
  11981. Py_RETURN_NONE;
  11982. }
  11983. /*[clinic input]
  11984. os.removexattr
  11985. path: path_t(allow_fd=True)
  11986. attribute: path_t
  11987. *
  11988. follow_symlinks: bool = True
  11989. Remove extended attribute attribute on path.
  11990. path may be either a string, a path-like object, or an open file descriptor.
  11991. If follow_symlinks is False, and the last element of the path is a symbolic
  11992. link, removexattr will modify the symbolic link itself instead of the file
  11993. the link points to.
  11994. [clinic start generated code]*/
  11995. static PyObject *
  11996. os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
  11997. int follow_symlinks)
  11998. /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
  11999. {
  12000. ssize_t result;
  12001. if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
  12002. return NULL;
  12003. if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
  12004. return NULL;
  12005. }
  12006. Py_BEGIN_ALLOW_THREADS;
  12007. if (path->fd > -1)
  12008. result = fremovexattr(path->fd, attribute->narrow);
  12009. else if (follow_symlinks)
  12010. result = removexattr(path->narrow, attribute->narrow);
  12011. else
  12012. result = lremovexattr(path->narrow, attribute->narrow);
  12013. Py_END_ALLOW_THREADS;
  12014. if (result) {
  12015. return path_error(path);
  12016. }
  12017. Py_RETURN_NONE;
  12018. }
  12019. /*[clinic input]
  12020. os.listxattr
  12021. path: path_t(allow_fd=True, nullable=True) = None
  12022. *
  12023. follow_symlinks: bool = True
  12024. Return a list of extended attributes on path.
  12025. path may be either None, a string, a path-like object, or an open file descriptor.
  12026. if path is None, listxattr will examine the current directory.
  12027. If follow_symlinks is False, and the last element of the path is a symbolic
  12028. link, listxattr will examine the symbolic link itself instead of the file
  12029. the link points to.
  12030. [clinic start generated code]*/
  12031. static PyObject *
  12032. os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
  12033. /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
  12034. {
  12035. Py_ssize_t i;
  12036. PyObject *result = NULL;
  12037. const char *name;
  12038. char *buffer = NULL;
  12039. if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
  12040. goto exit;
  12041. if (PySys_Audit("os.listxattr", "(O)",
  12042. path->object ? path->object : Py_None) < 0) {
  12043. return NULL;
  12044. }
  12045. name = path->narrow ? path->narrow : ".";
  12046. for (i = 0; ; i++) {
  12047. const char *start, *trace, *end;
  12048. ssize_t length;
  12049. static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
  12050. Py_ssize_t buffer_size = buffer_sizes[i];
  12051. if (!buffer_size) {
  12052. /* ERANGE */
  12053. path_error(path);
  12054. break;
  12055. }
  12056. buffer = PyMem_Malloc(buffer_size);
  12057. if (!buffer) {
  12058. PyErr_NoMemory();
  12059. break;
  12060. }
  12061. Py_BEGIN_ALLOW_THREADS;
  12062. if (path->fd > -1)
  12063. length = flistxattr(path->fd, buffer, buffer_size);
  12064. else if (follow_symlinks)
  12065. length = listxattr(name, buffer, buffer_size);
  12066. else
  12067. length = llistxattr(name, buffer, buffer_size);
  12068. Py_END_ALLOW_THREADS;
  12069. if (length < 0) {
  12070. if (errno == ERANGE) {
  12071. PyMem_Free(buffer);
  12072. buffer = NULL;
  12073. continue;
  12074. }
  12075. path_error(path);
  12076. break;
  12077. }
  12078. result = PyList_New(0);
  12079. if (!result) {
  12080. goto exit;
  12081. }
  12082. end = buffer + length;
  12083. for (trace = start = buffer; trace != end; trace++) {
  12084. if (!*trace) {
  12085. int error;
  12086. PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
  12087. trace - start);
  12088. if (!attribute) {
  12089. Py_SETREF(result, NULL);
  12090. goto exit;
  12091. }
  12092. error = PyList_Append(result, attribute);
  12093. Py_DECREF(attribute);
  12094. if (error) {
  12095. Py_SETREF(result, NULL);
  12096. goto exit;
  12097. }
  12098. start = trace + 1;
  12099. }
  12100. }
  12101. break;
  12102. }
  12103. exit:
  12104. if (buffer)
  12105. PyMem_Free(buffer);
  12106. return result;
  12107. }
  12108. #endif /* USE_XATTRS */
  12109. /*[clinic input]
  12110. os.urandom
  12111. size: Py_ssize_t
  12112. /
  12113. Return a bytes object containing random bytes suitable for cryptographic use.
  12114. [clinic start generated code]*/
  12115. static PyObject *
  12116. os_urandom_impl(PyObject *module, Py_ssize_t size)
  12117. /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
  12118. {
  12119. PyObject *bytes;
  12120. int result;
  12121. if (size < 0)
  12122. return PyErr_Format(PyExc_ValueError,
  12123. "negative argument not allowed");
  12124. bytes = PyBytes_FromStringAndSize(NULL, size);
  12125. if (bytes == NULL)
  12126. return NULL;
  12127. result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
  12128. if (result == -1) {
  12129. Py_DECREF(bytes);
  12130. return NULL;
  12131. }
  12132. return bytes;
  12133. }
  12134. #ifdef HAVE_MEMFD_CREATE
  12135. /*[clinic input]
  12136. os.memfd_create
  12137. name: FSConverter
  12138. flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
  12139. [clinic start generated code]*/
  12140. static PyObject *
  12141. os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
  12142. /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
  12143. {
  12144. int fd;
  12145. const char *bytes = PyBytes_AS_STRING(name);
  12146. Py_BEGIN_ALLOW_THREADS
  12147. fd = memfd_create(bytes, flags);
  12148. Py_END_ALLOW_THREADS
  12149. if (fd == -1) {
  12150. return PyErr_SetFromErrno(PyExc_OSError);
  12151. }
  12152. return PyLong_FromLong(fd);
  12153. }
  12154. #endif
  12155. #if defined(HAVE_EVENTFD) && defined(EFD_CLOEXEC)
  12156. /*[clinic input]
  12157. os.eventfd
  12158. initval: unsigned_int
  12159. flags: int(c_default="EFD_CLOEXEC") = EFD_CLOEXEC
  12160. Creates and returns an event notification file descriptor.
  12161. [clinic start generated code]*/
  12162. static PyObject *
  12163. os_eventfd_impl(PyObject *module, unsigned int initval, int flags)
  12164. /*[clinic end generated code: output=ce9c9bbd1446f2de input=66203e3c50c4028b]*/
  12165. {
  12166. /* initval is limited to uint32_t, internal counter is uint64_t */
  12167. int fd;
  12168. Py_BEGIN_ALLOW_THREADS
  12169. fd = eventfd(initval, flags);
  12170. Py_END_ALLOW_THREADS
  12171. if (fd == -1) {
  12172. return PyErr_SetFromErrno(PyExc_OSError);
  12173. }
  12174. return PyLong_FromLong(fd);
  12175. }
  12176. /*[clinic input]
  12177. os.eventfd_read
  12178. fd: fildes
  12179. Read eventfd value
  12180. [clinic start generated code]*/
  12181. static PyObject *
  12182. os_eventfd_read_impl(PyObject *module, int fd)
  12183. /*[clinic end generated code: output=8f2c7b59a3521fd1 input=110f8b57fa596afe]*/
  12184. {
  12185. eventfd_t value;
  12186. int result;
  12187. Py_BEGIN_ALLOW_THREADS
  12188. result = eventfd_read(fd, &value);
  12189. Py_END_ALLOW_THREADS
  12190. if (result == -1) {
  12191. return PyErr_SetFromErrno(PyExc_OSError);
  12192. }
  12193. return PyLong_FromUnsignedLongLong(value);
  12194. }
  12195. /*[clinic input]
  12196. os.eventfd_write
  12197. fd: fildes
  12198. value: unsigned_long_long
  12199. Write eventfd value.
  12200. [clinic start generated code]*/
  12201. static PyObject *
  12202. os_eventfd_write_impl(PyObject *module, int fd, unsigned long long value)
  12203. /*[clinic end generated code: output=bebd9040bbf987f5 input=156de8555be5a949]*/
  12204. {
  12205. int result;
  12206. Py_BEGIN_ALLOW_THREADS
  12207. result = eventfd_write(fd, value);
  12208. Py_END_ALLOW_THREADS
  12209. if (result == -1) {
  12210. return PyErr_SetFromErrno(PyExc_OSError);
  12211. }
  12212. Py_RETURN_NONE;
  12213. }
  12214. #endif /* HAVE_EVENTFD && EFD_CLOEXEC */
  12215. /* Terminal size querying */
  12216. PyDoc_STRVAR(TerminalSize_docstring,
  12217. "A tuple of (columns, lines) for holding terminal window size");
  12218. static PyStructSequence_Field TerminalSize_fields[] = {
  12219. {"columns", "width of the terminal window in characters"},
  12220. {"lines", "height of the terminal window in characters"},
  12221. {NULL, NULL}
  12222. };
  12223. static PyStructSequence_Desc TerminalSize_desc = {
  12224. "os.terminal_size",
  12225. TerminalSize_docstring,
  12226. TerminalSize_fields,
  12227. 2,
  12228. };
  12229. #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
  12230. /*[clinic input]
  12231. os.get_terminal_size
  12232. fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
  12233. /
  12234. Return the size of the terminal window as (columns, lines).
  12235. The optional argument fd (default standard output) specifies
  12236. which file descriptor should be queried.
  12237. If the file descriptor is not connected to a terminal, an OSError
  12238. is thrown.
  12239. This function will only be defined if an implementation is
  12240. available for this system.
  12241. shutil.get_terminal_size is the high-level function which should
  12242. normally be used, os.get_terminal_size is the low-level implementation.
  12243. [clinic start generated code]*/
  12244. static PyObject *
  12245. os_get_terminal_size_impl(PyObject *module, int fd)
  12246. /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
  12247. {
  12248. int columns, lines;
  12249. PyObject *termsize;
  12250. /* Under some conditions stdout may not be connected and
  12251. * fileno(stdout) may point to an invalid file descriptor. For example
  12252. * GUI apps don't have valid standard streams by default.
  12253. *
  12254. * If this happens, and the optional fd argument is not present,
  12255. * the ioctl below will fail returning EBADF. This is what we want.
  12256. */
  12257. #ifdef TERMSIZE_USE_IOCTL
  12258. {
  12259. struct winsize w;
  12260. if (ioctl(fd, TIOCGWINSZ, &w))
  12261. return PyErr_SetFromErrno(PyExc_OSError);
  12262. columns = w.ws_col;
  12263. lines = w.ws_row;
  12264. }
  12265. #endif /* TERMSIZE_USE_IOCTL */
  12266. #ifdef TERMSIZE_USE_CONIO
  12267. {
  12268. HANDLE handle;
  12269. CONSOLE_SCREEN_BUFFER_INFO csbi;
  12270. handle = _Py_get_osfhandle(fd);
  12271. if (handle == INVALID_HANDLE_VALUE)
  12272. return NULL;
  12273. if (!GetConsoleScreenBufferInfo(handle, &csbi))
  12274. return PyErr_SetFromWindowsErr(0);
  12275. columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  12276. lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  12277. }
  12278. #endif /* TERMSIZE_USE_CONIO */
  12279. PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
  12280. termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
  12281. if (termsize == NULL)
  12282. return NULL;
  12283. int pos = 0;
  12284. #define SET_TERMSIZE(CALL) \
  12285. do { \
  12286. PyObject *item = (CALL); \
  12287. if (item == NULL) { \
  12288. Py_DECREF(termsize); \
  12289. return NULL; \
  12290. } \
  12291. PyStructSequence_SET_ITEM(termsize, pos++, item); \
  12292. } while(0)
  12293. SET_TERMSIZE(PyLong_FromLong(columns));
  12294. SET_TERMSIZE(PyLong_FromLong(lines));
  12295. #undef SET_TERMSIZE
  12296. return termsize;
  12297. }
  12298. #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
  12299. /*[clinic input]
  12300. os.cpu_count
  12301. Return the number of CPUs in the system; return None if indeterminable.
  12302. This number is not equivalent to the number of CPUs the current process can
  12303. use. The number of usable CPUs can be obtained with
  12304. ``len(os.sched_getaffinity(0))``
  12305. [clinic start generated code]*/
  12306. static PyObject *
  12307. os_cpu_count_impl(PyObject *module)
  12308. /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
  12309. {
  12310. int ncpu = 0;
  12311. #ifdef MS_WINDOWS
  12312. #ifdef MS_WINDOWS_DESKTOP
  12313. ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
  12314. #endif
  12315. #elif defined(__hpux)
  12316. ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
  12317. #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
  12318. ncpu = sysconf(_SC_NPROCESSORS_ONLN);
  12319. #elif defined(__VXWORKS__)
  12320. ncpu = _Py_popcount32(vxCpuEnabledGet());
  12321. #elif defined(__DragonFly__) || \
  12322. defined(__OpenBSD__) || \
  12323. defined(__FreeBSD__) || \
  12324. defined(__NetBSD__) || \
  12325. defined(__APPLE__)
  12326. int mib[2];
  12327. size_t len = sizeof(ncpu);
  12328. mib[0] = CTL_HW;
  12329. mib[1] = HW_NCPU;
  12330. if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
  12331. ncpu = 0;
  12332. #endif
  12333. if (ncpu >= 1)
  12334. return PyLong_FromLong(ncpu);
  12335. else
  12336. Py_RETURN_NONE;
  12337. }
  12338. /*[clinic input]
  12339. os.get_inheritable -> bool
  12340. fd: int
  12341. /
  12342. Get the close-on-exe flag of the specified file descriptor.
  12343. [clinic start generated code]*/
  12344. static int
  12345. os_get_inheritable_impl(PyObject *module, int fd)
  12346. /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
  12347. {
  12348. int return_value;
  12349. _Py_BEGIN_SUPPRESS_IPH
  12350. return_value = _Py_get_inheritable(fd);
  12351. _Py_END_SUPPRESS_IPH
  12352. return return_value;
  12353. }
  12354. /*[clinic input]
  12355. os.set_inheritable
  12356. fd: int
  12357. inheritable: int
  12358. /
  12359. Set the inheritable flag of the specified file descriptor.
  12360. [clinic start generated code]*/
  12361. static PyObject *
  12362. os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
  12363. /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
  12364. {
  12365. int result;
  12366. _Py_BEGIN_SUPPRESS_IPH
  12367. result = _Py_set_inheritable(fd, inheritable, NULL);
  12368. _Py_END_SUPPRESS_IPH
  12369. if (result < 0)
  12370. return NULL;
  12371. Py_RETURN_NONE;
  12372. }
  12373. #ifdef MS_WINDOWS
  12374. #ifndef HANDLE_FLAG_INHERIT
  12375. #define HANDLE_FLAG_INHERIT 0x00000001
  12376. #endif
  12377. /*[clinic input]
  12378. os.get_handle_inheritable -> bool
  12379. handle: intptr_t
  12380. /
  12381. Get the close-on-exe flag of the specified file descriptor.
  12382. [clinic start generated code]*/
  12383. static int
  12384. os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
  12385. /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
  12386. {
  12387. DWORD flags;
  12388. if (!GetHandleInformation((HANDLE)handle, &flags)) {
  12389. PyErr_SetFromWindowsErr(0);
  12390. return -1;
  12391. }
  12392. return flags & HANDLE_FLAG_INHERIT;
  12393. }
  12394. /*[clinic input]
  12395. os.set_handle_inheritable
  12396. handle: intptr_t
  12397. inheritable: bool
  12398. /
  12399. Set the inheritable flag of the specified handle.
  12400. [clinic start generated code]*/
  12401. static PyObject *
  12402. os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
  12403. int inheritable)
  12404. /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
  12405. {
  12406. DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
  12407. if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
  12408. PyErr_SetFromWindowsErr(0);
  12409. return NULL;
  12410. }
  12411. Py_RETURN_NONE;
  12412. }
  12413. #endif /* MS_WINDOWS */
  12414. /*[clinic input]
  12415. os.get_blocking -> bool
  12416. fd: int
  12417. /
  12418. Get the blocking mode of the file descriptor.
  12419. Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
  12420. [clinic start generated code]*/
  12421. static int
  12422. os_get_blocking_impl(PyObject *module, int fd)
  12423. /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
  12424. {
  12425. int blocking;
  12426. _Py_BEGIN_SUPPRESS_IPH
  12427. blocking = _Py_get_blocking(fd);
  12428. _Py_END_SUPPRESS_IPH
  12429. return blocking;
  12430. }
  12431. /*[clinic input]
  12432. os.set_blocking
  12433. fd: int
  12434. blocking: bool
  12435. /
  12436. Set the blocking mode of the specified file descriptor.
  12437. Set the O_NONBLOCK flag if blocking is False,
  12438. clear the O_NONBLOCK flag otherwise.
  12439. [clinic start generated code]*/
  12440. static PyObject *
  12441. os_set_blocking_impl(PyObject *module, int fd, int blocking)
  12442. /*[clinic end generated code: output=384eb43aa0762a9d input=7e9dfc9b14804dd4]*/
  12443. {
  12444. int result;
  12445. _Py_BEGIN_SUPPRESS_IPH
  12446. result = _Py_set_blocking(fd, blocking);
  12447. _Py_END_SUPPRESS_IPH
  12448. if (result < 0)
  12449. return NULL;
  12450. Py_RETURN_NONE;
  12451. }
  12452. /*[clinic input]
  12453. class os.DirEntry "DirEntry *" "DirEntryType"
  12454. [clinic start generated code]*/
  12455. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
  12456. typedef struct {
  12457. PyObject_HEAD
  12458. PyObject *name;
  12459. PyObject *path;
  12460. PyObject *stat;
  12461. PyObject *lstat;
  12462. #ifdef MS_WINDOWS
  12463. struct _Py_stat_struct win32_lstat;
  12464. uint64_t win32_file_index;
  12465. uint64_t win32_file_index_high;
  12466. int got_file_index;
  12467. #else /* POSIX */
  12468. #ifdef HAVE_DIRENT_D_TYPE
  12469. unsigned char d_type;
  12470. #endif
  12471. ino_t d_ino;
  12472. int dir_fd;
  12473. #endif
  12474. } DirEntry;
  12475. static void
  12476. DirEntry_dealloc(DirEntry *entry)
  12477. {
  12478. PyTypeObject *tp = Py_TYPE(entry);
  12479. Py_XDECREF(entry->name);
  12480. Py_XDECREF(entry->path);
  12481. Py_XDECREF(entry->stat);
  12482. Py_XDECREF(entry->lstat);
  12483. freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
  12484. free_func(entry);
  12485. Py_DECREF(tp);
  12486. }
  12487. /* Forward reference */
  12488. static int
  12489. DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
  12490. int follow_symlinks, unsigned short mode_bits);
  12491. /*[clinic input]
  12492. os.DirEntry.is_symlink -> bool
  12493. defining_class: defining_class
  12494. /
  12495. Return True if the entry is a symbolic link; cached per entry.
  12496. [clinic start generated code]*/
  12497. static int
  12498. os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class)
  12499. /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/
  12500. {
  12501. #ifdef MS_WINDOWS
  12502. return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
  12503. #elif defined(HAVE_DIRENT_D_TYPE)
  12504. /* POSIX */
  12505. if (self->d_type != DT_UNKNOWN)
  12506. return self->d_type == DT_LNK;
  12507. else
  12508. return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
  12509. #else
  12510. /* POSIX without d_type */
  12511. return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
  12512. #endif
  12513. }
  12514. /*[clinic input]
  12515. os.DirEntry.is_junction -> bool
  12516. defining_class: defining_class
  12517. /
  12518. Return True if the entry is a junction; cached per entry.
  12519. [clinic start generated code]*/
  12520. static int
  12521. os_DirEntry_is_junction_impl(DirEntry *self, PyTypeObject *defining_class)
  12522. /*[clinic end generated code: output=7061a07b0ef2cd1f input=475cd36fb7d4723f]*/
  12523. {
  12524. #ifdef MS_WINDOWS
  12525. return self->win32_lstat.st_reparse_tag == IO_REPARSE_TAG_MOUNT_POINT;
  12526. #else
  12527. return 0;
  12528. #endif
  12529. }
  12530. static PyObject *
  12531. DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
  12532. {
  12533. int result;
  12534. STRUCT_STAT st;
  12535. PyObject *ub;
  12536. #ifdef MS_WINDOWS
  12537. if (!PyUnicode_FSDecoder(self->path, &ub))
  12538. return NULL;
  12539. wchar_t *path = PyUnicode_AsWideCharString(ub, NULL);
  12540. Py_DECREF(ub);
  12541. #else /* POSIX */
  12542. if (!PyUnicode_FSConverter(self->path, &ub))
  12543. return NULL;
  12544. const char *path = PyBytes_AS_STRING(ub);
  12545. if (self->dir_fd != DEFAULT_DIR_FD) {
  12546. #ifdef HAVE_FSTATAT
  12547. if (HAVE_FSTATAT_RUNTIME) {
  12548. Py_BEGIN_ALLOW_THREADS
  12549. result = fstatat(self->dir_fd, path, &st,
  12550. follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
  12551. Py_END_ALLOW_THREADS
  12552. } else
  12553. #endif /* HAVE_FSTATAT */
  12554. {
  12555. Py_DECREF(ub);
  12556. PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
  12557. return NULL;
  12558. }
  12559. }
  12560. else
  12561. #endif
  12562. {
  12563. Py_BEGIN_ALLOW_THREADS
  12564. if (follow_symlinks) {
  12565. result = STAT(path, &st);
  12566. }
  12567. else {
  12568. result = LSTAT(path, &st);
  12569. }
  12570. Py_END_ALLOW_THREADS
  12571. }
  12572. int saved_errno = errno;
  12573. #if defined(MS_WINDOWS)
  12574. PyMem_Free(path);
  12575. #else
  12576. Py_DECREF(ub);
  12577. #endif
  12578. if (result != 0) {
  12579. errno = saved_errno;
  12580. path_object_error(self->path);
  12581. return NULL;
  12582. }
  12583. return _pystat_fromstructstat(module, &st);
  12584. }
  12585. static PyObject *
  12586. DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self)
  12587. {
  12588. if (!self->lstat) {
  12589. PyObject *module = PyType_GetModule(defining_class);
  12590. #ifdef MS_WINDOWS
  12591. self->lstat = _pystat_fromstructstat(module, &self->win32_lstat);
  12592. #else /* POSIX */
  12593. self->lstat = DirEntry_fetch_stat(module, self, 0);
  12594. #endif
  12595. }
  12596. return Py_XNewRef(self->lstat);
  12597. }
  12598. /*[clinic input]
  12599. os.DirEntry.stat
  12600. defining_class: defining_class
  12601. /
  12602. *
  12603. follow_symlinks: bool = True
  12604. Return stat_result object for the entry; cached per entry.
  12605. [clinic start generated code]*/
  12606. static PyObject *
  12607. os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
  12608. int follow_symlinks)
  12609. /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
  12610. {
  12611. if (!follow_symlinks) {
  12612. return DirEntry_get_lstat(defining_class, self);
  12613. }
  12614. if (!self->stat) {
  12615. int result = os_DirEntry_is_symlink_impl(self, defining_class);
  12616. if (result == -1) {
  12617. return NULL;
  12618. }
  12619. if (result) {
  12620. PyObject *module = PyType_GetModule(defining_class);
  12621. self->stat = DirEntry_fetch_stat(module, self, 1);
  12622. }
  12623. else {
  12624. self->stat = DirEntry_get_lstat(defining_class, self);
  12625. }
  12626. }
  12627. return Py_XNewRef(self->stat);
  12628. }
  12629. /* Set exception and return -1 on error, 0 for False, 1 for True */
  12630. static int
  12631. DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
  12632. int follow_symlinks, unsigned short mode_bits)
  12633. {
  12634. PyObject *stat = NULL;
  12635. PyObject *st_mode = NULL;
  12636. long mode;
  12637. int result;
  12638. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  12639. int is_symlink;
  12640. int need_stat;
  12641. #endif
  12642. #ifdef MS_WINDOWS
  12643. unsigned long dir_bits;
  12644. #endif
  12645. #ifdef MS_WINDOWS
  12646. is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
  12647. need_stat = follow_symlinks && is_symlink;
  12648. #elif defined(HAVE_DIRENT_D_TYPE)
  12649. is_symlink = self->d_type == DT_LNK;
  12650. need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
  12651. #endif
  12652. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  12653. if (need_stat) {
  12654. #endif
  12655. stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
  12656. if (!stat) {
  12657. if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
  12658. /* If file doesn't exist (anymore), then return False
  12659. (i.e., say it's not a file/directory) */
  12660. PyErr_Clear();
  12661. return 0;
  12662. }
  12663. goto error;
  12664. }
  12665. _posixstate* state = get_posix_state(PyType_GetModule(defining_class));
  12666. st_mode = PyObject_GetAttr(stat, state->st_mode);
  12667. if (!st_mode)
  12668. goto error;
  12669. mode = PyLong_AsLong(st_mode);
  12670. if (mode == -1 && PyErr_Occurred())
  12671. goto error;
  12672. Py_CLEAR(st_mode);
  12673. Py_CLEAR(stat);
  12674. result = (mode & S_IFMT) == mode_bits;
  12675. #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
  12676. }
  12677. else if (is_symlink) {
  12678. assert(mode_bits != S_IFLNK);
  12679. result = 0;
  12680. }
  12681. else {
  12682. assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
  12683. #ifdef MS_WINDOWS
  12684. dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
  12685. if (mode_bits == S_IFDIR)
  12686. result = dir_bits != 0;
  12687. else
  12688. result = dir_bits == 0;
  12689. #else /* POSIX */
  12690. if (mode_bits == S_IFDIR)
  12691. result = self->d_type == DT_DIR;
  12692. else
  12693. result = self->d_type == DT_REG;
  12694. #endif
  12695. }
  12696. #endif
  12697. return result;
  12698. error:
  12699. Py_XDECREF(st_mode);
  12700. Py_XDECREF(stat);
  12701. return -1;
  12702. }
  12703. /*[clinic input]
  12704. os.DirEntry.is_dir -> bool
  12705. defining_class: defining_class
  12706. /
  12707. *
  12708. follow_symlinks: bool = True
  12709. Return True if the entry is a directory; cached per entry.
  12710. [clinic start generated code]*/
  12711. static int
  12712. os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
  12713. int follow_symlinks)
  12714. /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/
  12715. {
  12716. return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR);
  12717. }
  12718. /*[clinic input]
  12719. os.DirEntry.is_file -> bool
  12720. defining_class: defining_class
  12721. /
  12722. *
  12723. follow_symlinks: bool = True
  12724. Return True if the entry is a file; cached per entry.
  12725. [clinic start generated code]*/
  12726. static int
  12727. os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
  12728. int follow_symlinks)
  12729. /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/
  12730. {
  12731. return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG);
  12732. }
  12733. /*[clinic input]
  12734. os.DirEntry.inode
  12735. Return inode of the entry; cached per entry.
  12736. [clinic start generated code]*/
  12737. static PyObject *
  12738. os_DirEntry_inode_impl(DirEntry *self)
  12739. /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
  12740. {
  12741. #ifdef MS_WINDOWS
  12742. if (!self->got_file_index) {
  12743. PyObject *unicode;
  12744. STRUCT_STAT stat;
  12745. int result;
  12746. if (!PyUnicode_FSDecoder(self->path, &unicode))
  12747. return NULL;
  12748. wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
  12749. Py_DECREF(unicode);
  12750. result = LSTAT(path, &stat);
  12751. int saved_errno = errno;
  12752. PyMem_Free(path);
  12753. if (result != 0) {
  12754. errno = saved_errno;
  12755. return path_object_error(self->path);
  12756. }
  12757. self->win32_file_index = stat.st_ino;
  12758. self->win32_file_index_high = stat.st_ino_high;
  12759. self->got_file_index = 1;
  12760. }
  12761. return _pystat_l128_from_l64_l64(self->win32_file_index, self->win32_file_index_high);
  12762. #else /* POSIX */
  12763. static_assert(sizeof(unsigned long long) >= sizeof(self->d_ino),
  12764. "DirEntry.d_ino is larger than unsigned long long");
  12765. return PyLong_FromUnsignedLongLong(self->d_ino);
  12766. #endif
  12767. }
  12768. static PyObject *
  12769. DirEntry_repr(DirEntry *self)
  12770. {
  12771. return PyUnicode_FromFormat("<DirEntry %R>", self->name);
  12772. }
  12773. /*[clinic input]
  12774. os.DirEntry.__fspath__
  12775. Returns the path for the entry.
  12776. [clinic start generated code]*/
  12777. static PyObject *
  12778. os_DirEntry___fspath___impl(DirEntry *self)
  12779. /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
  12780. {
  12781. return Py_NewRef(self->path);
  12782. }
  12783. static PyMemberDef DirEntry_members[] = {
  12784. {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
  12785. "the entry's base filename, relative to scandir() \"path\" argument"},
  12786. {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
  12787. "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
  12788. {NULL}
  12789. };
  12790. #include "clinic/posixmodule.c.h"
  12791. static PyMethodDef DirEntry_methods[] = {
  12792. OS_DIRENTRY_IS_DIR_METHODDEF
  12793. OS_DIRENTRY_IS_FILE_METHODDEF
  12794. OS_DIRENTRY_IS_SYMLINK_METHODDEF
  12795. OS_DIRENTRY_IS_JUNCTION_METHODDEF
  12796. OS_DIRENTRY_STAT_METHODDEF
  12797. OS_DIRENTRY_INODE_METHODDEF
  12798. OS_DIRENTRY___FSPATH___METHODDEF
  12799. {"__class_getitem__", Py_GenericAlias,
  12800. METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  12801. {NULL}
  12802. };
  12803. static PyType_Slot DirEntryType_slots[] = {
  12804. {Py_tp_dealloc, DirEntry_dealloc},
  12805. {Py_tp_repr, DirEntry_repr},
  12806. {Py_tp_methods, DirEntry_methods},
  12807. {Py_tp_members, DirEntry_members},
  12808. {0, 0},
  12809. };
  12810. static PyType_Spec DirEntryType_spec = {
  12811. MODNAME ".DirEntry",
  12812. sizeof(DirEntry),
  12813. 0,
  12814. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
  12815. DirEntryType_slots
  12816. };
  12817. #ifdef MS_WINDOWS
  12818. static wchar_t *
  12819. join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
  12820. {
  12821. Py_ssize_t path_len;
  12822. Py_ssize_t size;
  12823. wchar_t *result;
  12824. wchar_t ch;
  12825. if (!path_wide) { /* Default arg: "." */
  12826. path_wide = L".";
  12827. path_len = 1;
  12828. }
  12829. else {
  12830. path_len = wcslen(path_wide);
  12831. }
  12832. /* The +1's are for the path separator and the NUL */
  12833. size = path_len + 1 + wcslen(filename) + 1;
  12834. result = PyMem_New(wchar_t, size);
  12835. if (!result) {
  12836. PyErr_NoMemory();
  12837. return NULL;
  12838. }
  12839. wcscpy(result, path_wide);
  12840. if (path_len > 0) {
  12841. ch = result[path_len - 1];
  12842. if (ch != SEP && ch != ALTSEP && ch != L':')
  12843. result[path_len++] = SEP;
  12844. wcscpy(result + path_len, filename);
  12845. }
  12846. return result;
  12847. }
  12848. static PyObject *
  12849. DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
  12850. {
  12851. DirEntry *entry;
  12852. BY_HANDLE_FILE_INFORMATION file_info;
  12853. ULONG reparse_tag;
  12854. wchar_t *joined_path;
  12855. PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
  12856. entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
  12857. if (!entry)
  12858. return NULL;
  12859. entry->name = NULL;
  12860. entry->path = NULL;
  12861. entry->stat = NULL;
  12862. entry->lstat = NULL;
  12863. entry->got_file_index = 0;
  12864. entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
  12865. if (!entry->name)
  12866. goto error;
  12867. int return_bytes = path->wide && PyBytes_Check(path->object);
  12868. if (return_bytes) {
  12869. Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
  12870. if (!entry->name)
  12871. goto error;
  12872. }
  12873. joined_path = join_path_filenameW(path->wide, dataW->cFileName);
  12874. if (!joined_path)
  12875. goto error;
  12876. entry->path = PyUnicode_FromWideChar(joined_path, -1);
  12877. PyMem_Free(joined_path);
  12878. if (!entry->path)
  12879. goto error;
  12880. if (return_bytes) {
  12881. Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
  12882. if (!entry->path)
  12883. goto error;
  12884. }
  12885. find_data_to_file_info(dataW, &file_info, &reparse_tag);
  12886. _Py_attribute_data_to_stat(&file_info, reparse_tag, NULL, NULL, &entry->win32_lstat);
  12887. /* ctime is only deprecated from 3.12, so we copy birthtime across */
  12888. entry->win32_lstat.st_ctime = entry->win32_lstat.st_birthtime;
  12889. entry->win32_lstat.st_ctime_nsec = entry->win32_lstat.st_birthtime_nsec;
  12890. return (PyObject *)entry;
  12891. error:
  12892. Py_DECREF(entry);
  12893. return NULL;
  12894. }
  12895. #else /* POSIX */
  12896. static char *
  12897. join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
  12898. {
  12899. Py_ssize_t path_len;
  12900. Py_ssize_t size;
  12901. char *result;
  12902. if (!path_narrow) { /* Default arg: "." */
  12903. path_narrow = ".";
  12904. path_len = 1;
  12905. }
  12906. else {
  12907. path_len = strlen(path_narrow);
  12908. }
  12909. if (filename_len == -1)
  12910. filename_len = strlen(filename);
  12911. /* The +1's are for the path separator and the NUL */
  12912. size = path_len + 1 + filename_len + 1;
  12913. result = PyMem_New(char, size);
  12914. if (!result) {
  12915. PyErr_NoMemory();
  12916. return NULL;
  12917. }
  12918. strcpy(result, path_narrow);
  12919. if (path_len > 0 && result[path_len - 1] != '/')
  12920. result[path_len++] = '/';
  12921. strcpy(result + path_len, filename);
  12922. return result;
  12923. }
  12924. static PyObject *
  12925. DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
  12926. Py_ssize_t name_len, ino_t d_ino
  12927. #ifdef HAVE_DIRENT_D_TYPE
  12928. , unsigned char d_type
  12929. #endif
  12930. )
  12931. {
  12932. DirEntry *entry;
  12933. char *joined_path;
  12934. PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
  12935. entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
  12936. if (!entry)
  12937. return NULL;
  12938. entry->name = NULL;
  12939. entry->path = NULL;
  12940. entry->stat = NULL;
  12941. entry->lstat = NULL;
  12942. if (path->fd != -1) {
  12943. entry->dir_fd = path->fd;
  12944. joined_path = NULL;
  12945. }
  12946. else {
  12947. entry->dir_fd = DEFAULT_DIR_FD;
  12948. joined_path = join_path_filename(path->narrow, name, name_len);
  12949. if (!joined_path)
  12950. goto error;
  12951. }
  12952. if (!path->narrow || !PyBytes_Check(path->object)) {
  12953. entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
  12954. if (joined_path)
  12955. entry->path = PyUnicode_DecodeFSDefault(joined_path);
  12956. }
  12957. else {
  12958. entry->name = PyBytes_FromStringAndSize(name, name_len);
  12959. if (joined_path)
  12960. entry->path = PyBytes_FromString(joined_path);
  12961. }
  12962. PyMem_Free(joined_path);
  12963. if (!entry->name)
  12964. goto error;
  12965. if (path->fd != -1) {
  12966. entry->path = Py_NewRef(entry->name);
  12967. }
  12968. else if (!entry->path)
  12969. goto error;
  12970. #ifdef HAVE_DIRENT_D_TYPE
  12971. entry->d_type = d_type;
  12972. #endif
  12973. entry->d_ino = d_ino;
  12974. return (PyObject *)entry;
  12975. error:
  12976. Py_XDECREF(entry);
  12977. return NULL;
  12978. }
  12979. #endif
  12980. typedef struct {
  12981. PyObject_HEAD
  12982. path_t path;
  12983. #ifdef MS_WINDOWS
  12984. HANDLE handle;
  12985. WIN32_FIND_DATAW file_data;
  12986. int first_time;
  12987. #else /* POSIX */
  12988. DIR *dirp;
  12989. #endif
  12990. #ifdef HAVE_FDOPENDIR
  12991. int fd;
  12992. #endif
  12993. } ScandirIterator;
  12994. #ifdef MS_WINDOWS
  12995. static int
  12996. ScandirIterator_is_closed(ScandirIterator *iterator)
  12997. {
  12998. return iterator->handle == INVALID_HANDLE_VALUE;
  12999. }
  13000. static void
  13001. ScandirIterator_closedir(ScandirIterator *iterator)
  13002. {
  13003. HANDLE handle = iterator->handle;
  13004. if (handle == INVALID_HANDLE_VALUE)
  13005. return;
  13006. iterator->handle = INVALID_HANDLE_VALUE;
  13007. Py_BEGIN_ALLOW_THREADS
  13008. FindClose(handle);
  13009. Py_END_ALLOW_THREADS
  13010. }
  13011. static PyObject *
  13012. ScandirIterator_iternext(ScandirIterator *iterator)
  13013. {
  13014. WIN32_FIND_DATAW *file_data = &iterator->file_data;
  13015. BOOL success;
  13016. PyObject *entry;
  13017. /* Happens if the iterator is iterated twice, or closed explicitly */
  13018. if (iterator->handle == INVALID_HANDLE_VALUE)
  13019. return NULL;
  13020. while (1) {
  13021. if (!iterator->first_time) {
  13022. Py_BEGIN_ALLOW_THREADS
  13023. success = FindNextFileW(iterator->handle, file_data);
  13024. Py_END_ALLOW_THREADS
  13025. if (!success) {
  13026. /* Error or no more files */
  13027. if (GetLastError() != ERROR_NO_MORE_FILES)
  13028. path_error(&iterator->path);
  13029. break;
  13030. }
  13031. }
  13032. iterator->first_time = 0;
  13033. /* Skip over . and .. */
  13034. if (wcscmp(file_data->cFileName, L".") != 0 &&
  13035. wcscmp(file_data->cFileName, L"..") != 0)
  13036. {
  13037. PyObject *module = PyType_GetModule(Py_TYPE(iterator));
  13038. entry = DirEntry_from_find_data(module, &iterator->path, file_data);
  13039. if (!entry)
  13040. break;
  13041. return entry;
  13042. }
  13043. /* Loop till we get a non-dot directory or finish iterating */
  13044. }
  13045. /* Error or no more files */
  13046. ScandirIterator_closedir(iterator);
  13047. return NULL;
  13048. }
  13049. #else /* POSIX */
  13050. static int
  13051. ScandirIterator_is_closed(ScandirIterator *iterator)
  13052. {
  13053. return !iterator->dirp;
  13054. }
  13055. static void
  13056. ScandirIterator_closedir(ScandirIterator *iterator)
  13057. {
  13058. DIR *dirp = iterator->dirp;
  13059. if (!dirp)
  13060. return;
  13061. iterator->dirp = NULL;
  13062. Py_BEGIN_ALLOW_THREADS
  13063. #ifdef HAVE_FDOPENDIR
  13064. if (iterator->path.fd != -1)
  13065. rewinddir(dirp);
  13066. #endif
  13067. closedir(dirp);
  13068. Py_END_ALLOW_THREADS
  13069. return;
  13070. }
  13071. static PyObject *
  13072. ScandirIterator_iternext(ScandirIterator *iterator)
  13073. {
  13074. struct dirent *direntp;
  13075. Py_ssize_t name_len;
  13076. int is_dot;
  13077. PyObject *entry;
  13078. /* Happens if the iterator is iterated twice, or closed explicitly */
  13079. if (!iterator->dirp)
  13080. return NULL;
  13081. while (1) {
  13082. errno = 0;
  13083. Py_BEGIN_ALLOW_THREADS
  13084. direntp = readdir(iterator->dirp);
  13085. Py_END_ALLOW_THREADS
  13086. if (!direntp) {
  13087. /* Error or no more files */
  13088. if (errno != 0)
  13089. path_error(&iterator->path);
  13090. break;
  13091. }
  13092. /* Skip over . and .. */
  13093. name_len = NAMLEN(direntp);
  13094. is_dot = direntp->d_name[0] == '.' &&
  13095. (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
  13096. if (!is_dot) {
  13097. PyObject *module = PyType_GetModule(Py_TYPE(iterator));
  13098. entry = DirEntry_from_posix_info(module,
  13099. &iterator->path, direntp->d_name,
  13100. name_len, direntp->d_ino
  13101. #ifdef HAVE_DIRENT_D_TYPE
  13102. , direntp->d_type
  13103. #endif
  13104. );
  13105. if (!entry)
  13106. break;
  13107. return entry;
  13108. }
  13109. /* Loop till we get a non-dot directory or finish iterating */
  13110. }
  13111. /* Error or no more files */
  13112. ScandirIterator_closedir(iterator);
  13113. return NULL;
  13114. }
  13115. #endif
  13116. static PyObject *
  13117. ScandirIterator_close(ScandirIterator *self, PyObject *args)
  13118. {
  13119. ScandirIterator_closedir(self);
  13120. Py_RETURN_NONE;
  13121. }
  13122. static PyObject *
  13123. ScandirIterator_enter(PyObject *self, PyObject *args)
  13124. {
  13125. return Py_NewRef(self);
  13126. }
  13127. static PyObject *
  13128. ScandirIterator_exit(ScandirIterator *self, PyObject *args)
  13129. {
  13130. ScandirIterator_closedir(self);
  13131. Py_RETURN_NONE;
  13132. }
  13133. static void
  13134. ScandirIterator_finalize(ScandirIterator *iterator)
  13135. {
  13136. /* Save the current exception, if any. */
  13137. PyObject *exc = PyErr_GetRaisedException();
  13138. if (!ScandirIterator_is_closed(iterator)) {
  13139. ScandirIterator_closedir(iterator);
  13140. if (PyErr_ResourceWarning((PyObject *)iterator, 1,
  13141. "unclosed scandir iterator %R", iterator)) {
  13142. /* Spurious errors can appear at shutdown */
  13143. if (PyErr_ExceptionMatches(PyExc_Warning)) {
  13144. PyErr_WriteUnraisable((PyObject *) iterator);
  13145. }
  13146. }
  13147. }
  13148. path_cleanup(&iterator->path);
  13149. /* Restore the saved exception. */
  13150. PyErr_SetRaisedException(exc);
  13151. }
  13152. static void
  13153. ScandirIterator_dealloc(ScandirIterator *iterator)
  13154. {
  13155. PyTypeObject *tp = Py_TYPE(iterator);
  13156. if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
  13157. return;
  13158. freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
  13159. free_func(iterator);
  13160. Py_DECREF(tp);
  13161. }
  13162. static PyMethodDef ScandirIterator_methods[] = {
  13163. {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
  13164. {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
  13165. {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
  13166. {NULL}
  13167. };
  13168. static PyType_Slot ScandirIteratorType_slots[] = {
  13169. {Py_tp_dealloc, ScandirIterator_dealloc},
  13170. {Py_tp_finalize, ScandirIterator_finalize},
  13171. {Py_tp_iter, PyObject_SelfIter},
  13172. {Py_tp_iternext, ScandirIterator_iternext},
  13173. {Py_tp_methods, ScandirIterator_methods},
  13174. {0, 0},
  13175. };
  13176. static PyType_Spec ScandirIteratorType_spec = {
  13177. MODNAME ".ScandirIterator",
  13178. sizeof(ScandirIterator),
  13179. 0,
  13180. // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
  13181. // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
  13182. (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE
  13183. | Py_TPFLAGS_DISALLOW_INSTANTIATION),
  13184. ScandirIteratorType_slots
  13185. };
  13186. /*[clinic input]
  13187. os.scandir
  13188. path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
  13189. Return an iterator of DirEntry objects for given path.
  13190. path can be specified as either str, bytes, or a path-like object. If path
  13191. is bytes, the names of yielded DirEntry objects will also be bytes; in
  13192. all other circumstances they will be str.
  13193. If path is None, uses the path='.'.
  13194. [clinic start generated code]*/
  13195. static PyObject *
  13196. os_scandir_impl(PyObject *module, path_t *path)
  13197. /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
  13198. {
  13199. ScandirIterator *iterator;
  13200. #ifdef MS_WINDOWS
  13201. wchar_t *path_strW;
  13202. #else
  13203. const char *path_str;
  13204. #ifdef HAVE_FDOPENDIR
  13205. int fd = -1;
  13206. #endif
  13207. #endif
  13208. if (PySys_Audit("os.scandir", "O",
  13209. path->object ? path->object : Py_None) < 0) {
  13210. return NULL;
  13211. }
  13212. PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
  13213. iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
  13214. if (!iterator)
  13215. return NULL;
  13216. #ifdef MS_WINDOWS
  13217. iterator->handle = INVALID_HANDLE_VALUE;
  13218. #else
  13219. iterator->dirp = NULL;
  13220. #endif
  13221. /* Move the ownership to iterator->path */
  13222. memcpy(&iterator->path, path, sizeof(path_t));
  13223. memset(path, 0, sizeof(path_t));
  13224. #ifdef MS_WINDOWS
  13225. iterator->first_time = 1;
  13226. path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
  13227. if (!path_strW)
  13228. goto error;
  13229. Py_BEGIN_ALLOW_THREADS
  13230. iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
  13231. Py_END_ALLOW_THREADS
  13232. if (iterator->handle == INVALID_HANDLE_VALUE) {
  13233. path_error(&iterator->path);
  13234. PyMem_Free(path_strW);
  13235. goto error;
  13236. }
  13237. PyMem_Free(path_strW);
  13238. #else /* POSIX */
  13239. errno = 0;
  13240. #ifdef HAVE_FDOPENDIR
  13241. if (iterator->path.fd != -1) {
  13242. if (HAVE_FDOPENDIR_RUNTIME) {
  13243. /* closedir() closes the FD, so we duplicate it */
  13244. fd = _Py_dup(iterator->path.fd);
  13245. if (fd == -1)
  13246. goto error;
  13247. Py_BEGIN_ALLOW_THREADS
  13248. iterator->dirp = fdopendir(fd);
  13249. Py_END_ALLOW_THREADS
  13250. } else {
  13251. PyErr_SetString(PyExc_TypeError,
  13252. "scandir: path should be string, bytes, os.PathLike or None, not int");
  13253. return NULL;
  13254. }
  13255. }
  13256. else
  13257. #endif
  13258. {
  13259. if (iterator->path.narrow)
  13260. path_str = iterator->path.narrow;
  13261. else
  13262. path_str = ".";
  13263. Py_BEGIN_ALLOW_THREADS
  13264. iterator->dirp = opendir(path_str);
  13265. Py_END_ALLOW_THREADS
  13266. }
  13267. if (!iterator->dirp) {
  13268. path_error(&iterator->path);
  13269. #ifdef HAVE_FDOPENDIR
  13270. if (fd != -1) {
  13271. Py_BEGIN_ALLOW_THREADS
  13272. close(fd);
  13273. Py_END_ALLOW_THREADS
  13274. }
  13275. #endif
  13276. goto error;
  13277. }
  13278. #endif
  13279. return (PyObject *)iterator;
  13280. error:
  13281. Py_DECREF(iterator);
  13282. return NULL;
  13283. }
  13284. /*
  13285. Return the file system path representation of the object.
  13286. If the object is str or bytes, then allow it to pass through with
  13287. an incremented refcount. If the object defines __fspath__(), then
  13288. return the result of that method. All other types raise a TypeError.
  13289. */
  13290. PyObject *
  13291. PyOS_FSPath(PyObject *path)
  13292. {
  13293. /* For error message reasons, this function is manually inlined in
  13294. path_converter(). */
  13295. PyObject *func = NULL;
  13296. PyObject *path_repr = NULL;
  13297. if (PyUnicode_Check(path) || PyBytes_Check(path)) {
  13298. return Py_NewRef(path);
  13299. }
  13300. func = _PyObject_LookupSpecial(path, &_Py_ID(__fspath__));
  13301. if (NULL == func) {
  13302. return PyErr_Format(PyExc_TypeError,
  13303. "expected str, bytes or os.PathLike object, "
  13304. "not %.200s",
  13305. _PyType_Name(Py_TYPE(path)));
  13306. }
  13307. path_repr = _PyObject_CallNoArgs(func);
  13308. Py_DECREF(func);
  13309. if (NULL == path_repr) {
  13310. return NULL;
  13311. }
  13312. if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
  13313. PyErr_Format(PyExc_TypeError,
  13314. "expected %.200s.__fspath__() to return str or bytes, "
  13315. "not %.200s", _PyType_Name(Py_TYPE(path)),
  13316. _PyType_Name(Py_TYPE(path_repr)));
  13317. Py_DECREF(path_repr);
  13318. return NULL;
  13319. }
  13320. return path_repr;
  13321. }
  13322. /*[clinic input]
  13323. os.fspath
  13324. path: object
  13325. Return the file system path representation of the object.
  13326. If the object is str or bytes, then allow it to pass through as-is. If the
  13327. object defines __fspath__(), then return the result of that method. All other
  13328. types raise a TypeError.
  13329. [clinic start generated code]*/
  13330. static PyObject *
  13331. os_fspath_impl(PyObject *module, PyObject *path)
  13332. /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
  13333. {
  13334. return PyOS_FSPath(path);
  13335. }
  13336. #ifdef HAVE_GETRANDOM_SYSCALL
  13337. /*[clinic input]
  13338. os.getrandom
  13339. size: Py_ssize_t
  13340. flags: int=0
  13341. Obtain a series of random bytes.
  13342. [clinic start generated code]*/
  13343. static PyObject *
  13344. os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
  13345. /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
  13346. {
  13347. PyObject *bytes;
  13348. Py_ssize_t n;
  13349. if (size < 0) {
  13350. errno = EINVAL;
  13351. return posix_error();
  13352. }
  13353. bytes = PyBytes_FromStringAndSize(NULL, size);
  13354. if (bytes == NULL) {
  13355. PyErr_NoMemory();
  13356. return NULL;
  13357. }
  13358. while (1) {
  13359. n = syscall(SYS_getrandom,
  13360. PyBytes_AS_STRING(bytes),
  13361. PyBytes_GET_SIZE(bytes),
  13362. flags);
  13363. if (n < 0 && errno == EINTR) {
  13364. if (PyErr_CheckSignals() < 0) {
  13365. goto error;
  13366. }
  13367. /* getrandom() was interrupted by a signal: retry */
  13368. continue;
  13369. }
  13370. break;
  13371. }
  13372. if (n < 0) {
  13373. PyErr_SetFromErrno(PyExc_OSError);
  13374. goto error;
  13375. }
  13376. if (n != size) {
  13377. _PyBytes_Resize(&bytes, n);
  13378. }
  13379. return bytes;
  13380. error:
  13381. Py_DECREF(bytes);
  13382. return NULL;
  13383. }
  13384. #endif /* HAVE_GETRANDOM_SYSCALL */
  13385. #if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
  13386. /* bpo-36085: Helper functions for managing DLL search directories
  13387. * on win32
  13388. */
  13389. /*[clinic input]
  13390. os._add_dll_directory
  13391. path: path_t
  13392. Add a path to the DLL search path.
  13393. This search path is used when resolving dependencies for imported
  13394. extension modules (the module itself is resolved through sys.path),
  13395. and also by ctypes.
  13396. Returns an opaque value that may be passed to os.remove_dll_directory
  13397. to remove this directory from the search path.
  13398. [clinic start generated code]*/
  13399. static PyObject *
  13400. os__add_dll_directory_impl(PyObject *module, path_t *path)
  13401. /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
  13402. {
  13403. DLL_DIRECTORY_COOKIE cookie = 0;
  13404. DWORD err = 0;
  13405. if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
  13406. return NULL;
  13407. }
  13408. Py_BEGIN_ALLOW_THREADS
  13409. if (!(cookie = AddDllDirectory(path->wide))) {
  13410. err = GetLastError();
  13411. }
  13412. Py_END_ALLOW_THREADS
  13413. if (err) {
  13414. return win32_error_object_err("add_dll_directory",
  13415. path->object, err);
  13416. }
  13417. return PyCapsule_New(cookie, "DLL directory cookie", NULL);
  13418. }
  13419. /*[clinic input]
  13420. os._remove_dll_directory
  13421. cookie: object
  13422. Removes a path from the DLL search path.
  13423. The parameter is an opaque value that was returned from
  13424. os.add_dll_directory. You can only remove directories that you added
  13425. yourself.
  13426. [clinic start generated code]*/
  13427. static PyObject *
  13428. os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
  13429. /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
  13430. {
  13431. DLL_DIRECTORY_COOKIE cookieValue;
  13432. DWORD err = 0;
  13433. if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
  13434. PyErr_SetString(PyExc_TypeError,
  13435. "Provided cookie was not returned from os.add_dll_directory");
  13436. return NULL;
  13437. }
  13438. cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
  13439. cookie, "DLL directory cookie");
  13440. Py_BEGIN_ALLOW_THREADS
  13441. if (!RemoveDllDirectory(cookieValue)) {
  13442. err = GetLastError();
  13443. }
  13444. Py_END_ALLOW_THREADS
  13445. if (err) {
  13446. return win32_error_object_err("remove_dll_directory",
  13447. NULL, err);
  13448. }
  13449. if (PyCapsule_SetName(cookie, NULL)) {
  13450. return NULL;
  13451. }
  13452. Py_RETURN_NONE;
  13453. }
  13454. #endif /* MS_WINDOWS_APP || MS_WINDOWS_SYSTEM */
  13455. /* Only check if WIFEXITED is available: expect that it comes
  13456. with WEXITSTATUS, WIFSIGNALED, etc.
  13457. os.waitstatus_to_exitcode() is implemented in C and not in Python, so
  13458. subprocess can safely call it during late Python finalization without
  13459. risking that used os attributes were set to None by finalize_modules(). */
  13460. #if defined(WIFEXITED) || defined(MS_WINDOWS)
  13461. /*[clinic input]
  13462. os.waitstatus_to_exitcode
  13463. status as status_obj: object
  13464. Convert a wait status to an exit code.
  13465. On Unix:
  13466. * If WIFEXITED(status) is true, return WEXITSTATUS(status).
  13467. * If WIFSIGNALED(status) is true, return -WTERMSIG(status).
  13468. * Otherwise, raise a ValueError.
  13469. On Windows, return status shifted right by 8 bits.
  13470. On Unix, if the process is being traced or if waitpid() was called with
  13471. WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
  13472. This function must not be called if WIFSTOPPED(status) is true.
  13473. [clinic start generated code]*/
  13474. static PyObject *
  13475. os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
  13476. /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
  13477. {
  13478. #ifndef MS_WINDOWS
  13479. int status = _PyLong_AsInt(status_obj);
  13480. if (status == -1 && PyErr_Occurred()) {
  13481. return NULL;
  13482. }
  13483. WAIT_TYPE wait_status;
  13484. WAIT_STATUS_INT(wait_status) = status;
  13485. int exitcode;
  13486. if (WIFEXITED(wait_status)) {
  13487. exitcode = WEXITSTATUS(wait_status);
  13488. /* Sanity check to provide warranty on the function behavior.
  13489. It should not occur in practice */
  13490. if (exitcode < 0) {
  13491. PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
  13492. return NULL;
  13493. }
  13494. }
  13495. else if (WIFSIGNALED(wait_status)) {
  13496. int signum = WTERMSIG(wait_status);
  13497. /* Sanity check to provide warranty on the function behavior.
  13498. It should not occurs in practice */
  13499. if (signum <= 0) {
  13500. PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
  13501. return NULL;
  13502. }
  13503. exitcode = -signum;
  13504. } else if (WIFSTOPPED(wait_status)) {
  13505. /* Status only received if the process is being traced
  13506. or if waitpid() was called with WUNTRACED option. */
  13507. int signum = WSTOPSIG(wait_status);
  13508. PyErr_Format(PyExc_ValueError,
  13509. "process stopped by delivery of signal %i",
  13510. signum);
  13511. return NULL;
  13512. }
  13513. else {
  13514. PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
  13515. return NULL;
  13516. }
  13517. return PyLong_FromLong(exitcode);
  13518. #else
  13519. /* Windows implementation: see os.waitpid() implementation
  13520. which uses _cwait(). */
  13521. unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
  13522. if (status == (unsigned long long)-1 && PyErr_Occurred()) {
  13523. return NULL;
  13524. }
  13525. unsigned long long exitcode = (status >> 8);
  13526. /* ExitProcess() accepts an UINT type:
  13527. reject exit code which doesn't fit in an UINT */
  13528. if (exitcode > UINT_MAX) {
  13529. PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
  13530. return NULL;
  13531. }
  13532. return PyLong_FromUnsignedLong((unsigned long)exitcode);
  13533. #endif
  13534. }
  13535. #endif
  13536. static PyMethodDef posix_methods[] = {
  13537. OS_STAT_METHODDEF
  13538. OS_ACCESS_METHODDEF
  13539. OS_TTYNAME_METHODDEF
  13540. OS_CHDIR_METHODDEF
  13541. OS_CHFLAGS_METHODDEF
  13542. OS_CHMOD_METHODDEF
  13543. OS_FCHMOD_METHODDEF
  13544. OS_LCHMOD_METHODDEF
  13545. OS_CHOWN_METHODDEF
  13546. OS_FCHOWN_METHODDEF
  13547. OS_LCHOWN_METHODDEF
  13548. OS_LCHFLAGS_METHODDEF
  13549. OS_CHROOT_METHODDEF
  13550. OS_CTERMID_METHODDEF
  13551. OS_GETCWD_METHODDEF
  13552. OS_GETCWDB_METHODDEF
  13553. OS_LINK_METHODDEF
  13554. OS_LISTDIR_METHODDEF
  13555. OS_LISTDRIVES_METHODDEF
  13556. OS_LISTMOUNTS_METHODDEF
  13557. OS_LISTVOLUMES_METHODDEF
  13558. OS_LSTAT_METHODDEF
  13559. OS_MKDIR_METHODDEF
  13560. OS_NICE_METHODDEF
  13561. OS_GETPRIORITY_METHODDEF
  13562. OS_SETPRIORITY_METHODDEF
  13563. OS_POSIX_SPAWN_METHODDEF
  13564. OS_POSIX_SPAWNP_METHODDEF
  13565. OS_READLINK_METHODDEF
  13566. OS_COPY_FILE_RANGE_METHODDEF
  13567. OS_SPLICE_METHODDEF
  13568. OS_RENAME_METHODDEF
  13569. OS_REPLACE_METHODDEF
  13570. OS_RMDIR_METHODDEF
  13571. OS_SYMLINK_METHODDEF
  13572. OS_SYSTEM_METHODDEF
  13573. OS_UMASK_METHODDEF
  13574. OS_UNAME_METHODDEF
  13575. OS_UNLINK_METHODDEF
  13576. OS_REMOVE_METHODDEF
  13577. OS_UTIME_METHODDEF
  13578. OS_TIMES_METHODDEF
  13579. OS__EXIT_METHODDEF
  13580. OS__FCOPYFILE_METHODDEF
  13581. OS_EXECV_METHODDEF
  13582. OS_EXECVE_METHODDEF
  13583. OS_SPAWNV_METHODDEF
  13584. OS_SPAWNVE_METHODDEF
  13585. OS_FORK1_METHODDEF
  13586. OS_FORK_METHODDEF
  13587. OS_REGISTER_AT_FORK_METHODDEF
  13588. OS_SCHED_GET_PRIORITY_MAX_METHODDEF
  13589. OS_SCHED_GET_PRIORITY_MIN_METHODDEF
  13590. OS_SCHED_GETPARAM_METHODDEF
  13591. OS_SCHED_GETSCHEDULER_METHODDEF
  13592. OS_SCHED_RR_GET_INTERVAL_METHODDEF
  13593. OS_SCHED_SETPARAM_METHODDEF
  13594. OS_SCHED_SETSCHEDULER_METHODDEF
  13595. OS_SCHED_YIELD_METHODDEF
  13596. OS_SCHED_SETAFFINITY_METHODDEF
  13597. OS_SCHED_GETAFFINITY_METHODDEF
  13598. OS_OPENPTY_METHODDEF
  13599. OS_LOGIN_TTY_METHODDEF
  13600. OS_FORKPTY_METHODDEF
  13601. OS_GETEGID_METHODDEF
  13602. OS_GETEUID_METHODDEF
  13603. OS_GETGID_METHODDEF
  13604. OS_GETGROUPLIST_METHODDEF
  13605. OS_GETGROUPS_METHODDEF
  13606. OS_GETPID_METHODDEF
  13607. OS_GETPGRP_METHODDEF
  13608. OS_GETPPID_METHODDEF
  13609. OS_GETUID_METHODDEF
  13610. OS_GETLOGIN_METHODDEF
  13611. OS_KILL_METHODDEF
  13612. OS_KILLPG_METHODDEF
  13613. OS_PLOCK_METHODDEF
  13614. OS_STARTFILE_METHODDEF
  13615. OS_SETUID_METHODDEF
  13616. OS_SETEUID_METHODDEF
  13617. OS_SETREUID_METHODDEF
  13618. OS_SETGID_METHODDEF
  13619. OS_SETEGID_METHODDEF
  13620. OS_SETREGID_METHODDEF
  13621. OS_SETGROUPS_METHODDEF
  13622. OS_INITGROUPS_METHODDEF
  13623. OS_GETPGID_METHODDEF
  13624. OS_SETPGRP_METHODDEF
  13625. OS_WAIT_METHODDEF
  13626. OS_WAIT3_METHODDEF
  13627. OS_WAIT4_METHODDEF
  13628. OS_WAITID_METHODDEF
  13629. OS_WAITPID_METHODDEF
  13630. OS_PIDFD_OPEN_METHODDEF
  13631. OS_GETSID_METHODDEF
  13632. OS_SETSID_METHODDEF
  13633. OS_SETPGID_METHODDEF
  13634. OS_TCGETPGRP_METHODDEF
  13635. OS_TCSETPGRP_METHODDEF
  13636. OS_OPEN_METHODDEF
  13637. OS_CLOSE_METHODDEF
  13638. OS_CLOSERANGE_METHODDEF
  13639. OS_DEVICE_ENCODING_METHODDEF
  13640. OS_DUP_METHODDEF
  13641. OS_DUP2_METHODDEF
  13642. OS_LOCKF_METHODDEF
  13643. OS_LSEEK_METHODDEF
  13644. OS_READ_METHODDEF
  13645. OS_READV_METHODDEF
  13646. OS_PREAD_METHODDEF
  13647. OS_PREADV_METHODDEF
  13648. OS_WRITE_METHODDEF
  13649. OS_WRITEV_METHODDEF
  13650. OS_PWRITE_METHODDEF
  13651. OS_PWRITEV_METHODDEF
  13652. OS_SENDFILE_METHODDEF
  13653. OS_FSTAT_METHODDEF
  13654. OS_ISATTY_METHODDEF
  13655. OS_PIPE_METHODDEF
  13656. OS_PIPE2_METHODDEF
  13657. OS_MKFIFO_METHODDEF
  13658. OS_MKNOD_METHODDEF
  13659. OS_MAJOR_METHODDEF
  13660. OS_MINOR_METHODDEF
  13661. OS_MAKEDEV_METHODDEF
  13662. OS_FTRUNCATE_METHODDEF
  13663. OS_TRUNCATE_METHODDEF
  13664. OS_POSIX_FALLOCATE_METHODDEF
  13665. OS_POSIX_FADVISE_METHODDEF
  13666. OS_PUTENV_METHODDEF
  13667. OS_UNSETENV_METHODDEF
  13668. OS_STRERROR_METHODDEF
  13669. OS_FCHDIR_METHODDEF
  13670. OS_FSYNC_METHODDEF
  13671. OS_SYNC_METHODDEF
  13672. OS_FDATASYNC_METHODDEF
  13673. OS_WCOREDUMP_METHODDEF
  13674. OS_WIFCONTINUED_METHODDEF
  13675. OS_WIFSTOPPED_METHODDEF
  13676. OS_WIFSIGNALED_METHODDEF
  13677. OS_WIFEXITED_METHODDEF
  13678. OS_WEXITSTATUS_METHODDEF
  13679. OS_WTERMSIG_METHODDEF
  13680. OS_WSTOPSIG_METHODDEF
  13681. OS_FSTATVFS_METHODDEF
  13682. OS_STATVFS_METHODDEF
  13683. OS_CONFSTR_METHODDEF
  13684. OS_SYSCONF_METHODDEF
  13685. OS_FPATHCONF_METHODDEF
  13686. OS_PATHCONF_METHODDEF
  13687. OS_ABORT_METHODDEF
  13688. OS__GETFULLPATHNAME_METHODDEF
  13689. OS__GETDISKUSAGE_METHODDEF
  13690. OS__GETFINALPATHNAME_METHODDEF
  13691. OS__GETVOLUMEPATHNAME_METHODDEF
  13692. OS__PATH_SPLITROOT_METHODDEF
  13693. OS__PATH_NORMPATH_METHODDEF
  13694. OS_GETLOADAVG_METHODDEF
  13695. OS_URANDOM_METHODDEF
  13696. OS_SETRESUID_METHODDEF
  13697. OS_SETRESGID_METHODDEF
  13698. OS_GETRESUID_METHODDEF
  13699. OS_GETRESGID_METHODDEF
  13700. OS_GETXATTR_METHODDEF
  13701. OS_SETXATTR_METHODDEF
  13702. OS_REMOVEXATTR_METHODDEF
  13703. OS_LISTXATTR_METHODDEF
  13704. OS_GET_TERMINAL_SIZE_METHODDEF
  13705. OS_CPU_COUNT_METHODDEF
  13706. OS_GET_INHERITABLE_METHODDEF
  13707. OS_SET_INHERITABLE_METHODDEF
  13708. OS_GET_HANDLE_INHERITABLE_METHODDEF
  13709. OS_SET_HANDLE_INHERITABLE_METHODDEF
  13710. OS_GET_BLOCKING_METHODDEF
  13711. OS_SET_BLOCKING_METHODDEF
  13712. OS_SCANDIR_METHODDEF
  13713. OS_FSPATH_METHODDEF
  13714. OS_GETRANDOM_METHODDEF
  13715. OS_MEMFD_CREATE_METHODDEF
  13716. OS_EVENTFD_METHODDEF
  13717. OS_EVENTFD_READ_METHODDEF
  13718. OS_EVENTFD_WRITE_METHODDEF
  13719. OS__ADD_DLL_DIRECTORY_METHODDEF
  13720. OS__REMOVE_DLL_DIRECTORY_METHODDEF
  13721. OS_WAITSTATUS_TO_EXITCODE_METHODDEF
  13722. OS_SETNS_METHODDEF
  13723. OS_UNSHARE_METHODDEF
  13724. OS__PATH_ISDEVDRIVE_METHODDEF
  13725. OS__PATH_ISDIR_METHODDEF
  13726. OS__PATH_ISFILE_METHODDEF
  13727. OS__PATH_ISLINK_METHODDEF
  13728. OS__PATH_EXISTS_METHODDEF
  13729. {NULL, NULL} /* Sentinel */
  13730. };
  13731. static int
  13732. all_ins(PyObject *m)
  13733. {
  13734. #ifdef F_OK
  13735. if (PyModule_AddIntMacro(m, F_OK)) return -1;
  13736. #endif
  13737. #ifdef R_OK
  13738. if (PyModule_AddIntMacro(m, R_OK)) return -1;
  13739. #endif
  13740. #ifdef W_OK
  13741. if (PyModule_AddIntMacro(m, W_OK)) return -1;
  13742. #endif
  13743. #ifdef X_OK
  13744. if (PyModule_AddIntMacro(m, X_OK)) return -1;
  13745. #endif
  13746. #ifdef NGROUPS_MAX
  13747. if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
  13748. #endif
  13749. #ifdef TMP_MAX
  13750. if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
  13751. #endif
  13752. #ifdef WCONTINUED
  13753. if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
  13754. #endif
  13755. #ifdef WNOHANG
  13756. if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
  13757. #endif
  13758. #ifdef WUNTRACED
  13759. if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
  13760. #endif
  13761. #ifdef O_RDONLY
  13762. if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
  13763. #endif
  13764. #ifdef O_WRONLY
  13765. if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
  13766. #endif
  13767. #ifdef O_RDWR
  13768. if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
  13769. #endif
  13770. #ifdef O_NDELAY
  13771. if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
  13772. #endif
  13773. #ifdef O_NONBLOCK
  13774. if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
  13775. #endif
  13776. #ifdef O_APPEND
  13777. if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
  13778. #endif
  13779. #ifdef O_DSYNC
  13780. if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
  13781. #endif
  13782. #ifdef O_RSYNC
  13783. if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
  13784. #endif
  13785. #ifdef O_SYNC
  13786. if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
  13787. #endif
  13788. #ifdef O_NOCTTY
  13789. if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
  13790. #endif
  13791. #ifdef O_CREAT
  13792. if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
  13793. #endif
  13794. #ifdef O_EXCL
  13795. if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
  13796. #endif
  13797. #ifdef O_TRUNC
  13798. if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
  13799. #endif
  13800. #ifdef O_BINARY
  13801. if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
  13802. #endif
  13803. #ifdef O_TEXT
  13804. if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
  13805. #endif
  13806. #ifdef O_XATTR
  13807. if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
  13808. #endif
  13809. #ifdef O_LARGEFILE
  13810. if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
  13811. #endif
  13812. #ifndef __GNU__
  13813. #ifdef O_SHLOCK
  13814. if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
  13815. #endif
  13816. #ifdef O_EXLOCK
  13817. if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
  13818. #endif
  13819. #endif
  13820. #ifdef O_EXEC
  13821. if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
  13822. #endif
  13823. #ifdef O_SEARCH
  13824. if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
  13825. #endif
  13826. #ifdef O_PATH
  13827. if (PyModule_AddIntMacro(m, O_PATH)) return -1;
  13828. #endif
  13829. #ifdef O_TTY_INIT
  13830. if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
  13831. #endif
  13832. #ifdef O_TMPFILE
  13833. if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
  13834. #endif
  13835. #ifdef PRIO_PROCESS
  13836. if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
  13837. #endif
  13838. #ifdef PRIO_PGRP
  13839. if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
  13840. #endif
  13841. #ifdef PRIO_USER
  13842. if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
  13843. #endif
  13844. #ifdef PRIO_DARWIN_THREAD
  13845. if (PyModule_AddIntMacro(m, PRIO_DARWIN_THREAD)) return -1;
  13846. #endif
  13847. #ifdef PRIO_DARWIN_PROCESS
  13848. if (PyModule_AddIntMacro(m, PRIO_DARWIN_PROCESS)) return -1;
  13849. #endif
  13850. #ifdef PRIO_DARWIN_BG
  13851. if (PyModule_AddIntMacro(m, PRIO_DARWIN_BG)) return -1;
  13852. #endif
  13853. #ifdef PRIO_DARWIN_NONUI
  13854. if (PyModule_AddIntMacro(m, PRIO_DARWIN_NONUI)) return -1;
  13855. #endif
  13856. #ifdef O_CLOEXEC
  13857. if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
  13858. #endif
  13859. #ifdef O_ACCMODE
  13860. if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
  13861. #endif
  13862. #ifdef O_EVTONLY
  13863. if (PyModule_AddIntMacro(m, O_EVTONLY)) return -1;
  13864. #endif
  13865. #ifdef O_FSYNC
  13866. if (PyModule_AddIntMacro(m, O_FSYNC)) return -1;
  13867. #endif
  13868. #ifdef O_SYMLINK
  13869. if (PyModule_AddIntMacro(m, O_SYMLINK)) return -1;
  13870. #endif
  13871. #ifdef SEEK_HOLE
  13872. if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
  13873. #endif
  13874. #ifdef SEEK_DATA
  13875. if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
  13876. #endif
  13877. /* MS Windows */
  13878. #ifdef O_NOINHERIT
  13879. /* Don't inherit in child processes. */
  13880. if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
  13881. #endif
  13882. #ifdef _O_SHORT_LIVED
  13883. /* Optimize for short life (keep in memory). */
  13884. /* MS forgot to define this one with a non-underscore form too. */
  13885. if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
  13886. #endif
  13887. #ifdef O_TEMPORARY
  13888. /* Automatically delete when last handle is closed. */
  13889. if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
  13890. #endif
  13891. #ifdef O_RANDOM
  13892. /* Optimize for random access. */
  13893. if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
  13894. #endif
  13895. #ifdef O_SEQUENTIAL
  13896. /* Optimize for sequential access. */
  13897. if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
  13898. #endif
  13899. /* GNU extensions. */
  13900. #ifdef O_ASYNC
  13901. /* Send a SIGIO signal whenever input or output
  13902. becomes available on file descriptor */
  13903. if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
  13904. #endif
  13905. #ifdef O_DIRECT
  13906. /* Direct disk access. */
  13907. if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
  13908. #endif
  13909. #ifdef O_DIRECTORY
  13910. /* Must be a directory. */
  13911. if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
  13912. #endif
  13913. #ifdef O_NOFOLLOW
  13914. /* Do not follow links. */
  13915. if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
  13916. #endif
  13917. #ifdef O_NOFOLLOW_ANY
  13918. if (PyModule_AddIntMacro(m, O_NOFOLLOW_ANY)) return -1;
  13919. #endif
  13920. #ifdef O_NOLINKS
  13921. /* Fails if link count of the named file is greater than 1 */
  13922. if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
  13923. #endif
  13924. #ifdef O_NOATIME
  13925. /* Do not update the access time. */
  13926. if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
  13927. #endif
  13928. /* These come from sysexits.h */
  13929. #ifdef EX_OK
  13930. if (PyModule_AddIntMacro(m, EX_OK)) return -1;
  13931. #endif /* EX_OK */
  13932. #ifdef EX_USAGE
  13933. if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
  13934. #endif /* EX_USAGE */
  13935. #ifdef EX_DATAERR
  13936. if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
  13937. #endif /* EX_DATAERR */
  13938. #ifdef EX_NOINPUT
  13939. if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
  13940. #endif /* EX_NOINPUT */
  13941. #ifdef EX_NOUSER
  13942. if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
  13943. #endif /* EX_NOUSER */
  13944. #ifdef EX_NOHOST
  13945. if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
  13946. #endif /* EX_NOHOST */
  13947. #ifdef EX_UNAVAILABLE
  13948. if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
  13949. #endif /* EX_UNAVAILABLE */
  13950. #ifdef EX_SOFTWARE
  13951. if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
  13952. #endif /* EX_SOFTWARE */
  13953. #ifdef EX_OSERR
  13954. if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
  13955. #endif /* EX_OSERR */
  13956. #ifdef EX_OSFILE
  13957. if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
  13958. #endif /* EX_OSFILE */
  13959. #ifdef EX_CANTCREAT
  13960. if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
  13961. #endif /* EX_CANTCREAT */
  13962. #ifdef EX_IOERR
  13963. if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
  13964. #endif /* EX_IOERR */
  13965. #ifdef EX_TEMPFAIL
  13966. if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
  13967. #endif /* EX_TEMPFAIL */
  13968. #ifdef EX_PROTOCOL
  13969. if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
  13970. #endif /* EX_PROTOCOL */
  13971. #ifdef EX_NOPERM
  13972. if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
  13973. #endif /* EX_NOPERM */
  13974. #ifdef EX_CONFIG
  13975. if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
  13976. #endif /* EX_CONFIG */
  13977. #ifdef EX_NOTFOUND
  13978. if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
  13979. #endif /* EX_NOTFOUND */
  13980. /* statvfs */
  13981. #ifdef ST_RDONLY
  13982. if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
  13983. #endif /* ST_RDONLY */
  13984. #ifdef ST_NOSUID
  13985. if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
  13986. #endif /* ST_NOSUID */
  13987. /* GNU extensions */
  13988. #ifdef ST_NODEV
  13989. if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
  13990. #endif /* ST_NODEV */
  13991. #ifdef ST_NOEXEC
  13992. if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
  13993. #endif /* ST_NOEXEC */
  13994. #ifdef ST_SYNCHRONOUS
  13995. if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
  13996. #endif /* ST_SYNCHRONOUS */
  13997. #ifdef ST_MANDLOCK
  13998. if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
  13999. #endif /* ST_MANDLOCK */
  14000. #ifdef ST_WRITE
  14001. if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
  14002. #endif /* ST_WRITE */
  14003. #ifdef ST_APPEND
  14004. if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
  14005. #endif /* ST_APPEND */
  14006. #ifdef ST_NOATIME
  14007. if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
  14008. #endif /* ST_NOATIME */
  14009. #ifdef ST_NODIRATIME
  14010. if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
  14011. #endif /* ST_NODIRATIME */
  14012. #ifdef ST_RELATIME
  14013. if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
  14014. #endif /* ST_RELATIME */
  14015. /* FreeBSD sendfile() constants */
  14016. #ifdef SF_NODISKIO
  14017. if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
  14018. #endif
  14019. /* is obsolete since the 11.x release */
  14020. #ifdef SF_MNOWAIT
  14021. if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
  14022. #endif
  14023. #ifdef SF_SYNC
  14024. if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
  14025. #endif
  14026. #ifdef SF_NOCACHE
  14027. if (PyModule_AddIntMacro(m, SF_NOCACHE)) return -1;
  14028. #endif
  14029. /* constants for posix_fadvise */
  14030. #ifdef POSIX_FADV_NORMAL
  14031. if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
  14032. #endif
  14033. #ifdef POSIX_FADV_SEQUENTIAL
  14034. if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
  14035. #endif
  14036. #ifdef POSIX_FADV_RANDOM
  14037. if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
  14038. #endif
  14039. #ifdef POSIX_FADV_NOREUSE
  14040. if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
  14041. #endif
  14042. #ifdef POSIX_FADV_WILLNEED
  14043. if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
  14044. #endif
  14045. #ifdef POSIX_FADV_DONTNEED
  14046. if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
  14047. #endif
  14048. /* constants for waitid */
  14049. #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
  14050. if (PyModule_AddIntMacro(m, P_PID)) return -1;
  14051. if (PyModule_AddIntMacro(m, P_PGID)) return -1;
  14052. if (PyModule_AddIntMacro(m, P_ALL)) return -1;
  14053. #ifdef P_PIDFD
  14054. if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
  14055. #endif
  14056. #ifdef PIDFD_NONBLOCK
  14057. if (PyModule_AddIntMacro(m, PIDFD_NONBLOCK)) return -1;
  14058. #endif
  14059. #endif
  14060. #ifdef WEXITED
  14061. if (PyModule_AddIntMacro(m, WEXITED)) return -1;
  14062. #endif
  14063. #ifdef WNOWAIT
  14064. if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
  14065. #endif
  14066. #ifdef WSTOPPED
  14067. if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
  14068. #endif
  14069. #ifdef CLD_EXITED
  14070. if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
  14071. #endif
  14072. #ifdef CLD_KILLED
  14073. if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
  14074. #endif
  14075. #ifdef CLD_DUMPED
  14076. if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
  14077. #endif
  14078. #ifdef CLD_TRAPPED
  14079. if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
  14080. #endif
  14081. #ifdef CLD_STOPPED
  14082. if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
  14083. #endif
  14084. #ifdef CLD_CONTINUED
  14085. if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
  14086. #endif
  14087. /* constants for lockf */
  14088. #ifdef F_LOCK
  14089. if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
  14090. #endif
  14091. #ifdef F_TLOCK
  14092. if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
  14093. #endif
  14094. #ifdef F_ULOCK
  14095. if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
  14096. #endif
  14097. #ifdef F_TEST
  14098. if (PyModule_AddIntMacro(m, F_TEST)) return -1;
  14099. #endif
  14100. #ifdef RWF_DSYNC
  14101. if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
  14102. #endif
  14103. #ifdef RWF_HIPRI
  14104. if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
  14105. #endif
  14106. #ifdef RWF_SYNC
  14107. if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
  14108. #endif
  14109. #ifdef RWF_NOWAIT
  14110. if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
  14111. #endif
  14112. #ifdef RWF_APPEND
  14113. if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1;
  14114. #endif
  14115. /* constants for splice */
  14116. #if defined(HAVE_SPLICE) && defined(__linux__)
  14117. if (PyModule_AddIntConstant(m, "SPLICE_F_MOVE", SPLICE_F_MOVE)) return -1;
  14118. if (PyModule_AddIntConstant(m, "SPLICE_F_NONBLOCK", SPLICE_F_NONBLOCK)) return -1;
  14119. if (PyModule_AddIntConstant(m, "SPLICE_F_MORE", SPLICE_F_MORE)) return -1;
  14120. #endif
  14121. /* constants for posix_spawn */
  14122. #ifdef HAVE_POSIX_SPAWN
  14123. if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
  14124. if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
  14125. if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
  14126. #endif
  14127. #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
  14128. if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
  14129. if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
  14130. if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
  14131. #endif
  14132. #ifdef HAVE_SPAWNV
  14133. if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
  14134. if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
  14135. #endif
  14136. #ifdef HAVE_SCHED_H
  14137. #ifdef SCHED_OTHER
  14138. if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
  14139. #endif
  14140. #ifdef SCHED_FIFO
  14141. if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
  14142. #endif
  14143. #ifdef SCHED_RR
  14144. if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
  14145. #endif
  14146. #ifdef SCHED_SPORADIC
  14147. if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
  14148. #endif
  14149. #ifdef SCHED_BATCH
  14150. if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
  14151. #endif
  14152. #ifdef SCHED_IDLE
  14153. if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
  14154. #endif
  14155. #ifdef SCHED_RESET_ON_FORK
  14156. if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
  14157. #endif
  14158. #ifdef SCHED_SYS
  14159. if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
  14160. #endif
  14161. #ifdef SCHED_IA
  14162. if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
  14163. #endif
  14164. #ifdef SCHED_FSS
  14165. if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
  14166. #endif
  14167. #ifdef SCHED_FX
  14168. if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
  14169. #endif
  14170. /* constants for namespaces */
  14171. #if defined(HAVE_SETNS) || defined(HAVE_UNSHARE)
  14172. #ifdef CLONE_FS
  14173. if (PyModule_AddIntMacro(m, CLONE_FS)) return -1;
  14174. #endif
  14175. #ifdef CLONE_FILES
  14176. if (PyModule_AddIntMacro(m, CLONE_FILES)) return -1;
  14177. #endif
  14178. #ifdef CLONE_NEWNS
  14179. if (PyModule_AddIntMacro(m, CLONE_NEWNS)) return -1;
  14180. #endif
  14181. #ifdef CLONE_NEWCGROUP
  14182. if (PyModule_AddIntMacro(m, CLONE_NEWCGROUP)) return -1;
  14183. #endif
  14184. #ifdef CLONE_NEWUTS
  14185. if (PyModule_AddIntMacro(m, CLONE_NEWUTS)) return -1;
  14186. #endif
  14187. #ifdef CLONE_NEWIPC
  14188. if (PyModule_AddIntMacro(m, CLONE_NEWIPC)) return -1;
  14189. #endif
  14190. #ifdef CLONE_NEWUSER
  14191. if (PyModule_AddIntMacro(m, CLONE_NEWUSER)) return -1;
  14192. #endif
  14193. #ifdef CLONE_NEWPID
  14194. if (PyModule_AddIntMacro(m, CLONE_NEWPID)) return -1;
  14195. #endif
  14196. #ifdef CLONE_NEWNET
  14197. if (PyModule_AddIntMacro(m, CLONE_NEWNET)) return -1;
  14198. #endif
  14199. #ifdef CLONE_NEWTIME
  14200. if (PyModule_AddIntMacro(m, CLONE_NEWTIME)) return -1;
  14201. #endif
  14202. #ifdef CLONE_SYSVSEM
  14203. if (PyModule_AddIntMacro(m, CLONE_SYSVSEM)) return -1;
  14204. #endif
  14205. #ifdef CLONE_THREAD
  14206. if (PyModule_AddIntMacro(m, CLONE_THREAD)) return -1;
  14207. #endif
  14208. #ifdef CLONE_SIGHAND
  14209. if (PyModule_AddIntMacro(m, CLONE_SIGHAND)) return -1;
  14210. #endif
  14211. #ifdef CLONE_VM
  14212. if (PyModule_AddIntMacro(m, CLONE_VM)) return -1;
  14213. #endif
  14214. #endif
  14215. #endif
  14216. #ifdef USE_XATTRS
  14217. if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
  14218. if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
  14219. if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
  14220. #endif
  14221. #if HAVE_DECL_RTLD_LAZY
  14222. if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
  14223. #endif
  14224. #if HAVE_DECL_RTLD_NOW
  14225. if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
  14226. #endif
  14227. #if HAVE_DECL_RTLD_GLOBAL
  14228. if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
  14229. #endif
  14230. #if HAVE_DECL_RTLD_LOCAL
  14231. if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
  14232. #endif
  14233. #if HAVE_DECL_RTLD_NODELETE
  14234. if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
  14235. #endif
  14236. #if HAVE_DECL_RTLD_NOLOAD
  14237. if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
  14238. #endif
  14239. #if HAVE_DECL_RTLD_DEEPBIND
  14240. if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
  14241. #endif
  14242. #if HAVE_DECL_RTLD_MEMBER
  14243. if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
  14244. #endif
  14245. #ifdef HAVE_GETRANDOM_SYSCALL
  14246. if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
  14247. if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
  14248. #endif
  14249. #ifdef HAVE_MEMFD_CREATE
  14250. if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
  14251. if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
  14252. #ifdef MFD_HUGETLB
  14253. if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
  14254. #endif
  14255. #ifdef MFD_HUGE_SHIFT
  14256. if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
  14257. #endif
  14258. #ifdef MFD_HUGE_MASK
  14259. if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
  14260. #endif
  14261. #ifdef MFD_HUGE_64KB
  14262. if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
  14263. #endif
  14264. #ifdef MFD_HUGE_512KB
  14265. if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
  14266. #endif
  14267. #ifdef MFD_HUGE_1MB
  14268. if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
  14269. #endif
  14270. #ifdef MFD_HUGE_2MB
  14271. if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
  14272. #endif
  14273. #ifdef MFD_HUGE_8MB
  14274. if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
  14275. #endif
  14276. #ifdef MFD_HUGE_16MB
  14277. if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
  14278. #endif
  14279. #ifdef MFD_HUGE_32MB
  14280. if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
  14281. #endif
  14282. #ifdef MFD_HUGE_256MB
  14283. if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
  14284. #endif
  14285. #ifdef MFD_HUGE_512MB
  14286. if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
  14287. #endif
  14288. #ifdef MFD_HUGE_1GB
  14289. if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
  14290. #endif
  14291. #ifdef MFD_HUGE_2GB
  14292. if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
  14293. #endif
  14294. #ifdef MFD_HUGE_16GB
  14295. if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
  14296. #endif
  14297. #endif /* HAVE_MEMFD_CREATE */
  14298. #if defined(HAVE_EVENTFD) && defined(EFD_CLOEXEC)
  14299. if (PyModule_AddIntMacro(m, EFD_CLOEXEC)) return -1;
  14300. #ifdef EFD_NONBLOCK
  14301. if (PyModule_AddIntMacro(m, EFD_NONBLOCK)) return -1;
  14302. #endif
  14303. #ifdef EFD_SEMAPHORE
  14304. if (PyModule_AddIntMacro(m, EFD_SEMAPHORE)) return -1;
  14305. #endif
  14306. #endif /* HAVE_EVENTFD && EFD_CLOEXEC */
  14307. #if defined(__APPLE__)
  14308. if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
  14309. if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;
  14310. if (PyModule_AddIntConstant(m, "_COPYFILE_ACL", COPYFILE_ACL)) return -1;
  14311. if (PyModule_AddIntConstant(m, "_COPYFILE_XATTR", COPYFILE_XATTR)) return -1;
  14312. #endif
  14313. #ifdef MS_WINDOWS
  14314. if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
  14315. if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
  14316. if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
  14317. if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
  14318. if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
  14319. #endif
  14320. return 0;
  14321. }
  14322. #define PROBE(name, test) \
  14323. static int name(void) \
  14324. { \
  14325. if (test) { \
  14326. return 1; \
  14327. } else { \
  14328. return 0; \
  14329. } \
  14330. }
  14331. #ifdef HAVE_FSTATAT
  14332. PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME)
  14333. #endif
  14334. #ifdef HAVE_FACCESSAT
  14335. PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME)
  14336. #endif
  14337. #ifdef HAVE_FCHMODAT
  14338. PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME)
  14339. #endif
  14340. #ifdef HAVE_FCHOWNAT
  14341. PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME)
  14342. #endif
  14343. #ifdef HAVE_LINKAT
  14344. PROBE(probe_linkat, HAVE_LINKAT_RUNTIME)
  14345. #endif
  14346. #ifdef HAVE_FDOPENDIR
  14347. PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME)
  14348. #endif
  14349. #ifdef HAVE_MKDIRAT
  14350. PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME)
  14351. #endif
  14352. #ifdef HAVE_MKFIFOAT
  14353. PROBE(probe_mkfifoat, HAVE_MKFIFOAT_RUNTIME)
  14354. #endif
  14355. #ifdef HAVE_MKNODAT
  14356. PROBE(probe_mknodat, HAVE_MKNODAT_RUNTIME)
  14357. #endif
  14358. #ifdef HAVE_RENAMEAT
  14359. PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME)
  14360. #endif
  14361. #ifdef HAVE_UNLINKAT
  14362. PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME)
  14363. #endif
  14364. #ifdef HAVE_OPENAT
  14365. PROBE(probe_openat, HAVE_OPENAT_RUNTIME)
  14366. #endif
  14367. #ifdef HAVE_READLINKAT
  14368. PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME)
  14369. #endif
  14370. #ifdef HAVE_SYMLINKAT
  14371. PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME)
  14372. #endif
  14373. #ifdef HAVE_FUTIMENS
  14374. PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
  14375. #endif
  14376. #ifdef HAVE_UTIMENSAT
  14377. PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
  14378. #endif
  14379. static const struct have_function {
  14380. const char * const label;
  14381. int (*probe)(void);
  14382. } have_functions[] = {
  14383. #ifdef HAVE_EVENTFD
  14384. {"HAVE_EVENTFD", NULL},
  14385. #endif
  14386. #ifdef HAVE_FACCESSAT
  14387. { "HAVE_FACCESSAT", probe_faccessat },
  14388. #endif
  14389. #ifdef HAVE_FCHDIR
  14390. { "HAVE_FCHDIR", NULL },
  14391. #endif
  14392. #ifdef HAVE_FCHMOD
  14393. { "HAVE_FCHMOD", NULL },
  14394. #endif
  14395. #ifdef HAVE_FCHMODAT
  14396. { "HAVE_FCHMODAT", probe_fchmodat },
  14397. #endif
  14398. #ifdef HAVE_FCHOWN
  14399. { "HAVE_FCHOWN", NULL },
  14400. #endif
  14401. #ifdef HAVE_FCHOWNAT
  14402. { "HAVE_FCHOWNAT", probe_fchownat },
  14403. #endif
  14404. #ifdef HAVE_FEXECVE
  14405. { "HAVE_FEXECVE", NULL },
  14406. #endif
  14407. #ifdef HAVE_FDOPENDIR
  14408. { "HAVE_FDOPENDIR", probe_fdopendir },
  14409. #endif
  14410. #ifdef HAVE_FPATHCONF
  14411. { "HAVE_FPATHCONF", NULL },
  14412. #endif
  14413. #ifdef HAVE_FSTATAT
  14414. { "HAVE_FSTATAT", probe_fstatat },
  14415. #endif
  14416. #ifdef HAVE_FSTATVFS
  14417. { "HAVE_FSTATVFS", NULL },
  14418. #endif
  14419. #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
  14420. { "HAVE_FTRUNCATE", NULL },
  14421. #endif
  14422. #ifdef HAVE_FUTIMENS
  14423. { "HAVE_FUTIMENS", probe_futimens },
  14424. #endif
  14425. #ifdef HAVE_FUTIMES
  14426. { "HAVE_FUTIMES", NULL },
  14427. #endif
  14428. #ifdef HAVE_FUTIMESAT
  14429. { "HAVE_FUTIMESAT", NULL },
  14430. #endif
  14431. #ifdef HAVE_LINKAT
  14432. { "HAVE_LINKAT", probe_linkat },
  14433. #endif
  14434. #ifdef HAVE_LCHFLAGS
  14435. { "HAVE_LCHFLAGS", NULL },
  14436. #endif
  14437. #ifdef HAVE_LCHMOD
  14438. { "HAVE_LCHMOD", NULL },
  14439. #endif
  14440. #ifdef HAVE_LCHOWN
  14441. { "HAVE_LCHOWN", NULL },
  14442. #endif
  14443. #ifdef HAVE_LSTAT
  14444. { "HAVE_LSTAT", NULL },
  14445. #endif
  14446. #ifdef HAVE_LUTIMES
  14447. { "HAVE_LUTIMES", NULL },
  14448. #endif
  14449. #ifdef HAVE_MEMFD_CREATE
  14450. { "HAVE_MEMFD_CREATE", NULL },
  14451. #endif
  14452. #ifdef HAVE_MKDIRAT
  14453. { "HAVE_MKDIRAT", probe_mkdirat },
  14454. #endif
  14455. #ifdef HAVE_MKFIFOAT
  14456. { "HAVE_MKFIFOAT", probe_mkfifoat },
  14457. #endif
  14458. #ifdef HAVE_MKNODAT
  14459. { "HAVE_MKNODAT", probe_mknodat },
  14460. #endif
  14461. #ifdef HAVE_OPENAT
  14462. { "HAVE_OPENAT", probe_openat },
  14463. #endif
  14464. #ifdef HAVE_READLINKAT
  14465. { "HAVE_READLINKAT", probe_readlinkat },
  14466. #endif
  14467. #ifdef HAVE_RENAMEAT
  14468. { "HAVE_RENAMEAT", probe_renameat },
  14469. #endif
  14470. #ifdef HAVE_SYMLINKAT
  14471. { "HAVE_SYMLINKAT", probe_symlinkat },
  14472. #endif
  14473. #ifdef HAVE_UNLINKAT
  14474. { "HAVE_UNLINKAT", probe_unlinkat },
  14475. #endif
  14476. #ifdef HAVE_UTIMENSAT
  14477. { "HAVE_UTIMENSAT", probe_utimensat },
  14478. #endif
  14479. #ifdef MS_WINDOWS
  14480. { "MS_WINDOWS", NULL },
  14481. #endif
  14482. { NULL, NULL }
  14483. };
  14484. static int
  14485. posixmodule_exec(PyObject *m)
  14486. {
  14487. _posixstate *state = get_posix_state(m);
  14488. #if defined(HAVE_PWRITEV)
  14489. if (HAVE_PWRITEV_RUNTIME) {} else {
  14490. PyObject* dct = PyModule_GetDict(m);
  14491. if (dct == NULL) {
  14492. return -1;
  14493. }
  14494. if (PyDict_DelItemString(dct, "pwritev") == -1) {
  14495. PyErr_Clear();
  14496. }
  14497. if (PyDict_DelItemString(dct, "preadv") == -1) {
  14498. PyErr_Clear();
  14499. }
  14500. }
  14501. #endif
  14502. /* Initialize environ dictionary */
  14503. if (_PyModule_Add(m, "environ", convertenviron()) != 0) {
  14504. return -1;
  14505. }
  14506. if (all_ins(m))
  14507. return -1;
  14508. if (setup_confname_tables(m))
  14509. return -1;
  14510. if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
  14511. return -1;
  14512. }
  14513. #if defined(HAVE_WAITID) && !defined(__APPLE__)
  14514. waitid_result_desc.name = MODNAME ".waitid_result";
  14515. state->WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
  14516. if (PyModule_AddObjectRef(m, "waitid_result", state->WaitidResultType) < 0) {
  14517. return -1;
  14518. }
  14519. #endif
  14520. stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
  14521. stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
  14522. stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
  14523. stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
  14524. state->StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
  14525. if (PyModule_AddObjectRef(m, "stat_result", state->StatResultType) < 0) {
  14526. return -1;
  14527. }
  14528. state->statresult_new_orig = ((PyTypeObject *)state->StatResultType)->tp_new;
  14529. ((PyTypeObject *)state->StatResultType)->tp_new = statresult_new;
  14530. statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
  14531. state->StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
  14532. if (PyModule_AddObjectRef(m, "statvfs_result", state->StatVFSResultType) < 0) {
  14533. return -1;
  14534. }
  14535. #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
  14536. sched_param_desc.name = MODNAME ".sched_param";
  14537. state->SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
  14538. if (PyModule_AddObjectRef(m, "sched_param", state->SchedParamType) < 0) {
  14539. return -1;
  14540. }
  14541. ((PyTypeObject *)state->SchedParamType)->tp_new = os_sched_param;
  14542. if (_PyType_AddMethod((PyTypeObject *)state->SchedParamType,
  14543. &os_sched_param_reduce_method) < 0)
  14544. {
  14545. return -1;
  14546. }
  14547. PyType_Modified((PyTypeObject *)state->SchedParamType);
  14548. #endif
  14549. /* initialize TerminalSize_info */
  14550. state->TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
  14551. if (PyModule_AddObjectRef(m, "terminal_size", state->TerminalSizeType) < 0) {
  14552. return -1;
  14553. }
  14554. /* initialize scandir types */
  14555. PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
  14556. if (ScandirIteratorType == NULL) {
  14557. return -1;
  14558. }
  14559. state->ScandirIteratorType = ScandirIteratorType;
  14560. state->DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
  14561. if (PyModule_AddObjectRef(m, "DirEntry", state->DirEntryType) < 0) {
  14562. return -1;
  14563. }
  14564. times_result_desc.name = MODNAME ".times_result";
  14565. state->TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
  14566. if (PyModule_AddObjectRef(m, "times_result", state->TimesResultType) < 0) {
  14567. return -1;
  14568. }
  14569. state->UnameResultType = (PyObject *)PyStructSequence_NewType(&uname_result_desc);
  14570. if (PyModule_AddObjectRef(m, "uname_result", state->UnameResultType) < 0) {
  14571. return -1;
  14572. }
  14573. if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
  14574. return -1;
  14575. #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
  14576. state->struct_rusage = PyUnicode_InternFromString("struct_rusage");
  14577. if (state->struct_rusage == NULL)
  14578. return -1;
  14579. #endif
  14580. state->st_mode = PyUnicode_InternFromString("st_mode");
  14581. if (state->st_mode == NULL)
  14582. return -1;
  14583. /* suppress "function not used" warnings */
  14584. {
  14585. int ignored;
  14586. fd_specified("", -1);
  14587. follow_symlinks_specified("", 1);
  14588. dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
  14589. dir_fd_converter(Py_None, &ignored);
  14590. dir_fd_unavailable(Py_None, &ignored);
  14591. }
  14592. /*
  14593. * provide list of locally available functions
  14594. * so os.py can populate support_* lists
  14595. */
  14596. PyObject *list = PyList_New(0);
  14597. if (!list) {
  14598. return -1;
  14599. }
  14600. for (const struct have_function *trace = have_functions; trace->label; trace++) {
  14601. PyObject *unicode;
  14602. if (trace->probe && !trace->probe()) continue;
  14603. unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL);
  14604. if (!unicode)
  14605. return -1;
  14606. if (PyList_Append(list, unicode))
  14607. return -1;
  14608. Py_DECREF(unicode);
  14609. }
  14610. return _PyModule_Add(m, "_have_functions", list);
  14611. }
  14612. static PyModuleDef_Slot posixmodile_slots[] = {
  14613. {Py_mod_exec, posixmodule_exec},
  14614. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  14615. {0, NULL}
  14616. };
  14617. static struct PyModuleDef posixmodule = {
  14618. PyModuleDef_HEAD_INIT,
  14619. .m_name = MODNAME,
  14620. .m_doc = posix__doc__,
  14621. .m_size = sizeof(_posixstate),
  14622. .m_methods = posix_methods,
  14623. .m_slots = posixmodile_slots,
  14624. .m_traverse = _posix_traverse,
  14625. .m_clear = _posix_clear,
  14626. .m_free = _posix_free,
  14627. };
  14628. PyMODINIT_FUNC
  14629. INITFUNC(void)
  14630. {
  14631. return PyModuleDef_Init(&posixmodule);
  14632. }
  14633. #ifdef __cplusplus
  14634. }
  14635. #endif