Skip to content

Commit fdf19d2

Browse files
committed
luci: Fix func execAndRead
1 parent 4cccfa7 commit fdf19d2

File tree

2 files changed

+70
-36
lines changed

2 files changed

+70
-36
lines changed

luci-app-zapret/htdocs/luci-static/resources/view/zapret/tools.js

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -783,65 +783,85 @@ return baseclass.extend({
783783
const logFile = log; // file for reading: '/tmp/zapret_pkg_install.log'
784784
const rcFile = logFile + '.rc';
785785
try {
786-
await fs.exec('/bin/busybox', [ 'rm', '-f', logFile + '*' ], null);
787-
appendLog('Output file cleared!');
786+
await fs.exec('/bin/busybox', [ 'rm', '-f', logFile ], null);
787+
await fs.exec('/bin/busybox', [ 'rm', '-f', rcFile ], null);
788+
//appendLog('Output file cleared!');
788789
} catch (e) {
789790
return callback.call(ctx, 500, 'ERROR: Failed to clear output file');
790791
}
791-
let processStarted = false;
792+
let execRetCode = -1;
792793
let opt_list = [ logFile ];
793794
try {
794795
opt_list.push(...cmd);
795796
//console.log('script-exec.sh ... '+JSON.stringify(opt_list));
796-
let proc = new Promise((resolve) => {
797-
fs.exec(this.appDir+'/script-exec.sh', opt_list, null)
798-
.then (() => { resolve(); })
799-
.catch(() => { resolve(); });
797+
fs.exec(this.appDir+'/script-exec.sh', opt_list, null)
798+
.then( (res) => {
799+
if (execRetCode < 0) {
800+
execRetCode = res.code;
801+
fixLogEnd();
802+
if (res.code == 0) {
803+
appendLog('Process started....');
804+
} else {
805+
if (res.stdout) appendLog(res.stdout);
806+
appendLog('ERROR: process not executed! ret_code = '+res.code);
807+
}
808+
}
809+
}).catch( (e) => {
810+
console.log('ERROR: execAndRead: process not exec: '+e.message);
811+
execRetCode = -100;
800812
});
801813
} catch (e) {
802814
return callback.call(ctx, 520, 'ERROR: Failed on execute process: ' + e.message);
803815
}
804816
let lastLen = 0;
805-
let retCode = -1;
817+
let retCode = -2; // rc file not found
806818
return await new Promise(async (resolve, reject) => {
807819
let ticks = 0;
808820
async function epoll()
809821
{
810822
ticks += 1;
811823
try {
824+
if (retCode < 0) {
825+
let rc = await fs.exec('/bin/cat', [ rcFile ], null);
826+
if (rc.code != 0) {
827+
if (ticks >= 2) {
828+
console.log('ERROR: execAndRead: '+JSON.stringify(opt_list));
829+
fixLogEnd();
830+
resolve(callback.call(ctx, 542, 'ERROR: Failed on read process rc-file: code = ' + rc.code));
831+
return;
832+
}
833+
console.log('WARN: execAndRead: read rc-file res.code = '+rc.code);
834+
}
835+
if (rc.code == 0) {
836+
if (rc.stdout) {
837+
retCode = parseInt(rc.stdout.trim(), 10);
838+
} else {
839+
retCode = -1; // rc file exists, but empty
840+
}
841+
}
842+
if (retCode <= -2) {
843+
setTimeout(epoll, 500);
844+
return; // skip first step with error
845+
}
846+
}
812847
let res = await fs.exec('/bin/cat', [ logFile ], null);
813848
if (res.code != 0) {
814-
if (ticks > 1) {
815-
console.log('ERROR: execAndRead: '+JSON.stringify(opt_list));
816-
resolve(callback.call(ctx, 541, 'ERROR: Failed on read process log: code = ' + res.code));
817-
return;
818-
}
819-
setTimeout(epoll, 500);
820-
return; // skip first step with error
849+
fixLogEnd();
850+
resolve(callback.call(ctx, 546, 'ERROR: Failed on read process log: code = ' + res.code));
851+
return;
821852
}
822-
if (!processStarted) {
853+
if (execRetCode < 0) {
854+
execRetCode = 9999;
823855
appendLog('Process started...');
824-
processStarted = true;
825856
}
826-
if (res.stdout && res.stdout.length > lastLen) {
857+
if (res.code == 0 && res.stdout && res.stdout.length > lastLen) {
827858
let log = res.stdout.slice(lastLen);
828859
hide_rows.forEach(re => {
829860
log = log.replace(re, '');
830861
});
831862
appendLog(log, '');
832863
lastLen = res.stdout.length;
833864
}
834-
if (retCode < 0) {
835-
let rc = await fs.exec('/bin/cat', [ rcFile ], null);
836-
if (rc.code != 0) {
837-
fixLogEnd();
838-
resolve(callback.call(ctx, 545, 'ERROR: cannot read file "' + rcFile + '"'));
839-
return;
840-
}
841-
if (rc.stdout) {
842-
retCode = parseInt(rc.stdout.trim(), 10);
843-
}
844-
}
845865
if (retCode >= 0) {
846866
fixLogEnd();
847867
if (retCode == 0 && res.stdout) {

zapret/script-exec.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
#!/bin/sh
22
# Copyright (c) 2024 remittor
3-
awk -V
43
PID_FILE=/tmp/zapret-script-exec.pid
5-
[ -f $PID_FILE ] && exit 70
4+
if [ -f $PID_FILE ]; then
5+
echo "ERROR: file $PID_FILE already exists!" | awk 'NR==1'
6+
exit 70
7+
fi
68
LOG_FILE=$1
79
RC_FILE=$1.rc
810
SH_FILE=$2
911
shift 2
10-
[ ! -f $SH_FILE ] && exit 71
11-
: > $LOG_FILE
12-
: > $RC_FILE
12+
if [ ! -e $SH_FILE ]; then
13+
echo "ERROR: script $SH_FILE not found!" | awk 'NR==1'
14+
exit 71
15+
fi
16+
if [ ! -x $SH_FILE ]; then
17+
echo "ERROR: script $SH_FILE not Found!" | awk 'NR==1'
18+
exit 72
19+
fi
1320
start-stop-daemon -S -b -p $PID_FILE -x /bin/sh -- -c '
1421
LOG_FILE=$1
1522
RC_FILE=$2
1623
SH_FILE=$3
1724
shift 3
18-
sh $SH_FILE "$@" > $LOG_FILE 2>&1
25+
printf "" > $LOG_FILE
26+
printf "" > $RC_FILE
27+
$SH_FILE "$@" >> $LOG_FILE 2>&1
1928
RET_CODE=$?
20-
sleep 1
2129
echo $RET_CODE > $RC_FILE
2230
' sh $LOG_FILE $RC_FILE $SH_FILE "$@"
31+
RET_CODE=$?
32+
if [ $RET_CODE != 0 ]; then
33+
echo "ERROR: script $SH_FILE not executed! ret_code = $RET_CODE" | awk 'NR==1'
34+
exit $RET_CODE
35+
fi
36+
echo "Script $SH_FILE running..." | awk 'NR==1'
2337
exit 0

0 commit comments

Comments
 (0)