blob: 6e5810e6dbd092f4f9ed0e789462b6a103051f1d [file] [log] [blame]
David 'Digit' Turner36597752011-05-20 01:18:01 +02001#include <unistd.h>
thomas knych3a176972014-04-09 19:42:36 -07002#include <stdlib.h>
David 'Digit' Turner36597752011-05-20 01:18:01 +02003#include <string.h>
4#include <sys/utsname.h>
thomas knych3a176972014-04-09 19:42:36 -07005#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +02006#include "android/utils/debug.h"
7
8#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)
9
10/* A simple routine used to check that we can run the program under KVM.
David 'Digit' Turner80ab3272011-05-30 21:40:26 +020011 * We simply want to ensure that the kvm driver is loaded and that the
12 * corresponding device file is accessible by the user.
David 'Digit' Turner36597752011-05-20 01:18:01 +020013 */
14
15#ifndef __linux__
16#error "This file should only be compiled under linux"
17#endif
18
19int
20kvm_check_allowed(void)
21{
thomas knych3a176972014-04-09 19:42:36 -070022
23 char* kvm_device = getenv(KVM_DEVICE_NAME_ENV);
24 if (NULL == kvm_device) {
25 kvm_device = "/dev/kvm";
26 }
David 'Digit' Turner36597752011-05-20 01:18:01 +020027 /* Is there a /dev/kvm device file here? */
thomas knych3a176972014-04-09 19:42:36 -070028 if (access(kvm_device,F_OK)) {
David 'Digit' Turner36597752011-05-20 01:18:01 +020029 /* no need to print a warning here */
30 D("No kvm device file detected");
31 return 0;
32 }
33
34 /* Can we access it? */
thomas knych3a176972014-04-09 19:42:36 -070035 if (access(kvm_device,R_OK)) {
David 'Digit' Turner36597752011-05-20 01:18:01 +020036 D("KVM device file is not readable for this user.");
37 return 0;
38 }
39
David 'Digit' Turner36597752011-05-20 01:18:01 +020040 D("KVM mode auto-enabled!");
41 return 1;
42}
43