Module: xenomai-forge
Branch: master
Commit: b0a867efad33ddeaeb7d1daa4b98defc039f6da1
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=b0a867efad33ddeaeb7d1daa4b98defc039f6da1

Author: Jan Kiszka <jan.kis...@siemens.com>
Date:   Wed Feb 12 18:48:58 2014 +0100

lib/cobalt: Flush print buffer on fclose

As we keep a reference to the stream that was written to via fwrite &
Co., we need to flush our buffers before closing streams. Otherwise, we
risk that print_buffers crashes while trying to use a stale FILE object.

Signed-off-by: Jan Kiszka <jan.kis...@siemens.com>

---

 include/cobalt/stdio.h     |    2 ++
 lib/cobalt/cobalt.wrappers |    1 +
 lib/cobalt/printf.c        |    6 ++++++
 lib/cobalt/wrappers.c      |    6 ++++++
 4 files changed, 15 insertions(+)

diff --git a/include/cobalt/stdio.h b/include/cobalt/stdio.h
index d2c2e43..b7407fd 100644
--- a/include/cobalt/stdio.h
+++ b/include/cobalt/stdio.h
@@ -82,6 +82,8 @@ int __wrap_putchar(int c);
 COBALT_DECL(size_t,
            fwrite(const void *ptr, size_t sz, size_t nmemb, FILE *stream));
 
+COBALT_DECL(int, fclose(FILE *stream));
+
 int rt_vfprintf(FILE *stream, const char *format, va_list args);
 
 int rt_vprintf(const char *format, va_list args);
diff --git a/lib/cobalt/cobalt.wrappers b/lib/cobalt/cobalt.wrappers
index dae8c11..ef5f8ba 100644
--- a/lib/cobalt/cobalt.wrappers
+++ b/lib/cobalt/cobalt.wrappers
@@ -95,6 +95,7 @@
 --wrap fputc
 --wrap putchar
 --wrap fwrite
+--wrap fclose
 --wrap syslog
 --wrap vsyslog
 --wrap malloc
diff --git a/lib/cobalt/printf.c b/lib/cobalt/printf.c
index 2e2bbe5..ba75ef5 100644
--- a/lib/cobalt/printf.c
+++ b/lib/cobalt/printf.c
@@ -921,6 +921,12 @@ COBALT_IMPL(size_t, fwrite, (const void *ptr, size_t size, 
size_t nmemb, FILE *s
 
 }
 
+COBALT_IMPL(int, fclose, (FILE *stream))
+{
+       rt_print_flush_buffers();
+       return __STD(fclose(stream));
+}
+
 COBALT_IMPL(void, vsyslog, (int priority, const char *fmt, va_list ap))
 {
        if (cobalt_get_current() != XN_NO_HANDLE &&
diff --git a/lib/cobalt/wrappers.c b/lib/cobalt/wrappers.c
index a4f643e..bf07a04 100644
--- a/lib/cobalt/wrappers.c
+++ b/lib/cobalt/wrappers.c
@@ -368,6 +368,12 @@ size_t __real_fwrite(const void *ptr, size_t sz, size_t 
nmemb, FILE *stream)
 }
 
 __attribute__ ((weak))
+int __real_fclose(FILE *stream)
+{
+       return fclose(stream);
+}
+
+__attribute__ ((weak))
 void __real_syslog(int priority, const char *fmt, ...)
 {
        va_list args;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to