Hacker News new | ask | show | jobs
by Vogtinator 350 days ago
errno is in thread-local storage (TLS)
2 comments

Notably, the POSIX Threads API itself (i.e. pthread_ routines) returns errors directly rather than through errno.
It was a common design of new operating system APIs, not encumbered by PDP-11 compatibility, in the 1980s.

* https://tty0.social/@JdeBP/114816928464571239

Even some of the later augmentations in MS/PC/DR-DOS did things like return an error code in AX and the result in (say) CX instead of using AX and CF.

Yes. It is too bad that they didn't use a similar solution for the current working directory. Chdir() is process-wide, not thread local :(
`openat` has basically solved that since 2.6.16 (which came out in 2006). There are still some uncommon APIs have been slow to gain `at` variants but there's usually a workaround (for example, `getxattrat` and family were only added in 6.13 (this January), but can be implemented in terms of `openat` + `fgetxattr`)
> can be implemented in terms of `openat` + `fgetxattr`

Except for symlinks. `fgetxattr` requires a file opened for read or write, but symlinks can only be opened as `O_PATH`.

This is good because the current directory conceptually process wide for the user as well. If your program isn't a shell or performs a similar function then you probably should not change the working directory at all.

If you need thread-specific local paths just use one of the *at() variants that let you explicitly specify a directory handle that your path is relative to.