summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c97c875)
raw | patch | inline | side by side (parent: c97c875)
author | Florian Forster <octo@collectd.org> | |
Wed, 20 Feb 2013 08:14:15 +0000 (09:14 +0100) | ||
committer | Florian Forster <octo@collectd.org> | |
Wed, 20 Feb 2013 08:14:15 +0000 (09:14 +0100) |
snort_read() handles the file opening / mmap part of the process.
snort_read_buffer() parses the buffer, splits the last line and calls
the submit function. This way cleaning up the FD and memory mapping is
easier in case of an error when parsing the file.
snort_read_buffer() parses the buffer, splits the last line and calls
the submit function. This way cleaning up the FD and memory mapping is
easier in case of an error when parsing the file.
src/snort.c | patch | blob | history |
diff --git a/src/snort.c b/src/snort.c
index a49cb5fe52a0b1f4c7b061fba733d02a426a2a99..208258228b0c6da4d32384719da68f6790077466 100644 (file)
--- a/src/snort.c
+++ b/src/snort.c
@@ -88,58 +88,25 @@ static int snort_read_submit(instance_definition_t *id, metric_definition_t *md,
return (0);
}
-static int snort_read(user_data_t *ud){
- instance_definition_t *id;
- metric_definition_t *md;
-
+static int snort_read_buffer (instance_definition_t *id,
+ char const *buffer, size_t buffer_size)
+{
int i;
- int fd;
char **metrics;
int metrics_num;
- struct stat sb;
char *buf, *buf_ptr;
/* mmap, char pointers */
- char *p_start;
- char *p_end;
-
- id = ud->data;
- DEBUG("snort plugin: snort_read (instance = %s)", id->name);
-
- fd = open(id->path, O_RDONLY);
- if (fd == -1){
- ERROR("snort plugin: Unable to open `%s'.", id->path);
- return (-1);
- }
-
- if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){
- ERROR("snort plugin: `%s' is not a file.", id->path);
- close (fd);
- return (-1);
- }
-
- if (sb.st_size == 0){
- ERROR("snort plugin: `%s' is empty.", id->path);
- close (fd);
- return (-1);
- }
-
- p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd,
- /* offset = */ 0);
- if (p_start == MAP_FAILED){
- ERROR("snort plugin: mmap error");
- close (fd);
- return (-1);
- }
+ char const *p_end;
/* Set the start value count. */
metrics_num = 1;
/* Set the pointer to the last line of the file and count the fields.
(Skip the last two characters of the buffer: `\n' and `\0') */
- for (p_end = (p_start + sb.st_size) - 2; p_end > p_start; --p_end){
+ for (p_end = (buffer + buffer_size) - 2; p_end > buffer; --p_end){
if (*p_end == ','){
++metrics_num;
} else if (*p_end == '\n'){
if (metrics_num == 1){
ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path);
- close (fd);
- munmap(p_start, sb.st_size);
return (-1);
}
if (*p_end == '#'){
ERROR("snort plugin: last line of `%s' is a comment.", id->path);
- close (fd);
- munmap(p_start, sb.st_size);
return (-1);
}
/* Copy the line to the buffer */
buf = strdup(p_end);
- /* Done with mmap and file pointer */
- close(fd);
- munmap(p_start, sb.st_size);
-
/* Create a list of all values */
metrics = calloc (metrics_num, sizeof (*metrics));
if (metrics == NULL) {
ERROR ("snort plugin: calloc failed.");
sfree (buf);
- return (-1);
+ return (ENOMEM);
}
buf_ptr = buf;
/* Register values */
for (i = 0; i < id->metric_list_len; ++i){
- md = id->metric_list[i];
+ metric_definition_t *md = id->metric_list[i];
if (md->index >= metrics_num) {
ERROR ("snort plugin: Metric \"%s\": Request for index %i when "
return (0);
}
+static int snort_read(user_data_t *ud){
+ instance_definition_t *id;
+
+ int fd;
+
+ struct stat sb;
+
+ /* mmap, char pointers */
+ char *p_start;
+
+ id = ud->data;
+ DEBUG("snort plugin: snort_read (instance = %s)", id->name);
+
+ fd = open(id->path, O_RDONLY);
+ if (fd == -1){
+ ERROR("snort plugin: Unable to open `%s'.", id->path);
+ return (-1);
+ }
+
+ if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){
+ ERROR("snort plugin: `%s' is not a file.", id->path);
+ close (fd);
+ return (-1);
+ }
+
+ if (sb.st_size == 0){
+ ERROR("snort plugin: `%s' is empty.", id->path);
+ close (fd);
+ return (-1);
+ }
+
+ p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd,
+ /* offset = */ 0);
+ if (p_start == MAP_FAILED){
+ ERROR("snort plugin: mmap error");
+ close (fd);
+ return (-1);
+ }
+
+ snort_read_buffer (id, p_start, (size_t) sb.st_size);
+
+ /* Done with mmap and file pointer */
+ close(fd);
+ munmap(p_start, sb.st_size);
+ return (0);
+}
+
static void snort_metric_definition_destroy(void *arg){
metric_definition_t *md;