[pmqa] add method to check the userid for android

Message ID 1390567855-20601-1-git-send-email-sanjay.rawat@linaro.org
State New
Headers show

Commit Message

Sanjay Singh Rawat Jan. 24, 2014, 12:50 p.m.
id command option doesn't return same output, as it does for
ubuntu. Add function with will check the system type and return
the userid.

Signed-off-by: Sanjay Singh Rawat <sanjay.rawat@linaro.org>
---

On android the script shows error for "id" commadn return value. Added
function to get the userid based on system type (ubuntu/android).

- calling function in below manner doesn't return value to the _if_ case

if [ $(is_root) -eq 0 ]; then

so i am using

is_root
if [ $? -eq 0 ]

- saving return value of function is not working on android. e.g
local ret=$(is_root)

Belows changes work find both.

thanks,
sanjay
---
 cpufreq/cpufreq_03.sh |   11 ++++++-----
 cpufreq/cpufreq_04.sh |   11 ++++++-----
 cpufreq/cpufreq_05.sh |    5 +++--
 cpufreq/cpufreq_06.sh |    5 +++--
 cpufreq/cpufreq_07.sh |    5 +++--
 cpufreq/cpufreq_08.sh |    5 +++--
 cpufreq/cpufreq_09.sh |    5 +++--
 cpuidle/cpuidle_02.sh |    5 +++--
 cpuidle/cpuidle_03.sh |    8 +++++---
 cpuidle/cpuidle_04.sh |    5 +++--
 include/functions.sh  |   25 +++++++++++++++++++++++++
 11 files changed, 63 insertions(+), 27 deletions(-)

Patch hide | download patch | download mbox

diff --git a/cpufreq/cpufreq_03.sh b/cpufreq/cpufreq_03.sh
index 85118d7..2bf2b3d 100755
--- a/cpufreq/cpufreq_03.sh
+++ b/cpufreq/cpufreq_03.sh
@@ -27,6 +27,12 @@ 
 
 source ../include/functions.sh
 
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
+    exit 0
+fi
+
 cpufreq_enabled
 if [ $? -ne 0 ]; then
 	log_skip "cpufreq not supported"
@@ -49,10 +55,5 @@  check_governor() {
     set_governor $cpu $oldgov
 }
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
-    exit 0
-fi
-
 for_each_cpu for_each_governor check_governor || exit 1
 test_status_show
diff --git a/cpufreq/cpufreq_04.sh b/cpufreq/cpufreq_04.sh
index 4a07315..3136b5c 100755
--- a/cpufreq/cpufreq_04.sh
+++ b/cpufreq/cpufreq_04.sh
@@ -33,6 +33,12 @@  if [ $? -ne 0 ]; then
 	exit 0
 fi
 
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
+    exit 0
+fi
+
 check_frequency() {
 
     local cpu=$1
@@ -52,11 +58,6 @@  check_frequency() {
     set_governor $cpu $oldgov
 }
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
-    exit 0
-fi
-
 supported=$(cat $CPU_PATH/cpu0/cpufreq/scaling_available_governors | grep "userspace")
 if [ -z "$supported" ]; then
     log_skip "userspace not supported"
diff --git a/cpufreq/cpufreq_05.sh b/cpufreq/cpufreq_05.sh
index 3c76bc1..9a4e575 100755
--- a/cpufreq/cpufreq_05.sh
+++ b/cpufreq/cpufreq_05.sh
@@ -33,8 +33,9 @@  if [ $? -ne 0 ]; then
 	exit 0
 fi
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpufreq/cpufreq_06.sh b/cpufreq/cpufreq_06.sh
index 157b1ef..ea2eb66 100755
--- a/cpufreq/cpufreq_06.sh
+++ b/cpufreq/cpufreq_06.sh
@@ -107,8 +107,9 @@  check_deviation() {
     for_each_frequency $cpu check_freq_deviation
 }
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpufreq/cpufreq_07.sh b/cpufreq/cpufreq_07.sh
index 10fe909..12e1692 100755
--- a/cpufreq/cpufreq_07.sh
+++ b/cpufreq/cpufreq_07.sh
@@ -83,8 +83,9 @@  check_ondemand() {
     return 1
 }
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpufreq/cpufreq_08.sh b/cpufreq/cpufreq_08.sh
index 8ab33b7..636777c 100755
--- a/cpufreq/cpufreq_08.sh
+++ b/cpufreq/cpufreq_08.sh
@@ -72,8 +72,9 @@  check_userspace() {
 
 save_governors
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpufreq/cpufreq_09.sh b/cpufreq/cpufreq_09.sh
index 832c303..8e89e10 100755
--- a/cpufreq/cpufreq_09.sh
+++ b/cpufreq/cpufreq_09.sh
@@ -68,8 +68,9 @@  check_powersave() {
 
 save_governors
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpuidle/cpuidle_02.sh b/cpuidle/cpuidle_02.sh
index e604a8b..77261c4 100755
--- a/cpuidle/cpuidle_02.sh
+++ b/cpuidle/cpuidle_02.sh
@@ -29,8 +29,9 @@  source ../include/functions.sh
 
 CPUIDLE_KILLER=./cpuidle_killer
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/cpuidle/cpuidle_03.sh b/cpuidle/cpuidle_03.sh
index b07bc89..645a94d 100755
--- a/cpuidle/cpuidle_03.sh
+++ b/cpuidle/cpuidle_03.sh
@@ -29,8 +29,9 @@  source ../include/functions.sh
 
 CPUIDLE_KILLER=./cpuidle_killer
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
@@ -49,7 +50,8 @@  check_cpuidle_kill() {
     check "cpuidle program runs successfully (120 secs)" "./$CPUIDLE_KILLER"
 }
 
-if [ $(id -u) -ne 0 ]; then
+is_root
+if [ $? -ne 0 ]; then
     log_skip "run as non-root"
     exit 0
 fi
diff --git a/cpuidle/cpuidle_04.sh b/cpuidle/cpuidle_04.sh
index 1688873..028b3ee 100644
--- a/cpuidle/cpuidle_04.sh
+++ b/cpuidle/cpuidle_04.sh
@@ -27,8 +27,9 @@  source ../include/functions.sh
 
 CPUIDLE_STATS=./cpuidle_stats
 
-if [ $(id -u) -ne 0 ]; then
-    log_skip "run as non-root"
+is_root
+if [ $? -ne 0 ]; then
+    log_skip "user is not root"
     exit 0
 fi
 
diff --git a/include/functions.sh b/include/functions.sh
index 2124ceb..728f186 100644
--- a/include/functions.sh
+++ b/include/functions.sh
@@ -348,3 +348,28 @@  sigtrap() {
 cpufreq_enabled() {
     test -d /sys/devices/system/cpu/cpufreq
 }
+
+# currently we support ubuntu and android
+get_os() {
+    lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
+    if [ $? -eq 0 ]; then
+        # for ubuntu
+        return 1
+    else
+        # for android (if needed can look for build.prop)
+        return 2
+    fi
+}
+
+get_uid() {
+    local ret
+    get_os
+    if [ $? -eq 1 ]; then
+        # for ubuntu
+        ret=$(id -u)
+    else
+        # for android
+        ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
+    fi
+    return $ret
+}