summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b40c428)
raw | patch | inline | side by side (parent: b40c428)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Thu, 12 Mar 2009 07:34:51 +0000 (08:34 +0100) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Thu, 12 Mar 2009 07:34:51 +0000 (08:34 +0100) |
I. e. move to `strerror_r'.
configure.ac | patch | blob | history | |
src/liboping.c | patch | blob | history |
diff --git a/configure.ac b/configure.ac
index 67bc8929b416c34a6b49aed4f45751eb21ea4924..984ad856833abf517315b577522597863d5277ec 100644 (file)
--- a/configure.ac
+++ b/configure.ac
AC_MSG_ERROR(cannot find nanosleep)))
AM_CONDITIONAL(BUILD_WITH_LIBRT, test "x$nanosleep_needs_rt" = "xyes")
+AC_FUNC_STRERROR_R
+
AC_ARG_ENABLE(debug, [AS_HELP_STRING([--enable-debug], [Enable extensive debugging output.])],
[
if test "x$enable_debug" = "xyes"
diff --git a/src/liboping.c b/src/liboping.c
index c7bdaecb08396855abd87e15a75d975ea0fcece8..c64060c0ba2ad7ef27bd1e30a13c43e2ef88e616 100644 (file)
--- a/src/liboping.c
+++ b/src/liboping.c
/*
* private (static) functions
*/
+/* Even though Posix requires "strerror_r" to return an "int",
+ * some systems (e.g. the GNU libc) return a "char *" _and_
+ * ignore the second argument ... -tokkee */
+char *sstrerror (int errnum, char *buf, size_t buflen)
+{
+ buf[0] = 0;
+
+#if !HAVE_STRERROR_R
+ {
+ snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
+ }
+/* #endif !HAVE_STRERROR_R */
+
+#elif STRERROR_R_CHAR_P
+ {
+ char *temp;
+ temp = strerror_r (errnum, buf, buflen);
+ if (buf[0] == 0)
+ {
+ if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
+ strncpy (buf, temp, buflen);
+ else
+ strncpy (buf, "strerror_r did not return "
+ "an error message", buflen);
+ }
+ }
+/* #endif STRERROR_R_CHAR_P */
+
+#else
+ if (strerror_r (errnum, buf, buflen) != 0)
+ {
+ snprintf (buf, buflen, "Error %i (%#x); "
+ "Additionally, strerror_r failed.",
+ errnum, errnum);
+ }
+#endif /* STRERROR_R_CHAR_P */
+
+ buf[buflen - 1] = 0;
+
+ return (buf);
+} /* char *sstrerror */
+
static void ping_set_error (pingobj_t *obj, const char *function,
const char *message)
{
- snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
- obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
+ snprintf (obj->errmsg, sizeof (obj->errmsg),
+ "%s: %s", function, message);
+ obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
+}
+
+static void ping_set_errno (pingobj_t *obj, const char *function,
+ int error_number)
+{
+ sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
}
static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
(struct sockaddr *) &sa, &sa_len);
if (buffer_len < 0)
{
- dprintf ("recvfrom: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("recvfrom: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
return (-1);
}
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, "gettimeofday", errno);
return (-1);
}
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, "gettimeofday", errno);
return (-1);
}
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, "gettimeofday", errno);
return (-1);
}
}
else if (status < 0)
{
- dprintf ("select: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("select: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
break;
}
else if (status == 0)
if (errno == ENETUNREACH)
return (0);
#endif
- ping_set_error (obj, "sendto", strerror (errno));
+ ping_set_errno (obj, "sendto", errno);
}
return (ret);
* sending the packet, so I will do that too */
if (gettimeofday (ptr->timer, NULL) == -1)
{
- dprintf ("gettimeofday: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("gettimeofday: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
timerclear (ptr->timer);
ret--;
continue;
close (fd);
}
+#if WITH_DEBUG
else
{
- dprintf ("open (/dev/urandom): %s\n", strerror (errno));
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("open (/dev/urandom): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
}
+#endif
}
retval = (int) random ();
status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
if (status != 0)
{
+#if defined(EAI_SYSTEM)
+ char errbuf[PING_ERRMSG_LEN];
+#endif
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(status == EAI_SYSTEM)
- ? strerror (errno) :
+ ? sstrerror (errno, errbuf, sizeof (errbuf)) :
#endif
gai_strerror (status));
ret = -1;
obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage));
if (obj->srcaddr == NULL)
{
- ping_set_error (obj, "malloc",
- strerror (errno));
+ ping_set_errno (obj, "malloc", errno);
ret = -1;
freeaddrinfo (ai_list);
break;
if ((ph->username = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, "strdup", errno);
ping_free (ph);
return (-1);
}
if ((ph->hostname = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, "strdup", errno);
ping_free (ph);
return (-1);
}
if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, "strdup", errno);
ping_free (ph);
return (-1);
}
if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
{
+#if defined(EAI_SYSTEM)
+ char errbuf[PING_ERRMSG_LEN];
+#endif
dprintf ("getaddrinfo failed\n");
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(ai_return == EAI_SYSTEM)
- ? strerror (errno) :
+ ? sstrerror (errno, errbuf, sizeof (errbuf)) :
#endif
gai_strerror (ai_return));
ping_free (ph);
ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
if (ph->fd == -1)
{
- dprintf ("socket: %s\n", strerror (errno));
- ping_set_error (obj, "socket", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("socket: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, "socket", errno);
continue;
}
if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
{
- dprintf ("bind: %s\n", strerror (errno));
- ping_set_error (obj, "bind", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("bind: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, "bind", errno);
close (ph->fd);
ph->fd = -1;
continue;