[Toybox] [PATCH] getconf: fix glibc NPROCESSORS_ONLN.

enh enh at google.com
Tue Oct 2 14:12:04 PDT 2018


glibc doesn't have _XOPEN_UUCP (though bionic does), which meant that the
generated array of values was out of sync with the hand-written array of
names.

This patch removes that by using a unified array and the preprocessor.

A side benefit of this for me is that it makes toybox easier to integrate
in the AOSP build system (the less shell script magic, the better).
---
 scripts/make.sh      |  36 ---------
 toys/posix/getconf.c | 170 ++++++++++++++++++++++++-------------------
 2 files changed, 94 insertions(+), 112 deletions(-)

diff --git a/scripts/make.sh b/scripts/make.sh
index a268eaf..06dec15 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -261,42 +261,6 @@ then
   generated/config2help Config.in $KCONFIG_CONFIG > generated/help.h || exit 1
 fi

-mksysconf()
-{
-  echo "int ${1}_vals[] = {" &&
-
-  # Extract names, remove blank lines, filter, replace unknown #defines
-  # with UNKNOWN
-  sed -n "/char [*]${1}_names[[]/"',/^}/s/[^"]*"\([^"]*\) *",*/\1\n/pg' \
-    toys/posix/getconf.c | grep -v '^$' | $2 |
-    sed -e "$DEFINES" -e "t;d;a UNKNOWN" | xargs | tr ' ' ',' &&
-  echo '};'
-}
-
-if ! [ generated/getconf.h -nt toys/posix/getconf.c ]
-then
-  echo generated/getconf.h
-
-  # Dump #define list for limits.h and unistd.h, create sed expression to
-  # match known defines
-  DEFINES="$(echo -e '#include <limits.h>\n#include <unistd.h>' | \
-    gcc -E -dM - | \
-    sed -n 's@^#define[ \t][ \t]*\([^ \t(]*\)[ \t].*@s/^\1$/\&/@p' )"
-
-  # Extract limit names, compare against limits.h #defines, replace unknown
-  # ones with UNKNOWN
-
-  {
-    mksysconf sysconf \
-      sed\ 's/^_POSIX2/2/;s/^PTHREAD/THREAD/;s/^_POSIX_//;s/^_XOPEN_/XOPEN_/;s/^/_SC_/'
&&
-    mksysconf confstr sed\ 's/.*/_CS_&/' &&
-    mksysconf limit cat
-  } > generated/getconf.h
-
-  unset HEADERS
-fi
-
-
 [ ! -z "$NOBUILD" ] && exit 0

 echo -n "Compile toybox"
diff --git a/toys/posix/getconf.c b/toys/posix/getconf.c
index a97bc1a..a9ef298 100644
--- a/toys/posix/getconf.c
+++ b/toys/posix/getconf.c
@@ -23,104 +23,120 @@ config GETCONF
 #include "toys.h"
 #include <limits.h>

-// make.sh calls sed on this file (getconf.c) to extract symbols from our three
-// XXX_names arrays and matches them with #defines in limits.h and unistd.h to
-// produce getconf.h, which contains corresponding XXX_values[] arrays
-// with the appropriate constants or -1 for unknown entries.
-
-#define UNKNOWN -1
-#include "generated/getconf.h"
+// This is missing on glibc (bionic has it).
+#ifndef _SC_XOPEN_UUCP
+#define _SC_XOPEN_UUCP -1
+#endif
+
+struct config {
+  char *name;
+  int value;
+};

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

 // Probe the live system
