create meminfo in memory
fopenRedirect.c
Sat Feb 21 12:08:59 CET 2009 tobias@rautenkranz.ch
* create meminfo in memory
--- old-myth2/fopenRedirect.c 2014-10-30 08:04:51.000000000 +0100
+++ new-myth2/fopenRedirect.c 2014-10-30 08:04:51.000000000 +0100
@@ -23,50 +23,22 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
+#define _GNU_SOURCE
#include <stdio.h>
-
-#define __USE_GNU
#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
#include <dlfcn.h>
/* Old meminfo emulating 256Mb */
-static const char meminfo_str[] =
+static char meminfo_str[] =
"root: total: used: free: shared: buffers: cached:\n"
"Mem: 268435456 0 268435456 0 0 0\n"
"Swap: 268435456 0 268435456\n";
-int tmp_meminfo = -1;
-
-/** Returns a stream to an meminfo file. */
FILE*
-make_meminfo()
-{
- FILE* tmp_meminfo = tmpfile();
-
- if (!tmp_meminfo)
- {
- perror("myth-meminfo: creating temponary meminfo");
- return NULL;
- }
-
- size_t len = fwrite(meminfo_str, 1, sizeof(meminfo_str), tmp_meminfo);
- if (len != sizeof(meminfo_str))
- {
- perror("myth-meminfo: writing temporary meminfo");
- return NULL;
- }
-
- rewind(tmp_meminfo);
-
- return tmp_meminfo;
-}
-
-FILE* fopen(const char *filename, const char *mode)
+fopen(const char *filename, const char *mode)
{
FILE* (*_fopen)(const char*,const char*) = dlsym(RTLD_NEXT, "fopen");
+
if (!_fopen)
{
fprintf(stderr, "myth-meminfo: dlsym: %s\n", dlerror());
@@ -77,7 +49,7 @@
{
fprintf(stderr, "myth-meminfo: fixing /proc/meminfo\n");
- return make_meminfo();
+ return fmemopen(meminfo_str, sizeof(meminfo_str), mode);
}
else
return _fopen(filename, mode);