文章目录
背景
- 最近在搞随身wifi烧录openwrt系统,配置好passwall2后,系统隔20分钟就崩溃重启一下,初步分析原因可能是内存泄漏,因为passwall2占用的内存巨大无比,如下:
- 在关闭了docker等无关的组件后,仍然存在崩溃,且查看日志信息找不到重启的日志。因此分析下Passwall2的启动文件,并使用ulimit限制进程资源占用。
- passwall2的启动文件在
/etc/init.d/passwall2
文件中,具体内容如下
openwrt的启动文件夹:/etc/init.d/
- 文件夹说明如下:
- 实际该系统的
/etc/init.d/
如下:root@OpenWrt:~# ls /etc/init.d/ -all drwxr-xr-x 2 root root 4096 Jul 13 2023 . drwxr-xr-x 36 root root 4096 Jan 6 20:45 .. -rwxr-xr-x 1 root root 402 Jul 13 2023 adbd -rwxr-xr-x 1 root root 3648 Jul 13 2023 alist -rwxr-xr-x 1 root root 300 Jul 13 2023 avahi-daemon -rwxr-xr-x 1 root root 1399 Jul 13 2023 boot -rwxr-xr-x 1 root root 213 Jul 13 2023 cgroupfs-mount -rwxr-xr-x 1 root root 1637 Jul 13 2023 cpufreq -rwxr-xr-x 1 root root 846 Jul 13 2023 cron -rwxr-xr-x 1 root root 681 Jul 13 2023 dbus -rwxr-xr-x 1 root root 1230 Jul 13 2023 ddns-go -rwxr-xr-x 1 root root 35636 Jul 13 2023 dnsmasq -rwxr-xr-x 1 root root 8301 Jul 13 2023 dockerd -rwxr-xr-x 1 root root 3506 Jul 13 2023 dockerman -rwxr-xr-x 1 root root 263 Jul 13 2023 done -rwxr-xr-x 1 root root 6038 Jul 13 2023 dropbear -rwxr-xr-x 1 root root 378 Jul 13 2023 firewall -rwxr-xr-x 1 root root 1713 Jul 13 2023 frpc -rwxr-xr-x 1 root root 1535 Jul 13 2023 gc -rwxr-xr-x 1 root root 1379 Jul 13 2023 gpio_switch -rwxr-xr-x 1 root root 585 Jul 13 2023 haproxy -rwxr-xr-x 1 root root 3640 Jul 13 2023 led -rwxr-xr-x 1 root root 2665 Jul 13 2023 log -rwxr-xr-x 1 root root 1892 Jul 13 2023 microsocks -rwxr-xr-x 1 root root 1260 Jul 13 2023 modemmanager -rwxr-xr-x 1 root root 2706 Jul 13 2023 network -rwxr-xr-x 1 root root 292 Jul 13 2023 odhcpd -rwxr-xr-x 1 root root 1694 Jul 13 2023 openssl -rwxr-xr-x 1 root root 1746 Jul 13 2023 p910nd -rwxr-xr-x 1 root root 321 Jul 13 2023 packet_steering -rwxr-xr-x 1 root root 1245 Jul 13 2023 passwall2 -rwxr-xr-x 1 root root 194 Jul 13 2023 passwall2_server -rwxr-xr-x 1 root root 416 Jul 13 2023 qrtr -rwxr-xr-x 1 root root 764 Jul 13 2023 rmtfs -rwxr-xr-x 1 root root 393 Jul 13 2023 rpcd -rwxr-xr-x 1 root root 1660 Jul 13 2023 rssileds -rwxr-xr-x 1 root root 7996 Jul 13 2023 samba4 -rwxr-xr-x 1 root root 8902 Jul 13 2023 shadowsocks-libev -rwxr-xr-x 1 root root 1095 Jul 13 2023 socat -rwxr-xr-x 1 root root 4793 Jul 13 2023 socat_ser2net -rwxr-xr-x 1 root root 1212 Jul 13 2023 sysctl -rwxr-xr-x 1 root root 764 Jul 13 2023 sysfixtime -rwxr-xr-x 1 root root 3226 Jul 13 2023 sysntpd -rwxr-xr-x 1 root root 1007 Jul 13 2023 system -rwxr-xr-x 1 root root 4035 Jul 13 2023 tasks -rwxr-xr-x 1 root root 2036 Jul 13 2023 ttyd -rwxr-xr-x 1 root root 1339 Jul 13 2023 ucitrack -rwxr-xr-x 1 root root 6056 Jul 13 2023 uhttpd -rwxr-xr-x 1 root root 125 Jul 13 2023 umount -rwxr-xr-x 1 root root 239 Jul 13 2023 urandom_seed -rwxr-xr-x 1 root root 220 Jul 13 2023 urngd -rwxr-xr-x 1 root root 328 Jul 13 2023 usb_printer -rwxr-xr-x 1 root root 1309 Jul 13 2023 v2ray -rwxr-xr-x 1 root root 1262 Jul 13 2023 wpad -rwxr-xr-x 1 root root 2715 Jul 13 2023 wsdd2 -rwxr-xr-x 1 root root 1269 Jul 13 2023 xray -rwxr-xr-x 1 root root 2409 Jul 13 2023 zerotier
/etc/rc.d:启动链接管理文件夹
-
说明如下:
-
实际该系统的
/etc/rc.d
:root@OpenWrt:~# ls /etc/rc.d/ -all drwxr-xr-x 2 root root 4096 Jan 6 22:12 . drwxr-xr-x 36 root root 4096 Jan 6 20:45 .. lrwxrwxrwx 1 root root 23 Jul 13 2023 K100socat_ser2net -> ../init.d/socat_ser2net lrwxrwxrwx 1 root root 21 Jul 13 2023 K10gpio_switch -> ../init.d/gpio_switch lrwxrwxrwx 1 root root 19 Jul 13 2023 K15passwall2 -> ../init.d/passwall2 lrwxrwxrwx 1 root root 14 Jul 13 2023 K21wpad -> ../init.d/wpad lrwxrwxrwx 1 root root 12 Jul 13 2023 K30gc -> ../init.d/gc lrwxrwxrwx 1 root root 18 Jul 13 2023 K50dropbear -> ../init.d/dropbear lrwxrwxrwx 1 root root 14 Jul 13 2023 K50ttyd -> ../init.d/ttyd lrwxrwxrwx 1 root root 17 Jul 13 2023 K80haproxy -> ../init.d/haproxy lrwxrwxrwx 1 root root 16 Jul 13 2023 K85odhcpd -> ../init.d/odhcpd lrwxrwxrwx 1 root root 13 Jul 13 2023 K89log -> ../init.d/log lrwxrwxrwx 1 root root 18 Jul 13 2023 K89rssileds -> ../init.d/rssileds lrwxrwxrwx 1 root root 14 Jul 13 2023 K90boot -> ../init.d/boot lrwxrwxrwx 1 root root 17 Jul 13 2023 K90network -> ../init.d/network lrwxrwxrwx 1 root root 20 Jul 13 2023 K90sysfixtime -> ../init.d/sysfixtime lrwxrwxrwx 1 root root 16 Jul 13 2023 K90umount -> ../init.d/umount lrwxrwxrwx 1 root root 14 Jul 13 2023 K99qrtr -> ../init.d/qrtr lrwxrwxrwx 1 root root 15 Jul 13 2023 K99rmtfs -> ../init.d/rmtfs lrwxrwxrwx 1 root root 15 Jul 13 2023 K99socat -> ../init.d/socat lrwxrwxrwx 1 root root 20 Jul 13 2023 S00sysfixtime -> ../init.d/sysfixtime lrwxrwxrwx 1 root root 15 Jul 13 2023 S00urngd -> ../init.d/urngd lrwxrwxrwx 1 root root 24 Jul 13 2023 S01cgroupfs-mount -> ../init.d/cgroupfs-mount lrwxrwxrwx 1 root root 23 Jul 13 2023 S100socat_ser2net -> ../init.d/socat_ser2net lrwxrwxrwx 1 root root 14 Jul 13 2023 S10boot -> ../init.d/boot lrwxrwxrwx 1 root root 16 Jul 13 2023 S10system -> ../init.d/system lrwxrwxrwx 1 root root 16 Jul 13 2023 S11sysctl -> ../init.d/sysctl lrwxrwxrwx 1 root root 13 Jul 13 2023 S12log -> ../init.d/log lrwxrwxrwx 1 root root 14 Jul 13 2023 S12rpcd -> ../init.d/rpcd lrwxrwxrwx 1 root root 17 Jul 13 2023 S13openssl -> ../init.d/openssl lrwxrwxrwx 1 root root 17 Jul 13 2023 S15cpufreq -> ../init.d/cpufreq lrwxrwxrwx 1 root root 17 Jul 13 2023 S19dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx 1 root root 18 Jul 13 2023 S19dropbear -> ../init.d/dropbear lrwxrwxrwx 1 root root 18 Jul 13 2023 S19firewall -> ../init.d/firewall lrwxrwxrwx 1 root root 14 Jul 13 2023 S19wpad -> ../init.d/wpad lrwxrwxrwx 1 root root 12 Jul 13 2023 S20gc -> ../init.d/gc lrwxrwxrwx 1 root root 17 Jul 13 2023 S20network -> ../init.d/network lrwxrwxrwx 1 root root 25 Jul 13 2023 S25packet_steering -> ../init.d/packet_steering lrwxrwxrwx 1 root root 16 Jul 13 2023 S35odhcpd -> ../init.d/odhcpd lrwxrwxrwx 1 root root 15 Jul 13 2023 S49tasks -> ../init.d/tasks lrwxrwxrwx 1 root root 14 Jul 13 2023 S50cron -> ../init.d/cron lrwxrwxrwx 1 root root 16 Jul 13 2023 S50uhttpd -> ../init.d/uhttpd lrwxrwxrwx 1 root root 14 Jul 13 2023 S60dbus -> ../init.d/dbus lrwxrwxrwx 1 root root 14 Jul 13 2023 S60qrtr -> ../init.d/qrtr lrwxrwxrwx 1 root root 22 Jul 13 2023 S61avahi-daemon -> ../init.d/avahi-daemon lrwxrwxrwx 1 root root 15 Jul 13 2023 S80rmtfs -> ../init.d/rmtfs lrwxrwxrwx 1 root root 18 Jul 13 2023 S80ucitrack -> ../init.d/ucitrack lrwxrwxrwx 1 root root 21 Jul 13 2023 S94gpio_switch -> ../init.d/gpio_switch lrwxrwxrwx 1 root root 14 Jul 13 2023 S95done -> ../init.d/done lrwxrwxrwx 1 root root 20 Jul 13 2023 S95microsocks -> ../init.d/microsocks lrwxrwxrwx 1 root root 13 Jul 13 2023 S96led -> ../init.d/led lrwxrwxrwx 1 root root 18 Jul 13 2023 S96rssileds -> ../init.d/rssileds lrwxrwxrwx 1 root root 17 Jan 1 1970 S98sysntpd -> ../init.d/sysntpd lrwxrwxrwx 1 root root 17 Jul 13 2023 S99ddns-go -> ../init.d/ddns-go lrwxrwxrwx 1 root root 17 Jul 13 2023 S99haproxy -> ../init.d/haproxy lrwxrwxrwx 1 root root 19 Jul 13 2023 S99passwall2 -> ../init.d/passwall2 lrwxrwxrwx 1 root root 26 Jul 13 2023 S99passwall2_server -> ../init.d/passwall2_server lrwxrwxrwx 1 root root 27 Jul 13 2023 S99shadowsocks-libev -> ../init.d/shadowsocks-libev lrwxrwxrwx 1 root root 15 Jul 13 2023 S99socat -> ../init.d/socat lrwxrwxrwx 1 root root 14 Jul 13 2023 S99ttyd -> ../init.d/ttyd lrwxrwxrwx 1 root root 22 Jul 13 2023 S99urandom_seed -> ../init.d/urandom_seed lrwxrwxrwx 1 root root 15 Jul 13 2023 S99v2ray -> ../init.d/v2ray lrwxrwxrwx 1 root root 15 Jul 13 2023 S99wsdd2 -> ../init.d/wsdd2
启动程序源码:/etc/init.d/passwall2
-
root@OpenWrt:~# cat /etc/init.d/passwall2 #!/bin/sh /etc/rc.common START=99 STOP=15 CONFIG=passwall2 APP_FILE=/usr/share/${CONFIG}/app.sh LOCK_FILE_DIR=/var/lock LOCK_FILE=${LOCK_FILE_DIR}/${CONFIG}.lock set_lock() { [ ! -d "$LOCK_FILE_DIR" ] && mkdir -p $LOCK_FILE_DIR exec 999>"$LOCK_FILE" flock -xn 999 } unset_lock() { flock -u 999 rm -rf "$LOCK_FILE" } unlock() { failcount=1 while [ "$failcount" -le 10 ]; do if [ -f "$LOCK_FILE" ]; then let "failcount++" sleep 1s [ "$failcount" -ge 10 ] && unset_lock else break fi done } boot() { local delay=$(uci -q get ${CONFIG}.@global_delay[0].start_delay || echo 1) if [ "$delay" -gt 0 ]; then $APP_FILE echolog "执行启动延时 $delay 秒后再启动!" sleep $delay fi restart touch ${LOCK_FILE_DIR}/${CONFIG}_ready.lock } start() { set_lock [ $? == 1 ] && $APP_FILE echolog "脚本已经在运行,不重复运行,退出." && exit 0 $APP_FILE start unset_lock } stop() { unlock set_lock [ $? == 1 ] && $APP_FILE echolog "停止脚本等待超时,不重复运行,退出." && exit 0 $APP_FILE stop unset_lock } restart() { set_lock [ $? == 1 ] && $APP_FILE echolog "脚本已经在运行,不重复运行,退出." && exit 0 $APP_FILE stop $APP_FILE start unset_lock }
启动程序分析:/etc/init.d/passwall2
- 借助GPT4的解释,如下,较为详细准确:
备份passwall2启动脚本并添加ulimit资源限制
- 关于ulimit:
- 关于ulimit的作用范围:在哪个程序中使用ulimit,限制范围仅在该文件内。
- 因此,我们可以在
/etc/init.d/passwall2
文件的start函数中添加ulimit,来限制passwall进程可以使用的资源。在某个函数中添加ulimit,则只限制该函数中的进程。