aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/getconf.c
blob: 56324730bdbbe6bcc0d1f2f1f3d3d08cf94c9846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* getconf.c - get configuration values
 *
 * Copyright 2017 Rob Landley <rob@landley.net>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.c
 *
 * Deviations from posix: no -v because nothing says what it should DO.
 * Added -l, what symbols should be included is a bit unclear

USE_GETCONF(NEWTOY(getconf, ">2l", TOYFLAG_USR|TOYFLAG_BIN))

config GETCONF
  bool "getconf"
  default y
  help
    usage: getconf -l | NAME [PATH]

    Get system configuration values. Values from pathconf(3) require a path.

    -l	List available value names (grouped by source)
*/

#define FOR_getconf
#include "toys.h"
#include <limits.h>

// This is missing on glibc (bionic has it).
#ifndef _SC_XOPEN_UUCP
#define _SC_XOPEN_UUCP -1
#endif

struct config {
  char *name;
  long long value;
};

// Lists of symbols getconf can query, broken down by whether we call sysconf(),
// confstr(), or output the macro value directly.

// Probe the live system
struct config sysconfs[] = {
  /* POSIX */
#define CONF(n) {"_POSIX_" #n,_SC_ ## n}
  CONF(ADVISORY_INFO), CONF(BARRIERS), CONF(ASYNCHRONOUS_IO),
  CONF(CLOCK_SELECTION), CONF(CPUTIME), CONF(FSYNC), CONF(IPV6),
  CONF(JOB_CONTROL), CONF(MAPPED_FILES), CONF(MEMLOCK), CONF(MEMLOCK_RANGE),
  CONF(MEMORY_PROTECTION), CONF(MESSAGE_PASSING), CONF(MONOTONIC_CLOCK),
  CONF(PRIORITY_SCHEDULING), CONF(RAW_SOCKETS), CONF(READER_WRITER_LOCKS),
  CONF(REALTIME_SIGNALS), CONF(REGEXP), CONF(SAVED_IDS), CONF(SEMAPHORES),
  CONF(SHARED_MEMORY_OBJECTS), CONF(SHELL), CONF(SPAWN), CONF(SPIN_LOCKS),
  CONF(SPORADIC_SERVER), CONF(SS_REPL_MAX), CONF(SYNCHRONIZED_IO),
  CONF(THREAD_ATTR_STACKADDR), CONF(THREAD_ATTR_STACKSIZE),
  CONF(THREAD_CPUTIME), CONF(THREAD_PRIO_INHERIT), CONF(THREAD_PRIO_PROTECT),
  CONF(THREAD_PRIORITY_SCHEDULING), CONF(THREAD_PROCESS_SHARED),
  CONF(THREAD_ROBUST_PRIO_INHERIT), CONF(THREAD_ROBUST_PRIO_PROTECT),
  CONF(THREAD_SAFE_FUNCTIONS), CONF(THREAD_SPORADIC_SERVER), CONF(THREADS),
  CONF(TIMEOUTS), CONF(TIMERS), CONF(TRACE), CONF(TRACE_EVENT_FILTER),
  CONF(TRACE_EVENT_NAME_MAX), CONF(TRACE_INHERIT), CONF(TRACE_LOG),
  CONF(TRACE_NAME_MAX), CONF(TRACE_SYS_MAX), CONF(TRACE_USER_EVENT_MAX),
  CONF(TYPED_MEMORY_OBJECTS), CONF(VERSION), CONF(V7_ILP32_OFF32),
  CONF(V7_ILP32_OFFBIG), CONF(V7_LP64_OFF64), CONF(V7_LPBIG_OFFBIG),

  /* POSIX.2 */
#undef CONF
#define CONF(n) {"POSIX2_" #n,_SC_2_ ## n}
  CONF(C_BIND), CONF(C_DEV), CONF(CHAR_TERM), CONF(FORT_DEV), CONF(FORT_RUN),
  CONF(LOCALEDEF), CONF(PBS), CONF(PBS_ACCOUNTING), CONF(PBS_CHECKPOINT),
  CONF(PBS_LOCATE), CONF(PBS_MESSAGE), CONF(PBS_TRACK), CONF(SW_DEV),
  CONF(UPE), CONF(VERSION),

