Skip to content

Commit 1d9c11a

Browse files
committed
optware: fix initscript for asusrouter.
1 parent d748b9e commit 1d9c11a

File tree

1 file changed

+157
-14
lines changed

1 file changed

+157
-14
lines changed

package/optware/S50smartdns

+157-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SMARTDNS_BIN=/opt/usr/sbin/smartdns
1919
SMARTDNS_CONF=/opt/etc/smartdns/smartdns.conf
2020
DNSMASQ_CONF="/etc/dnsmasq.conf /var/etc/dnsmasq.conf /etc/storage/dnsmasq/dnsmasq.conf"
2121
SMARTDNS_PID=/run/smartdns.pid
22+
SMARTDNS_CHECK_PID=/tmp/smartdns_delay_check.pid
2223
if [ ! -d "/run" ]; then
2324
SMARTDNS_PID=/var/run/smartdns.pid
2425
fi
@@ -31,6 +32,8 @@ SMARTDNS_OPT=/opt/etc/smartdns/smartdns-opt.conf
3132
# 2: replace
3233
SMARTDNS_WORKMODE="1"
3334

35+
SMARTDNS_INIT_SCRIPT="$0"
36+
3437
if [ -f "$SMARTDNS_OPT" ]; then
3538
. "$SMARTDNS_OPT"
3639
fi
@@ -102,13 +105,26 @@ get_dnsmasq_cmd()
102105
fi
103106
}
104107

105-
restart_dnsmasq()
108+
get_dnsmasq_cmdline()
106109
{
110+
107111
local CMD=""
112+
local loop=0
113+
114+
while [ $loop -lt 3 ]; do
115+
get_dnsmasq_cmd
116+
if [ ! -z "$CMD" ]; then
117+
break;
118+
fi
119+
120+
$SMARTDNS_INIT_SCRIPT stop
121+
sleep 1
122+
loop=$((loop+1))
123+
done
108124

109-
get_dnsmasq_cmd
110125
if [ -z "$CMD" ]; then
111126
echo "cannot find dnsmasq"
127+
service restart_dnsmasq 2>/dev/null
112128
return 1
113129
fi
114130

@@ -121,7 +137,7 @@ restart_dnsmasq()
121137
PID2="$(echo "$CMD" | awk 'NR==2{print $1}')"
122138
PID2_PPID="$(grep 'PPid:' /proc/$PID2/status | awk '{print $2}' 2>/dev/null)"
123139
if [ "$PID2_PPID" != "$PID1" ]; then
124-
kill -9 "$PID2"
140+
kill -9 "$PID2" 2>/dev/null
125141
fi
126142
PID=$PID1
127143
else
@@ -134,13 +150,29 @@ restart_dnsmasq()
134150
return 1
135151
fi
136152

137-
kill -9 "$PID"
138-
139-
# get dnsmasq command
140153
CMD="$(echo "$CMD" | head -n 1)"
141154
DNSMASQ_CMD="$(echo "$CMD" | awk '{for(i=5; i<=NF;i++)printf $i " "}')"
142155

156+
return 0
157+
}
158+
159+
restart_dnsmasq()
160+
{
161+
if [ -z "$DNSMASQ_CMD" ]; then
162+
get_dnsmasq_cmdline
163+
if [ $? -ne 0 ]; then
164+
echo "cannot find dnsmasq"
165+
return 1
166+
fi
167+
fi
168+
169+
if [ ! -z "$PID" ]; then
170+
kill -9 "$PID"
171+
fi
172+
143173
$DNSMASQ_CMD
174+
175+
return $?
144176
}
145177

146178
add_dhcp_options6()
@@ -213,7 +245,7 @@ set_dnsmasq_conf()
213245
fi
214246
}
215247

216-
set_dnsmasq()
248+
do_set_dnsmasq()
217249
{
218250
local RESTART_DNSMASQ=0
219251

@@ -231,6 +263,82 @@ set_dnsmasq()
231263
fi
232264
}
233265

266+
kill_dnsmasq_delay_check_pid()
267+
{
268+
if [ ! -e "$SMARTDNS_CHECK_PID" ]; then
269+
return
270+
fi
271+
272+
PID="$(cat $SMARTDNS_CHECK_PID)"
273+
if [ -d "/proc/$PID" ]; then
274+
kill -9 $PID
275+
fi
276+
rm -f $SMARTDNS_CHECK_PID
277+
}
278+
279+
dnsmasq_delay_check()
280+
{
281+
sleep 8
282+
rm -f $SMARTDNS_CHECK_PID
283+
get_dnsmasq_cmdline
284+
if [ -z "$DNSMASQ_CMD" ] ; then
285+
$SMARTDNS_INIT_SCRIPT restart
286+
return
287+
fi
288+
289+
do_set_dnsmasq
290+
pid="$(cat $SMARTDNS_PID |head -n 1 2>/dev/null)"
291+
if [ -z "$pid" ]; then
292+
do_clear_dnsmasq
293+
$SMARTDNS_INIT_SCRIPT start > /dev/null 2>&1 &
294+
elif [ ! -d "/proc/$pid" ]; then
295+
do_clear_dnsmasq
296+
$SMARTDNS_INIT_SCRIPT start > /dev/null 2>&1 &
297+
fi
298+
exit 0
299+
}
300+
301+
begin_dnsmasq_delay_check()
302+
{
303+
DNSMASQ_CMD=""
304+
kill_dnsmasq_delay_check_pid
305+
get_dnsmasq_cmdline
306+
dnsmasq_delay_check > /dev/null 2>&1 &
307+
PID=$!
308+
echo $PID > $SMARTDNS_CHECK_PID
309+
}
310+
311+
set_dnsmasq()
312+
{
313+
get_dnsmasq_cmdline
314+
do_set_dnsmasq
315+
begin_dnsmasq_delay_check
316+
}
317+
318+
set_jffs_dnsmasq()
319+
{
320+
local RESTART_DNSMASQ=0
321+
322+
if [ "$(nvram get jffs2_scripts)" -ne 1 ]; then
323+
nvram set jffs2_scripts="1"
324+
nvram commit
325+
fi
326+
327+
touch /jffs/configs/dnsmasq.conf.add
328+
329+
if [ -e "/jffs/configs/dnsmasq.conf.add" ]; then
330+
set_dnsmasq_conf "/jffs/configs/dnsmasq.conf.add"
331+
fi
332+
333+
if [ -e "/jffs/configs/dnsmasq.conf" ]; then
334+
set_dnsmasq_conf "/jffs/configs/dnsmasq.conf"
335+
fi
336+
337+
if [ $RESTART_DNSMASQ -ne 0 ]; then
338+
restart_dnsmasq
339+
fi
340+
}
341+
234342
clear_dnsmasq_conf()
235343
{
236344
local LOCAL_SERVER_IP=""
@@ -246,7 +354,7 @@ clear_dnsmasq_conf()
246354
fi
247355
}
248356

