Commit graph

403 commits

Author SHA1 Message Date
Andrew Kelley
0e37ff0d59 std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API

make std.testing.expectFmt work at compile-time

std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.

Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
  - anytype -> *std.io.Writer
  - inferred error set -> error{WriteFailed}
  - options -> (deleted)
* std.fmt.Formatted
  - now takes context type explicitly
  - no fmt string
2025-07-07 22:43:51 -07:00
Nameless
7c42517151 os: fix missing and incorrect msghdr definitions
Macos uses the BSD definition of msghdr

All linux architectures share a single msghdr definition. Many
architectures had manually inserted padding fields that were endian
specific and some had fields with different integers. This unifies all
architectures to use a single correct msghdr definition.
2025-07-07 22:43:51 -07:00
Pat Tullmann
89d15a8d47 linux: futex v2 API updates
* `futex2_waitv` always takes a 64-bit timespec.  Perhaps the
  `kernel_timespec` should be renamed `timespec64`?  Its used in iouring,
  too.

* Add `packed struct` for futex v2 flags and parameters.

* Add very basic "tests" for the futex v2 syscalls (just to ensure the
  code compiles).

* Update the stale or broken comments.  (I could also just delete these
  they're not really documenting Zig-specific behavior.)

Given that the futex2 APIs are not used by Zig's library (they're a bit
too new), and the fact that these are very specialized syscalls, and they
currently provide no benefit over the existing v1 API, I wonder if instead
of fixing these up, we should just replace them with a stub that says 'use
a 3rd party library'.
2025-06-18 19:53:50 -07:00
Pat Tullmann
cfe5defd02 linux: futex v1 API cleanup
* Use `packed struct` for flags arguments.  So, instead of
  `linux.FUTEX.WAIT` use `.{ .cmd = .WAIT, .private = true }`

* rename `futex_wait` and `futex_wake` which didn't actually specify
  wait/wake, as `futex_3arg` and `futex_4arg` (as its the number
  of parameters that is different, the `op` is whatever is specified.

* expose the full six-arg flavor of the syscall (for some of the advanced
  ops), and add packed structs for their arguments.

* Use a `packed union` to support the 4th parameter which is sometimes a
  `timespec` pointer, and sometimes a `u32`.

* Add tests that make sure the structure layout is correct and that the
  basic argument passing is working (no actual futexes are contended).
2025-06-17 22:06:39 -07:00
Alex Rønne Petersen
999777e73a compiler: Scaffold stage2_powerpc backend.
Nothing interesting here; literally just the bare minimum so I can work on this
on and off in a branch without worrying about merge conflicts in the non-backend
code.
2025-05-20 10:23:16 +02:00
Alex Rønne Petersen
74a3ae4927 start: Don't artificially limit some posixCallMainAndExit() logic to Linux.
This code applies to ~any POSIX OS where we don't link libc. For example, it'll
be useful for FreeBSD and NetBSD.

As part of this, move std.os.linux.pie to std.pie since there's really nothing
Linux-specific about what that file is doing.
2025-05-18 17:14:09 +02:00
Michael Pfaff
49c7318056 Fix implementation of std.os.linux.accept on x86 2025-05-10 02:24:33 +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
tjog
0ba77eca74
disable getauxvalImpl instrumentation as libfuzzer's allocator may need to call it 2025-05-03 23:33:26 +02:00
Alex Rønne Petersen
ad9cb40112
Merge pull request #23601 from rootbeer/sig-split
Split glibc and linux sigset_t ABIs and the accessor functions
2025-05-01 21:29:23 +02:00
Cutie Deng
c1649d586a fix mount api
- mount syscall allow `source` to be null
2025-05-01 06:47:56 +02:00
Pat Tullmann
a55ecd7532 std.os.linux: Fix MIPS signal numbers
Dunno why the MIPS signal numbers are different, or why Zig had them
already special cased, but wrong.

We have the technology to test these constants.  We should use it.
2025-04-30 20:32:04 -07:00
Pat Tullmann
120c4789c3 sigset_t: sigemptyset() and sigfillset() are functions that return sigset_t
By returning an initialized sigset (instead of taking the set as an output
parameter), these functions can be used to directly initialize the `mask`
parameter of a `Sigaction` instance.
2025-04-30 20:32:04 -07:00
Pat Tullmann
298b1886b2 std.os.linux: export kernel-sized sigset_t and operations
The kernel ABI sigset_t is smaller than the glibc one.  Define the
right-sized sigset_t and fixup the sigaction() wrapper to leverage it.
The Sigaction wrapper here is not an ABI, so relax it (drop the "extern"
and the "restorer" fields), the existing `k_sigaction` is the ABI
sigaction struct.

Linux defines `sigset_t` with a c_ulong, so it can be 32-bit or 64-bit,
depending on the platform.  This can make a difference on big-endian
systems.

Patch up `ucontext_t` so that this change doesn't impact its layout.
AFAICT, its currently the glibc layout.
2025-04-30 20:32:04 -07:00
Ryan King
6c598e8341 std: add os.linux.sysinfo(), use it for process.totalSystemMemory()
Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-04-15 20:08:59 +02:00
Alex Rønne Petersen
2527c82482
std.os.linux: Use faccessat2 syscall in faccessat().
Only the former has a flags parameter. It's only available in Linux 5.8+.

Closes #16606.
2025-04-11 21:17:35 +02:00
Pat Tullmann
4b63f94b4e Fix sigaddset/sigdelset bit-fiddling math
The code was using u32 and usize interchangably, which doesn't work on
64-bit systems.  This:
  `pub const sigset_t = [1024 / 32]u32;`
is not consistent with this:
  `const shift = @as(u5, @intCast(s & (usize_bits - 1)));`

However, normal signal numbers are less than 31, so the bad math doesn't matter much.  Also, despite support for 1024 signals in the set, only setting signals between 1 and NSIG (which is mostly 65, but sometimes 128) is defined.  The existing tests only exercised signal numbers in the first 31 bits so they didn't trip over this:

The C library `sigaddset` will return `EINVAL` if given an out of bounds signal number.  I made the Zig code just silently ignore any out of bounds signal numbers.

Moved all the `sigset` related declarations next to each in the source, too.

The `filled_sigset` seems non-standard to me.  I think it is meant to be used like `empty_sigset`, but it only contains 31 set signals, which seems wrong (should be 64 or 128, aka `NSIG`).  It's also unused.  The oddly named but similar `all_mask` is used (by posix.zig) but sets all 1024 bits (which I understood to be undefined behavior but seems to work just fine).  For comparison the musl `sigfillset` fills in 65 bits or 128 bits.
2025-04-10 23:49:44 +02:00
Pat Tullmann
991560fb49 linux.zig: epoll_wait: pass kernel sigset size
Linux kernel syscalls expect to be given the number of bits of sigset that
they're built for, not the full 1024-bit sigsets that glibc supports.

I audited the other syscalls in here that use `sigset_t` and they're all
using `NSIG / 8`.

Fixes #12715
2025-04-09 16:37:58 +02:00
Alex Rønne Petersen
2d33cc2e42
Merge pull request #23376 from sweiglbosker/m68k-archbits 2025-04-08 06:04:16 +02:00
Alex Rønne Petersen
9423712519
std: Disable usage of fstat() and friends on loongarch.
Like riscv32, loongarch exclusively uses statx().
2025-04-07 16:03:22 +02:00
Stefan Weigl-Bosker
bcb4ba9afd
std.os.linux: use heap.pageSize() instead of MMAP2_UNIT 2025-04-07 13:37:01 +02:00
Stefan Weigl-Bosker
6fd358d287
std.os.linux: add arch bits for m68k
fixup
2025-04-07 13:36:45 +02:00
Dacheng Gao
596e0bd47b std.os.linux: add constants for ETH 2025-04-06 02:52:20 +02:00
Alex Rønne Petersen
9dfdf35032
Merge pull request #22337 from ruihe774/fix-app-mask
* std.os.linux: remove app_mask
* std.posix: on libc-less linux, block all signals in raise(), not just app_mask
2025-04-02 17:36:59 +02:00
wooster0
263ba34619 linux: don't export getauxval when not required 2025-03-26 20:37:35 +01:00
Igor Anić
94b36dbe50 io_uring: refactor buf_reg flags
Use packed struct instead of or-ed integers.

Thanks to @linsug for pr comments: https://github.com/ziglang/zig/pull/23062
2025-03-05 13:35:52 +01:00
Igor Anić
c133171567 io_uring: incremental provided buffer consumption
[Incremental provided buffer
consumption](https://github.com/axboe/liburing/wiki/What's-new-with-io_uring-in-6.11-and-6.12#incremental-provided-buffer-consumption)
support is added in kernel 6.12.

IoUring.BufferGroup will now use incremental consumption whenever
kernel supports it.

Before, provided buffers are wholly consumed when picked. Each cqe
points to the different buffer. With this, cqe points to the part of the
buffer. Multiple cqe's can reuse same buffer.
Appropriate sizing of buffers becomes less important.

There are slight changes in BufferGroup interface (it now needs to track
current receive point for each buffer). Init requires allocator
instead of buffers slice, it will allocate buffers slice and head
pointers slice. Get and put now requires cqe becasue there we have
information will the buffer be reused.
2025-03-05 13:35:52 +01:00
Igor Anić
4df039d235 io_uring: add setsockopt/getsockopt
ring.cmd_sock is generic socket operation. Two most common uses are
setsockopt and getsockopt. This provides same interface as posix
versions of this methods.

libring has also [sqe_set_flags](https://man7.org/linux/man-pages/man3/io_uring_sqe_set_flags.3.html)
method. Adding that in our io_uring_sqe. Adding sqe.link_next method for setting most common flag.
2025-03-05 13:35:52 +01:00
Igor Anić
d98c0893b0 io_uring: probe capabilities function
ring.get_probe returns io_uring_probe which can be use to probe
capabilities of the current running kernel.

Ref:
https://unixism.net/loti/ref-liburing/supported_caps.html
e1003e496e/src/setup.c (L454)
2025-03-05 13:35:52 +01:00
Igor Anić
2da8eff9d6 io_uring: add bind and listen 2025-03-05 13:35:52 +01:00
Jari Vetoniemi
aa5c6c027c linux: add UDP socket options 2025-02-21 06:05:04 +01:00
Jari Vetoniemi
c41bc20ec7 linux: add IORING_RECVSEND_BUNDLE 2025-02-20 12:09:38 +01:00
Andrew Kelley
2c5113f6d1 std.os.linux.mmap: remove logic that does not belong here 2025-02-06 14:23:23 -08:00
Andrew Kelley
a4d4e086c5 introduce std.posix.mremap and use it
in std.heap.page_allocator
2025-02-06 14:23:23 -08:00
Misaki Kasumi
cc65eaf0a9 std.os.linux: remove app_mask 2025-02-05 06:25:04 +01:00
John Benediktsson
6a1b76a02c std.os.linux: re-add missing timerfd_create() constants 2025-02-03 15:44:27 +01:00
Chris Boesch
58c00a829e
std.posix: Use separate clock ID enums for clock_gettime() and timerfd_create() (#22627) 2025-02-01 06:53:57 +00:00
John Benediktsson
c104e86442
std.os.linux: adding recvmmsg() (#22651) 2025-01-31 15:44:50 +00:00
Meghan Denny
0bf57b7114 std: mkdir(2) mode uses mode_t 2025-01-29 14:57:07 +01:00
thejohnny5
78b7a446f0 std: add optional times pointer for futimes, futimens, utimes, utimensat 2025-01-29 09:17:20 +01:00
Enrique Miguel Mora Meza
24965af295
std.os.linux: Adding sigdelset (#22406) 2025-01-26 18:49:25 +01:00
mlugg
d00e05f186
all: update to std.builtin.Type.Pointer.Size field renames
This was done by regex substitution with `sed`. I then manually went
over the entire diff and fixed any incorrect changes.

This diff also changes a lot of `callconv(.C)` to `callconv(.c)`, since
my regex happened to also trigger here. I opted to leave these changes
in, since they *are* a correct migration, even if they're not the one I
was trying to do!
2025-01-16 12:46:29 +00:00
Misaki Kasumi
021289a653 linux: make ptid and ctid in clone() optional 2024-12-31 05:24:10 +01:00
Meili C
0f17cbfc6a fix: allow std.linux.getgroups to accept null
looking at `man getgroups` and `info getgroups` this is given as an
example:

  ```c
       // Here's how to use ‘getgroups’ to read all the supplementary group
       // IDs:

            gid_t *
            read_all_groups (void)
            {
              int ngroups = getgroups (0, NULL);
              gid_t *groups
                = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
              int val = getgroups (ngroups, groups);
              if (val < 0)
                {
                  free (groups);
                  return NULL;
                }
              return groups;
            }
  ```

getgroups(0, NULL) is used to get the count of groups so that the
correct count can be used to allocate a list of gid_t. This small changes makes this
possible.

equivalent example in Zig after the change:

  ```zig
    // get the group count
    const ngroups: usize = std.os.linux.getgroups(0, null);
    if (ngroups <= 0) {
        return error.GetGroupsError;
    }

    std.debug.print("number of groups: {d}\n", .{ngroups});
    const groups_gids: []u32 = try alloc.alloc(u32, ngroups);

    // populate an array of gid_t
    _ = std.os.linux.getgroups(ngroups, @ptrCast(groups_gids));
  ```
2024-12-22 21:48:47 +01:00
Alex Rønne Petersen
14c79203c4
std.os.linux: Fix fadvise64 syscall selection for n32/x32. 2024-12-01 02:23:55 +01:00
curuvar
53a232e51d
Add realtime scheduling calls to std.os.linux (issue #19671) (#19675)
Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2024-11-16 20:55:39 +00:00
Benjamin Hetz
c59aee03c8 Flags for SIOC{G,S}IFFLAGS 2024-11-13 06:11:39 +01:00
Alex Rønne Petersen
2f003f39b2
Merge pull request #21599 from alexrp/thumb-porting 2024-11-03 14:25:30 +01:00
Alex Rønne Petersen
c9e67e71c1
std.Target: Replace isARM() with isArmOrThumb() and rename it to isArm().
The old isARM() function was a portability trap. With the name it had, it seemed
like the obviously correct function to use, but it didn't include Thumb. In the
vast majority of cases where someone wants to ask "is the target Arm?", Thumb
*should* be included.

There are exactly 3 cases in the codebase where we do actually need to exclude
Thumb, although one of those is in Aro and mirrors a check in Clang that is
itself likely a bug. These rare cases can just add an extra isThumb() check.
2024-11-03 09:29:30 +01:00
Alex Rønne Petersen
d61e4ef8b0
generate_linux_syscalls: Generate syscalls for x32.
Also update the syscalls file based on Linux 6.10. No diffs other than x32.
2024-11-02 10:42:53 +01:00