getconf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*-
  2. * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to The NetBSD Foundation
  6. * by J.T. Conklin.
  7. *
  8. * Mostly rewritten to be used in Alpine Linux (with musl c-library)
  9. * by Timo Teräs.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <err.h>
  33. #include <errno.h>
  34. #include <values.h>
  35. #include <limits.h>
  36. #include <locale.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. struct conf_variable {
  42. const char *name;
  43. enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT, UCONSTANT, NUM_TYPES } type;
  44. long value;
  45. };
  46. static const struct conf_variable conf_table[] = {
  47. { "PATH", CONFSTR, _CS_PATH },
  48. /* Utility Limit Minimum Values */
  49. { "POSIX2_BC_BASE_MAX", CONSTANT, _POSIX2_BC_BASE_MAX },
  50. { "POSIX2_BC_DIM_MAX", CONSTANT, _POSIX2_BC_DIM_MAX },
  51. { "POSIX2_BC_SCALE_MAX", CONSTANT, _POSIX2_BC_SCALE_MAX },
  52. { "POSIX2_BC_STRING_MAX", CONSTANT, _POSIX2_BC_STRING_MAX },
  53. { "POSIX2_COLL_WEIGHTS_MAX", CONSTANT, _POSIX2_COLL_WEIGHTS_MAX },
  54. { "POSIX2_EXPR_NEST_MAX", CONSTANT, _POSIX2_EXPR_NEST_MAX },
  55. { "POSIX2_LINE_MAX", CONSTANT, _POSIX2_LINE_MAX },
  56. { "POSIX2_RE_DUP_MAX", CONSTANT, _POSIX2_RE_DUP_MAX },
  57. { "POSIX2_VERSION", CONSTANT, _POSIX2_VERSION },
  58. /* POSIX.1 Minimum Values */
  59. { "_POSIX_AIO_LISTIO_MAX", CONSTANT, _POSIX_AIO_LISTIO_MAX },
  60. { "_POSIX_AIO_MAX", CONSTANT, _POSIX_AIO_MAX },
  61. { "_POSIX_ARG_MAX", CONSTANT, _POSIX_ARG_MAX },
  62. { "_POSIX_CHILD_MAX", CONSTANT, _POSIX_CHILD_MAX },
  63. { "_POSIX_LINK_MAX", CONSTANT, _POSIX_LINK_MAX },
  64. { "_POSIX_MAX_CANON", CONSTANT, _POSIX_MAX_CANON },
  65. { "_POSIX_MAX_INPUT", CONSTANT, _POSIX_MAX_INPUT },
  66. { "_POSIX_MQ_OPEN_MAX", CONSTANT, _POSIX_MQ_OPEN_MAX },
  67. { "_POSIX_MQ_PRIO_MAX", CONSTANT, _POSIX_MQ_PRIO_MAX },
  68. { "_POSIX_NAME_MAX", CONSTANT, _POSIX_NAME_MAX },
  69. { "_POSIX_NGROUPS_MAX", CONSTANT, _POSIX_NGROUPS_MAX },
  70. { "_POSIX_OPEN_MAX", CONSTANT, _POSIX_OPEN_MAX },
  71. { "_POSIX_PATH_MAX", CONSTANT, _POSIX_PATH_MAX },
  72. { "_POSIX_PIPE_BUF", CONSTANT, _POSIX_PIPE_BUF },
  73. { "_POSIX_SSIZE_MAX", CONSTANT, _POSIX_SSIZE_MAX },
  74. { "_POSIX_STREAM_MAX", CONSTANT, _POSIX_STREAM_MAX },
  75. { "_POSIX_TZNAME_MAX", CONSTANT, _POSIX_TZNAME_MAX },
  76. /* Symbolic Utility Limits */
  77. { "BC_BASE_MAX", SYSCONF, _SC_BC_BASE_MAX },
  78. { "BC_DIM_MAX", SYSCONF, _SC_BC_DIM_MAX },
  79. { "BC_SCALE_MAX", SYSCONF, _SC_BC_SCALE_MAX },
  80. { "BC_STRING_MAX", SYSCONF, _SC_BC_STRING_MAX },
  81. { "COLL_WEIGHTS_MAX", SYSCONF, _SC_COLL_WEIGHTS_MAX },
  82. { "EXPR_NEST_MAX", SYSCONF, _SC_EXPR_NEST_MAX },
  83. { "LINE_MAX", SYSCONF, _SC_LINE_MAX },
  84. { "RE_DUP_MAX", SYSCONF, _SC_RE_DUP_MAX },
  85. /* Optional Facility Configuration Values */
  86. { "_POSIX2_C_BIND", SYSCONF, _SC_2_C_BIND },
  87. { "POSIX2_C_DEV", SYSCONF, _SC_2_C_DEV },
  88. { "POSIX2_CHAR_TERM", SYSCONF, _SC_2_CHAR_TERM },
  89. { "POSIX2_FORT_DEV", SYSCONF, _SC_2_FORT_DEV },
  90. { "POSIX2_FORT_RUN", SYSCONF, _SC_2_FORT_RUN },
  91. { "POSIX2_LOCALEDEF", SYSCONF, _SC_2_LOCALEDEF },
  92. { "POSIX2_SW_DEV", SYSCONF, _SC_2_SW_DEV },
  93. { "POSIX2_UPE", SYSCONF, _SC_2_UPE },
  94. /* POSIX.1 Configurable System Variables */
  95. { "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX },
  96. { "AIO_MAX", SYSCONF, _SC_AIO_MAX },
  97. { "ARG_MAX", SYSCONF, _SC_ARG_MAX },
  98. { "CHILD_MAX", SYSCONF, _SC_CHILD_MAX },
  99. { "CLK_TCK", SYSCONF, _SC_CLK_TCK },
  100. { "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX },
  101. { "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX },
  102. { "NGROUPS_MAX", SYSCONF, _SC_NGROUPS_MAX },
  103. { "OPEN_MAX", SYSCONF, _SC_OPEN_MAX },
  104. { "STREAM_MAX", SYSCONF, _SC_STREAM_MAX },
  105. { "TZNAME_MAX", SYSCONF, _SC_TZNAME_MAX },
  106. { "_POSIX_JOB_CONTROL", SYSCONF, _SC_JOB_CONTROL },
  107. { "_POSIX_SAVED_IDS", SYSCONF, _SC_SAVED_IDS },
  108. { "_POSIX_VERSION", SYSCONF, _SC_VERSION },
  109. { "LINK_MAX", PATHCONF, _PC_LINK_MAX },
  110. { "MAX_CANON", PATHCONF, _PC_MAX_CANON },
  111. { "MAX_INPUT", PATHCONF, _PC_MAX_INPUT },
  112. { "NAME_MAX", PATHCONF, _PC_NAME_MAX },
  113. { "PATH_MAX", PATHCONF, _PC_PATH_MAX },
  114. { "PIPE_BUF", PATHCONF, _PC_PIPE_BUF },
  115. { "_POSIX_CHOWN_RESTRICTED", PATHCONF, _PC_CHOWN_RESTRICTED },
  116. { "_POSIX_NO_TRUNC", PATHCONF, _PC_NO_TRUNC },
  117. { "_POSIX_VDISABLE", PATHCONF, _PC_VDISABLE },
  118. /* POSIX.1b Configurable System Variables */
  119. { "PAGESIZE", SYSCONF, _SC_PAGESIZE },
  120. { "_POSIX_ASYNCHRONOUS_IO", SYSCONF, _SC_ASYNCHRONOUS_IO },
  121. { "_POSIX_FSYNC", SYSCONF, _SC_FSYNC },
  122. { "_POSIX_MAPPED_FILES", SYSCONF, _SC_MAPPED_FILES },
  123. { "_POSIX_MEMLOCK", SYSCONF, _SC_MEMLOCK },
  124. { "_POSIX_MEMLOCK_RANGE", SYSCONF, _SC_MEMLOCK_RANGE },
  125. { "_POSIX_MEMORY_PROTECTION", SYSCONF, _SC_MEMORY_PROTECTION },
  126. { "_POSIX_MESSAGE_PASSING", SYSCONF, _SC_MESSAGE_PASSING },
  127. { "_POSIX_MONOTONIC_CLOCK", SYSCONF, _SC_MONOTONIC_CLOCK },
  128. { "_POSIX_PRIORITY_SCHEDULING", SYSCONF, _SC_PRIORITY_SCHEDULING },
  129. { "_POSIX_SEMAPHORES", SYSCONF, _SC_SEMAPHORES },
  130. { "_POSIX_SHARED_MEMORY_OBJECTS", SYSCONF, _SC_SHARED_MEMORY_OBJECTS },
  131. { "_POSIX_SYNCHRONIZED_IO", SYSCONF, _SC_SYNCHRONIZED_IO },
  132. { "_POSIX_TIMERS", SYSCONF, _SC_TIMERS },
  133. { "_POSIX_SYNC_IO", PATHCONF, _PC_SYNC_IO },
  134. /* POSIX.1c Configurable System Variables */
  135. { "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX },
  136. { "_POSIX_THREADS", SYSCONF, _SC_THREADS },
  137. /* POSIX.1j Configurable System Variables */
  138. { "_POSIX_BARRIERS", SYSCONF, _SC_BARRIERS },
  139. { "_POSIX_READER_WRITER_LOCKS", SYSCONF, _SC_READER_WRITER_LOCKS },
  140. { "_POSIX_SPIN_LOCKS", SYSCONF, _SC_SPIN_LOCKS },
  141. /* XPG4.2 Configurable System Variables */
  142. { "IOV_MAX", SYSCONF, _SC_IOV_MAX },
  143. { "PAGE_SIZE", SYSCONF, _SC_PAGE_SIZE },
  144. { "_XOPEN_SHM", SYSCONF, _SC_XOPEN_SHM },
  145. /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */
  146. { "FILESIZEBITS", PATHCONF, _PC_FILESIZEBITS },
  147. /* POSIX.1-2001 XSI Option Group Configurable System Variables */
  148. { "ATEXIT_MAX", SYSCONF, _SC_ATEXIT_MAX },
  149. /* POSIX.1-2001 TSF Configurable System Variables */
  150. { "GETGR_R_SIZE_MAX", SYSCONF, _SC_GETGR_R_SIZE_MAX },
  151. { "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX },
  152. /* Commonly provided extensions */
  153. { "_PHYS_PAGES", SYSCONF, _SC_PHYS_PAGES },
  154. { "_AVPHYS_PAGES", SYSCONF, _SC_AVPHYS_PAGES },
  155. { "_NPROCESSORS_CONF", SYSCONF, _SC_NPROCESSORS_CONF },
  156. { "_NPROCESSORS_ONLN", SYSCONF, _SC_NPROCESSORS_ONLN },
  157. /* Data type related extensions */
  158. { "CHAR_BIT", CONSTANT, CHAR_BIT },
  159. { "CHAR_MAX", CONSTANT, CHAR_MAX },
  160. { "CHAR_MIN", CONSTANT, CHAR_MIN },
  161. { "INT_MAX", CONSTANT, INT_MAX },
  162. { "INT_MIN", CONSTANT, INT_MIN },
  163. { "LONG_BIT", CONSTANT, LONG_BIT },
  164. { "LONG_MAX", CONSTANT, LONG_MAX },
  165. { "LONG_MIN", CONSTANT, LONG_MIN },
  166. { "SCHAR_MAX", CONSTANT, SCHAR_MAX },
  167. { "SCHAR_MIN", CONSTANT, SCHAR_MIN },
  168. { "SHRT_MAX", CONSTANT, SHRT_MAX },
  169. { "SHRT_MIN", CONSTANT, SHRT_MIN },
  170. { "SSIZE_MAX", CONSTANT, SSIZE_MAX },
  171. { "UCHAR_MAX", UCONSTANT, (long) UCHAR_MAX },
  172. { "UINT_MAX", UCONSTANT, (long) UINT_MAX },
  173. { "ULONG_MAX", UCONSTANT, (long) ULONG_MAX },
  174. { "USHRT_MAX", UCONSTANT, (long) USHRT_MAX },
  175. { "WORD_BIT", CONSTANT, WORD_BIT },
  176. { NULL, CONSTANT, 0L }
  177. };
  178. static int all = 0;
  179. static void usage(const char *p)
  180. {
  181. (void)fprintf(stderr, "Usage: %s system_var\n\t%s -a\n"
  182. "\t%s path_var pathname\n\t%s -a pathname\n", p, p, p, p);
  183. exit(EXIT_FAILURE);
  184. }
  185. static void print_long(const char *name, long val)
  186. {
  187. if (all) printf("%s = %ld\n", name, val);
  188. else printf("%ld\n", val);
  189. }
  190. static void print_ulong(const char *name, unsigned long val)
  191. {
  192. if (all) printf("%s = %lu\n", name, val);
  193. else printf("%lu\n", val);
  194. }
  195. static void print_string(const char *name, const char *val)
  196. {
  197. if (all) printf("%s = %s\n", name, val);
  198. else printf("%s\n", val);
  199. }
  200. static int print_constant(const struct conf_variable *cp, const char *pathname)
  201. {
  202. print_long(cp->name, cp->value);
  203. return 0;
  204. }
  205. static int print_uconstant(const struct conf_variable *cp, const char *pathname)
  206. {
  207. print_ulong(cp->name, (unsigned long) cp->value);
  208. return 0;
  209. }
  210. static int print_sysconf(const struct conf_variable *cp, const char *pathname)
  211. {
  212. long val;
  213. errno = 0;
  214. if ((val = sysconf((int)cp->value)) == -1) {
  215. if (errno != 0) err(EXIT_FAILURE, "sysconf(%ld)", cp->value);
  216. return -1;
  217. }
  218. print_long(cp->name, val);
  219. return 0;
  220. }
  221. static int print_confstr(const struct conf_variable *cp, const char *pathname)
  222. {
  223. size_t len;
  224. char *val;
  225. errno = 0;
  226. if ((len = confstr((int)cp->value, NULL, 0)) == 0) goto error;
  227. if ((val = malloc(len)) == NULL) err(EXIT_FAILURE, "Can't allocate %zu bytes", len);
  228. errno = 0;
  229. if (confstr((int)cp->value, val, len) == 0) goto error;
  230. print_string(cp->name, val);
  231. free(val);
  232. return 0;
  233. error:
  234. if (errno != EINVAL) err(EXIT_FAILURE, "confstr(%ld)", cp->value);
  235. return -1;
  236. }
  237. static int print_pathconf(const struct conf_variable *cp, const char *pathname)
  238. {
  239. long val;
  240. errno = 0;
  241. if ((val = pathconf(pathname, (int)cp->value)) == -1) {
  242. if (all && errno == EINVAL) return 0;
  243. if (errno != 0) err(EXIT_FAILURE, "pathconf(%s, %ld)", pathname, cp->value);
  244. return -1;
  245. }
  246. print_long(cp->name, val);
  247. return 0;
  248. }
  249. typedef int (*handler_t)(const struct conf_variable *cp, const char *pathname);
  250. static const handler_t type_handlers[NUM_TYPES] = {
  251. [SYSCONF] = print_sysconf,
  252. [CONFSTR] = print_confstr,
  253. [PATHCONF] = print_pathconf,
  254. [CONSTANT] = print_constant,
  255. [UCONSTANT] = print_uconstant,
  256. };
  257. int main(int argc, char **argv)
  258. {
  259. const char *progname = argv[0];
  260. const struct conf_variable *cp;
  261. const char *varname, *pathname;
  262. int ch, found = 0;
  263. (void)setlocale(LC_ALL, "");
  264. while ((ch = getopt(argc, argv, "a")) != -1) {
  265. switch (ch) {
  266. case 'a':
  267. all = 1;
  268. break;
  269. case '?':
  270. default:
  271. usage(progname);
  272. }
  273. }
  274. argc -= optind;
  275. argv += optind;
  276. if (!all) {
  277. if (argc == 0)
  278. usage(progname);
  279. varname = argv[0];
  280. argc--;
  281. argv++;
  282. } else
  283. varname = NULL;
  284. if (argc > 1)
  285. usage(progname);
  286. pathname = argv[0]; /* may be NULL */
  287. for (cp = conf_table; cp->name != NULL; cp++) {
  288. if (!all && strcmp(varname, cp->name) != 0) continue;
  289. if ((cp->type == PATHCONF) == (pathname != NULL)) {
  290. if (type_handlers[cp->type](cp, pathname) < 0)
  291. print_string(cp->name, "undefined");
  292. found = 1;
  293. } else if (!all)
  294. errx(EXIT_FAILURE, "%s: invalid variable type", cp->name);
  295. }
  296. if (!all && !found) errx(EXIT_FAILURE, "%s: unknown variable", varname);
  297. (void)fflush(stdout);
  298. return ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
  299. }