249-
clear_dnsmasq()
357+
do_clear_dnsmasq()
250358
{
251359
local RESTART_DNSMASQ=0
252360

@@ -259,6 +367,33 @@ clear_dnsmasq()
259367
clear_dnsmasq_conf "$conf"
260368
done
261369

370+
if [ $RESTART_DNSMASQ -ne 0 ]; then
371+
if [ $? -eq 0 ]; then
372+
return
373+
fi
374+
375+
restart_dnsmasq
376+
fi
377+
}
378+
379+
clear_dnsmasq()
380+
{
381+
kill_dnsmasq_delay_check_pid
382+
do_clear_dnsmasq
383+
}
384+
385+
clear_jffs_dnsmasq()
386+
{
387+
local RESTART_DNSMASQ=0
388+
389+
if [ -e "/jffs/configs/dnsmasq.conf.add" ]; then
390+
clear_dnsmasq_conf "/jffs/configs/dnsmasq.conf.add"
391+
fi
392+
393+
if [ -e "/jffs/configs/dnsmasq.conf" ]; then
394+
clear_dnsmasq_conf "/jffs/configs/dnsmasq.conf"
395+
fi
396+
262397
if [ $RESTART_DNSMASQ -ne 0 ]; then
263398
restart_dnsmasq
264399
fi
@@ -269,14 +404,16 @@ set_smartdns_port()
269404
if [ "$SMARTDNS_WORKMODE" = "0" ]; then
270405
return 0
271406
elif [ "$SMARTDNS_WORKMODE" = "1" ]; then
272-
sed -i "s/^\(bind .*\):53\( .*\)\?$/\1:$SMARTDNS_PORT \2/g" $SMARTDNS_CONF
273-
sed -i "s/^\(bind-tcp .*\):53\( .*\)\?$/\1:$SMARTDNS_PORT \2/g" $SMARTDNS_CONF
407+
sed -i "s/^\(bind .*\):53\(@[^ ]*\)\?\( .*\)\?$/\1:$SMARTDNS_PORT\2 \3/g" $SMARTDNS_CONF
408+
sed -i "s/^\(bind-tcp .*\):53\(@[^ ]*\)\?\( .*\)\?$/\1:$SMARTDNS_PORT\2 \3/g" $SMARTDNS_CONF
274409
elif [ "$SMARTDNS_WORKMODE" = "2" ]; then
275-
sed -i "s/^\(bind .*\):$SMARTDNS_PORT\( .*\)\?$/\1:53 \2/g" $SMARTDNS_CONF
276-
sed -i "s/^\(bind-tcp .*\):$SMARTDNS_PORT\( .*\)\?$/\1:53 \2/g" $SMARTDNS_CONF
410+
sed -i "s/^\(bind .*\):$SMARTDNS_PORT\(@[^ ]*\)\?\( .*\)\?$/\1:53\2 \3/g" $SMARTDNS_CONF
411+
sed -i "s/^\(bind-tcp .*\):$SMARTDNS_PORT\(@[^ ]*\)\?\( .*\)\?$/\1:53\2 \3/g" $SMARTDNS_CONF
412+
elif [ "$SMARTDNS_WORKMODE" = "3" ]; then
413+
return 0
277414
else
278415
return 1
279-
fi
416+
fi
280417

281418
return 0
282419
}
@@ -291,6 +428,9 @@ set_rule()
291428
elif [ "$SMARTDNS_WORKMODE" = "2" ]; then
292429
set_dnsmasq
293430
return $?
431+
elif [ "$SMARTDNS_WORKMODE" = "3" ]; then
432+
set_jffs_dnsmasq
433+
return $?
294434
else
295435
return 1
296436
fi
@@ -306,6 +446,9 @@ clear_rule()
306446
elif [ "$SMARTDNS_WORKMODE" = "2" ]; then
307447
clear_dnsmasq
308448
return $?
449+
elif [ "$SMARTDNS_WORKMODE" = "3" ]; then
450+
clear_jffs_dnsmasq
451+
return $?
309452
else
310453
return 1
311454
fi
@@ -391,7 +534,7 @@ case "$1" in
391534
break
392535
fi
393536

394-
stat="$(cat /proc/${pid}/stat | awk '{print $3}' 2>/dev/null)"
537+
stat="$(cat /proc/${pid}/stat 2>/dev/null | awk '{print $3}' 2>/dev/null)"
395538
if [ "$stat" = "Z" ]; then
396539
$SLEEP $SLEEPTIME
397540
break

0 commit comments

Comments
 (0)