-char *sysconf_names[] = {
+struct config sysconfs[] = {
   /* POSIX */
-  "_POSIX_ADVISORY_INFO", "_POSIX_BARRIERS", "_POSIX_ASYNCHRONOUS_IO",
-  "_POSIX_CLOCK_SELECTION", "_POSIX_CPUTIME", "_POSIX_FSYNC", "_POSIX_IPV6",
-  "_POSIX_JOB_CONTROL", "_POSIX_MAPPED_FILES", "_POSIX_MEMLOCK",
-  "_POSIX_MEMLOCK_RANGE", "_POSIX_MEMORY_PROTECTION", "_POSIX_MESSAGE_PASSING",
-  "_POSIX_MONOTONIC_CLOCK", "_POSIX_PRIORITIZED_IO",
-  "_POSIX_PRIORITY_SCHEDULING", "_POSIX_RAW_SOCKETS",
-  "_POSIX_READER_WRITER_LOCKS", "_POSIX_REALTIME_SIGNALS", "_POSIX_REGEXP",
-  "_POSIX_SAVED_IDS", "_POSIX_SEMAPHORES", "_POSIX_SHARED_MEMORY_OBJECTS",
-  "_POSIX_SHELL", "_POSIX_SPAWN", "_POSIX_SPIN_LOCKS",
"_POSIX_SPORADIC_SERVER",
-  "_POSIX_SS_REPL_MAX", "_POSIX_SYNCHRONIZED_IO",
-  "_POSIX_THREAD_ATTR_STACKADDR", "_POSIX_THREAD_ATTR_STACKSIZE",
-  "_POSIX_THREAD_CPUTIME", "_POSIX_THREAD_PRIO_INHERIT",
-  "_POSIX_THREAD_PRIO_PROTECT", "_POSIX_THREAD_PRIORITY_SCHEDULING",
-  "_POSIX_THREAD_PROCESS_SHARED", "_POSIX_THREAD_ROBUST_PRIO_INHERIT",
-  "_POSIX_THREAD_ROBUST_PRIO_PROTECT", "_POSIX_THREAD_SAFE_FUNCTIONS",
-  "_POSIX_THREAD_SPORADIC_SERVER", "_POSIX_THREADS", "_POSIX_TIMEOUTS",
-  "_POSIX_TIMERS", "_POSIX_TRACE", "_POSIX_TRACE_EVENT_FILTER",
-  "_POSIX_TRACE_EVENT_NAME_MAX", "_POSIX_TRACE_INHERIT", "_POSIX_TRACE_LOG",
-  "_POSIX_TRACE_NAME_MAX", "_POSIX_TRACE_SYS_MAX",
-  "_POSIX_TRACE_USER_EVENT_MAX", "_POSIX_TYPED_MEMORY_OBJECTS",
-  "_POSIX_VERSION", "_POSIX_V7_ILP32_OFF32", "_POSIX_V7_ILP32_OFFBIG",
-  "_POSIX_V7_LP64_OFF64", "_POSIX_V7_LPBIG_OFFBIG",
+#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 */
-  "_POSIX2_C_BIND", "_POSIX2_C_DEV", "_POSIX2_CHAR_TERM", "_POSIX2_FORT_DEV",
-  "_POSIX2_FORT_RUN", "_POSIX2_LOCALEDEF", "_POSIX2_PBS",
-  "_POSIX2_PBS_ACCOUNTING", "_POSIX2_PBS_CHECKPOINT", "_POSIX2_PBS_LOCATE",
-  "_POSIX2_PBS_MESSAGE", "_POSIX2_PBS_TRACK", "_POSIX2_SW_DEV", "_POSIX2_UPE",
-  "_POSIX2_VERSION",
+#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 */
-  "_XOPEN_CRYPT", "_XOPEN_ENH_I18N", "_XOPEN_REALTIME",
-  "_XOPEN_REALTIME_THREADS", "_XOPEN_SHM", "_XOPEN_STREAMS", "_XOPEN_UNIX",
-  "_XOPEN_UUCP", "_XOPEN_VERSION",
+#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 */
-  "AIO_LISTIO_MAX", "AIO_MAX", "AIO_PRIO_DELTA_MAX", "ARG_MAX", "ATEXIT_MAX",
-  "BC_BASE_MAX", "BC_DIM_MAX", "BC_SCALE_MAX", "BC_STRING_MAX", "CHILD_MAX",
-  "COLL_WEIGHTS_MAX", "DELAYTIMER_MAX", "EXPR_NEST_MAX", "HOST_NAME_MAX",
-  "IOV_MAX", "LINE_MAX", "LOGIN_NAME_MAX", "NGROUPS_MAX", "MQ_OPEN_MAX",
-  "MQ_PRIO_MAX", "OPEN_MAX", "PAGE_SIZE", "PAGESIZE",
-  "PTHREAD_DESTRUCTOR_ITERATIONS", "PTHREAD_KEYS_MAX", "PTHREAD_STACK_MIN",
-  "PTHREAD_THREADS_MAX", "RE_DUP_MAX", "RTSIG_MAX", "SEM_NSEMS_MAX",
-  "SEM_VALUE_MAX", "SIGQUEUE_MAX", "STREAM_MAX", "SYMLOOP_MAX", "TIMER_MAX",
-  "TTY_NAME_MAX", "TZNAME_MAX", "NPROCESSORS_CONF", "NPROCESSORS_ONLN"
+#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(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),
+  /* 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},
+  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), CONF(NPROCESSORS_CONF),
+  CONF(NPROCESSORS_ONLN)
 };

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

 // Integers out of a header
-char *limit_names[] = {
-  "_POSIX_AIO_LISTIO_MAX", "_POSIX_AIO_MAX", "_POSIX_ARG_MAX",
-  "_POSIX_CHILD_MAX", "_POSIX_DELAYTIMER_MAX", "_POSIX_HOST_NAME_MAX",
-  "_POSIX_LINK_MAX", "_POSIX_LOGIN_NAME_MAX", "_POSIX_MAX_CANON",
-  "_POSIX_MAX_INPUT", "_POSIX_NAME_MAX", "_POSIX_NGROUPS_MAX",
-  "_POSIX_OPEN_MAX", "_POSIX_PATH_MAX", "_POSIX_PIPE_BUF", "_POSIX_RE_DUP_MAX",
-  "_POSIX_RTSIG_MAX", "_POSIX_SEM_NSEMS_MAX", "_POSIX_SEM_VALUE_MAX",
-  "_POSIX_SIGQUEUE_MAX", "_POSIX_SSIZE_MAX", "_POSIX_STREAM_MAX",
-  "_POSIX_SYMLINK_MAX", "_POSIX_SYMLOOP_MAX",
-  "_POSIX_THREAD_DESTRUCTOR_ITERATIONS", "_POSIX_THREAD_KEYS_MAX",
-  "_POSIX_THREAD_THREADS_MAX", "_POSIX_TIMER_MAX", "_POSIX_TTY_NAME_MAX",
-  "_POSIX_TZNAME_MAX", "_POSIX2_BC_BASE_MAX", "_POSIX2_BC_DIM_MAX",
-  "_POSIX2_BC_SCALE_MAX", "_POSIX2_BC_STRING_MAX",
"_POSIX2_CHARCLASS_NAME_MAX",
-  "_POSIX2_COLL_WEIGHTS_MAX", "_POSIX2_EXPR_NEST_MAX", "_POSIX2_LINE_MAX",
-  "_POSIX2_RE_DUP_MAX"
+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(_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 that default to blank
-char *other_names[] = {
-  "LFS_CFLAGS", "LFS_LDFLAGS", "LFS_LIBS"
+struct config others[] = {
+  {"LFS_CFLAGS", 0}, {"LFS_LDFLAGS", 0}, {"LFS_LIBS", 0}
 };

 void getconf_main(void)
 {
-  char **names[] = {sysconf_names, confstr_names, limit_names, other_names},
-    **args;
-  int i, j, lens[] = {ARRAY_LEN(sysconf_names), ARRAY_LEN(confstr_names),
-    ARRAY_LEN(limit_names), ARRAY_LEN(other_names)};
+  struct config *configs[] = {sysconfs, confstrs, limits, others};
+  char **args;
+  int i, j, lens[] = {ARRAY_LEN(sysconfs), ARRAY_LEN(confstrs),
+    ARRAY_LEN(limits), ARRAY_LEN(others)};

   if (toys.optflags&FLAG_l) {
-    for (i = 0; i<4; i++) for (j = 0; j<lens[i]; j++) puts(names[i][j]);
+    for (i = 0; i<4; i++) for (j = 0; j<lens[i]; j++) puts(configs[i][j].name);

     return;
   }
@@ -132,13 +148,15 @@ void getconf_main(void)
     if (!strcmp("CS_PATH", name)) name += 3;

     for (i = 0; i<4; i++) for (j = 0; j<lens[i]; j++) {
-      if (strcmp(names[i][j], name)) continue;
+      struct config *c = &configs[i][j];
+
+      if (strcmp(c->name, name)) continue;

-      if (!i) printf("%ld\n", sysconf(sysconf_vals[j]));
+      if (!i) printf("%ld\n", sysconf(c->value));
       else if (i==1) {
-        confstr(confstr_vals[j], toybuf, sizeof(toybuf));
+        confstr(c->value, toybuf, sizeof(toybuf));
         puts(toybuf);
-      } else if (i==2) printf("%d\n", limit_vals[j]);
+      } else if (i==2) printf("%d\n", c->value);
       // For legacy kernel build
       else if (sizeof(long)==4 && !j)
         puts("-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64");
-- 
2.19.0.605.g01d371f741-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-getconf-fix-glibc-NPROCESSORS_ONLN.patch
Type: text/x-patch
Size: 12667 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20181002/3af887da/attachment-0002.bin>


More information about the Toybox mailing list