  /* X/Open */
#undef CONF
#define CONF(n) {"_XOPEN_" #n,_SC_XOPEN_ ## n}
  CONF(CRYPT), CONF(ENH_I18N), CONF(REALTIME), CONF(REALTIME_THREADS),
  CONF(SHM), CONF(STREAMS), CONF(UNIX), CONF(UUCP), CONF(VERSION),

  /* No obvious standard */
#undef CONF
#define CONF(n) {#n,_SC_ ## n}
  CONF(AIO_LISTIO_MAX), CONF(AIO_MAX), CONF(AIO_PRIO_DELTA_MAX), CONF(ARG_MAX),
  CONF(ATEXIT_MAX), CONF(BC_BASE_MAX), CONF(BC_DIM_MAX), CONF(BC_SCALE_MAX),
  CONF(BC_STRING_MAX), CONF(CHILD_MAX), CONF(CLK_TCK), CONF(COLL_WEIGHTS_MAX),
  CONF(DELAYTIMER_MAX), CONF(EXPR_NEST_MAX), CONF(HOST_NAME_MAX),
  CONF(IOV_MAX), CONF(LINE_MAX), CONF(LOGIN_NAME_MAX), CONF(NGROUPS_MAX),
  CONF(MQ_OPEN_MAX), CONF(MQ_PRIO_MAX), CONF(OPEN_MAX), CONF(PAGE_SIZE),
  CONF(PAGESIZE), CONF(RAW_SOCKETS), CONF(RE_DUP_MAX), CONF(RTSIG_MAX),
  CONF(SEM_NSEMS_MAX), CONF(SEM_VALUE_MAX), CONF(SIGQUEUE_MAX),
  CONF(STREAM_MAX), CONF(SYMLOOP_MAX), CONF(TIMER_MAX), CONF(TTY_NAME_MAX),
  CONF(TZNAME_MAX),

