• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Check that sysfs wakeup nodes are correctly labeled by the
18# device's sepolicy.
19
20wakeup_attr="u:object_r:sysfs_wakeup:s0"
21wakeup_paths=()
22unlabeled_paths=()
23
24get_wakeup_paths() {
25    adb shell 'paths=()
26        wakeup_dir=/sys/class/wakeup
27        cd $wakeup_dir
28        for file in $wakeup_dir/*; do
29            paths+=( $(realpath $file) )
30        done
31        echo "${paths[@]}"'
32}
33
34has_wakeup_attr() { #path
35    adb shell ls -dZ "$1" | grep -q "$wakeup_attr"
36    return $?
37}
38
39check_wakeup_dup() { # wakeup_path
40    ret=1
41    for path in "${unlabeled_paths[@]}"; do
42        if [ "$path" == "$1" ]; then
43            ret=0
44            break
45        fi
46    done
47    return $ret
48}
49
50get_unlabeled_wakeup_paths() {
51    for path in ${wakeup_paths[@]}; do
52        # If there exists a common wakeup parent directory, label that instead
53        # of each wakeupN directory
54        wakeup_path="$path"
55        dir_path="$(dirname $path)"
56        [ "$(basename $dir_path)" == "wakeup" ] && wakeup_path="$dir_path"
57        has_wakeup_attr "$wakeup_path" || check_wakeup_dup $wakeup_path \
58            || unlabeled_paths+=( $wakeup_path )
59    done
60}
61
62print_missing_labels() {
63    nr_unlabeled="${#unlabeled_paths[@]}"
64
65    [ "$nr_unlabeled" == "0" ] && return 1
66
67    echo ""
68    echo "Unlabeled wakeup nodes found, your device is likely missing"
69    echo "device/oem specific selinux genfscon rules for suspend."
70    echo ""
71    echo "Please review and add the following generated rules to the"
72    echo "device specific genfs_contexts:"
73    echo ""
74
75    for path in ${unlabeled_paths[@]}; do
76        echo "genfscon sysfs ${path#/sys/} $wakeup_attr"
77    done
78
79    echo ""
80
81    return 0
82}
83
84fail() { #msg
85    echo $1
86    exit 1
87}
88
89pass() { #msg
90    echo $1
91    exit 0
92}
93
94# requirement added in T (33)
95vendor_api_level="$(adb shell getprop ro.vendor.api_level 33)"
96if [ "$vendor_api_level" -lt 33 ]; then
97    pass "Test skipped: vendor_api_level ($vendor_api_level) < min_api_level (33)"
98fi
99
100# Test unlabeled sysfs_wakeup nodes
101wakeup_paths+=( $(get_wakeup_paths) )
102get_unlabeled_wakeup_paths
103print_missing_labels && fail "Missing sysfs_wakeup labels" \
104    || pass "No missing sysfs_wakeup labels"
105