Skip to content

Commit

Permalink
all: merge branch 'strerror_l'
Browse files Browse the repository at this point in the history
http://lists.infradead.org/pipermail/libnl/2016-August/002192.html

Signed-off-by: Thomas Haller <thaller@redhat.com>
  • Loading branch information
thom311 committed Aug 25, 2016
2 parents b3dfa79 + 6c2d111 commit e1ca773
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 51 deletions.
1 change: 1 addition & 0 deletions include/Makefile.am
Expand Up @@ -166,6 +166,7 @@ noinst_HEADERS = \
netlink-private/socket.h \
netlink-private/tc.h \
netlink-private/types.h \
netlink-private/utils.h \
netlink-private/cache-api.h \
netlink-private/object-api.h \
netlink-private/route/link/api.h \
Expand Down
17 changes: 17 additions & 0 deletions include/netlink-private/utils.h
@@ -0,0 +1,17 @@
/*
* netlink-private/utils.h Local Utility Functions
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
*/

#ifndef NETLINK_UTILS_PRIV_H_
#define NETLINK_UTILS_PRIV_H_

extern const char * nl_strerror_l(int err);

#endif
5 changes: 2 additions & 3 deletions lib/cache_mngr.c
Expand Up @@ -33,6 +33,7 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
Expand Down Expand Up @@ -392,10 +393,8 @@ int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
ret = poll(&fds, 1, timeout);
NL_DBG(3, "Cache manager %p, poll() returned %d\n", mngr, ret);
if (ret < 0) {
char buf[64];

NL_DBG(4, "nl_cache_mngr_poll(%p): poll() failed with %d (%s)\n",
mngr, errno, strerror_r(errno, buf, sizeof(buf)));
mngr, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/fib_lookup/lookup.c
Expand Up @@ -17,6 +17,7 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/utils.h>
Expand Down Expand Up @@ -133,7 +134,7 @@ static void result_dump_line(struct nl_object *obj, struct nl_dump_params *p)
nl_rtntype2str(res->fr_type, buf, sizeof(buf)));
nl_dump(p, "scope %s error %s (%d)\n",
rtnl_scope2str(res->fr_scope, buf, sizeof(buf)),
strerror_r(-res->fr_error, buf, sizeof(buf)), res->fr_error);
nl_strerror_l(-res->fr_error), res->fr_error);
}

static void result_dump_details(struct nl_object *obj, struct nl_dump_params *p)
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers.c
Expand Up @@ -26,6 +26,7 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/msg.h>
Expand Down Expand Up @@ -79,10 +80,9 @@ static int nl_error_handler_verbose(struct sockaddr_nl *who,
struct nlmsgerr *e, void *arg)
{
FILE *ofd = arg ? arg : stderr;
char buf[256];

fprintf(ofd, "-- Error received: %s\n-- Original message: ",
strerror_r(-e->error, buf, sizeof(buf)));
nl_strerror_l(-e->error));
print_header_content(ofd, &e->msg);
fprintf(ofd, "\n");

Expand Down
4 changes: 2 additions & 2 deletions lib/msg.c
Expand Up @@ -27,6 +27,7 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/cache.h>
Expand Down Expand Up @@ -913,11 +914,10 @@ static void dump_error_msg(struct nl_msg *msg, FILE *ofd)
fprintf(ofd, " [ERRORMSG] %zu octets\n", sizeof(*err));

if (nlmsg_len(hdr) >= sizeof(*err)) {
char buf[256];
struct nl_msg *errmsg;

fprintf(ofd, " .error = %d \"%s\"\n", err->error,
strerror_r(-err->error, buf, sizeof(buf)));
nl_strerror_l(-err->error));
fprintf(ofd, " [ORIGINAL MESSAGE] %zu octets\n", sizeof(*hdr));

errmsg = nlmsg_inherit(&err->msg);
Expand Down
26 changes: 9 additions & 17 deletions lib/nl.c
Expand Up @@ -27,6 +27,7 @@

