爲什麼dnsmasq服務總是重啓(by quqi99)

作者:張華 發表於:2020-05-19
版權聲明:可以任意轉載,轉載時請務必以超鏈接形式標明文章原始出處和作者信息及本版權聲明

問題

最近, 是有點感覺到家裏的網絡有時有點慢, 特別有時候看網頁時顯示dns找不到,但馬上在半秒之內又自動找着了成功顯示頁面. 能用也不算特別慢所以一直就沒太注意這個事.
依平時的經驗, 這種慢一般和dns有關, 所以今早就特地登錄到路由器查看dnsmasq的狀態. 檢查了一遍配置啥的也沒見異常, 但無意間發現dnsmasq的進程ID有時在變(不是很經常, 不容易觀察出來).

dnsmasq: exiting on receipt of SIGTERM

然後就想着將 dnmasq進程停掉, 然後以debug模式(-d)啓動看看日誌, 但是當運行了/etc/init.d/dnsma stop之後怎麼也停不掉啊, 一會兒進程又自動啓動了. 讀代碼發現openwrt的init腳本使用了procd, 但將所有procd相關的行全註釋掉之後, 停掉了但還是馬上就自動啓動了.
然後發現是/etc/rc.common在自動啓動, 接着就在反覆測試過程中形成了下列的代碼.

root@OpenWrt:~# cat /etc/init.d/dnsmasq 
#!/bin/sh /etc/rc.common
START=80
start() {
      #/usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.d --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq --server=114.114.114.114 -k &
   fi
}
stop() {
   killall dnsmasq
}

這時在前臺以’/usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.d --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq --server=114.114.114.114 -d’調試時發現dnsmasq進程自已會退出, 報這個錯"dnsmasq: exiting on receipt of SIGTERM"
這時以爲是dnsmasq程序有問題, 正在絕望之刻, 注意到了原因dnsmasq是被上面的stop殺死的. 通過/etc/init.d/dnsmasq stop根本停不住dnsmasq, 所以上面的stop函數中的killall dnsmasq在後臺被定期運行, 這樣就殺死了前臺的debug dnsmasq進程.

解決辦法

搞清楚’dnsmasq: exiting on receipt of SIGTERM’發生的原因之後, 將腳本暫時改成下列醜陋的代碼:

oot@OpenWrt:~# cat /etc/init.d/dnsmasq 
#!/bin/sh /etc/rc.common
START=80

start() {
   if pidof dnsmasq >/dev/null
   then
      echo "dnsmasq is running"
   else
      echo "dnsmasq stopped, start it now ..."
      /usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.d --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq \
      --server=114.114.114.114 --listen-address=192.168.99.1,127.0.0.1 --dhcp-range=192.168.99.100,192.168.99.199,2400h \
      --dhcp-option=3,192.168.99.1 --dhcp-option=option:dns-server,192.168.99.1 -k &
   fi
}

stop() {
   echo 'NOTE: we must not use 'killall dnsmasq' to stop dnsmasq here because stop function always be called by rc.common periodically'
}

server can’t find xxx.my.salesforce.com: REFUSED

另外, 解釋白名單的dns沒問題, 解析黑名單的dns也沒問題, 但解析不在名單之內的dns時報’server can’t find xxx.my.salesforce.com: REFUSED’之類的錯誤時, 那是因爲dnsmasq命令中沒有添加–server=114.114.114.114

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章