Read android dmesg with code

How can I read dmesg output in my program?

Thank..

+3
source share
2 answers

You can write Android NDK code that calls the klogctl functions . Sort of:

#include <sys/klog.h>

#define KLOG_READ_ALL   3
#define KLOG_LEN    (1 << 17)

char buf[KLOG_LEN];

if (klogctl(KLOG_READ_ALL, buf, KLOG_LEN) < 0)
{
  printf("Error %s reading dmesg\n", strerror(errno));
}
else
{
  /* do something with contents of buf */
}

However, in Android 4.1 Jelly Bean, they have implemented a security feature that prohibits access to dmesg messages. The above code will end with an error "Operation not allowed." If you have root access to the device, you can disable dmesg_restrict:

echo 0 > /proc/sys/kernel/dmesg_restrict

In addition, some recent devices have SELinux, in which case you will need to

setenforce 0

If you don't have root access, you're still out of luck.

+7
source

Runtime.getruntime.exec

0
source

Source: https://habr.com/ru/post/1763255/


All Articles