parlib: Write to FD 2 for akaros_printf()
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 4 May 2017 18:48:15 +0000 (14:48 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 4 May 2017 18:48:15 +0000 (14:48 -0400)
And be a little more careful with snprint().

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/debug.c
user/parlib/include/parlib/stdio.h

index 1419a34..46a96d3 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 
+/* This is called from glibc in delicate places, like signal handlers.  We might
+ * as well just write any valid output to FD 2. */
 int akaros_printf(const char *format, ...)
 {
+       char buf[128];
        va_list ap;
        int ret;
 
        va_start(ap, format);
-       ret = vprintf(format, ap);
+       ret = vsnprintf(buf, sizeof(buf), format, ap);
        va_end(ap);
+       if (ret < 0)
+               return ret;
+       write(2, buf, MIN(sizeof(buf), ret));
        return ret;
 }
 
index 572b473..8ed87b5 100644 (file)
@@ -40,6 +40,9 @@ do {                                                                           \
        else                                                                       \
                panic("__vc_ctx tried to fprintf to non-std stream!");                 \
        ret = snprintf(buf, sizeof(buf), __VA_ARGS__);                             \
+       /* Just print whatever we can */                                           \
+       ret = MAX(ret, 0);                                                         \
+       ret = MIN(ret, sizeof(buf));                                               \
        write(fd, buf, ret);                                                       \
 } while (0)