123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502 |
- #define _GNU_SOURCE
- #include "orconfig.h"
- #ifdef HAVE_FCNTL_H
- #include <fcntl.h>
- #endif
- #define UTIL_PRIVATE
- #include "util.h"
- #include "torlog.h"
- #include "crypto.h"
- #include "torint.h"
- #include "container.h"
- #include "address.h"
- #include "sandbox.h"
- #include "backtrace.h"
- #include "util_process.h"
- #include "util_format.h"
- #ifdef _WIN32
- #include <io.h>
- #include <direct.h>
- #include <process.h>
- #include <tchar.h>
- #include <winbase.h>
- #else
- #include <dirent.h>
- #include <pwd.h>
- #include <grp.h>
- #endif
- #ifndef _USE_ISOC99_
- #define _USE_ISOC99_ 1
- #endif
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <signal.h>
- #ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #ifdef HAVE_ERRNO_H
- #include <errno.h>
- #endif
- #ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
- #endif
- #ifdef HAVE_SYS_TIME_H
- #include <sys/time.h>
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #ifdef HAVE_SYS_FCNTL_H
- #include <sys/fcntl.h>
- #endif
- #ifdef HAVE_TIME_H
- #include <time.h>
- #endif
- #ifdef HAVE_MALLOC_MALLOC_H
- #include <malloc/malloc.h>
- #endif
- #ifdef HAVE_MALLOC_H
- #if !defined(OPENBSD) && !defined(__FreeBSD__)
- #include <malloc.h>
- #endif
- #endif
- #ifdef HAVE_MALLOC_NP_H
- #include <malloc_np.h>
- #endif
- #ifdef HAVE_SYS_WAIT_H
- #include <sys/wait.h>
- #endif
- #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
- #include <sys/prctl.h>
- #endif
- #ifdef __clang_analyzer__
- #undef MALLOC_ZERO_WORKS
- #endif
- void
- tor_assertion_failed_(const char *fname, unsigned int line,
- const char *func, const char *expr)
- {
- char buf[256];
- log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
- fname, line, func, expr);
- tor_snprintf(buf, sizeof(buf),
- "Assertion %s failed in %s at %s:%u",
- expr, func, fname, line);
- log_backtrace(LOG_ERR, LD_BUG, buf);
- }
- #ifdef USE_DMALLOC
- #undef strndup
- #include <dmalloc.h>
-
- #define DMALLOC_FN_ARGS , file, line
- #if defined(HAVE_DMALLOC_STRDUP)
-
- #elif defined(HAVE_DMALLOC_STRNDUP)
- #define dmalloc_strdup(file, line, string, xalloc_b) \
- dmalloc_strndup(file, line, (string), -1, xalloc_b)
- #else
- #error "No dmalloc_strdup or equivalent"
- #endif
- #else
- #define DMALLOC_FN_ARGS
- #endif
- void *
- tor_malloc_(size_t size DMALLOC_PARAMS)
- {
- void *result;
- tor_assert(size < SIZE_T_CEILING);
- #ifndef MALLOC_ZERO_WORKS
-
- if (size==0) {
- size=1;
- }
- #endif
- #ifdef USE_DMALLOC
- result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
- #else
- result = malloc(size);
- #endif
- if (PREDICT_UNLIKELY(result == NULL)) {
- log_err(LD_MM,"Out of memory on malloc(). Dying.");
-
- exit(1);
- }
- return result;
- }
- void *
- tor_malloc_zero_(size_t size DMALLOC_PARAMS)
- {
-
- void *result = tor_malloc_(size DMALLOC_FN_ARGS);
- memset(result, 0, size);
- return result;
- }
- #define SQRT_SIZE_MAX_P1 (((size_t)1) << (sizeof(size_t)*4))
- static inline int
- size_mul_check(const size_t x, const size_t y)
- {
-
- return ((x|y) < SQRT_SIZE_MAX_P1 ||
- y == 0 ||
- x <= SIZE_MAX / y);
- }
- void *
- tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS)
- {
- tor_assert(size_mul_check(nmemb, size));
- return tor_malloc_zero_((nmemb * size) DMALLOC_FN_ARGS);
- }
- void *
- tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
- {
- void *result;
- tor_assert(size < SIZE_T_CEILING);
- #ifndef MALLOC_ZERO_WORKS
-
- if (size==0) {
- size=1;
- }
- #endif
- #ifdef USE_DMALLOC
- result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
- #else
- result = realloc(ptr, size);
- #endif
- if (PREDICT_UNLIKELY(result == NULL)) {
- log_err(LD_MM,"Out of memory on realloc(). Dying.");
- exit(1);
- }
- return result;
- }
- void *
- tor_reallocarray_(void *ptr, size_t sz1, size_t sz2 DMALLOC_PARAMS)
- {
-
- tor_assert(size_mul_check(sz1, sz2));
- return tor_realloc(ptr, (sz1 * sz2) DMALLOC_FN_ARGS);
- }
- char *
- tor_strdup_(const char *s DMALLOC_PARAMS)
- {
- char *dup;
- tor_assert(s);
- #ifdef USE_DMALLOC
- dup = dmalloc_strdup(file, line, s, 0);
- #else
- dup = strdup(s);
- #endif
- if (PREDICT_UNLIKELY(dup == NULL)) {
- log_err(LD_MM,"Out of memory on strdup(). Dying.");
- exit(1);
- }
- return dup;
- }
- char *
- tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
- {
- char *dup;
- tor_assert(s);
- tor_assert(n < SIZE_T_CEILING);
- dup = tor_malloc_((n+1) DMALLOC_FN_ARGS);
-
- strncpy(dup, s, n);
- dup[n]='\0';
- return dup;
- }
- void *
- tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS)
- {
- char *dup;
- tor_assert(len < SIZE_T_CEILING);
- tor_assert(mem);
- dup = tor_malloc_(len DMALLOC_FN_ARGS);
- memcpy(dup, mem, len);
- return dup;
- }
- void *
- tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS)
- {
- char *dup;
- tor_assert(len < SIZE_T_CEILING+1);
- tor_assert(mem);
- dup = tor_malloc_(len+1 DMALLOC_FN_ARGS);
- memcpy(dup, mem, len);
- dup[len] = '\0';
- return dup;
- }
- void
- tor_free_(void *mem)
- {
- tor_free(mem);
- }
- void
- tor_log_mallinfo(int severity)
- {
- #ifdef HAVE_MALLINFO
- struct mallinfo mi;
- memset(&mi, 0, sizeof(mi));
- mi = mallinfo();
- tor_log(severity, LD_MM,
- "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
- "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
- "keepcost=%d",
- mi.arena, mi.ordblks, mi.smblks, mi.hblks,
- mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
- mi.keepcost);
- #else
- (void)severity;
- #endif
- #ifdef USE_DMALLOC
- dmalloc_log_changed(0,
- 1,
- 0,
- 0
- );
- #endif
- }
- double
- tor_mathlog(double d)
- {
- return log(d);
- }
- long
- tor_lround(double d)
- {
- #if defined(HAVE_LROUND)
- return lround(d);
- #elif defined(HAVE_RINT)
- return (long)rint(d);
- #else
- return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
- #endif
- }
- int64_t
- tor_llround(double d)
- {
- #if defined(HAVE_LLROUND)
- return (int64_t)llround(d);
- #elif defined(HAVE_RINT)
- return (int64_t)rint(d);
- #else
- return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
- #endif
- }
- int
- tor_log2(uint64_t u64)
- {
- int r = 0;
- if (u64 >= (U64_LITERAL(1)<<32)) {
- u64 >>= 32;
- r = 32;
- }
- if (u64 >= (U64_LITERAL(1)<<16)) {
- u64 >>= 16;
- r += 16;
- }
- if (u64 >= (U64_LITERAL(1)<<8)) {
- u64 >>= 8;
- r += 8;
- }
- if (u64 >= (U64_LITERAL(1)<<4)) {
- u64 >>= 4;
- r += 4;
- }
- if (u64 >= (U64_LITERAL(1)<<2)) {
- u64 >>= 2;
- r += 2;
- }
- if (u64 >= (U64_LITERAL(1)<<1)) {
- u64 >>= 1;
- r += 1;
- }
- return r;
- }
- uint64_t
- round_to_power_of_2(uint64_t u64)
- {
- int lg2;
- uint64_t low;
- uint64_t high;
- if (u64 == 0)
- return 1;
- lg2 = tor_log2(u64);
- low = U64_LITERAL(1) << lg2;
- if (lg2 == 63)
- return low;
- high = U64_LITERAL(1) << (lg2+1);
- if (high - u64 < u64 - low)
- return high;
- else
- return low;
- }
- unsigned
- round_to_next_multiple_of(unsigned number, unsigned divisor)
- {
- tor_assert(divisor > 0);
- if (UINT_MAX - divisor + 1 < number)
- return UINT_MAX;
- number += divisor - 1;
- number -= number % divisor;
- return number;
- }
- uint32_t
- round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
- {
- tor_assert(divisor > 0);
- if (UINT32_MAX - divisor + 1 < number)
- return UINT32_MAX;
- number += divisor - 1;
- number -= number % divisor;
- return number;
- }
- uint64_t
- round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
- {
- tor_assert(divisor > 0);
- if (UINT64_MAX - divisor + 1 < number)
- return UINT64_MAX;
- number += divisor - 1;
- number -= number % divisor;
- return number;
- }
- int64_t
- round_int64_to_next_multiple_of(int64_t number, int64_t divisor)
- {
- tor_assert(divisor > 0);
- if (INT64_MAX - divisor + 1 < number)
- return INT64_MAX;
- if (number >= 0)
- number += divisor - 1;
- number -= number % divisor;
- return number;
- }
- int64_t
- sample_laplace_distribution(double mu, double b, double p)
- {
- double result;
- tor_assert(p >= 0.0 && p < 1.0);
-
- if (p <= 0.0) {
-
- return INT64_MIN;
- }
- result = mu - b * (p > 0.5 ? 1.0 : -1.0)
- * tor_mathlog(1.0 - 2.0 * fabs(p - 0.5));
- return clamp_double_to_int64(result);
- }
- int64_t
- add_laplace_noise(int64_t signal, double random, double delta_f,
- double epsilon)
- {
- int64_t noise;
-
- tor_assert(epsilon > 0.0 && epsilon <= 1.0);
-
- tor_assert(delta_f > 0.0);
-
- noise = sample_laplace_distribution(0.0,
- delta_f / epsilon,
- random);
-
- if (noise > 0 && INT64_MAX - noise < signal)
- return INT64_MAX;
- else if (noise < 0 && INT64_MIN - noise > signal)
- return INT64_MIN;
- else
- return signal + noise;
- }
- int
- n_bits_set_u8(uint8_t v)
- {
- static const int nybble_table[] = {
- 0,
- 1,
- 1,
- 2,
- 1,
- 2,
- 2,
- 3,
- 1,
- 2,
- 2,
- 3,
- 2,
- 3,
- 3,
- 4,
- };
- return nybble_table[v & 15] + nybble_table[v>>4];
- }
- void
- tor_strstrip(char *s, const char *strip)
- {
- char *read = s;
- while (*read) {
- if (strchr(strip, *read)) {
- ++read;
- } else {
- *s++ = *read++;
- }
- }
- *s = '\0';
- }
- const char *
- hex_str(const char *from, size_t fromlen)
- {
- static char buf[65];
- if (fromlen>(sizeof(buf)-1)/2)
- fromlen = (sizeof(buf)-1)/2;
- base16_encode(buf,sizeof(buf),from,fromlen);
- return buf;
- }
- void
- tor_strlower(char *s)
- {
- while (*s) {
- *s = TOR_TOLOWER(*s);
- ++s;
- }
- }
- void
- tor_strupper(char *s)
- {
- while (*s) {
- *s = TOR_TOUPPER(*s);
- ++s;
- }
- }
- int
- tor_strisprint(const char *s)
- {
- while (*s) {
- if (!TOR_ISPRINT(*s))
- return 0;
- s++;
- }
- return 1;
- }
- int
- tor_strisnonupper(const char *s)
- {
- while (*s) {
- if (TOR_ISUPPER(*s))
- return 0;
- s++;
- }
- return 1;
- }
- int
- strcmp_opt(const char *s1, const char *s2)
- {
- if (!s1) {
- if (!s2)
- return 0;
- else
- return -1;
- } else if (!s2) {
- return 1;
- } else {
- return strcmp(s1, s2);
- }
- }
- int
- strcmpstart(const char *s1, const char *s2)
- {
- size_t n = strlen(s2);
- return strncmp(s1, s2, n);
- }
- int
- strcmp_len(const char *s1, const char *s2, size_t s1_len)
- {
- size_t s2_len = strlen(s2);
- if (s1_len < s2_len)
- return -1;
- if (s1_len > s2_len)
- return 1;
- return fast_memcmp(s1, s2, s2_len);
- }
- int
- strcasecmpstart(const char *s1, const char *s2)
- {
- size_t n = strlen(s2);
- return strncasecmp(s1, s2, n);
- }
- int
- strcmpend(const char *s1, const char *s2)
- {
- size_t n1 = strlen(s1), n2 = strlen(s2);
- if (n2>n1)
- return strcmp(s1,s2);
- else
- return strncmp(s1+(n1-n2), s2, n2);
- }
- int
- strcasecmpend(const char *s1, const char *s2)
- {
- size_t n1 = strlen(s1), n2 = strlen(s2);
- if (n2>n1)
- return strcasecmp(s1,s2);
- else
- return strncasecmp(s1+(n1-n2), s2, n2);
- }
- int
- fast_memcmpstart(const void *mem, size_t memlen,
- const char *prefix)
- {
- size_t plen = strlen(prefix);
- if (memlen < plen)
- return -1;
- return fast_memcmp(mem, prefix, plen);
- }
- const char *
- eat_whitespace(const char *s)
- {
- tor_assert(s);
- while (1) {
- switch (*s) {
- case '\0':
- default:
- return s;
- case ' ':
- case '\t':
- case '\n':
- case '\r':
- ++s;
- break;
- case '#':
- ++s;
- while (*s && *s != '\n')
- ++s;
- }
- }
- }
- const char *
- eat_whitespace_eos(const char *s, const char *eos)
- {
- tor_assert(s);
- tor_assert(eos && s <= eos);
- while (s < eos) {
- switch (*s) {
- case '\0':
- default:
- return s;
- case ' ':
- case '\t':
- case '\n':
- case '\r':
- ++s;
- break;
- case '#':
- ++s;
- while (s < eos && *s && *s != '\n')
- ++s;
- }
- }
- return s;
- }
- const char *
- eat_whitespace_no_nl(const char *s)
- {
- while (*s == ' ' || *s == '\t' || *s == '\r')
- ++s;
- return s;
- }
- const char *
- eat_whitespace_eos_no_nl(const char *s, const char *eos)
- {
- while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
- ++s;
- return s;
- }
- const char *
- find_whitespace(const char *s)
- {
-
- while (1) {
- switch (*s)
- {
- case '\0':
- case '#':
- case ' ':
- case '\r':
- case '\n':
- case '\t':
- return s;
- default:
- ++s;
- }
- }
- }
- const char *
- find_whitespace_eos(const char *s, const char *eos)
- {
-
- while (s < eos) {
- switch (*s)
- {
- case '\0':
- case '#':
- case ' ':
- case '\r':
- case '\n':
- case '\t':
- return s;
- default:
- ++s;
- }
- }
- return s;
- }
- const char *
- find_str_at_start_of_line(const char *haystack, const char *needle)
- {
- size_t needle_len = strlen(needle);
- do {
- if (!strncmp(haystack, needle, needle_len))
- return haystack;
- haystack = strchr(haystack, '\n');
- if (!haystack)
- return NULL;
- else
- ++haystack;
- } while (*haystack);
- return NULL;
- }
- int
- string_is_C_identifier(const char *string)
- {
- size_t iter;
- size_t length = strlen(string);
- if (!length)
- return 0;
- for (iter = 0; iter < length ; iter++) {
- if (iter == 0) {
- if (!(TOR_ISALPHA(string[iter]) ||
- string[iter] == '_'))
- return 0;
- } else {
- if (!(TOR_ISALPHA(string[iter]) ||
- TOR_ISDIGIT(string[iter]) ||
- string[iter] == '_'))
- return 0;
- }
- }
- return 1;
- }
- int
- tor_mem_is_zero(const char *mem, size_t len)
- {
- static const char ZERO[] = {
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- };
- while (len >= sizeof(ZERO)) {
-
- if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
- return 0;
- len -= sizeof(ZERO);
- mem += sizeof(ZERO);
- }
-
- if (len)
- return fast_memeq(mem, ZERO, len);
- return 1;
- }
- int
- tor_digest_is_zero(const char *digest)
- {
- static const uint8_t ZERO_DIGEST[] = {
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
- };
- return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
- }
- int
- string_is_key_value(int severity, const char *string)
- {
-
- const char *equal_sign_pos = NULL;
- tor_assert(string);
- if (strlen(string) < 2) {
- tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
- escaped(string));
- return 0;
- }
- equal_sign_pos = strchr(string, '=');
- if (!equal_sign_pos) {
- tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
- return 0;
- }
-
- if (equal_sign_pos == string) {
- tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
- escaped(string));
- return 0;
- }
- return 1;
- }
- int
- string_is_valid_ipv4_address(const char *string)
- {
- struct in_addr addr;
- return (tor_inet_pton(AF_INET,string,&addr) == 1);
- }
- int
- string_is_valid_ipv6_address(const char *string)
- {
- struct in6_addr addr;
- return (tor_inet_pton(AF_INET6,string,&addr) == 1);
- }
- int
- string_is_valid_hostname(const char *string)
- {
- int result = 1;
- smartlist_t *components;
- components = smartlist_new();
- smartlist_split_string(components,string,".",0,0);
- SMARTLIST_FOREACH_BEGIN(components, char *, c) {
- if ((c[0] == '-') || (*c == '_')) {
- result = 0;
- break;
- }
-
- if ((c_sl_idx > 0) && (c_sl_idx + 1 == c_sl_len) && !*c) {
- continue;
- }
- do {
- if ((*c >= 'a' && *c <= 'z') ||
- (*c >= 'A' && *c <= 'Z') ||
- (*c >= '0' && *c <= '9') ||
- (*c == '-') || (*c == '_'))
- c++;
- else
- result = 0;
- } while (result && *c);
- } SMARTLIST_FOREACH_END(c);
- SMARTLIST_FOREACH_BEGIN(components, char *, c) {
- tor_free(c);
- } SMARTLIST_FOREACH_END(c);
- smartlist_free(components);
- return result;
- }
- int
- tor_digest256_is_zero(const char *digest)
- {
- return tor_mem_is_zero(digest, DIGEST256_LEN);
- }
- #define CHECK_STRTOX_RESULT() \
- \
- if (errno == ERANGE) \
- goto err; \
- \
- if (endptr == s) \
- goto err; \
- \
- if (!next && *endptr) \
- goto err; \
- \
- if (r < min || r > max) \
- goto err; \
- if (ok) *ok = 1; \
- if (next) *next = endptr; \
- return r; \
- err: \
- if (ok) *ok = 0; \
- if (next) *next = endptr; \
- return 0
- long
- tor_parse_long(const char *s, int base, long min, long max,
- int *ok, char **next)
- {
- char *endptr;
- long r;
- if (base < 0) {
- if (ok)
- *ok = 0;
- return 0;
- }
- errno = 0;
- r = strtol(s, &endptr, base);
- CHECK_STRTOX_RESULT();
- }
- unsigned long
- tor_parse_ulong(const char *s, int base, unsigned long min,
- unsigned long max, int *ok, char **next)
- {
- char *endptr;
- unsigned long r;
- if (base < 0) {
- if (ok)
- *ok = 0;
- return 0;
- }
- errno = 0;
- r = strtoul(s, &endptr, base);
- CHECK_STRTOX_RESULT();
- }
- double
- tor_parse_double(const char *s, double min, double max, int *ok, char **next)
- {
- char *endptr;
- double r;
- errno = 0;
- r = strtod(s, &endptr);
- CHECK_STRTOX_RESULT();
- }
- uint64_t
- tor_parse_uint64(const char *s, int base, uint64_t min,
- uint64_t max, int *ok, char **next)
- {
- char *endptr;
- uint64_t r;
- if (base < 0) {
- if (ok)
- *ok = 0;
- return 0;
- }
- errno = 0;
- #ifdef HAVE_STRTOULL
- r = (uint64_t)strtoull(s, &endptr, base);
- #elif defined(_WIN32)
- #if defined(_MSC_VER) && _MSC_VER < 1300
- tor_assert(base <= 10);
- r = (uint64_t)_atoi64(s);
- endptr = (char*)s;
- while (TOR_ISSPACE(*endptr)) endptr++;
- while (TOR_ISDIGIT(*endptr)) endptr++;
- #else
- r = (uint64_t)_strtoui64(s, &endptr, base);
- #endif
- #elif SIZEOF_LONG == 8
- r = (uint64_t)strtoul(s, &endptr, base);
- #else
- #error "I don't know how to parse 64-bit numbers."
- #endif
- CHECK_STRTOX_RESULT();
- }
- char *
- esc_for_log(const char *s)
- {
- const char *cp;
- char *result, *outp;
- size_t len = 3;
- if (!s) {
- return tor_strdup("(null)");
- }
- for (cp = s; *cp; ++cp) {
- switch (*cp) {
- case '\\':
- case '\"':
- case '\'':
- case '\r':
- case '\n':
- case '\t':
- len += 2;
- break;
- default:
- if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
- ++len;
- else
- len += 4;
- break;
- }
- }
- tor_assert(len <= SSIZE_MAX);
- result = outp = tor_malloc(len);
- *outp++ = '\"';
- for (cp = s; *cp; ++cp) {
-
- tor_assert((outp-result) < (ssize_t)len-2);
- switch (*cp) {
- case '\\':
- case '\"':
- case '\'':
- *outp++ = '\\';
- *outp++ = *cp;
- break;
- case '\n':
- *outp++ = '\\';
- *outp++ = 'n';
- break;
- case '\t':
- *outp++ = '\\';
- *outp++ = 't';
- break;
- case '\r':
- *outp++ = '\\';
- *outp++ = 'r';
- break;
- default:
- if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
- *outp++ = *cp;
- } else {
- tor_assert((outp-result) < (ssize_t)len-4);
- tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
- outp += 4;
- }
- break;
- }
- }
- tor_assert((outp-result) <= (ssize_t)len-2);
- *outp++ = '\"';
- *outp++ = 0;
- return result;
- }
- char *
- esc_for_log_len(const char *chars, size_t n)
- {
- char *string = tor_strndup(chars, n);
- char *string_escaped = esc_for_log(string);
- tor_free(string);
- return string_escaped;
- }
- const char *
- escaped(const char *s)
- {
- static char *escaped_val_ = NULL;
- tor_free(escaped_val_);
- if (s)
- escaped_val_ = esc_for_log(s);
- else
- escaped_val_ = NULL;
- return escaped_val_;
- }
- char *
- tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
- {
- char *new_string = NULL;
- char *new_cp = NULL;
- size_t length, new_length;
- tor_assert(string);
- length = strlen(string);
- if (!length)
- return tor_strdup("");
-
- if (length > (SIZE_MAX - 1)/2)
- return NULL;
-
- new_length = (length * 2) + 1;
- new_string = new_cp = tor_malloc(new_length);
- while (*string) {
- if (strchr(chars_to_escape, *string))
- *new_cp++ = '\\';
- *new_cp++ = *string++;
- }
- *new_cp = '\0';
- return new_string;
- }
- long
- tv_udiff(const struct timeval *start, const struct timeval *end)
- {
- long udiff;
- long secdiff = end->tv_sec - start->tv_sec;
- if (labs(secdiff+1) > LONG_MAX/1000000) {
- log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
- "apart: %ld seconds", secdiff);
- return LONG_MAX;
- }
- udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
- return udiff;
- }
- long
- tv_mdiff(const struct timeval *start, const struct timeval *end)
- {
- long mdiff;
- long secdiff = end->tv_sec - start->tv_sec;
- if (labs(secdiff+1) > LONG_MAX/1000) {
- log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
- "apart: %ld seconds", secdiff);
- return LONG_MAX;
- }
-
- mdiff = secdiff*1000L +
- ((long)end->tv_usec - (long)start->tv_usec + 500L) / 1000L;
- return mdiff;
- }
- int64_t
- tv_to_msec(const struct timeval *tv)
- {
- int64_t conv = ((int64_t)tv->tv_sec)*1000L;
-
- conv += ((int64_t)tv->tv_usec+500)/1000L;
- return conv;
- }
- #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
- static int
- n_leapdays(int y1, int y2)
- {
- --y1;
- --y2;
- return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
- }
- static const int days_per_month[] =
- { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int
- tor_timegm(const struct tm *tm, time_t *time_out)
- {
-
- time_t year, days, hours, minutes, seconds;
- int i, invalid_year, dpm;
-
- if (tm->tm_year < INT32_MAX-1900) {
- year = tm->tm_year + 1900;
- } else {
-
- year = INT32_MAX;
- }
- invalid_year = (year < 1970 || tm->tm_year >= INT32_MAX-1900);
- if (tm->tm_mon >= 0 && tm->tm_mon <= 11) {
- dpm = days_per_month[tm->tm_mon];
- if (tm->tm_mon == 1 && !invalid_year && IS_LEAPYEAR(tm->tm_year)) {
- dpm = 29;
- }
- } else {
-
- dpm = 0;
- }
- if (invalid_year ||
- tm->tm_mon < 0 || tm->tm_mon > 11 ||
- tm->tm_mday < 1 || tm->tm_mday > dpm ||
- tm->tm_hour < 0 || tm->tm_hour > 23 ||
- tm->tm_min < 0 || tm->tm_min > 59 ||
- tm->tm_sec < 0 || tm->tm_sec > 60) {
- log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
- return -1;
- }
- days = 365 * (year-1970) + n_leapdays(1970,(int)year);
- for (i = 0; i < tm->tm_mon; ++i)
- days += days_per_month[i];
- if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
- ++days;
- days += tm->tm_mday - 1;
- hours = days*24 + tm->tm_hour;
- minutes = hours*60 + tm->tm_min;
- seconds = minutes*60 + tm->tm_sec;
- *time_out = seconds;
- return 0;
- }
- static const char *WEEKDAY_NAMES[] =
- { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
- static const char *MONTH_NAMES[] =
- { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
- void
- format_rfc1123_time(char *buf, time_t t)
- {
- struct tm tm;
- tor_gmtime_r(&t, &tm);
- strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
- tor_assert(tm.tm_wday >= 0);
- tor_assert(tm.tm_wday <= 6);
- memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
- tor_assert(tm.tm_mon >= 0);
- tor_assert(tm.tm_mon <= 11);
- memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
- }
- int
- parse_rfc1123_time(const char *buf, time_t *t)
- {
- struct tm tm;
- char month[4];
- char weekday[4];
- int i, m, invalid_year;
- unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
- unsigned dpm;
- if (strlen(buf) != RFC1123_TIME_LEN)
- return -1;
- memset(&tm, 0, sizeof(tm));
- if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
- &tm_mday, month, &tm_year, &tm_hour,
- &tm_min, &tm_sec) < 7) {
- char *esc = esc_for_log(buf);
- log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
- tor_free(esc);
- return -1;
- }
- m = -1;
- for (i = 0; i < 12; ++i) {
- if (!strcmp(month, MONTH_NAMES[i])) {
- m = i;
- break;
- }
- }
- if (m<0) {
- char *esc = esc_for_log(buf);
- log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
- tor_free(esc);
- return -1;
- }
- tm.tm_mon = m;
- invalid_year = (tm_year >= INT32_MAX || tm_year < 1970);
- tor_assert(m >= 0 && m <= 11);
- dpm = days_per_month[m];
- if (m == 1 && !invalid_year && IS_LEAPYEAR(tm_year)) {
- dpm = 29;
- }
- if (invalid_year || tm_mday < 1 || tm_mday > dpm ||
- tm_hour > 23 || tm_min > 59 || tm_sec > 60) {
- char *esc = esc_for_log(buf);
- log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
- tor_free(esc);
- return -1;
- }
- tm.tm_mday = (int)tm_mday;
- tm.tm_year = (int)tm_year;
- tm.tm_hour = (int)tm_hour;
- tm.tm_min = (int)tm_min;
- tm.tm_sec = (int)tm_sec;
- if (tm.tm_year < 1970) {
- char *esc = esc_for_log(buf);
- log_warn(LD_GENERAL,
- "Got invalid RFC1123 time %s. (Before 1970)", esc);
- tor_free(esc);
- return -1;
- }
- tm.tm_year -= 1900;
- return tor_timegm(&tm, t);
- }
- void
- format_local_iso_time(char *buf, time_t t)
- {
- struct tm tm;
- strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
- }
- void
- format_iso_time(char *buf, time_t t)
- {
- struct tm tm;
- strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
- }
- void
- format_iso_time_nospace(char *buf, time_t t)
- {
- format_iso_time(buf, t);
- buf[10] = 'T';
- }
- void
- format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
- {
- tor_assert(tv);
- format_iso_time_nospace(buf, (time_t)tv->tv_sec);
- tor_snprintf(buf+ISO_TIME_LEN, 8, ".%06d", (int)tv->tv_usec);
- }
- int
- parse_iso_time_(const char *cp, time_t *t, int strict)
- {
- struct tm st_tm;
- unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0;
- int n_fields;
- char extra_char;
- n_fields = tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u%c", &year, &month,
- &day, &hour, &minute, &second, &extra_char);
- if (strict ? (n_fields != 6) : (n_fields < 6)) {
- char *esc = esc_for_log(cp);
- log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
- tor_free(esc);
- return -1;
- }
- if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
- hour > 23 || minute > 59 || second > 60 || year >= INT32_MAX) {
- char *esc = esc_for_log(cp);
- log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
- tor_free(esc);
- return -1;
- }
- st_tm.tm_year = (int)year-1900;
- st_tm.tm_mon = month-1;
- st_tm.tm_mday = day;
- st_tm.tm_hour = hour;
- st_tm.tm_min = minute;
- st_tm.tm_sec = second;
- if (st_tm.tm_year < 70) {
- char *esc = esc_for_log(cp);
- log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
- tor_free(esc);
- return -1;
- }
- return tor_timegm(&st_tm, t);
- }
- int
- parse_iso_time(const char *cp, time_t *t)
- {
- return parse_iso_time_(cp, t, 1);
- }
- int
- parse_http_time(const char *date, struct tm *tm)
- {
- const char *cp;
- char month[4];
- char wkday[4];
- int i;
- unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
- tor_assert(tm);
- memset(tm, 0, sizeof(*tm));
-
- if ((cp = strchr(date, ','))) {
- ++cp;
- if (*cp != ' ')
- return -1;
- ++cp;
- if (tor_sscanf(cp, "%2u %3s %4u %2u:%2u:%2u GMT",
- &tm_mday, month, &tm_year,
- &tm_hour, &tm_min, &tm_sec) == 6) {
-
- tm_year -= 1900;
- } else if (tor_sscanf(cp, "%2u-%3s-%2u %2u:%2u:%2u GMT",
- &tm_mday, month, &tm_year,
- &tm_hour, &tm_min, &tm_sec) == 6) {
-
- } else {
- return -1;
- }
- } else {
-
- if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
- wkday, month, &tm_mday,
- &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
- tm_year -= 1900;
- } else {
- return -1;
- }
- }
- tm->tm_mday = (int)tm_mday;
- tm->tm_year = (int)tm_year;
- tm->tm_hour = (int)tm_hour;
- tm->tm_min = (int)tm_min;
- tm->tm_sec = (int)tm_sec;
- month[3] = '\0';
-
-
- tm->tm_mon = -1;
- for (i = 0; i < 12; ++i) {
- if (!strcasecmp(MONTH_NAMES[i], month)) {
- tm->tm_mon = i;
- }
- }
- if (tm->tm_year < 0 ||
- tm->tm_mon < 0 || tm->tm_mon > 11 ||
- tm->tm_mday < 1 || tm->tm_mday > 31 ||
- tm->tm_hour < 0 || tm->tm_hour > 23 ||
- tm->tm_min < 0 || tm->tm_min > 59 ||
- tm->tm_sec < 0 || tm->tm_sec > 60)
- return -1;
- return 0;
- }
- int
- format_time_interval(char *out, size_t out_len, long interval)
- {
-
- long sec = 0, min = 0, hour = 0, day = 0;
-
- if (interval < -LONG_MAX)
- interval = LONG_MAX;
- else if (interval < 0)
- interval = -interval;
- if (interval >= 86400) {
- day = interval / 86400;
- interval %= 86400;
- }
- if (interval >= 3600) {
- hour = interval / 3600;
- interval %= 3600;
- }
- if (interval >= 60) {
- min = interval / 60;
- interval %= 60;
- }
- sec = interval;
- if (day) {
- return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
- day, hour, min);
- } else if (hour) {
- return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
- } else if (min) {
- return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
- } else {
- return tor_snprintf(out, out_len, "%ld seconds", sec);
- }
- }
- #ifndef TIME_IS_FAST
- static time_t cached_approx_time = 0;
- time_t
- approx_time(void)
- {
- return cached_approx_time;
- }
- void
- update_approx_time(time_t now)
- {
- cached_approx_time = now;
- }
- #endif
- static int
- rate_limit_is_ready(ratelim_t *lim, time_t now)
- {
- if (lim->rate + lim->last_allowed <= now) {
- int res = lim->n_calls_since_last_time + 1;
- lim->last_allowed = now;
- lim->n_calls_since_last_time = 0;
- return res;
- } else {
- ++lim->n_calls_since_last_time;
- return 0;
- }
- }
- char *
- rate_limit_log(ratelim_t *lim, time_t now)
- {
- int n;
- if ((n = rate_limit_is_ready(lim, now))) {
- if (n == 1) {
- return tor_strdup("");
- } else {
- char *cp=NULL;
- tor_asprintf(&cp,
- " [%d similar message(s) suppressed in last %d seconds]",
- n-1, lim->rate);
- return cp;
- }
- } else {
- return NULL;
- }
- }
- ssize_t
- write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
- {
- size_t written = 0;
- ssize_t result;
- tor_assert(count < SSIZE_MAX);
- while (written != count) {
- if (isSocket)
- result = tor_socket_send(fd, buf+written, count-written, 0);
- else
- result = write((int)fd, buf+written, count-written);
- if (result<0)
- return -1;
- written += result;
- }
- return (ssize_t)count;
- }
- ssize_t
- read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
- {
- size_t numread = 0;
- ssize_t result;
- if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
- errno = EINVAL;
- return -1;
- }
- while (numread != count) {
- if (isSocket)
- result = tor_socket_recv(fd, buf+numread, count-numread, 0);
- else
- result = read((int)fd, buf+numread, count-numread);
- if (result<0)
- return -1;
- else if (result == 0)
- break;
- numread += result;
- }
- return (ssize_t)numread;
- }
- static void
- clean_name_for_stat(char *name)
- {
- #ifdef _WIN32
- size_t len = strlen(name);
- if (!len)
- return;
- if (name[len-1]=='\\' || name[len-1]=='/') {
- if (len == 1 || (len==3 && name[1]==':'))
- return;
- name[len-1]='\0';
- }
- #else
- (void)name;
- #endif
- }
- file_status_t
- file_status(const char *fname)
- {
- struct stat st;
- char *f;
- int r;
- if (!fname || strlen(fname) == 0) {
- return FN_ERROR;
- }
- f = tor_strdup(fname);
- clean_name_for_stat(f);
- log_debug(LD_FS, "stat()ing %s", f);
- r = stat(sandbox_intern_string(f), &st);
- tor_free(f);
- if (r) {
- if (errno == ENOENT) {
- return FN_NOENT;
- }
- return FN_ERROR;
- }
- if (st.st_mode & S_IFDIR) {
- return FN_DIR;
- } else if (st.st_mode & S_IFREG) {
- if (st.st_size > 0) {
- return FN_FILE;
- } else if (st.st_size == 0) {
- return FN_EMPTY;
- } else {
- return FN_ERROR;
- }
- #ifndef _WIN32
- } else if (st.st_mode & S_IFIFO) {
- return FN_FILE;
- #endif
- } else {
- return FN_ERROR;
- }
- }
- int
- check_private_dir(const char *dirname, cpd_check_t check,
- const char *effective_user)
- {
- int fd;
- int r;
- struct stat st;
-
- #ifndef _WIN32
- unsigned unwanted_bits = 0;
- const struct passwd *pw = NULL;
- uid_t running_uid;
- gid_t running_gid;
- #else
- (void)effective_user;
- #endif
-
- tor_assert(dirname);
-
- fd = open(sandbox_intern_string(dirname), O_NOFOLLOW);
-
- if (fd == -1) {
- if (errno != ENOENT) {
-
- log_warn(LD_FS, "Directory %s cannot be read: %s", dirname,
- strerror(errno));
- return -1;
- }
-
-
- if (check & CPD_CREATE) {
- log_info(LD_GENERAL, "Creating directory %s", dirname);
- #if defined (_WIN32)
- r = mkdir(dirname);
- #else
- if (check & CPD_GROUP_READ) {
- r = mkdir(dirname, 0750);
- } else {
- r = mkdir(dirname, 0700);
- }
- #endif
-
- if (r) {
- log_warn(LD_FS, "Error creating directory %s: %s", dirname,
- strerror(errno));
- return -1;
- }
-
- fd = open(sandbox_intern_string(dirname), O_NOFOLLOW);
- if ( fd == -1 ) return -1;
- } else if (!(check & CPD_CHECK)) {
- log_warn(LD_FS, "Directory %s does not exist.", dirname);
- return -1;
- }
-
- return 0;
- }
- tor_assert(fd);
-
-
- log_debug(LD_FS, "stat()ing %s", dirname);
-
- r = fstat(fd, &st);
- if (r == -1) {
- log_warn(LD_FS, "fstat() on directory %s failed.", dirname);
- close(fd);
- return -1;
- }
-
-
- if (!(st.st_mode & S_IFDIR)) {
- log_warn(LD_FS, "%s is not a directory", dirname);
- close(fd);
- return -1;
- }
- #ifndef _WIN32
- if (effective_user) {
-
- pw = tor_getpwnam(effective_user);
- if (pw == NULL) {
- log_warn(LD_CONFIG, "Error setting configured user: %s not found",
- effective_user);
- return -1;
- }
- running_uid = pw->pw_uid;
- running_gid = pw->pw_gid;
- } else {
- running_uid = getuid();
- running_gid = getgid();
- }
- if (st.st_uid != running_uid) {
- const struct passwd *pw = NULL;
- char *process_ownername = NULL;
- pw = tor_getpwuid(running_uid);
- process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
- pw = tor_getpwuid(st.st_uid);
- log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by "
- "%s (%d). Perhaps you are running Tor as the wrong user?",
- dirname, process_ownername, (int)running_uid,
- pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
- tor_free(process_ownername);
- close(fd);
- return -1;
- }
- if ( (check & (CPD_GROUP_OK|CPD_GROUP_READ))
- && (st.st_gid != running_gid) && (st.st_gid != 0)) {
- struct group *gr;
- char *process_groupname = NULL;
- gr = getgrgid(running_gid);
- process_groupname = gr ? tor_strdup(gr->gr_name) : tor_strdup("<unknown>");
- gr = getgrgid(st.st_gid);
- log_warn(LD_FS, "%s is not owned by this group (%s, %d) but by group "
- "%s (%d). Are you running Tor as the wrong user?",
- dirname, process_groupname, (int)running_gid,
- gr ? gr->gr_name : "<unknown>", (int)st.st_gid);
- tor_free(process_groupname);
- close(fd);
- return -1;
- }
- if (check & (CPD_GROUP_OK|CPD_GROUP_READ)) {
- unwanted_bits = 0027;
- } else {
- unwanted_bits = 0077;
- }
- if ((st.st_mode & unwanted_bits) != 0) {
- unsigned new_mode;
- if (check & CPD_CHECK_MODE_ONLY) {
- log_warn(LD_FS, "Permissions on directory %s are too permissive.",
- dirname);
- close(fd);
- return -1;
- }
- log_warn(LD_FS, "Fixing permissions on directory %s", dirname);
- new_mode = st.st_mode;
- new_mode |= 0700;
- if (check & CPD_GROUP_READ) {
- new_mode |= 0050;
- }
- new_mode &= ~unwanted_bits;
- if (fchmod(fd, new_mode)) {
- log_warn(LD_FS, "Could not chmod directory %s: %s", dirname,
- strerror(errno));
- close(fd);
- return -1;
- } else {
- close(fd);
- return 0;
- }
- }
- #endif
- close(fd);
- return 0;
- }
- int
- write_str_to_file(const char *fname, const char *str, int bin)
- {
- #ifdef _WIN32
- if (!bin && strchr(str, '\r')) {
- log_warn(LD_BUG,
- "We're writing a text string that already contains a CR to %s",
- escaped(fname));
- }
- #endif
- return write_bytes_to_file(fname, str, strlen(str), bin);
- }
- struct open_file_t {
- char *tempname;
- char *filename;
- unsigned rename_on_close:1;
- unsigned binary:1;
- int fd;
- FILE *stdio_file;
- };
- int
- start_writing_to_file(const char *fname, int open_flags, int mode,
- open_file_t **data_out)
- {
- open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
- const char *open_name;
- int append = 0;
- tor_assert(fname);
- tor_assert(data_out);
- #if (O_BINARY != 0 && O_TEXT != 0)
- tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
- #endif
- new_file->fd = -1;
- new_file->filename = tor_strdup(fname);
- if (open_flags & O_APPEND) {
- open_name = fname;
- new_file->rename_on_close = 0;
- append = 1;
- open_flags &= ~O_APPEND;
- } else {
- tor_asprintf(&new_file->tempname, "%s.tmp", fname);
- open_name = new_file->tempname;
-
- open_flags |= O_CREAT|O_TRUNC;
- open_flags &= ~O_EXCL;
- new_file->rename_on_close = 1;
- }
- #if O_BINARY != 0
- if (open_flags & O_BINARY)
- new_file->binary = 1;
- #endif
- new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
- if (new_file->fd < 0) {
- log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
- open_name, fname, strerror(errno));
- goto err;
- }
- if (append) {
- if (tor_fd_seekend(new_file->fd) < 0) {
- log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
- strerror(errno));
- goto err;
- }
- }
- *data_out = new_file;
- return new_file->fd;
- err:
- if (new_file->fd >= 0)
- close(new_file->fd);
- *data_out = NULL;
- tor_free(new_file->filename);
- tor_free(new_file->tempname);
- tor_free(new_file);
- return -1;
- }
- FILE *
- fdopen_file(open_file_t *file_data)
- {
- tor_assert(file_data);
- if (file_data->stdio_file)
- return file_data->stdio_file;
- tor_assert(file_data->fd >= 0);
- if (!(file_data->stdio_file = fdopen(file_data->fd,
- file_data->binary?"ab":"a"))) {
- log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
- file_data->fd, strerror(errno));
- }
- return file_data->stdio_file;
- }
- FILE *
- start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
- open_file_t **data_out)
- {
- FILE *res;
- if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
- return NULL;
- if (!(res = fdopen_file(*data_out))) {
- abort_writing_to_file(*data_out);
- *data_out = NULL;
- }
- return res;
- }
- static int
- finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
- {
- int r = 0;
- tor_assert(file_data && file_data->filename);
- if (file_data->stdio_file) {
- if (fclose(file_data->stdio_file)) {
- log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
- strerror(errno));
- abort_write = r = -1;
- }
- } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
- log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
- strerror(errno));
- abort_write = r = -1;
- }
- if (file_data->rename_on_close) {
- tor_assert(file_data->tempname && file_data->filename);
- if (abort_write) {
- int res = unlink(file_data->tempname);
- if (res != 0) {
-
- log_warn(LD_FS, "Failed to unlink %s: %s",
- file_data->tempname, strerror(errno));
- r = -1;
- }
- } else {
- tor_assert(strcmp(file_data->filename, file_data->tempname));
- if (replace_file(file_data->tempname, file_data->filename)) {
- log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
- strerror(errno));
- r = -1;
- }
- }
- }
- tor_free(file_data->filename);
- tor_free(file_data->tempname);
- tor_free(file_data);
- return r;
- }
- int
- finish_writing_to_file(open_file_t *file_data)
- {
- return finish_writing_to_file_impl(file_data, 0);
- }
- int
- abort_writing_to_file(open_file_t *file_data)
- {
- return finish_writing_to_file_impl(file_data, 1);
- }
- static int
- write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
- int open_flags)
- {
- open_file_t *file = NULL;
- int fd;
- ssize_t result;
- fd = start_writing_to_file(fname, open_flags, 0600, &file);
- if (fd<0)
- return -1;
- SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
- {
- result = write_all(fd, chunk->bytes, chunk->len, 0);
- if (result < 0) {
- log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
- strerror(errno));
- goto err;
- }
- tor_assert((size_t)result == chunk->len);
- });
- return finish_writing_to_file(file);
- err:
- abort_writing_to_file(file);
- return -1;
- }
- int
- write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin,
- int no_tempfile)
- {
- int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
- if (no_tempfile) {
-
- flags |= O_APPEND;
- }
- return write_chunks_to_file_impl(fname, chunks, flags);
- }
- static int
- write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
- int flags)
- {
- int r;
- sized_chunk_t c = { str, len };
- smartlist_t *chunks = smartlist_new();
- smartlist_add(chunks, &c);
- r = write_chunks_to_file_impl(fname, chunks, flags);
- smartlist_free(chunks);
- return r;
- }
- MOCK_IMPL(int,
- write_bytes_to_file,(const char *fname, const char *str, size_t len,
- int bin))
- {
- return write_bytes_to_file_impl(fname, str, len,
- OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
- }
- int
- append_bytes_to_file(const char *fname, const char *str, size_t len,
- int bin)
- {
- return write_bytes_to_file_impl(fname, str, len,
- OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
- }
- int
- write_bytes_to_new_file(const char *fname, const char *str, size_t len,
- int bin)
- {
- return write_bytes_to_file_impl(fname, str, len,
- OPEN_FLAGS_DONT_REPLACE|
- (bin?O_BINARY:O_TEXT));
- }
- char *
- read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
- {
- ssize_t r;
- size_t pos = 0;
- char *string = NULL;
- size_t string_max = 0;
- if (max_bytes_to_read+1 >= SIZE_T_CEILING) {
- errno = EINVAL;
- return NULL;
- }
- do {
-
- string_max = pos + 1024;
- if (string_max > max_bytes_to_read)
- string_max = max_bytes_to_read + 1;
- string = tor_realloc(string, string_max);
- r = read(fd, string + pos, string_max - pos - 1);
- if (r < 0) {
- int save_errno = errno;
- tor_free(string);
- errno = save_errno;
- return NULL;
- }
- pos += r;
- } while (r > 0 && pos < max_bytes_to_read);
- tor_assert(pos < string_max);
- *sz_out = pos;
- string[pos] = '\0';
- return string;
- }
- char *
- read_file_to_str(const char *filename, int flags, struct stat *stat_out)
- {
- int fd;
- struct stat statbuf;
- char *string;
- ssize_t r;
- int bin = flags & RFTS_BIN;
- tor_assert(filename);
- fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
- if (fd<0) {
- int severity = LOG_WARN;
- int save_errno = errno;
- if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
- severity = LOG_INFO;
- log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
- strerror(errno));
- errno = save_errno;
- return NULL;
- }
- if (fstat(fd, &statbuf)<0) {
- int save_errno = errno;
- close(fd);
- log_warn(LD_FS,"Could not fstat \"%s\".",filename);
- errno = save_errno;
- return NULL;
- }
- #ifndef _WIN32
- #define FIFO_READ_MAX (1024*1024)
- if (S_ISFIFO(statbuf.st_mode)) {
- size_t sz = 0;
- string = read_file_to_str_until_eof(fd, FIFO_READ_MAX, &sz);
- int save_errno = errno;
- if (string && stat_out) {
- statbuf.st_size = sz;
- memcpy(stat_out, &statbuf, sizeof(struct stat));
- }
- close(fd);
- if (!string)
- errno = save_errno;
- return string;
- }
- #endif
- if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING) {
- close(fd);
- errno = EINVAL;
- return NULL;
- }
- string = tor_malloc((size_t)(statbuf.st_size+1));
- r = read_all(fd,string,(size_t)statbuf.st_size,0);
- if (r<0) {
- int save_errno = errno;
- log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
- strerror(errno));
- tor_free(string);
- close(fd);
- errno = save_errno;
- return NULL;
- }
- string[r] = '\0';
- #if defined(_WIN32) || defined(__CYGWIN__)
- if (!bin && strchr(string, '\r')) {
- log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
- "when reading %s. Coping.",
- filename);
- tor_strstrip(string, "\r");
- r = strlen(string);
- }
- if (!bin) {
- statbuf.st_size = (size_t) r;
- } else
- #endif
- if (r != statbuf.st_size) {
-
- int save_errno = errno;
- log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
- (int)r, (long)statbuf.st_size,filename);
- tor_free(string);
- close(fd);
- errno = save_errno;
- return NULL;
- }
- close(fd);
- if (stat_out) {
- memcpy(stat_out, &statbuf, sizeof(struct stat));
- }
- return string;
- }
- #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
- static const char *
- unescape_string(const char *s, char **result, size_t *size_out)
- {
- const char *cp;
- char *out;
- if (s[0] != '\"')
- return NULL;
- cp = s+1;
- while (1) {
- switch (*cp) {
- case '\0':
- case '\n':
- return NULL;
- case '\"':
- goto end_of_loop;
- case '\\':
- if (cp[1] == 'x' || cp[1] == 'X') {
- if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
- return NULL;
- cp += 4;
- } else if (TOR_ISODIGIT(cp[1])) {
- cp += 2;
- if (TOR_ISODIGIT(*cp)) ++cp;
- if (TOR_ISODIGIT(*cp)) ++cp;
- } else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
- || cp[1] == '\\' || cp[1] == '\'') {
- cp += 2;
- } else {
- return NULL;
- }
- break;
- default:
- ++cp;
- break;
- }
- }
- end_of_loop:
- out = *result = tor_malloc(cp-s + 1);
- cp = s+1;
- while (1) {
- switch (*cp)
- {
- case '\"':
- *out = '\0';
- if (size_out) *size_out = out - *result;
- return cp+1;
- case '\0':
- tor_fragile_assert();
- tor_free(*result);
- return NULL;
- case '\\':
- switch (cp[1])
- {
- case 'n': *out++ = '\n'; cp += 2; break;
- case 'r': *out++ = '\r'; cp += 2; break;
- case 't': *out++ = '\t'; cp += 2; break;
- case 'x': case 'X':
- {
- int x1, x2;
- x1 = hex_decode_digit(cp[2]);
- x2 = hex_decode_digit(cp[3]);
- if (x1 == -1 || x2 == -1) {
- tor_free(*result);
- return NULL;
- }
- *out++ = ((x1<<4) + x2);
- cp += 4;
- }
- break;
- case '0': case '1': case '2': case '3': case '4': case '5':
- case '6': case '7':
- {
- int n = cp[1]-'0';
- cp += 2;
- if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
- if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
- if (n > 255) { tor_free(*result); return NULL; }
- *out++ = (char)n;
- }
- break;
- case '\'':
- case '\"':
- case '\\':
- case '\?':
- *out++ = cp[1];
- cp += 2;
- break;
- default:
- tor_free(*result); return NULL;
- }
- break;
- default:
- *out++ = *cp++;
- }
- }
- }
- const char *
- parse_config_line_from_str_verbose(const char *line, char **key_out,
- char **value_out,
- const char **err_out)
- {
-
- const char *key, *val, *cp;
- int continuation = 0;
- tor_assert(key_out);
- tor_assert(value_out);
- *key_out = *value_out = NULL;
- key = val = NULL;
-
- while (1) {
- while (TOR_ISSPACE(*line))
- ++line;
- if (*line == '#') {
- while (*line && *line != '\n')
- ++line;
- } else {
- break;
- }
- }
- if (!*line) {
- *key_out = *value_out = NULL;
- return line;
- }
-
- key = line;
- while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
- ! (line[0] == '\\' && line[1] == '\n'))
- ++line;
- *key_out = tor_strndup(key, line-key);
-
- while (*line == ' ' || *line == '\t')
- ++line;
- val = line;
-
- if (*line == '\"') {
- if (!(line = unescape_string(line, value_out, NULL))) {
- if (err_out)
- *err_out = "Invalid escape sequence in quoted string";
- return NULL;
- }
- while (*line == ' ' || *line == '\t')
- ++line;
- if (*line && *line != '#' && *line != '\n') {
- if (err_out)
- *err_out = "Excess data after quoted string";
- return NULL;
- }
- } else {
-
- while (*line && *line != '\n' && (*line != '#' || continuation)) {
- if (*line == '\\' && line[1] == '\n') {
- continuation = 1;
- line += 2;
- } else if (*line == '#') {
- do {
- ++line;
- } while (*line && *line != '\n');
- if (*line == '\n')
- ++line;
- } else {
- ++line;
- }
- }
- if (*line == '\n') {
- cp = line++;
- } else {
- cp = line;
- }
-
- while (cp>val && TOR_ISSPACE(*(cp-1)))
- --cp;
- tor_assert(cp >= val);
-
- *value_out = tor_strndup(val, cp-val);
- if (continuation) {
- char *v_out, *v_in;
- v_out = v_in = *value_out;
- while (*v_in) {
- if (*v_in == '#') {
- do {
- ++v_in;
- } while (*v_in && *v_in != '\n');
- if (*v_in == '\n')
- ++v_in;
- } else if (v_in[0] == '\\' && v_in[1] == '\n') {
- v_in += 2;
- } else {
- *v_out++ = *v_in++;
- }
- }
- *v_out = '\0';
- }
- }
- if (*line == '#') {
- do {
- ++line;
- } while (*line && *line != '\n');
- }
- while (TOR_ISSPACE(*line)) ++line;
- return line;
- }
- char *
- expand_filename(const char *filename)
- {
- tor_assert(filename);
- #ifdef _WIN32
-
- return tor_strdup(filename);
- #else
- if (*filename == '~') {
- char *home, *result=NULL;
- const char *rest;
- if (filename[1] == '/' || filename[1] == '\0') {
- home = getenv("HOME");
- if (!home) {
- log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
- "expanding \"%s\"; defaulting to \"\".", filename);
- home = tor_strdup("");
- } else {
- home = tor_strdup(home);
- }
- rest = strlen(filename)>=2?(filename+2):"";
- } else {
- #ifdef HAVE_PWD_H
- char *username, *slash;
- slash = strchr(filename, '/');
- if (slash)
- username = tor_strndup(filename+1,slash-filename-1);
- else
- username = tor_strdup(filename+1);
- if (!(home = get_user_homedir(username))) {
- log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
- tor_free(username);
- return NULL;
- }
- tor_free(username);
- rest = slash ? (slash+1) : "";
- #else
- log_warn(LD_CONFIG, "Couldn't expand homedir on system without pwd.h");
- return tor_strdup(filename);
- #endif
- }
- tor_assert(home);
-
- if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
- home[strlen(home)-1] = '\0';
- }
- tor_asprintf(&result,"%s"PATH_SEPARATOR"%s",home,rest);
- tor_free(home);
- return result;
- } else {
- return tor_strdup(filename);
- }
- #endif
- }
- #define MAX_SCANF_WIDTH 9999
- static int
- digit_to_num(char d)
- {
- int num = ((int)d) - (int)'0';
- tor_assert(num <= 9 && num >= 0);
- return num;
- }
- static int
- scan_unsigned(const char **bufp, unsigned long *out, int width, int base)
- {
- unsigned long result = 0;
- int scanned_so_far = 0;
- const int hex = base==16;
- tor_assert(base == 10 || base == 16);
- if (!bufp || !*bufp || !out)
- return -1;
- if (width<0)
- width=MAX_SCANF_WIDTH;
- while (**bufp && (hex?TOR_ISXDIGIT(**bufp):TOR_ISDIGIT(**bufp))
- && scanned_so_far < width) {
- int digit = hex?hex_decode_digit(*(*bufp)++):digit_to_num(*(*bufp)++);
-
-
-
-
-
- if (result > (ULONG_MAX - digit)/base)
- return -1;
- result = result * base + digit;
- ++scanned_so_far;
- }
- if (!scanned_so_far)
- return -1;
- *out = result;
- return 0;
- }
- static int
- scan_signed(const char **bufp, long *out, int width)
- {
- int neg = 0;
- unsigned long result = 0;
- if (!bufp || !*bufp || !out)
- return -1;
- if (width<0)
- width=MAX_SCANF_WIDTH;
- if (**bufp == '-') {
- neg = 1;
- ++*bufp;
- --width;
- }
- if (scan_unsigned(bufp, &result, width, 10) < 0)
- return -1;
- if (neg && result > 0) {
- if (result > ((unsigned long)LONG_MAX) + 1)
- return -1;
-
-
-
-
-
-
-
- *out = (-(long)(result - 1)) - 1;
- } else {
- if (result > LONG_MAX)
- return -1;
- *out = (long)result;
- }
- return 0;
- }
- static int
- scan_double(const char **bufp, double *out, int width)
- {
- int neg = 0;
- double result = 0;
- int scanned_so_far = 0;
- if (!bufp || !*bufp || !out)
- return -1;
- if (width<0)
- width=MAX_SCANF_WIDTH;
- if (**bufp == '-') {
- neg = 1;
- ++*bufp;
- }
- while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
- const int digit = digit_to_num(*(*bufp)++);
- result = result * 10 + digit;
- ++scanned_so_far;
- }
- if (**bufp == '.') {
- double fracval = 0, denominator = 1;
- ++*bufp;
- ++scanned_so_far;
- while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
- const int digit = digit_to_num(*(*bufp)++);
- fracval = fracval * 10 + digit;
- denominator *= 10;
- ++scanned_so_far;
- }
- result += fracval / denominator;
- }
- if (!scanned_so_far)
- return -1;
- *out = neg ? -result : result;
- return 0;
- }
- static int
- scan_string(const char **bufp, char *out, int width)
- {
- int scanned_so_far = 0;
- if (!bufp || !out || width < 0)
- return -1;
- while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
- *out++ = *(*bufp)++;
- ++scanned_so_far;
- }
- *out = '\0';
- return 0;
- }
- int
- tor_vsscanf(const char *buf, const char *pattern, va_list ap)
- {
- int n_matched = 0;
- while (*pattern) {
- if (*pattern != '%') {
- if (*buf == *pattern) {
- ++buf;
- ++pattern;
- continue;
- } else {
- return n_matched;
- }
- } else {
- int width = -1;
- int longmod = 0;
- ++pattern;
- if (TOR_ISDIGIT(*pattern)) {
- width = digit_to_num(*pattern++);
- while (TOR_ISDIGIT(*pattern)) {
- width *= 10;
- width += digit_to_num(*pattern++);
- if (width > MAX_SCANF_WIDTH)
- return -1;
- }
- if (!width)
- return -1;
- }
- if (*pattern == 'l') {
- longmod = 1;
- ++pattern;
- }
- if (*pattern == 'u' || *pattern == 'x') {
- unsigned long u;
- const int base = (*pattern == 'u') ? 10 : 16;
- if (!*buf)
- return n_matched;
- if (scan_unsigned(&buf, &u, width, base)<0)
- return n_matched;
- if (longmod) {
- unsigned long *out = va_arg(ap, unsigned long *);
- *out = u;
- } else {
- unsigned *out = va_arg(ap, unsigned *);
- if (u > UINT_MAX)
- return n_matched;
- *out = (unsigned) u;
- }
- ++pattern;
- ++n_matched;
- } else if (*pattern == 'f') {
- double *d = va_arg(ap, double *);
- if (!longmod)
- return -1;
- if (!*buf)
- return n_matched;
- if (scan_double(&buf, d, width)<0)
- return n_matched;
- ++pattern;
- ++n_matched;
- } else if (*pattern == 'd') {
- long lng=0;
- if (scan_signed(&buf, &lng, width)<0)
- return n_matched;
- if (longmod) {
- long *out = va_arg(ap, long *);
- *out = lng;
- } else {
- int *out = va_arg(ap, int *);
- if (lng < INT_MIN || lng > INT_MAX)
- return n_matched;
- *out = (int)lng;
- }
- ++pattern;
- ++n_matched;
- } else if (*pattern == 's') {
- char *s = va_arg(ap, char *);
- if (longmod)
- return -1;
- if (width < 0)
- return -1;
- if (scan_string(&buf, s, width)<0)
- return n_matched;
- ++pattern;
- ++n_matched;
- } else if (*pattern == 'c') {
- char *ch = va_arg(ap, char *);
- if (longmod)
- return -1;
- if (width != -1)
- return -1;
- if (!*buf)
- return n_matched;
- *ch = *buf++;
- ++pattern;
- ++n_matched;
- } else if (*pattern == '%') {
- if (*buf != '%')
- return n_matched;
- if (longmod)
- return -1;
- ++buf;
- ++pattern;
- } else {
- return -1;
- }
- }
- }
- return n_matched;
- }
- int
- tor_sscanf(const char *buf, const char *pattern, ...)
- {
- int r;
- va_list ap;
- va_start(ap, pattern);
- r = tor_vsscanf(buf, pattern, ap);
- va_end(ap);
- return r;
- }
- void
- smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
- {
- va_list ap;
- va_start(ap, pattern);
- smartlist_add_vasprintf(sl, pattern, ap);
- va_end(ap);
- }
- void
- smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
- va_list args)
- {
- char *str = NULL;
- tor_vasprintf(&str, pattern, args);
- tor_assert(str != NULL);
- smartlist_add(sl, str);
- }
- smartlist_t *
- tor_listdir(const char *dirname)
- {
- smartlist_t *result;
- #ifdef _WIN32
- char *pattern=NULL;
- TCHAR tpattern[MAX_PATH] = {0};
- char name[MAX_PATH*2+1] = {0};
- HANDLE handle;
- WIN32_FIND_DATA findData;
- tor_asprintf(&pattern, "%s\\*", dirname);
- #ifdef UNICODE
- mbstowcs(tpattern,pattern,MAX_PATH);
- #else
- strlcpy(tpattern, pattern, MAX_PATH);
- #endif
- if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(tpattern, &findData))) {
- tor_free(pattern);
- return NULL;
- }
- result = smartlist_new();
- while (1) {
- #ifdef UNICODE
- wcstombs(name,findData.cFileName,MAX_PATH);
- name[sizeof(name)-1] = '\0';
- #else
- strlcpy(name,findData.cFileName,sizeof(name));
- #endif
- if (strcmp(name, ".") &&
- strcmp(name, "..")) {
- smartlist_add(result, tor_strdup(name));
- }
- if (!FindNextFile(handle, &findData)) {
- DWORD err;
- if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
- char *errstr = format_win32_error(err);
- log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
- tor_free(errstr);
- }
- break;
- }
- }
- FindClose(handle);
- tor_free(pattern);
- #else
- const char *prot_dname = sandbox_intern_string(dirname);
- DIR *d;
- struct dirent *de;
- if (!(d = opendir(prot_dname)))
- return NULL;
- result = smartlist_new();
- while ((de = readdir(d))) {
- if (!strcmp(de->d_name, ".") ||
- !strcmp(de->d_name, ".."))
- continue;
- smartlist_add(result, tor_strdup(de->d_name));
- }
- closedir(d);
- #endif
- return result;
- }
- int
- path_is_relative(const char *filename)
- {
- if (filename && filename[0] == '/')
- return 0;
- #ifdef _WIN32
- else if (filename && filename[0] == '\\')
- return 0;
- else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
- filename[1] == ':' && filename[2] == '\\')
- return 0;
- #endif
- else
- return 1;
- }
- #ifndef _WIN32
- static int start_daemon_called = 0;
- static int finish_daemon_called = 0;
- static int daemon_filedes[2];
- void
- start_daemon(void)
- {
- pid_t pid;
- if (start_daemon_called)
- return;
- start_daemon_called = 1;
- if (pipe(daemon_filedes)) {
- log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
- exit(1);
- }
- pid = fork();
- if (pid < 0) {
- log_err(LD_GENERAL,"fork failed. Exiting.");
- exit(1);
- }
- if (pid) {
- int ok;
- char c;
- close(daemon_filedes[1]);
- ok = -1;
- while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
- if (c == '.')
- ok = 1;
- }
- fflush(stdout);
- if (ok == 1)
- exit(0);
- else
- exit(1);
- } else {
- close(daemon_filedes[0]);
- pid = setsid();
-
- if (fork() != 0) {
- exit(0);
- }
- set_main_thread();
- return;
- }
- }
- void
- finish_daemon(const char *desired_cwd)
- {
- int nullfd;
- char c = '.';
- if (finish_daemon_called)
- return;
- if (!start_daemon_called)
- start_daemon();
- finish_daemon_called = 1;
- if (!desired_cwd)
- desired_cwd = "/";
-
- if (chdir(desired_cwd) < 0) {
- log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
- exit(1);
- }
- nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
- if (nullfd < 0) {
- log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
- exit(1);
- }
-
- if (dup2(nullfd,0) < 0 ||
- dup2(nullfd,1) < 0 ||
- dup2(nullfd,2) < 0) {
- log_err(LD_GENERAL,"dup2 failed. Exiting.");
- exit(1);
- }
- if (nullfd > 2)
- close(nullfd);
-
- if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
- log_err(LD_GENERAL,"write failed. Exiting.");
- }
- close(daemon_filedes[1]);
- }
- #else
- void
- start_daemon(void)
- {
- }
- void
- finish_daemon(const char *cp)
- {
- (void)cp;
- }
- #endif
- void
- write_pidfile(const char *filename)
- {
- FILE *pidfile;
- if ((pidfile = fopen(filename, "w")) == NULL) {
- log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
- strerror(errno));
- } else {
- #ifdef _WIN32
- fprintf(pidfile, "%d\n", (int)_getpid());
- #else
- fprintf(pidfile, "%d\n", (int)getpid());
- #endif
- fclose(pidfile);
- }
- }
- #ifdef _WIN32
- HANDLE
- load_windows_system_library(const TCHAR *library_name)
- {
- TCHAR path[MAX_PATH];
- unsigned n;
- n = GetSystemDirectory(path, MAX_PATH);
- if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
- return 0;
- _tcscat(path, TEXT("\\"));
- _tcscat(path, library_name);
- return LoadLibrary(path);
- }
- #endif
- static char *
- format_win_cmdline_argument(const char *arg)
- {
- char *formatted_arg;
- char need_quotes;
- const char *c;
- int i;
- int bs_counter = 0;
-
- const char backslash = '\\';
-
- smartlist_t *arg_chars;
- arg_chars = smartlist_new();
-
- need_quotes = (strchr(arg, ' ') || strchr(arg, '\t') || '\0' == arg[0]);
-
- for (c=arg; *c != '\0'; c++) {
- if ('"' == *c) {
-
- for (i=0; i<(bs_counter*2); i++)
- smartlist_add(arg_chars, (void*)&backslash);
- bs_counter = 0;
-
- smartlist_add(arg_chars, (void*)&backslash);
- smartlist_add(arg_chars, (void*)c);
- } else if ('\\' == *c) {
-
- bs_counter++;
- } else {
-
- for (i=0; i<bs_counter; i++)
- smartlist_add(arg_chars, (void*)&backslash);
- bs_counter = 0;
- smartlist_add(arg_chars, (void*)c);
- }
- }
-
- for (i=0; i<bs_counter; i++)
- smartlist_add(arg_chars, (void*)&backslash);
-
- const size_t formatted_arg_len = smartlist_len(arg_chars) +
- (need_quotes ? 2 : 0) + 1;
- formatted_arg = tor_malloc_zero(formatted_arg_len);
-
- i=0;
- if (need_quotes)
- formatted_arg[i++] = '"';
-
- SMARTLIST_FOREACH(arg_chars, char*, c,
- {
- formatted_arg[i++] = *c;
- });
-
- if (need_quotes)
- formatted_arg[i++] = '"';
- formatted_arg[i] = '\0';
- smartlist_free(arg_chars);
- return formatted_arg;
- }
- char *
- tor_join_win_cmdline(const char *argv[])
- {
- smartlist_t *argv_list;
- char *joined_argv;
- int i;
-
- argv_list = smartlist_new();
- for (i=0; argv[i] != NULL; i++) {
- smartlist_add(argv_list, (void *)format_win_cmdline_argument(argv[i]));
- }
-
- joined_argv = smartlist_join_strings(argv_list, " ", 0, NULL);
-
- SMARTLIST_FOREACH(argv_list, char *, arg,
- {
- tor_free(arg);
- });
- smartlist_free(argv_list);
- return joined_argv;
- }
- static int
- format_number_sigsafe(unsigned long x, char *buf, int buf_len,
- unsigned int radix)
- {
- unsigned long tmp;
- int len;
- char *cp;
-
- if (radix < 2 || radix > 16)
- return 0;
-
- tmp = x;
- len = 1;
- while (tmp >= radix) {
- tmp /= radix;
- ++len;
- }
-
- if (!buf || len >= buf_len)
- return 0;
- cp = buf + len;
- *cp = '\0';
- do {
- unsigned digit = (unsigned) (x % radix);
- tor_assert(cp > buf);
- --cp;
- *cp = "0123456789ABCDEF"[digit];
- x /= radix;
- } while (x);
-
- if (cp != buf) {
- abort();
- }
- return len;
- }
- int
- format_hex_number_sigsafe(unsigned long x, char *buf, int buf_len)
- {
- return format_number_sigsafe(x, buf, buf_len, 16);
- }
- int
- format_dec_number_sigsafe(unsigned long x, char *buf, int buf_len)
- {
- return format_number_sigsafe(x, buf, buf_len, 10);
- }
- #ifndef _WIN32
- STATIC int
- format_helper_exit_status(unsigned char child_state, int saved_errno,
- char *hex_errno)
- {
- unsigned int unsigned_errno;
- int written, left;
- char *cur;
- size_t i;
- int res = -1;
-
- for (i = 0; i < (HEX_ERRNO_SIZE - 1); i++)
- hex_errno[i] = ' ';
- hex_errno[HEX_ERRNO_SIZE - 1] = '\n';
-
- if (saved_errno < 0) {
-
-
-
-
-
-
- unsigned_errno = ((unsigned int) -(saved_errno + 1)) + 1;
- } else {
- unsigned_errno = (unsigned int) saved_errno;
- }
-
- left = HEX_ERRNO_SIZE+1;
- cur = hex_errno;
-
- written = format_hex_number_sigsafe(child_state, cur, left);
- if (written <= 0)
- goto err;
-
- left -= written;
- cur += written;
- if (left <= 0)
- goto err;
-
- *cur = '/';
-
- ++cur;
- --left;
- if (left <= 0)
- goto err;
-
- if (saved_errno < 0) {
- *cur = '-';
- ++cur;
- --left;
- if (left <= 0)
- goto err;
- }
-
- written = format_hex_number_sigsafe(unsigned_errno, cur, left);
- if (written <= 0)
- goto err;
-
- left -= written;
- cur += written;
-
- if (left <= 1)
- goto err;
-
- *cur++ = '\n';
- *cur++ = '\0';
- res = (int)(cur - hex_errno - 1);
- goto done;
- err:
-
- *hex_errno = '\0';
- done:
- return res;
- }
- #endif
- #define DEFAULT_MAX_FD 256
- int
- tor_terminate_process(process_handle_t *process_handle)
- {
- #ifdef _WIN32
- if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) {
- HANDLE handle = process_handle->pid.hProcess;
- if (!TerminateProcess(handle, 0))
- return -1;
- else
- return 0;
- }
- #else
- if (process_handle->waitpid_cb) {
-
- return kill(process_handle->pid, SIGTERM);
- }
- #endif
- return -1;
- }
- int
- tor_process_get_pid(process_handle_t *process_handle)
- {
- #ifdef _WIN32
- return (int) process_handle->pid.dwProcessId;
- #else
- return (int) process_handle->pid;
- #endif
- }
- #ifdef _WIN32
- HANDLE
- tor_process_get_stdout_pipe(process_handle_t *process_handle)
- {
- return process_handle->stdout_pipe;
- }
- #else
- FILE *
- tor_process_get_stdout_pipe(process_handle_t *process_handle)
- {
- return process_handle->stdout_handle;
- }
- #endif
- static process_handle_t *
- process_handle_new(void)
- {
- process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
- #ifdef _WIN32
- out->stdin_pipe = INVALID_HANDLE_VALUE;
- out->stdout_pipe = INVALID_HANDLE_VALUE;
- out->stderr_pipe = INVALID_HANDLE_VALUE;
- #else
- out->stdin_pipe = -1;
- out->stdout_pipe = -1;
- out->stderr_pipe = -1;
- #endif
- return out;
- }
- #ifndef _WIN32
- static void
- process_handle_waitpid_cb(int status, void *arg)
- {
- process_handle_t *process_handle = arg;
- process_handle->waitpid_exit_status = status;
- clear_waitpid_callback(process_handle->waitpid_cb);
- if (process_handle->status == PROCESS_STATUS_RUNNING)
- process_handle->status = PROCESS_STATUS_NOTRUNNING;
- process_handle->waitpid_cb = 0;
- }
- #endif
- #define CHILD_STATE_INIT 0
- #define CHILD_STATE_PIPE 1
- #define CHILD_STATE_MAXFD 2
- #define CHILD_STATE_FORK 3
- #define CHILD_STATE_DUPOUT 4
- #define CHILD_STATE_DUPERR 5
- #define CHILD_STATE_DUPIN 6
- #define CHILD_STATE_CLOSEFD 7
- #define CHILD_STATE_EXEC 8
- #define CHILD_STATE_FAILEXEC 9
- int
- tor_spawn_background(const char *const filename, const char **argv,
- process_environment_t *env,
- process_handle_t **process_handle_out)
- {
- #ifdef _WIN32
- HANDLE stdout_pipe_read = NULL;
- HANDLE stdout_pipe_write = NULL;
- HANDLE stderr_pipe_read = NULL;
- HANDLE stderr_pipe_write = NULL;
- HANDLE stdin_pipe_read = NULL;
- HANDLE stdin_pipe_write = NULL;
- process_handle_t *process_handle;
- int status;
- STARTUPINFOA siStartInfo;
- BOOL retval = FALSE;
- SECURITY_ATTRIBUTES saAttr;
- char *joined_argv;
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
- saAttr.bInheritHandle = TRUE;
-
- saAttr.lpSecurityDescriptor = NULL;
-
- status = PROCESS_STATUS_ERROR;
-
- if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
- log_warn(LD_GENERAL,
- "Failed to create pipe for stdout communication with child process: %s",
- format_win32_error(GetLastError()));
- return status;
- }
- if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
- log_warn(LD_GENERAL,
- "Failed to configure pipe for stdout communication with child "
- "process: %s", format_win32_error(GetLastError()));
- return status;
- }
-
- if (!CreatePipe(&stderr_pipe_read, &stderr_pipe_write, &saAttr, 0)) {
- log_warn(LD_GENERAL,
- "Failed to create pipe for stderr communication with child process: %s",
- format_win32_error(GetLastError()));
- return status;
- }
- if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
- log_warn(LD_GENERAL,
- "Failed to configure pipe for stderr communication with child "
- "process: %s", format_win32_error(GetLastError()));
- return status;
- }
-
- if (!CreatePipe(&stdin_pipe_read, &stdin_pipe_write, &saAttr, 0)) {
- log_warn(LD_GENERAL,
- "Failed to create pipe for stdin communication with child process: %s",
- format_win32_error(GetLastError()));
- return status;
- }
- if (!SetHandleInformation(stdin_pipe_write, HANDLE_FLAG_INHERIT, 0)) {
- log_warn(LD_GENERAL,
- "Failed to configure pipe for stdin communication with child "
- "process: %s", format_win32_error(GetLastError()));
- return status;
- }
-
-
- joined_argv = tor_join_win_cmdline(argv);
- process_handle = process_handle_new();
- process_handle->status = status;
- ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
- ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.hStdError = stderr_pipe_write;
- siStartInfo.hStdOutput = stdout_pipe_write;
- siStartInfo.hStdInput = stdin_pipe_read;
- siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
-
- retval = CreateProcessA(filename,
- joined_argv,
-
- NULL,
- NULL,
- TRUE,
-
- CREATE_NO_WINDOW,
- (env==NULL) ? NULL : env->windows_environment_block,
- NULL,
- &siStartInfo,
- &(process_handle->pid));
- tor_free(joined_argv);
- if (!retval) {
- log_warn(LD_GENERAL,
- "Failed to create child process %s: %s", filename?filename:argv[0],
- format_win32_error(GetLastError()));
- tor_free(process_handle);
- } else {
-
- process_handle->stdout_pipe = stdout_pipe_read;
- process_handle->stderr_pipe = stderr_pipe_read;
- process_handle->stdin_pipe = stdin_pipe_write;
- status = process_handle->status = PROCESS_STATUS_RUNNING;
- }
-
- *process_handle_out = process_handle;
- return status;
- #else
- pid_t pid;
- int stdout_pipe[2];
- int stderr_pipe[2];
- int stdin_pipe[2];
- int fd, retval;
- ssize_t nbytes;
- process_handle_t *process_handle;
- int status;
- const char *error_message = SPAWN_ERROR_MESSAGE;
- size_t error_message_length;
-
- unsigned char child_state = CHILD_STATE_INIT;
- char hex_errno[HEX_ERRNO_SIZE + 2];
- static int max_fd = -1;
- status = PROCESS_STATUS_ERROR;
-
- error_message_length = strlen(error_message);
- child_state = CHILD_STATE_PIPE;
-
- retval = pipe(stdout_pipe);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to set up pipe for stdout communication with child process: %s",
- strerror(errno));
- return status;
- }
- retval = pipe(stderr_pipe);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to set up pipe for stderr communication with child process: %s",
- strerror(errno));
- close(stdout_pipe[0]);
- close(stdout_pipe[1]);
- return status;
- }
- retval = pipe(stdin_pipe);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to set up pipe for stdin communication with child process: %s",
- strerror(errno));
- close(stdout_pipe[0]);
- close(stdout_pipe[1]);
- close(stderr_pipe[0]);
- close(stderr_pipe[1]);
- return status;
- }
- child_state = CHILD_STATE_MAXFD;
- #ifdef _SC_OPEN_MAX
- if (-1 == max_fd) {
- max_fd = (int) sysconf(_SC_OPEN_MAX);
- if (max_fd == -1) {
- max_fd = DEFAULT_MAX_FD;
- log_warn(LD_GENERAL,
- "Cannot find maximum file descriptor, assuming %d", max_fd);
- }
- }
- #else
- max_fd = DEFAULT_MAX_FD;
- #endif
- child_state = CHILD_STATE_FORK;
- pid = fork();
- if (0 == pid) {
-
- #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
-
- prctl(PR_SET_PDEATHSIG, SIGTERM);
- #endif
- child_state = CHILD_STATE_DUPOUT;
-
- retval = dup2(stdout_pipe[1], STDOUT_FILENO);
- if (-1 == retval)
- goto error;
- child_state = CHILD_STATE_DUPERR;
-
- retval = dup2(stderr_pipe[1], STDERR_FILENO);
- if (-1 == retval)
- goto error;
- child_state = CHILD_STATE_DUPIN;
-
- retval = dup2(stdin_pipe[0], STDIN_FILENO);
- if (-1 == retval)
- goto error;
- child_state = CHILD_STATE_CLOSEFD;
- close(stderr_pipe[0]);
- close(stderr_pipe[1]);
- close(stdout_pipe[0]);
- close(stdout_pipe[1]);
- close(stdin_pipe[0]);
- close(stdin_pipe[1]);
-
-
- for (fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
- close(fd);
- }
- child_state = CHILD_STATE_EXEC;
-
- if (env)
- execve(filename, (char *const *) argv, env->unixoid_environment_block);
- else {
- static char *new_env[] = { NULL };
- execve(filename, (char *const *) argv, new_env);
- }
-
- child_state = CHILD_STATE_FAILEXEC;
- error:
- {
-
- int n;
- n = format_helper_exit_status(child_state, errno, hex_errno);
- if (n >= 0) {
-
-
- nbytes = write(STDOUT_FILENO, error_message, error_message_length);
- nbytes = write(STDOUT_FILENO, hex_errno, n);
- }
- }
- (void) nbytes;
- _exit(255);
-
- return status;
- }
-
- if (-1 == pid) {
- log_warn(LD_GENERAL, "Failed to fork child process: %s", strerror(errno));
- close(stdin_pipe[0]);
- close(stdin_pipe[1]);
- close(stdout_pipe[0]);
- close(stdout_pipe[1]);
- close(stderr_pipe[0]);
- close(stderr_pipe[1]);
- return status;
- }
- process_handle = process_handle_new();
- process_handle->status = status;
- process_handle->pid = pid;
-
-
- process_handle->stdout_pipe = stdout_pipe[0];
- retval = close(stdout_pipe[1]);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to close write end of stdout pipe in parent process: %s",
- strerror(errno));
- }
- process_handle->waitpid_cb = set_waitpid_callback(pid,
- process_handle_waitpid_cb,
- process_handle);
- process_handle->stderr_pipe = stderr_pipe[0];
- retval = close(stderr_pipe[1]);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to close write end of stderr pipe in parent process: %s",
- strerror(errno));
- }
-
- process_handle->stdin_pipe = stdin_pipe[1];
- retval = close(stdin_pipe[0]);
- if (-1 == retval) {
- log_warn(LD_GENERAL,
- "Failed to close read end of stdin pipe in parent process: %s",
- strerror(errno));
- }
- status = process_handle->status = PROCESS_STATUS_RUNNING;
-
- if (fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK) < 0 ||
- fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK) < 0 ||
- fcntl(process_handle->stdin_pipe, F_SETFL, O_NONBLOCK) < 0) {
- log_warn(LD_GENERAL, "Failed to set stderror/stdout/stdin pipes "
- "nonblocking in parent process: %s", strerror(errno));
- }
-
- process_handle->stdout_handle = fdopen(process_handle->stdout_pipe, "r");
- process_handle->stderr_handle = fdopen(process_handle->stderr_pipe, "r");
- process_handle->stdin_handle = fdopen(process_handle->stdin_pipe, "r");
- *process_handle_out = process_handle;
- return process_handle->status;
- #endif
- }
- MOCK_IMPL(void,
- tor_process_handle_destroy,(process_handle_t *process_handle,
- int also_terminate_process))
- {
- if (!process_handle)
- return;
- if (also_terminate_process) {
- if (tor_terminate_process(process_handle) < 0) {
- const char *errstr =
- #ifdef _WIN32
- format_win32_error(GetLastError());
- #else
- strerror(errno);
- #endif
- log_notice(LD_GENERAL, "Failed to terminate process with "
- "PID '%d' ('%s').", tor_process_get_pid(process_handle),
- errstr);
- } else {
- log_info(LD_GENERAL, "Terminated process with PID '%d'.",
- tor_process_get_pid(process_handle));
- }
- }
- process_handle->status = PROCESS_STATUS_NOTRUNNING;
- #ifdef _WIN32
- if (process_handle->stdout_pipe)
- CloseHandle(process_handle->stdout_pipe);
- if (process_handle->stderr_pipe)
- CloseHandle(process_handle->stderr_pipe);
- if (process_handle->stdin_pipe)
- CloseHandle(process_handle->stdin_pipe);
- #else
- if (process_handle->stdout_handle)
- fclose(process_handle->stdout_handle);
- if (process_handle->stderr_handle)
- fclose(process_handle->stderr_handle);
- if (process_handle->stdin_handle)
- fclose(process_handle->stdin_handle);
- clear_waitpid_callback(process_handle->waitpid_cb);
- #endif
- memset(process_handle, 0x0f, sizeof(process_handle_t));
- tor_free(process_handle);
- }
- int
- tor_get_exit_code(process_handle_t *process_handle,
- int block, int *exit_code)
- {
- #ifdef _WIN32
- DWORD retval;
- BOOL success;
- if (block) {
-
- retval = WaitForSingleObject(process_handle->pid.hProcess, INFINITE);
- if (retval != WAIT_OBJECT_0) {
- log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
- (int)retval, format_win32_error(GetLastError()));
- return PROCESS_EXIT_ERROR;
- }
- } else {
- retval = WaitForSingleObject(process_handle->pid.hProcess, 0);
- if (WAIT_TIMEOUT == retval) {
-
- return PROCESS_EXIT_RUNNING;
- } else if (retval != WAIT_OBJECT_0) {
- log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
- (int)retval, format_win32_error(GetLastError()));
- return PROCESS_EXIT_ERROR;
- }
- }
- if (exit_code != NULL) {
- success = GetExitCodeProcess(process_handle->pid.hProcess,
- (PDWORD)exit_code);
- if (!success) {
- log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
- format_win32_error(GetLastError()));
- return PROCESS_EXIT_ERROR;
- }
- }
- #else
- int stat_loc;
- int retval;
- if (process_handle->waitpid_cb) {
-
- retval = waitpid(process_handle->pid, &stat_loc, block?0:WNOHANG);
- if (retval == process_handle->pid) {
- clear_waitpid_callback(process_handle->waitpid_cb);
- process_handle->waitpid_cb = NULL;
- process_handle->waitpid_exit_status = stat_loc;
- }
- } else {
-
- retval = process_handle->pid;
- stat_loc = process_handle->waitpid_exit_status;
- }
- if (!block && 0 == retval) {
-
- return PROCESS_EXIT_RUNNING;
- } else if (retval != process_handle->pid) {
- log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s",
- process_handle->pid, strerror(errno));
- return PROCESS_EXIT_ERROR;
- }
- if (!WIFEXITED(stat_loc)) {
- log_warn(LD_GENERAL, "Process %d did not exit normally",
- process_handle->pid);
- return PROCESS_EXIT_ERROR;
- }
- if (exit_code != NULL)
- *exit_code = WEXITSTATUS(stat_loc);
- #endif
- return PROCESS_EXIT_EXITED;
- }
- static inline size_t
- str_num_before(const char *s, char ch)
- {
- const char *cp = strchr(s, ch);
- if (cp)
- return cp - s;
- else
- return strlen(s);
- }
- int
- environment_variable_names_equal(const char *s1, const char *s2)
- {
- size_t s1_name_len = str_num_before(s1, '=');
- size_t s2_name_len = str_num_before(s2, '=');
- return (s1_name_len == s2_name_len &&
- tor_memeq(s1, s2, s1_name_len));
- }
- void
- process_environment_free(process_environment_t *env)
- {
- if (env == NULL) return;
-
- tor_free(env->unixoid_environment_block);
- tor_free(env->windows_environment_block);
- tor_free(env);
- }
- process_environment_t *
- process_environment_make(struct smartlist_t *env_vars)
- {
- process_environment_t *env = tor_malloc_zero(sizeof(process_environment_t));
- size_t n_env_vars = smartlist_len(env_vars);
- size_t i;
- size_t total_env_length;
- smartlist_t *env_vars_sorted;
- tor_assert(n_env_vars + 1 != 0);
- env->unixoid_environment_block = tor_calloc(n_env_vars + 1, sizeof(char *));
-
- total_env_length = 1;
- for (i = 0; i < n_env_vars; ++i) {
- const char *s = smartlist_get(env_vars, i);
- size_t slen = strlen(s);
- tor_assert(slen + 1 != 0);
- tor_assert(slen + 1 < SIZE_MAX - total_env_length);
- total_env_length += slen + 1;
- }
- env->windows_environment_block = tor_malloc_zero(total_env_length);
-
-
- env_vars_sorted = smartlist_new();
- smartlist_add_all(env_vars_sorted, env_vars);
- smartlist_sort_strings(env_vars_sorted);
-
- {
- char *cp = env->windows_environment_block;
- const char *prev_env_var = NULL;
- for (i = 0; i < n_env_vars; ++i) {
- const char *s = smartlist_get(env_vars_sorted, i);
- size_t slen = strlen(s);
- size_t s_name_len = str_num_before(s, '=');
- if (s_name_len == slen) {
- log_warn(LD_GENERAL,
- "Preparing an environment containing a variable "
- "without a value: %s",
- s);
- }
- if (prev_env_var != NULL &&
- environment_variable_names_equal(s, prev_env_var)) {
- log_warn(LD_GENERAL,
- "Preparing an environment containing two variables "
- "with the same name: %s and %s",
- prev_env_var, s);
- }
- prev_env_var = s;
-
- memcpy(cp, s, slen+1);
- env->unixoid_environment_block[i] = cp;
- cp += slen+1;
- }
- tor_assert(cp == env->windows_environment_block + total_env_length - 1);
- }
- smartlist_free(env_vars_sorted);
- return env;
- }
- struct smartlist_t *
- get_current_process_environment_variables(void)
- {
- smartlist_t *sl = smartlist_new();
- char **environ_tmp;
- for (environ_tmp = get_environment(); *environ_tmp; ++environ_tmp) {
- smartlist_add(sl, tor_strdup(*environ_tmp));
- }
- return sl;
- }
- void
- set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
- const char *new_var,
- void (*free_old)(void*),
- int free_p)
- {
- SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
- if (environment_variable_names_equal(s, new_var)) {
- SMARTLIST_DEL_CURRENT(env_vars, s);
- if (free_p) {
- free_old((void *)s);
- }
- }
- } SMARTLIST_FOREACH_END(s);
- if (strchr(new_var, '=') != NULL) {
- smartlist_add(env_vars, (void *)new_var);
- }
- }
- #ifdef _WIN32
- ssize_t
- tor_read_all_handle(HANDLE h, char *buf, size_t count,
- const process_handle_t *process)
- {
- size_t numread = 0;
- BOOL retval;
- DWORD byte_count;
- BOOL process_exited = FALSE;
- if (count > SIZE_T_CEILING || count > SSIZE_MAX)
- return -1;
- while (numread != count) {
-
- retval = PeekNamedPipe(h, NULL, 0, NULL, &byte_count, NULL);
- if (!retval) {
- log_warn(LD_GENERAL,
- "Failed to peek from handle: %s",
- format_win32_error(GetLastError()));
- return -1;
- } else if (0 == byte_count) {
-
-
- if (NULL == process)
- break;
-
- if (process_exited)
- break;
-
- tor_assert(process != NULL);
- byte_count = WaitForSingleObject(process->pid.hProcess, 0);
- if (WAIT_TIMEOUT != byte_count)
- process_exited = TRUE;
- continue;
- }
-
- retval = ReadFile(h, buf+numread, count-numread, &byte_count, NULL);
- tor_assert(byte_count + numread <= count);
- if (!retval) {
- log_warn(LD_GENERAL, "Failed to read from handle: %s",
- format_win32_error(GetLastError()));
- return -1;
- } else if (0 == byte_count) {
-
- break;
- }
- numread += byte_count;
- }
- return (ssize_t)numread;
- }
- #else
- ssize_t
- tor_read_all_handle(FILE *h, char *buf, size_t count,
- const process_handle_t *process,
- int *eof)
- {
- size_t numread = 0;
- char *retval;
- if (eof)
- *eof = 0;
- if (count > SIZE_T_CEILING || count > SSIZE_MAX)
- return -1;
- while (numread != count) {
-
- retval = fgets(buf+numread, (int)(count-numread), h);
- if (NULL == retval) {
- if (feof(h)) {
- log_debug(LD_GENERAL, "fgets() reached end of file");
- if (eof)
- *eof = 1;
- break;
- } else {
- if (EAGAIN == errno) {
- if (process)
- continue;
- else
- break;
- } else {
- log_warn(LD_GENERAL, "fgets() from handle failed: %s",
- strerror(errno));
- return -1;
- }
- }
- }
- tor_assert(retval != NULL);
- tor_assert(strlen(retval) + numread <= count);
- numread += strlen(retval);
- }
- log_debug(LD_GENERAL, "fgets() read %d bytes from handle", (int)numread);
- return (ssize_t)numread;
- }
- #endif
- ssize_t
- tor_read_all_from_process_stdout(const process_handle_t *process_handle,
- char *buf, size_t count)
- {
- #ifdef _WIN32
- return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
- process_handle);
- #else
- return tor_read_all_handle(process_handle->stdout_handle, buf, count,
- process_handle, NULL);
- #endif
- }
- ssize_t
- tor_read_all_from_process_stderr(const process_handle_t *process_handle,
- char *buf, size_t count)
- {
- #ifdef _WIN32
- return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
- process_handle);
- #else
- return tor_read_all_handle(process_handle->stderr_handle, buf, count,
- process_handle, NULL);
- #endif
- }
- int
- tor_split_lines(smartlist_t *sl, char *buf, int len)
- {
-
- int start = 0;
-
- int cur = 0;
-
- char in_line = 0;
-
- while (cur < len) {
-
- for (; cur < len; cur++) {
- if (in_line) {
- if ('\r' == buf[cur] || '\n' == buf[cur]) {
-
- buf[cur] = '\0';
-
- cur++;
-
- break;
- } else {
- if (!TOR_ISPRINT(buf[cur]))
- buf[cur] = '.';
- }
- } else {
- if ('\r' == buf[cur] || '\n' == buf[cur]) {
-
- ;
- } else {
- in_line = 1;
- start = cur;
- if (!TOR_ISPRINT(buf[cur]))
- buf[cur] = '.';
- }
- }
- }
-
- if (in_line)
- smartlist_add(sl, (void *)(buf+start));
- in_line = 0;
- }
- return smartlist_len(sl);
- }
- const char *
- stream_status_to_string(enum stream_status stream_status)
- {
- switch (stream_status) {
- case IO_STREAM_OKAY:
- return "okay";
- case IO_STREAM_EAGAIN:
- return "temporarily unavailable";
- case IO_STREAM_TERM:
- return "terminated";
- case IO_STREAM_CLOSED:
- return "closed";
- default:
- tor_fragile_assert();
- return "unknown";
- }
- }
- static void
- log_portfw_spawn_error_message(const char *buf,
- const char *executable, int *child_status)
- {
-
- int retval, child_state, saved_errno;
- retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
- &child_state, &saved_errno);
- if (retval == 2) {
- log_warn(LD_GENERAL,
- "Failed to start child process \"%s\" in state %d: %s",
- executable, child_state, strerror(saved_errno));
- if (child_status)
- *child_status = 1;
- } else {
-
- log_warn(LD_GENERAL,
- "Unexpected message from port forwarding helper \"%s\": %s",
- executable, buf);
- }
- }
- #ifdef _WIN32
- MOCK_IMPL(smartlist_t *,
- tor_get_lines_from_handle, (HANDLE *handle,
- enum stream_status *stream_status_out))
- {
- int pos;
- char stdout_buf[600] = {0};
- smartlist_t *lines = NULL;
- tor_assert(stream_status_out);
- *stream_status_out = IO_STREAM_TERM;
- pos = tor_read_all_handle(handle, stdout_buf, sizeof(stdout_buf) - 1, NULL);
- if (pos < 0) {
- *stream_status_out = IO_STREAM_TERM;
- return NULL;
- }
- if (pos == 0) {
- *stream_status_out = IO_STREAM_EAGAIN;
- return NULL;
- }
-
-
- stdout_buf[pos] = '\0';
-
- lines = smartlist_new();
- tor_split_lines(lines, stdout_buf, pos);
-
- SMARTLIST_FOREACH(lines, char *, line,
- SMARTLIST_REPLACE_CURRENT(lines, line, tor_strdup(line)));
- *stream_status_out = IO_STREAM_OKAY;
- return lines;
- }
- static int
- log_from_handle(HANDLE *pipe, int severity)
- {
- char buf[256];
- int pos;
- smartlist_t *lines;
- pos = tor_read_all_handle(pipe, buf, sizeof(buf) - 1, NULL);
- if (pos < 0) {
-
- log_warn(LD_GENERAL, "Failed to read data from subprocess");
- return -1;
- }
- if (0 == pos) {
-
- log_debug(LD_GENERAL, "Subprocess had nothing to say");
- return 0;
- }
-
-
- buf[pos] = '\0';
- log_debug(LD_GENERAL, "Subprocess had %d bytes to say", pos);
-
- lines = smartlist_new();
- tor_split_lines(lines, buf, pos);
-
- SMARTLIST_FOREACH(lines, char *, line,
- {
- log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", line);
- });
- smartlist_free(lines);
- return 0;
- }
- #else
- MOCK_IMPL(smartlist_t *,
- tor_get_lines_from_handle, (FILE *handle,
- enum stream_status *stream_status_out))
- {
- enum stream_status stream_status;
- char stdout_buf[400];
- smartlist_t *lines = NULL;
- while (1) {
- memset(stdout_buf, 0, sizeof(stdout_buf));
- stream_status = get_string_from_pipe(handle,
- stdout_buf, sizeof(stdout_buf) - 1);
- if (stream_status != IO_STREAM_OKAY)
- goto done;
- if (!lines) lines = smartlist_new();
- smartlist_add(lines, tor_strdup(stdout_buf));
- }
- done:
- *stream_status_out = stream_status;
- return lines;
- }
- static int
- log_from_pipe(FILE *stream, int severity, const char *executable,
- int *child_status)
- {
- char buf[256];
- enum stream_status r;
- for (;;) {
- r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
- if (r == IO_STREAM_CLOSED) {
- return 1;
- } else if (r == IO_STREAM_EAGAIN) {
- return 0;
- } else if (r == IO_STREAM_TERM) {
- return -1;
- }
- tor_assert(r == IO_STREAM_OKAY);
-
- if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
- log_portfw_spawn_error_message(buf, executable, child_status);
- } else {
- log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
- }
- }
-
- return -1;
- }
- #endif
- enum stream_status
- get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
- {
- char *retval;
- size_t len;
- tor_assert(count <= INT_MAX);
- retval = fgets(buf_out, (int)count, stream);
- if (!retval) {
- if (feof(stream)) {
-
-
- return IO_STREAM_CLOSED;
- } else {
- if (EAGAIN == errno) {
-
- return IO_STREAM_EAGAIN;
- } else {
-
- return IO_STREAM_TERM;
- }
- }
- } else {
- len = strlen(buf_out);
- if (len == 0) {
-
- return IO_STREAM_EAGAIN;
- }
- if (buf_out[len - 1] == '\n') {
-
- buf_out[len - 1] = '\0';
- } else {
-
- if (!feof(stream))
- log_info(LD_GENERAL,
- "Line from stream was truncated: %s", buf_out);
-
- }
- return IO_STREAM_OKAY;
- }
-
- return IO_STREAM_TERM;
- }
- static void
- handle_fw_helper_line(const char *executable, const char *line)
- {
- smartlist_t *tokens = smartlist_new();
- char *message = NULL;
- char *message_for_log = NULL;
- const char *external_port = NULL;
- const char *internal_port = NULL;
- const char *result = NULL;
- int port = 0;
- int success = 0;
- if (strcmpstart(line, SPAWN_ERROR_MESSAGE) == 0) {
-
- int child_status;
- log_portfw_spawn_error_message(line, executable, &child_status);
- goto done;
- }
- smartlist_split_string(tokens, line, NULL,
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(tokens) < 5)
- goto err;
- if (strcmp(smartlist_get(tokens, 0), "tor-fw-helper") ||
- strcmp(smartlist_get(tokens, 1), "tcp-forward"))
- goto err;
- external_port = smartlist_get(tokens, 2);
- internal_port = smartlist_get(tokens, 3);
- result = smartlist_get(tokens, 4);
- if (smartlist_len(tokens) > 5) {
-
- int i;
- int message_words_n = smartlist_len(tokens) - 5;
- smartlist_t *message_sl = smartlist_new();
- for (i = 0; i < message_words_n; i++)
- smartlist_add(message_sl, smartlist_get(tokens, 5+i));
- tor_assert(smartlist_len(message_sl) > 0);
- message = smartlist_join_strings(message_sl, " ", 0, NULL);
-
- tor_asprintf(&message_for_log, " ('%s')", message);
- smartlist_free(message_sl);
- }
- port = atoi(external_port);
- if (port < 1 || port > 65535)
- goto err;
- port = atoi(internal_port);
- if (port < 1 || port > 65535)
- goto err;
- if (!strcmp(result, "SUCCESS"))
- success = 1;
- else if (!strcmp(result, "FAIL"))
- success = 0;
- else
- goto err;
- if (!success) {
- log_warn(LD_GENERAL, "Tor was unable to forward TCP port '%s' to '%s'%s. "
- "Please make sure that your router supports port "
- "forwarding protocols (like NAT-PMP). Note that if '%s' is "
- "your ORPort, your relay will be unable to receive inbound "
- "traffic.", external_port, internal_port,
- message_for_log ? message_for_log : "",
- internal_port);
- } else {
- log_info(LD_GENERAL,
- "Tor successfully forwarded TCP port '%s' to '%s'%s.",
- external_port, internal_port,
- message_for_log ? message_for_log : "");
- }
- goto done;
- err:
- log_warn(LD_GENERAL, "tor-fw-helper sent us a string we could not "
- "parse (%s).", line);
- done:
- SMARTLIST_FOREACH(tokens, char *, cp, tor_free(cp));
- smartlist_free(tokens);
- tor_free(message);
- tor_free(message_for_log);
- }
- static int
- handle_fw_helper_output(const char *executable,
- process_handle_t *process_handle)
- {
- smartlist_t *fw_helper_output = NULL;
- enum stream_status stream_status = 0;
- fw_helper_output =
- tor_get_lines_from_handle(tor_process_get_stdout_pipe(process_handle),
- &stream_status);
- if (!fw_helper_output) {
-
- return (stream_status == IO_STREAM_EAGAIN) ? 0 : -1;
- }
-
- SMARTLIST_FOREACH_BEGIN(fw_helper_output, char *, line) {
- handle_fw_helper_line(executable, line);
- tor_free(line);
- } SMARTLIST_FOREACH_END(line);
- smartlist_free(fw_helper_output);
- return 0;
- }
- void
- tor_check_port_forwarding(const char *filename,
- smartlist_t *ports_to_forward,
- time_t now)
- {
- #define TIME_TO_EXEC_FWHELPER_SUCCESS 300
- #define TIME_TO_EXEC_FWHELPER_FAIL 60
-
- static process_handle_t *child_handle=NULL;
- static time_t time_to_run_helper = 0;
- int stderr_status, retval;
- int stdout_status = 0;
- tor_assert(filename);
-
- if ((!child_handle || child_handle->status != PROCESS_STATUS_RUNNING) &&
- time_to_run_helper < now) {
-
- const char **argv;
- int args_n, status;
- int argv_index = 0;
- tor_assert(smartlist_len(ports_to_forward) > 0);
-
- if ((size_t) smartlist_len(ports_to_forward) >
- (((SIZE_MAX/sizeof(char*)) - 2)/2)) {
- log_warn(LD_GENERAL,
- "Overflow during argv allocation. This shouldn't happen.");
- return;
- }
-
- if (smartlist_len(ports_to_forward) > (INT_MAX - 2)/2) {
- log_warn(LD_GENERAL,
- "Overflow during argv_index increase. This shouldn't happen.");
- return;
- }
-
- args_n = 1 + 2*smartlist_len(ports_to_forward) + 1;
- argv = tor_calloc(args_n, sizeof(char *));
- argv[argv_index++] = filename;
- SMARTLIST_FOREACH_BEGIN(ports_to_forward, const char *, port) {
- argv[argv_index++] = "-p";
- argv[argv_index++] = port;
- } SMARTLIST_FOREACH_END(port);
- argv[argv_index] = NULL;
-
- time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
- if (child_handle) {
- tor_process_handle_destroy(child_handle, 1);
- child_handle = NULL;
- }
- #ifdef _WIN32
-
- status = tor_spawn_background(NULL, argv, NULL, &child_handle);
- #else
- status = tor_spawn_background(filename, argv, NULL, &child_handle);
- #endif
- tor_free_((void*)argv);
- argv=NULL;
- if (PROCESS_STATUS_ERROR == status) {
- log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
- filename);
- time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
- return;
- }
- log_info(LD_GENERAL,
- "Started port forwarding helper (%s) with pid '%d'",
- filename, tor_process_get_pid(child_handle));
- }
-
- if (child_handle && PROCESS_STATUS_RUNNING == child_handle->status) {
-
- retval = 0;
- #ifdef _WIN32
- stderr_status = log_from_handle(child_handle->stderr_pipe, LOG_INFO);
- #else
- stderr_status = log_from_pipe(child_handle->stderr_handle,
- LOG_INFO, filename, &retval);
- #endif
- if (handle_fw_helper_output(filename, child_handle) < 0) {
- log_warn(LD_GENERAL, "Failed to handle fw helper output.");
- stdout_status = -1;
- retval = -1;
- }
- if (retval) {
-
- time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
- }
-
- if (-1 == stdout_status || -1 == stderr_status)
-
- retval = -1;
- #ifdef _WIN32
- else if (!child_handle || tor_get_exit_code(child_handle, 0, NULL) !=
- PROCESS_EXIT_RUNNING) {
-
-
-
- retval = 1;
- }
- #else
- else if (1 == stdout_status || 1 == stderr_status)
-
-
- retval = 1;
- #endif
- else
-
- retval = 0;
-
- if (0 != retval) {
- if (1 == retval) {
- log_info(LD_GENERAL, "Port forwarding helper terminated");
- child_handle->status = PROCESS_STATUS_NOTRUNNING;
- } else {
- log_warn(LD_GENERAL, "Failed to read from port forwarding helper");
- child_handle->status = PROCESS_STATUS_ERROR;
- }
-
- }
- }
- }
- void
- tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
- {
- rng->state = (uint32_t)(seed & 0x7fffffff);
- }
- int32_t
- tor_weak_random(tor_weak_rng_t *rng)
- {
-
- rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
- return (int32_t) rng->state;
- }
- int32_t
- tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
- {
-
- int divisor, result;
- tor_assert(top > 0);
- divisor = TOR_WEAK_RANDOM_MAX / top;
- do {
- result = (int32_t)(tor_weak_random(rng) / divisor);
- } while (result >= top);
- return result;
- }
- int64_t
- clamp_double_to_int64(double number)
- {
- int exp;
-
- if (isnan(number)) {
- return 0;
- }
-
- frexp(number, &exp);
-
- if (isfinite(number) && exp <= 63) {
- return number;
- }
-
- return signbit(number) ? INT64_MIN : INT64_MAX;
- }
|