Skip to content

Commit df68c08

Browse files
committed
Merge pull request #2946 from uraimo/ioctl-patch
2 parents fb6b63b + fe6007e commit df68c08

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

stdlib/public/Platform/Misc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <semaphore.h>
1515
#include <sys/types.h>
1616
#include <sys/stat.h>
17+
#include <sys/ioctl.h>
1718

1819
#include "swift/Runtime/Config.h"
1920

@@ -49,6 +50,18 @@ extern int _swift_Platform_fcntlPtr(int fd, int cmd, void* ptr) {
4950
return fcntl(fd, cmd, ptr);
5051
}
5152

53+
SWIFT_CC(swift)
54+
extern int
55+
_swift_Platform_ioctl(int fd, unsigned long int request, int value) {
56+
return ioctl(fd, request, value);
57+
}
58+
59+
SWIFT_CC(swift)
60+
extern int
61+
_swift_Platform_ioctlPtr(int fd, unsigned long int request, void* ptr) {
62+
return ioctl(fd, request, ptr);
63+
}
64+
5265
#if defined(__APPLE__)
5366
#define _REENTRANT
5467
#include <math.h>

stdlib/public/Platform/Platform.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,48 @@ public var S_IWRITE: mode_t { return S_IWUSR }
277277
public var S_IEXEC: mode_t { return S_IXUSR }
278278
#endif
279279

280+
//===----------------------------------------------------------------------===//
281+
// ioctl.h
282+
//===----------------------------------------------------------------------===//
283+
284+
@_silgen_name("_swift_Platform_ioctl")
285+
internal func _swift_Platform_ioctl(
286+
_ fd: CInt,
287+
_ request: UInt,
288+
_ value: CInt
289+
) -> CInt
290+
291+
@_silgen_name("_swift_Platform_ioctlPtr")
292+
internal func _swift_Platform_ioctlPtr(
293+
_ fd: CInt,
294+
_ request: UInt,
295+
_ ptr: UnsafeMutablePointer<Void>
296+
) -> CInt
297+
298+
public func ioctl(
299+
_ fd: CInt,
300+
_ request: UInt,
301+
_ value: CInt
302+
) -> CInt {
303+
return _swift_Platform_ioctl(fd, request, value)
304+
}
305+
306+
public func ioctl(
307+
_ fd: CInt,
308+
_ request: UInt,
309+
_ ptr: UnsafeMutablePointer<Void>
310+
) -> CInt {
311+
return _swift_Platform_ioctlPtr(fd, request, ptr)
312+
}
313+
314+
public func ioctl(
315+
_ fd: CInt,
316+
_ request: UInt
317+
) -> CInt {
318+
return _swift_Platform_ioctl(fd, request, 0)
319+
}
320+
321+
280322
//===----------------------------------------------------------------------===//
281323
// unistd.h
282324
//===----------------------------------------------------------------------===//

test/1_stdlib/POSIX.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,36 @@ POSIXTests.test("sem_open existing O_EXCL fail") {
8282
let res2 = sem_unlink(semaphoreName)
8383
expectEqual(0, res2)
8484
}
85+
86+
// Fail because the file descriptor is invalid.
87+
POSIXTests.test("ioctl(CInt, UInt, CInt): fail") {
88+
let fd = open(fn, 0)
89+
expectEqual(-1, fd)
90+
expectEqual(ENOENT, errno)
8591

92+
// A simple check to verify that ioctl is available
93+
let _ = ioctl(fd, 0, 0)
94+
expectEqual(EBADF, errno)
95+
}
96+
97+
#if os(Linux)
98+
// Successful creation of a socket and listing interfaces
99+
POSIXTests.test("ioctl(CInt, UInt, UnsafeMutablePointer<Void>): listing interfaces success") {
100+
// Create a socket
101+
let sock = socket(PF_INET, 1, 0)
102+
expectGT(Int(sock), 0)
103+
104+
// List interfaces
105+
var ic = ifconf()
106+
let io = ioctl(sock, UInt(SIOCGIFCONF), &ic);
107+
expectGE(io, 0)
108+
109+
//Cleanup
110+
let res = close(sock)
111+
expectEqual(0, res)
112+
}
113+
#endif
114+
86115
// Fail because file doesn't exist.
87116
POSIXTests.test("fcntl(CInt, CInt): fail") {
88117
let fd = open(fn, 0)

0 commit comments

Comments
 (0)