chiark / gitweb /
manager: flush memory stream before using the buffer
authorDaniel Mack <zonque@gmail.com>
Fri, 7 Mar 2014 13:43:59 +0000 (14:43 +0100)
committerDaniel Mack <zonque@gmail.com>
Fri, 7 Mar 2014 14:15:56 +0000 (15:15 +0100)
When the manager receives a SIGUSR2 signal, it opens a memory stream
with open_memstream(), uses the returned file handle for logging, and
dumps the logged content with log_dump().

However, the char* buffer is only safe to use after the file handle has
been flushed with fflush, as the man pages states:

  When the stream is closed (fclose(3)) or flushed (fflush(3)), the
  locations pointed to by ptr and sizeloc are updated to contain,
  respectively, a pointer to the buffer and the current size of  the
  buffer.
  These values remain valid only as long as the caller performs no
  further output on the stream.  If further output is performed, then the
  stream must again be flushed before trying to access these variables.

Without that call, dump remains NULL and the daemon crashes in
log_dump().

src/core/manager.c

index 27a1cc6777eaf2be55821236f9355b4a99edff93..78f4f3dfd8ec3c46fd9cd05379fc7db5a5c15d1d 100644 (file)
@@ -1621,6 +1621,11 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                                 break;
                         }
 
+                        if (fflush(f)) {
+                                log_warning("Failed to flush status stream");
+                                break;
+                        }
+
                         log_dump(LOG_INFO, dump);
                         break;
                 }