4 ms·
A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- i
by userbinator 10y ago
A value in the range between -4095 and -1 indicates an error, it is -errno.
The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but the syscall wrappers. And thanks to POSIX standardising this mechanism, the alternative will likely never get much adoption; of course, if you write your own syscall wrappers like this article, then you can skip that bloat.
For now this guide is linux-only, but I will be writing a windows version when I feel like firing up a virtual machine.
Unfortunately the Windows syscalls are not officially documented and even less stable than on Linux, changing even between service packs.
http://j00ru.vexillium.org/ntapi/ http://j00ru.vexillium.org/ntapi/
http://j00ru.vexillium.org/ntapi_64/ http://j00ru.vexillium.org/ntapi_64/
At least on Linux the first few (i.e. the oldest, most common and useful) syscalls have not really moved around over the years:
https://filippo.io/linux-syscall-table/ https://filippo.io/linux-syscall-table/
- masklinn 10y ago> At least on Linux the first few (i.e. the oldest, most common and useful) syscalls have not really moved around over the years IIRC raw syscalls are an officially supported kernel API, that's why you can have alternate libc implementations (e.g. musl), and Linux is an oddity in that, on most systems even if the syscalls are fairly stable there are no actual guarantees with respect to them, and the only officially supported interface to the kernel is the standard library. OSX does not allow statically linking libSystem for that reason for instance.
- 0xcde4c3db 10y agoAnother example: for OpenBSD part of the manual upgrade procedure (not the recommended method, but useful if you don't have a console to run the installer) is to make a copy of /sbin/reboot, because there's a good chance that the new /sbin/reboot you're about to install will have the wrong syscalls for the running kernel.
- sebcat 10y agogolang also targets syscalls instead of the C standard library (or other libraries except for windows, and maybe others), which is interesting on e.g., Darwin: https://github.com/golang/go/issues/17490 https://github.com/golang/go/issues/17490
- Cyph0n 10y agoInteresting, thanks for linking that.
- masklinn 10y agoYeah the linked issue (16570) is the most interesting one, with the Go runtime breaking multiple times in the runup to Sierra as Apple changed the ABI of the underlying gettimeofday syscall.
- iainmerrick 10y agoWow, that seems like a really bad approach for Go to take. So is anybody actually using Go on OS X? I guess maybe not, if all the real Go deployments are on servers running Linux.
- zimpenfish 10y agoI do a bunch of development work using Go on OSX but I do deploy the resulting solutions on Linux.
- iainmerrick 10y agoAha, that's kind of what I'd figured. Work on Mac, deploy on Linux. It makes me think twice about e.g. using Go as part of the build workflow in a big project if it's going to randomly stop working on various OS X versions.
- bogomipz 10y ago>"golang also targets syscalls instead of the C standard library" What would have been the reasons for targeting syscalls directly instead of the C standard library?
- valarauca1 10y agoIIRC raw syscalls are an officially supported kernel API The term you are looking for is We don't break userland. Once a systemcall goes live it is engraved in stone. This is why if you look though the syscall table you see call, call2, call_ext.
- ketralnis 10y agoYou're implying that exposing syscalls as the ABI instead of a higher level ABI is the only way to get backwards compatibility but that's not true. OS X can change its syscalls without breaking userland in exactly the way GP says: don't allow static linking of e.g. libSystem and its ABI is the ABI you're expected to use instead of directly calling syscalls. Sure, Linux makes promises about its syscalls. But that's not the only way to get robust backwards compatibility and as GP says it's the oddity in that respect.
- goldfire 10y ago> Unfortunately the Windows syscalls are not officially documented and even less stable than on Linux, changing even between service packs. You can get pretty close though; it's possible to skip the C runtime and most of the other user space libraries and call into ntdll directly. Many of the functions it exports are fairly thin wrappers over the system calls.
- int_19h 10y agontdll.dll is still not a stable API - if you use anything exported from it (excepting a few documented ones), you basically have zero guarantee that your app will still be working with the next Windows update.
- 77pt77 10y ago> The syscall/errno stuff has always seemed unusual, inelegant, and inefficient And non-reentrant. errno is usually defined as TLS (thread local storage). It's a mess.
- bodyfour 10y agoYes, if UNIX had supported multithreading from the start, there is no way they would have chosen "errno" as the solution. Obvious in hindsight, not so much in the 1970s.
- PeCaN 10y agoSomehow VMS managed to avoid that same mistake in the 70s.
- 77pt77 10y agoI keep hearing that a lot! VMS sounds amazing, but somehow I'm still thinking this might be nostalgia.
- bogomipz 10y agoCan you elaborate on how "errno"'s design is rooted in the fact that there was no multithreading at the time? I am not understanding the connection. Thanks.
- lomnakkus 10y agoMultithreading usually means that threads share the address space and so can (inadvertently) stomp on each other's "errno". (Hence making it Thread-Local Storage to solve that issue. Doesn't solve the reentrancy issue, though.) If multithreading were pervasive nobody in their right mind would choose to use a global variable for error status codes.
- bogomipz 10y agoAh yes that makes sense. Thanks!
- hermitdev 10y agoI didn't see it mentioned at a cursory glance of the comments, but syscalls != standard library. Standard Libraray for C is that which is defined by the ISO C standard. syscalls are whatever *nix decides them to be. Additionally Standard Library != posix. Sure, there may be some overlap or interleaving between Standard Libraries, syscalls, and posix, but they are definitely not the same.