304 Commits

Author SHA1 Message Date
Brandon Black
3e372f1994 std: add in_pktinfo and in6_pktinfo structs defs
in_pktinfo is only used on a few targets for the IP_PKTINFO
sockopt, as many BSDs use an alternate mechanism (IP_RECVDSTADDR)
that doesn't require a special struct.  in6_pktinfo is more
universal.
2025-09-09 16:59:50 -05:00
Brandon Black
c449a30213 std.c: Add accept_filter_arg for some BSDs
This is the struct type used as set/getsockopt() option data with
SO.ACCEPTFILTER, which is also only declared on this same limited
set of BSD-ish targets.

In theory this could be aliased over to std.posix as well, but I
think for a corner case like this, it's not unreasonable for a
user that is avoiding uneccessary std.c references to access it as
"posix.system.accept_filter_arg" (which would still work fine if,
in the future, FreeBSD escapes its libc dep and defines it in
std.os.freebsd).
2025-09-09 16:59:50 -05:00
Brandon Black
c70521e7b1 std: Add SCM constants for socket control messages 2025-09-09 16:59:50 -05:00
Brandon Black
c4c6c01cc5 recvmsg: posix wrapper, void on windows
Also, added EPIPE to recvfrom() error set (it's a documented error
for unix and tcp sockets, at least), which recvmsg() largely
shares.  Windows has an odd, callback-based form of recvmsg() that
doesn't fit the normal interface here.
2025-09-08 14:45:51 -05:00
Brandon Black
79313d844f socketpair: posix wrapper, void on windows
socketpair is something like a pipe2() for sockets, and generally
only works for AF_UNIX sockets for most platforms.  Winsock2
explicitly does not support this call, even though it does have
AF_UNIX sockets.
2025-09-08 14:45:51 -05:00
Brandon Black
76c62e509b Fix cmsghdr struct for the *nixes
Previously we had a single definition of std.c.cmsghdr for all
libc-linking platforms which aliased from the Solaris definition,
a superfluous matching one in std.os.dragonfly, and no others.

The existing definition from std.c didn't actually work for Linux,
as Linux's "len" field is usize in the kernel's definition.

Emscripten follows the Linux model of course (but uses the
binary-compatible musl definition, which has an endian-sensitive
padding scheme to make the len type "socklen_t" even though the
kernel uses a usize, which is fair).

