libcp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. libcp.h
  9. Abstract:
  10. This header contains internal definitions for the Minoca C Library.
  11. Author:
  12. Evan Green 4-Mar-2013
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #define LIBC_API __DLLEXPORT
  18. #include <minoca/lib/minocaos.h>
  19. #include <libcbase.h>
  20. #include <minoca/lib/mlibc.h>
  21. #include <pthread.h>
  22. #include <sys/resource.h>
  23. #include <sys/types.h>
  24. #include <wchar.h>
  25. //
  26. // --------------------------------------------------------------------- Macros
  27. //
  28. //
  29. // This macro non-destructively sets the orientation of the given stream.
  30. //
  31. #define ORIENT_STREAM(_Stream, _Orientation) \
  32. if (((_Stream)->Flags & FILE_FLAG_ORIENTATION_MASK) == 0) { \
  33. (_Stream)->Flags |= (_Orientation); \
  34. }
  35. //
  36. // This macro zeros memory and ensures that the compiler doesn't optimize away
  37. // the memset.
  38. //
  39. #define SECURITY_ZERO(_Buffer, _Size) \
  40. { \
  41. memset((_Buffer), 0, (_Size)); \
  42. *(volatile char *)(_Buffer) = *((volatile char *)(_Buffer) + 1); \
  43. }
  44. //
  45. // This macro asserts that the file permission bits are equivalent.
  46. //
  47. #define ASSERT_FILE_PERMISSIONS_EQUIVALENT() \
  48. assert((FILE_PERMISSION_USER_READ == S_IRUSR) && \
  49. (FILE_PERMISSION_USER_WRITE == S_IWUSR) && \
  50. (FILE_PERMISSION_USER_EXECUTE == S_IXUSR) && \
  51. (FILE_PERMISSION_GROUP_READ == S_IRGRP) && \
  52. (FILE_PERMISSION_GROUP_WRITE == S_IWGRP) && \
  53. (FILE_PERMISSION_GROUP_EXECUTE == S_IXGRP) && \
  54. (FILE_PERMISSION_OTHER_READ == S_IROTH) && \
  55. (FILE_PERMISSION_OTHER_WRITE == S_IWOTH) && \
  56. (FILE_PERMISSION_OTHER_EXECUTE == S_IXOTH) && \
  57. (FILE_PERMISSION_SET_USER_ID == S_ISUID) && \
  58. (FILE_PERMISSION_SET_GROUP_ID == S_ISGID))
  59. //
  60. // ---------------------------------------------------------------- Definitions
  61. //
  62. //
  63. // Define internal file flags.
  64. //
  65. #define FILE_FLAG_UNGET_VALID 0x00000001
  66. #define FILE_FLAG_END_OF_FILE 0x00000002
  67. #define FILE_FLAG_ERROR 0x00000004
  68. #define FILE_FLAG_BYTE_ORIENTED 0x00000008
  69. #define FILE_FLAG_WIDE_ORIENTED 0x00000010
  70. #define FILE_FLAG_READ_LAST 0x00000020
  71. #define FILE_FLAG_DISABLE_LOCKING 0x00000040
  72. #define FILE_FLAG_BUFFER_ALLOCATED 0x00000080
  73. #define FILE_FLAG_STANDARD_IO 0x00000100
  74. #define FILE_FLAG_CAN_READ 0x00000200
  75. #define FILE_FLAG_ORIENTATION_MASK \
  76. (FILE_FLAG_BYTE_ORIENTED | FILE_FLAG_WIDE_ORIENTED)
  77. //
  78. // Define the maximum size of a passwd or group line/data buffer.
  79. //
  80. #define USER_DATABASE_LINE_MAX 1024
  81. //
  82. // Define the internal signal number used for thread cancellation.
  83. //
  84. #define SIGNAL_PTHREAD 32
  85. //
  86. // Define the internal signal number used for set ID requests.
  87. //
  88. #define SIGNAL_SETID 33
  89. //
  90. // ------------------------------------------------------ Data Type Definitions
  91. //
  92. /*++
  93. Structure Description:
  94. This structure stores information about an open file stream.
  95. Members:
  96. ListEntry - Stores pointers to the next and previous streams in the global
  97. list.
  98. Descriptor - Stores the file descriptor number.
  99. OpenFlags - Stores the flags the file was opened with.
  100. Flags - Stores internal flags. See FILE_FLAG_* definitions.
  101. Lock - Stores the stream lock.
  102. BufferMode - Stores the buffering mode for this file. This value is either
  103. _IOFBF for fully buffered, _IOLBF for line buffered, or _IONBF for
  104. non-buffered.
  105. Buffer - Stores a pointer to the file buffer.
  106. BufferSize - Stores the size of the file buffer in bytes.
  107. BufferValidSize - Stores the number of bytes in the buffer that actually
  108. have good data in them.
  109. BufferNextIndex - Stores the index into the buffer where the next read or
  110. write will occur.
  111. UngetCharacter - Stores the unget character.
  112. Pid - Stores the process ID of the process if the stream was opened with
  113. the popen routine.
  114. ShiftState - Stores the current multi-byte shift state.
  115. --*/
  116. typedef struct _FILE {
  117. LIST_ENTRY ListEntry;
  118. ULONG Descriptor;
  119. ULONG OpenFlags;
  120. ULONG Flags;
  121. pthread_mutex_t Lock;
  122. ULONG BufferMode;
  123. PCHAR Buffer;
  124. ULONG BufferSize;
  125. ULONG BufferValidSize;
  126. ULONG BufferNextIndex;
  127. WCHAR UngetCharacter;
  128. pid_t Pid;
  129. mbstate_t ShiftState;
  130. } *PFILE;
  131. /*++
  132. Structure Description:
  133. This structure defines a C library type conversion interface.
  134. Members:
  135. ListEntry - Stores the interfaces list entry into the global list of type
  136. conversion interfaces.
  137. Type - Stores the conversion type of the interface.
  138. Buffer - Stores an opaque pointer to the interface buffer.
  139. Network - Stores a pointer to the network type conversion interface.
  140. --*/
  141. typedef struct _CL_TYPE_CONVERSION_INTERFACE {
  142. LIST_ENTRY ListEntry;
  143. CL_CONVERSION_TYPE Type;
  144. union {
  145. PVOID Buffer;
  146. PCL_NETWORK_CONVERSION_INTERFACE Network;
  147. } Interface;
  148. } CL_TYPE_CONVERSION_INTERFACE, *PCL_TYPE_CONVERSION_INTERFACE;
  149. //
  150. // -------------------------------------------------------------------- Globals
  151. //
  152. //
  153. // Store the global list of type conversion interfaces, protected by a global
  154. // lock.
  155. //
  156. extern LIST_ENTRY ClTypeConversionInterfaceList;
  157. extern pthread_mutex_t ClTypeConversionInterfaceLock;
  158. //
  159. // -------------------------------------------------------- Function Prototypes
  160. //
  161. VOID
  162. ClpInitializeSignals (
  163. VOID
  164. );
  165. /*++
  166. Routine Description:
  167. This routine initializes signal handling functionality for the C library.
  168. Arguments:
  169. None.
  170. Return Value:
  171. None.
  172. --*/
  173. BOOL
  174. ClpInitializeFileIo (
  175. VOID
  176. );
  177. /*++
  178. Routine Description:
  179. This routine initializes the file I/O subsystem of the C library.
  180. Arguments:
  181. None.
  182. Return Value:
  183. TRUE on success.
  184. FALSE on failure.
  185. --*/
  186. BOOL
  187. ClpInitializeTypeConversions (
  188. VOID
  189. );
  190. /*++
  191. Routine Description:
  192. This routine initializes the type conversion subsystem of the C library.
  193. Arguments:
  194. None.
  195. Return Value:
  196. TRUE on success.
  197. FALSE on failure.
  198. --*/
  199. VOID
  200. ClpInitializeEnvironment (
  201. VOID
  202. );
  203. /*++
  204. Routine Description:
  205. This routine initializes the environment variable support in the C library.
  206. Arguments:
  207. None.
  208. Return Value:
  209. None.
  210. --*/
  211. VOID
  212. ClpLockStream (
  213. FILE *Stream
  214. );
  215. /*++
  216. Routine Description:
  217. This routine locks the file stream.
  218. Arguments:
  219. Stream - Supplies a pointer to the stream to lock.
  220. Return Value:
  221. None.
  222. --*/
  223. BOOL
  224. ClpTryToLockStream (
  225. FILE *Stream
  226. );
  227. /*++
  228. Routine Description:
  229. This routine makes a single attempt at locking the file stream. If locking
  230. is disabled on the stream, this always returns TRUE.
  231. Arguments:
  232. Stream - Supplies a pointer to the stream to try to lock.
  233. Return Value:
  234. TRUE if the lock was successfully acquired.
  235. FALSE if the lock was not successfully acquired.
  236. --*/
  237. VOID
  238. ClpUnlockStream (
  239. FILE *Stream
  240. );
  241. /*++
  242. Routine Description:
  243. This routine unlocks the file stream.
  244. Arguments:
  245. Stream - Supplies a pointer to the stream to unlock.
  246. Return Value:
  247. None.
  248. --*/
  249. VOID
  250. ClpFlushAllStreams (
  251. BOOL AllUnlocked,
  252. PFILE UnlockedStream
  253. );
  254. /*++
  255. Routine Description:
  256. This routine flushes every stream in the application.
  257. Arguments:
  258. AllUnlocked - Supplies a boolean that if TRUE flushes every stream without
  259. acquiring the file lock. This is used during aborts.
  260. UnlockedStream - Supplies a specific stream that when flushed should be
  261. flushed unlocked.
  262. Return Value:
  263. None.
  264. --*/
  265. VOID
  266. ClpInitializeTimeZoneSupport (
  267. VOID
  268. );
  269. /*++
  270. Routine Description:
  271. This routine initializes support for time zones.
  272. Arguments:
  273. None.
  274. Return Value:
  275. None.
  276. --*/
  277. time_t
  278. ClpConvertSystemTimeToUnixTime (
  279. PSYSTEM_TIME SystemTime
  280. );
  281. /*++
  282. Routine Description:
  283. This routine converts the given system time structure into a time_t time
  284. value. Fractional seconds are truncated.
  285. Arguments:
  286. SystemTime - Supplies a pointer to the system time structure.
  287. Return Value:
  288. Returns the time_t value corresponding to the given system time.
  289. --*/
  290. VOID
  291. ClpConvertUnixTimeToSystemTime (
  292. PSYSTEM_TIME SystemTime,
  293. time_t UnixTime
  294. );
  295. /*++
  296. Routine Description:
  297. This routine converts the given time_t value into a system time structure.
  298. Fractional seconds in the system time structure are set to zero.
  299. Arguments:
  300. SystemTime - Supplies a pointer to the system time structure to initialize.
  301. UnixTime - Supplies the time to set.
  302. Return Value:
  303. None.
  304. --*/
  305. VOID
  306. ClpConvertTimeValueToSystemTime (
  307. PSYSTEM_TIME SystemTime,
  308. const struct timeval *TimeValue
  309. );
  310. /*++
  311. Routine Description:
  312. This routine converts the given time value into a system time structure.
  313. Arguments:
  314. SystemTime - Supplies a pointer to the system time structure to initialize.
  315. TimeValue - Supplies a pointer to the time value structure to be converted
  316. to system time.
  317. Return Value:
  318. None.
  319. --*/
  320. VOID
  321. ClpConvertSpecificTimeToSystemTime (
  322. PSYSTEM_TIME SystemTime,
  323. const struct timespec *SpecificTime
  324. );
  325. /*++
  326. Routine Description:
  327. This routine converts the given specific time into a system time structure.
  328. Arguments:
  329. SystemTime - Supplies a pointer to the system time structure to initialize.
  330. SpecificTime - Supplies a pointer to the specific time structure to be
  331. converted to system time.
  332. Return Value:
  333. None.
  334. --*/
  335. VOID
  336. ClpConvertCounterToTimeValue (
  337. ULONGLONG Counter,
  338. ULONGLONG Frequency,
  339. struct timeval *TimeValue
  340. );
  341. /*++
  342. Routine Description:
  343. This routine converts a tick count at a known frequency into a time value
  344. structure, rounded up to the nearest microsecond.
  345. Arguments:
  346. Counter - Supplies the counter value in ticks.
  347. Frequency - Supplies the frequency of the counter in Hertz. This must not
  348. be zero.
  349. TimeValue - Supplies a pointer where the time value equivalent will be
  350. returned.
  351. Return Value:
  352. None.
  353. --*/
  354. VOID
  355. ClpConvertTimeValueToCounter (
  356. PULONGLONG Counter,
  357. ULONGLONG Frequency,
  358. const struct timeval *TimeValue
  359. );
  360. /*++
  361. Routine Description:
  362. This routine converts a time value into a tick count at a known frequency,
  363. rounded up to the nearest tick.
  364. Arguments:
  365. Counter - Supplies a pointer that receives the calculated counter value in
  366. ticks.
  367. Frequency - Supplies the frequency of the counter in Hertz. This must not
  368. be zero.
  369. TimeValue - Supplies a pointer to the time value.
  370. Return Value:
  371. None.
  372. --*/
  373. VOID
  374. ClpConvertCounterToSpecificTime (
  375. ULONGLONG Counter,
  376. ULONGLONG Frequency,
  377. struct timespec *SpecificTime
  378. );
  379. /*++
  380. Routine Description:
  381. This routine converts a tick count at a known frequency into a specific
  382. time structure, rounded up to the nearest nanosecond.
  383. Arguments:
  384. Counter - Supplies the counter value in ticks.
  385. Frequency - Supplies the frequency of the counter in Hertz. This must not
  386. be zero.
  387. SpecificTime - Supplies a pointer where the specific time equivalent will
  388. be returned.
  389. Return Value:
  390. None.
  391. --*/
  392. VOID
  393. ClpConvertSpecificTimeToCounter (
  394. PULONGLONG Counter,
  395. ULONGLONG Frequency,
  396. const struct timespec *SpecificTime
  397. );
  398. /*++
  399. Routine Description:
  400. This routine converts a specific time into a tick count at a known
  401. frequency, rounded up to the nearest tick.
  402. Arguments:
  403. Counter - Supplies a pointer that receives the calculated counter value in
  404. ticks.
  405. Frequency - Supplies the frequency of the counter in Hertz. This must not
  406. be zero.
  407. SpecificTime - Supplies a pointer to the specific time.
  408. Return Value:
  409. None.
  410. --*/
  411. INT
  412. ClpConvertSpecificTimeoutToSystemTimeout (
  413. const struct timespec *SpecificTimeout,
  414. PULONG TimeoutInMilliseconds
  415. );
  416. /*++
  417. Routine Description:
  418. This routine converts the given specific timeout into a system timeout in
  419. milliseconds. The specific timeout's seconds and nanoseconds must not be
  420. negative and the nanoseconds must not be greater than 1 billion (the number
  421. of nanoseconds in a second). If the specific timeout is NULL, then the
  422. timeout in milliseconds will be set to an indefinite timeout.
  423. Arguments:
  424. SpecificTimeout - Supplies an optional pointer to the specific timeout.
  425. TimeoutInMilliseconds - Supplies a pointer that receives the system timeout
  426. in milliseconds.
  427. Return Value:
  428. 0 on success.
  429. Returns an error number on failure.
  430. --*/
  431. VOID
  432. ClpConvertResourceUsage (
  433. PRESOURCE_USAGE KernelUsage,
  434. ULONGLONG Frequency,
  435. struct rusage *LibraryUsage
  436. );
  437. /*++
  438. Routine Description:
  439. This routine converts a kernel resource usage structure into a struct
  440. rusage.
  441. Arguments:
  442. KernelUsage - Supplies a a pointer to the structure to convert.
  443. Frequency - Supplies the frequency of the processor for converting cycle
  444. counts.
  445. LibraryUsage - Supplies a pointer where the library usage structure will
  446. be returned.
  447. Return Value:
  448. None.
  449. --*/
  450. VOID
  451. ClpSetThreadIdentityOnAllThreads (
  452. ULONG Fields,
  453. PTHREAD_IDENTITY Identity
  454. );
  455. /*++
  456. Routine Description:
  457. This routine uses a signal to set the thread identity on all threads
  458. except the current one (which is assumed to have already been set).
  459. Arguments:
  460. Fields - Supplies the bitfield of identity fields to set. See
  461. THREAD_IDENTITY_FIELD_* definitions.
  462. Identity - Supplies a pointer to the thread identity information to set.
  463. Return Value:
  464. None.
  465. --*/
  466. VOID
  467. ClpSetSupplementaryGroupsOnAllThreads (
  468. PGROUP_ID GroupIds,
  469. UINTN GroupIdCount
  470. );
  471. /*++
  472. Routine Description:
  473. This routine uses a signal to set the supplementary groups on all threads
  474. except the current one (which is assumed to have already been set).
  475. Arguments:
  476. GroupIds - Supplies a pointer to the array of group IDs to set.
  477. GroupIdCount - Supplies the number of elements in the group ID array.
  478. Return Value:
  479. None.
  480. --*/
  481. void
  482. ClpUnregisterAtfork (
  483. void *DynamicObjectHandle
  484. );
  485. /*++
  486. Routine Description:
  487. This routine unregisters any at-fork handlers registered with the given
  488. dynamic object handle.
  489. Arguments:
  490. DynamicObjectHandle - Supplies the value unique to the dynamic object. All
  491. handlers registered with this same value will be removed.
  492. Return Value:
  493. None.
  494. --*/
  495. VOID
  496. ClpRunAtforkPrepareRoutines (
  497. VOID
  498. );
  499. /*++
  500. Routine Description:
  501. This routine calls the prepare routine for any fork handlers.
  502. Arguments:
  503. None.
  504. Return Value:
  505. None. This function returns with the at-fork mutex held.
  506. --*/
  507. VOID
  508. ClpRunAtforkChildRoutines (
  509. VOID
  510. );
  511. /*++
  512. Routine Description:
  513. This routine calls the child routine for any fork handlers. This routine
  514. must obviously be called from a newly forked child. This function assumes
  515. that the at-fork mutex is held, and re-initializes it.
  516. Arguments:
  517. None.
  518. Return Value:
  519. None.
  520. --*/
  521. VOID
  522. ClpRunAtforkParentRoutines (
  523. VOID
  524. );
  525. /*++
  526. Routine Description:
  527. This routine calls the child routine for any fork handlers. This routine
  528. must obviously be called from a newly forked child. This function assumes
  529. that the at-fork mutex is held, and releases it.
  530. Arguments:
  531. None.
  532. Return Value:
  533. None.
  534. --*/
  535. PSTR
  536. ClpGetFqdn (
  537. VOID
  538. );
  539. /*++
  540. Routine Description:
  541. This routine returns a null terminated string containing the fully
  542. qualified domain name of the machine.
  543. Arguments:
  544. None.
  545. Return Value:
  546. Returns a null terminated string containing nodename.domainname on success.
  547. The caller is responsible for freeing this string.
  548. NULL on allocation failure.
  549. --*/
  550. int
  551. ClpSetSignalAction (
  552. int SignalNumber,
  553. struct sigaction *NewAction,
  554. struct sigaction *OriginalAction
  555. );
  556. /*++
  557. Routine Description:
  558. This routine sets a new signal action for the given signal number.
  559. Arguments:
  560. SignalNumber - Supplies the signal number that will be affected.
  561. NewAction - Supplies an optional pointer to the new signal action to
  562. perform upon receiving that signal. If this pointer is NULL, then no
  563. change will be made to the signal's action.
  564. OriginalAction - Supplies a pointer where the original signal action will
  565. be returned.
  566. Return Value:
  567. 0 on success.
  568. -1 on error, and the errno variable will contain more information.
  569. --*/