Fix small matters and improve sysconf()

- Fix mkdeps.com out of memory error
- Remove static memory from __get_cpu_count()
- Add support for passing hyphen to cat in cocmd
- Change more ZipOS errors from ENOTSUP to EROFS
- Specify mem_unit in sysinfo() output on BSD OSes
This commit is contained in:
Justine Tunney 2023-08-17 00:25:01 -07:00
parent eebc24b9cd
commit 3a9cac4892
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
55 changed files with 411 additions and 262 deletions

View file

@ -218,64 +218,69 @@ static const char *GetOptArg(int c, int *i, int j) {
static int Echo(void) {
int i = 1;
bool once = false;
const char *l = " ";
if (i < n && !strcmp(args[i], "-l")) {
++i, l = "\n";
bool print_newline = true;
if (i < n && args[i][0] == '-' && args[i][1] == 'n' && !args[i][2]) {
++i, print_newline = false;
}
for (; i < n; ++i) {
if (once) {
Write(1, l);
Write(1, " ");
} else {
once = true;
}
Write(1, args[i]);
}
Write(1, "\n");
if (print_newline) {
Write(1, "\n");
}
return 0;
}
static int CatDump(const char *path, int fd, bool dontclose) {
ssize_t rc;
char buf[512];
for (;;) {
rc = read(fd, buf, sizeof(buf));
if (rc == -1) {
perror(path);
if (!dontclose) {
close(fd);
}
return 1;
}
if (!rc) break;
rc = write(1, buf, rc);
if (rc == -1) {
perror("write");
if (!dontclose) {
close(fd);
}
return 1;
}
}
if (!dontclose && close(fd)) {
perror(path);
return 1;
}
return 0;
}
static int Cat(void) {
int i, fd;
ssize_t rc;
char buf[512];
int i, fd, rc;
if (n < 2) {
for (;;) {
rc = read(0, buf, sizeof(buf));
if (rc == -1) {
perror("read");
return 1;
}
if (!rc) break;
rc = write(1, buf, rc);
if (rc == -1) {
perror("write");
return 1;
}
}
return CatDump("<stdin>", 0, true);
} else {
for (i = 1; i < n; ++i) {
if ((fd = open(args[i], O_RDONLY)) == -1) {
bool dontclose = false;
if (args[i][0] == '-' && !args[i][1]) {
dontclose = true;
fd = 0;
} else if ((fd = open(args[i], O_RDONLY)) == -1) {
perror(args[i]);
return 1;
}
for (;;) {
rc = read(fd, buf, sizeof(buf));
if (rc == -1) {
perror(args[i]);
close(fd);
return 1;
}
if (!rc) break;
rc = write(1, buf, rc);
if (rc == -1) {
perror("write");
close(fd);
return 1;
}
}
if (close(fd)) {
perror(args[i]);
return 1;
if ((rc = CatDump(args[i], fd, dontclose))) {
return rc;
}
}
}

View file

@ -17,17 +17,50 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/limits.h"
#include "libc/sysv/consts/rlimit.h"
#define CTL_KERN 1
#define KERN_ARGMAX 8
/**
* Returns `ARG_MAX` for host platform.
* Returns expensive but more correct version of `ARG_MAX`.
*/
int __arg_max(void) {
if (IsWindows()) return 32767;
if (IsLinux()) return 128 * 1024;
if (IsNetbsd()) return 256 * 1024;
if (IsFreebsd()) return 512 * 1024;
if (IsOpenbsd()) return 512 * 1024;
if (IsXnu()) return 1024 * 1024;
return ARG_MAX;
int __get_arg_max(void) {
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);
return MAX(MIN(stacksz / 4, 3 * (8 * 1024 * 1024) / 4), _ARG_MAX);
} else if (IsBsd()) {
return __get_sysctl(CTL_KERN, KERN_ARGMAX);
} else {
return _ARG_MAX;
}
}

View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 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/calls/struct/sysinfo.h"
#include "libc/runtime/runtime.h"
long __get_avphys_pages(void) {
struct sysinfo si;
if (sysinfo(&si) == -1) return -1;
return (((int64_t)si.freeram + si.bufferram) * si.mem_unit) / FRAMESIZE;
}

View file

@ -0,0 +1,26 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 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/calls/struct/sysinfo.h"
#include "libc/runtime/runtime.h"
long __get_phys_pages(void) {
struct sysinfo si;
if (sysinfo(&si) == -1) return -1;
return ((int64_t)si.totalram * si.mem_unit) / FRAMESIZE;
}

View file

@ -21,9 +21,9 @@
#include "libc/macros.internal.h"
#include "libc/sysv/consts/rlim.h"
long _GetResourceLimit(int resource) {
long __get_rlimit(int resource) {
struct rlimit rl;
getrlimit(resource, &rl);
if (getrlimit(resource, &rl) == -1) return -1;
if (rl.rlim_cur == RLIM_INFINITY) return -1;
return MIN(rl.rlim_cur, LONG_MAX);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,22 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/rlimit.h"
#define F_MAXFD 11
/**
* Returns maximum number of open files.
*/
long _GetMaxFd(void) {
int rc;
if (IsNetbsd()) {
if ((rc = __sys_fcntl(0, F_MAXFD, 0)) != -1) {
return rc;
}
long __get_sysctl(int x, int y) {
int value;
int mib[2] = {x, y};
size_t len = sizeof(value);
if (sys_sysctl(mib, 2, &value, &len, 0, 0) != -1) {
return value;
} else {
return -1;
}
return _GetResourceLimit(RLIMIT_NOFILE);
}

View file

@ -129,9 +129,14 @@ bool IsCygwin(void);
const char *GetCpuidOs(void);
const char *GetCpuidEmulator(void);
void GetCpuidBrand(char[13], uint32_t);
long _GetResourceLimit(int);
long __get_rlimit(int);
int __set_rlimit(int, int64_t);
const char *__describe_os(void);
int __arg_max(void);
long __get_sysctl(int, int);
int __get_arg_max(void) pureconst;
int __get_cpu_count(void) pureconst;
long __get_avphys_pages(void) pureconst;
long __get_phys_pages(void) pureconst;
#endif /* _COSMO_SOURCE */
COSMOPOLITAN_C_END_

View file

@ -17,10 +17,21 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/sysconf.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/calls/struct/sysinfo.h"
#include "libc/calls/struct/sysinfo.internal.h"
#include "libc/dce.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/runtime/clktck.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/sysconf.h"
#include "libc/sysv/consts/_posix.h"
#include "libc/sysv/consts/limits.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/thread.h"
/**
* Returns configuration value about system.
@ -28,30 +39,58 @@
* The following parameters are supported:
*
* - `_SC_CLK_TCK` returns number of clock ticks per second
* - `_SC_ARG_MAX` currently always returns 32768 due to Windows
* - `_SC_ARG_MAX` will perform expensive rlimit calculations
* - `_SC_PAGESIZE` currently always returns 65536 due to Windows
* - `_SC_NPROCESSORS_ONLN` returns number of CPUs in the system
* - `_SC_OPEN_MAX` returns maximum number of open files
* - `_SC_AVPHYS_PAGES` returns average physical memory pages
* - `_SC_PHYS_PAGES` returns physical memory pages available
* - `_SC_NPROCESSORS_ONLN` returns number of effective CPUs
* - `_SC_CHILD_MAX` returns maximum number of processes
* - `_SC_OPEN_MAX` returns maximum number of open files
*
* @return value on success, or -1 w/ errno
* @raise EINVAL if `name` isn't valid
*/
long sysconf(int name) {
int n;
switch (name) {
case _SC_ARG_MAX:
return _ARG_MAX;
case _SC_CHILD_MAX:
return _GetResourceLimit(RLIMIT_NPROC);
case _SC_CLK_TCK:
return CLK_TCK;
case _SC_OPEN_MAX:
return _GetMaxFd();
case _SC_PAGESIZE:
return FRAMESIZE;
case _SC_ARG_MAX:
return __get_arg_max();
case _SC_CHILD_MAX:
return __get_rlimit(RLIMIT_NPROC);
case _SC_OPEN_MAX:
return __get_rlimit(RLIMIT_NOFILE);
case _SC_NPROCESSORS_CONF:
case _SC_NPROCESSORS_ONLN:
n = _getcpucount();
return n > 0 ? n : -1;
return __get_cpu_count();
case _SC_PHYS_PAGES:
return __get_phys_pages();
case _SC_AVPHYS_PAGES:
return __get_avphys_pages();
case _SC_THREADS:
return _POSIX_THREADS;
case _SC_THREAD_DESTRUCTOR_ITERATIONS:
return PTHREAD_DESTRUCTOR_ITERATIONS;
case _SC_THREAD_KEYS_MAX:
return PTHREAD_KEYS_MAX;
case _SC_THREAD_STACK_MIN:
return PTHREAD_STACK_MIN;
case _SC_THREAD_THREADS_MAX:
return LONG_MAX;
case _SC_LINE_MAX:
return _POSIX2_LINE_MAX;
case _SC_MONOTONIC_CLOCK:
return _POSIX_MONOTONIC_CLOCK;
case _SC_HOST_NAME_MAX:
return _POSIX_HOST_NAME_MAX;
case _SC_SPAWN:
return _POSIX_SPAWN;
case _SC_SYMLOOP_MAX:
return _POSIX_SYMLOOP_MAX;
default:
return -1;
return einval();
}
}