authorgravatar for git@l4.pmLuna <git@l4.pm> 2020-04-20 16:34:37-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-02 14:56:19-04:00
logc8468bed42db773cc0622c6ea2e18d5e17808f2b
tree83be6e3ed804ab505a3a280bba5941a8a474a61d
parentc7b790ded685cb3bcb17fe7a27de7ae054133250

Add std.os.ioctl


3 files changed, 28 insertions(+), 24 deletions(-)

lib/std/net.zig+6-18
......@@ -545,24 +545,12 @@ fn if_nametoindex(name: []const u8) !u32 {
545545 std.mem.copy(u8, &ifr.ifr_ifrn.name, name);
546546 ifr.ifr_ifrn.name[name.len] = 0;
547547
548 const rc = os.system.syscall3(
549 os.linux.SYS_ioctl,
550 @bitCast(usize, @as(isize, sockfd)),
551 os.linux.SIOCGIFINDEX,
552 @ptrToInt(&ifr),
553 );
554
555 switch (os.errno(rc)) {
556 os.EBADF => return error.BadFile,
557 os.EINTR => return error.CaughtSignal,
558 os.EIO => return error.FileSystem,
559 os.EINVAL => unreachable,
560 os.ENOTTY => unreachable,
561 os.ENXIO => unreachable,
562 // ioctl() sends ENODEV for an unknown scope id.
563 os.ENODEV => return error.InterfaceNotFound,
564 else => {},
565 }
548 std.os.ioctl(sockfd, os.linux.SIOCGIFINDEX, @ptrToInt(&ifr)) catch |err| {
549 switch (err) {
550 error.NoDevice => return error.InterfaceNotFound,
551 else => return err,
552 }
553 };
566554
567555 return @bitCast(u32, ifr.ifr_ifru.ifru_ivalue);
568556}
lib/std/os.zig+18-6
......@@ -405,7 +405,6 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
405405 else => |err| return unexpectedErrno(err),
406406 }
407407 }
408
409408 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
410409 while (true) {
411410 // TODO handle the case when iov_len is too large and get rid of this @intCast
......@@ -2451,9 +2450,8 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
24512450 if (builtin.os.tag == .windows) {
24522451 // NOTE: windows translates the SOCK_NONBLOCK/SOCK_CLOEXEC flags into windows-analagous operations
24532452 const filtered_sock_type = socket_type & ~@as(u32, SOCK_NONBLOCK | SOCK_CLOEXEC);
2454 const flags : u32 = if ((socket_type & SOCK_CLOEXEC) != 0) windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT else 0;
2455 const rc = windows.ws2_32.WSASocketW(@intCast(c_int, domain), @intCast(c_int, filtered_sock_type),
2456 @intCast(c_int, protocol), null, 0, flags);
2453 const flags: u32 = if ((socket_type & SOCK_CLOEXEC) != 0) windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT else 0;
2454 const rc = windows.ws2_32.WSASocketW(@intCast(c_int, domain), @intCast(c_int, filtered_sock_type), @intCast(c_int, protocol), null, 0, flags);
24572455 if (rc == windows.ws2_32.INVALID_SOCKET) switch (windows.ws2_32.WSAGetLastError()) {
24582456 .WSAEMFILE => return error.ProcessFdQuotaExceeded,
24592457 .WSAENOBUFS => return error.SystemResources,
......@@ -2463,7 +2461,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
24632461 };
24642462 errdefer windows.closesocket(rc) catch unreachable;
24652463 if ((socket_type & SOCK_NONBLOCK) != 0) {
2466 var mode : c_ulong = 1; // nonblocking
2464 var mode: c_ulong = 1; // nonblocking
24672465 if (windows.ws2_32.SOCKET_ERROR == windows.ws2_32.ioctlsocket(rc, windows.ws2_32.FIONBIO, &mode)) {
24682466 switch (windows.ws2_32.WSAGetLastError()) {
24692467 // have not identified any error codes that should be handled yet
......@@ -2858,7 +2856,7 @@ pub fn connect(sockfd: socket_t, sock_addr: *const sockaddr, len: socklen_t) Con
28582856 .WSAECONNREFUSED => return error.ConnectionRefused,
28592857 .WSAETIMEDOUT => return error.ConnectionTimedOut,
28602858 .WSAEHOSTUNREACH // TODO: should we return NetworkUnreachable in this case as well?
2861 ,.WSAENETUNREACH => return error.NetworkUnreachable,
2859 , .WSAENETUNREACH => return error.NetworkUnreachable,
28622860 .WSAEFAULT => unreachable,
28632861 .WSAEINVAL => unreachable,
28642862 .WSAEISCONN => unreachable,
......@@ -4906,3 +4904,17 @@ pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) Termio
49064904 }
49074905 }
49084906}
4907
4908pub fn ioctl(handle: fd_t, request: u32, arg: var) !void {
4909 switch (errno(system.ioctl(handle, request, arg))) {
4910 0 => {},
4911 EINVAL => unreachable,
4912 ENOTTY => unreachable,
4913 ENXIO => unreachable,
4914 EBADF => return error.BadFile,
4915 EINTR => return error.CaughtSignal,
4916 EIO => return error.FileSystem,
4917 ENODEV => return error.NoDevice,
4918 else => |err| return unexpectedErrno(err),
4919 }
4920}
lib/std/os/linux.zig+4
......@@ -1193,6 +1193,10 @@ pub fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) usi
11931193 return syscall3(.ioctl, @bitCast(usize, @as(isize, fd)), TCSETS + @enumToInt(optional_action), @ptrToInt(termios_p));
11941194}
11951195
1196pub fn ioctl(fd: fd_t, request: u32, arg: var) usize {
1197 return syscall3(.ioctl, @bitCast(usize, @as(isize, fd)), request, arg);
1198}
1199
11961200test "" {
11971201 if (builtin.os.tag == .linux) {
11981202 _ = @import("linux/test.zig");