Skip to content

Commit

Permalink
Merge pull request #106 from haircommander/write_dev_null
Browse files Browse the repository at this point in the history
journal logging: write to /dev/null instead of -1
  • Loading branch information
vrothberg authored Jan 7, 2020
2 parents b7bfc7b + 81f77be commit 6e39a83
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/ctr_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ bool write_to_logs(stdpipe_t pipe, char *buf, ssize_t num_read)
*/
int write_journald(int pipe, char *buf, ssize_t buflen)
{
/* When using writev_buffer_append_segment, we should never approach the number of
* entries necessary to flush the buffer. Therefore, the fd passed in is for /dev/null
*/
_cleanup_close_ int dev_null = open("/dev/null", O_WRONLY | O_CLOEXEC);
if (dev_null < 0)
pexit("Failed to open /dev/null");

/* Since we know the priority values for the journal (6 being log info and 3 being log err
* we can set it statically here. This will also save on runtime, at the expense of needing
* to be changed if this convention is changed.
Expand All @@ -188,35 +195,32 @@ int write_journald(int pipe, char *buf, ssize_t buflen)
char tmp_line_end = buf[line_len];
buf[line_len] = '\0';

/* When using writev_buffer_append_segment here, we should never approach the number of
* entries necessary to flush the buffer. Therefore, the fd passed in is -1.
*/
_cleanup_free_ char *message = g_strdup_printf("MESSAGE=%s", buf);
if (writev_buffer_append_segment(-1, &bufv, message, line_len + MESSAGE_EQ_LEN) < 0)
if (writev_buffer_append_segment(dev_null, &bufv, message, line_len + MESSAGE_EQ_LEN) < 0)
return -1;

/* Restore state of the buffer */
buf[line_len] = tmp_line_end;


if (writev_buffer_append_segment(-1, &bufv, container_id_full, cuuid_len + CID_FULL_EQ_LEN) < 0)
if (writev_buffer_append_segment(dev_null, &bufv, container_id_full, cuuid_len + CID_FULL_EQ_LEN) < 0)
return -1;

if (writev_buffer_append_segment(-1, &bufv, message_priority, PRIORITY_EQ_LEN) < 0)
if (writev_buffer_append_segment(dev_null, &bufv, message_priority, PRIORITY_EQ_LEN) < 0)
return -1;

if (writev_buffer_append_segment(-1, &bufv, container_id, TRUNC_ID_LEN + CID_EQ_LEN) < 0)
if (writev_buffer_append_segment(dev_null, &bufv, container_id, TRUNC_ID_LEN + CID_EQ_LEN) < 0)
return -1;

if (container_tag && writev_buffer_append_segment(-1, &bufv, container_tag, container_tag_len) < 0)
if (container_tag && writev_buffer_append_segment(dev_null, &bufv, container_tag, container_tag_len) < 0)
return -1;

/* only print the name if we have a name to print */
if (name && writev_buffer_append_segment(-1, &bufv, container_name, name_len + CID_FULL_EQ_LEN) < 0)
if (name && writev_buffer_append_segment(dev_null, &bufv, container_name, name_len + CID_FULL_EQ_LEN) < 0)
return -1;

/* per docker journald logging format, CONTAINER_PARTIAL_MESSAGE is set to true if it's partial, but otherwise not set. */
if (partial && writev_buffer_append_segment(-1, &bufv, "CONTAINER_PARTIAL_MESSAGE=true", PARTIAL_MESSAGE_EQ_LEN) < 0)
if (partial && writev_buffer_append_segment(dev_null, &bufv, "CONTAINER_PARTIAL_MESSAGE=true", PARTIAL_MESSAGE_EQ_LEN) < 0)
return -1;

int err = sd_journal_sendv(bufv.iov, bufv.iovcnt);
Expand Down

0 comments on commit 6e39a83

Please sign in to comment.