osbase.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. osbase.c
  5. Abstract:
  6. This module implements the Operating System Base interface.
  7. Author:
  8. Evan Green 25-Feb-2013
  9. Environment:
  10. User mode
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "osbasep.h"
  16. //
  17. // ---------------------------------------------------------------- Definitions
  18. //
  19. #define OS_MOUNT_ALLOCATION_TAG 0x744D734F // 'tMsO'
  20. #define OS_VERSION_ALLOCATION_TAG 0x73726556 // 'sreV'
  21. #define OS_CURRENT_DIRECTORY_ALLOCATION_TAG 0x6443734F // 'dCsO'
  22. //
  23. // Defines the number of times the get mount points routine should try to
  24. // collect mount points.
  25. //
  26. #define OS_GET_MOUNT_POINTS_TRY_COUNT 5
  27. //
  28. // Defines the initial size of the mount points buffer.
  29. //
  30. #define OS_GET_MOUNT_POINTS_BUFFER_SIZE_GUESS 4096
  31. //
  32. // Defines the number of times the get current directory routine should try
  33. // to collect the current directory string.
  34. //
  35. #define OS_GET_CURRENT_DIRECTORY_TRY_COUNT 5
  36. //
  37. // Defines the initial size of the current directory buffer.
  38. //
  39. #define OS_GET_CURRENT_DIRECTORY_BUFFER_SIZE_GUESS 256
  40. //
  41. // ------------------------------------------------------ Data Type Definitions
  42. //
  43. //
  44. // ----------------------------------------------- Internal Function Prototypes
  45. //
  46. PVOID
  47. OspSetSignalHandler (
  48. PVOID SignalHandlerRoutine
  49. );
  50. KSTATUS
  51. OspGetSetFileInformation (
  52. HANDLE Directory,
  53. PSTR Path,
  54. ULONG PathSize,
  55. BOOL FollowLink,
  56. PSET_FILE_INFORMATION Request
  57. );
  58. KSTATUS
  59. OspMountOrUnmount (
  60. PSTR MountPointPath,
  61. ULONG MountPointPathSize,
  62. PSTR TargetPath,
  63. ULONG TargetPathSize,
  64. ULONG Flags
  65. );
  66. NO_RETURN
  67. VOID
  68. OspExitThread (
  69. PVOID UnmapAddress,
  70. UINTN UnmapSize
  71. );
  72. //
  73. // -------------------------------------------------------------------- Globals
  74. //
  75. //
  76. // Store a boolean indicating whether this library is initialized or not to be
  77. // moderately defensive against people accidentally calling this routine
  78. // multiple times.
  79. //
  80. BOOL OsLibraryInitialized = FALSE;
  81. //
  82. // Store a pointer to the signal handling routine.
  83. //
  84. volatile PSIGNAL_HANDLER_ROUTINE OsSignalHandler = NULL;
  85. //
  86. // Store pointers to the system version information if it has been requested
  87. // already.
  88. //
  89. PSTR OsProductName;
  90. PSTR OsBuildString;
  91. ULONG OsSystemVersionStringsSize;
  92. //
  93. // ------------------------------------------------------------------ Functions
  94. //
  95. OS_API
  96. VOID
  97. OsInitializeLibrary (
  98. PPROCESS_ENVIRONMENT Environment
  99. )
  100. /*++
  101. Routine Description:
  102. This routine initializes the base OS library. It needs to be called only
  103. once, when the library is first loaded.
  104. Arguments:
  105. Environment - Supplies a pointer to the environment information.
  106. Return Value:
  107. None.
  108. --*/
  109. {
  110. if (OsLibraryInitialized != FALSE) {
  111. return;
  112. }
  113. OsEnvironment = Environment;
  114. OsLibraryInitialized = TRUE;
  115. OspSetUpSystemCalls();
  116. OspInitializeMemory();
  117. OspInitializeImageSupport();
  118. OspInitializeThreadSupport();
  119. //
  120. // Register the signal handler to start receiving signals.
  121. //
  122. OspSetSignalHandler(OspSignalHandler);
  123. return;
  124. }
  125. OS_API
  126. VOID
  127. OsTestSystemCall (
  128. VOID
  129. )
  130. /*++
  131. Routine Description:
  132. This routine performs a meaningless system call.
  133. Arguments:
  134. None.
  135. Return Value:
  136. None.
  137. --*/
  138. {
  139. OsSystemCall(SystemCallInvalid, NULL);
  140. return;
  141. }
  142. OS_API
  143. KSTATUS
  144. OsOpen (
  145. HANDLE Directory,
  146. PSTR Path,
  147. ULONG PathLength,
  148. ULONG Flags,
  149. FILE_PERMISSIONS CreatePermissions,
  150. PHANDLE Handle
  151. )
  152. /*++
  153. Routine Description:
  154. This routine opens a file or other I/O object.
  155. Arguments:
  156. Directory - Supplies an optional handle to a directory to start the
  157. search from if the supplied path is relative. Supply INVALID_HANDLE
  158. here to to use the current directory for relative paths.
  159. Path - Supplies a pointer to a string containing the path of the object
  160. to open.
  161. PathLength - Supplies the length of the path buffer, in bytes, including
  162. the null terminator.
  163. Flags - Supplies flags associated with the open operation. See
  164. SYS_OPEN_FLAG_* definitions.
  165. CreatePermissions - Supplies the permissions for create operations.
  166. Handle - Supplies a pointer where a handle will be returned on success.
  167. Return Value:
  168. Status code.
  169. --*/
  170. {
  171. SYSTEM_CALL_OPEN Parameters;
  172. Parameters.Directory = Directory;
  173. Parameters.Path = Path;
  174. Parameters.PathBufferLength = PathLength;
  175. Parameters.Flags = Flags;
  176. Parameters.CreatePermissions = CreatePermissions & FILE_PERMISSION_MASK;
  177. OsSystemCall(SystemCallOpen, &Parameters);
  178. *Handle = Parameters.Handle;
  179. return Parameters.Status;
  180. }
  181. OS_API
  182. KSTATUS
  183. OsOpenDevice (
  184. DEVICE_ID DeviceId,
  185. ULONG Flags,
  186. PHANDLE Handle
  187. )
  188. /*++
  189. Routine Description:
  190. This routine opens a device directly.
  191. Arguments:
  192. DeviceId - Supplies the identifier of the device to open.
  193. Flags - Supplies flags associated with the open operation. See
  194. SYS_OPEN_FLAG_* definitions.
  195. Handle - Supplies a pointer where a handle will be returned on success.
  196. Return Value:
  197. Status code.
  198. --*/
  199. {
  200. SYSTEM_CALL_OPEN_DEVICE Parameters;
  201. Parameters.DeviceId = DeviceId;
  202. Parameters.Flags = Flags;
  203. OsSystemCall(SystemCallOpenDevice, &Parameters);
  204. *Handle = Parameters.Handle;
  205. return Parameters.Status;
  206. }
  207. OS_API
  208. KSTATUS
  209. OsClose (
  210. HANDLE Handle
  211. )
  212. /*++
  213. Routine Description:
  214. This routine closes an I/O handle.
  215. Arguments:
  216. Handle - Supplies a pointer to the open handle.
  217. Return Value:
  218. Status code.
  219. --*/
  220. {
  221. SYSTEM_CALL_CLOSE Parameters;
  222. Parameters.Handle = Handle;
  223. OsSystemCall(SystemCallClose, &Parameters);
  224. return Parameters.Status;
  225. }
  226. OS_API
  227. KSTATUS
  228. OsPerformIo (
  229. HANDLE Handle,
  230. IO_OFFSET Offset,
  231. UINTN Size,
  232. ULONG Flags,
  233. ULONG TimeoutInMilliseconds,
  234. PVOID Buffer,
  235. PUINTN BytesCompleted
  236. )
  237. /*++
  238. Routine Description:
  239. This routine performs I/O on an open handle.
  240. Arguments:
  241. Handle - Supplies a pointer to the opened I/O handle.
  242. Offset - Supplies the offset into the file to read from or write to. Set
  243. this to IO_OFFSET_NONE to do I/O at the current file position or for
  244. handles that are not seekable.
  245. Size - Supplies the number of bytes to transfer.
  246. Flags - Supplies a bitfield of flags. See SYS_IO_FLAG_* definitions.
  247. TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
  248. operation should be waited on before timing out. Use
  249. SYS_WAIT_TIME_INDEFINITE to wait forever on the I/O.
  250. Buffer - Supplies a pointer to the buffer containing the data to write or
  251. where the read data should be returned, depending on the operation.
  252. BytesCompleted - Supplies a pointer where the number of bytes completed
  253. will be returned.
  254. Return Value:
  255. Status code.
  256. --*/
  257. {
  258. SYSTEM_CALL_PERFORM_IO Parameters;
  259. Parameters.Handle = Handle;
  260. Parameters.Buffer = Buffer;
  261. Parameters.Flags = Flags;
  262. Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
  263. Parameters.Offset = Offset;
  264. Parameters.Size = Size;
  265. OsSystemCall(SystemCallPerformIo, &Parameters);
  266. *BytesCompleted = Parameters.Size;
  267. return Parameters.Status;
  268. }
  269. OS_API
  270. KSTATUS
  271. OsPerformVectoredIo (
  272. HANDLE Handle,
  273. IO_OFFSET Offset,
  274. UINTN Size,
  275. ULONG Flags,
  276. ULONG TimeoutInMilliseconds,
  277. PIO_VECTOR VectorArray,
  278. UINTN VectorCount,
  279. PUINTN BytesCompleted
  280. )
  281. /*++
  282. Routine Description:
  283. This routine performs I/O on an open handle.
  284. Arguments:
  285. Handle - Supplies a pointer to the opened I/O handle.
  286. Offset - Supplies the offset into the file to read from or write to. Set
  287. this to IO_OFFSET_NONE to do I/O at the current file position or for
  288. handles that are not seekable.
  289. Size - Supplies the number of bytes to transfer.
  290. Flags - Supplies a bitfield of flags. See SYS_IO_FLAG_* definitions.
  291. TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
  292. operation should be waited on before timing out. Use
  293. SYS_WAIT_TIME_INDEFINITE to wait forever on the I/O.
  294. VectorArray - Supplies an array of I/O vector structures to do I/O to/from.
  295. VectorCount - Supplies the number of elements in the vector array.
  296. BytesCompleted - Supplies a pointer where the number of bytes completed
  297. will be returned.
  298. Return Value:
  299. Status code.
  300. --*/
  301. {
  302. SYSTEM_CALL_PERFORM_VECTORED_IO Parameters;
  303. Parameters.Handle = Handle;
  304. Parameters.Flags = Flags;
  305. Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
  306. Parameters.Offset = Offset;
  307. Parameters.Size = Size;
  308. Parameters.VectorArray = VectorArray;
  309. Parameters.VectorCount = VectorCount;
  310. OsSystemCall(SystemCallPerformVectoredIo, &Parameters);
  311. *BytesCompleted = Parameters.Size;
  312. return Parameters.Status;
  313. }
  314. OS_API
  315. KSTATUS
  316. OsFlush (
  317. HANDLE Handle,
  318. ULONG Flags
  319. )
  320. /*++
  321. Routine Description:
  322. This routine flushes handle data to its backing device. If the flags
  323. specify that all data is to be flushed, then a handle is not required.
  324. Arguments:
  325. Handle - Supplies an open I/O handle. This parameter is not required if
  326. SYS_FLUSH_FLAG_ALL is set.
  327. Flags - Supplies a bitfield of flags. See SYS_FLUSH_FLAG_* definitions.
  328. Return Value:
  329. Status code.
  330. --*/
  331. {
  332. SYSTEM_CALL_FLUSH Parameters;
  333. Parameters.Handle = Handle;
  334. Parameters.Flags = Flags;
  335. OsSystemCall(SystemCallFlush, &Parameters);
  336. return Parameters.Status;
  337. }
  338. OS_API
  339. KSTATUS
  340. OsCreatePipe (
  341. HANDLE Directory,
  342. PSTR Path,
  343. ULONG PathLength,
  344. ULONG Flags,
  345. FILE_PERMISSIONS Permissions,
  346. PHANDLE ReadHandle,
  347. PHANDLE WriteHandle
  348. )
  349. /*++
  350. Routine Description:
  351. This routine creates a pipe.
  352. Arguments:
  353. Directory - Supplies an optional handle to a directory to start the
  354. search from if the supplied path is relative. Supply INVALID_HANDLE
  355. here to to use the current directory for relative paths.
  356. Path - Supplies an optional pointer to the path to create the pipe at.
  357. PathLength - Supplies the length of the path buffer in bytes, including
  358. the null terminator character.
  359. Flags - Supplies a bitfield of flags governing the behavior of the new pipe
  360. descriptors. Only SYS_OPEN_FLAG_CLOSE_ON_EXECUTE and
  361. SYS_OPEN_FLAG_NON_BLOCKING are permitted.
  362. Permissions - Supplies the initial permissions to set on the pipe.
  363. ReadHandle - Supplies a pointer where the handle to the read end of the
  364. pipe will be returned on success. Handles are only returned if a
  365. NULL path was passed in.
  366. WriteHandle - Supplies a pointer where the handle to the write end of the
  367. pipe will be returned on success. Handles are only returned in a NULL
  368. path was passed in.
  369. Return Value:
  370. Status code.
  371. --*/
  372. {
  373. SYSTEM_CALL_CREATE_PIPE Parameters;
  374. Parameters.Directory = Directory;
  375. Parameters.Path = Path;
  376. Parameters.PathLength = PathLength;
  377. Parameters.OpenFlags = Flags;
  378. Parameters.Permissions = Permissions;
  379. OsSystemCall(SystemCallCreatePipe, &Parameters);
  380. if (Path == NULL) {
  381. *ReadHandle = Parameters.ReadHandle;
  382. *WriteHandle = Parameters.WriteHandle;
  383. }
  384. return Parameters.Status;
  385. }
  386. OS_API
  387. VOID
  388. OsExitThread (
  389. PVOID UnmapAddress,
  390. UINTN UnmapSize
  391. )
  392. /*++
  393. Routine Description:
  394. This routine terminates the current thread, and optionally attempts to
  395. unmap a region of memory on its way out. Usually this is the stack of the
  396. thread that is exiting.
  397. Arguments:
  398. UnmapAddress - Supplies an optional pointer to a region of memory to unmap
  399. as the thread exits. Supply NULL to skip unmapping.
  400. UnmapSize - Supplies the size of the region to unmap in bytes. This must be
  401. aligned to the page size. If it is not, the unmap simply won't happen.
  402. Supply 0 to skip the unmap and just exit the thread. If -1 is supplied,
  403. this routine returns. This value can be used to warm up the PLT entry,
  404. since lazy binding cannot take place after the thread's control block
  405. has been destroyed.
  406. Return Value:
  407. This routine does not return, unless the magic size is passed in.
  408. --*/
  409. {
  410. if (UnmapSize == -1UL) {
  411. return;
  412. }
  413. OspExitThread(UnmapAddress, UnmapSize);
  414. }
  415. OS_API
  416. KSTATUS
  417. OsCreateThread (
  418. PSTR ThreadName,
  419. ULONG ThreadNameBufferLength,
  420. PTHREAD_ENTRY_ROUTINE ThreadRoutine,
  421. PVOID Parameter,
  422. PVOID StackBase,
  423. ULONG StackSize,
  424. PVOID ThreadPointer,
  425. PTHREAD_ID ThreadId
  426. )
  427. /*++
  428. Routine Description:
  429. This routine creates a new thread.
  430. Arguments:
  431. ThreadName - Supplies an optional pointer to the thread name.
  432. ThreadNameBufferLength - Supplies the size of the thread name buffer,
  433. including the null terminator.
  434. ThreadRoutine - Supplies a pointer to the funtion that should be run on the
  435. new thread.
  436. Parameter - Supplies a pointer that will be passed directly to the thread
  437. routine.
  438. StackBase - Supplies an optional pointer to the stack base address. If
  439. supplied, the kernel will not add a guard region or automatically
  440. expand the stack.
  441. StackSize - Supplies the size of the new thread's stack. Supply 0 to use
  442. the system default.
  443. ThreadPointer - Supplies the thread pointer to set for the new thread.
  444. ThreadId - Supplies an optional pointer where the ID of the new thread will
  445. be returned on success.
  446. Return Value:
  447. Status code.
  448. --*/
  449. {
  450. SYSTEM_CALL_CREATE_THREAD Parameters;
  451. Parameters.Name = ThreadName;
  452. Parameters.NameBufferLength = ThreadNameBufferLength;
  453. Parameters.ThreadRoutine = ThreadRoutine;
  454. Parameters.Parameter = Parameter;
  455. Parameters.StackBase = StackBase;
  456. Parameters.StackSize = StackSize;
  457. Parameters.ThreadPointer = ThreadPointer;
  458. Parameters.ThreadId = ThreadId;
  459. OsSystemCall(SystemCallCreateThread, &Parameters);
  460. return Parameters.Status;
  461. }
  462. OS_API
  463. KSTATUS
  464. OsForkProcess (
  465. PPROCESS_ID NewProcessId
  466. )
  467. /*++
  468. Routine Description:
  469. This routine forks the current process into two separate processes. The
  470. child process begins executing in the middle of this function.
  471. Arguments:
  472. NewProcessId - Supplies a pointer that on success contains the process ID
  473. of the child process in the parent, and 0 in the child. This value
  474. contains -1 if the new process failed to spawn.
  475. Return Value:
  476. STATUS_SUCCESS in both the parent and child on success.
  477. Other status codes are returned to the parent if the child failed to spawn.
  478. --*/
  479. {
  480. SYSTEM_CALL_FORK_PROCESS Parameters;
  481. //
  482. // If the call is successful, the child's parameter structure will not be
  483. // modified. Set the parameters up with the return values for the child
  484. // before making the call so that when the child returns, it looks like the
  485. // kernel filled those values in.
  486. //
  487. Parameters.Status = STATUS_SUCCESS;
  488. Parameters.ProcessId = 0;
  489. OspSystemCallFull(SystemCallForkProcess, &Parameters);
  490. *NewProcessId = Parameters.ProcessId;
  491. return Parameters.Status;
  492. }
  493. OS_API
  494. KSTATUS
  495. OsExecuteImage (
  496. PPROCESS_ENVIRONMENT Environment
  497. )
  498. /*++
  499. Routine Description:
  500. This routine replaces the currently running process with the given binary
  501. image.
  502. Arguments:
  503. Environment - Supplies a pointer to the environment to execute, which
  504. includes the image name, parameters, and environment variables.
  505. Return Value:
  506. If this routine succeeds, it will not return, as the process will be
  507. replaced with the new executable. If the process could not be started,
  508. a failing status code will be returned to the caller.
  509. --*/
  510. {
  511. SYSTEM_CALL_EXECUTE_IMAGE Parameters;
  512. RtlCopyMemory(&(Parameters.Environment),
  513. Environment,
  514. sizeof(PROCESS_ENVIRONMENT));
  515. return OspSystemCallFull(SystemCallExecuteImage, &Parameters);
  516. }
  517. OS_API
  518. KSTATUS
  519. OsGetSystemVersion (
  520. PSYSTEM_VERSION_INFORMATION VersionInformation,
  521. BOOL WantStrings
  522. )
  523. /*++
  524. Routine Description:
  525. This routine gets the system version information.
  526. Arguments:
  527. VersionInformation - Supplies a pointer where the system version
  528. information will be returned. The caller should not attempt to modify
  529. or free the strings pointed to by members of this structure.
  530. WantStrings - Supplies a boolean indicating if the build strings are
  531. desired or just the major/minor version information.
  532. Return Value:
  533. Status code.
  534. --*/
  535. {
  536. UINTN BufferSize;
  537. ULONG EncodedVersion;
  538. PVOID HeapAllocation;
  539. PSYSTEM_VERSION_INFORMATION LocalInformation;
  540. KSTATUS Status;
  541. PUSER_SHARED_DATA UserSharedData;
  542. Status = STATUS_SUCCESS;
  543. //
  544. // Get the build strings from the kernel if needed.
  545. //
  546. if ((WantStrings != FALSE) && (OsProductName == NULL)) {
  547. BufferSize = 0;
  548. Status = OsGetSetSystemInformation(SystemInformationKe,
  549. KeInformationSystemVersion,
  550. NULL,
  551. &BufferSize,
  552. FALSE);
  553. ASSERT(Status == STATUS_BUFFER_TOO_SMALL);
  554. OsSystemVersionStringsSize = BufferSize;
  555. HeapAllocation = OsHeapAllocate(BufferSize, OS_VERSION_ALLOCATION_TAG);
  556. if (HeapAllocation == NULL) {
  557. Status = STATUS_INSUFFICIENT_RESOURCES;
  558. goto GetSystemVersionEnd;
  559. }
  560. Status = OsGetSetSystemInformation(SystemInformationKe,
  561. KeInformationSystemVersion,
  562. HeapAllocation,
  563. &BufferSize,
  564. FALSE);
  565. if (!KSUCCESS(Status)) {
  566. goto GetSystemVersionEnd;
  567. }
  568. LocalInformation = HeapAllocation;
  569. if (LocalInformation->BuildString != NULL) {
  570. LocalInformation->BuildString =
  571. (PVOID)((UINTN)LocalInformation +
  572. (UINTN)(LocalInformation->BuildString));
  573. }
  574. if (LocalInformation->ProductName != NULL) {
  575. LocalInformation->ProductName =
  576. (PVOID)((UINTN)LocalInformation +
  577. (UINTN)(LocalInformation->ProductName));
  578. }
  579. OsBuildString = LocalInformation->BuildString;
  580. OsProductName = LocalInformation->ProductName;
  581. }
  582. //
  583. // Fill in the caller's structure.
  584. //
  585. UserSharedData = OspGetUserSharedData();
  586. EncodedVersion = UserSharedData->EncodedSystemVersion;
  587. VersionInformation->MajorVersion = DECODE_MAJOR_VERSION(EncodedVersion);
  588. VersionInformation->MinorVersion = DECODE_MINOR_VERSION(EncodedVersion);
  589. VersionInformation->Revision = DECODE_VERSION_REVISION(EncodedVersion);
  590. VersionInformation->SerialVersion = UserSharedData->SystemVersionSerial;
  591. VersionInformation->ReleaseLevel = DECODE_VERSION_RELEASE(EncodedVersion);
  592. VersionInformation->DebugLevel = DECODE_VERSION_DEBUG(EncodedVersion);
  593. VersionInformation->BuildTime.Seconds = UserSharedData->BuildTime;
  594. VersionInformation->BuildTime.Nanoseconds = 0;
  595. VersionInformation->ProductName = NULL;
  596. VersionInformation->BuildString = NULL;
  597. //
  598. // Copy the strings as well if requested.
  599. //
  600. if (WantStrings != FALSE) {
  601. VersionInformation->ProductName = OsProductName;
  602. VersionInformation->BuildString = OsBuildString;
  603. }
  604. Status = STATUS_SUCCESS;
  605. GetSystemVersionEnd:
  606. return Status;
  607. }
  608. OS_API
  609. KSTATUS
  610. OsGetCurrentDirectory (
  611. BOOL Root,
  612. PSTR *Buffer,
  613. PUINTN BufferSize
  614. )
  615. /*++
  616. Routine Description:
  617. This routine retrieves a pointer to a null terminated string containing the
  618. path to the current working directory or the current root directory.
  619. Arguments:
  620. Root - Supplies a boolean indicating whether caller would like the current
  621. working directory (FALSE) or the path to the current root directory
  622. (TRUE). If the caller does not have permission to escape roots, or
  623. does not currently have an altered root directory, then / is returned.
  624. Buffer - Supplies a pointer that receives a pointer to a buffer that
  625. contains a null terminated string for the path to the current
  626. directory.
  627. BufferSize - Supplies a pointer that receives the size of the buffer, in
  628. bytes.
  629. Return Value:
  630. Status code.
  631. --*/
  632. {
  633. PSTR CurrentDirectory;
  634. UINTN CurrentDirectorySize;
  635. ULONG Index;
  636. SYSTEM_CALL_GET_CURRENT_DIRECTORY Parameters;
  637. KSTATUS Status;
  638. CurrentDirectorySize = OS_GET_CURRENT_DIRECTORY_BUFFER_SIZE_GUESS;
  639. for (Index = 0; Index < OS_GET_CURRENT_DIRECTORY_TRY_COUNT; Index += 1) {
  640. CurrentDirectory = OsHeapAllocate(CurrentDirectorySize,
  641. OS_CURRENT_DIRECTORY_ALLOCATION_TAG);
  642. if (CurrentDirectory == NULL) {
  643. Status = STATUS_INSUFFICIENT_RESOURCES;
  644. goto GetCurrentDirectoryEnd;
  645. }
  646. Parameters.Root = Root;
  647. Parameters.Buffer = CurrentDirectory;
  648. Parameters.BufferSize = CurrentDirectorySize;
  649. OsSystemCall(SystemCallGetCurrentDirectory, &Parameters);
  650. //
  651. // Exit on any status besides a buffer too small result.
  652. //
  653. if (Parameters.Status != STATUS_BUFFER_TOO_SMALL) {
  654. break;
  655. }
  656. //
  657. // If the buffer is too small. Double the expected size just in case
  658. // another thread changes directories.
  659. //
  660. CurrentDirectorySize = Parameters.BufferSize * 2;
  661. OsHeapFree(CurrentDirectory);
  662. }
  663. Status = Parameters.Status;
  664. GetCurrentDirectoryEnd:
  665. if (KSUCCESS(Status)) {
  666. *Buffer = Parameters.Buffer;
  667. *BufferSize = Parameters.BufferSize;
  668. } else {
  669. OsHeapFree(CurrentDirectory);
  670. }
  671. return Parameters.Status;
  672. }
  673. OS_API
  674. KSTATUS
  675. OsChangeDirectory (
  676. BOOL Root,
  677. PSTR Path,
  678. ULONG PathSize
  679. )
  680. /*++
  681. Routine Description:
  682. This routine sets the current working directory or current root directory.
  683. Arguments:
  684. Root - Supplies a boolean indicating whether to change the current working
  685. directory (FALSE) or the current root directory (TRUE). If attempting
  686. to change the root, the caller must have permission to change the root,
  687. must be running a single thread, and must not have any other handles
  688. to directories open.
  689. Path - Supplies a pointer to the path of the new working directory. If
  690. trying to escape the root, supply NULL here. The caller must have
  691. sufficient privileges to escape a root.
  692. PathSize - Supplies the size of the path directory string in bytes
  693. including the null terminator.
  694. Return Value:
  695. Status code.
  696. --*/
  697. {
  698. SYSTEM_CALL_CHANGE_DIRECTORY Parameters;
  699. Parameters.Root = Root;
  700. Parameters.Buffer = Path;
  701. Parameters.BufferLength = PathSize;
  702. Parameters.Handle = INVALID_HANDLE;
  703. OsSystemCall(SystemCallChangeDirectory, &Parameters);
  704. return Parameters.Status;
  705. }
  706. OS_API
  707. KSTATUS
  708. OsChangeDirectoryHandle (
  709. BOOL Root,
  710. HANDLE Handle
  711. )
  712. /*++
  713. Routine Description:
  714. This routine sets the current working directory or root directory to the
  715. same directory opened with the given file handle.
  716. Arguments:
  717. Root - Supplies a boolean indicating whether to change the current working
  718. directory (FALSE) or the current root directory (TRUE). If attempting
  719. to change the root, the caller must have permission to change the root,
  720. must be running a single thread, and must not have any other handles
  721. to directories open.
  722. Handle - Supplies an open handle to a directory to change the current
  723. working directroy to. Supply INVALID_HANDLE here to escape the root.
  724. The caller must have sufficient privileges to escape a root.
  725. Return Value:
  726. Status code.
  727. --*/
  728. {
  729. SYSTEM_CALL_CHANGE_DIRECTORY Parameters;
  730. Parameters.Root = Root;
  731. Parameters.Buffer = NULL;
  732. Parameters.BufferLength = 0;
  733. Parameters.Handle = Handle;
  734. OsSystemCall(SystemCallChangeDirectory, &Parameters);
  735. return Parameters.Status;
  736. }
  737. OS_API
  738. KSTATUS
  739. OsPoll (
  740. PSIGNAL_SET SignalMask,
  741. PPOLL_DESCRIPTOR Descriptors,
  742. ULONG DescriptorCount,
  743. ULONG TimeoutInMilliseconds,
  744. PULONG DescriptorsSelected
  745. )
  746. /*++
  747. Routine Description:
  748. This routine polls several I/O handles.
  749. Arguments:
  750. SignalMask - Supplies an optional pointer to a mask to set for the
  751. duration of the wait.
  752. Descriptors - Supplies a pointer to an array of poll descriptor structures
  753. describing the descriptors and events to wait on.
  754. DescriptorCount - Supplies the number of descriptors in the array.
  755. TimeoutInMilliseconds - Supplies the number of milliseconds to wait before
  756. giving up.
  757. DescriptorsSelected - Supplies a pointer where the number of descriptors
  758. that had activity will be returned on success.
  759. Return Value:
  760. STATUS_SUCCESS if one or more descriptors is ready for action.
  761. STATUS_INSUFFICIENT_RESOURCES if memory could not be allocated for the
  762. operation.
  763. STATUS_INTERRUPTED if a signal was caught during the wait.
  764. STATUS_TIMEOUT if no descriptors were ready in the given amount of time.
  765. --*/
  766. {
  767. SYSTEM_CALL_POLL Poll;
  768. Poll.SignalMask = SignalMask;
  769. Poll.Descriptors = Descriptors;
  770. Poll.DescriptorCount = DescriptorCount;
  771. Poll.TimeoutInMilliseconds = TimeoutInMilliseconds;
  772. OsSystemCall(SystemCallPoll, &Poll);
  773. *DescriptorsSelected = Poll.DescriptorsSelected;
  774. return Poll.Status;
  775. }
  776. OS_API
  777. PSIGNAL_HANDLER_ROUTINE
  778. OsSetSignalHandler (
  779. PSIGNAL_HANDLER_ROUTINE NewHandler
  780. )
  781. /*++
  782. Routine Description:
  783. This routine sets the signal handler routine called whenever a signal is
  784. delivered by the kernel.
  785. Arguments:
  786. NewHandler - Supplies a pointer to the new handler routine to use.
  787. Return Value:
  788. Returns a pointer to the old handler, or NULL if no other signal handlers
  789. were registered.
  790. --*/
  791. {
  792. PSIGNAL_HANDLER_ROUTINE OldHandler;
  793. OldHandler = (PVOID)RtlAtomicExchange((PVOID)&OsSignalHandler,
  794. (UINTN)NewHandler);
  795. return OldHandler;
  796. }
  797. OS_API
  798. KSTATUS
  799. OsSendSignal (
  800. SIGNAL_TARGET_TYPE TargetType,
  801. ULONG TargetId,
  802. ULONG SignalNumber,
  803. SHORT SignalCode,
  804. UINTN SignalParameter
  805. )
  806. /*++
  807. Routine Description:
  808. This routine sends a signal to a process, process group or thread.
  809. Arguments:
  810. TargetType - Supplies the target type to which the signal is being sent. It
  811. can be either a process, process group, or thread.
  812. TargetId - Supplies the ID for the signal's target process, process group,
  813. or thread.
  814. SignalNumber - Supplies the signal number to send.
  815. SignalCode - Supplies the signal code to send. See SIGNAL_CODE_*
  816. definitions.
  817. SignalParameter - Supplies a parameter to send with the signal if the
  818. signal is in the real time signal range.
  819. Return Value:
  820. Status code.
  821. --*/
  822. {
  823. SYSTEM_CALL_SEND_SIGNAL SendSignal;
  824. SendSignal.TargetType = TargetType;
  825. SendSignal.TargetId = TargetId;
  826. SendSignal.SignalNumber = SignalNumber;
  827. SendSignal.SignalCode = SignalCode;
  828. SendSignal.SignalParameter = SignalParameter;
  829. //
  830. // Use the full system call because it makes debugging easier when
  831. // something like an abort gets forwarded to the kernel mode debugger.
  832. //
  833. OspSystemCallFull(SystemCallSendSignal, &SendSignal);
  834. return SendSignal.Status;
  835. }
  836. OS_API
  837. KSTATUS
  838. OsGetProcessId (
  839. PROCESS_ID_TYPE ProcessIdType,
  840. PPROCESS_ID ProcessId
  841. )
  842. /*++
  843. Routine Description:
  844. This routine gets an identifier associated with the process, such as the
  845. process ID, thread ID, parent process ID, process group ID, and session ID.
  846. Arguments:
  847. ProcessIdType - Supplies the type of ID to get.
  848. ProcessId - Supplies a pointer that on input contains the process ID
  849. argument if the operation takes a parameter. On successful output,
  850. returns the desired ID. Supply zero to use the calling process ID.
  851. Return Value:
  852. Status code.
  853. --*/
  854. {
  855. SYSTEM_CALL_GET_SET_PROCESS_ID Parameters;
  856. //
  857. // TODO: These values should be read once and cached, which means changing
  858. // the system call interface and then invalidating on fork.
  859. //
  860. Parameters.ProcessIdType = ProcessIdType;
  861. Parameters.ProcessId = *ProcessId;
  862. Parameters.NewValue = 0;
  863. Parameters.Set = FALSE;
  864. OsSystemCall(SystemCallGetSetProcessId, &Parameters);
  865. *ProcessId = Parameters.ProcessId;
  866. return Parameters.Status;
  867. }
  868. OS_API
  869. KSTATUS
  870. OsSetProcessId (
  871. PROCESS_ID_TYPE ProcessIdType,
  872. PROCESS_ID ProcessId,
  873. PROCESS_ID NewValue
  874. )
  875. /*++
  876. Routine Description:
  877. This routine sets an identifier associated with the process, such as the
  878. process group ID or session ID.
  879. Arguments:
  880. ProcessIdType - Supplies the type of ID to set. Not all types can be set.
  881. ProcessId - Supplies the ID of the process to change. Supply 0 to use the
  882. current process.
  883. NewValue - Supplies the new value to set.
  884. Return Value:
  885. Status code.
  886. --*/
  887. {
  888. SYSTEM_CALL_GET_SET_PROCESS_ID Parameters;
  889. Parameters.ProcessIdType = ProcessIdType;
  890. Parameters.ProcessId = ProcessId;
  891. Parameters.NewValue = NewValue;
  892. Parameters.Set = TRUE;
  893. OsSystemCall(SystemCallGetSetProcessId, &Parameters);
  894. return Parameters.Status;
  895. }
  896. OS_API
  897. SIGNAL_SET
  898. OsSetSignalBehavior (
  899. SIGNAL_MASK_TYPE MaskType,
  900. SIGNAL_MASK_OPERATION Operation,
  901. PSIGNAL_SET NewMask
  902. )
  903. /*++
  904. Routine Description:
  905. This routine sets signal behavior, either for the current thread in the
  906. case of the blocked signals, or for the process for other signal masks.
  907. Arguments:
  908. MaskType - Supplies the type of mask to change.
  909. Operation - Supplies the interaction between the new mask and the previous
  910. signal mask.
  911. NewMask - Supplies a pointer to the mask of signals that are affected by
  912. this operation.
  913. Return Value:
  914. Returns the original signal mask before this function was called.
  915. --*/
  916. {
  917. SYSTEM_CALL_SET_SIGNAL_BEHAVIOR SetSignalBehavior;
  918. SetSignalBehavior.MaskType = MaskType;
  919. SetSignalBehavior.Operation = Operation;
  920. if (NewMask != NULL) {
  921. SetSignalBehavior.SignalSet = *NewMask;
  922. } else {
  923. INITIALIZE_SIGNAL_SET(SetSignalBehavior.SignalSet);
  924. }
  925. OsSystemCall(SystemCallSetSignalBehavior, &SetSignalBehavior);
  926. return SetSignalBehavior.SignalSet;
  927. }
  928. OS_API
  929. KSTATUS
  930. OsWaitForChildProcess (
  931. ULONG Flags,
  932. PPROCESS_ID ChildPid,
  933. PULONG Reason,
  934. PUINTN ChildExitValue,
  935. PRESOURCE_USAGE ChildResourceUsage
  936. )
  937. /*++
  938. Routine Description:
  939. This routine is called to suspend execution of the current thread until
  940. a child process completes.
  941. Arguments:
  942. Flags - Supplies a bitfield of flags governing the behavior of the wait.
  943. See SYSTEM_CALL_WAIT_FLAG_* definitions.
  944. ChildPid - Supplies a pointer that on input supplies the child process ID
  945. parameter. This parameter can be one of the following:
  946. If -1 is supplied, any child signal will be pulled off and returned.
  947. If a number greater than 0 is supplied, only the specific process ID
  948. will be pulled off and returned.
  949. If 0 is supplied, any child process whose process group ID is equal to
  950. that of the calling process will be pulled.
  951. If a number less than zero (but not -1) is supplied, then any process
  952. whose process group ID is equal to the absolute value of this parameter
  953. will be dequeued and returned.
  954. On output, this parameter will contain the process ID of the child that
  955. generated the signal activity, and the child signal will be discarded.
  956. If the wait for child parameter is set to FALSE, then this parameter
  957. is ignored. If a non-child signal cause the routine to return, then
  958. the value at this parameter is undefined.
  959. Reason - Supplies a pointer where the reason for the child event will be
  960. returned. See CHILD_SIGNAL_REASON_* definitions.
  961. ChildExitValue - Supplies a pointer where the child exit value (or signal
  962. that caused the event) will be returned.
  963. ChildResourceUsage - Supplies an optional pointer where the resource usage
  964. of the child will be returned on success.
  965. Return Value:
  966. STATUS_SUCCESS if the wait was successfully satisfied. If the
  967. SYSTEM_CALL_WAIT_FLAG_RETURN_IMMEDIATELY flag is set and there are no
  968. children ready to be reaped, then STATUS_SUCCESS is returned and the child
  969. PID is returned as -1.
  970. STATUS_INTERRUPTED if the wait was interrupted by a signal.
  971. STATUS_NO_ELIGIBLE_CHILDREN if no eligible children could be reaped.
  972. --*/
  973. {
  974. SYSTEM_CALL_WAIT_FOR_CHILD Parameters;
  975. Parameters.Flags = Flags;
  976. Parameters.ChildPid = -1;
  977. if (ChildPid != NULL) {
  978. Parameters.ChildPid = *ChildPid;
  979. }
  980. Parameters.ResourceUsage = ChildResourceUsage;
  981. OsSystemCall(SystemCallWaitForChildProcess, &Parameters);
  982. if (ChildPid != NULL) {
  983. *ChildPid = Parameters.ChildPid;
  984. }
  985. if (Reason != NULL) {
  986. *Reason = Parameters.Reason;
  987. }
  988. if (ChildExitValue != NULL) {
  989. *ChildExitValue = Parameters.ChildExitValue;
  990. }
  991. return Parameters.Status;
  992. }
  993. OS_API
  994. KSTATUS
  995. OsSuspendExecution (
  996. SIGNAL_MASK_OPERATION SignalOperation,
  997. PSIGNAL_SET SignalSet,
  998. PSIGNAL_PARAMETERS SignalParameters,
  999. ULONG TimeoutInMilliseconds
  1000. )
  1001. /*++
  1002. Routine Description:
  1003. This routine suspends execution of the current thread until a signal comes
  1004. in. The current thread's blocked signal mask can be changed for the
  1005. duration of the call by providing an operation and a signal set.
  1006. Arguments:
  1007. SignalOperation - Supplies the operation to perform with the signal set for
  1008. the duration of the call: set, clear, overwrite or none.
  1009. SignalSet - Supplies a pointer to the signal set to apply for the duration
  1010. of this system call as dictated by the signal operation.
  1011. SignalParameters - Supplies an optional pointer where the signal
  1012. information for the signal that occurred will be returned.
  1013. TimeoutInMilliseconds - Supplies the timeout of the operation in
  1014. milliseconds.
  1015. Return Value:
  1016. STATUS_SUCCESS if a signal arrived.
  1017. STATUS_INTERRUPTED on a clear signal operation if a signal that is not in
  1018. the given set arrived.
  1019. STATUS_TIMEOUT if no signal arrived before the given timeout expires.
  1020. STATUS_INVALID_PARAMETER if no signal set is supplied for an operation
  1021. other than SignalMaskOperationNone.
  1022. --*/
  1023. {
  1024. SYSTEM_CALL_SUSPEND_EXECUTION Parameters;
  1025. if (SignalSet == NULL) {
  1026. if (SignalOperation != SignalMaskOperationNone) {
  1027. return STATUS_INVALID_PARAMETER;
  1028. }
  1029. } else {
  1030. Parameters.SignalSet = *SignalSet;
  1031. }
  1032. Parameters.SignalOperation = SignalOperation;
  1033. Parameters.SignalParameters = SignalParameters;
  1034. Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
  1035. OsSystemCall(SystemCallSuspendExecution, &Parameters);
  1036. return Parameters.Status;
  1037. }
  1038. OS_API
  1039. NO_RETURN
  1040. VOID
  1041. OsExitProcess (
  1042. UINTN Status
  1043. )
  1044. /*++
  1045. Routine Description:
  1046. This routine terminates the current process and any threads that may be
  1047. running in it.
  1048. Arguments:
  1049. Status - Supplies the exit status, returned to the parent in the wait
  1050. calls. Conventionally 0 indicates success, and non-zero indicates
  1051. failure. The C library only recieves the first eight bits of the return
  1052. status, portable applications should not set bits beyond that.
  1053. Return Value:
  1054. This routine does not return.
  1055. --*/
  1056. {
  1057. SYSTEM_CALL_EXIT_PROCESS Parameters;
  1058. Parameters.Status = Status;
  1059. OsSystemCall(SystemCallExitProcess, &Parameters);
  1060. while (TRUE) {
  1061. ASSERT(FALSE);
  1062. }
  1063. }
  1064. OS_API
  1065. KSTATUS
  1066. OsFileControl (
  1067. HANDLE Handle,
  1068. FILE_CONTROL_COMMAND Command,
  1069. PFILE_CONTROL_PARAMETERS_UNION Parameters
  1070. )
  1071. /*++
  1072. Routine Description:
  1073. This routine performs a file control operation on the given handle.
  1074. Arguments:
  1075. Handle - Supplies the file handle to operate on.
  1076. Command - Supplies the command to perform.
  1077. Parameters - Supplies an optional pointer to any additional parameters
  1078. needed by the command.
  1079. Return Value:
  1080. Status code.
  1081. --*/
  1082. {
  1083. SYSTEM_CALL_FILE_CONTROL FileControl;
  1084. FileControl.File = Handle;
  1085. FileControl.Command = Command;
  1086. FileControl.Parameters = Parameters;
  1087. OsSystemCall(SystemCallFileControl, &FileControl);
  1088. return FileControl.Status;
  1089. }
  1090. OS_API
  1091. KSTATUS
  1092. OsGetFileInformation (
  1093. HANDLE Directory,
  1094. PSTR Path,
  1095. ULONG PathLength,
  1096. BOOL FollowLink,
  1097. PFILE_PROPERTIES Properties
  1098. )
  1099. /*++
  1100. Routine Description:
  1101. This routine gets the file properties for a given file.
  1102. Arguments:
  1103. Directory - Supplies an optional handle to a directory to start the
  1104. search from if the supplied path is relative. Supply INVALID_HANDLE
  1105. here to to use the current directory for relative paths.
  1106. Path - Supplies a pointer to the string containing the file path to get
  1107. properties for.
  1108. PathLength - Supplies the length of the path string in bytes including the
  1109. null terminator.
  1110. FollowLink - Supplies a boolean indicating what to do if the file path
  1111. points to a symbolic link. If set to TRUE, the file information set or
  1112. returned will be for the file the link points to. If FALSE, the call
  1113. will set or get information for the link itself.
  1114. Properties - Supplies a pointer where the file properties will be returned
  1115. on success.
  1116. Return Value:
  1117. Status code.
  1118. --*/
  1119. {
  1120. SET_FILE_INFORMATION Request;
  1121. KSTATUS Status;
  1122. Request.FieldsToSet = 0;
  1123. Status = OspGetSetFileInformation(Directory,
  1124. Path,
  1125. PathLength,
  1126. FollowLink,
  1127. &Request);
  1128. if (KSUCCESS(Status)) {
  1129. RtlCopyMemory(Properties,
  1130. &(Request.FileProperties),
  1131. sizeof(FILE_PROPERTIES));
  1132. }
  1133. return Status;
  1134. }
  1135. OS_API
  1136. KSTATUS
  1137. OsSetFileInformation (
  1138. HANDLE Directory,
  1139. PSTR Path,
  1140. ULONG PathLength,
  1141. BOOL FollowLink,
  1142. PSET_FILE_INFORMATION Request
  1143. )
  1144. /*++
  1145. Routine Description:
  1146. This routine sets the file properties for a given file.
  1147. Arguments:
  1148. Directory - Supplies an optional handle to a directory to start the
  1149. search from if the supplied path is relative. Supply INVALID_HANDLE
  1150. here to to use the current directory for relative paths.
  1151. Path - Supplies a pointer to the string containing the file path to set
  1152. properties for.
  1153. PathLength - Supplies the length of the path string in bytes including the
  1154. null terminator.
  1155. FollowLink - Supplies a boolean indicating what to do if the file path
  1156. points to a symbolic link. If set to TRUE, the file information set or
  1157. returned will be for the file the link points to. If FALSE, the call
  1158. will set or get information for the link itself.
  1159. Request - Supplies a pointer to the set file information request.
  1160. Return Value:
  1161. Status code.
  1162. --*/
  1163. {
  1164. KSTATUS Status;
  1165. Status = OspGetSetFileInformation(Directory,
  1166. Path,
  1167. PathLength,
  1168. FollowLink,
  1169. Request);
  1170. return Status;
  1171. }
  1172. OS_API
  1173. VOID
  1174. OsDebugPrint (
  1175. PSTR String,
  1176. ULONG StringSize
  1177. )
  1178. /*++
  1179. Routine Description:
  1180. This routine prints a message to the debug console. No formatting is
  1181. provided.
  1182. Arguments:
  1183. String - Supplies a pointer to the string to print.
  1184. StringSize - Supplies the size of the string in bytes including the null
  1185. terminator.
  1186. Return Value:
  1187. None.
  1188. --*/
  1189. {
  1190. OsDebug(DebugCommandPrint, 0, NULL, String, StringSize, 0);
  1191. return;
  1192. }
  1193. OS_API
  1194. KSTATUS
  1195. OsDebug (
  1196. DEBUG_COMMAND_TYPE Command,
  1197. PROCESS_ID Process,
  1198. PVOID Address,
  1199. PVOID Data,
  1200. ULONG Size,
  1201. ULONG SignalToDeliver
  1202. )
  1203. /*++
  1204. Routine Description:
  1205. This routine sends a debug command to a process.
  1206. Arguments:
  1207. Command - Supplies the command to send.
  1208. Process - Supplies the process ID to send the command to.
  1209. Address - Supplies the address parameter of the command, usually the
  1210. address in the target to read from or write to.
  1211. Data - Supplies the data parameter, usually the buffer containing the data
  1212. to write or the buffer where the read data will be returned.
  1213. Size - Supplies the size of the buffer, in bytes.
  1214. SignalToDeliver - Supplies the signal number to deliver to the debugged
  1215. process for step and continue commands. For other commands, this
  1216. parameter is ignored.
  1217. Return Value:
  1218. Status code.
  1219. --*/
  1220. {
  1221. SYSTEM_CALL_DEBUG Request;
  1222. Request.Command.Command = Command;
  1223. Request.Process = Process;
  1224. Request.Command.U.Address = Address;
  1225. Request.Command.Data = Data;
  1226. Request.Command.Size = Size;
  1227. Request.Command.SignalToDeliver = SignalToDeliver;
  1228. OsSystemCall(SystemCallDebug, &Request);
  1229. return Request.Command.Status;
  1230. }
  1231. OS_API
  1232. KSTATUS
  1233. OsSeek (
  1234. HANDLE Handle,
  1235. SEEK_COMMAND SeekCommand,
  1236. IO_OFFSET Offset,
  1237. PIO_OFFSET NewOffset
  1238. )
  1239. /*++
  1240. Routine Description:
  1241. This routine seeks to the given position in a file. This routine is only
  1242. relevant for normal file or block based devices.
  1243. Arguments:
  1244. Handle - Supplies the open file handle.
  1245. SeekCommand - Supplies the reference point for the seek offset. Usual
  1246. reference points are the beginning of the file, current file position,
  1247. and the end of the file.
  1248. Offset - Supplies the offset from the reference point to move in bytes.
  1249. NewOffset - Supplies an optional pointer where the file position after the
  1250. move will be returned on success.
  1251. Return Value:
  1252. Status code.
  1253. --*/
  1254. {
  1255. SYSTEM_CALL_SEEK Request;
  1256. Request.Handle = Handle;
  1257. Request.Command = SeekCommand;
  1258. Request.Offset = Offset;
  1259. OsSystemCall(SystemCallSeek, &Request);
  1260. if (NewOffset != NULL) {
  1261. *NewOffset = Request.Offset;
  1262. }
  1263. return Request.Status;
  1264. }
  1265. OS_API
  1266. KSTATUS
  1267. OsCreateSymbolicLink (
  1268. HANDLE Directory,
  1269. PSTR Path,
  1270. ULONG PathSize,
  1271. PSTR LinkDestinationBuffer,
  1272. ULONG LinkDestinationBufferSize
  1273. )
  1274. /*++
  1275. Routine Description:
  1276. This routine creates a symbolic link.
  1277. Arguments:
  1278. Directory - Supplies an optional handle to a directory to start the
  1279. search from if the supplied path is relative. Supply INVALID_HANDLE
  1280. here to to use the current directory for relative paths.
  1281. Path - Supplies a pointer to the symbolic link path.
  1282. PathSize - Supplies the size of the symbolic link pointer in bytes
  1283. including the null terminator.
  1284. LinkDestinationBuffer - Supplies a pointer to a string containing the link's
  1285. target path, the location the link points to.
  1286. LinkDestinationBufferSize - Supplies the size of the link destination
  1287. buffer in bytes, NOT including the null terminator.
  1288. Return Value:
  1289. Status code.
  1290. --*/
  1291. {
  1292. SYSTEM_CALL_CREATE_SYMBOLIC_LINK Parameters;
  1293. Parameters.Directory = Directory;
  1294. Parameters.Path = Path;
  1295. Parameters.PathSize = PathSize;
  1296. Parameters.LinkDestinationBuffer = LinkDestinationBuffer;
  1297. Parameters.LinkDestinationBufferSize = LinkDestinationBufferSize;
  1298. OsSystemCall(SystemCallCreateSymbolicLink, &Parameters);
  1299. return Parameters.Status;
  1300. }
  1301. OS_API
  1302. KSTATUS
  1303. OsReadSymbolicLink (
  1304. HANDLE Directory,
  1305. PSTR Path,
  1306. ULONG PathSize,
  1307. PSTR LinkDestinationBuffer,
  1308. ULONG LinkDestinationBufferSize,
  1309. PULONG LinkDestinationSize
  1310. )
  1311. /*++
  1312. Routine Description:
  1313. This routine reads the destination path of a symbolic link.
  1314. Arguments:
  1315. Directory - Supplies an optional handle to a directory to start the
  1316. search from if the supplied path is relative. Supply INVALID_HANDLE
  1317. here to to use the current directory for relative paths.
  1318. Path - Supplies a pointer to the symbolic link path.
  1319. PathSize - Supplies the size of the symbolic link pointer in bytes
  1320. including the null terminator.
  1321. LinkDestinationBuffer - Supplies a pointer to a buffer where the
  1322. destination of the link will be returned. A null terminator byte is not
  1323. written.
  1324. LinkDestinationBufferSize - Supplies the size of the link destination
  1325. buffer in bytes.
  1326. LinkDestinationSize - Supplies a pointer where the actual size of the
  1327. link destination (including the null terminator) will be returned on
  1328. either success or a STATUS_BUFFER_TOO_SMALL case. On failure, 0 will be
  1329. returned here.
  1330. Return Value:
  1331. STATUS_SUCCESS on success.
  1332. STATUS_BUFFER_TOO_SMALL if the link destination buffer was not large
  1333. enough to store the link destination.
  1334. Other errors on other failures.
  1335. --*/
  1336. {
  1337. SYSTEM_CALL_READ_SYMBOLIC_LINK Parameters;
  1338. Parameters.Directory = Directory;
  1339. Parameters.Path = Path;
  1340. Parameters.PathSize = PathSize;
  1341. Parameters.LinkDestinationBuffer = LinkDestinationBuffer;
  1342. Parameters.LinkDestinationBufferSize = LinkDestinationBufferSize;
  1343. OsSystemCall(SystemCallReadSymbolicLink, &Parameters);
  1344. *LinkDestinationSize = Parameters.LinkDestinationSize;
  1345. return Parameters.Status;
  1346. }
  1347. OS_API
  1348. KSTATUS
  1349. OsCreateHardLink (
  1350. HANDLE ExistingFileDirectory,
  1351. PSTR ExistingFile,
  1352. ULONG ExistingFileSize,
  1353. HANDLE LinkDirectory,
  1354. PSTR LinkPath,
  1355. ULONG LinkPathSize,
  1356. BOOL FollowExistingFileLinks
  1357. )
  1358. /*++
  1359. Routine Description:
  1360. This routine creates a hard link.
  1361. Arguments:
  1362. ExistingFileDirectory - Supplies an optional handle to the directory to
  1363. start path traversal from if the specified existing file path is
  1364. relative. Supply INVALID_HANDLE here to use the current directory for
  1365. relative paths.
  1366. ExistingFile - Supplies a pointer to the path of the existing file to
  1367. create the link from.
  1368. ExistingFileSize - Supplies the size of the existing file path buffer in
  1369. bytes, including the null terminator.
  1370. LinkDirectory - Supplies an optional handle to the directory to start path
  1371. traversal from if the specified new link path is relative. Supply
  1372. INVALID_HANDLE here to use the current directory for relative paths.
  1373. LinkPath - Supplies a pointer to a string containing the destination path
  1374. of the new link.
  1375. LinkPathSize - Supplies the size of the link path buffer in bytes.
  1376. FollowExistingFileLinks - Supplies a boolean indicating that if the
  1377. existing file path exists and is a symbolic link, the new link shall be
  1378. for the target of that link (TRUE) rather than the link itself (FALSE).
  1379. Return Value:
  1380. Status code.
  1381. --*/
  1382. {
  1383. SYSTEM_CALL_CREATE_HARD_LINK Parameters;
  1384. Parameters.ExistingFileDirectory = ExistingFileDirectory;
  1385. Parameters.ExistingFilePath = ExistingFile;
  1386. Parameters.ExistingFilePathSize = ExistingFileSize;
  1387. Parameters.NewLinkDirectory = LinkDirectory;
  1388. Parameters.NewLinkPath = LinkPath;
  1389. Parameters.NewLinkPathSize = LinkPathSize;
  1390. Parameters.FollowLinks = FollowExistingFileLinks;
  1391. OsSystemCall(SystemCallCreateHardLink, &Parameters);
  1392. return Parameters.Status;
  1393. }
  1394. OS_API
  1395. KSTATUS
  1396. OsDelete (
  1397. HANDLE Directory,
  1398. PSTR Path,
  1399. ULONG PathSize,
  1400. ULONG Flags
  1401. )
  1402. /*++
  1403. Routine Description:
  1404. This routine attempts to delete the object at the given path. If the path
  1405. points to a directory, the directory must be empty. If the path points to
  1406. a file, the hard link count on the file is decremented. If the hard link
  1407. count reaches zero and no processes have the file open, the contents of the
  1408. file are destroyed. If processes have open handles to the file, the
  1409. destruction of the file contents are deferred until the last handle to the
  1410. old file is closed. If the path points to a symbolic link, the link itself
  1411. is removed and not the destination. The removal of the entry from the
  1412. directory is immediate.
  1413. Arguments:
  1414. Directory - Supplies an optional handle to a directory to start the
  1415. search from if the supplied path is relative. Supply INVALID_HANDLE
  1416. here to to use the current directory for relative paths.
  1417. Path - Supplies a pointer to the path to delete.
  1418. PathSize - Supplies the length of the path buffer in bytes, including the
  1419. null terminator.
  1420. Flags - Supplies flags associated with the delete operation. See
  1421. SYS_DELETE_FLAG_* definitions.
  1422. Return Value:
  1423. Status code.
  1424. --*/
  1425. {
  1426. SYSTEM_CALL_DELETE Parameters;
  1427. Parameters.Directory = Directory;
  1428. Parameters.Path = Path;
  1429. Parameters.PathSize = PathSize;
  1430. Parameters.Flags = Flags;
  1431. OsSystemCall(SystemCallDelete, &Parameters);
  1432. return Parameters.Status;
  1433. }
  1434. OS_API
  1435. KSTATUS
  1436. OsRename (
  1437. HANDLE SourceDirectory,
  1438. PSTR SourcePath,
  1439. ULONG SourcePathSize,
  1440. HANDLE DestinationDirectory,
  1441. PSTR DestinationPath,
  1442. ULONG DestinationPathSize
  1443. )
  1444. /*++
  1445. Routine Description:
  1446. This routine attempts to rename the object at the given path. This routine
  1447. operates on symbolic links themselves, not the destinations of symbolic
  1448. links. If the source and destination paths are equal, this routine will do
  1449. nothing and return successfully. If the source path is not a directory, the
  1450. destination path must not be a directory. If the destination file exists,
  1451. it will be deleted. The caller must have write access in both the old and
  1452. new directories. If the source path is a directory, the destination path
  1453. must not exist or be an empty directory. The destination path must not have
  1454. a path prefix of the source (ie it's illegal to move /my/path into
  1455. /my/path/stuff).
  1456. Arguments:
  1457. SourceDirectory - Supplies an optional handle to the directory to start
  1458. source path searches from. If the source path is absolute, this value
  1459. is ignored. If this is INVALID_HANDLE, then source path searches will
  1460. start from the current working directory.
  1461. SourcePath - Supplies a pointer to the path of the file to rename.
  1462. SourcePathSize - Supplies the length of the source path buffer in bytes,
  1463. including the null terminator.
  1464. DestinationDirectory - Supplies an optional handle to the directory to
  1465. start destination path searches from. If the destination path is
  1466. absolute, this value is ignored. If this is INVALID_HANDLE, then
  1467. destination path searches will start from the current working directory.
  1468. DestinationPath - Supplies a pointer to the path to rename the file to.
  1469. DestinationPathSize - Supplies the size of the destination path buffer in
  1470. bytes, including the null terminator.
  1471. Return Value:
  1472. Status code.
  1473. --*/
  1474. {
  1475. SYSTEM_CALL_RENAME Parameters;
  1476. Parameters.SourceDirectory = SourceDirectory;
  1477. Parameters.SourcePath = SourcePath;
  1478. Parameters.SourcePathSize = SourcePathSize;
  1479. Parameters.DestinationDirectory = DestinationDirectory;
  1480. Parameters.DestinationPath = DestinationPath;
  1481. Parameters.DestinationPathSize = DestinationPathSize;
  1482. OsSystemCall(SystemCallRename, &Parameters);
  1483. return Parameters.Status;
  1484. }
  1485. OS_API
  1486. KSTATUS
  1487. OsUserControl (
  1488. HANDLE Handle,
  1489. ULONG RequestCode,
  1490. PVOID Context,
  1491. UINTN ContextSize
  1492. )
  1493. /*++
  1494. Routine Description:
  1495. This routine sends a user I/O request to the given file/device/etc.
  1496. Arguments:
  1497. Handle - Supplies the open file handle to send the request to.
  1498. RequestCode - Supplies the request to send. For device handles, this is the
  1499. minor code of the IRP.
  1500. Context - Supplies a pointer to the context buffer allocated by the
  1501. caller for the request.
  1502. ContextSize - Supplies the size of the supplied context buffer.
  1503. Return Value:
  1504. Status code.
  1505. --*/
  1506. {
  1507. SYSTEM_CALL_USER_CONTROL Parameters;
  1508. Parameters.Handle = Handle;
  1509. Parameters.RequestCode = RequestCode;
  1510. Parameters.Context = Context;
  1511. Parameters.ContextSize = ContextSize;
  1512. OsSystemCall(SystemCallUserControl, &Parameters);
  1513. return Parameters.Status;
  1514. }
  1515. OS_API
  1516. KSTATUS
  1517. OsMount (
  1518. PSTR MountPointPath,
  1519. ULONG MountPointPathSize,
  1520. PSTR TargetPath,
  1521. ULONG TargetPathSize,
  1522. ULONG Flags
  1523. )
  1524. /*++
  1525. Routine Description:
  1526. This routine attempts to mount the given target at the given mount point.
  1527. Arguments:
  1528. MountPointPath - Supplies a pointer to a string containing the path to the
  1529. mount point where the target is to be mounted.
  1530. MountPointPathSize - Supplies the size of the mount point path string in
  1531. bytes, including the null terminator.
  1532. TargetPath - Supplies a pointer to a string containing the path to the
  1533. target file, directory, volume, or device that is to be mounted.
  1534. TargetPathSize - Supplies the size of the target path string in bytes,
  1535. including the null terminator.
  1536. Flags - Supplies the flags associated with the mount operation. See
  1537. SYS_MOUNT_FLAG_* for definitions.
  1538. Return Value:
  1539. Status code.
  1540. --*/
  1541. {
  1542. KSTATUS Status;
  1543. Flags &= ~SYS_MOUNT_FLAG_UNMOUNT;
  1544. Status = OspMountOrUnmount(MountPointPath,
  1545. MountPointPathSize,
  1546. TargetPath,
  1547. TargetPathSize,
  1548. Flags);
  1549. return Status;
  1550. }
  1551. OS_API
  1552. KSTATUS
  1553. OsUnmount (
  1554. PSTR MountPointPath,
  1555. ULONG MountPointPathSize,
  1556. ULONG Flags
  1557. )
  1558. /*++
  1559. Routine Description:
  1560. This routine attempts to unmount the given target. If the target is not a
  1561. mount point, or the user does not have access to the mount point, then the
  1562. routine will return the appropriate error. Otherwise, it will remove the
  1563. mount point based on the supplied flags.
  1564. Arguments:
  1565. MountPointPath - Supplies a pointer to a string containing the path to the
  1566. mount point that is to be unmounted.
  1567. MountPointPathSize - Supplies the size of the mount point path string in
  1568. bytes, including the null terminator.
  1569. Flags - Supplies the flags associated with the mount operation. See
  1570. SYS_MOUNT_FLAG_* for definitions.
  1571. Return Value:
  1572. Status code.
  1573. --*/
  1574. {
  1575. KSTATUS Status;
  1576. Flags |= SYS_MOUNT_FLAG_UNMOUNT;
  1577. Status = OspMountOrUnmount(MountPointPath,
  1578. MountPointPathSize,
  1579. NULL,
  1580. 0,
  1581. Flags);
  1582. return Status;
  1583. }
  1584. OS_API
  1585. KSTATUS
  1586. OsGetMountPoints (
  1587. PVOID *Buffer,
  1588. PUINTN BufferSize
  1589. )
  1590. /*++
  1591. Routine Description:
  1592. This routine returns the list of mount points currently in the system. It
  1593. only returns the mounts that are visible to the calling process. The caller
  1594. is responsible for releasing the buffer.
  1595. Arguments:
  1596. Buffer - Supplies a pointer that receives a buffer of mount point data.
  1597. BufferSize - Supplies a pointer that receives the size of the mount point
  1598. data.
  1599. Return Value:
  1600. Status code.
  1601. --*/
  1602. {
  1603. ULONG Index;
  1604. PVOID MountPoints;
  1605. UINTN MountPointsSize;
  1606. KSTATUS Status;
  1607. MountPointsSize = OS_GET_MOUNT_POINTS_BUFFER_SIZE_GUESS;
  1608. for (Index = 0; Index < OS_GET_MOUNT_POINTS_TRY_COUNT; Index += 1) {
  1609. MountPoints = OsHeapAllocate(MountPointsSize, OS_MOUNT_ALLOCATION_TAG);
  1610. if (MountPoints == NULL) {
  1611. Status = STATUS_INSUFFICIENT_RESOURCES;
  1612. goto GetMountPointsEnd;
  1613. }
  1614. Status = OsGetSetSystemInformation(SystemInformationIo,
  1615. IoInformationMountPoints,
  1616. MountPoints,
  1617. &MountPointsSize,
  1618. FALSE);
  1619. //
  1620. // Exit on any status besides a buffer too small result.
  1621. //
  1622. if (Status != STATUS_BUFFER_TOO_SMALL) {
  1623. break;
  1624. }
  1625. //
  1626. // If the buffer is too small. Double the expected size just in case
  1627. // something else sneaks in and try again.
  1628. //
  1629. MountPointsSize *= 2;
  1630. OsHeapFree(MountPoints);
  1631. }
  1632. GetMountPointsEnd:
  1633. if (KSUCCESS(Status)) {
  1634. *Buffer = MountPoints;
  1635. *BufferSize = MountPointsSize;
  1636. } else {
  1637. OsHeapFree(MountPoints);
  1638. }
  1639. return Status;
  1640. }
  1641. OS_API
  1642. KSTATUS
  1643. OsGetEffectiveAccess (
  1644. HANDLE Directory,
  1645. PSTR Path,
  1646. ULONG PathSize,
  1647. ULONG DesiredFlags,
  1648. BOOL UseRealIds,
  1649. PULONG EffectiveAccess
  1650. )
  1651. /*++
  1652. Routine Description:
  1653. This routine determines the effective access for the given path.
  1654. Arguments:
  1655. Directory - Supplies an optional handle to a directory to start the
  1656. search from if the supplied path is relative. Supply INVALID_HANDLE
  1657. here to to use the current directory for relative paths.
  1658. Path - Supplies a pointer to a string containing the path to the object
  1659. to query access of.
  1660. PathSize - Supplies the size of the supplied path buffer in bytes including
  1661. the null terminator.
  1662. DesiredFlags - Supplies the bitfield of flags the user is interested in.
  1663. See EFFECTIVE_ACCESS_* definitions.
  1664. UseRealIds - Supplies a boolean indicating whether the access check should
  1665. use the real user and group IDs (TRUE) or the effective user and group
  1666. IDs (FALSE).
  1667. EffectiveAccess - Supplies a pointer where the effective access permission
  1668. bits are returned. Only bits set in the desired flags parameter will be
  1669. potentially set. All other bits will be set to 0. See
  1670. EFFECTIVE_ACCESS_* definitions.
  1671. Return Value:
  1672. Status code indicating if the request completed successfully.
  1673. --*/
  1674. {
  1675. SYSTEM_CALL_GET_EFFECTIVE_ACCESS Parameters;
  1676. Parameters.Directory = Directory;
  1677. Parameters.FilePath = Path;
  1678. Parameters.FilePathSize = PathSize;
  1679. Parameters.UseRealIds = UseRealIds;
  1680. Parameters.DesiredFlags = DesiredFlags;
  1681. Parameters.EffectiveAccess = 0;
  1682. OsSystemCall(SystemCallGetEffectiveAccess, &Parameters);
  1683. *EffectiveAccess = Parameters.EffectiveAccess;
  1684. return Parameters.Status;
  1685. }
  1686. OS_API
  1687. KSTATUS
  1688. OsLoadDriver (
  1689. PSTR Path,
  1690. ULONG PathSize
  1691. )
  1692. /*++
  1693. Routine Description:
  1694. This routine loads the given driver into kernel address space.
  1695. Arguments:
  1696. Path - Supplies a pointer to a string containing the path to the driver.
  1697. PathSize - Supplies the size of the supplied path buffer in bytes including
  1698. the null terminator.
  1699. Return Value:
  1700. Status code indicating if the request completed successfully.
  1701. --*/
  1702. {
  1703. SYSTEM_CALL_LOAD_DRIVER Parameters;
  1704. Parameters.DriverName = Path;
  1705. Parameters.DriverNameSize = PathSize;
  1706. OsSystemCall(SystemCallLoadDriver, &Parameters);
  1707. return Parameters.Status;
  1708. }
  1709. OS_API
  1710. KSTATUS
  1711. OsLocateDeviceInformation (
  1712. PUUID Uuid,
  1713. PDEVICE_ID DeviceId,
  1714. PDEVICE_INFORMATION_RESULT Results,
  1715. PULONG ResultCount
  1716. )
  1717. /*++
  1718. Routine Description:
  1719. This routine returns instances of devices enumerating information. Callers
  1720. can get all devices enumerating the given information type, or all
  1721. information types enumerated by a given device. This routine must be called
  1722. at low level.
  1723. Arguments:
  1724. Uuid - Supplies an optional pointer to the information identifier to
  1725. filter on. If NULL, any information type will match.
  1726. DeviceId - Supplies an optional pointer to the device ID to match against.
  1727. If NULL, then any device will match.
  1728. Results - Supplies a pointer to a caller allocated buffer where the
  1729. results will be returned.
  1730. ResultCount - Supplies a pointer that upon input contains the size of the
  1731. buffer in information result elements. On output, returns the number
  1732. of elements in the query, even if the provided buffer was too small.
  1733. Do note however that the number of results can change between two
  1734. successive searches.
  1735. Return Value:
  1736. STATUS_SUCCESS on success.
  1737. STATUS_BUFFER_TOO_SMALL if the provided buffer was not large enough to
  1738. contain all the results. The result count will contain the required number
  1739. of elements to contain the results.
  1740. --*/
  1741. {
  1742. SYSTEM_CALL_LOCATE_DEVICE_INFORMATION Request;
  1743. RtlZeroMemory(&Request, sizeof(SYSTEM_CALL_LOCATE_DEVICE_INFORMATION));
  1744. if (Uuid != NULL) {
  1745. Request.ByUuid = TRUE;
  1746. RtlCopyMemory(&(Request.Uuid), Uuid, sizeof(UUID));
  1747. }
  1748. if (DeviceId != NULL) {
  1749. Request.ByDeviceId = TRUE;
  1750. Request.DeviceId = *DeviceId;
  1751. }
  1752. Request.Results = Results;
  1753. Request.ResultCount = *ResultCount;
  1754. OsSystemCall(SystemCallLocateDeviceInformation, &Request);
  1755. *ResultCount = Request.ResultCount;
  1756. return Request.Status;
  1757. }
  1758. OS_API
  1759. KSTATUS
  1760. OsGetSetDeviceInformation (
  1761. DEVICE_ID DeviceId,
  1762. PUUID Uuid,
  1763. PVOID Data,
  1764. PUINTN DataSize,
  1765. BOOL Set
  1766. )
  1767. /*++
  1768. Routine Description:
  1769. This routine gets or sets device information.
  1770. Arguments:
  1771. DeviceId - Supplies the device ID of the device to get or set information
  1772. for.
  1773. Uuid - Supplies a pointer to the identifier of the device information type
  1774. to get or set.
  1775. Data - Supplies a pointer to the data buffer that either contains the
  1776. information to set or will contain the information to get on success.
  1777. DataSize - Supplies a pointer that on input contains the size of the
  1778. data buffer. On output contains the actual size of the data.
  1779. Set - Supplies a boolean indicating whether to get information (FALSE) or
  1780. set information (TRUE).
  1781. Return Value:
  1782. Status code.
  1783. --*/
  1784. {
  1785. SYSTEM_CALL_GET_SET_DEVICE_INFORMATION Request;
  1786. RtlCopyMemory(&(Request.Uuid), Uuid, sizeof(UUID));
  1787. Request.DeviceId = DeviceId;
  1788. Request.Data = Data;
  1789. Request.DataSize = *DataSize;
  1790. Request.Set = Set;
  1791. OsSystemCall(SystemCallGetSetDeviceInformation, &Request);
  1792. *DataSize = Request.DataSize;
  1793. return Request.Status;
  1794. }
  1795. OS_API
  1796. KSTATUS
  1797. OsGetSetSystemInformation (
  1798. SYSTEM_INFORMATION_SUBSYSTEM Subsystem,
  1799. UINTN InformationType,
  1800. PVOID Data,
  1801. PUINTN DataSize,
  1802. BOOL Set
  1803. )
  1804. /*++
  1805. Routine Description:
  1806. This routine gets or sets system information.
  1807. Arguments:
  1808. Subsystem - Supplies the subsystem to query or set information of.
  1809. InformationType - Supplies the information type, which is specific to
  1810. the subsystem. The type of this value is generally
  1811. <subsystem>_INFORMATION_TYPE (eg IO_INFORMATION_TYPE).
  1812. Data - Supplies a pointer to the data buffer where the data is either
  1813. returned for a get operation or given for a set operation.
  1814. DataSize - Supplies a pointer that on input contains the size of the
  1815. data buffer. On output, contains the required size of the data buffer.
  1816. Set - Supplies a boolean indicating if this is a get operation (FALSE) or
  1817. a set operation (TRUE).
  1818. Return Value:
  1819. STATUS_SUCCESS if the information was successfully queried or set.
  1820. STATUS_BUFFER_TOO_SMALL if the buffer size specified was too small. The
  1821. required buffer size will be returned in the data size parameter.
  1822. STATUS_DATA_LENGTH_MISMATCH if the buffer size was not correct. The
  1823. correct buffer size will be returned in the data size parameter.
  1824. STATUS_INVALID_PARAMETER if the given subsystem or information type is
  1825. not known.
  1826. Other status codes on other failures.
  1827. --*/
  1828. {
  1829. SYSTEM_CALL_GET_SET_SYSTEM_INFORMATION Request;
  1830. Request.Subsystem = Subsystem;
  1831. Request.InformationType = InformationType;
  1832. Request.Data = Data;
  1833. Request.DataSize = *DataSize;
  1834. Request.Set = Set;
  1835. OsSystemCall(SystemCallGetSetSystemInformation, &Request);
  1836. *DataSize = Request.DataSize;
  1837. return Request.Status;
  1838. }
  1839. OS_API
  1840. KSTATUS
  1841. OsResetSystem (
  1842. SYSTEM_RESET_TYPE ResetType
  1843. )
  1844. /*++
  1845. Routine Description:
  1846. This routine attempts to reboot the system.
  1847. Arguments:
  1848. ResetType - Supplies the desired system reset type. If the given type is
  1849. not supported and a cold reset is, then a cold reset will be
  1850. performed.
  1851. Return Value:
  1852. STATUS_SUCCESS if the reset request was successfully queued. The process
  1853. should expect to receive a termination signal shortly, followed by a
  1854. kill signal shortly after that.
  1855. STATUS_INVALID_PARAMETER if the given reset type is not valid.
  1856. STATUS_INSUFFICIENT_RESOURCES if an allocation failure in the kernel
  1857. prevented queuing of the reset system work item.
  1858. --*/
  1859. {
  1860. SYSTEM_CALL_RESET_SYSTEM Request;
  1861. Request.ResetType = ResetType;
  1862. OsSystemCall(SystemCallResetSystem, &Request);
  1863. return Request.Status;
  1864. }
  1865. OS_API
  1866. PVOID
  1867. OsSetProgramBreak (
  1868. PVOID NewBreak
  1869. )
  1870. /*++
  1871. Routine Description:
  1872. This routine gets or sets the application program break for the process.
  1873. Arguments:
  1874. NewBreak - Supplies an optional pointer to the new break to set. If this
  1875. is less than the original break, then no change is made. Set to NULL
  1876. to simply get the current program break.
  1877. Return Value:
  1878. Returns the current program break, which is either the new value set or
  1879. the previous value.
  1880. --*/
  1881. {
  1882. SYSTEM_CALL_SET_BREAK Request;
  1883. Request.Break = NewBreak;
  1884. OsSystemCall(SystemCallSetBreak, &Request);
  1885. return Request.Break;
  1886. }
  1887. OS_API
  1888. KSTATUS
  1889. OsMemoryMap (
  1890. HANDLE Handle,
  1891. IO_OFFSET Offset,
  1892. UINTN Size,
  1893. ULONG Flags,
  1894. PVOID *Address
  1895. )
  1896. /*++
  1897. Routine Description:
  1898. This routine maps the specified object starting at the given offset for the
  1899. requested size, in bytes. A suggested address can optionally be supplied.
  1900. Arguments:
  1901. Handle - Supplies a pointer to an opened I/O handle.
  1902. Offset - Supplies the offset into the I/O object where the mapping should
  1903. begin.
  1904. Size - Supplies the size of the mapping region, in bytes.
  1905. Flags - Supplies a bitfield of flags. See SYS_MAP_FLAG_* for definitions.
  1906. Address - Supplies a pointer that receives the address of the mapped
  1907. region. The caller can optionally specify a suggested address using
  1908. this paramter.
  1909. Return Value:
  1910. Status code.
  1911. --*/
  1912. {
  1913. SYSTEM_CALL_MAP_UNMAP_MEMORY Parameters;
  1914. Parameters.Map = TRUE;
  1915. Parameters.Flags = Flags;
  1916. Parameters.Handle = Handle;
  1917. Parameters.Address = *Address;
  1918. Parameters.Offset = Offset;
  1919. Parameters.Size = Size;
  1920. OsSystemCall(SystemCallMapOrUnmapMemory, &Parameters);
  1921. if (KSUCCESS(Parameters.Status)) {
  1922. *Address = Parameters.Address;
  1923. }
  1924. return Parameters.Status;
  1925. }
  1926. OS_API
  1927. KSTATUS
  1928. OsMemoryUnmap (
  1929. PVOID Address,
  1930. UINTN Size
  1931. )
  1932. /*++
  1933. Routine Description:
  1934. This routine unmaps the specified region from the current process' address
  1935. space.
  1936. Arguments:
  1937. Address - Supplies the starting address of the memory region to unmap.
  1938. Size - Supplies the size of the region to unmap, in bytes.
  1939. Return Value:
  1940. Status code.
  1941. --*/
  1942. {
  1943. SYSTEM_CALL_MAP_UNMAP_MEMORY Parameters;
  1944. Parameters.Map = FALSE;
  1945. Parameters.Address = Address;
  1946. Parameters.Size = Size;
  1947. OsSystemCall(SystemCallMapOrUnmapMemory, &Parameters);
  1948. return Parameters.Status;
  1949. }
  1950. OS_API
  1951. KSTATUS
  1952. OsSetMemoryProtection (
  1953. PVOID Address,
  1954. UINTN Size,
  1955. ULONG NewAttributes
  1956. )
  1957. /*++
  1958. Routine Description:
  1959. This routine set the memory protection attributes for the given region.
  1960. Arguments:
  1961. Address - Supplies the starting address (inclusive) to change the memory
  1962. protection for. This must be aligned to a page boundary.
  1963. Size - Supplies the length, in bytes, of the region to change attributes
  1964. for.
  1965. NewAttributes - Supplies the new attributes to set. See SYS_MAP_FLAG_*
  1966. definitions.
  1967. Return Value:
  1968. Status code.
  1969. --*/
  1970. {
  1971. SYSTEM_CALL_SET_MEMORY_PROTECTION Parameters;
  1972. Parameters.Address = Address;
  1973. Parameters.Size = Size;
  1974. Parameters.NewAttributes = NewAttributes;
  1975. OsSystemCall(SystemCallSetMemoryProtection, &Parameters);
  1976. return Parameters.Status;
  1977. }
  1978. OS_API
  1979. KSTATUS
  1980. OsMemoryFlush (
  1981. PVOID Address,
  1982. ULONGLONG Size,
  1983. ULONG Flags
  1984. )
  1985. /*++
  1986. Routine Description:
  1987. This routine flushes a region of the current process' mapped memory to
  1988. permament storage, if the region has a backing image.
  1989. Arguments:
  1990. Address - Supplies the starting address of the memory region to synchronize.
  1991. Size - Supplies the size of the region to synchronize, in bytes.
  1992. Flags - Supplies a bitfield of flags. See SYS_MAP_SYNC_FLAG_* for
  1993. definitions.
  1994. Return Value:
  1995. Status code.
  1996. --*/
  1997. {
  1998. SYSTEM_CALL_FLUSH_MEMORY Parameters;
  1999. Parameters.Address = Address;
  2000. Parameters.Size = Size;
  2001. Parameters.Flags = Flags;
  2002. OsSystemCall(SystemCallFlushMemory, &Parameters);
  2003. return Parameters.Status;
  2004. }
  2005. OS_API
  2006. KSTATUS
  2007. OsSetThreadIdentity (
  2008. ULONG FieldsToSet,
  2009. PTHREAD_IDENTITY Identity
  2010. )
  2011. /*++
  2012. Routine Description:
  2013. This routine gets or sets a thread's identity.
  2014. Arguments:
  2015. FieldsToSet - Supplies a bitfield indicating which identity fields to set.
  2016. Supply zero to simply get the current thread identity. See
  2017. THREAD_IDENTITY_FIELD_* definitions.
  2018. Identity - Supplies a pointer that on input contains the thread identity
  2019. fields to set. On successful output, will contain the complete new
  2020. thread identity.
  2021. Return Value:
  2022. Status code.
  2023. --*/
  2024. {
  2025. SYSTEM_CALL_SET_THREAD_IDENTITY Parameters;
  2026. Parameters.Request.FieldsToSet = FieldsToSet;
  2027. if (FieldsToSet != 0) {
  2028. RtlCopyMemory(&(Parameters.Request.Identity),
  2029. Identity,
  2030. sizeof(THREAD_IDENTITY));
  2031. }
  2032. OsSystemCall(SystemCallSetThreadIdentity, &Parameters);
  2033. if (KSUCCESS(Parameters.Status)) {
  2034. RtlCopyMemory(Identity,
  2035. &(Parameters.Request.Identity),
  2036. sizeof(THREAD_IDENTITY));
  2037. }
  2038. return Parameters.Status;
  2039. }
  2040. OS_API
  2041. KSTATUS
  2042. OsSetThreadPermissions (
  2043. ULONG FieldsToSet,
  2044. PTHREAD_PERMISSIONS Permissions
  2045. )
  2046. /*++
  2047. Routine Description:
  2048. This routine gets or sets a thread's permission masks.
  2049. Arguments:
  2050. FieldsToSet - Supplies a bitfield indicating which permission sets or
  2051. fields to modify. Supply zero to simply get the current thread
  2052. permission sets. See THREAD_PERMISSION_FIELD_* definitions.
  2053. Permissions - Supplies a pointer that on input contains the thread
  2054. permission masks to set. On successful output, will contain the
  2055. complete new set of permission masks.
  2056. Return Value:
  2057. Status code.
  2058. --*/
  2059. {
  2060. SYSTEM_CALL_SET_THREAD_PERMISSIONS Parameters;
  2061. Parameters.Request.FieldsToSet = FieldsToSet;
  2062. if (FieldsToSet != 0) {
  2063. RtlCopyMemory(&(Parameters.Request.Permissions),
  2064. Permissions,
  2065. sizeof(THREAD_PERMISSIONS));
  2066. }
  2067. OsSystemCall(SystemCallSetThreadPermissions, &Parameters);
  2068. if (KSUCCESS(Parameters.Status)) {
  2069. RtlCopyMemory(Permissions,
  2070. &(Parameters.Request.Permissions),
  2071. sizeof(THREAD_PERMISSIONS));
  2072. }
  2073. return Parameters.Status;
  2074. }
  2075. OS_API
  2076. KSTATUS
  2077. OsSetSupplementaryGroups (
  2078. BOOL Set,
  2079. PGROUP_ID Groups,
  2080. PUINTN Count
  2081. )
  2082. /*++
  2083. Routine Description:
  2084. This routine gets or sets a thread's set of supplementary groups. To set
  2085. the supplementary groups, the thread must have the set group ID permission.
  2086. Arguments:
  2087. Set - Supplies a boolean indicating whether to set the new groups (TRUE) or
  2088. just get the current list of supplementary groups.
  2089. Groups - Supplies a pointer that recieves the supplementary groups for a
  2090. get operation or contains the new group IDs to set for a set operation.
  2091. Count - Supplies a pointer that on input contains the number of elements
  2092. in the given buffer. On output, contains the number of valid elements.
  2093. If STATUS_BUFFER_TOO_SMALL is returned, the number of elements needed
  2094. will be retunred.
  2095. Return Value:
  2096. STATUS_SUCCESS on success.
  2097. STATUS_ACCESS_VIOLATION if the given groups buffer is invalid.
  2098. STATUS_PERMISSION_DENIED if the caller does not have the set group ID
  2099. permission.
  2100. STATUS_INSUFFICIENT_RESOURCES if an internal kernel allocation failed.
  2101. STATUS_INVALID_PARAMETER if the count was too big.
  2102. STATUS_BUFFER_TOO_SMALL if the given buffer was not big enough to contain
  2103. all the current supplementary groups. In this case, count contains the
  2104. number of elements needed.
  2105. --*/
  2106. {
  2107. SYSTEM_CALL_SET_SUPPLEMENTARY_GROUPS Parameters;
  2108. Parameters.Set = Set;
  2109. Parameters.Groups = Groups;
  2110. Parameters.Count = *Count;
  2111. OsSystemCall(SystemCallSetSupplementaryGroups, &Parameters);
  2112. *Count = Parameters.Count;
  2113. return Parameters.Status;
  2114. }
  2115. OS_API
  2116. KSTATUS
  2117. OsSetResourceLimit (
  2118. RESOURCE_LIMIT_TYPE Type,
  2119. PRESOURCE_LIMIT NewValue,
  2120. PRESOURCE_LIMIT OldValue
  2121. )
  2122. /*++
  2123. Routine Description:
  2124. This routine gets or sets the current resource limit value for a given type.
  2125. Arguments:
  2126. Type - Supplies the resource limit type to get the limit for.
  2127. NewValue - Supplies an optional pointer to the new limit to set. If this is
  2128. NULL, then a new value is not set.
  2129. OldValue - Supplies an optional pointer where the previous limit will be
  2130. returned.
  2131. Return Value:
  2132. STATUS_SUCCESS on success.
  2133. STATUS_INVALID_PARAMETER if the resource type is not valid or the current
  2134. value was greater than the max.
  2135. STATUS_PERMISSION_DENIED if the caller is trying to raise the max/hard
  2136. limit and does not have the resources permission.
  2137. --*/
  2138. {
  2139. SYSTEM_CALL_SET_RESOURCE_LIMIT Parameters;
  2140. Parameters.Type = Type;
  2141. if (NewValue != NULL) {
  2142. Parameters.Set = TRUE;
  2143. Parameters.Value.Current = NewValue->Current;
  2144. Parameters.Value.Max = NewValue->Max;
  2145. } else {
  2146. Parameters.Set = FALSE;
  2147. }
  2148. OsSystemCall(SystemCallSetResourceLimit, &Parameters);
  2149. if (OldValue != NULL) {
  2150. OldValue->Current = Parameters.Value.Current;
  2151. OldValue->Max = Parameters.Value.Max;
  2152. }
  2153. return Parameters.Status;
  2154. }
  2155. OS_API
  2156. KSTATUS
  2157. OsCreateTerminal (
  2158. HANDLE MasterDirectory,
  2159. HANDLE SlaveDirectory,
  2160. PSTR MasterPath,
  2161. UINTN MasterPathLength,
  2162. PSTR SlavePath,
  2163. UINTN SlavePathLength,
  2164. ULONG MasterOpenFlags,
  2165. FILE_PERMISSIONS MasterCreatePermissions,
  2166. FILE_PERMISSIONS SlaveCreatePermissions,
  2167. PHANDLE MasterHandle
  2168. )
  2169. /*++
  2170. Routine Description:
  2171. This routine creates a new pseudo-terminal master and slave at the given
  2172. paths.
  2173. Arguments:
  2174. MasterDirectory - Supplies an optional handle to a directory for relative
  2175. paths when creating the master. Supply INVALID_HANDLE to use the
  2176. current working directory.
  2177. SlaveDirectory - Supplies an optional handle to a directory for relative
  2178. paths when creating the slave. Supply INVALID_HANDLE to use the
  2179. current working directory.
  2180. MasterPath - Supplies an optional pointer to the path to create for the
  2181. master.
  2182. MasterPathLength - Supplies the length of the master side path buffer in
  2183. bytes, including the null terminator.
  2184. SlavePath - Supplies an optional pointer to the path to create for the
  2185. master.
  2186. SlavePathLength - Supplies the length of the slave side path buffer in
  2187. bytes, including the null terminator.
  2188. MasterOpenFlags - Supplies the open flags to use when opening the master.
  2189. Only read, write, and "no controlling terminal" flags are honored.
  2190. MasterCreatePermissions - Supplies the permissions to apply to the created
  2191. master side.
  2192. SlaveCreatePermissions - Supplies the permission to apply to the created
  2193. slave side.
  2194. MasterHandle - Supplies a pointer where the opened handle to the master
  2195. will be returned on success.
  2196. Return Value:
  2197. Status code.
  2198. --*/
  2199. {
  2200. SYSTEM_CALL_CREATE_TERMINAL Parameters;
  2201. Parameters.MasterDirectory = MasterDirectory;
  2202. Parameters.SlaveDirectory = SlaveDirectory;
  2203. Parameters.MasterPath = MasterPath;
  2204. Parameters.MasterPathLength = MasterPathLength;
  2205. Parameters.SlavePath = SlavePath;
  2206. Parameters.SlavePathLength = SlavePathLength;
  2207. Parameters.MasterOpenFlags = MasterOpenFlags;
  2208. Parameters.MasterCreatePermissions = MasterCreatePermissions;
  2209. Parameters.SlaveCreatePermissions = SlaveCreatePermissions;
  2210. OsSystemCall(SystemCallCreateTerminal, &Parameters);
  2211. *MasterHandle = INVALID_HANDLE;
  2212. if (KSUCCESS(Parameters.Status)) {
  2213. *MasterHandle = Parameters.MasterHandle;
  2214. }
  2215. return Parameters.Status;
  2216. }
  2217. OS_API
  2218. KSTATUS
  2219. OsGetFilePath (
  2220. HANDLE Handle,
  2221. PSTR Path,
  2222. PUINTN PathSize
  2223. )
  2224. /*++
  2225. Routine Description:
  2226. This routine returns the file path for the given handle, if possible.
  2227. Arguments:
  2228. Handle - Supplies the open handle to get the file path of.
  2229. Path - Supplies a pointer where the path will be returned on success.
  2230. PathSize - Supplies a pointer that on input contains the size of the path
  2231. buffer. On output, returns the needed size of the path buffer, even
  2232. if the supplied buffer is too small.
  2233. Return Value:
  2234. STATUS_SUCCESS on success.
  2235. STATUS_INVALID_HANDLE if the given handle is not valid.
  2236. STATUS_PATH_NOT_FOUND if no path exists for the given handle.
  2237. STATUS_BUFFER_TOO_SMALL if the supplied path buffer was not large enough to
  2238. contain the complete path. In this case the path size returned is the size
  2239. needed.
  2240. STATUS_ACCESS_VIOLATION if the path buffer was invalid.
  2241. --*/
  2242. {
  2243. FILE_CONTROL_PARAMETERS_UNION Parameters;
  2244. KSTATUS Status;
  2245. Parameters.FilePath.Path = Path;
  2246. Parameters.FilePath.PathSize = *PathSize;
  2247. Status = OsFileControl(Handle, FileControlCommandGetPath, &Parameters);
  2248. *PathSize = Parameters.FilePath.PathSize;
  2249. return Status;
  2250. }
  2251. OS_API
  2252. VOID
  2253. OsSetThreadIdPointer (
  2254. PTHREAD_ID Pointer
  2255. )
  2256. /*++
  2257. Routine Description:
  2258. This routine sets the thread ID pointer in the kernel. If this value is
  2259. non-null when the thread exits, then zero will be written to this address,
  2260. and a UserLockWake operation will be performed to wake up one thread.
  2261. Arguments:
  2262. Pointer - Supplies the new user mode pointer to the thread ID that will be
  2263. cleared when the thread exits. Supply NULL to clear the thread ID
  2264. pointer in the kernel. If this is non-null, the kernel will write the
  2265. thread ID into this region.
  2266. Return Value:
  2267. None.
  2268. --*/
  2269. {
  2270. SYSTEM_CALL_SET_THREAD_ID_POINTER Parameters;
  2271. Parameters.Pointer = Pointer;
  2272. OsSystemCall(SystemCallSetThreadIdPointer, &Parameters);
  2273. return;
  2274. }
  2275. OS_API
  2276. FILE_PERMISSIONS
  2277. OsSetUmask (
  2278. FILE_PERMISSIONS NewMask
  2279. )
  2280. /*++
  2281. Routine Description:
  2282. This routine sets file permission mask for the current process. Bits set
  2283. in this mask will be automatically cleared out of the permissions of any
  2284. file or directory created.
  2285. Arguments:
  2286. NewMask - Supplies the new mask to set.
  2287. Return Value:
  2288. Returns the previously set mask.
  2289. --*/
  2290. {
  2291. SYSTEM_CALL_SET_UMASK Parameters;
  2292. Parameters.Mask = NewMask;
  2293. OsSystemCall(SystemCallSetUmask, &Parameters);
  2294. return Parameters.Mask;
  2295. }
  2296. OS_API
  2297. KSTATUS
  2298. OsDuplicateHandle (
  2299. HANDLE ExistingHandle,
  2300. PHANDLE NewHandle,
  2301. ULONG Flags
  2302. )
  2303. /*++
  2304. Routine Description:
  2305. This routine duplicates a given handle at a new handle.
  2306. Arguments:
  2307. ExistingHandle - Supplies the handle to duplicate.
  2308. NewHandle - Supplies a pointer that contains the destination handle value
  2309. for the new handle. If this is INVALID_HANDLE, then the duplicated
  2310. handle will be the lowest available handle value, and will be returned
  2311. here. If this is not INVALID_HANDLE, then the previous handle at that
  2312. location will be closed. If the new handle equals the existing handle,
  2313. failure is returned.
  2314. Flags - Supplies open flags to be set on the new handle. Only
  2315. SYS_OPEN_FLAG_CLOSE_ON_EXECUTE is permitted. If not set, the new handle
  2316. will have the close on execute flag cleared.
  2317. Return Value:
  2318. Status code.
  2319. --*/
  2320. {
  2321. SYSTEM_CALL_DUPLICATE_HANDLE Parameters;
  2322. Parameters.OldHandle = ExistingHandle;
  2323. Parameters.NewHandle = *NewHandle;
  2324. Parameters.OpenFlags = Flags;
  2325. OsSystemCall(SystemCallDuplicateHandle, &Parameters);
  2326. *NewHandle = Parameters.NewHandle;
  2327. return Parameters.Status;
  2328. }
  2329. VOID
  2330. OspProcessSignal (
  2331. PSIGNAL_PARAMETERS Parameters,
  2332. PSIGNAL_CONTEXT Context
  2333. )
  2334. /*++
  2335. Routine Description:
  2336. This routine processes a signal sent via the kernel.
  2337. Arguments:
  2338. Parameters - Supplies a pointer to the signal parameters from the kernel.
  2339. Context - Supplies a pointer to the signal context from the kernel.
  2340. Return Value:
  2341. None.
  2342. --*/
  2343. {
  2344. BOOL RestartAllowed;
  2345. PSIGNAL_HANDLER_ROUTINE SignalHandler;
  2346. RestartAllowed = FALSE;
  2347. SignalHandler = OsSignalHandler;
  2348. if (SignalHandler != NULL) {
  2349. RestartAllowed = SignalHandler(Parameters);
  2350. }
  2351. //
  2352. // Clear the restart flag if it's set but the handler does not allow
  2353. // restarts.
  2354. //
  2355. if (((Context->Flags & SIGNAL_CONTEXT_FLAG_RESTART) != 0) &&
  2356. (RestartAllowed == FALSE)) {
  2357. Context->Flags &= ~SIGNAL_CONTEXT_FLAG_RESTART;
  2358. }
  2359. return;
  2360. }
  2361. //
  2362. // --------------------------------------------------------- Internal Functions
  2363. //
  2364. PVOID
  2365. OspSetSignalHandler (
  2366. PVOID SignalHandlerRoutine
  2367. )
  2368. /*++
  2369. Routine Description:
  2370. This routine sets the signal handler routine for the given thread.
  2371. Arguments:
  2372. SignalHandlerRoutine - Supplies a pointer to the signal handler routine to
  2373. invoke when signals occur. The two parameters to the routine will be
  2374. the signal number and the optional parameter, both of which will be
  2375. passed via registers.
  2376. Return Value:
  2377. Returns a pointer to the original signal handler registered, or NULL if no
  2378. signal handler was previously registered.
  2379. --*/
  2380. {
  2381. SYSTEM_CALL_SET_SIGNAL_HANDLER Parameters;
  2382. Parameters.SignalHandler = SignalHandlerRoutine;
  2383. OsSystemCall(SystemCallSetSignalHandler, &Parameters);
  2384. return Parameters.SignalHandler;
  2385. }
  2386. KSTATUS
  2387. OspGetSetFileInformation (
  2388. HANDLE Directory,
  2389. PSTR Path,
  2390. ULONG PathSize,
  2391. BOOL FollowLink,
  2392. PSET_FILE_INFORMATION Request
  2393. )
  2394. /*++
  2395. Routine Description:
  2396. This routine gets or sets the file properties for a given file.
  2397. Arguments:
  2398. Directory - Supplies an optional handle to a directory to start the
  2399. search from if the supplied path is relative. Supply INVALID_HANDLE
  2400. here to to use the current directory for relative paths (usually the
  2401. expected default).
  2402. Path - Supplies a pointer to the string containing the file path to get or
  2403. set properties for.
  2404. PathSize - Supplies the size of the path string in bytes including the
  2405. null terminator.
  2406. FollowLink - Supplies a boolean indicating what to do if the file path
  2407. points to a symbolic link. If set to TRUE, the file information set or
  2408. returned will be for the file the link points to. If FALSE, the call
  2409. will set or get information for the link itself.
  2410. Request - Supplies a pointer to the get or set file properties request.
  2411. Return Value:
  2412. Status code.
  2413. --*/
  2414. {
  2415. SYSTEM_CALL_GET_SET_FILE_INFORMATION Parameters;
  2416. Parameters.Request.FieldsToSet = 0;
  2417. if (Request->FieldsToSet != 0) {
  2418. RtlCopyMemory(&(Parameters.Request),
  2419. Request,
  2420. sizeof(SET_FILE_INFORMATION));
  2421. }
  2422. Parameters.Directory = Directory;
  2423. Parameters.FilePath = Path;
  2424. Parameters.FilePathSize = PathSize;
  2425. Parameters.FollowLink = FollowLink;
  2426. OsSystemCall(SystemCallGetSetFileInformation, &Parameters);
  2427. if (Request->FieldsToSet == 0) {
  2428. RtlCopyMemory(Request,
  2429. &(Parameters.Request),
  2430. sizeof(SET_FILE_INFORMATION));
  2431. }
  2432. return Parameters.Status;
  2433. }
  2434. KSTATUS
  2435. OspMountOrUnmount (
  2436. PSTR MountPointPath,
  2437. ULONG MountPointPathSize,
  2438. PSTR TargetPath,
  2439. ULONG TargetPathSize,
  2440. ULONG Flags
  2441. )
  2442. /*++
  2443. Routine Description:
  2444. This routine mounts the target at the given mount point or unmounts the
  2445. mount point.
  2446. Arguments:
  2447. MountPointPath - Supplies a pointer to a string containing the path to the
  2448. mount point where the target is to be mounted.
  2449. MountPointPathSize - Supplies the size of the mount point path string in
  2450. bytes, including the null terminator.
  2451. TargetPath - Supplies a pointer to a string containing the path to the
  2452. target file, directory, volume, or device that is to be mounted.
  2453. TargetPathSize - Supplies the size of the target path string in bytes,
  2454. including the null terminator.
  2455. Flags - Supplies the flags associated with the mount operation. See
  2456. SYS_MOUNT_FLAG_* for definitions.
  2457. Return Value:
  2458. Status code.
  2459. --*/
  2460. {
  2461. SYSTEM_CALL_MOUNT_UNMOUNT Parameters;
  2462. //
  2463. // Make the system call with the mount/unmount information.
  2464. //
  2465. Parameters.MountPointPath = MountPointPath;
  2466. Parameters.MountPointPathSize = MountPointPathSize;
  2467. Parameters.TargetPath = TargetPath;
  2468. Parameters.TargetPathSize = TargetPathSize;
  2469. Parameters.Flags = Flags;
  2470. OsSystemCall(SystemCallMountOrUnmount, &Parameters);
  2471. return Parameters.Status;
  2472. }
  2473. NO_RETURN
  2474. VOID
  2475. OspExitThread (
  2476. PVOID UnmapAddress,
  2477. UINTN UnmapSize
  2478. )
  2479. /*++
  2480. Routine Description:
  2481. This routine terminates the current thread, and optionally attempts to
  2482. unmap a region of memory on its way out. Usually this is the stack of the
  2483. thread that is exiting.
  2484. Arguments:
  2485. UnmapAddress - Supplies an optional pointer to a region of memory to unmap
  2486. as the thread exits. Supply NULL to skip unmapping.
  2487. UnmapSize - Supplies the size of the region to unmap in bytes. This must be
  2488. aligned to the page size. If it is not, the unmap simply won't happen.
  2489. Supply 0 to skip the unmap and just exit the thread.
  2490. Return Value:
  2491. This routine does not return.
  2492. --*/
  2493. {
  2494. SYSTEM_CALL_EXIT_THREAD Parameters;
  2495. Parameters.UnmapAddress = UnmapAddress;
  2496. Parameters.UnmapSize = UnmapSize;
  2497. OsSystemCall(SystemCallExitThread, &Parameters);
  2498. while (TRUE) {
  2499. ASSERT(FALSE);
  2500. }
  2501. }