X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Funixsock.c;h=215abdd01ec1daeaf8819781bbdd79cac5cc4af9;hb=aa39b2bf774bf297f6c1a3967764bd7d84e0a49f;hp=75f3117e1e54136f2be78d2037df7c1d9316874a;hpb=1905f1c798900322495f8c988820930c2a1bcae2;p=collectd.git diff --git a/src/unixsock.c b/src/unixsock.c index 75f3117e..215abdd0 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -29,7 +29,6 @@ #include #include -#include #include @@ -85,52 +84,6 @@ static unsigned int cache_oldest = UINT_MAX; /* * Functions */ -static int parse_identifier (char *str, char **ret_host, - char **ret_plugin, char **ret_plugin_instance, - char **ret_type, char **ret_type_instance) -{ - char *hostname = NULL; - char *plugin = NULL; - char *plugin_instance = NULL; - char *type = NULL; - char *type_instance = NULL; - - hostname = str; - if (hostname == NULL) - return (-1); - - plugin = strchr (hostname, '/'); - if (plugin == NULL) - return (-1); - *plugin = '\0'; plugin++; - - type = strchr (plugin, '/'); - if (type == NULL) - return (-1); - *type = '\0'; type++; - - plugin_instance = strchr (plugin, '-'); - if (plugin_instance != NULL) - { - *plugin_instance = '\0'; - plugin_instance++; - } - - type_instance = strchr (type, '-'); - if (type_instance != NULL) - { - *type_instance = '\0'; - type_instance++; - } - - *ret_host = hostname; - *ret_plugin = plugin; - *ret_plugin_instance = plugin_instance; - *ret_type = type; - *ret_type_instance = type_instance; - return (0); -} /* int parse_identifier */ - static value_cache_t *cache_search (const char *name) { value_cache_t *vc; @@ -150,9 +103,20 @@ static int cache_insert (const data_set_t *ds, const value_list_t *vl) value_cache_t *vc; int i; - DEBUG ("ds->ds_num = %i; vl->values_len = %i;", - ds->ds_num, vl->values_len); + DEBUG ("unixsock plugin: cache_insert: ds->type = %s; ds->ds_num = %i;" + " vl->values_len = %i;", + ds->type, ds->ds_num, vl->values_len); +#if COLLECT_DEBUG assert (ds->ds_num == vl->values_len); +#else + if (ds->ds_num != vl->values_len) + { + ERROR ("unixsock plugin: ds->type = %s: (ds->ds_num = %i) != " + "(vl->values_len = %i)", + ds->type, ds->ds_num, vl->values_len); + return (-1); + } +#endif vc = (value_cache_t *) malloc (sizeof (value_cache_t)); if (vc == NULL) @@ -242,12 +206,24 @@ static int cache_update (const data_set_t *ds, const value_list_t *vl) vc = cache_search (name); + /* pthread_mutex_lock is called by cache_insert. */ if (vc == NULL) return (cache_insert (ds, vl)); assert (vc->values_num == ds->ds_num); assert (vc->values_num == vl->values_len); + /* Avoid floating-point exceptions due to division by zero. */ + if (vc->time >= vl->time) + { + pthread_mutex_unlock (&cache_lock); + ERROR ("unixsock plugin: vc->time >= vl->time. vc->time = %u; " + "vl->time = %u; vl = %s;", + (unsigned int) vc->time, (unsigned int) vl->time, + name); + return (-1); + } /* if (vc->time >= vl->time) */ + /* * Update the values. This is possibly a lot more that you'd expect * because we honor min and max values and handle counter overflows here. @@ -472,9 +448,11 @@ static int us_handle_getval (FILE *fh, char **fields, int fields_num) status = format_name (name, sizeof (name), hostname, plugin, plugin_instance, type, type_instance); - /* FIXME: Send some response */ if (status != 0) + { + fprintf (fh, "-1 format_name failed.\n"); return (-1); + } pthread_mutex_lock (&cache_lock); @@ -544,14 +522,16 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) return (-1); } - /* FIXME: Send some response */ - if ((strlen (hostname) > sizeof (vl.host)) - || (strlen (plugin) > sizeof (vl.plugin)) + if ((strlen (hostname) >= sizeof (vl.host)) + || (strlen (plugin) >= sizeof (vl.plugin)) || ((plugin_instance != NULL) - && (strlen (plugin_instance) > sizeof (vl.plugin_instance))) + && (strlen (plugin_instance) >= sizeof (vl.plugin_instance))) || ((type_instance != NULL) - && (strlen (type_instance) > sizeof (vl.type_instance)))) + && (strlen (type_instance) >= sizeof (vl.type_instance)))) + { + fprintf (fh, "-1 Identifier too long."); return (-1); + } strcpy (vl.host, hostname); strcpy (vl.plugin, plugin); @@ -564,7 +544,10 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) char *t = fields[2]; char *v = strchr (t, ':'); if (v == NULL) + { + fprintf (fh, "-1 No time found."); return (-1); + } *v = '\0'; v++; vl.time = (time_t) atoi (t); @@ -579,10 +562,11 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) return (-1); value_ptr = (char **) calloc (ds->ds_num, sizeof (char *)); - /* FIXME: Send some response */ if (value_ptr == NULL) + { + fprintf (fh, "-1 calloc failed."); return (-1); - + } { /* parse the value-list. It's colon-separated. */ char *dummy; @@ -607,7 +591,8 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) if (i != ds->ds_num) { sfree (value_ptr); - /* FIXME: Send some response */ + fprintf (fh, "-1 Number of values incorrect: Got %i, " + "expected %i.", i, ds->ds_num); return (-1); } } /* done parsing the value-list */ @@ -617,6 +602,7 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num) if (vl.values == NULL) { sfree (value_ptr); + fprintf (fh, "-1 malloc failed."); return (-1); } DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values); @@ -701,7 +687,7 @@ static void *us_handle_client (void *arg) } else { - fprintf (fh, "Unknown command: %s\n", fields[0]); + fprintf (fh, "-1 Unknown command: %s\n", fields[0]); fflush (fh); } } /* while (fgets) */ @@ -771,6 +757,15 @@ static void *us_server_thread (void *arg) close (sock_fd); sock_fd = -1; + status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH); + if (status != 0) + { + char errbuf[1024]; + NOTICE ("unixsock plugin: unlink (%s) failed: %s", + (sock_file != NULL) ? sock_file : US_DEFAULT_PATH, + sstrerror (errno, errbuf, sizeof (errbuf))); + } + return ((void *) 0); } /* void *us_server_thread */