Patrick Collins
Patrick Collins

Reputation: 10604

Do errno values differ across *nix systems?

I'm writing a library that emits Linux kernel 4.4 errno values when things go wrong --- these are defined in a header for the program and aren't necessarily the same as the host errno values. (There's a good reason for doing this, and I can't change this part of it.) But I'm guaranteed that the environment it's running on:

I realize that in theory, the C/POSIX standards allow errno values to be whatever the implementer wants them to be, and in theory, I could compile my own kernel with whatever bizarre errno values I want. But then I would never be able to reliably use any binary that I didn't compile myself, so I'm probably going to have a bad time, and I'm not going to be surprised when things break at random.

In practice, can I count on this kind of host having the same errno values as the values defined in the kernel's errno.h? i.e. can I rely on getting sensible behavior from the host's perror if I directly set errno in my library?

Upvotes: 1

Views: 287

Answers (3)

DigitalRoss
DigitalRoss

Reputation: 146231

Actually it really depends on the error. Below about 35 they are the same, except for EAGAIN which isn't so much changed but split differently. (Who gets the old number? EAGAIN or EDEADLK?)

I can think of two things that would work:

  1. Perhaps you can just return errors that are common to Linux, OSX, and BSD.
  2. You could compile a master include (thanks, @Chris Beck) and make some kind of hash map keyed by printable names, then return the right value at runtime.

Upvotes: 2

rici
rici

Reputation: 241931

The names are reliable, at least the ones which are in Posix. The actual values, not. They certainly differ between Linux and *BSD, for example.

If you translate using the host's errno.h, you will be fine. Anything else is speculative.

Upvotes: 2

Chris Beck
Chris Beck

Reputation: 16224

Here's a very large list comparing ERRNO values from POSIX with the actual associated messages and numbers on various systems. Some differences between Linux and BSD for instance are obvious in the spreadsheet:

http://www.ioplex.com/~miallen/errcmp.html

So the answer is, maybe in practice it's fairly safe, depending on exactly what code you are looking at? For instance ENOMEM, EACCESS, are the same on all listed platforms here.

But in general no.

Upvotes: 4

Related Questions