Repository navigation
Expand file tree
/
Copy pathpatch.d-env
More file actions
executable file
·174 lines (151 loc) · 4.53 KB
/
Copy pathpatch.d-env
File metadata and controls
executable file
·174 lines (151 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
tmp=/tmp/kernel-flasher
console=$(cat /tmp/console)
[ "$console" ] || console=/proc/$$/fd/1
cd "$tmp"
. config.sh
cmdline=$split_img/boot.img-cmdline
default_prop=$ramdisk/default.prop
build_prop=/system/build.prop
ueventd=$ramdisk/ueventd.rc
file_contexts=$ramdisk/file_contexts
found_prop=false
[ -f "$default_prop" ] && found_prop=true
found_build_prop=false
[ -f "$build_prop" ] && found_build_prop=true
found_ueventd=false
[ -f "$ueventd" ] && found_ueventd=true
found_file_contexts=false
[ -f "$file_contexts" ] && found_file_contexts=true
print() {
if [ "$1" ]; then
echo "ui_print -- $1" > "$console"
else
echo "ui_print " > "$console"
fi
echo
}
abort() {
[ "$1" ] && print "Error: $1"
exit 1
}
# setperm <directory permissions> <file permissions> <directory>
# recursively sets permissions of files & directories
setperm() {
find "$3" -type d -exec chmod "$1" {} \;
find "$3" -type f -exec chmod "$2" {} \;
}
# replace_file <old file> <new file> (preserving metadata)
# replace a file, preserving metadata (using cat)
replace_file() {
cat "$2" > "$1" || return
rm -f "$2"
}
# replace_line <file> <line match pattern> <replacement line>
# replace a matching line in a file with another line
replace_line() {
sed -i "s/[[:space:]]*$2[[:space:]]*$/$3/" "$1"
}
# insert_after_last <file> <line match pattern> <inserted line>
# insert a specified line after the last matching line
insert_after_last() {
grep -q "^$3$" "$1" || {
line=$(($(grep -n "^[[:space:]]*$2[[:space:]]*$" "$1" | tail -1 | cut -d: -f1) + 1))
sed -i "${line}i$3" "$1"
}
}
# setcmdline <key> <value>
# set a key's value on the boot image's initial command line
setcmdline() {
[ -f "$cmdline" ] || touch "$cmdline"
grep -q "\b$1=" "$cmdline" && sed -i "s|\b$1=.*\b|$1=$2|g" "$cmdline" && return
sed -i "1 s/$/ $1=$2/" "$cmdline"
}
# setprop <prop> <value>
# set a prop value in default.prop
setprop() {
$found_prop || return
if grep -q "^[[:space:]]*$1[[:space:]]*=" "$default_prop"; then
sed -i "s/^[[:space:]]*$1[[:space:]]*=.*$/$1=$2/g" "$default_prop"
else
echo "$1=$2" >> "$default_prop"
fi
}
# delprop <prop>
# delete a prop from both default.prop and build.prop
delprop() {
$found_prop && sed -i "/^[[:space:]]*$1[[:space:]]*=/d" "$default_prop"
$found_build_prop && sed -i "/^[[:space:]]*$1[[:space:]]*=/d" "$build_prop"
}
# disable_service <service name>
# this only sets a service to disabled, it won't prevent it from being started manually
disable_service() {
for rc in "$ramdisk"/*.rc; do
grep -q "^[[:space:]]*service[[:space:]]\+$1\b" "$rc" || continue
echo "Found service $1 in $rc"
awk -vsc_name="$1" '
$1 == "service" || $1 == "on" { in_sc = 0 }
in_sc && $1 == "disabled" { next }
{ print }
$1 == "service" && $2 == sc_name {
print " disabled"
in_sc = 1
}
' "$rc" > "$rc-"
replace_file "$rc" "$rc-"
done
}
# remove_service <service name>
# this comments out a service entry entirely, as well as commands referencing it
remove_service() {
for rc in "$ramdisk"/*.rc; do
grep -q "^[[:space:]]*\(service\|start\|stop\|restart\)[[:space:]]\+$1\b" "$rc" || continue
echo "Found service $1 in $rc"
awk -vsc_name="$1" '
!NF || $1 ~ /^#/ { print; next }
$1 == "service" || $1 == "on" { in_sc = 0 }
$1 == "service" && $2 == sc_name { in_sc = 1 }
in_sc || ($2 == sc_name && ($1 == "start" || $1 == "stop" || $1 == "restart")) { printf "#" }
{ print }
' "$rc" > "$rc-"
replace_file "$rc" "$rc-"
done
}
# ueventd_set <device node> <permissions> <chown> <chgrp>
# use this to set permissions of /dev nodes
ueventd_set() {
$found_ueventd || return
awk -vdev="$1" -vperm="$2" -vuser="$3" -vgroup="$4" '
function pdev() {
printf "%-25s %-6s %-10s %s\n", dev, perm, user, group
set = 1
}
$1 == dev && !set { pdev() }
$1 == dev { next }
{ print }
END { if (!set) pdev() }
' "$ueventd" > "$ueventd-"
replace_file "$ueventd" "$ueventd-"
}
# context_set <file path regex> <context>
# use this to set selinux contexts of file paths
context_set() {
$found_file_contexts || return
awk -vfile="$1" -vcontext="$2" '
function pfcon() {
printf "%-48s %s\n", file, context
set = 1
}
$1 == file && !set { pfcon() }
$1 == file { next }
{ print }
END { if (!set) pfcon() }
' "$file_contexts" > "$file_contexts-"
replace_file "$file_contexts" "$file_contexts-"
}
# import_rc <rc file>
# adds an init rc file as an import to init.rc, it will be imported last
import_rc() {
insert_after_last "$ramdisk/init.rc" "import .*\.rc" "import /$1"
}
cd "$ramdisk" || abort "Unable to enter ramdisk directory!"