Skip to content

Commit

Permalink
Replace nonportable 'strerror_r' with 'strerror_l', which is Musl-com…
Browse files Browse the repository at this point in the history
…patible.

Addresses GitHub CESNET/ipfixcol2 issue CESNET#34

Signed-off-by: Jake Staehle <[email protected]>
  • Loading branch information
staehle committed Jun 15, 2020
1 parent c789c06 commit 7342013
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 15 deletions.
52 changes: 46 additions & 6 deletions src/plugins/output/json/src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <unistd.h>
#include <climits>
#include <zlib.h>
#include <locale.h>

/**
* \brief Class constructor
Expand Down Expand Up @@ -342,8 +343,21 @@ File::dir_create(ipx_ctx_t *ctx, const std::string &path)
continue;
default:
// Other errors
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_ERROR(ctx, "(File output) Failed to create a directory %s (%s).",
aux_str.c_str(), err_str);
return 1;
Expand All @@ -357,8 +371,21 @@ File::dir_create(ipx_ctx_t *ctx, const std::string &path)

if (mkdir(aux_str.c_str(), mask) != 0) {
// Failed to create directory
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_ERROR(ctx, "(File output) Failed to create a directory %s (%s).",
aux_str.c_str(), err_str);
return 1;
Expand Down Expand Up @@ -419,8 +446,21 @@ File::file_create(ipx_ctx_t *ctx, const std::string &tmplt, const std::string &p
}
if (!file) {
// Failed to create a flow file
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_ERROR(ctx, "Failed to create a flow file '%s' (%s).", file_name.c_str(), err_str);
return NULL;
}
Expand Down
19 changes: 16 additions & 3 deletions src/plugins/output/json/src/Sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,21 @@ Sender::send(const char *str, size_t len)
}

// Connection failed
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_INFO(_ctx, "(Send output) Destination '%s:%" PRIu16 "' disconnected: %s",
params.addr.c_str(), params.port, err_str);
return SEND_FAILED;
Expand All @@ -257,4 +270,4 @@ Sender::send(const char *str, size_t len)
std::string tmp(ptr, todo);
msg_rest.assign(tmp);
return SEND_WOULDBLOCK;
}
}
52 changes: 46 additions & 6 deletions src/plugins/output/json/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <locale.h>

/** How many pending connections queue will hold */
#define BACKLOG (10)
Expand Down Expand Up @@ -201,8 +202,21 @@ Server::thread_accept(void *context)
continue;
}

char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_ERROR(acc->ctx, "(Server output) select() - failed (%s)", err_str);
break;
}
Expand All @@ -214,8 +228,21 @@ Server::thread_accept(void *context)

new_fd = accept(acc->socket_fd, (struct sockaddr *) &client_addr, &sin_size);
if (new_fd == -1) {
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_ERROR(acc->ctx, "(Server output) accept() - failed (%s)", err_str);
continue;
}
Expand Down Expand Up @@ -270,8 +297,21 @@ Server::msg_send(const char *data, ssize_t len, client_t &client)
}

// Connection failed
char buffer[128];
const char *err_str = strerror_r(errno, buffer, 128);
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *err_str;
if (loc == (locale_t)0) {
if (errno == ENOENT) {
loc = newlocale(LC_MESSAGES_MASK, "POSIX", (locale_t)0);
}
}
if (loc != (locale_t)0) {
err_str = strerror_l(errno_save, loc);
freelocale(loc);
} else {
err_str = "newlocale() failed";
}
errno = errno_save;
IPX_CTX_INFO(_ctx, "(Server output) Client disconnected: %s (%s)",
get_client_desc(client.info).c_str(), err_str);
return SEND_FAILED;
Expand Down

0 comments on commit 7342013

Please sign in to comment.