-
Notifications
You must be signed in to change notification settings - Fork 105
/
enigma2.sh.in
420 lines (381 loc) · 13.2 KB
/
enigma2.sh.in
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/bash
# This *is* a bash script, not sh...
prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@
# perform a NTP update sync, before starting Enigma2, as it is broke in OE.
#if [ -x /usr/sbin/ntpdate ]; then
# /usr/sbin/ntpdate -s -u pool.ntp.org && sleep 2
#fi
# hook to execute user startup script before enigma2 start
if [ -x @sysconfdir@/enigma2/startup.sh ]; then
@sysconfdir@/enigma2/startup.sh
fi
do_reader_kill=0
if [ -e /proc/stb/info/boxtype ]; then
# Set relevant boxtypes to 1 - missing ones are treated as 0.
declare -A boxtest=(
viper4k 1 beyonwizv2 1 sf8008 1 sf8008m 1
sfx6008 1 sx988 1 sx88v2 1 gbmv200 1
)
stbcheck=`cat /proc/stb/info/boxtype`
if [ ${boxtest[$stbcheck]:-0} -eq 1 ]; then
count=`ps -ef |grep libreader |grep -v "grep" |wc -l`
if [ 0 == $count ];then
libreader 720P_50
do_reader_kill=1
fi
fi
unset boxtest
fi
if [ -x @bindir@/showiframe ]; then
SKIN=`sed -En 's|config\.skin\.primary_skin=(.+)/skin\.xml|\1|p' @sysconfdir@/enigma2/settings`
if [ -z "$SKIN" ]; then
SKIN=`strings -n 10 @prefix@/lib/enigma2/python/skin.pyc | egrep -o -m 1 ".+/skin.xml" | sed 's|/skin.xml.*||'`
fi
if [ -n "${SKIN}" ]; then
SEARCHDIRS="@sysconfdir@/enigma2/${SKIN} @sysconfdir@/enigma2/skin_common @sysconfdir@/enigma2 @datadir@/enigma2/${SKIN} @datadir@/enigma2/skin_default @datadir@/enigma2 @datadir@"
else
SEARCHDIRS="@sysconfdir@/enigma2/skin_common @sysconfdir@/enigma2 @datadir@/enigma2/skin_default @datadir@/enigma2 @datadir@"
fi
for DIR in ${SEARCHDIRS}; do
if [ -d "${DIR}" ] && [ -f "${DIR}/backdrop.mvi" ]; then
@bindir@/showiframe "${DIR}/backdrop.mvi" &
break
fi
done
fi
# hook to execute scripts always before enigma2 start
if [ -x @bindir@/enigma2_pre_start.sh ]; then
@bindir@/enigma2_pre_start.sh
fi
if [ -d /home/root ]; then
export HOME=/home/root
cd
fi
# Pre-load libopen, which will set the Close-on-Exec flag for all opened
# file units so that they don't leak into sub-processes.
#
#check for sundtek tuner helper lib
if [ -e /opt/lib/libmediaclient.so ]; then
LIBS="/opt/lib/libmediaclient.so /usr/lib/libopen.so.0.0.0"
else
LIBS="/usr/lib/libopen.so.0.0.0"
fi
sync
(sleep 2; echo "enigma2 is the main pvr application... adjust oom score!"; PID=$(pidof enigma2); \
[ -e /proc/$PID/oom_score_adj ] && echo "-999" > /proc/$PID/oom_score_adj || echo "-17" > /proc/$PID/oom_adj;) &
export LC_CTYPE="en_US.UTF-8"
# set time format used to prefix each line in the debug logs
#
if grep -q 'config\.crash\.logtimeformat' @sysconfdir@/enigma2/settings; then
export ENIGMA_DEBUG_TIME=`grep "config\.crash\.logtimeformat" @sysconfdir@/enigma2/settings | sed -E "s/^.*=(.*)$/\1/" -`
fi
# Enable generation of core dumps
debug_dir=$(sed -n 's/config.crash.debug_path=//p' @sysconfdir@/enigma2/settings)
[ -n "${debug_dir}" -a -d "${debug_dir}" ] || debug_dir=/home/root/logs/
# It would be best to have this at system startup because this way we may not
# catch core dumps that happen early to enigma2.
# The kernel.core_pattern could be set via /etc/sysctl.conf.
# The size limit could be set via /etc/security/limits.conf.
if [ "$(grep -i config.crash.coredump=true /etc/enigma2/settings)" != "" ]; then
COREDUMPSIZE=$((10 * 512 * 4 - 1 * 512 * 4)) # size is in blocks (512 byte) so this sets 10 MB - 1 MB for log files
COREDUMPFILE=${debug_dir}%t-enigma2-core.dump
if [ "$(grep -i config.crash.sizeloglimit= /etc/enigma2/settings | sed 's/config.crash.sizeloglimit=//g')" != "" ]; then
COREDUMPSIZE=$(grep -i config.crash.sizeloglimit= /etc/enigma2/settings | sed 's/config.crash.sizeloglimit=//g')
COREDUMPSIZE=$((${COREDUMPSIZE} * 512 * 4 - 1 * 512 * 4))
fi
echo ${COREDUMPFILE} > /proc/sys/kernel/core_pattern
ulimit -c ${COREDUMPSIZE}
fi
# If we are running with a log then we need to set up a sub-process to
# do this so that it can open a new log if the current one's size
# becomes too large.
# So, we define various functions for use
#
if grep -q 'config\.crash\.enabledebug=True' @sysconfdir@/enigma2/settings; then
DEBUG_LVL='4'
# Generate a random message for the logger to know that enigma2 has
# ended
#
unem="Enigma2 is LEAVING: $RANDOM"
# ========== Start of log functions ==========
# fixup_log() - remove any partial-line at the end a logfile as we
# switch and add a note there instead.
#
fixup_log() {
old="$1"
new="$2"
# Remove the fsize limit, so we can add the continuation tag.
# We're in a subprocess and exit when done, so this won't affect
# restricting the active log file
#
ulimit -S -f unlimited
# An in-line python script...
python - "$old" "$new" <<EOP
import sys, os
ofile = sys.argv[1]
nfile = sys.argv[2]
size = os.stat(ofile).st_size
pos = size - (size % 1024)
# We partition the file into 1024-byte blocks (plus the probably
# incomplete one at the end), read them in reverse order and
# look for the last newline character in each block.
# As soon as we've found one we've found the last newline in the file.
# Anything beyond that is an incomplete line which took us to the
# file-size limit, so we truncate there and add the new log location.
# The complete text of any such incomplete ending line will be written
# at the start of the next log, after the note of the preceding log.
#
with open(ofile, "r+b") as f:
while pos >= 0:
f.seek(pos)
buf = f.read(1024)
where = buf.rfind(b"\n")
if where < 0:
pos -= 1024
continue
# OK, we've found the last newline
# seek there and truncate
#
f.seek(pos + where + 1)
f.truncate()
# Now add the note as to where future logging has gone
#
f.write(b"Continued in %s\n" % bytes(nfile, "UTF8"))
break
EOP
}
# new_log() - open a new log and, if this is a restart, note the
# next/prev logs pathnames at the end/start of the relevant logs
#
new_log() {
if [ -n "$logname" ]; then
prev=$logname
else
prev=
fi
logname="${debug_dir}/Enigma2_debug_`date +%Y-%m-%d_%H-%M-%S`.log"
exec > $logname 2>&1 # Open the new one
# If we have opened a new one then we wish to record what the previous
# one was and also to remove any partial-line at the end of the old log
# and note the new logname there instead.
# We do this in the background so that we can get on with logging.
#
if [ -n "$prev" ]; then
fixup_log "$prev" "$logname"&
echo "Continued from $prev"
logswitched=0
else
logswitched=1
fi
}
# Set a new file-size limit
# We MUST ensure that this is ONLY set for the dblogger - NOT enigma2!!!
# ulimit uses 1024-byte units, so we must multiply the configured limit
# (in MB) by 1024
#
new_limit() {
lim=$(($1 * 1024))
ulimit -S -f $lim
}
# dblogger - the main function. This receives the log text on
# stdout
#
dblogger() {
# We want the logger to ignore TERM, so that if a reboot command is run
# from the command line this doesn't get booted out before enigma2
# closes down.
# We'll exit when enigma2 does anyway (the "read line" will fail).
#
trap "" SIGTERM
logname=
lim=
logswitched=0
debug_dir="/home/root/logs"
# We look for config.crash.debugloglimit in settings
# (default == 4, see Screens/LogManager.py) and use it:
#
debugloglimit=`awk -F= '
BEGIN{res=4};
$1 == "config.crash.debugloglimit" {res=$2; exit};
END{print res}' @sysconfdir@/enigma2/settings`
new_limit $debugloglimit
# We look for config.crash.debug_path in settings
# (default == /home/root/logs/, see Screens/LogManager.py) and use it:
#
raw=`awk -F= '
BEGIN{res="/home/root/logs/"};
$1 == "config.crash.debug_path" {res=$2; exit};
END{print res}' /etc/enigma2/settings`
debug_dir=${raw%/} # Strip trailing / if there.
trap new_log SIGXFSZ # Re-opener
new_log # First opener
# The values will be reported at start-up, but we've already read and
# handled them above, so set flags to ignore them the first time.
#
first_limit=1 # Not yet seen
first_lpath=1 # Not yet seen
echo "========== Enigma2 log starts =========="
while read -r line; do # -r == no slash-escaping of input
case "$line" in
# Are we being told that enigma2 has exited?
# If so, we exit too
#
$unem)
echo "enigma2 has exited"
break
;;
# Have we been asked to change the limit or log path?
# If so, activate the change.
# We'll log the CHANGE line as well....
#
CHANGE\ config.crash.debugloglimit=*)
if [ $first_limit -eq 1 ]; then
first_limit=0
else
debugloglimit=`echo $line |
sed -e's/^.*config.crash.debugloglimit=//'`
new_limit $debugloglimit
fi
;;
CHANGE\ config.crash.debug_path=*)
if [ $first_lpath -eq 1 ]; then
first_lpath=0
else
raw=`echo $line |
sed -e's/^.*config.crash.debug_path=//'`
debug_dir=${raw%/} # Strip trailing / if there.
new_log
# new_log() will set logswitched, but we don't want to rewrite this line
# for this case, so unset it
#
logswitched=0
fi
;;
esac
echo "$line"
if [ $logswitched -eq 1 ]; then # New log opened
echo "$line" # Write full line into new log
logswitched=0
fi
done
# enigma2 has left.
# As we leave, lift the limit (so we don't potentially create a
# new log just to record the closing) and record this.
#
ulimit -S -f unlimited
echo "========== Enigma2 log ends =========="
}
# ========== End of log functions ==========
# Run into a logging function in such a way that when
# enigma2 exits the logger exits too.
# We need to get the exit code from enigma2, not the logger
#
( LD_PRELOAD=$LIBS @bindir@/enigma2 # run enigma2
xstat=$? # its exit code
echo # in case of mid-line crisis
echo $unem # marker to end logger
exit $xstat # exit code for this "block"
) 2>&1 | dblogger
ret=${PIPESTATUS[0]} # enigma2 exit code, not dblogger's
else
# if debug logs are disabled
if grep -q 'config\.misc\.firstrun=False' @sysconfdir@/enigma2/settings; then
# Write unwanted output to /dev/null. Do not write output to the serial port or the receiver will run slow.
LD_PRELOAD=$LIBS @bindir@/enigma2 &> /dev/null
else
# on first run only, write the output to the serial port
LD_PRELOAD=$LIBS @bindir@/enigma2
fi
ret=$?
fi
enigma2pid=$!
# Enable generation of core dumps with ELF header included.
# This seem to be required by gdb.
# Would be default if kernel is configured with CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS.
# Default coredump filter can only be set via kernel command line.
# In all other cases this is only available per process.
if [ "$(grep -i config.crash.coredump=true /etc/enigma2/settings)" != "" ]; then
COREDUMPFILTER=0x33
echo ${COREDUMPFILTER} > /proc/${enigma2pid}/coredump_filter
fi
wait ${enigma2pid}
# enigma2 exit codes:
#
# 1 - halt
# 2 - reboot
# 3 - restart enigma
#
# >128 signal
if [ "$ret" -ne "1" ]; then
if [ $do_reader_kill -ne 0 ]; then
killall -9 libreader; sleep 5
fi
fi
# hook to execute user standby script before enigma2 exits
if [ -x @sysconfdir@/enigma2/standby.sh ]; then
/etc/enigma2/standby.sh
fi
case $ret in
1)
/sbin/halt
;;
2)
/sbin/reboot
;;
3)
rm -f /tmp/.listen.camd.socket.ignore
;;
4)
/sbin/rmmod lcd
/usr/sbin/fpupgrade --upgrade 2>&1 | tee /home/root/fpupgrade.log
sleep 1;
/sbin/rmmod fp
/sbin/modprobe fp
/sbin/reboot
;;
12)
if [ -e /dev/block/by-name/bootoptions ]; then
mkdir -p /tmp/bootoptions
mount /dev/block/by-name/bootoptions /tmp/bootoptions
cp -f /tmp/bootoptions/STARTUP_ANDROID /tmp/bootoptions/STARTUP_ONCE
else
echo "rescue" > /proc/stb/fp/boot_mode
fi
/sbin/reboot
;;
16)
if [ -e /dev/block/by-name/flag ]; then
echo -n "0" > /dev/block/by-name/flag
elif [ -e /dev/block/by-name/bootoptions ]; then
mkdir -p /tmp/bootoptions
mount /dev/block/by-name/bootoptions /tmp/bootoptions
cp -f /tmp/bootoptions/STARTUP_RECOVERY /tmp/bootoptions/STARTUP_ONCE
elif [ -e /boot/STARTUP_RECOVERY ]; then
cp -f /boot/STARTUP_RECOVERY /boot/STARTUP_ONCE
else
echo "rescue" > /proc/stb/fp/boot_mode
fi
[[ -e /etc/enigma2/.deep ]] && rm /etc/enigma2/.deep
/sbin/reboot
;;
42)
python @prefix@/lib/enigma2/python/upgrade.pyc
;;
43)
init 1
;;
44)
# little hack but it will be fixed soon in drivers
/usr/lib/enigma2/python/Plugins/SystemPlugins/MICOMUpgrade/bin/fbclear
@bindir@/showiframe /usr/lib/enigma2/python/Plugins/SystemPlugins/MICOMUpgrade/wait.mvi
echo fpupload >/proc/vfd && sleep 3 && dd bs=256k if=/tmp/micom.bin of=/dev/mcu
@bindir@/showiframe /usr/lib/enigma2/python/Plugins/SystemPlugins/MICOMUpgrade/reboot.mvi
# Wait forever for the user to power off
while(true) ; do sleep 60 ; done
;;
*)
rm -f /tmp/.listen.camd.socket.ignore
;;
esac