2015-01-12 16:14:56 +00:00
#!/bin/sh
export LC_ALL="en_US.UTF-8"
# working directory of koreader
2020-09-22 12:25:17 +00:00
KOREADER_DIR="/mnt/ext1/applications/koreader"
2015-01-12 16:14:56 +00:00
2023-07-16 01:51:57 +00:00
# load our own shared libraries if possible, solely because we don't control InkView, and we'd rather not it have load duplicate system libs...
# (We handle this via DT_RPATH for our own stuff).
export LD_LIBRARY_PATH="${KOREADER_DIR}/libs${LD_LIBRARY_PATH+:}${LD_LIBRARY_PATH}"
2020-09-01 19:43:21 +00:00
# file through which we communicate instanced opens
2020-09-22 12:25:17 +00:00
export KO_PATH_OPEN_BOOK="/tmp/.koreader.open"
2020-09-01 19:43:21 +00:00
# check for and notify a running instance
2020-09-01 20:46:43 +00:00
INSTANCE_PID=$(cat /tmp/koreader.pid 2>/dev/null)
2020-09-01 19:43:21 +00:00
if [ "${INSTANCE_PID}" != "" ] && [ -e "/proc/${INSTANCE_PID}" ]; then
2020-09-01 20:46:43 +00:00
echo "$@" >"${KO_PATH_OPEN_BOOK}"
2024-05-13 10:08:16 +00:00
exec $(if [ -f "/usr/bin/iv2sh" ]; then echo /usr/bin/iv2sh; else echo /ebrmain/bin/iv2sh; fi) SetActiveTask "${INSTANCE_PID}" 0
2020-09-01 19:43:21 +00:00
fi
2020-10-17 10:59:24 +00:00
# try to bring in raw device input (on rooted devices)
/mnt/secure/su /bin/chmod 644 /dev/input/*
2020-09-01 19:43:21 +00:00
# we're first, so publish our instance
2020-09-01 20:46:43 +00:00
echo $$ >/tmp/koreader.pid
2020-09-01 19:43:21 +00:00
2015-01-12 16:14:56 +00:00
# update to new version from OTA directory
2020-09-22 12:25:17 +00:00
ko_update_check() {
NEWUPDATE="${KOREADER_DIR}/ota/koreader.updated.tar"
INSTALLED="${KOREADER_DIR}/ota/koreader.installed.tar"
if [ -f "${NEWUPDATE}" ]; then
"${KOREADER_DIR}/fbink" -q -y -7 -pmh "Updating KOReader"
2021-04-24 23:15:38 +00:00
# Setup the FBInk daemon
export FBINK_NAMED_PIPE="/tmp/.koreader.fbink"
rm -f "${FBINK_NAMED_PIPE}"
FBINK_PID="$("${KOREADER_DIR}/fbink" --daemon 1 %KOREADER% -q -y -6 -P 0)"
2020-09-22 12:25:17 +00:00
# NOTE: See frontend/ui/otamanager.lua for a few more details on how we squeeze a percentage out of tar's checkpoint feature
# NOTE: %B should always be 512 in our case, so let stat do part of the maths for us instead of using %s ;).
FILESIZE="$(stat -c %b "${NEWUPDATE}")"
2021-05-31 20:19:24 +00:00
# shellcheck disable=SC2003
BLOCKS="$(expr "${FILESIZE}" / 20)"
# shellcheck disable=SC2003
CPOINTS="$(expr "${BLOCKS}" / 100)"
export CPOINTS
2021-04-24 17:02:48 +00:00
# NOTE: We don't run as root, but folders created over USBMS are owned by root, which yields fun permission shenanigans...
# c.f., https://github.com/koreader/koreader/issues/7581
KO_PB_TARLOG="/tmp/.koreader.tar"
2020-09-22 12:25:17 +00:00
# shellcheck disable=SC2016
2021-05-31 20:19:24 +00:00
"${KOREADER_DIR}/tar" --no-same-permissions --no-same-owner --checkpoint="${CPOINTS}" --checkpoint-action=exec='printf "%s" $(expr ${TAR_CHECKPOINT} / ${CPOINTS}) > ${FBINK_NAMED_PIPE}' -C "/mnt/ext1" -xf "${NEWUPDATE}" 2>"${KO_PB_TARLOG}"
2020-09-22 12:25:17 +00:00
fail=$?
2021-04-24 23:15:38 +00:00
kill -TERM "${FBINK_PID}"
2021-04-24 17:02:48 +00:00
# As mentioned above, filter out potential chmod & utime failures...
if [ "${fail}" -ne 0 ]; then
if [ "$(grep -Evc '(Cannot utime|Cannot change mode|Exiting with failure status due to previous errors)' "${KO_PB_TARLOG}")" -eq "0" ]; then
# No other errors, we're good!
fail=0
fi
fi
rm -f "${KO_PB_TARLOG}"
2020-09-22 12:25:17 +00:00
# Cleanup behind us...
if [ "${fail}" -eq 0 ]; then
mv "${NEWUPDATE}" "${INSTALLED}"
"${KOREADER_DIR}/fbink" -q -y -6 -pm "Update successful :)"
"${KOREADER_DIR}/fbink" -q -y -5 -pm "KOReader will start momentarily . . ."
else
# Uh oh...
"${KOREADER_DIR}/fbink" -q -y -6 -pmh "Update failed :("
"${KOREADER_DIR}/fbink" -q -y -5 -pm "KOReader may fail to function properly!"
fi
2021-04-24 17:02:48 +00:00
rm -f "${NEWUPDATE}" # always purge newupdate to prevent update loops
2021-04-24 23:15:38 +00:00
unset CPOINTS FBINK_NAMED_PIPE
unset BLOCKS FILESIZE FBINK_PID
2020-09-22 12:25:17 +00:00
# Ensure everything is flushed to disk before we restart. This *will* stall for a while on slow storage!
sync
fi
}
2015-01-12 16:14:56 +00:00
# we're always starting from our working directory
2017-04-11 09:23:42 +00:00
cd ${KOREADER_DIR} || exit
2015-01-12 16:14:56 +00:00
# export dict directory
export STARDICT_DATA_DIR="data/dict"
2020-09-22 12:25:17 +00:00
# we keep at maximum 500K worth of crash log
2017-04-11 09:23:42 +00:00
if [ -e crash.log ]; then
2017-04-11 09:28:01 +00:00
tail -c 500000 crash.log >crash.log.new
2017-04-11 09:23:42 +00:00
mv -f crash.log.new crash.log
fi
2020-09-01 19:43:21 +00:00
export KO_EXIT_CODE="/tmp/.koreader.exit"
2020-09-22 12:25:17 +00:00
CRASH_COUNT=0
CRASH_TS=0
CRASH_PREV_TS=0
# List of supported special return codes
KO_RC_RESTART=85
#KO_RC_USBMS=86
# Ensure a clean slate on startup
rm -f "${KO_EXIT_CODE}"
RETURN_VALUE=${KO_RC_RESTART}
while [ "${RETURN_VALUE}" -ne 0 ]; do
if [ "${RETURN_VALUE}" -eq ${KO_RC_RESTART} ]; then
# Do an update check now, so we can actually update KOReader via the "Restart KOReader" menu entry ;).
ko_update_check
fi
2021-01-07 19:38:10 +00:00
./reader.lua "$@" >>crash.log 2>&1
2020-09-22 12:25:17 +00:00
# Account for the fact a hard crash may have prevented the KO_EXIT_CODE file from being written to...
if [ -f "${KO_EXIT_CODE}" ]; then
RETURN_VALUE="$(cat ${KO_EXIT_CODE})"
rm -f "${KO_EXIT_CODE}"
else
# If we couldn't find it, something went horribly wrong ;).
RETURN_VALUE=42
fi
# Did we crash?
if [ "${RETURN_VALUE}" -ne 0 ] && [ "${RETURN_VALUE}" -ne ${KO_RC_RESTART} ]; then
# Increment the crash counter
2021-05-31 20:19:24 +00:00
# shellcheck disable=SC2003
2024-06-15 20:54:32 +00:00
CRASH_COUNT="$(expr "${CRASH_COUNT}" + 1)"
2021-05-31 20:19:24 +00:00
CRASH_TS="$(date +'%s')"
2020-09-22 12:25:17 +00:00
# Reset it to a first crash if it's been a while since our last crash...
2021-05-31 20:19:24 +00:00
# shellcheck disable=SC2003
if [ "$(expr "${CRASH_TS}" - "${CRASH_PREV_TS}")" -ge 20 ]; then
2020-09-22 12:25:17 +00:00
CRASH_COUNT=1
fi
# Check if the user requested to always abort on crash
if grep -q '\["dev_abort_on_crash"\] = true' 'settings.reader.lua' 2>/dev/null; then
ALWAYS_ABORT="true"
# In which case, make sure we pause on *every* crash
CRASH_COUNT=1
else
ALWAYS_ABORT="false"
fi
# Show a fancy bomb on screen
viewWidth=600
viewHeight=800
FONTH=16
eval "$("${KOREADER_DIR}/fbink" -e | tr ';' '\n' | grep -e viewWidth -e viewHeight -e FONTH | tr '\n' ';')"
# Compute margins & sizes relative to the screen's resolution, so we end up with a similar layout, no matter the device.
# Height @ ~56.7%, w/ a margin worth 1.5 lines
2021-05-31 20:19:24 +00:00
# shellcheck disable=SC2003
bombHeight="$(expr ${viewHeight} / 2 + ${viewHeight} / 15)"
# shellcheck disable=SC2003
bombMargin="$(expr ${FONTH} + ${FONTH} / 2)"
2020-09-22 12:25:17 +00:00
# With a little notice at the top of the screen, on a big gray screen of death ;).
"${KOREADER_DIR}/fbink" -q -b -c -B GRAY9 -m -y 1 "Don't Panic! (Crash n°${CRASH_COUNT} -> ${RETURN_VALUE})"
2024-06-15 20:54:32 +00:00
if [ "${CRASH_COUNT}" -eq 1 ]; then
2020-10-11 21:08:24 +00:00
# Warn that we're sleeping for a bit...
"${KOREADER_DIR}/fbink" -q -b -O -m -y 2 "KOReader will restart in 15 sec."
2020-09-22 12:25:17 +00:00
fi
# U+1F4A3, the hard way, because we can't use \u or \U escape sequences...
2021-04-24 17:02:48 +00:00
# shellcheck disable=SC2039,SC3003
2021-05-31 20:19:24 +00:00
"${KOREADER_DIR}/fbink" -q -b -O -m -t regular=${KOREADER_DIR}/fonts/freefont/FreeSerif.ttf,px="${bombHeight}",top="${bombMargin}" -- $'\xf0\x9f\x92\xa3'
2020-09-22 12:25:17 +00:00
# And then print the tail end of the log on the bottom of the screen...
crashLog="$(tail -n 25 crash.log | sed -e 's/\t/ /g')"
# The idea for the margins being to leave enough room for an fbink -Z bar, small horizontal margins, and a font size based on what 6pt looked like @ 265dpi
2021-05-31 20:19:24 +00:00
# shellcheck disable=SC2003
"${KOREADER_DIR}/fbink" -q -b -O -t regular=${KOREADER_DIR}/fonts/droid/DroidSansMono.ttf,top="$(expr ${viewHeight} / 2 + ${FONTH} '*' 2 + ${FONTH} / 2)",left="$(expr ${viewWidth} / 60)",right="$(expr ${viewWidth} / 60)",px="$(expr ${viewHeight} / 64)" -- "${crashLog}"
2020-09-22 12:25:17 +00:00
# So far, we hadn't triggered an actual screen refresh, do that now, to make sure everything is bundled in a single flashing refresh.
${KOREADER_DIR}/fbink -q -f -s
# Cue a lemming's faceplant sound effect!
{
echo "!!!!"
echo "Uh oh, something went awry... (Crash n°${CRASH_COUNT}: $(date +'%x @ %X'))"
} >>crash.log 2>&1
2024-06-15 20:54:32 +00:00
if [ "${CRASH_COUNT}" -lt 5 ] && [ "${ALWAYS_ABORT}" = "false" ]; then
2020-09-22 12:25:17 +00:00
echo "Attempting to restart KOReader . . ." >>crash.log 2>&1
echo "!!!!" >>crash.log 2>&1
fi
# Pause a bit if it's the first crash in a while, so that it actually has a chance of getting noticed ;).
2024-06-15 20:54:32 +00:00
if [ "${CRASH_COUNT}" -eq 1 ]; then
2020-09-22 12:25:17 +00:00
sleep 15
fi
# Cycle the last crash timestamp
CRASH_PREV_TS=${CRASH_TS}
# But if we've crashed more than 5 consecutive times, exit, because we wouldn't want to be stuck in a loop...
# NOTE: No need to check for ALWAYS_ABORT, CRASH_COUNT will always be 1 when it's true ;).
2024-06-15 20:54:32 +00:00
if [ "${CRASH_COUNT}" -ge 5 ]; then
2020-09-22 12:25:17 +00:00
echo "Too many consecutive crashes, aborting . . ." >>crash.log 2>&1
echo "!!!! ! !!!!" >>crash.log 2>&1
break
fi
# If the user requested to always abort on crash, do so.
if [ "${ALWAYS_ABORT}" = "true" ]; then
echo "Aborting . . ." >>crash.log 2>&1
echo "!!!! ! !!!!" >>crash.log 2>&1
break
fi
else
# Reset the crash counter if that was a sane exit/restart
CRASH_COUNT=0
fi
2017-05-16 09:11:11 +00:00
done
2015-01-12 16:14:56 +00:00
2020-09-22 12:25:17 +00:00
rm -f "/tmp/koreader.pid"
2020-09-01 19:43:21 +00:00
2017-04-11 09:28:01 +00:00
if pidof reader.lua >/dev/null 2>&1; then
2017-04-11 09:23:42 +00:00
killall -TERM reader.lua
2015-01-12 16:14:56 +00:00
fi
2017-05-16 09:11:11 +00:00
2020-09-01 19:43:21 +00:00
exit "${RETURN_VALUE}"