This unifies and documents all the known *nix-ish cases (I'm not
sure if wasi or windows really has cmsghdr support? Could be added
later, void for now), such that c.cmsghdr and posix.system.cmsghdr
should work correctly for all the known cases here, libc or
otherwise.
2025-09-03 21:45:03 -07:00
doclic
18bdd0e9a4 std.c: add getresuid & getresgid 2025-09-03 21:28:07 +02:00
Linus Groh
79f60302d6 std.c: Enable arc4random_buf for serenity 2025-09-03 02:24:47 +01:00
Brandon Mercer
7fdd60df1c
Populate MSG struct for OpenBSD (#25076)
* update the MSG struct with the correct values for openbsd

* add comment with link to sys/sys/socket.h

---------

Co-authored-by: Brandon Mercer <bmercer@eutonian.com>
2025-08-30 22:04:32 +00:00
Brandon Black
ae2622bf82 std.c: add correct SOMAXCONN for BSDs
Note the previous "28" here for openbsd was some kind of copy
error long ago.  That's the value of KERN.SOMAXCONN, which is an
entirely different thing.
2025-08-27 09:46:37 +02:00
Andrew Kelley
595c9d2353
Merge pull request #24921 from Justus2308/messy-mach
std.c.darwin: cleanup, expose everything in std.c
2025-08-21 11:52:22 -07:00
Brandon Black
8248597a27 Add mlock syscalls to std.c and std.posix
Linux already gained the relevant syscalls and consts in #24473

The basic mlock() and munlock() are fairly universal across the
*nix world with a consistent interface, but are missing on wasi
and windows.

The mlockall() and munlockall() calls are not as widely supported
as the basic ones.  Notable non-implementers include darwin,
haiku, and serenity (and of course wasi and windows again).

mlock2() is Linux-only, as are its MLOCK flags.
2025-08-20 23:38:45 +02:00
Justus Klausecker
590d264bdf std.c.darwin: cleanup, expose everything in std.c
This mainly just moves stuff around.
Justifications for other changes:
* `KEVENT.FLAGS` is backed by `c_uint` because that's what the `kevent64` flags param takes (according to the 'latest' manpage from 2008)
* `MACH_RCV_NOTIFY` is a legacy name and `MACH_RCV_OVERWRITE` is deprecated (xnu/osfmk/mach/message.h), so I removed them. They were 0 anyway and thus couldn't be represented
as a packed struct field.
* `MACH.RCV` and `MACH.SEND` are technically the same 'type' because they can both be supplied at the same time to `mach_msg`. I decided to still keep them separate because
naming works out better that way and all flags except for `MACH_MSG_STRICT_REPLY` aren't shared anyway. Both are part of a packed union `mach_msg_option_t` which supplies a
helper function to combine the two types.
* `PT` is backed by `c_int` because that's what `ptrace` takes as a request arg (according to the latest manpage from 2015)
2025-08-20 18:21:32 +02:00
Justus Klausecker
d748cc9c12 expose darwin.PT in std.c 2025-08-19 16:18:27 -07:00
Linus Groh
6a1d61d504 std.c: Remove serenity's internet_checksum() function
See: 59911d8da3
2025-08-10 18:21:47 +02:00
Meghan Denny
63c5329156 std: fix std.c._msize signature 2025-08-07 16:02:52 +02:00
Veikka Tuominen
82961a8c9f std.c: fix utsname array sizes 2025-08-05 00:18:14 -07:00
Chinmay Dalal
a2d21d6327 enable pwd.h functions for other OSes
also add the layout of `struct passwd` for DragonflyBSD
and FreeBSD:
 - c267aac007/include/pwd.h (L112)
 - https://cgit.freebsd.org/src/tree/include/pwd.h?id=d66f9c86fa3fd8d8f0a56ea96b03ca11f2fac1fb#n114
2025-08-01 04:34:45 +02:00
Andrew Kelley
982c387753
Merge pull request #24633 from linusg/more-serenity-fixes
std: A few more fixes for serenity
2025-07-31 09:56:26 -07:00
Chinmay Dalal
e941ce3e68 add grp.h functions to c.zig 2025-07-30 21:28:42 -07:00
Linus Groh
813a0f125e std.posix: Default ACCMODE to NONE for serenity
Unlike all other platforms where RDONLY is 0 it does not work as a
default for the O flags on serenity - various syscalls other than
'open', e.g. 'pipe', return EINVAL if unexpected bits are set in the
flags.
2025-07-30 23:27:32 +01:00
Linus Groh
f5e9384335 std.c: Fix MAP for serenity
I accidentally translated MAP_ constants representing the type as
individual fields. MAP_FILE is for compatibility only and not needed
here.
2025-07-30 23:22:06 +01:00
Linus Groh
467a1f4a1c std.c: Fix msghdr_const for serenity 2025-07-30 23:19:29 +01:00
Linus Groh
ea90ec4d88 std.c: Fix dirent.name size for serenity
The null terminator is added to the buffer size.
2025-07-24 00:24:55 +01:00
Andrew Kelley
96cbdd145d std.fs.File.Reader: fix sendFile logic
it wasn't accounting for both writer and reader buffering
2025-07-21 20:00:45 -07:00
Andrew Kelley
e4ebbdb354 std.c: add missing netbsd and freebsd MSG flags 2025-07-14 09:35:38 -07:00
Andrew Kelley
890f1fa848 make msghdr.iovlen unsigned
I don't care what C headers say. This value is never supposed to be
negative.
2025-07-14 00:16:49 -07:00
Brandon Black
e8a4e47d38 Add setsid to std.(c|posix)
The interface and errors for this seem to be very universal and
generic. Note Linux already has this defined as a syscall as well.
2025-07-14 07:26:49 +02:00
Alex Rønne Petersen
31e46be743 std.c: Use __sigemptyset14 for sigemptyset on NetBSD
Closes #24344.
2025-07-07 12:14:58 +02:00
Dacheng
35329b510c
c.darwin: define MSG for macos (#24224)
* c.darwin: define MSG for macos

* darwin: add series os name

* Update lib/std/c.zig

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

---------

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-06-24 15:07:06 +02:00
Elaine Gibson
2139eb75f9 std.c: getcontext is not supported on haiku 2025-06-05 12:30:54 +01:00
Elaine Gibson
8910ac2ba8 std.c.SOCK: define NONBLOCK and CLOEXEC for haiku 2025-06-05 12:30:54 +01:00
Alex Rønne Petersen
92b3c4b451
std.c: Expand the definition of NetBSD's pthread_rwlock_t for more targets. 2025-05-29 16:48:28 +02:00
Alex Rønne Petersen
71fd5ac38a
std.c: Fix return type for NetBSD's __sigfillset14(). 2025-05-29 16:48:28 +02:00
Alex Rønne Petersen
309ac27d35
std.c: Fix sigrtmin()/sigrtmax() for FreeBSD and NetBSD.
They just define the constants in the system headers.
2025-05-29 16:48:28 +02:00
Evan Silberman
931c6f90f5 Add EVFILT_USER and friends for OpenBSD
OpenBSD -current grew EVFILT_USER. See commit message [1] and and
current sys/event.h [2]

Also EVFILT_DEVICE was missing.

Closes #23930

[1]: https://marc.info/?l=openbsd-cvs&m=174686993115485&w=2
[2]: https://codeberg.org/OpenBSD/src/src/branch/master/sys/sys/event.h
2025-05-22 04:37:57 +02:00
David
55848363fd
libc: implement common abs for various integer sizes (#23893)
* libc: implement common `abs` for various integer sizes

* libc: move imaxabs to inttypes.zig and don't use cInclude

* libc: delete `fabs` c implementations because already implemented in compiler_rt

* libc: export functions depending on the target libc

Previously all the functions that were exported were handled equally,
though some may exist and some not inside the same file. Moving the
checks inside the file allows handling different functions differently

* remove empty ifs in inttypes

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* remove empty ifs in stdlib

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* libc: use `@abs` for the absolute value calculation

---------

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-05-21 00:57:38 +02:00
Alex Rønne Petersen
9af8e7c68e
std.c: Fill out some missing definitions for supported NetBSD libc targets.
This allows ubsan-rt to build.
2025-05-17 04:41:26 +02:00
Pat Tullmann
6eb5e56306 std.posix: Add sigrtmin() and sigrtmax()
For C code the macros SIGRTMIN and SIGRTMAX provide these values.  In
practice what looks like a constant is actually provided by a libc call.
So the Zig implementations are explicitly function calls.

glibc (and Musl) export a run-time minimum "real-time" signal number,
based on how many signals are reserved for internal implementation details
(generally threading).  In practice, on Linux, sigrtmin() is 35 on glibc
with the older LinuxThread and 34 with the newer NPTL-based
implementation.  Musl always returns 35.  The maximum "real-time" signal
number is NSIG - 1 (64 on most Linux kernels, but 128 on MIPS).

When not linking a C Library, Zig can report the full range of "rt"
signals (none are reserved by Zig).

Fixes #21189
2025-05-09 15:10:25 +02:00
Pat Tullmann
51654aea87 c.zig: glibc/musl export 1024-bit sigset_t
Export the sigset_t ops (sigaddset, etc) from the C library.  Don't rely
on the linux.zig defintions (which will be defined to use the kernel ABI).

Move Darwin sigset and NSIG declarations into darwin.zig.  Remove
extraneous (?) sigaddset.  The C library sigaddset can reject some signals
being added, so need to defer to it.
2025-04-30 20:32:04 -07:00
Carter Snook
573d9aab5e std.c: use arch's ino_t and off_t for dirent
Fixes #23622. The integer types used for these fields before would not
work on some platforms.
2025-04-26 14:55:59 +02:00
Stefan Weigl-Bosker
bcb4ba9afd
std.os.linux: use heap.pageSize() instead of MMAP2_UNIT 2025-04-07 13:37:01 +02:00
blurrycat
fb188c3d18
std.posix: add getuid()/geteuid() 2025-03-27 07:58:27 +00:00
孙冰
fbbdde41b4 std.c: android bionic C supports arc4random_buf and getentropy
1. https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/bits/getentropy.h
2. https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/stdlib.h
2025-03-25 13:17:45 +01:00
孙冰
77395457fe std.c: fix sysconf names (std.c._SC) for android api
c.f. https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/include/bits/sysconf.h
2025-03-11 21:41:58 +01:00
Linus Groh
79a0de2a2f std.c: Add definitions for SerenityOS 2025-03-11 14:59:29 +00:00
Andrew Kelley
eb3c7f5706 zig build fmt 2025-02-22 17:09:20 -08:00
Gabriel Borrelli
8e293ea8bd std.c: define MADV for darwin 2025-02-20 20:42:24 +01:00
Alex Rønne Petersen
481b7bf3f0
std.Target: Remove functions that just wrap component functions.
Functions like isMinGW() and isGnuLibC() have a good reason to exist: They look
at multiple components of the target. But functions like isWasm(), isDarwin(),
isGnu(), etc only exist to save 4-8 characters. I don't think this is a good
enough reason to keep them, especially given that:

* It's not immediately obvious to a reader whether target.isDarwin() means the
  same thing as target.os.tag.isDarwin() precisely because isMinGW() and similar
  functions *do* look at multiple components.
* It's not clear where we would draw the line. The logical conclusion before
  this commit would be to also wrap Arch.isX86(), Os.Tag.isSolarish(),
  Abi.isOpenHarmony(), etc... this obviously quickly gets out of hand.
* It's nice to just have a single correct way of doing something.
2025-02-17 19:18:19 +01:00
Meghan Denny
b2b830e0e9 std.c: was missing calloc 2025-02-11 17:21:07 -08:00