shared,core: add "nm-errno.h"
This will be our extension on top of <errno.h>.
We want to use (integer) error numbers, that can both
contain native errors from <errno.h> and our own defines,
both merge in one domain. That is, we will reserve a small
range of integers for our own defines (that hopefully won't
clash with errors from <errno.h>).
We can use this at places where GError is too cumbersome to use.
The advantage is, that our error numbers extend <errno.h> and can
be mixed.
This is what "src/platform/nm-netlink.h" already does with nl_errno(). Next,
the netlink errors from there will be merged into "nm-errno.h".
Also, platform has NMPlatformError, which are a distinct set of error
numbers. But these work differently in the sense that negative values
represent codes from <errno.h> and positive numbers are our own platform
specific defines. NMPlatformError will also be merged into "nm-errno.h".
"nm-errno.h" will unify the error handling of platform and netlink,
making it more similar to what we are used to from systemd, and give
room to extend it for our own purpose.
2018-12-22 12:41:04 +01:00
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2018 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __NM_ERRNO_H__
|
|
|
|
|
#define __NM_ERRNO_H__
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2018-12-22 12:45:01 +01:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2018-12-22 12:57:30 +01:00
|
|
|
enum {
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
_NM_ERRNO_MININT = G_MININT,
|
|
|
|
|
_NM_ERRNO_MAXINT = G_MAXINT,
|
2018-12-22 12:57:30 +01:00
|
|
|
_NM_ERRNO_RESERVED_FIRST = 100000,
|
2018-12-22 12:45:01 +01:00
|
|
|
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
/* an unspecified error. */
|
2018-12-22 12:57:30 +01:00
|
|
|
NME_UNSPEC = _NM_ERRNO_RESERVED_FIRST,
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
|
|
|
|
|
/* A bug, for example when an assertion failed.
|
|
|
|
|
* Should never happen. */
|
2018-12-22 12:57:30 +01:00
|
|
|
NME_BUG,
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
|
|
|
|
|
/* a native error number (from <errno.h>) cannot be mapped as
|
|
|
|
|
* an nm-error, because it is in the range [_NM_ERRNO_RESERVED_FIRST,
|
|
|
|
|
* _NM_ERRNO_RESERVED_LAST]. */
|
2018-12-22 12:57:30 +01:00
|
|
|
NME_NATIVE_ERRNO,
|
|
|
|
|
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
/* netlink errors. */
|
2018-12-22 12:57:30 +01:00
|
|
|
NME_NL_SEQ_MISMATCH,
|
|
|
|
|
NME_NL_MSG_TRUNC,
|
|
|
|
|
NME_NL_MSG_TOOSHORT,
|
|
|
|
|
NME_NL_DUMP_INTR,
|
|
|
|
|
NME_NL_ATTRSIZE,
|
|
|
|
|
NME_NL_BAD_SOCK,
|
|
|
|
|
NME_NL_NOADDR,
|
|
|
|
|
NME_NL_MSG_OVERFLOW,
|
|
|
|
|
|
platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.
This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.
The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-22 14:13:05 +01:00
|
|
|
/* platform errors. */
|
|
|
|
|
NME_PL_NOT_FOUND,
|
|
|
|
|
NME_PL_EXISTS,
|
|
|
|
|
NME_PL_WRONG_TYPE,
|
|
|
|
|
NME_PL_NOT_SLAVE,
|
|
|
|
|
NME_PL_NO_FIRMWARE,
|
|
|
|
|
NME_PL_OPNOTSUPP,
|
|
|
|
|
NME_PL_NETLINK,
|
|
|
|
|
NME_PL_CANT_SET_MTU,
|
|
|
|
|
|
2018-12-22 12:57:30 +01:00
|
|
|
_NM_ERRNO_RESERVED_LAST_PLUS_1,
|
|
|
|
|
_NM_ERRNO_RESERVED_LAST = _NM_ERRNO_RESERVED_LAST_PLUS_1 - 1,
|
|
|
|
|
};
|
2018-12-22 12:45:01 +01:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2018-12-22 13:09:21 +01:00
|
|
|
static inline int
|
2018-12-22 12:57:30 +01:00
|
|
|
nm_errno_native (int errsv)
|
2018-12-22 13:09:21 +01:00
|
|
|
{
|
|
|
|
|
/* several API returns negative errno values as errors. Normalize
|
|
|
|
|
* negative values to positive values.
|
|
|
|
|
*
|
|
|
|
|
* As a special case, map G_MININT to G_MAXINT. If you care about the
|
2018-12-22 12:57:30 +01:00
|
|
|
* distinction, then check for G_MININT before.
|
|
|
|
|
*
|
|
|
|
|
* Basically, this normalizes a plain errno to be non-negative. */
|
2018-12-22 13:09:21 +01:00
|
|
|
return errsv >= 0
|
|
|
|
|
? errsv
|
|
|
|
|
: ((errsv == G_MININT) ? G_MAXINT : -errsv);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-22 12:45:01 +01:00
|
|
|
static inline int
|
2018-12-22 12:57:30 +01:00
|
|
|
nm_errno (int nmerr)
|
2018-12-22 12:45:01 +01:00
|
|
|
{
|
2018-12-22 12:57:30 +01:00
|
|
|
/* Normalizes an nm-error to be positive. Various API returns negative
|
2018-12-22 12:45:01 +01:00
|
|
|
* error codes, and this function converts the negative value to its
|
|
|
|
|
* positive.
|
|
|
|
|
*
|
2018-12-22 12:57:30 +01:00
|
|
|
* It's very similar to nm_errno_native(), but not exactly. The difference is that
|
|
|
|
|
* nm_errno_native() is for plain errno, while nm_errno() is for nm-error numbers.
|
|
|
|
|
* Yes, nm-error number are ~almost~ the same as errno, except that a particular
|
|
|
|
|
* range (_NM_ERRNO_RESERVED_FIRST, _NM_ERRNO_RESERVED_LAST) is reserved. The difference
|
|
|
|
|
* between the two functions is only how G_MININT is mapped.
|
2018-12-22 12:45:01 +01:00
|
|
|
*
|
2018-12-22 13:35:57 +01:00
|
|
|
* See also nm_errno_from_native() below. */
|
|
|
|
|
return nmerr >= 0
|
|
|
|
|
? nmerr
|
|
|
|
|
: ((nmerr == G_MININT) ? NME_BUG : -nmerr);
|
2018-12-22 12:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int
|
2018-12-22 13:35:57 +01:00
|
|
|
nm_errno_from_native (int errsv)
|
2018-12-22 12:45:01 +01:00
|
|
|
{
|
2018-12-22 12:57:30 +01:00
|
|
|
/* this maps a native errno to a (always non-negative) nm-error number.
|
2018-12-22 12:45:01 +01:00
|
|
|
*
|
2018-12-22 12:57:30 +01:00
|
|
|
* Note that nm-error numbers are embedded into the range of regular
|
|
|
|
|
* errno. The only difference is, that nm-error numbers reserve a
|
|
|
|
|
* range (_NM_ERRNO_RESERVED_FIRST, _NM_ERRNO_RESERVED_LAST) for their
|
|
|
|
|
* own purpose.
|
2018-12-22 12:45:01 +01:00
|
|
|
*
|
2018-12-22 12:57:30 +01:00
|
|
|
* That means, converting an errno to nm-error number means in
|
2018-12-22 12:45:01 +01:00
|
|
|
* most cases just returning itself (negative values are normalized
|
2018-12-22 12:57:30 +01:00
|
|
|
* to be positive). Only values G_MININT and [_NM_ERRNO_RESERVED_FIRST, _NM_ERRNO_RESERVED_LAST]
|
2018-12-22 13:35:57 +01:00
|
|
|
* are coerced to the special value NME_NATIVE_ERRNO, as they cannot
|
2018-12-22 12:57:30 +01:00
|
|
|
* otherwise be represented in nm-error number domain. */
|
|
|
|
|
if (errsv < 0) {
|
|
|
|
|
return G_UNLIKELY (errsv == G_MININT)
|
|
|
|
|
? NME_NATIVE_ERRNO
|
|
|
|
|
: -errsv;
|
|
|
|
|
}
|
|
|
|
|
return G_UNLIKELY ( errsv >= _NM_ERRNO_RESERVED_FIRST
|
|
|
|
|
&& errsv <= _NM_ERRNO_RESERVED_LAST)
|
2018-12-22 13:35:57 +01:00
|
|
|
? NME_NATIVE_ERRNO
|
2018-12-22 12:45:01 +01:00
|
|
|
: errsv;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-22 13:35:57 +01:00
|
|
|
const char *nm_strerror (int nmerr);
|
2018-12-22 12:45:01 +01:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
shared,core: add "nm-errno.h"
This will be our extension on top of <errno.h>.
We want to use (integer) error numbers, that can both
contain native errors from <errno.h> and our own defines,
both merge in one domain. That is, we will reserve a small
range of integers for our own defines (that hopefully won't
clash with errors from <errno.h>).
We can use this at places where GError is too cumbersome to use.
The advantage is, that our error numbers extend <errno.h> and can
be mixed.
This is what "src/platform/nm-netlink.h" already does with nl_errno(). Next,
the netlink errors from there will be merged into "nm-errno.h".
Also, platform has NMPlatformError, which are a distinct set of error
numbers. But these work differently in the sense that negative values
represent codes from <errno.h> and positive numbers are our own platform
specific defines. NMPlatformError will also be merged into "nm-errno.h".
"nm-errno.h" will unify the error handling of platform and netlink,
making it more similar to what we are used to from systemd, and give
room to extend it for our own purpose.
2018-12-22 12:41:04 +01:00
|
|
|
#endif /* __NM_ERRNO_H__ */
|