Various: Simplify code using composite literals.
[collectd.git] / src / daemon / common.c
index d2fae57..208e16d 100644 (file)
 extern kstat_ctl_t *kc;
 #endif
 
+/* AIX doesn't have MSG_DONTWAIT */
+#ifndef MSG_DONTWAIT
+#  define MSG_DONTWAIT MSG_NONBLOCK
+#endif
+
 #if !HAVE_GETPWNAM_R
 static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
 #endif
@@ -1011,7 +1016,8 @@ int format_values (char *ret, size_t ret_len, /* {{{ */
 
 int parse_identifier (char *str, char **ret_host,
                char **ret_plugin, char **ret_plugin_instance,
-               char **ret_type, char **ret_type_instance)
+               char **ret_type, char **ret_type_instance,
+               char *default_host)
 {
        char *hostname = NULL;
        char *plugin = NULL;
@@ -1030,8 +1036,19 @@ int parse_identifier (char *str, char **ret_host,
 
        type = strchr (plugin, '/');
        if (type == NULL)
-               return (-1);
-       *type = '\0'; type++;
+       {
+               if (default_host == NULL)
+                       return (-1);
+               /* else: no host specified; use default */
+               type = plugin;
+               plugin = hostname;
+               hostname = default_host;
+       }
+       else
+       {
+               *type = '\0';
+               type++;
+       }
 
        plugin_instance = strchr (plugin, '-');
        if (plugin_instance != NULL)
@@ -1072,7 +1089,8 @@ int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
 
        status = parse_identifier (str_copy, &host,
                        &plugin, &plugin_instance,
-                       &type, &type_instance);
+                       &type, &type_instance,
+                       /* default_host = */ NULL);
        if (status != 0)
                return (status);
 
@@ -1091,11 +1109,6 @@ int parse_identifier_vl (const char *str, value_list_t *vl) /* {{{ */
 
 int parse_value (const char *value_orig, value_t *ret_value, int ds_type)
 {
-  return parse_value_ext(value_orig, ret_value, ds_type, "");
-}
-
-int parse_value_ext (const char *value_orig, value_t *ret_value, int ds_type, const char *error_identifier)
-{
   char *value;
   char *endptr = NULL;
   size_t value_len;
@@ -1134,19 +1147,19 @@ int parse_value_ext (const char *value_orig, value_t *ret_value, int ds_type, co
 
     default:
       sfree (value);
-      ERROR ("parse_value %s: Invalid data source type: %i.", error_identifier, ds_type);
+      ERROR ("parse_value: Invalid data source type: %i.", ds_type);
       return -1;
   }
 
   if (value == endptr) {
-    ERROR ("parse_value %s: Failed to parse string as %s: %s.", error_identifier,
+    ERROR ("parse_value: Failed to parse string as %s: \"%s\".",
         DS_TYPE_TO_STRING (ds_type), value);
     sfree (value);
     return -1;
   }
   else if ((NULL != endptr) && ('\0' != *endptr))
-    INFO ("parse_value %s: Ignoring trailing garbage \"%s\" after %s value. "
-        "Input string was \"%s\".", error_identifier,
+    INFO ("parse_value: Ignoring trailing garbage \"%s\" after %s value. "
+        "Input string was \"%s\".",
         endptr, DS_TYPE_TO_STRING (ds_type), value_orig);
 
   sfree (value);
@@ -1231,7 +1244,9 @@ int parse_value_file (char const *path, value_t *ret_value, int ds_type)
 
        fclose (fh);
 
-       return parse_value_ext (buffer, ret_value, ds_type, path);
+       strstripnewline (buffer);
+
+       return parse_value (buffer, ret_value, ds_type);
 } /* int parse_value_file */
 
 #if !HAVE_GETPWNAM_R
@@ -1605,10 +1620,8 @@ void set_sock_opts (int sockfd) /* {{{ */
        int status;
        int socktype;
 
-       socklen_t socklen = sizeof (socklen_t);
-       int so_keepalive = 1;
-
-       status = getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &socktype, &socklen);
+       status = getsockopt (sockfd, SOL_SOCKET, SO_TYPE,
+                       &socktype, &(socklen_t) { sizeof (socktype) });
        if (status != 0)
        {
                WARNING ("set_sock_opts: failed to determine socket type");
@@ -1618,7 +1631,7 @@ void set_sock_opts (int sockfd) /* {{{ */
        if (socktype == SOCK_STREAM)
        {
                status = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
-                               &so_keepalive, sizeof (so_keepalive));
+                               &(int) {1}, sizeof (int));
                if (status != 0)
                        WARNING ("set_sock_opts: failed to set socket keepalive flag");