David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
thomas knych | 3a17697 | 2014-04-09 19:42:36 -0700 | [diff] [blame] | 2 | #include <stdlib.h> |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <sys/utsname.h> |
thomas knych | 3a17697 | 2014-04-09 19:42:36 -0700 | [diff] [blame] | 5 | #include "android/kvm.h" |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 6 | #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' Turner | 80ab327 | 2011-05-30 21:40:26 +0200 | [diff] [blame] | 11 | * 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' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef __linux__ |
| 16 | #error "This file should only be compiled under linux" |
| 17 | #endif |
| 18 | |
| 19 | int |
| 20 | kvm_check_allowed(void) |
| 21 | { |
thomas knych | 3a17697 | 2014-04-09 19:42:36 -0700 | [diff] [blame] | 22 | |
| 23 | char* kvm_device = getenv(KVM_DEVICE_NAME_ENV); |
| 24 | if (NULL == kvm_device) { |
| 25 | kvm_device = "/dev/kvm"; |
| 26 | } |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 27 | /* Is there a /dev/kvm device file here? */ |
thomas knych | 3a17697 | 2014-04-09 19:42:36 -0700 | [diff] [blame] | 28 | if (access(kvm_device,F_OK)) { |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 29 | /* 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 knych | 3a17697 | 2014-04-09 19:42:36 -0700 | [diff] [blame] | 35 | if (access(kvm_device,R_OK)) { |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 36 | D("KVM device file is not readable for this user."); |
| 37 | return 0; |
| 38 | } |
| 39 | |
David 'Digit' Turner | 3659775 | 2011-05-20 01:18:01 +0200 | [diff] [blame] | 40 | D("KVM mode auto-enabled!"); |
| 41 | return 1; |
| 42 | } |
| 43 | |