sysconf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. sysconf.c
  9. Abstract:
  10. This module implements the sysconf function, which provides operating
  11. system limits and values to the application.
  12. Author:
  13. Evan Green 24-Jun-2013
  14. Environment:
  15. User Mode C Library
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include "libcp.h"
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <limits.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. //
  36. // -------------------------------------------------------------------- Globals
  37. //
  38. //
  39. // ------------------------------------------------------------------ Functions
  40. //
  41. LIBC_API
  42. long
  43. sysconf (
  44. int Variable
  45. )
  46. /*++
  47. Routine Description:
  48. This routine gets the system value for the given variable index. These
  49. variables are not expected to change within a single invocation of a
  50. process, and therefore need only be queried once per process.
  51. Arguments:
  52. Variable - Supplies the variable to get. See _SC_* definitions.
  53. Return Value:
  54. Returns the value for that variable.
  55. -1 if the variable has no limit. The errno variable will be left unchanged.
  56. -1 if the variable was invalid, and errno will be set to EINVAL.
  57. --*/
  58. {
  59. ULONGLONG BigValue;
  60. MM_STATISTICS MmStatistics;
  61. PROCESSOR_COUNT_INFORMATION ProcessorCountInformation;
  62. UINTN Size;
  63. KSTATUS Status;
  64. long Value;
  65. switch (Variable) {
  66. case _SC_CLK_TCK:
  67. Value = CLOCKS_PER_SEC;
  68. break;
  69. case _SC_PAGE_SIZE:
  70. case _SC_PHYS_PAGES:
  71. case _SC_AVPHYS_PAGES:
  72. MmStatistics.Version = MM_STATISTICS_VERSION;
  73. Size = sizeof(MM_STATISTICS);
  74. Status = OsGetSetSystemInformation(SystemInformationMm,
  75. MmInformationSystemMemory,
  76. &MmStatistics,
  77. &Size,
  78. FALSE);
  79. if (!KSUCCESS(Status)) {
  80. RtlDebugPrint("Get MmStatistics failed: %d\n", Status);
  81. errno = ClConvertKstatusToErrorNumber(Status);
  82. return -1;
  83. }
  84. if (Variable == _SC_PAGE_SIZE) {
  85. BigValue = MmStatistics.PageSize;
  86. } else if (Variable == _SC_PHYS_PAGES) {
  87. BigValue = MmStatistics.PhysicalPages;
  88. } else if (Variable == _SC_AVPHYS_PAGES) {
  89. BigValue = MmStatistics.AllocatedPhysicalPages;
  90. } else {
  91. assert(FALSE);
  92. BigValue = -1;
  93. }
  94. if (BigValue > MAX_LONG) {
  95. Value = MAX_LONG;
  96. } else {
  97. Value = (long)BigValue;
  98. }
  99. break;
  100. case _SC_ARG_MAX:
  101. case _SC_CHILD_MAX:
  102. case _SC_HOST_NAME_MAX:
  103. case _SC_LOGIN_NAME_MAX:
  104. case _SC_RE_DUP_MAX:
  105. case _SC_TTY_NAME_MAX:
  106. case _SC_EXPR_NEST_MAX:
  107. case _SC_LINE_MAX:
  108. Value = -1;
  109. break;
  110. case _SC_OPEN_MAX:
  111. Value = OB_MAX_HANDLES;
  112. break;
  113. case _SC_STREAM_MAX:
  114. Value = INT_MAX;
  115. break;
  116. case _SC_SYMLOOP_MAX:
  117. Value = MAX_SYMBOLIC_LINK_RECURSION;
  118. break;
  119. case _SC_TZNAME_MAX:
  120. Value = _POSIX_TZNAME_MAX;
  121. break;
  122. case _SC_VERSION:
  123. Value = _POSIX_VERSION;
  124. break;
  125. case _SC_2_VERSION:
  126. Value = _POSIX2_VERSION;
  127. break;
  128. case _SC_2_LOCALEDEF:
  129. Value = _POSIX2_LOCALEDEF;
  130. break;
  131. case _SC_2_SW_DEV:
  132. Value = _POSIX2_SW_DEV;
  133. break;
  134. case _SC_2_C_DEV:
  135. Value = _POSIX2_C_DEV;
  136. break;
  137. case _SC_BC_BASE_MAX:
  138. case _SC_BC_SCALE_MAX:
  139. Value = 99;
  140. break;
  141. case _SC_BC_DIM_MAX:
  142. Value = 2048;
  143. break;
  144. case _SC_BC_STRING_MAX:
  145. Value = 1000;
  146. break;
  147. case _SC_COLL_WEIGHTS_MAX:
  148. Value = -1;
  149. break;
  150. case _SC_2_FORT_DEV:
  151. Value = _POSIX2_FORT_DEV;
  152. break;
  153. case _SC_2_FORT_RUN:
  154. Value = _POSIX2_FORT_RUN;
  155. break;
  156. case _SC_NPROCESSORS_CONF:
  157. case _SC_NPROCESSORS_ONLN:
  158. Size = sizeof(PROCESSOR_COUNT_INFORMATION);
  159. Status = OsGetSetSystemInformation(SystemInformationKe,
  160. KeInformationProcessorCount,
  161. &ProcessorCountInformation,
  162. &Size,
  163. FALSE);
  164. if (!KSUCCESS(Status)) {
  165. errno = ClConvertKstatusToErrorNumber(Status);
  166. return -1;
  167. }
  168. if (Variable == _SC_NPROCESSORS_CONF) {
  169. Value = ProcessorCountInformation.MaxProcessorCount;
  170. } else if (Variable == _SC_NPROCESSORS_ONLN) {
  171. Value = ProcessorCountInformation.ActiveProcessorCount;
  172. } else {
  173. assert(FALSE);
  174. Value = -1;
  175. }
  176. break;
  177. case _SC_GETGR_R_SIZE_MAX:
  178. Value = USER_DATABASE_LINE_MAX;
  179. break;
  180. case _SC_GETPW_R_SIZE_MAX:
  181. Value = USER_DATABASE_LINE_MAX;
  182. break;
  183. case _SC_NGROUPS_MAX:
  184. Value = NGROUPS_MAX;
  185. break;
  186. case _SC_BARRIERS:
  187. Value = _POSIX_BARRIERS;
  188. break;
  189. case _SC_CLOCK_SELECTION:
  190. Value = _POSIX_CLOCK_SELECTION;
  191. break;
  192. case _SC_CPUTIME:
  193. Value = _POSIX_CPUTIME;
  194. break;
  195. case _SC_FSYNC:
  196. Value = _POSIX_FSYNC;
  197. break;
  198. case _SC_IPV6:
  199. Value = _POSIX_IPV6;
  200. break;
  201. case _SC_JOB_CONTROL:
  202. Value = _POSIX_JOB_CONTROL;
  203. break;
  204. case _SC_MAPPED_FILES:
  205. Value = _POSIX_MAPPED_FILES;
  206. break;
  207. case _SC_MEMLOCK:
  208. Value = _POSIX_MEMLOCK;
  209. break;
  210. case _SC_MEMLOCK_RANGE:
  211. Value = _POSIX_MEMLOCK_RANGE;
  212. break;
  213. case _SC_MEMORY_PROTECTION:
  214. Value = _POSIX_MEMORY_PROTECTION;
  215. break;
  216. case _SC_MESSAGE_PASSING:
  217. Value = _POSIX_MESSAGE_PASSING;
  218. break;
  219. case _SC_MONOTONIC_CLOCK:
  220. Value = _POSIX_MONOTONIC_CLOCK;
  221. break;
  222. case _SC_PRIORITIZED_IO:
  223. Value = _POSIX_PRIORITIZED_IO;
  224. break;
  225. case _SC_PRIORITY_SCHEDULING:
  226. Value = _POSIX_PRIORITY_SCHEDULING;
  227. break;
  228. case _SC_RAW_SOCKETS:
  229. Value = _POSIX_RAW_SOCKETS;
  230. break;
  231. case _SC_READER_WRITER_LOCKS:
  232. Value = _POSIX_READER_WRITER_LOCKS;
  233. break;
  234. case _SC_REALTIME_SIGNALS:
  235. Value = _POSIX_REALTIME_SIGNALS;
  236. break;
  237. case _SC_REGEXP:
  238. Value = _POSIX_REGEXP;
  239. break;
  240. case _SC_SAVED_IDS:
  241. Value = _POSIX_SAVED_IDS;
  242. break;
  243. case _SC_SEMAPHORES:
  244. Value = _POSIX_SEMAPHORES;
  245. break;
  246. case _SC_SHARED_MEMORY_OBJECTS:
  247. Value = _POSIX_SHARED_MEMORY_OBJECTS;
  248. break;
  249. case _SC_SHELL:
  250. Value = _POSIX_SHELL;
  251. break;
  252. case _SC_SPAWN:
  253. Value = _POSIX_SPAWN;
  254. break;
  255. case _SC_SPIN_LOCKS:
  256. Value = _POSIX_SPIN_LOCKS;
  257. break;
  258. case _SC_SPORADIC_SERVER:
  259. Value = _POSIX_SPORADIC_SERVER;
  260. break;
  261. case _SC_SYNCHRONIZED_IO:
  262. Value = _POSIX_SYNCHRONIZED_IO;
  263. break;
  264. case _SC_THREAD_ATTR_STACKADDR:
  265. Value = _POSIX_THREAD_ATTR_STACKADDR;
  266. break;
  267. case _SC_THREAD_ATTR_STACKSIZE:
  268. Value = _POSIX_THREAD_ATTR_STACKSIZE;
  269. break;
  270. case _SC_THREAD_CPUTIME:
  271. Value = _POSIX_THREAD_CPUTIME;
  272. break;
  273. case _SC_THREAD_PRIO_INHERIT:
  274. Value = _POSIX_THREAD_PRIO_INHERIT;
  275. break;
  276. case _SC_THREAD_PRIO_PROTECT:
  277. Value = _POSIX_THREAD_PRIO_PROTECT;
  278. break;
  279. case _SC_THREAD_PRIORITY_SCHEDULING:
  280. Value = _POSIX_THREAD_PRIORITY_SCHEDULING;
  281. break;
  282. case _SC_THREAD_PROCESS_SHARED:
  283. Value = _POSIX_THREAD_PROCESS_SHARED;
  284. break;
  285. case _SC_THREAD_ROBUST_PRIO_INHERIT:
  286. Value = _POSIX_THREAD_ROBUST_PRIO_INHERIT;
  287. break;
  288. case _SC_THREAD_ROBUST_PRIO_PROTECT:
  289. Value = _POSIX_THREAD_ROBUST_PRIO_PROTECT;
  290. break;
  291. case _SC_THREAD_SAFE_FUNCTIONS:
  292. Value = _POSIX_THREAD_SAFE_FUNCTIONS;
  293. break;
  294. case _SC_THREAD_SPORADIC_SERVER:
  295. Value = _POSIX_THREAD_SPORADIC_SERVER;
  296. break;
  297. case _SC_THREADS:
  298. Value = _POSIX_THREADS;
  299. break;
  300. case _SC_TIMEOUTS:
  301. Value = _POSIX_TIMEOUTS;
  302. break;
  303. case _SC_TIMERS:
  304. Value = _POSIX_TIMERS;
  305. break;
  306. case _SC_TRACE:
  307. Value = _POSIX_TRACE;
  308. break;
  309. case _SC_TRACE_EVENT_FILTER:
  310. Value = _POSIX_TRACE_EVENT_FILTER;
  311. break;
  312. case _SC_TRACE_INHERIT:
  313. Value = _POSIX_TRACE_INHERIT;
  314. break;
  315. case _SC_TRACE_LOG:
  316. Value = _POSIX_TRACE_LOG;
  317. break;
  318. case _SC_TYPED_MEMORY_OBJECTS:
  319. Value = _POSIX_TYPED_MEMORY_OBJECTS;
  320. break;
  321. case _SC_V6_ILP32_OFF32:
  322. Value = _POSIX_V6_ILP32_OFF32;
  323. break;
  324. case _SC_V6_ILP32_OFFBIG:
  325. Value = _POSIX_V6_ILP32_OFFBIG;
  326. break;
  327. case _SC_V6_LP64_OFF64:
  328. Value = _POSIX_V6_LP64_OFF64;
  329. break;
  330. case _SC_V6_LPBIG_OFFBIG:
  331. Value = _POSIX_V6_LPBIG_OFFBIG;
  332. break;
  333. case _SC_V7_ILP32_OFF32:
  334. Value = _POSIX_V7_ILP32_OFF32;
  335. break;
  336. case _SC_V7_ILP32_OFFBIG:
  337. Value = _POSIX_V7_ILP32_OFFBIG;
  338. break;
  339. case _SC_V7_LP64_OFF64:
  340. Value = _POSIX_V7_LP64_OFF64;
  341. break;
  342. case _SC_V7_LPBIG_OFFBIG:
  343. Value = _POSIX_V7_LPBIG_OFFBIG;
  344. break;
  345. case _SC_2_C_BIND:
  346. Value = _POSIX2_C_BIND;
  347. break;
  348. case _SC_2_CHAR_TERM:
  349. Value = _POSIX2_CHAR_TERM;
  350. break;
  351. case _SC_2_PBS:
  352. Value = _POSIX2_PBS;
  353. break;
  354. case _SC_2_PBS_ACCOUNTING:
  355. Value = _POSIX2_PBS_ACCOUNTING;
  356. break;
  357. case _SC_2_PBS_CHECKPOINT:
  358. Value = _POSIX2_PBS_CHECKPOINT;
  359. break;
  360. case _SC_2_PBS_LOCATE:
  361. Value = _POSIX2_PBS_LOCATE;
  362. break;
  363. case _SC_2_PBS_MESSAGE:
  364. Value = _POSIX2_PBS_MESSAGE;
  365. break;
  366. case _SC_2_PBS_TRACK:
  367. Value = _POSIX2_PBS_TRACK;
  368. break;
  369. case _SC_2_UPE:
  370. Value = _POSIX2_UPE;
  371. break;
  372. case _SC_XOPEN_CRYPT:
  373. Value = _XOPEN_CRYPT;
  374. break;
  375. case _SC_XOPEN_ENH_I18N:
  376. Value = _XOPEN_ENH_I18N;
  377. break;
  378. case _SC_XOPEN_REALTIME:
  379. Value = _XOPEN_REALTIME;
  380. break;
  381. case _SC_XOPEN_REALTIME_THREADS:
  382. Value = _XOPEN_REALTIME_THREADS;
  383. break;
  384. case _SC_XOPEN_SHM:
  385. Value = _XOPEN_SHM;
  386. break;
  387. case _SC_XOPEN_STREAMS:
  388. Value = _XOPEN_STREAMS;
  389. break;
  390. case _SC_XOPEN_UNIX:
  391. Value = _XOPEN_UNIX;
  392. break;
  393. case _SC_XOPEN_UUCP:
  394. Value = _XOPEN_UUCP;
  395. break;
  396. case _SC_XOPEN_VERSION:
  397. Value = _XOPEN_VERSION;
  398. break;
  399. default:
  400. fprintf(stderr, "SYSCONF called with unknown variable %d.\n", Variable);
  401. assert(FALSE);
  402. Value = -1;
  403. errno = EINVAL;
  404. break;
  405. }
  406. return Value;
  407. }
  408. LIBC_API
  409. long
  410. fpathconf (
  411. int FileDescriptor,
  412. int Variable
  413. )
  414. /*++
  415. Routine Description:
  416. This routine gets the current value of a configurable limit or option
  417. that is associated with the given open file descriptor.
  418. Arguments:
  419. FileDescriptor - Supplies the file descriptor to query the value for.
  420. Variable - Supplies the variable to get. See _PC_* definitions.
  421. Return Value:
  422. Returns the value for that variable.
  423. -1 if the variable has no limit. The errno variable will be left unchanged.
  424. -1 if the variable was invalid, and errno will be set to EINVAL.
  425. --*/
  426. {
  427. long Result;
  428. //
  429. // TODO: Return real fpathconf information at some point.
  430. //
  431. switch (Variable) {
  432. case _PC_2_SYMLINKS:
  433. Result = 1;
  434. break;
  435. case _PC_ALLOC_SIZE_MIN:
  436. Result = 4096;
  437. break;
  438. case _PC_ASYNC_IO:
  439. Result = 0;
  440. break;
  441. case _PC_CHOWN_RESTRICTED:
  442. Result = _POSIX_CHOWN_RESTRICTED;
  443. break;
  444. case _PC_FILESIZEBITS:
  445. Result = 32;
  446. break;
  447. case _PC_LINK_MAX:
  448. Result = _POSIX_LINK_MAX;
  449. break;
  450. case _PC_MAX_CANON:
  451. Result = MAX_CANON;
  452. break;
  453. case _PC_MAX_INPUT:
  454. Result = MAX_INPUT;
  455. break;
  456. case _PC_NAME_MAX:
  457. Result = NAME_MAX;
  458. break;
  459. case _PC_NO_TRUNC:
  460. Result = _POSIX_NO_TRUNC;
  461. break;
  462. case _PC_PATH_MAX:
  463. Result = PATH_MAX;
  464. break;
  465. case _PC_PIPE_BUF:
  466. Result = PIPE_BUF;
  467. break;
  468. case _PC_PRIO_IO:
  469. Result = 0;
  470. break;
  471. case _PC_REC_INCR_XFER_SIZE:
  472. Result = 4096;
  473. break;
  474. case _PC_REC_MIN_XFER_SIZE:
  475. Result = 4096;
  476. break;
  477. case _PC_REC_XFER_ALIGN:
  478. Result = 4096;
  479. break;
  480. case _PC_SYMLINK_MAX:
  481. Result = _POSIX_SYMLINK_MAX;
  482. break;
  483. case _PC_SYNC_IO:
  484. Result = 1;
  485. break;
  486. case _PC_VDISABLE:
  487. Result = _POSIX_VDISABLE;
  488. break;
  489. default:
  490. Result = -1;
  491. errno = EINVAL;
  492. break;
  493. }
  494. return Result;
  495. }
  496. LIBC_API
  497. long
  498. pathconf (
  499. const char *Path,
  500. int Variable
  501. )
  502. /*++
  503. Routine Description:
  504. This routine gets the current value of a configurable limit or option
  505. that is associated with the given file or directory path.
  506. Arguments:
  507. Path - Supplies a pointer to a null terminated string containing the file
  508. or directory path to get the configuration limit for.
  509. Variable - Supplies the variable to get. See _PC_* definitions.
  510. Return Value:
  511. Returns the value for that variable.
  512. -1 if the variable has no limit. The errno variable will be left unchanged.
  513. -1 if the variable was invalid, and errno will be set to EINVAL.
  514. --*/
  515. {
  516. //
  517. // TODO: Return real pathconf information eventually.
  518. //
  519. return fpathconf(-1, Variable);
  520. }
  521. LIBC_API
  522. int
  523. getdtablesize (
  524. void
  525. )
  526. /*++
  527. Routine Description:
  528. This routine returns the maximum number of file descriptors that are
  529. supported.
  530. Arguments:
  531. None.
  532. Return Value:
  533. Returns the maximum number of file descriptors that one process can have
  534. open.
  535. --*/
  536. {
  537. return OB_MAX_HANDLES;
  538. }
  539. LIBC_API
  540. int
  541. getpagesize (
  542. void
  543. )
  544. /*++
  545. Routine Description:
  546. This routine returns the number of bytes in the basic unit of memory
  547. allocation on the current machine, the page. This routine is provided for
  548. historical reasons, new applications should use sysconf(_SC_PAGESIZE)
  549. (which is in fact exactly what this routine turns around and does).
  550. Arguments:
  551. None.
  552. Return Value:
  553. Returns the number of bytes in a memory page.
  554. --*/
  555. {
  556. return sysconf(_SC_PAGESIZE);
  557. }
  558. //
  559. // --------------------------------------------------------- Internal Functions
  560. //