-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatch_remover.sh
executable file
·373 lines (323 loc) · 11.1 KB
/
patch_remover.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/sh
# bash or ash is required but sh for universal compatibility.
################################################################################
#
# Copyright (C) 2023, Roberto A. Foglietta <roberto.foglietta@gmail.com>
# Released under GPLv2 license terms
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
################################################################################
#
# TODO: create a back-up of the files before patching, date-time-Nprogressive
#
# TODO: https://raw.githubusercontent.com/brgl/busybox/master/shell/cttyhack.c
#
# release: 0.1.5
set -emu
errsig="ERR"; trap true ERR 2>/dev/null || errsig=""
trap 'echo -e "\nError occurred ($?) on $LINENO\n" >&2' $errsig EXIT
src_file_env "rfos-script-functions"
src_file_env "patch_dblock_functions"
export PATH=$HOME/bin:$PATH
# FUNTIONS DEFINITIONS #########################################################
patch_broken_warning() {
echo
echo "WARNING: patch #$n failed to be $past_action to rootfs, skipped"
echo " patch: $patch_name $vern"
echo " fix pre-requisites and then try to install again."
}
patch_string_to_filename() {
test -n "${1:-}" || return 1
# cast the patch data into useful variables
strn=$(echo $1 | cut -d\; -f1)
prov=$(echo $strn | cut -d\, -f1 | tr -d ' ' ||:)
name=$(echo $strn | cut -d\, -f2 | tr -d ' ' ||:)
vern=$(echo $strn | cut -d\, -f3 | tr -d ' ' ||:)
extn=$(echo $strn | cut -d\, -f4 | tr -d ' ' ||:)
srvs=$(echo $strn | cut -d\, -f5- | grep -vw none ||:)
srvs=$(echo $srvs | cut -d\# -f1 ||:)
patch_file="$prov-$name-$vern.patch"
patch_path="$patch_dir/$patch_file"
bckup_path=${patch_path%.patch}
return 0
}
exit_for_manual_intervention() {
trap - SIGINT EXIT ||:
stty echoctl 2>/dev/null ||:
servs_list=$(cat "$reload_path" 2>/dev/null ||:)
errexit "patch #$n cannot be $past_action automatically.
\tManual intervetion is needed, these are the working values:
patch path: $(dirname ${patch_path:-none})
patch name: $patch_name
patch opts: $patch_opts
patch file: $(basename ${patch_path:-none})
patch revr: $(basename ${patch_reversible:-none})
patch prev: $(basename ${patch_prev_path:-none})
srv2reload: $(echo ${servs_list:-none})"
}
reversible_check() {
echo " \_ Checking for reversibility..."
if do_patch -R --dry-run -i "$1"; then
reversible="OK"
patch_reversible=$1
else
reversible="KO"
patch_reversible=""
fi >/dev/null
echo " \_ Reversibility : $reversible"
test "$reversible" = "OK"
}
applicable_check() {
echo " \_ Checking for application... "
if do_patch --dry-run -i "$1"; then
applicable="OK"
patch_applicable=$1
else
applicable="KO"
patch_applicable=""
fi >/dev/null
echo " \_ Applicability : $applicable"
test "$applicable" = "OK"
}
read_patch_string() {
mkdb_lock || return 1
touch "$patch_db"
grep ", *$patch_name *," "$patch_db"
rmdb_lock ||:
return 0 # RAF: we do not care about finding or not, we check it later
}
output_filter() {
local output="" ret=0
if output=$(eval "$1" 2>&1)
then :; else ret=$?; fi
echo "$output" | eval "$2" ||:
return $ret
}
do_patch() {
output_filter "patch $patch_opts $*" "$filter_2"
}
# VARIABLES DEFINITIONS ########################################################
patch_db="/etc/patches.db"
patch_dir="/etc/patches.d"
patch_lst="/etc/patches.list"
patch_opts="-slEfp1 -r /dev/null --no-backup-if-mismatch -d/"
reload_path="$patch_dir/services-to-reload.list"
lockfile="${patch_db}.lck"
filter_1="grep . | sed -e 's,^,\ \ \ ,'"
filter_2="grep . | sed -e 's,^,\ \ \|\ \ ,'"
filter_3="grep -Ev 'Status:|Percentage:|Results:'"
filter_3="$filter_3 | $filter_2"
filter_4="sed -e 's/^+ //' | $filter_2"
filter_5="tr '\n' '^' | cut -d'^' -f1,3 | tr '^' '\n'"
filter_5="$filter_5 | $filter_1"
patches_to_apply=""
test "x${1:-}" == "x--all" \
&& patches_to_apply="
sshd-publickey-login-only
utilities-quick-fp-restart
x10ii-iii-agps-config-emea
x10ii-iii-udev-rules-fixing
dnsmasq-connman-integration
"
# SHELL TEST ###################################################################
echo
check_running_shell "$(basename $0)" "dash"
# PARAMETERS CHECK #############################################################
plst="ERROR"
if [ ! -n "$patches_to_apply" ]; then
if [ -n "${1:-}" ]; then
patches_to_apply="$@"
plst="args"
else
patches_to_apply=$(cat $patch_lst)
plst="file"
fi
else
plst="--all"
fi
if [ ! -n "$patches_to_apply" ]; then
errexit "no patches to apply found, abort."
fi
echo "=> Using the patch list from '$plst':"
echo " \_ List of patches to apply:"
echo
echo "$patches_to_apply" | eval "$filter_1"
servs_list=$(cat "$reload_path" 2>/dev/null ||:)
if [ -n "$servs_list" ]; then
echo
echo "WARNING: system services to restart found from a previous run"
echo " collected and put in the current list of restarting."
fi
verb_action="reverse"
past_action="reversed"
# MAIN LOOP EXECUTION ##########################################################
# this loop install all the patches in the ordered list ========================
n=0; mkdir -p "$patch_dir/"; for patch_name in $patches_to_apply; do n=$((n+1))
# this loop is a fake that allows to use break instead of if/else/fi construct ~
skip=0; while true; do # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # fake loop #
echo
echo "=> Current patch #$n check: $patch_name"
patch_prev_path=""
patch_prev_strn=$(read_patch_string)
if patch_string_to_filename "$patch_prev_strn"; then
echo " \_ Current patch: v$vern found"
echo " | $patch_prev_strn"
if reversible_check "$patch_path"; then
patch_prev_path=$patch_reversible
elif applicable_check "$patch_path"; then
echo " \_ Current patch: not applied"
skip=1; break
else
echo " \_ Current patch: broken"
fi
else
echo " \_ Current patch: none"
fi
verstr="current"
if [ -n "${patch_prev_path:-}" ]; then
echo " \_ Patch to $verb_action: $verstr $vern version"
break
fi
verstr="last"
echo
echo "=> Downloading the patch #$n last version..."
echo " \_ Patch name: $patch_name"
# RAF: this because -o pipefail is not available and set -e is set for debug ###
if output=$(patch_downloader.sh $patch_name 2>&1);
then ret=0; else ret=$?; fi
echo "$output" | eval "$filter_4" ||:
if [ $ret -ne 0 ]; then # -o pipefail alternative ends here ###
if [ -n "$patch_prev_strn" ]; then
echo " \_ Using local patch."
exit_for_manual_intervention
else
echo " \_ Patch unavailable: skip."
patch_broken_warning
skip=1; break
fi
else
patch_strn=$(read_patch_string)
if ! patch_string_to_filename "$patch_strn"; then
echo " \_ Patch void: skip."
patch_broken_warning
skip=1; break
fi
fi
if [ "$patch_path" = "$patch_prev_path" ]; then
patch_broken_warning
skip=1; break
fi
if applicable_check "$patch_path"; then
patch_broken_warning
skip=1; break
fi
break; done; test $skip -eq 1 && continue # ~~~~~~~~~~~~~~~~~~~~~~ # fake loop #
# This part cannot be interrupted # ********************************************
set +e; stty -echoctl 2>/dev/null ||:; trap 'true' SIGINT EXIT
echo
echo "=> Proceeding to $verb_action the patch #$n in $verstr version..."
# Test if the patch can be reversed as expected to be
# reversed. When patch fails despite the dry run test
# forcely apply the patch that reversed partially.
action="KO"
if ! reversible_check "$patch_path"; then
echo " \_ Patch #$n $past_action the dry run failed, skip."
elif do_patch -R -i "$patch_path"; then
echo " \_ Patch #$n $past_action in rootfs"
echo " \_ Services scheduled to restart: $srvs"
echo $srvs >> "$reload_path"
action="OK"
else
echo " \_ Patch #$n $verb_action failed, undoing the action..."
if do_patch -i "$patch_path"; then
echo " \_ Patch #$n reverted back sucessfully, skip."
elif ! reversible_check "$patch_path"; then
echo " \_ Patch #$n $verb_action failed, abort."
exit_for_manual_intervention #<- exit-point
fi
fi
if [ "$action" != "OK" ]; then
patch_broken_warning
fi
stty echoctl 2>/dev/null ||:; set -e; trap - SIGINT EXIT
# ******************************************************************************
# move to the next patch
done # =========================================================================
# This part cannot be interrupted # ********************************************
set +e; stty -echoctl 2>/dev/null ||:; trap 'true' SIGINT EXIT
service_switcher() {
test -n "${1:-}" || return 1
if [ "x$2" = "x-" ]; then
echo " | Service disable:" $1
$sctlcmd disable $1
$sctlcmd stop $1
else
echo " | Service enable:" $1
restart_list="${restart_list:-} $1"
$sctlcmd enable $1
fi
}
echo
sctlcmd="systemctl --no-pager"
servs_list=$(cat "$reload_path" 2>/dev/null ||:)
rm -f "$reload_path"
if [ -n "${servs_list:-}" ]; then
echo "=> Restarting system services"
echo " \_ To restart: "$servs_list
$sctlcmd daemon-reload
for i in $servs_list; do
s="$i"
m="${i:0:1}"
if [ "x$m" = "x-" ]; then
s="${i:1}"
fi
$sctlcmd reload $s
service_switcher $s $m
done 2>/dev/null ||:
echo " \_ Reload completed"
fi
if [ -n "${restart_list:-}" ]; then
rm -f /tmp/spm.fifo
mkfifo /tmp/spm.fifo
{ 2>/dev/null
sleep 1
$sctlcmd restart $restart_list >/dev/null
echo -e " \_ Restart done, ret:$?" >/tmp/spm.fifo
} &
disown &>/dev/null
echo
echo "WARNING: the connection will not automatically raise up again"
echo
dttm=$(date +"%Y-%m-%d %H:%M:%S")
echo "=> Restarted system services, ${dttm}..."
printf " \_ Press a key after the connection will be manually restored."
read
while IFS= read -r line; do
echo "$line"
done </tmp/spm.fifo
rm -f /tmp/spm.fifo
echo
dttm=$(date +"%Y-%m-%d %H:%M:%S")
echo "=> Checking the restarted system services, ${dttm}..."
echo " \_ To check:" $servs_list
echo
for i in $servs_list; do
s="$i"; test "x${i:0:1}" = "x-" && s="${i:1}"
$sctlcmd status $s 2>&1 | eval "$filter_5" ||:
done
echo
fi
stty echoctl 2>/dev/null ||:; set -e; trap - SIGINT EXIT
# ******************************************************************************