mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
std.net.listen: no REUSEPORT with unix socket
On Linux, set REUSEPORT option on an unix socket returns a EOPNOTSUPP error.
See 5b0af621c3
This commit is contained in:
parent
df1cd62720
commit
df9fdb1861
@ -248,7 +248,7 @@ pub const Address = extern union {
|
||||
posix.SO.REUSEADDR,
|
||||
&mem.toBytes(@as(c_int, 1)),
|
||||
);
|
||||
if (@hasDecl(posix.SO, "REUSEPORT")) {
|
||||
if (@hasDecl(posix.SO, "REUSEPORT") and address.any.family != posix.AF.UNIX) {
|
||||
try posix.setsockopt(
|
||||
sockfd,
|
||||
posix.SOL.SOCKET,
|
||||
|
||||
@ -296,6 +296,21 @@ test "listen on a unix socket, send bytes, receive bytes" {
|
||||
try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]);
|
||||
}
|
||||
|
||||
test "listen on a unix socket with reuse_port option" {
|
||||
if (!net.has_unix_sockets) return error.SkipZigTest;
|
||||
// Windows doesn't implement reuse port option.
|
||||
if (builtin.os.tag == .windows) return error.SkipZigTest;
|
||||
|
||||
const socket_path = try generateFileName("socket.unix");
|
||||
defer testing.allocator.free(socket_path);
|
||||
|
||||
const socket_addr = try net.Address.initUnix(socket_path);
|
||||
defer std.fs.cwd().deleteFile(socket_path) catch {};
|
||||
|
||||
var server = try socket_addr.listen(.{ .reuse_port = true });
|
||||
server.deinit();
|
||||
}
|
||||
|
||||
fn generateFileName(base_name: []const u8) ![]const u8 {
|
||||
const random_bytes_count = 12;
|
||||
const sub_path_len = comptime std.fs.base64_encoder.calcSize(random_bytes_count);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user