Browse Source

Line-buffer stderr output from pir subprocesses

Ian Goldberg 3 years ago
parent
commit
50f7a52282
1 changed files with 24 additions and 1 deletions
  1. 24 1
      src/feature/hs/hs_pirprocess.c

+ 24 - 1
src/feature/hs/hs_pirprocess.c

@@ -26,6 +26,7 @@ typedef enum {
 struct pir_process_st {
     process_handle_t *process;
     buf_t *stdin_buf;
+    buf_t *stderr_buf;
     struct event *stdin_ev;
     struct event *stdout_ev;
     struct event *stderr_ev;
@@ -151,11 +152,29 @@ pirprocess_stderrcb(evutil_socket_t fd, short what, void *arg)
         return;
     }
 
+    /* Add the data to the stderr buffer */
     char buf[1000];
     int res = read(fd, buf, sizeof(buf)-1);
     if (res <= 0) return;
     buf[res] = '\0';
-    log_info(LD_DIRSERV,"%s %s", handle->loglabel, escaped(buf));
+    buf_add(handle->stderr_buf, buf, res);
+
+    /* Read lines from the stderr_buf */
+    while (1) {
+        size_t linesize = 0;
+        res = buf_get_line(handle->stderr_buf, NULL, &linesize);
+        if (res == 0) {
+            /* No complete lines in the buffer */
+            break;
+        }
+        char *linebuf = tor_malloc(linesize);
+        buf_get_line(handle->stderr_buf, linebuf, &linesize);
+        if (linesize > 0 && linebuf[linesize-1] == '\n') {
+            linebuf[linesize-1] = '\0';
+        }
+        log_info(LD_DIRSERV,"%s %s", handle->loglabel, escaped(linebuf));
+        tor_free(linebuf);
+    }
 }
 
 void
@@ -172,6 +191,9 @@ hs_pirprocess_close(pir_process_t *handlep)
     if (handle->stdin_buf) {
         buf_free(handle->stdin_buf);
     }
+    if (handle->stderr_buf) {
+        buf_free(handle->stderr_buf);
+    }
     if (handle->stdin_ev) {
         event_free(handle->stdin_ev);
     }
@@ -263,6 +285,7 @@ hs_pirprocess_poke(const char *path, const char *loglabel,
     /* And one for writability to the pir process' stdin, but don't add
      * it just yet.  Also create the buffer it will use. */
     handle->stdin_buf = buf_new();
+    handle->stderr_buf = buf_new();
     handle->stdin_ev = event_new(tor_libevent_get_base(),
         handle->process->stdin_pipe, EV_WRITE, pirprocess_stdincb,
         handle);