use tmpfile
Sat Feb 21 11:26:12 CET 2009 tobias@rautenkranz.ch
* use tmpfile
diff -rN -u old-myth2/Makefile new-myth2/Makefile
--- old-myth2/Makefile 2014-09-27 05:12:15.000000000 +0200
+++ new-myth2/Makefile 2014-09-27 05:12:15.000000000 +0200
@@ -1,12 +1,23 @@
all: fopenr.so
fopenr.so: fopenRedirect.o
- gcc -shared -ldl -o fopenr.so fopenRedirect.o
+ $(CC) -shared -ldl -o fopenr.so fopenRedirect.o
fopenRedirect.o: fopenRedirect.c
- gcc -Wall -fpic -c fopenRedirect.c
+ $(CC) -Wall -fpic -c fopenRedirect.c
+## testing
+tests/fopen-cat: tests/fopen_cat.o
+ $(CC) -o $@ $<
+
+.PHONY: test
+test: fopenr.so tests/fopen-cat
+ @LD_PRELOAD=./fopenr.so ./tests/fopen-cat 2 "/proc/meminfo" \
+ | awk '/Mem:/ { print $$2 }' | cmp tests/mem
+
+## clean
.PHONY: clean
clean:
- rm -f *.o
- rm -f *.so
+ rm -f -- *.o
+ rm -f -- *.so
+ rm -f -- test/*.o test/fopen-cat
diff -rN -u old-myth2/README new-myth2/README
--- old-myth2/README 2014-09-27 05:12:15.000000000 +0200
+++ new-myth2/README 2014-09-27 05:12:15.000000000 +0200
@@ -1,4 +1,4 @@
-Myth2 for Linux parses proc/meminfo,
+Myth2 for Linux parses /proc/meminfo,
but its format changed some time ago.
Therefore it won't activate OpenGL.
@@ -10,3 +10,8 @@
* Compile with: make
* Start myth2: LD_PRELOAD=./fopenr.so myth2
+
+Changelog:
+ 21.2.2009: Release 0.2 (thanks to Alan Swanson)
+ * link to ld library
+ * create meminfo dynamically
diff -rN -u old-myth2/fopenRedirect.c new-myth2/fopenRedirect.c
--- old-myth2/fopenRedirect.c 2014-09-27 05:12:15.000000000 +0200
+++ new-myth2/fopenRedirect.c 2014-09-27 05:12:15.000000000 +0200
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004 Tobias Rautenkranz <tobias@rautenkranz.ch>
+ * Copyright (c) 2004, 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@@ -27,28 +27,58 @@
#define __USE_GNU
#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
#include <dlfcn.h>
/* Old meminfo emulating 256Mb */
-char const *str = "root: total: used: free: shared: buffers: cached:\n"
- "Mem: 268435456 0 268435456 0 0 0\n"
- "Swap: 268435456 0 268435456\n";
-
-FILE* fopen(const char *filename, const char *modes)
+static const 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* (*_fopen) (const char*,const char*) = dlsym(RTLD_NEXT, "fopen");
- FILE* fp;
+ 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;
+ }
- if (strcmp(filename,"/proc/meminfo") == 0) {
- printf("Compatibility meminfo fix for OpenGL and cutscenes by Tobias Rautenkranz\n");
+ rewind(tmp_meminfo);
- fp = fopen("/tmp/meminfo-myth2", "w");
- if (fp != NULL) {
- fwrite(str, strlen(str), 1, fp);
- fclose(fp);
- }
-
- return _fopen("/tmp/meminfo-myth2", modes);
- } else
- return _fopen(filename, modes);
+ return tmp_meminfo;
+}
+
+FILE* 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());
+ return NULL;
+ }
+
+ if (strcmp(filename, "/proc/meminfo") == 0)
+ {
+ fprintf(stderr, "myth-meminfo: fixing /proc/meminfo\n");
+
+ return make_meminfo();
+ }
+ else
+ return _fopen(filename, mode);
}
diff -rN -u old-myth2/tests/fopen_cat.c new-myth2/tests/fopen_cat.c
--- old-myth2/tests/fopen_cat.c 1970-01-01 01:00:00.000000000 +0100
+++ new-myth2/tests/fopen_cat.c 2014-09-27 05:12:15.000000000 +0200
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <error.h>
+#include <errno.h>
+
+int
+main(int argc, char* argv[])
+{
+ if (argc != 3)
+ error(EXIT_FAILURE, errno, "usage: %s TIMES FILENAME", argv[0]);
+
+ int count = atoi(argv[1]);
+ const char* filename = argv[2];
+
+ int i;
+ for(i=0; i < count; i++)
+ {
+ FILE* file = fopen(filename, "r");
+ if (!file)
+ error(EXIT_FAILURE, errno, "open %s", filename);
+
+ char buffer[100];
+ size_t len;
+ do {
+ len = fread(buffer, 1, sizeof(buffer), file);
+ size_t write_len = fwrite(buffer, 1, len, stdout);
+
+ if (write_len != len)
+ error(EXIT_FAILURE, 0, "write to stdout");
+ } while (sizeof(buffer) == len || !feof(file));
+ }
+
+ return EXIT_SUCCESS;
+}
diff -rN -u old-myth2/tests/mem new-myth2/tests/mem
--- old-myth2/tests/mem 1970-01-01 01:00:00.000000000 +0100
+++ new-myth2/tests/mem 2014-09-27 05:12:15.000000000 +0200
@@ -0,0 +1,2 @@
+268435456
+268435456