From 069770f5e3e5e65a70828e3d66c38cc3465e053a Mon Sep 17 00:00:00 2001 From: pedro Date: Sat, 22 Oct 2022 20:23:47 +0200 Subject: [PATCH] rc: transmission.c: rewrite transmission FW rules and watchdog script - avoid problems while starting/stopping in the GUI (and also in other cases) - use chain_in_accept as ACCEPT string (for log, if enabled) - remove FW rules if start of transmission daemon failed - some other fixes --- release/src-rt-6.x.4708/router/httpd/tomato.c | 3 +- .../src-rt-6.x.4708/router/others/Makefile | 5 - release/src-rt-6.x.4708/router/others/btcheck | 37 ---- release/src-rt-6.x.4708/router/rc/firewall.c | 20 +-- release/src-rt-6.x.4708/router/rc/rc.h | 1 + release/src-rt-6.x.4708/router/rc/services.c | 2 - .../src-rt-6.x.4708/router/rc/transmission.c | 163 +++++++++++++----- .../src-rt-6.x.4708/router/shared/defaults.c | 1 - .../router/www/nas-bittorrent.asp | 10 +- 9 files changed, 133 insertions(+), 109 deletions(-) delete mode 100755 release/src-rt-6.x.4708/router/others/btcheck diff --git a/release/src-rt-6.x.4708/router/httpd/tomato.c b/release/src-rt-6.x.4708/router/httpd/tomato.c index 516860f909..1b134420ef 100644 --- a/release/src-rt-6.x.4708/router/httpd/tomato.c +++ b/release/src-rt-6.x.4708/router/httpd/tomato.c @@ -1318,8 +1318,7 @@ static const nvset_t nvset_list[] = { { "bt_blocklist", V_01 }, { "bt_blocklist_url", V_LENGTH(0, 80) }, { "bt_sleep", V_RANGE(1, 60) }, - { "bt_check", V_01 }, - { "bt_check_time", V_RANGE(1, 55) }, + { "bt_check_time", V_RANGE(0, 55) }, { "bt_dl_queue_enable", V_01 }, { "bt_dl_queue_size", V_RANGE(1, 30) }, { "bt_ul_queue_enable", V_01 }, diff --git a/release/src-rt-6.x.4708/router/others/Makefile b/release/src-rt-6.x.4708/router/others/Makefile index 57ba588add..0a10fdee76 100644 --- a/release/src-rt-6.x.4708/router/others/Makefile +++ b/release/src-rt-6.x.4708/router/others/Makefile @@ -142,11 +142,6 @@ endif # system info install -m 0700 sysinfo $(INSTALLDIR)/usr/sbin -# BTGUI -ifeq ($(TCONFIG_BT),y) - install -m 0755 btcheck $(INSTALLDIR)/usr/bin -endif - # network discovery script for status-devices page ifeq ($(or $(TCONFIG_BCMARM),$(TCONFIG_MIPSR2)),y) install -m 0755 discovery.sh $(INSTALLDIR)/usr/sbin diff --git a/release/src-rt-6.x.4708/router/others/btcheck b/release/src-rt-6.x.4708/router/others/btcheck deleted file mode 100755 index c616b6359e..0000000000 --- a/release/src-rt-6.x.4708/router/others/btcheck +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/sh - -# Script for checking/adding transmission to cron - -BTON=$(nvram get bt_enable) -BTCH=$(nvram get bt_check) - -case "$1" in - addcru) - ISCRU=$(cru l | grep btinside | wc -l) - INTERVAL=$(nvram get bt_check_time) - - [ "$BTON" -eq 1 ] && { - [ "$BTCH" -eq 1 ] && { - [ "$ISCRU" -eq 0 ] && { - cru a btinside "*/$INTERVAL * * * * /usr/bin/btcheck check" - } || { - cru d btinside - cru a btinside "*/$INTERVAL * * * * /usr/bin/btcheck check" - } - } || { - [ "$ISCRU" -eq 1 ] && cru d btinside - } - } || { - [ "$ISCRU" -eq 1 ] && cru d btinside - } - ;; - check) - [ "$BTON" -eq 1 -a "$BTCH" -eq 1 -a "$(nvram get g_upgrade)" != "1" -a "$(nvram get g_reboot)" != "1" ] && { - pidof transmission-daemon >/dev/null || { - logger -t btcheck "Transmission stopped? Starting..." - service bittorrent restart - } - } - ;; -esac -exit 0 diff --git a/release/src-rt-6.x.4708/router/rc/firewall.c b/release/src-rt-6.x.4708/router/rc/firewall.c index 6911d4a517..cb488d3a3b 100644 --- a/release/src-rt-6.x.4708/router/rc/firewall.c +++ b/release/src-rt-6.x.4708/router/rc/firewall.c @@ -1048,12 +1048,6 @@ static void nat_table(void) ipt_write("-A WANPREROUTING -p tcp --dport %s -j DNAT --to-destination %s\n", nvram_safe_get("ftp_port"), lanaddr[0]); #endif -#ifdef TCONFIG_BT - /* BT Client ports from WAN interface */ - if (nvram_get_int("bt_enable") && nvram_match("bt_rpc_wan", "1")) - ipt_write("-A WANPREROUTING -p tcp --dport %s -j DNAT --to-destination %s\n", nvram_safe_get("bt_port_gui"), lanaddr[0]); -#endif - if (wanup || wan2up #ifdef TCONFIG_MULTIWAN || wan3up || wan4up @@ -1334,15 +1328,6 @@ static void filter_input(void) if (nvram_invmatch("dr_wan_rx", "0")) ipt_write("-A INPUT -p udp --dport 520 -j ACCEPT\n"); -#ifdef TCONFIG_BT - /* BT Client ports from WAN interface */ - if (nvram_match("bt_enable", "1")) { - ipt_write("-A INPUT -p tcp --dport %s -j ACCEPT\n", nvram_safe_get("bt_port")); - if (nvram_match("bt_rpc_wan", "1")) - ipt_write("-A INPUT -p tcp --dport %s -j ACCEPT\n", nvram_safe_get("bt_port_gui")); - } -#endif - #ifdef TCONFIG_PPTPD /* Add for pptp server */ if (nvram_match("pptpd_enable", "1")) @@ -2230,6 +2215,11 @@ int start_firewall(void) run_nginx_firewall_script(); #endif +#ifdef TCONFIG_BT + /* Open BT port/GUI WAN access */ + run_bt_firewall_script(); +#endif + #ifdef TCONFIG_OPENVPN run_ovpn_firewall_scripts(); #endif diff --git a/release/src-rt-6.x.4708/router/rc/rc.h b/release/src-rt-6.x.4708/router/rc/rc.h index bddaa05697..d0e4150fc1 100644 --- a/release/src-rt-6.x.4708/router/rc/rc.h +++ b/release/src-rt-6.x.4708/router/rc/rc.h @@ -522,6 +522,7 @@ extern int nvram_nvram2file(const char *name, const char *filename); #ifdef TCONFIG_BT extern void start_bittorrent(int force); extern void stop_bittorrent(void); +extern void run_bt_firewall_script(void); #endif /* nfs.c */ diff --git a/release/src-rt-6.x.4708/router/rc/services.c b/release/src-rt-6.x.4708/router/rc/services.c index 0b47fdf04d..0c8aa1467c 100644 --- a/release/src-rt-6.x.4708/router/rc/services.c +++ b/release/src-rt-6.x.4708/router/rc/services.c @@ -3567,8 +3567,6 @@ TOP: #ifdef TCONFIG_BT if ((strcmp(service, "bittorrent") == 0) || (strcmp(service, "transmission") == 0) || (strcmp(service, "transmission_da") == 0)) { if (act_stop) stop_bittorrent(); - stop_firewall(); - start_firewall(); /* always restarted */ if (act_start) start_bittorrent(1); /* force (re)start */ goto CLEAR; } diff --git a/release/src-rt-6.x.4708/router/rc/transmission.c b/release/src-rt-6.x.4708/router/rc/transmission.c index 7ea10fc987..c88c9804f1 100644 --- a/release/src-rt-6.x.4708/router/rc/transmission.c +++ b/release/src-rt-6.x.4708/router/rc/transmission.c @@ -14,15 +14,68 @@ #define tr_dir "/etc/transmission" #define tr_settings tr_dir"/settings.json" +#define tr_fw_script tr_dir"/tr-fw.sh" +#define tr_fw_del_script tr_dir"/tr-clear-fw-tmp.sh" +#define tr_child_pid tr_dir"/child.pid" /* needed by logmsg() */ #define LOGMSG_DISABLE DISABLE_SYSLOG_OSM #define LOGMSG_NVDEBUG "transmission_debug" -static int rmem_max = 0; -static int wmem_max = 0; -static pid_t pidof_child = 0; +static void setup_tr_watchdog(void) +{ + FILE *fp; + char buffer[64], buffer2[64]; + int nvi; + + if ((nvi = nvram_get_int("bt_check_time")) > 0) { + memset(buffer, 0, sizeof(buffer)); + snprintf(buffer, sizeof(buffer), tr_dir"/watchdog.sh"); + + if ((fp = fopen(buffer, "w"))) { + fprintf(fp, "#!/bin/sh\n" + "[ -z \"$(pidof transmission-daemon)\" -a \"$(nvram get g_upgrade)\" != \"1\" -a \"$(nvram get g_reboot)\" != \"1\" ] && {\n" + " logger -t transmission-watchdog transmission-daemon stopped? Starting...\n" + " service bittorrent restart\n" + "}\n"); + fclose(fp); + chmod(buffer, (S_IRUSR | S_IWUSR | S_IXUSR)); + + memset(buffer2, 0, sizeof(buffer2)); + snprintf(buffer2, sizeof(buffer2), "*/%d * * * * %s", nvi, buffer); + eval("cru", "a", "CheckTransmission", buffer2); + } + } +} + +static void build_tr_firewall(void) +{ + FILE *p; + + /* create firewall script */ + if (!(p = fopen(tr_fw_script, "w"))) { + perror(tr_fw_script); + return; + } + + chains_log_detection(); + + /* open BT port */ + fprintf(p, "#!/bin/sh\n" + "iptables -A INPUT -p tcp --dport %s -j %s\n", + nvram_safe_get("bt_port"), chain_in_accept); + + /* GUI WAN access */ + if (nvram_get_int("bt_rpc_wan")) + fprintf(p, "iptables -A INPUT -p tcp --dport %s -j %s\n" + "iptables -t nat -A WANPREROUTING -p tcp --dport %s -j DNAT --to-destination %s\n", /* nat table */ + nvram_safe_get("bt_port_gui"), chain_in_accept, + nvram_safe_get("bt_port_gui"), nvram_safe_get("lan_ipaddr")); + + fclose(p); + chmod(tr_fw_script, 0744); +} void start_bittorrent(int force) { @@ -31,6 +84,7 @@ void start_bittorrent(int force) char *whitelistEnabled; char buf[256], buf2[64]; int n; + pid_t pidof_child = 0; /* only if enabled or forced */ if (!nvram_get_int("bt_enable") && force == 0) @@ -39,8 +93,9 @@ void start_bittorrent(int force) if (serialize_restart("transmission-da", 1)) return; - if (pidof_child > 0) { /* fork is still up */ - logmsg(LOG_WARNING, "*** %s: another process (PID: %d) still up, aborting ...", __FUNCTION__, pidof_child); + memset(buf2, 0, sizeof(buf2)); + if (f_read_string(tr_child_pid, buf2, sizeof(buf2)) > 0 && atoi(buf2) > 0 && ppid(atoi(buf2)) > 0) { /* fork is still up */ + logmsg(LOG_WARNING, "%s: another process (PID: %s) still up, aborting ...", __FUNCTION__, buf2); return; } @@ -68,7 +123,7 @@ void start_bittorrent(int force) if (nvram_match("bt_binary", "internal")) { pn = "/usr/bin"; } else if (nvram_match("bt_binary", "optware") ) { pn = "/opt/bin"; } - else { pn = nvram_safe_get( "bt_binary_custom"); } + else { pn = nvram_safe_get("bt_binary_custom"); } if (nvram_get_int("bt_auth")) { pl = "true"; @@ -169,21 +224,8 @@ void start_bittorrent(int force) chmod(tr_settings, 0644); - /* backup original buffers values */ - if (rmem_max == 0) { - memset(buf, 0, sizeof(buf)); - f_read_string("/proc/sys/net/core/rmem_max", buf, sizeof(buf)); - rmem_max = atoi(buf); - } - if (wmem_max == 0) { - memset(buf, 0, sizeof(buf)); - f_read_string("/proc/sys/net/core/wmem_max", buf, sizeof(buf)); - wmem_max = atoi(buf); - } - - /* tune buffers */ - f_write_procsysnet("core/rmem_max", "4194304"); - f_write_procsysnet("core/wmem_max", "2080768"); + /* create firewall script */ + build_tr_firewall(); /* fork new process */ if (fork() != 0) @@ -191,6 +233,15 @@ void start_bittorrent(int force) pidof_child = getpid(); + /* write child pid to a file */ + memset(buf2, 0, sizeof(buf2)); + snprintf(buf2, sizeof(buf2), "%d", pidof_child); + f_write_string(tr_child_pid, buf2, 0, 0); + + /* tune buffers */ + f_write_procsysnet("core/rmem_max", "4194304"); + f_write_procsysnet("core/wmem_max", "2080768"); + /* wait a given time for partition to be mounted, etc */ n = atoi(nvram_safe_get("bt_sleep")); if (n > 0) @@ -232,6 +283,8 @@ void start_bittorrent(int force) system(buf); } + run_bt_firewall_script(); + memset(buf2, 0, sizeof(buf2)); if (nvram_get_int("bt_log")) snprintf(buf2, sizeof(buf2), "-e %s/transmission.log", nvram_safe_get("bt_log_path")); @@ -245,15 +298,17 @@ void start_bittorrent(int force) system(buf); sleep(1); - if (pidof("transmission-da") > 0) + if (pidof("transmission-da") > 0) { logmsg(LOG_INFO, "transmission-daemon started"); - else - logmsg(LOG_ERR, "starting transmission-daemon failed ..."); - - sleep(2); - eval("/usr/bin/btcheck", "addcru"); - - pidof_child = 0; /* reset pid */ + sleep(2); + setup_tr_watchdog(); + f_write_string(tr_child_pid, "0", 0, 0); + } + else { + logmsg(LOG_ERR, "starting transmission-daemon failed - check configuration ..."); + f_write_string(tr_child_pid, "0", 0, 0); + stop_bittorrent(); + } /* terminate the child */ exit(0); @@ -263,19 +318,28 @@ void stop_bittorrent(void) { pid_t pid; char buf[16]; - int n = 10; + int n = 10, m = atoi(nvram_safe_get("bt_sleep")) + 10; if (serialize_restart("transmission-da", 0)) return; + /* wait for child of start_bittorrent to finish (if any) */ + memset(buf, 0, sizeof(buf)); + while (f_read_string(tr_child_pid, buf, sizeof(buf)) > 0 && atoi(buf) > 0 && ppid(atoi(buf)) > 0 && (m-- > 0)) { + logmsg(LOG_DEBUG, "*** %s: waiting for child process of start_bittorrent to end, %d secs left ...", __FUNCTION__, m); + sleep(1); + } + + eval("cru", "d", "CheckTransmission"); + if (pidof("transmission-da") > 0) { - logmsg(LOG_INFO, "Terminating transmission-daemon ..."); + logmsg(LOG_INFO, "terminating transmission-daemon ..."); killall_tk_period_wait("transmission-da", 50); sleep(1); while ((pid = pidof("transmission-da")) > 0 && (n-- > 0)) { - logmsg(LOG_WARNING, "Killing transmission-daemon ..."); - /* Reap the zombie if it has terminated */ + logmsg(LOG_WARNING, "killing transmission-daemon ..."); + /* reap the zombie if it has terminated */ waitpid(pid, NULL, WNOHANG); sleep(1); } @@ -283,16 +347,35 @@ void stop_bittorrent(void) if (n < 10) logmsg(LOG_WARNING, "transmission-daemon forcefully stopped"); else - logmsg(LOG_INFO, "transmission-daemon successfully stopped"); + logmsg(LOG_INFO, "transmission-daemon stopped"); } - /* restore buffers */ + run_del_firewall_script(tr_fw_script, tr_fw_del_script); + + /* restore default buffers */ memset(buf, 0, sizeof(buf)); - snprintf(buf, sizeof(buf), "%d", rmem_max); - f_write_procsysnet("core/rmem_max", buf); + if (f_read_string("/proc/sys/net/core/rmem_default", buf, sizeof(buf)) > 0 && atoi(buf) > 0); + f_write_procsysnet("core/rmem_max", buf); + memset(buf, 0, sizeof(buf)); - snprintf(buf, sizeof(buf), "%d", wmem_max); - f_write_procsysnet("core/wmem_max", buf); + if (f_read_string("/proc/sys/net/core/wmem_default", buf, sizeof(buf)) > 0 && atoi(buf) > 0); + f_write_procsysnet("core/wmem_max", buf); + + /* clean-up */ + system("/bin/rm -rf "tr_dir); +} + +void run_bt_firewall_script(void) +{ + FILE *fp; + + /* first remove existing firewall rule(s) */ + run_del_firewall_script(tr_fw_script, tr_fw_del_script); - eval("/usr/bin/btcheck", "addcru"); + /* then (re-)add firewall rule(s) */ + if ((fp = fopen(tr_fw_script, "r"))) { + fclose(fp); + logmsg(LOG_DEBUG, "*** %s: running firewall script: %s", __FUNCTION__, tr_fw_script); + eval(tr_fw_script); + } } diff --git a/release/src-rt-6.x.4708/router/shared/defaults.c b/release/src-rt-6.x.4708/router/shared/defaults.c index 13b557b66b..3af2694f17 100644 --- a/release/src-rt-6.x.4708/router/shared/defaults.c +++ b/release/src-rt-6.x.4708/router/shared/defaults.c @@ -1473,7 +1473,6 @@ struct nvram_tuple router_defaults[] = { { "bt_blocklist", "0" , 0 }, { "bt_blocklist_url", "http://list.iblocklist.com/?list=bt_level1" , 0 }, { "bt_sleep", "10" , 0 }, - { "bt_check", "1" , 0 }, { "bt_check_time", "15" , 0 }, { "bt_dl_queue_enable", "0" , 0 }, { "bt_dl_queue_size", "5" , 0 }, diff --git a/release/src-rt-6.x.4708/router/www/nas-bittorrent.asp b/release/src-rt-6.x.4708/router/www/nas-bittorrent.asp index 4fbc7a97c7..f9a2758482 100644 --- a/release/src-rt-6.x.4708/router/www/nas-bittorrent.asp +++ b/release/src-rt-6.x.4708/router/www/nas-bittorrent.asp @@ -19,7 +19,7 @@