platform: pass string buffer to nm_platform_error_to_string() and print numeric errno

Change the output of nm_platform_error_to_string() to print the numeric value.
Also, accept a string buffer instead of using an alloca() allocated buffer.

There is still a macro to provide the previous functionality, but it
was ill-suited to call from inside a loop.
This commit is contained in:
Thomas Haller 2017-08-21 19:18:45 +02:00
parent 8746dddda2
commit b524d879f0
14 changed files with 62 additions and 31 deletions

View file

@ -483,7 +483,7 @@ create_and_realize (NMDevice *device,
"Failed to create bond interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
return TRUE;

View file

@ -473,7 +473,7 @@ create_and_realize (NMDevice *device,
"Failed to create bridge interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -112,7 +112,7 @@ create_and_realize (NMDevice *device,
"Failed to create dummy interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -295,7 +295,7 @@ create_and_realize (NMDevice *device,
"Failed to create InfiniBand P_Key interface '%s' for '%s': %s",
nm_device_get_iface (device),
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
@ -324,7 +324,7 @@ unrealize (NMDevice *device, GError **error)
g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
"Failed to remove InfiniBand P_Key interface '%s': %s",
nm_device_get_iface (device),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -647,7 +647,7 @@ create_and_realize (NMDevice *device,
"Failed to create GRE interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
break;
@ -673,7 +673,7 @@ create_and_realize (NMDevice *device,
"Failed to create SIT interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
break;
@ -699,7 +699,7 @@ create_and_realize (NMDevice *device,
"Failed to create IPIP interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
break;
@ -728,7 +728,7 @@ create_and_realize (NMDevice *device,
"Failed to create IPIP interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}
break;

View file

@ -720,7 +720,7 @@ create_and_realize (NMDevice *device,
"Failed to create macsec interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -258,7 +258,7 @@ create_and_realize (NMDevice *device,
lnk.tap ? "macvtap" : "macvlan",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -244,7 +244,7 @@ create_and_realize (NMDevice *device,
"Failed to create TUN/TAP interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -266,7 +266,7 @@ create_and_realize (NMDevice *device,
"Failed to create VLAN interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -223,7 +223,7 @@ create_and_realize (NMDevice *device,
"Failed to create VXLAN interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -7729,7 +7729,7 @@ set_nm_ipv6ll (NMDevice *self, gboolean enable)
LOGD_IP6,
"failed to %s userspace IPv6LL address handling (%s)",
detail,
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
}
if (enable) {
@ -13397,7 +13397,7 @@ handle_fail:
_NMLOG (plerr == NM_PLATFORM_ERROR_NOT_FOUND ? LOGL_DEBUG : LOGL_WARN,
LOGD_DEVICE, "set-hw-addr: failed to %s MAC address to %s (%s) (%s)",
operation, addr, detail,
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
}
if (was_up) {

View file

@ -774,7 +774,7 @@ create_and_realize (NMDevice *device,
"Failed to create team master interface '%s' for '%s': %s",
iface,
nm_connection_get_id (connection),
nm_platform_error_to_string (plerr));
nm_platform_error_to_string_a (plerr));
return FALSE;
}

View file

@ -228,17 +228,8 @@ nm_platform_get_multi_idx (NMPlatform *self)
/*****************************************************************************/
/**
* _nm_platform_error_to_string:
* @error_code: the error code to stringify.
*
* Returns: A string representation of the error.
* For negative numbers, this function interprets
* the code as -errno.
* For invalid (positive) numbers it returns NULL.
*/
NM_UTILS_LOOKUP_STR_DEFINE (_nm_platform_error_to_string, NMPlatformError,
NM_UTILS_LOOKUP_DEFAULT ( val < 0 ? g_strerror (- ((int) val)) : NULL ),
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_nm_platform_error_to_string, NMPlatformError,
NM_UTILS_LOOKUP_DEFAULT (NULL),
NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_SUCCESS, "success"),
NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_BUG, "bug"),
NM_UTILS_LOOKUP_STR_ITEM (NM_PLATFORM_ERROR_UNSPECIFIED, "unspecified"),
@ -252,6 +243,42 @@ NM_UTILS_LOOKUP_STR_DEFINE (_nm_platform_error_to_string, NMPlatformError,
NM_UTILS_LOOKUP_ITEM_IGNORE (_NM_PLATFORM_ERROR_MININT),
);
/**
* nm_platform_error_to_string:
* @error_code: the error code to stringify.
* @buf: (allow-none): buffer
* @buf_len: size of buffer
*
* Returns: A string representation of the error.
* For negative numbers, this function interprets
* the code as -errno.
* For invalid (positive) numbers it returns NULL.
*/
const char *
nm_platform_error_to_string (NMPlatformError error_code, char *buf, gsize buf_len)
{
const char *s;
if (error_code < 0) {
int errsv = -((int) error_code);
nm_utils_to_string_buffer_init (&buf, &buf_len);
g_snprintf (buf, buf_len, "%s (%d)", g_strerror (errsv), errsv);
} else {
s = _nm_platform_error_to_string (error_code);
if (s) {
if (!buf)
return s;
g_strlcpy (buf, s, buf_len);
} else {
nm_utils_to_string_buffer_init (&buf, &buf_len);
g_snprintf (buf, buf_len, "(%d)", (int) error_code);
}
}
return buf;
}
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (_nmp_nlm_flag_to_string_lookup, NMPNlmFlags,
NM_UTILS_LOOKUP_DEFAULT (NULL),
NM_UTILS_LOOKUP_ITEM (NMP_NLM_FLAG_ADD, "add"),
@ -3801,6 +3828,7 @@ nm_platform_ip_route_get (NMPlatform *self,
nm_auto_nmpobj NMPObject *route = NULL;
NMPlatformError result;
char buf[NM_UTILS_INET_ADDRSTRLEN];
char buf_err[200];
_CHECK_SELF (self, klass, FALSE);
@ -3826,7 +3854,7 @@ nm_platform_ip_route_get (NMPlatform *self,
_LOGW ("route: get IPv%c route for: %s failed with %s",
addr_family == AF_INET ? '4' : '6',
inet_ntop (addr_family, address, buf, sizeof (buf)),
nm_platform_error_to_string (result));
nm_platform_error_to_string (result, buf_err, sizeof (buf_err)));
} else {
nm_assert (NM_IN_SET (NMP_OBJECT_GET_TYPE (route), NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE));
nm_assert (!NMP_OBJECT_IS_STACKINIT (route));

View file

@ -868,8 +868,11 @@ gboolean nm_platform_netns_push (NMPlatform *platform, NMPNetns **netns);
const char *nm_link_type_to_string (NMLinkType link_type);
const char *_nm_platform_error_to_string (NMPlatformError error);
#define nm_platform_error_to_string(error) NM_UTILS_LOOKUP_STR (_nm_platform_error_to_string, error)
const char *nm_platform_error_to_string (NMPlatformError error,
char *buf,
gsize buf_len);
#define nm_platform_error_to_string_a(error) \
(nm_platform_error_to_string ((error), g_alloca (30), 30))
#define NMP_SYSCTL_PATHID_ABSOLUTE(path) \
((const char *) NULL), -1, (path)