#include <netlink-private/netlink.h>
#include <netlink-private/socket.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/handlers.h>
Expand Down Expand Up @@ -105,7 +106,6 @@ int nl_connect(struct nl_sock *sk, int protocol)
int errsv;
socklen_t addrlen;
struct sockaddr_nl local = { 0 };
char buf[64];
int try_bind = 1;

#ifdef SOCK_CLOEXEC
Expand All @@ -119,7 +119,7 @@ int nl_connect(struct nl_sock *sk, int protocol)
if (sk->s_fd < 0) {
errsv = errno;
NL_DBG(4, "nl_connect(%p): socket() failed with %d (%s)\n", sk, errsv,
strerror_r(errsv, buf, sizeof(buf)));
nl_strerror_l(errsv));
err = -nl_syserr2nlerr(errsv);
goto errout;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ int nl_connect(struct nl_sock *sk, int protocol)
_nl_socket_used_ports_set(used_ports, port);
} else {
NL_DBG(4, "nl_connect(%p): bind() for port %u failed with %d (%s)\n",
sk, (unsigned) port, errsv, strerror_r(errsv, buf, sizeof(buf)));
sk, (unsigned) port, errsv, nl_strerror_l(errsv));
_nl_socket_used_ports_release_all(used_ports);
err = -nl_syserr2nlerr(errsv);
goto errout;
Expand All @@ -172,7 +172,7 @@ int nl_connect(struct nl_sock *sk, int protocol)
if (err != 0) {
errsv = errno;
NL_DBG(4, "nl_connect(%p): bind() failed with %d (%s)\n",
sk, errsv, strerror_r(errsv, buf, sizeof(buf)));
sk, errsv, nl_strerror_l(errsv));
err = -nl_syserr2nlerr(errsv);
goto errout;
}
Expand All @@ -183,7 +183,7 @@ int nl_connect(struct nl_sock *sk, int protocol)
&addrlen);
if (err < 0) {
NL_DBG(4, "nl_connect(%p): getsockname() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
err = -nl_syserr2nlerr(errno);
goto errout;
}
Expand Down Expand Up @@ -280,10 +280,8 @@ int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
&sk->s_peer, sizeof(sk->s_peer));
if (ret < 0) {
char errbuf[64];

NL_DBG(4, "nl_sendto(%p): sendto() failed with %d (%s)\n",
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -343,10 +341,8 @@ int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)

ret = sendmsg(sk->s_fd, hdr, 0);
if (ret < 0) {
char errbuf[64];

NL_DBG(4, "nl_sendmsg(%p): sendmsg() failed with %d (%s)\n",
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -706,15 +702,13 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
goto abort;
}
if (n < 0) {
char errbuf[64];

if (errno == EINTR) {
NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
goto retry;
}

NL_DBG(4, "nl_sendmsg(%p): nl_recv() failed with %d (%s)\n",
sk, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
sk, errno, nl_strerror_l(errno));
retval = -nl_syserr2nlerr(errno);
goto abort;
}
Expand Down Expand Up @@ -980,10 +974,8 @@ static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
goto out;
}
} else if (e->error) {
char buf[64];

NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n",
sk, -e->error, strerror_r(-e->error, buf, sizeof(buf)));
sk, -e->error, nl_strerror_l(-e->error));

/* Error message reported back from kernel. */
if (cb->cb_err) {
Expand Down
3 changes: 2 additions & 1 deletion lib/route/route_obj.c
Expand Up @@ -31,6 +31,7 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
Expand Down Expand Up @@ -259,7 +260,7 @@ static void route_dump_details(struct nl_object *a, struct nl_dump_params *p)
if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
nl_dump_line(p, " cacheinfo error %d (%s)\n",
r->rt_cacheinfo.rtci_error,
strerror_r(-r->rt_cacheinfo.rtci_error, buf, sizeof(buf)));
nl_strerror_l(-r->rt_cacheinfo.rtci_error));
}

if (r->ce_mask & ROUTE_ATTR_METRICS) {
Expand Down
33 changes: 11 additions & 22 deletions lib/socket.c
Expand Up @@ -33,6 +33,7 @@

#include <netlink-private/netlink.h>
#include <netlink-private/socket.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/handlers.h>
Expand Down Expand Up @@ -449,11 +450,9 @@ int nl_socket_add_memberships(struct nl_sock *sk, int group, ...)
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
&group, sizeof(group));
if (err < 0) {
char buf[64];

va_end(ap);
NL_DBG(4, "nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -501,11 +500,9 @@ int nl_socket_drop_memberships(struct nl_sock *sk, int group, ...)
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
&group, sizeof(group));
if (err < 0) {
char buf[64];

va_end(ap);
NL_DBG(4, "nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -619,7 +616,6 @@ int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
{
int err = 0;
socklen_t addrlen;
char buf[64];
struct sockaddr_nl local = { 0 };
int so_type = -1, so_protocol = -1;

Expand All @@ -633,7 +629,7 @@ int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
&addrlen);
if (err < 0) {
NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
sk, fd, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}
if (addrlen != sizeof(local))
Expand All @@ -648,7 +644,7 @@ int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
if (err < 0) {
NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
sk, fd, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}
if (addrlen != sizeof(so_type))
Expand All @@ -666,7 +662,7 @@ int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
if (errno == ENOPROTOOPT)
goto no_so_protocol;
NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
sk, fd, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}
if (addrlen != sizeof(so_protocol))
Expand Down Expand Up @@ -709,10 +705,8 @@ int nl_socket_set_nonblocking(const struct nl_sock *sk)
return -NLE_BAD_SOCK;

if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
char buf[64];

NL_DBG(4, "nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -813,7 +807,6 @@ int nl_socket_modify_err_cb(struct nl_sock *sk, enum nl_cb_kind kind,
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
{
int err;
char buf[64];

if (rxbuf <= 0)
rxbuf = 32768;
Expand All @@ -828,15 +821,15 @@ int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
&txbuf, sizeof(txbuf));
if (err < 0) {
NL_DBG(4, "nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
&rxbuf, sizeof(rxbuf));
if (err < 0) {
NL_DBG(4, "nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -890,10 +883,8 @@ int nl_socket_set_passcred(struct nl_sock *sk, int state)
err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
&state, sizeof(state));
if (err < 0) {
char buf[64];

NL_DBG(4, "nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down Expand Up @@ -922,10 +913,8 @@ int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
&state, sizeof(state));
if (err < 0) {
char buf[64];

NL_DBG(4, "nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
sk, errno, strerror_r(errno, buf, sizeof(buf)));
sk, errno, nl_strerror_l(errno));
return -nl_syserr2nlerr(errno);
}

Expand Down
24 changes: 24 additions & 0 deletions lib/utils.c
Expand Up @@ -25,10 +25,12 @@
*/

#include <netlink-private/netlink.h>
#include <netlink-private/utils.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <linux/socket.h>
#include <stdlib.h> /* exit() */
#include <locale.h>

/**
* Global variable indicating the desired level of debugging output.
Expand Down Expand Up @@ -118,6 +120,28 @@ int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))

return 0;
}

const char *nl_strerror_l(int err)
{
int errno_save = errno;
locale_t loc = newlocale(LC_MESSAGES_MASK, "", (locale_t)0);
const char *buf;

if (loc == (locale_t)0) {
if (errno == ENOENT)
loc = newlocale(LC_MESSAGES_MASK,
"POSIX", (locale_t)0);
}
if (loc != (locale_t)0) {
buf = strerror_l(err, loc);
freelocale(loc);
} else {
buf = "newlocale() failed";
}

errno = errno_save;
return buf;
}
/** @endcond */

/**
Expand Down

0 comments on commit e1ca773

Please sign in to comment.