Code

src/collectd.c: Set the LC_NUMERIC locale to "C".
authorFlorian Forster <octo@google.com>
Wed, 17 Sep 2014 08:52:36 +0000 (10:52 +0200)
committerFlorian Forster <octo@collectd.org>
Thu, 18 Sep 2014 05:31:49 +0000 (07:31 +0200)
When using ASCII protocols, such as the Graphite protocol, doubles are
converted to strings using sprintf(3) and parsed with strtod(3). If the
effective locale uses something different from a dot (".") as the
decimal separator (e.g. Portuguese, German), this breaks.

This patch sets the LC_NUMERIC locale to "C" on start-up, so that
strings are produced and parsed in a well-defined manner.

Fixes: #512
configure.ac
src/collectd.c

index bcd8b6021b677ce0d14ff7efafe7f31476a2f532..65e535a66e159d129343571e94e32478a8f5158c 100644 (file)
@@ -527,7 +527,7 @@ AC_CHECK_HEADERS(linux/un.h, [], [],
 #endif
 ])
 
-AC_CHECK_HEADERS(pwd.h grp.h sys/un.h ctype.h limits.h xfs/xqm.h fs_info.h fshelp.h paths.h mntent.h mnttab.h sys/fstyp.h sys/fs_types.h sys/mntent.h sys/mnttab.h sys/statfs.h sys/statvfs.h sys/vfs.h sys/vfstab.h kvm.h wordexp.h)
+AC_CHECK_HEADERS(pwd.h grp.h sys/un.h ctype.h limits.h xfs/xqm.h fs_info.h fshelp.h paths.h mntent.h mnttab.h sys/fstyp.h sys/fs_types.h sys/mntent.h sys/mnttab.h sys/statfs.h sys/statvfs.h sys/vfs.h sys/vfstab.h kvm.h wordexp.h locale.h)
 
 # For the dns plugin
 AC_CHECK_HEADERS(arpa/nameser.h)
@@ -602,7 +602,7 @@ AC_HEADER_TIME
 # Checks for library functions.
 #
 AC_PROG_GCC_TRADITIONAL
-AC_CHECK_FUNCS(gettimeofday select strdup strtol getaddrinfo getnameinfo strchr memcpy strstr strcmp strncmp strncpy strlen strncasecmp strcasecmp openlog closelog sysconf setenv if_indextoname)
+AC_CHECK_FUNCS(gettimeofday select strdup strtol getaddrinfo getnameinfo strchr memcpy strstr strcmp strncmp strncpy strlen strncasecmp strcasecmp openlog closelog sysconf setenv if_indextoname setlocale)
 
 AC_FUNC_STRERROR_R
 
index f711fb705a744fa44b92ec386595aef5e34a1efa..2e2d821aade194873c923ceabcf8951291d0dc69 100644 (file)
 #include "collectd.h"
 #include "common.h"
 
+#include "plugin.h"
+#include "configfile.h"
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 
 #include <pthread.h>
 
-#include "plugin.h"
-#include "configfile.h"
+#if HAVE_LOCALE_H
+# include <locale.h>
+#endif
 
 #if HAVE_STATGRAB_H
 # include <statgrab.h>
 #endif
 
+#ifndef COLLECTD_LOCALE
+# define COLLECTD_LOCALE "C"
+#endif
+
 /*
  * Global variables
  */
@@ -288,6 +296,11 @@ static void exit_usage (int status)
 
 static int do_init (void)
 {
+#if HAVE_SETLOCALE
+       if (setlocale (LC_NUMERIC, COLLECTD_LOCALE) == NULL)
+               WARNING ("setlocale (\"%s\") failed.", COLLECTD_LOCALE);
+#endif
+
 #if HAVE_LIBKSTAT
        kc = NULL;
        update_kstat ();