Skip to content

Exporting ioctl #2946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions stdlib/public/Platform/Misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include "swift/Runtime/Config.h"

Expand Down Expand Up @@ -49,6 +50,18 @@ extern int _swift_Platform_fcntlPtr(int fd, int cmd, void* ptr) {
return fcntl(fd, cmd, ptr);
}

SWIFT_CC(swift)
extern int
_swift_Platform_ioctl(int fd, unsigned long int request, int value) {
return ioctl(fd, request, value);
}

SWIFT_CC(swift)
extern int
_swift_Platform_ioctlPtr(int fd, unsigned long int request, void* ptr) {
return ioctl(fd, request, ptr);
}

#if defined(__APPLE__)
#define _REENTRANT
#include <math.h>
Expand Down
42 changes: 42 additions & 0 deletions stdlib/public/Platform/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,48 @@ public var S_IWRITE: mode_t { return S_IWUSR }
public var S_IEXEC: mode_t { return S_IXUSR }
#endif

//===----------------------------------------------------------------------===//
// ioctl.h
//===----------------------------------------------------------------------===//

@_silgen_name("_swift_Platform_ioctl")
internal func _swift_Platform_ioctl(
_ fd: CInt,
_ request: UInt,
_ value: CInt
) -> CInt

@_silgen_name("_swift_Platform_ioctlPtr")
internal func _swift_Platform_ioctlPtr(
_ fd: CInt,
_ request: UInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt

public func ioctl(
_ fd: CInt,
_ request: UInt,
_ value: CInt
) -> CInt {
return _swift_Platform_ioctl(fd, request, value)
}

public func ioctl(
_ fd: CInt,
_ request: UInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt {
return _swift_Platform_ioctlPtr(fd, request, ptr)
}

public func ioctl(
_ fd: CInt,
_ request: UInt
) -> CInt {
return _swift_Platform_ioctl(fd, request, 0)
}


//===----------------------------------------------------------------------===//
// unistd.h
//===----------------------------------------------------------------------===//
Expand Down
29 changes: 29 additions & 0 deletions test/1_stdlib/POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,36 @@ POSIXTests.test("sem_open existing O_EXCL fail") {
let res2 = sem_unlink(semaphoreName)
expectEqual(0, res2)
}

// Fail because the file descriptor is invalid.
POSIXTests.test("ioctl(CInt, UInt, CInt): fail") {
let fd = open(fn, 0)
expectEqual(-1, fd)
expectEqual(ENOENT, errno)

// A simple check to verify that ioctl is available
let _ = ioctl(fd, 0, 0)
expectEqual(EBADF, errno)
}

#if os(Linux)
// Successful creation of a socket and listing interfaces
POSIXTests.test("ioctl(CInt, UInt, UnsafeMutablePointer<Void>): listing interfaces success") {
// Create a socket
let sock = socket(PF_INET, 1, 0)
expectGT(Int(sock), 0)

// List interfaces
var ic = ifconf()
let io = ioctl(sock, UInt(SIOCGIFCONF), &ic);
expectGE(io, 0)

//Cleanup
let res = close(sock)
expectEqual(0, res)
}
#endif

// Fail because file doesn't exist.
POSIXTests.test("fcntl(CInt, CInt): fail") {
let fd = open(fn, 0)
Expand Down