From: Florian Forster Date: Mon, 8 Jun 2015 16:19:40 +0000 (+0200) Subject: src/daemon/common.[ch]: Implement the "read_file()" function. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=a6f084608f38fa0ad66247f8e4b8ce7520292276;p=collectd.git src/daemon/common.[ch]: Implement the "read_file()" function. --- diff --git a/src/daemon/common.c b/src/daemon/common.c index e396b79d..84d86608 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -1,6 +1,6 @@ /** * 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"), @@ -264,6 +264,39 @@ ssize_t sread (int fd, void *buf, size_t count) 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) { diff --git a/src/daemon/common.h b/src/daemon/common.h index da21cad9..71aede0b 100644 --- a/src/daemon/common.h +++ b/src/daemon/common.h @@ -1,6 +1,6 @@ /** * 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"), @@ -99,6 +99,13 @@ char *sstrerror (int errnum, char *buf, size_t buflen); */ 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 diff --git a/src/protocols.c b/src/protocols.c index 1a39aad3..8b013155 100644 --- a/src/protocols.c +++ b/src/protocols.c @@ -77,7 +77,7 @@ static void submit (const char *protocol_name, 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]; @@ -126,7 +126,7 @@ static int read_file (const char *path) 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; } @@ -192,18 +192,18 @@ static int read_file (const char *path) 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++;