linux-kernelsystemd-journald

sd_journal_sendv writing BLOB instead of string


Can please some one explain me how to use sd_journal_sendv system call? I tried to use with below code snippet but input strings are interpreted as BLOB by journald.

#include <iostream>
#include <iostream>
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <systemd/sd-journal.h>
using namespace std;
int main() {
    char buf0[50];
    char buf1[50];
    char buf2[50];
    int iovcnt;
    struct iovec iov[3];
    strcpy(buf0,"MESSAGE=Hello World!");
    strcpy(buf1,"MESSAGE_ID=52fb62f99e2c4");
    strcpy(buf2,"PRIORITY=5");
    iov[0].iov_base = buf0;
    iov[0].iov_len = sizeof(buf0);
    iov[1].iov_base = buf1;
    iov[1].iov_len = sizeof(buf1);
    iov[2].iov_base = buf2;
    iov[2].iov_len = sizeof(buf2);
    iovcnt = sizeof(iov) / sizeof(struct iovec);
    int ret = sd_journal_sendv (iov, iovcnt);
    if ret(!=0)
        cout<<"sendv "<<strerror(ret)<<endl;
    return 0;
}

I have written "Hello world!", 52fb62f99e2c4, 5 for MESSAGE, MESSAGE_ID and PRIORITY respectively. but in journal I can see they are written as BLOB.

MESSAGE=[92B blob data]
MESSAGE_ID=[89B blob data]
PRIORITY=[91B blob data]

Solution

  • You aren't passing the sizes of your fields in .iov_len. This causes the 50-byte bufs to contain garbage at the end. journalctl shows those as blob data because it is invalid UTF-8.