Ran
Ran

Reputation: 31

read android dmesg with code

How can I read dmesg output in my program?

Thanks..

Upvotes: 3

Views: 3396

Answers (2)

ndyer
ndyer

Reputation: 973

You can write some Android NDK code which calls the klogctl functions. Something like:

#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 which disallows access to the dmesg messages. The code above will fail with an "Operation not permitted" error. If you have root access to the device, you can turn off dmesg_restrict:

echo 0 > /proc/sys/kernel/dmesg_restrict

Also, some recent devices have SELinux enabled, in which case you will need to do

setenforce 0

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

Upvotes: 7

Ran
Ran

Reputation: 31

Runtime.getruntime.exec

Upvotes: 0

Related Questions