Questions for POSIX if possible, else for Linux-specific platforms:
- Is there user-defined
errno
values? (as for signalsSIGUSR1
andSIGUSR2
) - How to find an
errno
value not used by the system? (negative values?) - How to prevent
strerror()
break? (check before theerrnum
sign?)
My code open()
a resource and notifies another object. The notification Event
conveys the system errno
if a failure occurs (zero on success).
But failures can also be detected in my code, e.g. if(count>=size)
. And I want to reuse the field Event::errnum
to convey this failure. Therefore my user-defined failure code should not overlap system-defined errno
values.
I have found errno
range 9000–11000 reserved for user, but this seems to be specific to Transaction Processing Facility...
Note my question is not about library-defined errno. The struct Event
is not exposed outside my code. My code does not overwrite errno
.
Below snippet is in c++ but my question also applies for c.
#include <cerrno>
#define E_MY_USER_DEFINED_ERROR 9999
struct Event
{
int fd;
int errnum;
};
struct Foo
{
Foo( int sz ) : count(0), size(sz) {}
Event open( const char name[] )
{
if( count >= size )
return { -1, E_MY_USER_DEFINED_ERROR };
int fd = 1; // TODO: open ressource...
if (fd < 0) return { -1, errno };
else return { fd, 0 };
}
int count, size;
};
int main()
{
Foo bar(0);
Event e = bar.open("my-ressource");
// send Event to another object...
}