5

Windows has different types of status codes, HRESULT and NTSTATUS for instance, that allow for representing both success and failure values. This allows for macros like FAILED and NT_SUCCESS. One example of a success status code is STATUS_PENDING that indicates an IO is not yet complete.

I'm trying to map similar style status codes in Linux. I have a (WIP) cross platform library that uses the above type of status codes on Windows (user mode and kernel mode). I have my own "FAILED" macro that wraps the above macros. I need an equivalent for Linux for it all.

I've looked around and haven't found anything interesting/helpful. For any system/platform function (sockets, thread, locks) that might return and error, I need to make sure it is converted to the appropriate status code type. Windows has lots of helpers for things like this, but I'm not sure about Linux.

Psuedocode example:

MY_STATUS SendAsync(...) {
   ASYNC_OPERATION* Operation = MY_ALLOC(...)
   if (Operation == NULL) {
       return MY_OUT_OF_MEMORY_ERROR; // Error status
   }
   // Build the operation
   MY_STATUS Status = QueueOperation(Operation);
   if (MY_FAILED(Status)) {
       return Status;
   }
   return MY_STATUS_PENDING; // Success status
}

The caller should then be able to have the following code:

MY_STATUS Status = SendAsync(...)
if (MY_FAILED(Status)) {
   // Bail
}
// Handle success

I think the crux of the problem is that I don't know how to stuff status codes from system/platform function calls into a generic type that supports success/fail. Generally, it looks like all the status codes are positive, so I could just say my success error codes are <= 0. But I'm not sure that's a great solution.

To be clear, since it was proposed that this question might the answer, I'm not trying to force the Linux status/error codes into HRESULT format explicitly. I'm just trying to find some way to represent error codes as either success or failure. If I have to convert system error codes (negate them?) along the way, that's acceptable. If there is no real solution to this problem, then that would be an acceptable answer (though not desired), and I'll have to find some work around.

Nick Banks
  • 4,102
  • 5
  • 35
  • 60
  • Please explicitly include a problem statement (i.e. a question with a question mark) in your question. – S.S. Anne Aug 20 '19 at 21:09
  • @shellter That isn't necessary for this question. It's asking about something other than code that's not working. – S.S. Anne Aug 20 '19 at 21:18
  • Can you give us a few examples of what you're trying to do? – S.S. Anne Aug 20 '19 at 21:22
  • 1
    @JL2210 It's a cross platform network library, that both has it's own error codes (success and failure) and returns system error codes. – Nick Banks Aug 20 '19 at 21:27
  • @NickBanks Any *specific* examples? – S.S. Anne Aug 20 '19 at 21:31
  • 2
    Possible duplicate of [Convert errno.h error values to Win32 GetLastError() equivalents](https://stackoverflow.com/q/3952342/608639), [In Windows, is there any way to convert an errno into an HRESULT?](https://stackoverflow.com/q/12352922/608639), etc. – jww Aug 21 '19 at 01:07
  • I also think you should handle errors independently at the library level based on the OS results. That is, your application maps `Win32:ERROR_ACCESS_DENIED` and `Linux:EACCES` to a library error like `MyLib:ACCESS_DENIED`. You can also map other types of errors, like `SQLite:SQLITE_PERM` into `MyLib:ACCESS_DENIED`. I found that is cleanest based on a cross-platform library that supported Android, BSDs, iOS, Linux, OS X and Windows. – jww Aug 21 '19 at 01:25

2 Answers2

6

For error codes in linux there are the constants defined in errno.h header file. The error constants are available from man page man 3 errno. Also there is: What errno means

As for exit status codes, there is no standard set of codes. The value 0 has always denoting success, and non-zero value has denoted an error state. Bash, for example has this Bash exit status information.

All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage, generally invalid options or missing arguments.

There is sysexits which was an attempt to define a set of status codes, but it has been largely ignored in the field of linux software.

For more info: Exit Status codes in Linux

suspectus
  • 14,884
  • 8
  • 41
  • 53
  • So, bottom line, there isn't any clear definition of error codes in Linux? I'll just have to figure something out for my library? – Nick Banks Aug 22 '19 at 14:26
  • *Error codes* , yes. Status codes, no. The full set of linux error codes are listed here: http://man7.org/linux/man-pages/man3/errno.3.html – suspectus Aug 22 '19 at 14:30
0

To get the error return code from a function in Linux, use the function's return value (usually nonzero on error) and the value of the errno variable, from errno.h.

Search for "linux errno codes" or run man errno on Linux to get more information.

errno (and return statuses) are usually represented as an int, so that's what I'd go with if you're looking for a type to use.

For example, checking to see if the return code of non-blocking connect is negative one and errno is EINPROGRESS is the same as checking for STATUS_PENDING in Windows.

Here's some pseudocode to respond to your pseudocode example:

#ifdef _WIN32
typedef /* Windows stuff */ MY_STATUS;
# define MY_FAILED(x) /* Windows stuff */
#else /* POSIX, other reasonable operating systems, etc. */
typedef int MY_STATUS;
# define MY_FAILED(x) ((x) == -1)
#endif

Of course, this assumes that your functions will return int or something similar, which may not be the case. But you get the idea.

S.S. Anne
  • 13,819
  • 7
  • 31
  • 62