Commit f3eb13a8 authored by Jan Včelák's avatar Jan Včelák 🚀

Merge branch 'errno_fix' into 'master'

Fixes #359
parents 7afb8835 5a7c74c3
......@@ -111,7 +111,7 @@ int pid_write(const char* fn)
ret = KNOT_ERROR;
close(fd);
} else {
ret = knot_map_errno(errno);
ret = knot_map_errno();
}
return ret;
......
......@@ -549,7 +549,7 @@ int remote_bind(struct sockaddr_storage *addr)
if (listen(sock, TCP_BACKLOG_SIZE) != 0) {
log_error("remote control, failed to listen on '%s'", addr_str);
close(sock);
return knot_map_errno(EADDRINUSE);
return knot_map_errno();
}
return sock;
......
......@@ -805,7 +805,7 @@ int dt_unit_lock(dt_unit_t *unit)
/* Map errors. */
if (ret < 0) {
return knot_map_errno(EINVAL, EAGAIN);
return knot_map_errno();
}
return KNOT_EOK;
......@@ -822,7 +822,7 @@ int dt_unit_unlock(dt_unit_t *unit)
/* Map errors. */
if (ret < 0) {
return knot_map_errno(EINVAL, EAGAIN);
return knot_map_errno();
}
return KNOT_EOK;
......
......@@ -108,7 +108,7 @@ static int journal_create_file(const char *fn, uint16_t max_nodes)
int fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
if (fd < 0) {
dbg_journal("journal: failed to create file '%s'\n", fn);
return knot_map_errno(errno);
return knot_map_errno();
}
/* Lock. */
......@@ -202,7 +202,7 @@ static int journal_open_file(journal_t *j)
dbg_journal_verb("journal: open_file '%s'\n", j->path);
if (j->fd < 0) {
if (errno != ENOENT) {
return knot_map_errno(errno);
return knot_map_errno();
}
/* Create new journal file and open if not exists. */
......@@ -220,7 +220,7 @@ static int journal_open_file(journal_t *j)
dbg_journal_verb("journal: locking journal %s\n", j->path);
ret = fcntl(j->fd, F_SETLKW, &lock);
if (ret < 0) {
return knot_map_errno(errno);
return knot_map_errno();
}
/* Read magic bytes. */
......
......@@ -14,15 +14,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_LMDB
#include <lmdb.h>
#endif
#include "libknot/errcode.h"
#include "libknot/internal/errcode.h"
#include "libknot/internal/macros.h"
#include "dnssec/error.h"
......@@ -189,15 +186,3 @@ const char *knot_strerror(int code)
return fallback_message(code);
}
_public_
int knot_map_errno(int arg0, ...)
{
/* Iterate all variable-length arguments. */
va_list ap;
va_start(ap, arg0);
int ret = knot_map_errno_internal(KNOT_ERROR, arg0, ap);
va_end(ap);
return ret;
}
......@@ -27,7 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <errno.h>
#include "libknot/internal/errcode.h"
/*!
......@@ -39,11 +38,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const char *knot_strerror(int code);
/*!
* \brief Map POSIX errno to Knot error code.
*
* KNOT_ERROR is used as a fallback error, the list is terminated implicitly.
*/
int knot_map_errno(int arg0, ...);
/*! @} */
......@@ -14,35 +14,46 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_LMDB
#include <lmdb.h>
#endif
#include "libknot/internal/errcode.h"
#include "libknot/internal/macros.h"
#define ERR_ITEM(name) { name, KNOT_##name }
typedef struct {
int errno_code;
int libknot_code;
} err_table_t;
/*!
* \brief Errno to libknot error mapping table.
*/
static const err_table_t errno_to_errcode[] = {
ERR_ITEM(ENOMEM),
ERR_ITEM(EINVAL),
ERR_ITEM(ENOTSUP),
ERR_ITEM(EBUSY),
ERR_ITEM(EAGAIN),
ERR_ITEM(EACCES),
ERR_ITEM(ECONNREFUSED),
ERR_ITEM(EISCONN),
ERR_ITEM(EADDRINUSE),
ERR_ITEM(ENOENT),
ERR_ITEM(EEXIST),
ERR_ITEM(ERANGE),
ERR_ITEM(EADDRNOTAVAIL),
/* Terminator - default value. */
{ 0, KNOT_ERROR }
};
_public_
int knot_map_errno_internal(int fallback, int arg0, ...)
int knot_map_errno(void)
{
/* Iterate all variable-length arguments. */
va_list ap;
va_start(ap, arg0);
/* KNOT_ERROR serves as a sentinel. */
for (int c = arg0; c != 0; c = va_arg(ap, int)) {
/* Error code matches with mapped. */
if (c == errno) {
/* Return negative value of the code. */
va_end(ap);
return -abs(c);
}
const err_table_t *err = errno_to_errcode;
while (err->errno_code != 0 && err->errno_code != errno) {
err++;
}
va_end(ap);
/* Fallback error code. */
return KNOT_ERROR;
return err->libknot_code;
}
......@@ -145,13 +145,8 @@ enum knot_error {
/*!
* \brief Get a POSIX errno mapped to Knot error code.
*
* \internal
*
* \param fallback Falback error code.
* \param arg0... Error codes allowed for lookup, list must be terminated by 0.
*
* \return Mapped errno or fallback error code.
* \return Mapped errno or KNOT_ERROR if unknown.
*/
int knot_map_errno_internal(int fallback, int arg0, ...);
int knot_map_errno(void);
/*! @} */
......@@ -41,7 +41,7 @@ static int socket_create(int family, int type, int proto)
/* Create socket. */
int ret = socket(family, type, proto);
if (ret < 0) {
return knot_map_errno_internal(KNOT_ERROR, EACCES, EINVAL, ENOMEM);
return knot_map_errno();
}
return ret;
......@@ -135,7 +135,7 @@ int net_bound_socket(int type, const struct sockaddr_storage *ss,
const struct sockaddr *sa = (const struct sockaddr *)ss;
int ret = bind(socket, sa, sockaddr_len(sa));
if (ret < 0) {
ret = knot_map_errno_internal(KNOT_ERROR, EADDRINUSE, EADDRNOTAVAIL, EINVAL, EACCES, ENOMEM);
ret = knot_map_errno();
close(socket);
return ret;
}
......@@ -176,8 +176,7 @@ int net_connected_socket(int type, const struct sockaddr_storage *dst_addr,
int ret = connect(socket, sa, sockaddr_len(sa));
if (ret != 0 && errno != EINPROGRESS) {
close(socket);
return knot_map_errno_internal(KNOT_ERROR, EACCES, EADDRINUSE, EAGAIN,
ECONNREFUSED, EISCONN);
return knot_map_errno();
}
return socket;
......
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