updater: Added retcode file (pid-file deleted)

This commit is contained in:
remittor
2025-12-18 10:46:29 +03:00
parent 06f45e3fbb
commit a616c6fa4e
3 changed files with 44 additions and 62 deletions

View File

@@ -104,80 +104,66 @@ return baseclass.extend({
} }
let rpc_opt = { timeout: 5*1000 } let rpc_opt = { timeout: 5*1000 }
//rpc_opt.uid = 0; // run under root //rpc_opt.uid = 0; // run under root
const x_filename = 'zapret_pkg_install'; const logFile = '/tmp/zapret_pkg_install.log';
const pidFile = '/tmp/' + x_filename + '.pid'; const rcFile = logFile + '.rc';
const logFile = '/tmp/' + x_filename + '.log';
const errFile = '/tmp/' + x_filename + '.err';
try { try {
await fs.exec('/bin/busybox', [ 'rm', '-f', '/tmp/' + x_filename + '.*' ], null, rpc_opt); await fs.exec('/bin/busybox', [ 'rm', '-f', logFile + '*' ], null, rpc_opt);
this.appendLog('Install log cleared.'); this.appendLog('Install log cleared.');
} catch (e) { } catch (e) {
this.appendLog('ERROR: Failed to clear log file'); this.appendLog('ERROR: Failed to clear log file');
this.setStage(999); this.setStage(999);
return 1; return 1;
} }
//console.log(`Start ${fn_update_pkg_sh}...`);
try { try {
let opt = [ x_filename, fn_update_pkg_sh ]; let opt = [ logFile, fn_update_pkg_sh ];
//opt.push('-t'); // only for testing //opt.push('-t'); opt.push('0'); // only for testing
opt.push(...opt_list); opt.push(...opt_list);
await fs.exec('/opt/zapret/script-exec.sh', opt, null, rpc_opt); let res = await fs.exec('/opt/zapret/script-exec.sh', opt, null, rpc_opt);
this.appendLog('Process started...'); if (res.code == 0) {
this.appendLog('Process started...');
} else {
this.appendLog('ERROR: cannot run ' + fn_update_pkg_sh + ' script! (error = ' + res.code + ')');
throw new Error('cannot run script');
}
} catch (e) { } catch (e) {
this.appendLog('ERROR: Failed to start process: ' + e.message); this.appendLog('ERROR: Failed to start process: ' + e.message);
this.setStage(999); this.setStage(999);
return 1; return 1;
} }
let lastLen = 0; let lastLen = 0;
let pid = 0; let retCode = -1;
//console.log('setInterval...');
let timer = setInterval(async () => { let timer = setInterval(async () => {
try { try {
if (pid == 0) {
try {
let pid_data = await fs.exec('/bin/cat', [ pidFile ], null, rpc_opt);
pid = parseInt(pid_data.stdout.trim(), 10);
} catch (e) {
//return; // goto next timer iteration
}
}
let alive = null;
if (pid > 0) {
try {
await fs.stat(`/proc/${pid}`, [ ], null, { timeout: 4*1000 });
alive = true;
} catch (e) {
// file "/proc/${pid}" not founded ==> sh process terminated
alive = false;
}
}
let res = await fs.exec('/bin/cat', [ logFile ], null, rpc_opt); let res = await fs.exec('/bin/cat', [ logFile ], null, rpc_opt);
if (res.stdout.length > lastLen) { if (res.stdout && res.stdout.length > lastLen) {
let log = res.stdout.slice(lastLen); let log = res.stdout.slice(lastLen);
log = log.replace(/^ \* resolve_conffiles.*(?:\r?\n|$)/gm, ''); log = log.replace(/^ \* resolve_conffiles.*(?:\r?\n|$)/gm, '');
this.appendLog(log, ''); this.appendLog(log, '');
lastLen = res.stdout.length; lastLen = res.stdout.length;
} }
if (pid > 0 && !alive) { if (retCode < 0) {
let rc = await fs.exec('/bin/cat', [ rcFile ], null, rpc_opt);
if (rc.code != 0) {
throw new Error('cannot read file "' + rcFile + '"');
}
if (rc.stdout) {
retCode = parseInt(rc.stdout.trim(), 10);
}
}
if (retCode >= 0) {
clearInterval(timer); clearInterval(timer);
this.appendLog('\nProcess finished.'); this.appendLog('\n' + 'Process finished.');
let err_code = -1; if (res.stdout) {
try { let code = res.stdout.match(/^RESULT:\s*\(([^)]+)\)\s+.+$/m);
let err_data = await fs.exec('/bin/cat', [ errFile ], null, rpc_opt); if (retCode == 0 && code && code[1] == '+') {
err_code = parseInt(err_data.stdout.trim(), 10); this.stage = 999;
} catch (e) { this.btn_action.textContent = _('OK');
// nothing this.btn_action.disabled = false;
this.btn_cancel.disabled = true;
return 0;
}
} }
let log = res.stdout; this.appendLog('ERROR: Install updates failed with error ' + retCode);
let code = log.match(/^RESULT:\s*\(([^)]+)\)\s+.+$/m);
if (code && code[1] == '+') {
this.stage = 999;
this.btn_action.textContent = _('OK');
this.btn_action.disabled = false;
this.btn_cancel.disabled = true;
return 0;
}
this.appendLog('ERROR: Install updates failed with error ' + err_code);
this.setStage(999); this.setStage(999);
} }
} catch (e) { } catch (e) {
@@ -235,9 +221,6 @@ return baseclass.extend({
ui.showModal(_('Package update'), [ ui.showModal(_('Package update'), [
E('div', { 'class': 'cbi-section' }, [ E('div', { 'class': 'cbi-section' }, [
E('div', {}, [
E('p', {'class': 'cbi-title-field'}, [ 'CPU architecture: ' + pkg_arch ]),
]),
exclude_prereleases, exclude_prereleases,
E('br'), E('br'), E('br'), E('br'),
forced_reinstall, forced_reinstall,

View File

@@ -1,16 +1,15 @@
#!/bin/sh #!/bin/sh
# Copyright (c) 2024 remittor # Copyright (c) 2024 remittor
PID_FILE=/tmp/$1.pid LOG_FILE=$1
LOG_FILE=/tmp/$1.log RC_FILE=$1.rc
ERR_FILE=/tmp/$1.err
shift 1 shift 1
: > $LOG_FILE : > $LOG_FILE
: > $ERR_FILE : > $RC_FILE
( (
exec </dev/null >/dev/null 2>&1 exec </dev/null >/dev/null 2>&1
"$@" >> $LOG_FILE 2>&1 "$@" >> $LOG_FILE 2>&1
echo $? > "$ERR_FILE" RETCODE=$?
sleep 1 sleep 1
echo $RETCODE > $RC_FILE
) & ) &
echo $! > $PID_FILE
exit 0 exit 0

View File

@@ -11,26 +11,26 @@ opt_update=
opt_forced= opt_forced=
opt_test= opt_test=
while getopts "cu:pft" opt; do while getopts "cu:pft:" opt; do
case $opt in case $opt in
c) opt_check=true;; c) opt_check=true;;
p) opt_prerelease=true;; p) opt_prerelease=true;;
u) opt_update="$OPTARG";; u) opt_update="$OPTARG";;
f) opt_forced=true;; f) opt_forced=true;;
t) opt_test=true;; t) opt_test="$OPTARG";;
esac esac
done done
ZAP_PKG_DIR=/tmp/zapret_pkg ZAP_PKG_DIR=/tmp/zapret_pkg
if [ "$opt_test" = "true" ]; then if [ "$opt_test" != "" ]; then
echo 1; sleep 2; echo 1; sleep 2;
echo 2; sleep 2; echo 2; sleep 2;
echo 3; sleep 2; echo 3; sleep 2;
echo ' * resolve_conffiles 123456'; sleep 1; echo ' * resolve_conffiles 123456'; sleep 1;
echo 4; sleep 2; echo 4; sleep 2;
echo END echo END
return 0; return "$opt_test"
fi fi
ZAP_CPU_ARCH=$(get_cpu_arch) ZAP_CPU_ARCH=$(get_cpu_arch)