1*6a54128fSAndroid Build Coastguard Worker#!/bin/bash 2*6a54128fSAndroid Build Coastguard Worker 3*6a54128fSAndroid Build Coastguard Worker# Copyright (C) 2018 Oracle. All Rights Reserved. 4*6a54128fSAndroid Build Coastguard Worker# 5*6a54128fSAndroid Build Coastguard Worker# Author: Darrick J. Wong <[email protected]> 6*6a54128fSAndroid Build Coastguard Worker# 7*6a54128fSAndroid Build Coastguard Worker# This program is free software; you can redistribute it and/or 8*6a54128fSAndroid Build Coastguard Worker# modify it under the terms of the GNU General Public License 9*6a54128fSAndroid Build Coastguard Worker# as published by the Free Software Foundation; either version 2 10*6a54128fSAndroid Build Coastguard Worker# of the License, or (at your option) any later version. 11*6a54128fSAndroid Build Coastguard Worker# 12*6a54128fSAndroid Build Coastguard Worker# This program is distributed in the hope that it would be useful, 13*6a54128fSAndroid Build Coastguard Worker# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6a54128fSAndroid Build Coastguard Worker# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6a54128fSAndroid Build Coastguard Worker# GNU General Public License for more details. 16*6a54128fSAndroid Build Coastguard Worker# 17*6a54128fSAndroid Build Coastguard Worker# You should have received a copy of the GNU General Public License 18*6a54128fSAndroid Build Coastguard Worker# along with this program; if not, write the Free Software Foundation, 19*6a54128fSAndroid Build Coastguard Worker# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 20*6a54128fSAndroid Build Coastguard Worker 21*6a54128fSAndroid Build Coastguard Worker# Run e2scrub_all from a cronjob if we don't have systemd and we're not 22*6a54128fSAndroid Build Coastguard Worker# running on AC power. 23*6a54128fSAndroid Build Coastguard Worker 24*6a54128fSAndroid Build Coastguard Workeron_ac_power() { 25*6a54128fSAndroid Build Coastguard Worker local any_known=no 26*6a54128fSAndroid Build Coastguard Worker 27*6a54128fSAndroid Build Coastguard Worker # try sysfs power class first 28*6a54128fSAndroid Build Coastguard Worker if [ -d /sys/class/power_supply ]; then 29*6a54128fSAndroid Build Coastguard Worker for psu in /sys/class/power_supply/*; do 30*6a54128fSAndroid Build Coastguard Worker if [ -r "$psu/type" ]; then 31*6a54128fSAndroid Build Coastguard Worker type=$(cat "$psu/type") 32*6a54128fSAndroid Build Coastguard Worker 33*6a54128fSAndroid Build Coastguard Worker # ignore batteries 34*6a54128fSAndroid Build Coastguard Worker [ "$type" = "Battery" ] && continue 35*6a54128fSAndroid Build Coastguard Worker 36*6a54128fSAndroid Build Coastguard Worker online=$(cat "$psu/online") 37*6a54128fSAndroid Build Coastguard Worker 38*6a54128fSAndroid Build Coastguard Worker [ "$online" = 1 ] && return 0 39*6a54128fSAndroid Build Coastguard Worker [ "$online" = 0 ] && any_known=yes 40*6a54128fSAndroid Build Coastguard Worker fi 41*6a54128fSAndroid Build Coastguard Worker done 42*6a54128fSAndroid Build Coastguard Worker 43*6a54128fSAndroid Build Coastguard Worker [ "$any_known" = "yes" ] && return 1 44*6a54128fSAndroid Build Coastguard Worker fi 45*6a54128fSAndroid Build Coastguard Worker 46*6a54128fSAndroid Build Coastguard Worker # else fall back to AC adapters in /proc 47*6a54128fSAndroid Build Coastguard Worker if [ -d /proc/acpi/ac_adapter ]; then 48*6a54128fSAndroid Build Coastguard Worker for ac in /proc/acpi/ac_adapter/*; do 49*6a54128fSAndroid Build Coastguard Worker if [ -r "$ac/state" ]; then 50*6a54128fSAndroid Build Coastguard Worker grep -q on-line "$ac/state" && return 0 51*6a54128fSAndroid Build Coastguard Worker grep -q off-line "$ac/state" && any_known=yes 52*6a54128fSAndroid Build Coastguard Worker elif [ -r "$ac/status" ]; then 53*6a54128fSAndroid Build Coastguard Worker grep -q on-line "$ac/status" && return 0 54*6a54128fSAndroid Build Coastguard Worker grep -q off-line "$ac/status" && any_known=yes 55*6a54128fSAndroid Build Coastguard Worker fi 56*6a54128fSAndroid Build Coastguard Worker done 57*6a54128fSAndroid Build Coastguard Worker 58*6a54128fSAndroid Build Coastguard Worker [ "$any_known" = "yes" ] && return 1 59*6a54128fSAndroid Build Coastguard Worker fi 60*6a54128fSAndroid Build Coastguard Worker 61*6a54128fSAndroid Build Coastguard Worker # Can't tell, just assume we're on AC. 62*6a54128fSAndroid Build Coastguard Worker return 0 63*6a54128fSAndroid Build Coastguard Worker} 64*6a54128fSAndroid Build Coastguard Worker 65*6a54128fSAndroid Build Coastguard Workertest -e @root_sbindir@/e2scrub_all || exit 0 66*6a54128fSAndroid Build Coastguard Workertest -e /run/systemd/system && exit 0 67*6a54128fSAndroid Build Coastguard Workeron_ac_power || exit 0 68*6a54128fSAndroid Build Coastguard Worker 69*6a54128fSAndroid Build Coastguard Workerexec @root_sbindir@/e2scrub_all 70