Commit 99f6f8ead9

Andrew Kelley <andrew@ziglang.org>
2019-12-31 01:35:05
update setsockopt error set according to POSIX
In the code review I accidentally encouraged Luna to remove some handling of errors that are possible according to POSIX, but I think how Luna had it before was better, so I fixed it, and now the branch should be good to merge.
1 parent 22f6297
Changed files (1)
lib
std
lib/std/os.zig
@@ -3251,16 +3251,33 @@ pub fn sched_yield() SchedYieldError!void {
     }
 }
 
+pub const SetSockOptError = error{
+    /// The socket is already connected, and a specified option cannot be set while the socket is connected.
+    AlreadyConnected,
+
+    /// The option is not supported by the protocol.
+    InvalidProtocolOption,
+
+    /// The send and receive timeout values are too big to fit into the timeout fields in the socket structure.
+    TimeoutTooBig,
+
+    /// Insufficient resources are available in the system to complete the call.
+    SystemResources,
+} || UnexpectedError;
+
 /// Set a socket's options.
-pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []const u8) !void {
+pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
     switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(socklen_t, opt.len)))) {
         0 => {},
-        EBADF => unreachable,
+        EBADF => unreachable, // always a race condition
+        ENOTSOCK => unreachable, // always a race condition
         EINVAL => unreachable,
         EFAULT => unreachable,
+        EDOM => return error.TimeoutTooBig,
         EISCONN => return error.AlreadyConnected,
         ENOPROTOOPT => return error.InvalidProtocolOption,
-        ENOTSOCK => unreachable,
+        ENOMEM => return error.SystemResources,
+        ENOBUFS => return error.SystemResources,
         else => |err| return unexpectedErrno(err),
     }
 }