/**
* collectd - src/common.c
- * Copyright (C) 2005-2014 Florian octo Forster
+ * Copyright (C) 2005-2015 Florian octo Forster
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
return (0);
}
+int read_file (char const *file, void **ret_data, size_t *ret_data_size)
+{
+ int fd = open (file, O_RDONLY);
+ if (fd == -1)
+ return (-1);
+
+ struct stat statbuf = { 0 };
+ if (fstat (fd, &statbuf) == -1)
+ {
+ close (fd);
+ return (-1);
+ }
+
+ size_t data_size = (size_t) statbuf.st_size;
+ void *data = malloc (data_size);
+ if (data == NULL)
+ {
+ close (fd);
+ return (-1);
+ }
+
+ if (sread (fd, data, data_size) != 0)
+ {
+ close (fd);
+ sfree (data);
+ return (-1);
+ }
+
+ close (fd);
+ *ret_data = data;
+ *ret_data_size = data_size;
+ return (0);
+} /* }}} int read_file */
ssize_t swrite (int fd, const void *buf, size_t count)
{
/**
* collectd - src/common.h
- * Copyright (C) 2005-2014 Florian octo Forster
+ * Copyright (C) 2005-2015 Florian octo Forster
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
*/
ssize_t sread (int fd, void *buf, size_t count);
+/* read_file reads the contents of "file" into memory and stores a pointer in
+ * "ret_data", which must be freed by the caller. The number of bytes read is
+ * stored in "ret_data_size". On success, 0 is returned. On failure, an error
+ * code is stored in "errno" and -1 is returned; "ret_data" and "ret_data_size"
+ * are left unmodified. */
+int read_file (char const *file, void **ret_data, size_t *ret_data_size);
+
/*
* NAME
* swrite
plugin_dispatch_values (&vl);
} /* void submit */
-static int read_file (const char *path)
+static int protocols_read_file (const char *path)
{
FILE *fh;
char key_buffer[4096];
value_ptr = fgets (value_buffer, sizeof (value_buffer), fh);
if (value_ptr == NULL)
{
- ERROR ("protocols plugin: read_file (%s): Could not read values line.",
+ ERROR ("protocols plugin: protocols_read_file (%s): Could not read values line.",
path);
break;
}
fclose (fh);
return (status);
-} /* int read_file */
+} /* int protocols_read_file */
static int protocols_read (void)
{
int status;
int success = 0;
- status = read_file (SNMP_FILE);
+ status = protocols_read_file (SNMP_FILE);
if (status == 0)
success++;
- status = read_file (NETSTAT_FILE);
+ status = protocols_read_file (NETSTAT_FILE);
if (status == 0)
success++;