Răsfoiți Sursa

Fix usage of strerror_r()/perror()

Signed-off-by: Jerome Flesch <jerome.flesch@netasq.com>
Reviewed-by: Angus Salkeld <asalkeld@redhat.com>
(backported from commit 00434a4f10f0a0b0dfb1714504860d7ef560f7fb)
Jerome Flesch 14 ani în urmă
părinte
comite
19e7a309de
11 a modificat fișierele cu 164 adăugiri și 137 ștergeri
  1. 8 15
      exec/coroipcs.c
  2. 5 2
      exec/coroparse.c
  3. 2 3
      exec/coropoll.c
  4. 20 4
      exec/logsys.c
  5. 7 14
      exec/main.c
  6. 5 4
      exec/totemconfig.c
  7. 8 14
      exec/totemsrp.c
  8. 52 47
      exec/totemudp.c
  9. 21 32
      exec/totemudpu.c
  10. 26 0
      include/corosync/engine/logsys.h
  11. 10 2
      lcr/uis.c

+ 8 - 15
exec/coroipcs.c

@@ -1040,9 +1040,8 @@ static void _corosync_ipc_init(void)
 
 	res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_CRIT, "Could not set non-blocking operation on server socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
+			"Could not set non-blocking operation on server socket");
 		api->fatal_error ("Could not set non-blocking operation on server socket");
 	}
 
@@ -1069,9 +1068,8 @@ static void _corosync_ipc_init(void)
 
 	res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
 	if (res) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_CRIT, "Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
+				"Could not bind AF_UNIX (%s)", un_addr.sun_path);
 		api->fatal_error ("Could not bind to AF_UNIX socket\n");
 	}
 
@@ -1477,20 +1475,15 @@ retry_accept:
 	}
 
 	if (new_fd == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_ERROR,
-			"Could not accept Library connection: %s\n", error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
+			"Could not accept Library connection");
 		return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
 	}
 
 	res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_ERROR,
-			"Could not set non-blocking operation on library connection: %s\n",
-			error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
+			"Could not set non-blocking operation on library connection");
 		close (new_fd);
 		return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
 	}

+ 5 - 2
exec/coroparse.c

@@ -54,6 +54,8 @@
 #include <corosync/lcr/lcr_comp.h>
 #include <corosync/engine/objdb.h>
 #include <corosync/engine/config.h>
+#define LOGSYS_UTILS_ONLY 1
+#include <corosync/engine/logsys.h>
 
 #include "util.h"
 
@@ -360,10 +362,11 @@ static int read_config_file_into_objdb(
 	fp = fopen (filename, "r");
 	if (fp == NULL) {
 		char error_str[100];
-		strerror_r (errno, error_str, 100);
+		const char *error_ptr;
+		LOGSYS_STRERROR_R (error_ptr, errno, error_str, sizeof(error_str));
 		snprintf (error_reason, sizeof(error_string_response),
 			"Can't read file %s reason = (%s)\n",
-			 filename, error_str);
+			 filename, error_ptr);
 		*error_string = error_reason;
 		return -1;
 	}

+ 2 - 3
exec/coropoll.c

@@ -49,6 +49,7 @@
 #include <corosync/totem/coropoll.h>
 #include <corosync/list.h>
 #include "tlist.h"
+#include "util.h"
 
 typedef int (*dispatch_fn_t) (hdb_handle_t hdb_handle, int fd, int revents, void *data);
 
