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

Exporting ioctl #2946

merged 1 commit into from
Jun 9, 2016

Conversation

uraimo
Copy link
Contributor

@uraimo uraimo commented Jun 8, 2016

What's in this pull request?

This pull request exports the variadic ioctl system call, needed to interact with device files on Linux and other Unix-like systems. At the moment, Linux Swift libraries that interact with a device driver need to implement a c wrapper for ioctl in a separate clang/llvm module (essentially the same c module is duplicated in every library that does something with devices).

Having ioctl available in Swift would allow the creation of pure-Swift device libraries (more portable, easier to compile).

The two new functions contained in this PR follow the same approach of the already exported fcntl, one function with an int parameter and one with a pointer. But unlike fcntl i don't expect ioctl to be used internally in Foundation any time soon.

On the testing side, the only thing that can be tested across platforms is the presence of the function, since aside from a few obscure commands related to tty devices, ioctl requests are heavily platform-dependent. I'm including a single test that tries an ioctl on a non-existing file (failure is expected).


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@uraimo uraimo changed the title Exported ioctl() Exporting ioctl Jun 8, 2016
@modocache
Copy link
Contributor

@swift-ci please test

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

extern int
_swift_Platform_ioctl(int fd, unsigned long int request, int value) {
return fcntl(fd, request, value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be ioctl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixed and testing it locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests passed on OSX&Linux.

@jrose-apple
Copy link
Contributor

Is there anything that can be tested across most Linux systems? I'd be fine with a #if around that part of the test if it meant we got a little more guarantee that things were actually working.

@uraimo
Copy link
Contributor Author

uraimo commented Jun 8, 2016

I've updated the PR:

  • Fixed the fcntl/ioctl typo
  • Added missing SWIFT_CC(swift)
  • Removed @warn_unused_result
  • Added a Linux-only test that retrieves the interface addresses for a newly created socket (should work on every Linux and probably on BSDs too but i put it inside an #if(Linux) anyway)

Tested locally on both OSX and Linux (Ubuntu 15).

@jrose-apple
Copy link
Contributor

Seems okay to me. @modocache, @gribozavr?

@swift-ci Please test

@gribozavr
Copy link
Contributor

@uraimo Thank you! What about an overload for commands without an argument?

var vptr = UnsafeMutablePointer<Void>(ptr)
let io = ioctl(sock, UInt(SIOCGIFCONF), vptr);
expectGE(io, 0)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to do this:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, way better, that was a remnant of previous ioctl attempts.

@uraimo
Copy link
Contributor Author

uraimo commented Jun 9, 2016

Makes sense, added.

@gribozavr
Copy link
Contributor

@swift-ci Please test and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants