2022-04-28 16:42:36 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/dce.h"
|
2023-08-17 07:25:01 +00:00
|
|
|
#include "libc/macros.h"
|
2022-04-28 16:42:36 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2024-02-01 11:39:46 +00:00
|
|
|
#include "libc/stdio/sysparam.h"
|
|
|
|
#include "libc/sysv/consts/_posix.h"
|
2023-08-17 07:25:01 +00:00
|
|
|
#include "libc/sysv/consts/limits.h"
|
|
|
|
#include "libc/sysv/consts/rlimit.h"
|
|
|
|
|
|
|
|
#define CTL_KERN 1
|
|
|
|
#define KERN_ARGMAX 8
|
2022-04-28 16:42:36 +00:00
|
|
|
|
|
|
|
/**
|
2023-08-17 07:25:01 +00:00
|
|
|
* Returns expensive but more correct version of `ARG_MAX`.
|
2022-04-28 16:42:36 +00:00
|
|
|
*/
|
2023-08-17 07:25:01 +00:00
|
|
|
int __get_arg_max(void) {
|
2024-02-01 11:39:46 +00:00
|
|
|
int res;
|
2023-08-17 07:25:01 +00:00
|
|
|
if (IsLinux()) {
|
|
|
|
// You might think that just returning a constant 128KiB (ARG_MAX)
|
|
|
|
// would make sense, as this guy did:
|
|
|
|
//
|
|
|
|
// https://lkml.org/lkml/2017/11/15/813...
|
|
|
|
//
|
|
|
|
// I suspect a 128kB sysconf(_SC_ARG_MAX) is the sanest bet, simply
|
|
|
|
// because of that "conservative is better than aggressive".
|
|
|
|
//
|
|
|
|
// Especially since _technically_ we're still limiting things to that
|
|
|
|
// 128kB due to the single-string limit.
|
|
|
|
//
|
|
|
|
// Linus
|
|
|
|
//
|
|
|
|
// In practice that caused us trouble with toybox tests for xargs
|
|
|
|
// edge cases. The tests assume that they can at least reach the
|
|
|
|
// kernel's "minimum maximum" of 128KiB, but if we report 128KiB for
|
|
|
|
// _SC_ARG_MAX and xargs starts subtracting the environment space
|
|
|
|
// and so on from that, then xargs will think it's run out of space
|
|
|
|
// when given 128KiB of data, which should always work. See this
|
|
|
|
// thread for more:
|
|
|
|
//
|
|
|
|
// http://lists.landley.net/pipermail/toybox-landley.net/2019-November/011229.html
|
|
|
|
//
|
|
|
|
// So let's resign ourselves to tracking what the kernel actually
|
|
|
|
// does. Right now (2019, Linux 5.3) that amounts to:
|
|
|
|
uint64_t stacksz;
|
|
|
|
stacksz = __get_rlimit(RLIMIT_STACK);
|
2024-02-01 11:39:46 +00:00
|
|
|
res = MAX(MIN(stacksz / 4, 3 * (8 * 1024 * 1024) / 4), _ARG_MAX);
|
2023-08-17 07:25:01 +00:00
|
|
|
} else if (IsBsd()) {
|
2024-02-01 11:39:46 +00:00
|
|
|
res = __get_sysctl(CTL_KERN, KERN_ARGMAX);
|
2023-08-17 07:25:01 +00:00
|
|
|
} else {
|
2024-02-01 11:39:46 +00:00
|
|
|
res = _ARG_MAX;
|
2023-08-17 07:25:01 +00:00
|
|
|
}
|
2024-02-01 11:39:46 +00:00
|
|
|
return MAX(res, _POSIX_ARG_MAX);
|
2022-04-28 16:42:36 +00:00
|
|
|
}
|