@@ -418,9 +419,7 @@ static void poll_fds_usage_check(struct poll_instance *poll_instance)
 
 	if (socks_limit == 0) {
 		if (getrlimit(RLIMIT_NOFILE, &lim) == -1) {
-			char error_str[100];
-			strerror_r(errno, error_str, 100);
-			printf("getrlimit: %s\n", error_str);
+			perror("getrlimit() failed");
 			return;
 		}
 		socks_limit = lim.rlim_cur;

+ 20 - 4
exec/logsys.c

@@ -66,6 +66,8 @@
 #include <corosync/list.h>
 #include <corosync/engine/logsys.h>
 
+#include "util.h"
+
 #define YIELD_AFTER_LOG_OPS 10
 
 #define MIN(x,y) ((x) < (y) ? (x) : (y))
@@ -906,14 +908,28 @@ static int logsys_config_file_set_unlocked (
 
 	logsys_loggers[subsysid].logfile_fp = fopen (file, "a+");
 	if (logsys_loggers[subsysid].logfile_fp == NULL) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
+		int err;
+		char error_str[LOGSYS_MAX_PERROR_MSG_LEN];
+		const char *error_ptr;
+
+		err = errno;
+#ifdef COROSYNC_LINUX
+		/* The GNU version of strerror_r returns a (char*) that *must* be used */
+		error_ptr = strerror_r(err, error_str, sizeof(error_str));
+#else
+		/* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
+		if ( strerror_r(err, error_str, sizeof(error_str)) < 0 )
+			error_ptr = "";
+		else
+			error_ptr = error_str;
+#endif
+
 		free(logsys_loggers[subsysid].logfile);
 		logsys_loggers[subsysid].logfile = NULL;
 		snprintf (error_string_response,
 			sizeof(error_string_response),
-			"Can't open logfile '%s' for reason (%s).\n",
-				 file, error_str);
+			"Can't open logfile '%s' for reason: %s (%d).\n",
+				 file, error_ptr, err);
 		*error_string = error_string_response;
 		return (-1);
 	}

+ 7 - 14
exec/main.c

@@ -549,11 +549,8 @@ static void corosync_mlockall (void)
 #else
 	res = mlockall (MCL_CURRENT | MCL_FUTURE);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_WARNING,
-			"Could not lock memory of service to avoid page faults: %s\n",
-			error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_WARNING,
+			"Could not lock memory of service to avoid page faults");
 	};
 #endif
 }
@@ -1245,10 +1242,9 @@ static void corosync_setscheduler (void)
 		global_sched_param.sched_priority = sched_priority;
 		res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
 		if (res == -1) {
-			char error_str[100];
-			strerror_r (errno, error_str, 100);
-			log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
-				global_sched_param.sched_priority, error_str);
+			LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING,
+				"Could not set SCHED_RR at priority %d",
+				global_sched_param.sched_priority);
 
 			global_sched_param.sched_priority = 0;
 			logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
@@ -1270,11 +1266,8 @@ static void corosync_setscheduler (void)
 			}
 		}
 	} else {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (LOGSYS_LEVEL_WARNING,
-			"Could not get maximum scheduler priority: %s\n",
-			error_str);
+		LOGSYS_PERROR (errno, LOGSYS_LEVEL_WARNING,
+			"Could not get maximum scheduler priority");
 		sched_priority = 0;
 	}
 #else

+ 5 - 4
exec/totemconfig.c

@@ -728,13 +728,14 @@ static int read_keyfile (
 	ssize_t expected_key_len = sizeof (totem_config->private_key);
 	int saved_errno;
 	char error_str[100];
+	const char *error_ptr;
 
 	fd = open (key_location, O_RDONLY);
 	if (fd == -1) {
-		strerror_r (errno, error_str, 100);
+		LOGSYS_STRERROR_R (error_ptr, errno, error_str, sizeof(error_str));
 		snprintf (error_string_response, sizeof(error_string_response),
 			"Could not open %s: %s\n",
-			 key_location, error_str);
+			 key_location, error_ptr);
 		goto parse_error;
 	}
 
@@ -743,10 +744,10 @@ static int read_keyfile (
 	close (fd);
 
 	if (res == -1) {
-		strerror_r (errno, error_str, 100);
+		LOGSYS_STRERROR_R (error_ptr, saved_errno, error_str, sizeof(error_str));
 		snprintf (error_string_response, sizeof(error_string_response),
 			"Could not read %s: %s\n",
-			 key_location, error_str);
+			 key_location, error_ptr);
 		goto parse_error;
 	}
 

+ 8 - 14
exec/totemsrp.c

@@ -90,6 +90,7 @@
 
 #include "crypto.h"
 #include "tlist.h"
+#include "util.h"
 
 #define LOCALHOST_IP				inet_addr("127.0.0.1")
 #define QUEUE_RTR_ITEMS_SIZE_MAX		16384 /* allow 16384 retransmit items */
@@ -3095,7 +3096,6 @@ static void memb_ring_id_create_or_load (
 	int fd;
 	int res = 0;
 	char filename[PATH_MAX];
-	char error_str[256];
 
 	snprintf (filename, sizeof(filename), "%s/ringid_%s",
 		rundir, totemip_print (&instance->my_id.addr[0]));
@@ -3118,16 +3118,12 @@ static void memb_ring_id_create_or_load (
 			res = write (fd, &memb_ring_id->seq, sizeof (uint64_t));
 			close (fd);
 			if (res == -1) {
-				strerror_r (errno, error_str, sizeof (error_str));
-				log_printf (instance->totemsrp_log_level_warning,
-					"Couldn't write ringid file '%s' %s\n",
-					filename, error_str);
+				LOGSYS_PERROR (errno, instance->totemsrp_log_level_warning,
+					"Couldn't write ringid file '%s'", filename);
 			}
 		} else {
-			strerror_r (errno, error_str, sizeof (error_str));
-			log_printf (instance->totemsrp_log_level_warning,
-				"Couldn't create ringid file '%s' %s\n",
-				filename, error_str);
+			LOGSYS_PERROR (errno, instance->totemsrp_log_level_warning,
+				"Couldn't create ringid file '%s'", filename);
 		}
 	}
 
@@ -3154,11 +3150,9 @@ static void memb_ring_id_set_and_store (
 		fd = open (filename, O_CREAT|O_RDWR, 0777);
 	}
 	if (fd == -1) {
-		char error_str[100];
-		strerror_r(errno, error_str, 100);
-		log_printf (instance->totemsrp_log_level_warning,
-			"Couldn't store new ring id %llx to stable storage (%s)\n",
-				instance->my_ring_id.seq, error_str);
+		LOGSYS_PERROR(errno, instance->totemsrp_log_level_warning,
+			"Couldn't store new ring id %llx to stable storage",
+			instance->my_ring_id.seq);
 		assert (0);
 		return;
 	}

+ 52 - 47
exec/totemudp.c

@@ -69,6 +69,7 @@
 #include "wthread.h"
 
 #include "crypto.h"
+#include "util.h"
 
 #ifdef HAVE_LIBNSS
 #include <nss.h>
@@ -961,10 +962,8 @@ static inline void ucast_sendmsg (
 	res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
 		MSG_NOSIGNAL);
 	if (res < 0) {
-		char error_str[100];
-		strerror_r (errno, error_str, sizeof(error_str));
-		log_printf (instance->totemudp_log_level_debug,
-				"sendmsg(ucast) failed (non-critical): %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
+			"sendmsg(ucast) failed (non-critical)");
 	}
 }
 
@@ -1046,10 +1045,8 @@ static inline void mcast_sendmsg (
 	res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
 		MSG_NOSIGNAL);
 	if (res < 0) {
-		char error_str[100];
-		strerror_r (errno, error_str, sizeof(error_str));
-		log_printf (instance->totemudp_log_level_debug,
-				"sendmsg(mcast) failed (non-critical): %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
+			"sendmsg(mcast) failed (non-critical)");
 	}
 }
 
@@ -1126,10 +1123,8 @@ static void totemudp_mcast_worker_fn (void *thread_state, void *work_item_in)
 	res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
 		MSG_NOSIGNAL);
 	if (res < 0) {
-		char error_str[100];
-		strerror_r (errno, error_str, sizeof(error_str));
-		log_printf (instance->totemudp_log_level_debug,
-				"sendmsg(mcast) failed (non-critical): %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
+			"sendmsg(mcast) failed (non-critical)");
 	}
 }
 
@@ -1403,12 +1398,9 @@ static void totemudp_traffic_control_set(struct totemudp_instance *instance, int
 {
 #ifdef SO_PRIORITY
 	int prio = 6; /* TC_PRIO_INTERACTIVE */
-	char error_str[100];
 
 	if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudp_log_level_warning,
-			"Could not set traffic priority. (%s)\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
     }
 #endif
 }
@@ -1440,17 +1432,16 @@ static int totemudp_build_sockets_ip (
 	 */
 	sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
 	if (sockets->mcast_recv == -1) {
-		perror ("socket");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"socket() failed");
 		return (-1);
 	}
 
 	totemip_nosigpipe (sockets->mcast_recv);
 	res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudp_log_level_warning,
-			"Could not set non-blocking operation on multicast socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Could not set non-blocking operation on multicast socket");
 		return (-1);
 	}
 
@@ -1459,7 +1450,8 @@ static int totemudp_build_sockets_ip (
 	 */
 	 flag = 1;
 	 if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
-	 	perror("setsockopt reuseaddr");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"setsockopt(SO_REUSEADDR) failed");
 		return (-1);
 	}
 
@@ -1470,7 +1462,8 @@ static int totemudp_build_sockets_ip (
 		instance->totem_interface->ip_port, &sockaddr, &addrlen);
 	res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
 	if (res == -1) {
-		perror ("bind mcast recv socket failed");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"Unable to bind the socket to receive multicast packets");
 		return (-1);
 	}
 
@@ -1479,17 +1472,16 @@ static int totemudp_build_sockets_ip (
 	 */
 	sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
 	if (sockets->mcast_send == -1) {
-		perror ("socket");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"socket() failed");
 		return (-1);
 	}
 
 	totemip_nosigpipe (sockets->mcast_send);
 	res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudp_log_level_warning,
-			"Could not set non-blocking operation on multicast socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Could not set non-blocking operation on multicast socket");
 		return (-1);
 	}
 
@@ -1498,7 +1490,8 @@ static int totemudp_build_sockets_ip (
 	 */
 	 flag = 1;
 	 if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
-	 	perror("setsockopt reuseaddr");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"setsockopt(SO_REUSEADDR) failed");
 		return (-1);
 	}
 
@@ -1506,7 +1499,8 @@ static int totemudp_build_sockets_ip (
 		&sockaddr, &addrlen);
 	res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
 	if (res == -1) {
-		perror ("bind mcast send socket failed");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Unable to bind the socket to send multicast packets");
 		return (-1);
 	}
 
@@ -1515,17 +1509,16 @@ static int totemudp_build_sockets_ip (
 	 */
 	sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
 	if (sockets->token == -1) {
-		perror ("socket2");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"socket() failed");
 		return (-1);
 	}
 
 	totemip_nosigpipe (sockets->token);
 	res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudp_log_level_warning,
-			"Could not set non-blocking operation on token socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Could not set non-blocking operation on token socket");
 		return (-1);
 	}
 
@@ -1534,7 +1527,8 @@ static int totemudp_build_sockets_ip (
 	 */
 	 flag = 1;
 	 if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
-	 	perror("setsockopt reuseaddr");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"setsockopt(SO_REUSEADDR) failed");
 		return (-1);
 	}
 
@@ -1545,7 +1539,8 @@ static int totemudp_build_sockets_ip (
 	totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
 	res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
 	if (res == -1) {
-		perror ("bind token socket failed");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Unable to bind UDP unicast socket");
 		return (-1);
 	}
 
@@ -1580,12 +1575,14 @@ static int totemudp_build_sockets_ip (
 
 		if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
 			SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
-			perror("setting broadcast option");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"setting broadcast option failed");
 			return (-1);
 		}
 		if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
 			SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
-			perror("setting broadcast option");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"setting broadcast option failed");
 			return (-1);
 		}
 	} else {
@@ -1597,7 +1594,8 @@ static int totemudp_build_sockets_ip (
 			res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
 				&mreq, sizeof (mreq));
 			if (res == -1) {
-				perror ("join ipv4 multicast group failed");
+				LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+					"join ipv4 multicast group failed");
 				return (-1);
 			}
 			break;
@@ -1609,7 +1607,8 @@ static int totemudp_build_sockets_ip (
 			res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
 				&mreq6, sizeof (mreq6));
 			if (res == -1) {
-				perror ("join ipv6 multicast group failed");
+				LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+					"join ipv6 multicast group failed");
 				return (-1);
 			}
 			break;
@@ -1631,7 +1630,8 @@ static int totemudp_build_sockets_ip (
 			&flag, sizeof (flag));
 	}
 	if (res == -1) {
-		perror ("turn off loopback");
+		LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+			"Unable to turn on multicast loopback");
 		return (-1);
 	}
 
@@ -1645,7 +1645,8 @@ static int totemudp_build_sockets_ip (
 		res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
 			&flag, sizeof (flag));
 		if (res == -1) {
-			perror ("setp mcast hops");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"set mcast v6 TTL failed");
 			return (-1);
 		}
 	}
@@ -1657,24 +1658,28 @@ static int totemudp_build_sockets_ip (
 		case AF_INET:
 		if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
 			&boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
-			perror ("cannot select interface");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"cannot select interface for multicast packets (send)");
 			return (-1);
 		}
 		if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
 			&boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
-			perror ("cannot select interface");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"cannot select interface for multicast packets (recv)");
 			return (-1);
 		}
 		break;
 		case AF_INET6:
 		if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
 			&interface_num, sizeof (interface_num)) < 0) {
-			perror ("cannot select interface");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"cannot select interface for multicast packets (send v6)");
 			return (-1);
 		}
 		if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
 			&interface_num, sizeof (interface_num)) < 0) {
-			perror ("cannot select interface");
+			LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
+				"cannot select interface for multicast packets (recv v6)");
 			return (-1);
 		}
 		break;

+ 21 - 32
exec/totemudpu.c

@@ -67,6 +67,7 @@
 #include "totemudpu.h"
 
 #include "crypto.h"
+#include "util.h"
 
 #ifdef HAVE_LIBNSS
 #include <nss.h>
@@ -941,10 +942,8 @@ static inline void ucast_sendmsg (
 	 */
 	res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
 	if (res < 0) {
-		char error_str[100];
-		strerror_r (errno, error_str, sizeof(error_str));
-		log_printf (instance->totemudpu_log_level_debug,
-				"sendmsg(ucast) failed (non-critical): %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
+				"sendmsg(ucast) failed (non-critical)");
 	}
 }
 
@@ -1034,10 +1033,8 @@ static inline void mcast_sendmsg (
 		 */
 		res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
 		if (res < 0) {
-			char error_str[100];
-			strerror_r (errno, error_str, sizeof(error_str));
-			log_printf (instance->totemudpu_log_level_debug,
-				"sendmsg(mcast) failed (non-critical): %s\n", error_str);
+			LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
+				"sendmsg(mcast) failed (non-critical)");
 		}
 	}
 }
@@ -1283,12 +1280,10 @@ static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, i
 {
 #ifdef SO_PRIORITY
 	int prio = 6; /* TC_PRIO_INTERACTIVE */
-	char error_str[100];
 
 	if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_warning,
-			"Could not set traffic priority. (%s)\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"Could not set traffic priority");
     }
 #endif
 }
@@ -1310,17 +1305,16 @@ static int totemudpu_build_sockets_ip (
 	 */
 	instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
 	if (instance->token_socket == -1) {
-		perror ("socket2");
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"socket() failed");
 		return (-1);
 	}
 
 	totemip_nosigpipe (instance->token_socket);
 	res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_warning,
-			"Could not set non-blocking operation on token socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"Could not set non-blocking operation on token socket");
 		return (-1);
 	}
 
@@ -1340,7 +1334,8 @@ static int totemudpu_build_sockets_ip (
 	totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
 	res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
 	if (res == -1) {
-		perror ("bind token socket failed");
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"bind token socket failed");
 		return (-1);
 	}
 
@@ -1352,10 +1347,8 @@ static int totemudpu_build_sockets_ip (
 	res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
 		&recvbuf_size, optlen);
 	if (res == -1) {
-		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_notice,
-			"Could not set recvbuf size %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
+			"Could not set recvbuf size");
 	}
 
 	return 0;
