# define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
#endif
-#define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
+#define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
/*
* Private data structures
/*
* 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;
} /* while (this != NULL) */
pthread_mutex_unlock (&cache_lock);
-} /* int cache_flush */
+} /* void cache_flush */
static int us_open_socket (void)
{
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);
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);
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);
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;
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 */
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);
return (0);
} /* int us_handle_putval */
+static int us_handle_listval (FILE *fh, char **fields, int fields_num)
+{
+ char buffer[1024];
+ char **value_list = NULL;
+ int value_list_len = 0;
+ value_cache_t *entry;
+ int i;
+
+ if (fields_num != 1)
+ {
+ DEBUG ("unixsock plugin: us_handle_listval: "
+ "Wrong number of fields: %i", fields_num);
+ fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
+ fields_num);
+ fflush (fh);
+ return (-1);
+ }
+
+ pthread_mutex_lock (&cache_lock);
+
+ for (entry = cache_head; entry != NULL; entry = entry->next)
+ {
+ char **tmp;
+
+ snprintf (buffer, sizeof (buffer), "%u %s\n",
+ (unsigned int) entry->time, entry->name);
+ buffer[sizeof (buffer) - 1] = '\0';
+
+ tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
+ if (tmp == NULL)
+ continue;
+ value_list = tmp;
+
+ value_list[value_list_len] = strdup (buffer);
+
+ if (value_list[value_list_len] != NULL)
+ value_list_len++;
+ } /* for (entry) */
+
+ pthread_mutex_unlock (&cache_lock);
+
+ DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
+ fprintf (fh, "%i Values found\n", value_list_len);
+ for (i = 0; i < value_list_len; i++)
+ fputs (value_list[i], fh);
+ fflush (fh);
+
+ return (0);
+} /* int us_handle_listval */
+
static void *us_handle_client (void *arg)
{
int fd;
{
us_handle_putval (fh, fields, fields_num);
}
+ else if (strcasecmp (fields[0], "listval") == 0)
+ {
+ us_handle_listval (fh, fields, fields_num);
+ }
else
{
- fprintf (fh, "Unknown command: %s\n", fields[0]);
+ fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
fflush (fh);
}
} /* while (fgets) */