  /* Names that just don't match the symbol, do it by hand */
  {"_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, {"_PHYS_PAGES", _SC_PHYS_PAGES},
  {"_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
  {"_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},

  /* There's a weird "PTHREAD" vs "THREAD" mismatch here. */
  {"PTHREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
  {"PTHREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
  {"PTHREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
  {"PTHREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
};

// Probe the live system with a path
struct config pathconfs[] = {
#undef CONF
#define CONF(n) {#n,_PC_ ## n}
  CONF(ASYNC_IO), CONF(CHOWN_RESTRICTED), CONF(FILESIZEBITS), CONF(LINK_MAX),
  CONF(MAX_CANON), CONF(MAX_INPUT), CONF(NAME_MAX), CONF(NO_TRUNC),
  CONF(PATH_MAX), CONF(PIPE_BUF), CONF(PRIO_IO), CONF(SYMLINK_MAX),
  CONF(SYNC_IO),
  {"_POSIX_VDISABLE", _PC_VDISABLE},
};

// Strings out of a header
struct config confstrs[] = {
#undef CONF
#define CONF(n) {#n,_CS_ ## n}
  CONF(PATH), CONF(V7_ENV)
};

// Integers out of a header
struct config limits[] = {
#undef CONF
#define CONF(n) {#n,n}
  CONF(_POSIX_AIO_LISTIO_MAX), CONF(_POSIX_AIO_MAX), CONF(_POSIX_ARG_MAX),
  CONF(_POSIX_CHILD_MAX), CONF(_POSIX_DELAYTIMER_MAX),
  CONF(_POSIX_HOST_NAME_MAX), CONF(_POSIX_LINK_MAX),
  CONF(_POSIX_LOGIN_NAME_MAX), CONF(_POSIX_MAX_CANON),
  CONF(_POSIX_MAX_INPUT), CONF(_POSIX_NAME_MAX), CONF(_POSIX_NGROUPS_MAX),
  CONF(_POSIX_OPEN_MAX), CONF(_POSIX_PATH_MAX), CONF(_POSIX_PIPE_BUF),
  CONF(_POSIX_RE_DUP_MAX), CONF(_POSIX_RTSIG_MAX), CONF(_POSIX_SEM_NSEMS_MAX),
  CONF(_POSIX_SEM_VALUE_MAX), CONF(_POSIX_SIGQUEUE_MAX), CONF(_POSIX_SSIZE_MAX),
  CONF(_POSIX_STREAM_MAX), CONF(_POSIX_SYMLINK_MAX), CONF(_POSIX_SYMLOOP_MAX),
  CONF(_POSIX_THREAD_DESTRUCTOR_ITERATIONS), CONF(_POSIX_THREAD_KEYS_MAX),
  CONF(_POSIX_THREAD_THREADS_MAX), CONF(_POSIX_TIMER_MAX),
  CONF(_POSIX_TTY_NAME_MAX), CONF(_POSIX_TZNAME_MAX),
  CONF(CHAR_MAX), CONF(CHAR_MIN), CONF(INT_MAX), CONF(INT_MIN), CONF(SCHAR_MAX),
  CONF(SCHAR_MIN), CONF(SHRT_MAX), CONF(SHRT_MIN), CONF(SSIZE_MAX),
  CONF(UCHAR_MAX), CONF(UINT_MAX), CONF(ULONG_MAX), CONF(USHRT_MAX),
  CONF(UIO_MAXIOV), CONF(CHAR_BIT),
  /* Not available in glibc without _GNU_SOURCE. */
  {"LONG_BIT", 8*sizeof(long)},
  {"WORD_BIT", 8*sizeof(int)},
#undef CONF
#define CONF(n) {#n,_ ## n}
  CONF(POSIX2_BC_BASE_MAX), CONF(POSIX2_BC_DIM_MAX),
  CONF(POSIX2_BC_SCALE_MAX), CONF(POSIX2_BC_STRING_MAX),
  CONF(POSIX2_CHARCLASS_NAME_MAX), CONF(POSIX2_COLL_WEIGHTS_MAX),
  CONF(POSIX2_EXPR_NEST_MAX), CONF(POSIX2_LINE_MAX), CONF(POSIX2_RE_DUP_MAX),
};

// Names we need to handle ourselves (default to blank but shouldn't error)
struct config others[] = {
  {"LFS_CFLAGS", 0}, {"LFS_LDFLAGS", 0}, {"LFS_LIBS", 0}
};

void getconf_main(void)
{
  struct config *configs[] = {sysconfs, pathconfs, confstrs, limits, others},
    *c = NULL;
  int i, j, lens[] = {ARRAY_LEN(sysconfs), ARRAY_LEN(pathconfs),
    ARRAY_LEN(confstrs), ARRAY_LEN(limits), ARRAY_LEN(others)};
  char *name, *config_names[] = {"sysconf(3)", "pathconf(3)", "confstr(3)",
    "<limits.h>", "Misc"};

  if (toys.optflags&FLAG_l) {
    for (i = 0; i<5; i++) {
      printf("%s\n", config_names[i]);
      for (j = 0; j<lens[i]; j++) printf("  %s\n", configs[i][j].name);
    }
    return;
  }

  if (toys.optc<1) help_exit(0);
  name = *toys.optargs;

  // Workaround for autogen using CS_PATH instead of PATH
  if (!strcmp("CS_PATH", name)) name += 3;

  // Find the config.
  for (i = j = 0; ; j++) {
    if (j==lens[i]) j = 0, i++;
    if (i==5) error_exit("bad '%s'", name);
    c = &configs[i][j];
    if (!strcmp(c->name, name)) break;
  }

  // Check that we do/don't have the extra path argument.
  if (i==1) {
    if (toys.optc!=2) help_exit("%s needs a path", name);
  } else if (toys.optc!=1) help_exit("%s does not take a path", name);

  if (i<2) {
    long l = i ? pathconf(toys.optargs[1], c->value) : sysconf(c->value);

    if (l == -1) puts("undefined");
    else printf("%ld\n", l);
  } else if (i==1) printf("%ld\n", pathconf(toys.optargs[1], c->value));
  else if (i==2) {
    confstr(c->value, toybuf, sizeof(toybuf));
    puts(toybuf);
  } else if (i==3) printf("%lld\n", c->value);
  // LFS_CFLAGS on 32 bit should enable Large File Support (kernel build cares)
  else if (sizeof(long)==4 && !j)
    puts("-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64");
}