|
Packit Service |
b23acc |
// SPDX-License-Identifier: LGPL-2.1+
|
|
Packit Service |
b23acc |
/*
|
|
Packit Service |
b23acc |
* Copyright (C) 2018 Red Hat, Inc.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#ifndef __NM_ERRNO_H__
|
|
Packit Service |
b23acc |
#define __NM_ERRNO_H__
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include <errno.h>
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
enum _NMErrno {
|
|
Packit Service |
b23acc |
_NM_ERRNO_MININT = G_MININT,
|
|
Packit Service |
b23acc |
_NM_ERRNO_MAXINT = G_MAXINT,
|
|
Packit Service |
b23acc |
_NM_ERRNO_RESERVED_FIRST = 100000,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* when we cannot represent a number as positive number, we resort to this
|
|
Packit Service |
b23acc |
* number. Basically, the values G_MININT, -NME_ERRNO_SUCCESS, NME_ERRNO_SUCCESS
|
|
Packit Service |
b23acc |
* and G_MAXINT all map to the same value. */
|
|
Packit Service |
b23acc |
NME_ERRNO_OUT_OF_RANGE = G_MAXINT,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* Indicate that the original errno was zero. Zero denotes *no error*, but we know something
|
|
Packit Service |
b23acc |
* went wrong and we want to report some error. This is a placeholder to mean, something
|
|
Packit Service |
b23acc |
* was wrong, but errno was zero. */
|
|
Packit Service |
b23acc |
NME_ERRNO_SUCCESS = G_MAXINT - 1,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* an unspecified error. */
|
|
Packit Service |
b23acc |
NME_UNSPEC = _NM_ERRNO_RESERVED_FIRST,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* A bug, for example when an assertion failed.
|
|
Packit Service |
b23acc |
* Should never happen. */
|
|
Packit Service |
b23acc |
NME_BUG,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* a native error number (from <errno.h>) cannot be mapped as
|
|
Packit Service |
b23acc |
* an nm-error, because it is in the range [_NM_ERRNO_RESERVED_FIRST,
|
|
Packit Service |
b23acc |
* _NM_ERRNO_RESERVED_LAST]. */
|
|
Packit Service |
b23acc |
NME_NATIVE_ERRNO,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* netlink errors. */
|
|
Packit Service |
b23acc |
NME_NL_SEQ_MISMATCH,
|
|
Packit Service |
b23acc |
NME_NL_MSG_TRUNC,
|
|
Packit Service |
b23acc |
NME_NL_MSG_TOOSHORT,
|
|
Packit Service |
b23acc |
NME_NL_DUMP_INTR,
|
|
Packit Service |
b23acc |
NME_NL_ATTRSIZE,
|
|
Packit Service |
b23acc |
NME_NL_BAD_SOCK,
|
|
Packit Service |
b23acc |
NME_NL_NOADDR,
|
|
Packit Service |
b23acc |
NME_NL_MSG_OVERFLOW,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* platform errors. */
|
|
Packit Service |
b23acc |
NME_PL_NOT_FOUND,
|
|
Packit Service |
b23acc |
NME_PL_EXISTS,
|
|
Packit Service |
b23acc |
NME_PL_WRONG_TYPE,
|
|
Packit Service |
b23acc |
NME_PL_NOT_SLAVE,
|
|
Packit Service |
b23acc |
NME_PL_NO_FIRMWARE,
|
|
Packit Service |
b23acc |
NME_PL_OPNOTSUPP,
|
|
Packit Service |
b23acc |
NME_PL_NETLINK,
|
|
Packit Service |
b23acc |
NME_PL_CANT_SET_MTU,
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
_NM_ERRNO_RESERVED_LAST_PLUS_1,
|
|
Packit Service |
b23acc |
_NM_ERRNO_RESERVED_LAST = _NM_ERRNO_RESERVED_LAST_PLUS_1 - 1,
|
|
Packit Service |
b23acc |
};
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* When we receive an errno from a system function, we can safely assume
|
|
Packit Service |
b23acc |
* that the error number is not negative. We rely on that, and possibly just
|
|
Packit Service |
b23acc |
* "return -errsv;" to signal an error. We also rely on that, because libc
|
|
Packit Service |
b23acc |
* is our trusted base: meaning, if it cannot even succeed at setting errno
|
|
Packit Service |
b23acc |
* according to specification, all bets are off.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* This macro returns the input argument, and asserts that the error variable
|
|
Packit Service |
b23acc |
* is positive.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* In a sense, the macro is related to nm_errno_native() function, but the difference
|
|
Packit Service |
b23acc |
* is that this macro asserts that @errsv is positive, while nm_errno_native() coerces
|
|
Packit Service |
b23acc |
* negative values to be non-negative. */
|
|
Packit Service |
b23acc |
#define NM_ERRNO_NATIVE(errsv) \
|
|
Packit Service |
b23acc |
({ \
|
|
Packit Service |
b23acc |
const int _errsv_x = (errsv); \
|
|
Packit Service |
b23acc |
\
|
|
Packit Service |
b23acc |
nm_assert (_errsv_x > 0); \
|
|
Packit Service |
b23acc |
_errsv_x; \
|
|
Packit Service |
b23acc |
})
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* Normalize native errno.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Our API may return native error codes (<errno.h>) as negative values. This function
|
|
Packit Service |
b23acc |
* takes such an errno, and normalizes it to their positive value.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* The special values G_MININT and zero are coerced to NME_ERRNO_OUT_OF_RANGE and NME_ERRNO_SUCCESS
|
|
Packit Service |
b23acc |
* respectively.
|
|
Packit Service |
b23acc |
* Other values are coerced to their inverse.
|
|
Packit Service |
b23acc |
* Other positive values are returned unchanged.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Basically, this normalizes errsv to be positive (taking care of two pathological cases).
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
static inline int
|
|
Packit Service |
b23acc |
nm_errno_native (int errsv)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
switch (errsv) {
|
|
Packit Service |
b23acc |
case 0: return NME_ERRNO_SUCCESS;
|
|
Packit Service |
b23acc |
case G_MININT: return NME_ERRNO_OUT_OF_RANGE;
|
|
Packit Service |
b23acc |
default:
|
|
Packit Service |
b23acc |
return errsv >= 0 ? errsv : -errsv;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* Normalizes an nm-error to be positive.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Various API returns negative error codes, and this function converts the negative
|
|
Packit Service |
b23acc |
* value to its positive.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Note that @nmerr is on the domain of NetworkManager specific error numbers,
|
|
Packit Service |
b23acc |
* which is not the same as the native error numbers (errsv from <errno.h>). But
|
|
Packit Service |
b23acc |
* as far as normalizing goes, nm_errno() does exactly the same remapping as
|
|
Packit Service |
b23acc |
* nm_errno_native(). */
|
|
Packit Service |
b23acc |
static inline int
|
|
Packit Service |
b23acc |
nm_errno (int nmerr)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return nm_errno_native (nmerr);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* this maps a native errno to a (always non-negative) nm-error number.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Note that nm-error numbers are embedded into the range of regular
|
|
Packit Service |
b23acc |
* errno. The only difference is, that nm-error numbers reserve a
|
|
Packit Service |
b23acc |
* range (_NM_ERRNO_RESERVED_FIRST, _NM_ERRNO_RESERVED_LAST) for their
|
|
Packit Service |
b23acc |
* own purpose.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* That means, converting an errno to nm-error number means in
|
|
Packit Service |
b23acc |
* most cases just returning itself.
|
|
Packit Service |
b23acc |
* Only pathological cases need special handling:
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* - 0 is mapped to NME_ERRNO_SUCCESS;
|
|
Packit Service |
b23acc |
* - G_MININT is mapped to NME_ERRNO_OUT_OF_RANGE;
|
|
Packit Service |
b23acc |
* - values in the range of (+/-) [_NM_ERRNO_RESERVED_FIRST, _NM_ERRNO_RESERVED_LAST]
|
|
Packit Service |
b23acc |
* are mapped to NME_NATIVE_ERRNO
|
|
Packit Service |
b23acc |
* - all other values are their (positive) absolute value.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
static inline int
|
|
Packit Service |
b23acc |
nm_errno_from_native (int errsv)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
switch (errsv) {
|
|
Packit Service |
b23acc |
case 0: return NME_ERRNO_SUCCESS;
|
|
Packit Service |
b23acc |
case G_MININT: return NME_ERRNO_OUT_OF_RANGE;
|
|
Packit Service |
b23acc |
default:
|
|
Packit Service |
b23acc |
if (errsv < 0)
|
|
Packit Service |
b23acc |
errsv = -errsv;
|
|
Packit Service |
b23acc |
return G_UNLIKELY ( errsv >= _NM_ERRNO_RESERVED_FIRST
|
|
Packit Service |
b23acc |
&& errsv <= _NM_ERRNO_RESERVED_LAST)
|
|
Packit Service |
b23acc |
? NME_NATIVE_ERRNO
|
|
Packit Service |
b23acc |
: errsv;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
const char *nm_strerror (int nmerr);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#define NM_STRERROR_BUFSIZE 1024
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
const char *nm_strerror_native_r (int errsv, char *buf, gsize buf_size);
|
|
Packit Service |
b23acc |
const char *nm_strerror_native (int errsv);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#endif /* __NM_ERRNO_H__ */
|