@@ -1662,7 +1655,6 @@ int totemudpu_member_add (
 	int res;
 	unsigned int sendbuf_size;
 	unsigned int optlen = sizeof (sendbuf_size);
-	char error_str[100];
 
 	new_member = malloc (sizeof (struct totemudpu_member));
 	if (new_member == NULL) {
@@ -1673,17 +1665,15 @@ int totemudpu_member_add (
 	memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
 	new_member->fd = socket (member->family, SOCK_DGRAM, 0);
 	if (new_member->fd == -1) {
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_warning,
-			"Could not create socket for new member: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"Could not create socket for new member");
 		return (-1);
 	}
 	totemip_nosigpipe (new_member->fd);
 	res = fcntl (new_member->fd, F_SETFL, O_NONBLOCK);
 	if (res == -1) {
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_warning,
-			"Could not set non-blocking operation on token socket: %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
+			"Could not set non-blocking operation on token socket");
 		return (-1);
 	}
 
@@ -1695,9 +1685,8 @@ int totemudpu_member_add (
 	res = setsockopt (new_member->fd, SOL_SOCKET, SO_SNDBUF,
 		&sendbuf_size, optlen);
 	if (res == -1) {
-		strerror_r (errno, error_str, 100);
-		log_printf (instance->totemudpu_log_level_notice,
-			"Could not set sendbuf size %s\n", error_str);
+		LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
+			"Could not set sendbuf size");
 	}
 	return (0);
 }

+ 26 - 0
include/corosync/engine/logsys.h

@@ -153,6 +153,32 @@ extern "C" {
 #define LOGSYS_DECODE_RECID(rec_ident) \
 	(((rec_ident) & LOGSYS_RECID_RECID_MASK) >> LOGSYS_SUBSYSID_END)
 
+#define LOGSYS_MAX_PERROR_MSG_LEN	128
+
+#ifdef COROSYNC_LINUX
+/* The GNU version of strerror_r returns a (char*) that *must* be used */
+#define LOGSYS_STRERROR_R(out_ptr, err_num, buffer, sizeof_buffer) \
+	out_ptr = strerror_r(err_num, buffer, sizeof_buffer);
+#else
+/* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
+#define LOGSYS_STRERROR_R(out_ptr, err_num, buffer, sizeof_buffer) do {	\
+		if ( strerror_r(err_num, buffer, sizeof_buffer) == 0 ) {		\
+			out_ptr = buffer;											\
+		} else {														\
+			out_ptr = "";												\
+		}																\
+	} while(0)
+#endif
+
+#define LOGSYS_PERROR(err_num, level, fmt, args...) do {							\
+		char _error_str[LOGSYS_MAX_PERROR_MSG_LEN];									\
+		const char *_error_ptr;														\
+		LOGSYS_STRERROR_R(_error_ptr, err_num, _error_str, sizeof(_error_str));		\
+		log_printf(level, fmt ": %s (%d)\n", ##args, _error_ptr, err_num);			\
+	} while(0)
+
+
+
 #ifndef LOGSYS_UTILS_ONLY
 
 extern int _logsys_system_setup(

+ 10 - 2
lcr/uis.c

@@ -99,8 +99,16 @@ static void uis_lcr_bind (int *server_fd)
 	res = bind (fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
 	if (res) {
 		char error_str[100];
-		strerror_r (errno, error_str, 100);
-		printf ("Could not bind AF_UNIX: %s\n", error_str);
+		const char *error_ptr;
+#ifdef _GNU_SOURCE
+/* The GNU version of strerror_r returns a (char*) that *must* be used */
+		error_ptr = strerror_r(errno, error_str, sizeof(error_str));
+#else
+/* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
+		strerror_r(errno, error_str, sizeof(error_str));
+		error_ptr = error_str;
+#endif
+		printf ("Could not bind AF_UNIX: %s\n", error_ptr);
 	}
 	listen (fd, SERVER_BACKLOG);
 	*server_fd = fd;