feat(misc): uname/gethostname/sysconf/pathconf

This commit is contained in:
2026-09-05 23:00:23 -04:00
parent 3ab43c7f10
commit e9aa002453
6 changed files with 1070 additions and 0 deletions
+229
View File
@@ -24,6 +24,17 @@
* Level 2 (muslmimic): vfork, setpgrp (obsolescent), setgroups, getpgid,
* getsid, tcgetpgrp, tcsetpgrp (XSI).
*
* System limits and options (todo 32) are declared after the process
* block:
*
* Level 1 (onlyposix): sysconf, pathconf, fpathconf, the _SC_* and _PC_*
* configuration keys.
* Level 2 (muslmimic): gethostname, sethostname, gethostid (XSI/BSD
* system identification — NOT POSIX.1-2008 base),
* and the _CS_* keys for todo 38's confstr().
*
* uname() and struct utsname live in <sys/utsname.h> (todo 32).
*
* The open-flag constants (O_RDONLY, O_CREAT, O_CLOEXEC, ...) belong to
* <fcntl.h> and are deliberately not defined here; the oflag arguments below
* are plain int and take their values from that header. The optional mode
@@ -297,6 +308,172 @@ getpgrp(void);
pid_t
setsid(void);
/* Level 1 (POSIX base): system limits and options. */
/*
* The configuration keys for sysconf(), pathconf() and fpathconf(), and
* (level 2) the _CS_* keys for confstr() (todo 38).
*
* POSIX defines the _SC_* and _PC_* names and the SEMANTICS of the values
* the configuration functions return for them, but NOT their numeric
* values: those are private to each C library (glibc numbers them
* arbitrarily in <bits/confname.h>, musl in a private table). The values
* below are vlibc's OWN stable ABI, assigned in declaration order and never
* reused — program source must pass the symbolic constants, never literal
* integers. They are deliberately NOT glibc's numbers. Any value outside
* these enums is unknown to the configuration functions and yields -1
* without errno (the unsupported-key convention, see below).
*/
/* NOLINTBEGIN(bugprone-reserved-identifier) -- POSIX-mandated _SC_/_PC_ names */
enum
{
/* Process and file limits (sysconf). */
_SC_ARG_MAX = 0, /* max bytes of arguments + environment for exec */
_SC_CHILD_MAX, /* max simultaneous processes per real user id */
_SC_CLK_TCK, /* clock ticks per second, the times() unit (100) */
_SC_NGROUPS_MAX, /* max supplementary group ids */
_SC_OPEN_MAX, /* max open file descriptors per process */
_SC_PAGESIZE, /* memory page size in bytes (from AT_PAGESZ) */
_SC_PAGE_SIZE, /* POSIX synonym of _SC_PAGESIZE */
_SC_NPROCESSORS_CONF, /* number of processors configured */
_SC_NPROCESSORS_ONLN, /* number of processors online */
_SC_PHYS_PAGES, /* number of physical memory pages */
_SC_AVPHYS_PAGES, /* number of currently available memory pages */
_SC_SYMLOOP_MAX, /* max symlink traversals while resolving a path */
_SC_STREAM_MAX, /* max open standard I/O streams (FOPEN_MAX) */
_SC_TZNAME_MAX, /* max bytes in a timezone name */
_SC_VERSION, /* POSIX.1 version, 200809L */
_SC_2_VERSION, /* POSIX.2 version, 200809L */
_SC_JOB_CONTROL, /* 1 if job control is supported */
_SC_SAVED_IDS, /* 1 if saved set-user/group-id is supported */
_SC_HOST_NAME_MAX, /* max bytes in a host name (excluding NUL) */
_SC_LOGIN_NAME_MAX, /* max bytes in a login name */
_SC_GETPW_R_SIZE_MAX, /* recommended buffer size for getpw*_r */
_SC_IOV_MAX, /* max iovec entries in readv/writev */
_SC_LINE_MAX, /* max bytes in an input line (POSIX.2) */
_SC_RE_DUP_MAX, /* max duplicate regexp interval counts */
_SC_COLL_WEIGHTS_MAX, /* max weights for collating elements */
_SC_EXPR_NEST_MAX, /* max expression nesting in expr(1) */
/* POSIX option indicators (sysconf). Values follow the _POSIX_*
* convention: a version value means supported, -1 unsupported. */
_SC_THREADS, /* _POSIX_THREADS */
_SC_SEMAPHORES, /* _POSIX_SEMAPHORES */
_SC_SHARED_MEMORY_OBJECTS, /* _POSIX_SHARED_MEMORY_OBJECTS */
_SC_MESSAGE_PASSING, /* _POSIX_MESSAGE_PASSING */
_SC_MONOTONIC_CLOCK, /* _POSIX_MONOTONIC_CLOCK */
_SC_TIMERS, /* _POSIX_TIMERS */
_SC_SYNCHRONIZED_IO, /* _POSIX_SYNCHRONIZED_IO */
_SC_THREAD_SAFE_FUNCTIONS, /* _POSIX_THREAD_SAFE_FUNCTIONS */
_SC_THREAD_ATTR_STACKADDR, /* _POSIX_THREAD_ATTR_STACKADDR */
_SC_THREAD_ATTR_STACKSIZE, /* _POSIX_THREAD_ATTR_STACKSIZE */
_SC_THREAD_PRIORITY_SCHEDULING, /* _POSIX_THREAD_PRIORITY_SCHEDULING */
_SC_THREAD_PRIO_INHERIT, /* _POSIX_THREAD_PRIO_INHERIT */
_SC_THREAD_PRIO_PROTECT, /* _POSIX_THREAD_PRIO_PROTECT */
_SC_THREAD_PROCESS_SHARED, /* _POSIX_THREAD_PROCESS_SHARED */
_SC_REALTIME_SIGNALS, /* _POSIX_REALTIME_SIGNALS */
/* POSIX.1-2008 option indicators that use plain 1 for supported. */
_SC_REGEXP, /* 1 if the RE functions are supported */
_SC_SHELL, /* 1 if the shell is supported */
_SC_XOPEN_VERSION, /* XSI version, 700 */
_SC_XOPEN_UNIX, /* 1 if XSI interfaces are supported */
_SC_XOPEN_CRYPT, /* 1 if the crypt function is available */
_SC_XOPEN_ENH_I18N, /* 1 if enhanced internationalization is present */
_SC_XOPEN_SHM, /* 1 if XSI shared memory is present */
_SC_2_C_BIND, /* POSIX.2 C-language binding */
_SC_2_C_DEV, /* POSIX.2 C-language development utilities */
_SC_2_FORT_DEV, /* POSIX.2 FORTRAN development utilities */
_SC_2_FORT_RUN, /* POSIX.2 FORTRAN runtime utilities */
_SC_2_LOCALEDEF, /* POSIX.2 locale creation utilities */
_SC_2_SW_DEV, /* POSIX.2 software development utilities */
_SC_2_UPE /* POSIX.2 user-portability utilities */
};
/*
* The _PC_* keys for pathconf() and fpathconf(). Each names a limit or an
* option of the FILESYSTEM holding the queried path or descriptor; see the
* pathconf() documentation below for the supported keys and their values.
*/
enum
{
_PC_LINK_MAX = 0, /* max link count of a file */
_PC_MAX_CANON, /* max bytes in a terminal canonical input line */
_PC_MAX_INPUT, /* max bytes available on a terminal input queue */
_PC_NAME_MAX, /* max bytes in a file name (excluding NUL) */
_PC_PATH_MAX, /* max bytes in a pathname (excluding NUL) */
_PC_PIPE_BUF, /* max bytes atomically writable to a pipe */
_PC_CHOWN_RESTRICTED, /* 1 if chown is restricted to the owner */
_PC_NO_TRUNC, /* 1 if overlong file names are an error */
_PC_VDISABLE, /* the terminal disable character (0 = none) */
_PC_ASYNC_IO, /* 200809L if asynchronous I/O is supported */
_PC_SYNC_IO, /* 200809L if synchronized I/O is supported */
_PC_PRIO_IO, /* 200809L if prioritized I/O is supported */
_PC_SOCK_MAXBUF, /* max bytes storable in a socket buffer */
_PC_FILESIZEBITS, /* bits in a file size field */
_PC_REC_INCR_XFER_SIZE, /* recommended increment for transfer sizes */
_PC_REC_MAX_XFER_SIZE, /* recommended maximum transfer size */
_PC_REC_MIN_XFER_SIZE, /* recommended minimum transfer size */
_PC_REC_XFER_ALIGN, /* recommended transfer alignment */
_PC_ALLOC_SIZE_MIN, /* minimum bytes of a file allocation */
_PC_SYMLINK_MAX, /* max bytes in a symbolic link target */
_PC_2_SYMLINKS /* 1 if symlinks support the ".." resolution rule */
};
/* NOLINTEND(bugprone-reserved-identifier) */
/*
* Query a system configuration value for key name (one of the _SC_* above)
* and return it as a long. A negative value means the option is not
* supported or the key is unknown; for unsupported options and for any key
* outside the _SC_* enum, -1 is returned WITHOUT setting errno (callers
* must not rely on errno after -1).
*
* The supported keys and their vlibc values: _SC_ARG_MAX (2097152),
* _SC_CHILD_MAX (65535), _SC_CLK_TCK (100, the fixed x86_64 clock-tick
* rate — see the note in src/misc/sysconf.c, todo 35's times() consumes
* that same constant), _SC_NGROUPS_MAX (65536), _SC_OPEN_MAX (the current
* RLIMIT_NOFILE soft limit, 1024 if it cannot be read), _SC_PAGESIZE and
* _SC_PAGE_SIZE (the memory page size from the AT_PAGESZ auxiliary vector
* entry, 4096 on x86_64), _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN
* (the processor count), _SC_PHYS_PAGES and _SC_AVPHYS_PAGES (physical and
* available memory page counts), _SC_SYMLOOP_MAX (40), _SC_STREAM_MAX
* (16), _SC_TZNAME_MAX (6), _SC_VERSION and _SC_2_VERSION (200809L),
* _SC_JOB_CONTROL, _SC_SAVED_IDS, _SC_REGEXP, _SC_SHELL (1 each),
* _SC_HOST_NAME_MAX (64), _SC_LOGIN_NAME_MAX (256), _SC_GETPW_R_SIZE_MAX
* (1024), _SC_IOV_MAX (1024), _SC_LINE_MAX (2048), _SC_RE_DUP_MAX (32767),
* _SC_COLL_WEIGHTS_MAX (255), _SC_EXPR_NEST_MAX (32), _SC_XOPEN_VERSION
* (700), _SC_XOPEN_UNIX, _SC_XOPEN_CRYPT, _SC_XOPEN_ENH_I18N, _SC_XOPEN_SHM
* (1 each), and the thread/option version keys (_SC_THREADS ... _SC_2_UPE,
* 200809L each).
*/
long
sysconf(int name);
/*
* Query a configurable limit or option of the filesystem holding path (the
* _PC_* keys above) and return it as a long; -1 with errno set when path
* cannot be examined. Fixed values: _PC_LINK_MAX (32000), _PC_MAX_CANON
* and _PC_MAX_INPUT (255), _PC_PATH_MAX (4096), _PC_PIPE_BUF (4096),
* _PC_CHOWN_RESTRICTED, _PC_NO_TRUNC and _PC_2_SYMLINKS (1), _PC_VDISABLE
* (0), _PC_FILESIZEBITS (64), and _PC_REC_MIN_XFER_SIZE,
* _PC_REC_XFER_ALIGN and _PC_ALLOC_SIZE_MIN (the filesystem block size).
* _PC_NAME_MAX is the filesystem's f_namelen (255 on ext4/xfs). For keys
* whose option the filesystem does not provide (_PC_ASYNC_IO, _PC_SYNC_IO,
* _PC_PRIO_IO, _PC_SOCK_MAXBUF, _PC_REC_INCR_XFER_SIZE,
* _PC_REC_MAX_XFER_SIZE, _PC_SYMLINK_MAX) and for any key outside the
* _PC_* enum, -1 is returned WITHOUT setting errno.
*/
long
pathconf(const char *path, int name);
/*
* Like pathconf(), but the filesystem is the one holding the open
* descriptor fildes.
*/
long
fpathconf(int fildes, int name);
#if VLIBC_LEVEL_GE(2)
/* Level 2 (muslmimic): Linux extensions + XSI + obsolescent. */
@@ -370,6 +547,58 @@ tcgetpgrp(int fildes);
int
tcsetpgrp(int fildes, pid_t pgid);
/* Level 2 (muslmimic): system identification (XSI / BSD, not POSIX base). */
/*
* The _CS_* keys for confstr() (todo 38, which owns the function and its
* string results). Values are vlibc-local ABI like the _SC_ and _PC_ keys
* above; they are defined here now so todo 38 only implements confstr()
* itself.
*/
/* NOLINTBEGIN(bugprone-reserved-identifier) -- POSIX-mandated _CS_ names */
enum
{
_CS_PATH = 0, /* value of PATH (always a usable default) */
_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS, /* 1 if the V6 width-restricted envs exist */
_CS_POSIX_V7_WIDTH_RESTRICTED_ENVS, /* 1 if the V7 width-restricted envs exist */
_CS_LFS_CFLAGS, /* cpp/cflags for large-file support */
_CS_LFS_LDFLAGS, /* ld flags for large-file support */
_CS_LFS_LIBS, /* libraries for large-file support */
_CS_LFS_LINTFLAGS, /* lint flags for large-file support */
_CS_LFS64_CFLAGS, /* cpp/cflags for large-file64 support */
_CS_LFS64_LDFLAGS, /* ld flags for large-file64 support */
_CS_LFS64_LIBS, /* libraries for large-file64 support */
_CS_LFS64_LINTFLAGS, /* lint flags for large-file64 support */
_CS_GNU_LIBC_VERSION, /* "glibc X.Y" (vlibc extension) */
_CS_GNU_LIBPTHREAD_VERSION /* "NPTL X.Y" (vlibc extension) */
};
/* NOLINTEND(bugprone-reserved-identifier) */
/*
* Store the NUL-terminated host name of the current machine into name,
* sized len bytes. Return 0 on success; if the name (including its NUL
* terminator) does not fit in len bytes, nothing is written beyond the
* fit and -1 is returned with errno set to ENAMETOOLONG. XSI.
*/
int
gethostname(char *name, size_t len);
/*
* Set the kernel's host name from name (len bytes, no NUL required).
* Requires privilege. Return 0, or -1 with errno set. BSD, not POSIX.
*/
int
sethostname(const char *name, size_t len);
/*
* Return the 32-bit host identifier of the current machine. vlibc returns
* 0 (musl's behavior): the historic value was derived from the host's
* primary IP address via gethostbyname, which the modern library does not
* perform. [OB] — obsolescent in POSIX.
*/
long
gethostid(void);
#endif /* VLIBC_LEVEL_GE(2) */
#ifdef __cplusplus