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 @@
#include "nm-shared-utils.h"
#include "nm-secret-utils.h"
#include "nm-errno.h"
/*****************************************************************************/
......@@ -36,11 +37,7 @@ _nm_printf (3, 4)
static int
_get_contents_error (GError **error, int errsv, const char *format, ...)
{
if (errsv < 0) {
errsv = errsv == G_MININT
? G_MAXINT
: -errsv;
}
nm_assert (NM_ERRNO_NATIVE (errsv));
if (error) {
char *msg;
......@@ -306,7 +303,7 @@ nm_utils_file_get_contents (int dirfd,
"Failed to open file \"%s\" with openat: %s",
filename,
g_strerror (errsv));
return -errsv;
return -NM_ERRNO_NATIVE (errsv);
}
} else {
fd = open (filename, O_RDONLY | O_CLOEXEC);
......@@ -319,7 +316,7 @@ nm_utils_file_get_contents (int dirfd,
"Failed to open file \"%s\": %s",
filename,
g_strerror (errsv));
return -errsv;
return -NM_ERRNO_NATIVE (errsv);
}
}
return nm_utils_fd_get_contents (fd,
......
......@@ -28,6 +28,8 @@
#include <fcntl.h>
#include <sys/syscall.h>
#include "nm-errno.h"
/*****************************************************************************/
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)
r = ppoll (&pollfd, 1, pts, NULL);
if (r < 0)
return -errno;
return -NM_ERRNO_NATIVE (errno);
if (r == 0)
return 0;
return pollfd.revents;
......@@ -1784,7 +1786,7 @@ nm_utils_fd_read_loop (int fd, void *buf, size_t nbytes, bool do_poll)
continue;
}
return n > 0 ? n : -errsv;
return n > 0 ? n : -NM_ERRNO_NATIVE (errsv);
}
if (k == 0)
......
......@@ -32,6 +32,7 @@
#include "NetworkManagerUtils.h"
#include "platform/nm-platform.h"
#include "platform/nmp-netns.h"
#include "nm-utils/nm-errno.h"
#define _NMLOG_PREFIX_NAME "ndisc-lndp"
......@@ -72,7 +73,6 @@ send_rs (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_new (&msg, NDP_MSG_RS);
if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"cannot create router solicitation");
return FALSE;
......@@ -82,7 +82,7 @@ send_rs (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_send (priv->ndp, msg);
ndp_msg_destroy (msg);
if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"%s (%d)",
g_strerror (errsv), errsv);
......@@ -360,7 +360,6 @@ send_ra (NMNDisc *ndisc, GError **error)
errsv = ndp_msg_new (&msg, NDP_MSG_RA);
if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"cannot create a router advertisement");
return FALSE;
......@@ -468,7 +467,7 @@ send_ra (NMNDisc *ndisc, GError **error)
ndp_msg_destroy (msg);
if (errsv) {
errsv = errsv > 0 ? errsv : -errsv;
errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"%s (%d)",
g_strerror (errsv), errsv);
......@@ -599,7 +598,7 @@ nm_lndp_ndisc_new (NMPlatform *platform,
errsv = ndp_open (&priv->ndp);
if (errsv != 0) {
errsv = errsv > 0 ? errsv : -errsv;
errsv = nm_errno_native (errsv);
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
"failure creating libndp socket: %s (%d)",
g_strerror (errsv), errsv);
......
......@@ -327,7 +327,7 @@ typedef enum {
typedef enum {
/* Negative values are errors from kernel. Add dummy member to
* 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_RESPONSE_OK,
......@@ -7825,7 +7825,7 @@ continue_reading:
err = -NME_NL_MSG_TRUNC;
abort_parsing = TRUE;
} 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)
&& hdr->nlmsg_len >= sizeof (*e) + e->msg.nlmsg_len) {
......@@ -7850,7 +7850,7 @@ continue_reading:
errsv,
NM_PRINT_FMT_QUOTED (extack_msg, " \"", extack_msg, "\"", ""),
nlmsg_hdr (msg)->nlmsg_seq);
seq_result = -errsv;
seq_result = -NM_ERRNO_NATIVE (errsv);
} else
seq_result = WAIT_FOR_NL_RESPONSE_RESULT_RESPONSE_OK;
} else
......
......@@ -84,7 +84,7 @@ socket_handle_init (SocketHandle *shandle, int ifindex)
shandle->fd = socket (PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (shandle->fd < 0) {
shandle->ifindex = 0;
return -errno;
return -NM_ERRNO_NATIVE (errno);
}
shandle->ifindex = ifindex;
......@@ -158,7 +158,7 @@ ethtool_call_handle (SocketHandle *shandle, gpointer edata)
_ethtool_data_to_string (edata, sbuf, sizeof (sbuf)),
shandle->ifname,
strerror (errsv));
return -errsv;
return -NM_ERRNO_NATIVE (errsv);
}
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