I want to write to /dev/kmsg so I can compare user space logging in an application to things that are happening in the kernel.
I wrote a simple application that should log a message similar to how you can do this from the command line with echo "foo" > /dev/kmsg
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int fdKmsg = -1;
fdKmsg = open("/dev/kmsg", O_WRONLY);
if (fdKmsg != -1)
{
dprintf(fdKmsg, "-- Hello World --\n");
close(fd);
}
else
{
printf("Unable to get file descriptor\n");
}
return 0;
}
However, when I run this even as root, I don't see anything either in dmesg or in /proc/kmsg. What am I missing? Even with a call to fsync(), it doesn't appear to write to dmesg.
It appears that dprintf() will not work in this case. However, calling write() will work. I'm not sure why this is, but I would guess that the printf() functions don't call the write method for the exposed /dev interface.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main()
{
int fdKmsg = -1;
fdKmsg = open("/dev/kmsg", O_WRONLY);
char *buf = "Hello World\n";
if (fdKmsg != -1)
{
//dprintf(fdKmsg, "-- Hello World --\n");
write(fdKmsg, buf, strlen(buf) + 1);
close(fd);
}
else
{
printf("Unable to get file descriptor\n");
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With