Commit 4d9918aa authored by Thomas Haller's avatar Thomas Haller

all: assert that native errno numbers are positive

Use the NM_ERRNO_NATIVE() macro that asserts that these errno numbers are
indeed positive. Using the macro also serves as a documentation of what
the meaning of these numbers is.

That is often not obvious, whether we have an nm_errno(), an nm_errno_native()
(from <errno.h>), or another error number (e.g. WaitForNlResponseResult). This
situation already improved by merging netlink error codes (nle),
NMPlatformError enum and <errno.h> as nm_errno(). But we still must
always be careful about not to mix error codes from different
domains or transform them appropriately (like nm_errno_from_native()).
parent 67130e67
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "nm-shared-utils.h" #include "nm-shared-utils.h"
#include "nm-secret-utils.h" #include "nm-secret-utils.h"
#include "nm-errno.h"
/*****************************************************************************/ /*****************************************************************************/
...@@ -36,11 +37,7 @@ _nm_printf (3, 4) ...@@ -36,11 +37,7 @@ _nm_printf (3, 4)
static int static int
_get_contents_error (GError **error, int errsv, const char *format, ...) _get_contents_error (GError **error, int errsv, const char *format, ...)
{ {
if (errsv < 0) { nm_assert (NM_ERRNO_NATIVE (errsv));
errsv = errsv == G_MININT
? G_MAXINT
: -errsv;
}
if (error) { if (error) {
char *msg; char *msg;
...@@ -306,7 +303,7 @@ nm_utils_file_get_contents (int dirfd, ...@@ -306,7 +303,7 @@ nm_utils_file_get_contents (int dirfd,
"Failed to open file \"%s\" with openat: %s", "Failed to open file \"%s\" with openat: %s",
filename, filename,
g_strerror (errsv)); g_strerror (errsv));
return -errsv; return -NM_ERRNO_NATIVE (errsv);
} }
} else { } else {
fd = open (filename, O_RDONLY | O_CLOEXEC); fd = open (filename, O_RDONLY | O_CLOEXEC);
...@@ -319,7 +316,7 @@ nm_utils_file_get_contents (int dirfd, ...@@ -319,7 +316,7 @@ nm_utils_file_get_contents (int dirfd,
"Failed to open file \"%s\": %s", "Failed to open file \"%s\": %s",
filename, filename,
g_strerror (errsv)); g_strerror (errsv));
return -errsv; return -NM_ERRNO_NATIVE (errsv);
} }
} }
return nm_utils_fd_get_contents (fd, return nm_utils_fd_get_contents (fd,
......
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include "nm-errno.h"
/*****************************************************************************/ /*****************************************************************************/
const void *const _NM_PTRARRAY_EMPTY[1] = { NULL }; const void *const _NM_PTRARRAY_EMPTY[1] = { NULL };
...@@ -1742,7 +1744,7 @@ nm_utils_fd_wait_for_event (int fd, int event, gint64 timeout_ns) ...@@ -1742,7 +1744,7 @@ nm_utils_fd_wait_for_event (int fd, int event, gint64 timeout_ns)
r = ppoll (&pollfd, 1, pts, NULL); r = ppoll (&pollfd, 1, pts, NULL);
if (r < 0) if (r < 0)
return -errno; return -NM_ERRNO_NATIVE (errno);
if (r == 0) if (r == 0)
return 0; return 0;
return pollfd.revents; return pollfd.revents;
...@@ -1784,7 +1786,7 @@ nm_utils_fd_read_loop (int fd, void *buf, size_t nbytes, bool do_poll) ...@@ -1784,7 +1786,7 @@ nm_utils_fd_read_loop (int fd, void *buf, size_t nbytes, bool do_poll)
continue; continue;
} }
return n > 0 ? n : -errsv; return n > 0 ? n : -NM_ERRNO_NATIVE (errsv);
} }
if (k == 0) if (k == 0)
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "NetworkManagerUtils.h" #include "NetworkManagerUtils.h"
#include "platform/nm-platform.h" #include "platform/nm-platform.h"
#include "platform/nmp-netns.h" #include "platform/nmp-netns.h"
#include "nm-utils/nm-errno.h"
#define _NMLOG_PREFIX_NAME "ndisc-lndp" #define _NMLOG_PREFIX_NAME "ndisc-lndp"
...@@ -72,7 +73,6 @@ send_rs (NMNDisc *ndisc, GError **error) ...@@ -72,7 +73,6 @@ send_rs (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_new (&msg, NDP_MSG_RS); errsv = ndp_msg_new (&msg, NDP_MSG_RS);
if (errsv) { if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"cannot create router solicitation"); "cannot create router solicitation");
return FALSE; return FALSE;
...@@ -82,7 +82,7 @@ send_rs (NMNDisc *ndisc, GError **error) ...@@ -82,7 +82,7 @@ send_rs (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_send (priv->ndp, msg); errsv = ndp_msg_send (priv->ndp, msg);
ndp_msg_destroy (msg); ndp_msg_destroy (msg);
if (errsv) { if (errsv) {
errsv = errsv > 0 ? errsv : -errsv; errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"%s (%d)", "%s (%d)",
g_strerror (errsv), errsv); g_strerror (errsv), errsv);
...@@ -360,7 +360,6 @@ send_ra (NMNDisc *ndisc, GError **error) ...@@ -360,7 +360,6 @@ send_ra (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_new (&msg, NDP_MSG_RA); errsv = ndp_msg_new (&msg, NDP_MSG_RA);
if (errsv) { if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"cannot create a router advertisement"); "cannot create a router advertisement");
return FALSE; return FALSE;
...@@ -468,7 +467,7 @@ send_ra (NMNDisc *ndisc, GError **error) ...@@ -468,7 +467,7 @@ send_ra (NMNDisc *ndisc, GError **error)
ndp_msg_destroy (msg); ndp_msg_destroy (msg);
if (errsv) { if (errsv) {
errsv = errsv > 0 ? errsv : -errsv; errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"%s (%d)", "%s (%d)",
g_strerror (errsv), errsv); g_strerror (errsv), errsv);
...@@ -599,7 +598,7 @@ nm_lndp_ndisc_new (NMPlatform *platform, ...@@ -599,7 +598,7 @@ nm_lndp_ndisc_new (NMPlatform *platform,
errsv = ndp_open (&priv->ndp); errsv = ndp_open (&priv->ndp);
if (errsv != 0) { if (errsv != 0) {
errsv = errsv > 0 ? errsv : -errsv; errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"failure creating libndp socket: %s (%d)", "failure creating libndp socket: %s (%d)",
g_strerror (errsv), errsv); g_strerror (errsv), errsv);
......
...@@ -327,7 +327,7 @@ typedef enum { ...@@ -327,7 +327,7 @@ typedef enum {
typedef enum { typedef enum {
/* Negative values are errors from kernel. Add dummy member to /* Negative values are errors from kernel. Add dummy member to
* make enum signed. */ * make enum signed. */
_WAIT_FOR_NL_RESPONSE_RESULT_SYSTEM_ERROR = -1, _WAIT_FOR_NL_RESPONSE_RESULT_SYSTEM_ERROR = G_MININT,
WAIT_FOR_NL_RESPONSE_RESULT_UNKNOWN = 0, WAIT_FOR_NL_RESPONSE_RESULT_UNKNOWN = 0,
WAIT_FOR_NL_RESPONSE_RESULT_RESPONSE_OK, WAIT_FOR_NL_RESPONSE_RESULT_RESPONSE_OK,
...@@ -7825,7 +7825,7 @@ continue_reading: ...@@ -7825,7 +7825,7 @@ continue_reading:
err = -NME_NL_MSG_TRUNC; err = -NME_NL_MSG_TRUNC;
abort_parsing = TRUE; abort_parsing = TRUE;
} else if (e->error) { } else if (e->error) {
int errsv = e->error > 0 ? e->error : -e->error; int errsv = nm_errno_native (e->error);
if ( NM_FLAGS_HAS (hdr->nlmsg_flags, NLM_F_ACK_TLVS) if ( NM_FLAGS_HAS (hdr->nlmsg_flags, NLM_F_ACK_TLVS)
&& hdr->nlmsg_len >= sizeof (*e) + e->msg.nlmsg_len) { && hdr->nlmsg_len >= sizeof (*e) + e->msg.nlmsg_len) {
...@@ -7850,7 +7850,7 @@ continue_reading: ...@@ -7850,7 +7850,7 @@ continue_reading:
errsv, errsv,
NM_PRINT_FMT_QUOTED (extack_msg, " \"", extack_msg, "\"", ""), NM_PRINT_FMT_QUOTED (extack_msg, " \"", extack_msg, "\"", ""),
nlmsg_hdr (msg)->nlmsg_seq); nlmsg_hdr (msg)->nlmsg_seq);
seq_result = -errsv; seq_result = -NM_ERRNO_NATIVE (errsv);
} else } else
seq_result = WAIT_FOR_NL_RESPONSE_RESULT_RESPONSE_OK; seq_result = WAIT_FOR_NL_RESPONSE_RESULT_RESPONSE_OK;
} else } else
......
...@@ -84,7 +84,7 @@ socket_handle_init (SocketHandle *shandle, int ifindex) ...@@ -84,7 +84,7 @@ socket_handle_init (SocketHandle *shandle, int ifindex)
shandle->fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); shandle->fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (shandle->fd < 0) { if (shandle->fd < 0) {
shandle->ifindex = 0; shandle->ifindex = 0;
return -errno; return -NM_ERRNO_NATIVE (errno);
} }
shandle->ifindex = ifindex; shandle->ifindex = ifindex;
...@@ -158,7 +158,7 @@ ethtool_call_handle (SocketHandle *shandle, gpointer edata) ...@@ -158,7 +158,7 @@ ethtool_call_handle (SocketHandle *shandle, gpointer edata)
_ethtool_data_to_string (edata, sbuf, sizeof (sbuf)), _ethtool_data_to_string (edata, sbuf, sizeof (sbuf)),
shandle->ifname, shandle->ifname,
strerror (errsv)); strerror (errsv));
return -errsv; return -NM_ERRNO_NATIVE (errsv);
} }
nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s, %s: success", nm_log_trace (LOGD_PLATFORM, "ethtool[%d]: %s, %s: success",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment