From 4186ddb58bb0b1de30e6e4f7433718d74ca7af4c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 10 Sep 2018 14:54:13 +0200 Subject: [PATCH] shared: add nm_errno() and nm_utils_error_set_errno() helper --- shared/nm-utils/nm-shared-utils.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index b3099738f..5125bd3d8 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -480,6 +480,19 @@ _nm_g_slice_free_fcn_define (16) /*****************************************************************************/ +static inline int +nm_errno (int errsv) +{ + /* 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 + * distinction, then check for G_MININT before. */ + return errsv >= 0 + ? errsv + : ((errsv == G_MININT) ? G_MAXINT : -errsv); +} + /** * NMUtilsError: * @NM_UTILS_ERROR_UNKNOWN: unknown or unclassified error @@ -542,6 +555,14 @@ nm_utils_error_set_literal (GError **error, int error_code, const char *literal) #define nm_utils_error_set(error, error_code, ...) \ g_set_error ((error), NM_UTILS_ERROR, error_code, __VA_ARGS__) +#define nm_utils_error_set_errno(error, errsv, fmt, ...) \ + g_set_error ((error), \ + NM_UTILS_ERROR, \ + NM_UTILS_ERROR_UNKNOWN, \ + fmt, \ + ##__VA_ARGS__, \ + g_strerror (nm_errno (errsv))) + /*****************************************************************************/ gboolean nm_g_object_set_property (GObject *object,