ioctl's suck?
The other day Linus (I think) made the statement, that I don't disagree with, that I will parapharse as "ioctl's suck". If I recall correctly and understand he was saying that a device that uses ioctls is broken. (this is my paraphrase and if it offends anybody it is my fault not the original author's).
This got me to thinking about a device driver that I'm working on. Currently I have some ioctls to handle status and out of band messages and I'm wondering about eliminating the ioctls. I'm wondering if anybody has any ideas or opinions that they would like to share, about just what i wrong with ioctls and/or how to avoid them.
I can see a number of problems with ioctls that I can'tr quite put into words.
I could add a control device and pass ascii strings for status and OOB messages, would that be an improvement?
Signature · 6 lines
A: Because it messes up the order in which people normally read text. Q: Why is top-posting a bad thing? Brian Beattie LFS12947 | "Honor isn't about making the right choices. [email protected] | It's about dealing with the consequences." www.beattie-home.net | -- Midori Koto
Quoted 3 lines · Brian Beattie
The other day Linus (I think) made the statement, that I don't disagree with, that I will parapharse as "ioctl's suck". If I recall correctly and understand he was saying that a device that uses ioctls is broken.
...
Quoted 2 lines
I could add a control device and pass ascii strings for status and OOB messages, would that be an improvement?
The problem is that the classical unix I/O abstraction really only handles two cases well, viz. "read next chunk of data" and "write this data and advance position". Anything else is seen as something strange.
ioctls() are the equivalent of device-specific RPCs (remote procedure calls): you present some arguments and a call code, and you get some results. Nothing wrong with that.
In practice however there are some pitfalls. For instance, ioctls() are often abused for quick hacks with little concern for backwards compatibility. So when someone changes an ioctl, and breaks user-space, who gets the blame? ioctls in general! I would argue that that is why, and the main reason why, some people put ioctls() down.
There may be something wrong with people abusing ioctls, but that is not a problem with ioctls per se. (I can easily design broken ABIs around read()/write()/open()-only interfaces.)
Quoted 2 lines
I could add a control device and pass ascii strings for status and OOB messages, would that be an improvement?
Usually not. The idea that ioctl can be replaced with ascii messages is clueless rubbish that generally gets spouted by people with their head in the clouds of conceptual elegance and no grasp of reality.
There are certain things you can expose that way usefully via sysfs
- things like general stateless status information. Ioctl however provides an interface tied to file handle not name (which is essential in a hotplug environment) and an ordering to events so you know the response you get matches the query you made.
There are good things to do with ioctls:
- Make sure the size of all the fields are consistent across 32/64bit (for 32bit binaries on 64bit)
- Use the IOR/IOW/IORW macros
Alan
Quoted 6 lines · Alan Cox
Quoted 2 lines
I could add a control device and pass ascii strings for status and OOB messages, would that be an improvement?
Usually not. The idea that ioctl can be replaced with ascii messages is clueless rubbish that generally gets spouted by people with their head in the clouds of conceptual elegance and no grasp of reality.
Yeah, I can see that. Though the case has been made that ioctls are only usable by C/C++ while I'm sure there are other languages, I'm working in the embedded space and mostly work with C and sh so my experience is limited and that is of less direct importance to me.
Quoted 6 lines
There are certain things you can expose that way usefully via sysfs
- things like general stateless status information. Ioctl however provides an interface tied to file handle not name (which is essential in a hotplug environment) and an ordering to events so you know the response you get matches the query you made.
Yeah coherence (if I'm using that word correctly) would be critical, and a separate file handle would make that tricky.
Quoted 1 line
Alan
Signature · 6 lines
A: Because it messes up the order in which people normally read text. Q: Why is top-posting a bad thing? Brian Beattie LFS12947 | "Honor isn't about making the right choices. [email protected] | It's about dealing with the consequences." www.beattie-home.net | -- Midori Koto