mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 22:04:21 +00:00
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.
170 lines
5.3 KiB
Zig
170 lines
5.3 KiB
Zig
const std = @import("../../std.zig");
|
|
const builtin = @import("builtin");
|
|
const linux = std.os.linux;
|
|
const mem = std.mem;
|
|
const elf = std.elf;
|
|
const expect = std.testing.expect;
|
|
const expectEqual = std.testing.expectEqual;
|
|
const fs = std.fs;
|
|
|
|
test "fallocate" {
|
|
var tmp = std.testing.tmpDir(.{});
|
|
defer tmp.cleanup();
|
|
|
|
const path = "test_fallocate";
|
|
const file = try tmp.dir.createFile(path, .{ .truncate = true, .mode = 0o666 });
|
|
defer file.close();
|
|
|
|
try expect((try file.stat()).size == 0);
|
|
|
|
const len: i64 = 65536;
|
|
switch (linux.E.init(linux.fallocate(file.handle, 0, 0, len))) {
|
|
.SUCCESS => {},
|
|
.NOSYS => return error.SkipZigTest,
|
|
.OPNOTSUPP => return error.SkipZigTest,
|
|
else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),
|
|
}
|
|
|
|
try expect((try file.stat()).size == len);
|
|
}
|
|
|
|
test "getpid" {
|
|
try expect(linux.getpid() != 0);
|
|
}
|
|
|
|
test "getppid" {
|
|
try expect(linux.getppid() != 0);
|
|
}
|
|
|
|
test "timer" {
|
|
const epoll_fd = linux.epoll_create();
|
|
var err: linux.E = linux.E.init(epoll_fd);
|
|
try expect(err == .SUCCESS);
|
|
|
|
const timer_fd = linux.timerfd_create(linux.TIMERFD_CLOCK.MONOTONIC, .{});
|
|
try expect(linux.E.init(timer_fd) == .SUCCESS);
|
|
|
|
const time_interval = linux.timespec{
|
|
.sec = 0,
|
|
.nsec = 2000000,
|
|
};
|
|
|
|
const new_time = linux.itimerspec{
|
|
.it_interval = time_interval,
|
|
.it_value = time_interval,
|
|
};
|
|
|
|
err = linux.E.init(linux.timerfd_settime(@as(i32, @intCast(timer_fd)), .{}, &new_time, null));
|
|
try expect(err == .SUCCESS);
|
|
|
|
var event = linux.epoll_event{
|
|
.events = linux.EPOLL.IN | linux.EPOLL.OUT | linux.EPOLL.ET,
|
|
.data = linux.epoll_data{ .ptr = 0 },
|
|
};
|
|
|
|
err = linux.E.init(linux.epoll_ctl(@as(i32, @intCast(epoll_fd)), linux.EPOLL.CTL_ADD, @as(i32, @intCast(timer_fd)), &event));
|
|
try expect(err == .SUCCESS);
|
|
|
|
const events_one: linux.epoll_event = undefined;
|
|
var events = [_]linux.epoll_event{events_one} ** 8;
|
|
|
|
err = linux.E.init(linux.epoll_wait(@as(i32, @intCast(epoll_fd)), &events, 8, -1));
|
|
try expect(err == .SUCCESS);
|
|
}
|
|
|
|
test "statx" {
|
|
var tmp = std.testing.tmpDir(.{});
|
|
defer tmp.cleanup();
|
|
|
|
const tmp_file_name = "just_a_temporary_file.txt";
|
|
var file = try tmp.dir.createFile(tmp_file_name, .{});
|
|
defer file.close();
|
|
|
|
var statx_buf: linux.Statx = undefined;
|
|
switch (linux.E.init(linux.statx(file.handle, "", linux.AT.EMPTY_PATH, linux.STATX_BASIC_STATS, &statx_buf))) {
|
|
.SUCCESS => {},
|
|
else => unreachable,
|
|
}
|
|
|
|
if (builtin.cpu.arch == .riscv32 or builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; // No fstatat, so the rest of the test is meaningless.
|
|
|
|
var stat_buf: linux.Stat = undefined;
|
|
switch (linux.E.init(linux.fstatat(file.handle, "", &stat_buf, linux.AT.EMPTY_PATH))) {
|
|
.SUCCESS => {},
|
|
else => unreachable,
|
|
}
|
|
|
|
try expect(stat_buf.mode == statx_buf.mode);
|
|
try expect(@as(u32, @bitCast(stat_buf.uid)) == statx_buf.uid);
|
|
try expect(@as(u32, @bitCast(stat_buf.gid)) == statx_buf.gid);
|
|
try expect(@as(u64, @bitCast(@as(i64, stat_buf.size))) == statx_buf.size);
|
|
try expect(@as(u64, @bitCast(@as(i64, stat_buf.blksize))) == statx_buf.blksize);
|
|
try expect(@as(u64, @bitCast(@as(i64, stat_buf.blocks))) == statx_buf.blocks);
|
|
}
|
|
|
|
test "user and group ids" {
|
|
if (builtin.link_libc) return error.SkipZigTest;
|
|
try expectEqual(linux.getauxval(elf.AT_UID), linux.getuid());
|
|
try expectEqual(linux.getauxval(elf.AT_GID), linux.getgid());
|
|
try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
|
|
try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
|
|
}
|
|
|
|
test "fadvise" {
|
|
var tmp = std.testing.tmpDir(.{});
|
|
defer tmp.cleanup();
|
|
|
|
const tmp_file_name = "temp_posix_fadvise.txt";
|
|
var file = try tmp.dir.createFile(tmp_file_name, .{});
|
|
defer file.close();
|
|
|
|
var buf: [2048]u8 = undefined;
|
|
try file.writeAll(&buf);
|
|
|
|
const ret = linux.fadvise(file.handle, 0, 0, linux.POSIX_FADV.SEQUENTIAL);
|
|
try expectEqual(@as(usize, 0), ret);
|
|
}
|
|
|
|
test "sigset_t" {
|
|
var sigset = linux.empty_sigset;
|
|
|
|
// See that none are set, then set each one, see that they're all set, then
|
|
// remove them all, and then see that none are set.
|
|
for (1..linux.NSIG) |i| {
|
|
try expectEqual(linux.sigismember(&sigset, @truncate(i)), false);
|
|
}
|
|
for (1..linux.NSIG) |i| {
|
|
linux.sigaddset(&sigset, @truncate(i));
|
|
}
|
|
for (1..linux.NSIG) |i| {
|
|
try expectEqual(linux.sigismember(&sigset, @truncate(i)), true);
|
|
try expectEqual(linux.sigismember(&linux.empty_sigset, @truncate(i)), false);
|
|
}
|
|
for (1..linux.NSIG) |i| {
|
|
linux.sigdelset(&sigset, @truncate(i));
|
|
}
|
|
for (1..linux.NSIG) |i| {
|
|
try expectEqual(linux.sigismember(&sigset, @truncate(i)), false);
|
|
}
|
|
|
|
linux.sigaddset(&sigset, 1);
|
|
try expectEqual(sigset[0], 1);
|
|
try expectEqual(sigset[1], 0);
|
|
|
|
linux.sigaddset(&sigset, 31);
|
|
try expectEqual(sigset[0], 0x4000_0001);
|
|
try expectEqual(sigset[1], 0);
|
|
|
|
linux.sigaddset(&sigset, 36);
|
|
try expectEqual(sigset[0], 0x4000_0001);
|
|
try expectEqual(sigset[1], 0x8);
|
|
|
|
linux.sigaddset(&sigset, 64);
|
|
try expectEqual(sigset[0], 0x4000_0001);
|
|
try expectEqual(sigset[1], 0x8000_0008);
|
|
try expectEqual(sigset[2], 0);
|
|
}
|
|
|
|
test {
|
|
_ = linux.IoUring;
|
|
}
|