You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.2 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010-2015 OpenWrt.org
  3. START=99
  4. STOP=10
  5. USE_PROCD=1
  6. PROG="/usr/bin/transmission-daemon"
  7. LIST_SEP="
  8. "
  9. append_params() {
  10. local p; local v; local s="$1"; shift
  11. IFS="$LIST_SEP"
  12. for p in "$@"; do
  13. config_get v "$s" "$p"
  14. for v in $v; do
  15. [ -n "$v" ] && echo "\"$p\": $v," | sed -e 's|_|-|g'
  16. done
  17. done
  18. unset IFS
  19. }
  20. append_params_quotes() {
  21. local p; local v; local s="$1"; shift
  22. IFS="$LIST_SEP"
  23. for p in "$@"; do
  24. config_get v "$s" "$p"
  25. for v in $v; do
  26. [ -n "$v" ] && {
  27. printf "\"%s" "$p" | sed -e 's|/|\\/|g;s|_|-|g'; \
  28. echo "\": \"$v\","
  29. }
  30. done
  31. done
  32. unset IFS
  33. }
  34. transmission() {
  35. local cfg="$1"
  36. local enabled
  37. config_get_bool enabled "$cfg" enabled 0
  38. [ "$enabled" -gt 0 ] || {
  39. echo "Transmission not enabled. Please enable in /etc/config/transmission"
  40. logger -t "transmission" -p "daemon.info" "Transmission not enabled. Please enable in /etc/config/transmission"
  41. return 1
  42. }
  43. local config_dir
  44. config_get config_dir "$cfg" 'config_dir' '/var/etc/transmission'
  45. local user
  46. config_get user "$cfg" 'user'
  47. local group
  48. config_get group "$cfg" 'group'
  49. local download_dir
  50. config_get download_dir "$cfg" 'download_dir' '/var/etc/transmission'
  51. local incomplete_dir
  52. config_get incomplete_dir "$cfg" 'incomplete_dir' '/var/etc/transmission'
  53. local incomplete_dir_enabled
  54. config_get incomplete_dir_enabled "$cfg" 'incomplete_dir_enabled' 0
  55. local mem_percentage
  56. config_get mem_percentage "$cfg" 'mem_percentage' '50'
  57. local config_overwrite
  58. config_get config_overwrite "$cfg" config_overwrite 1
  59. local nice
  60. config_get nice "$cfg" nice 0
  61. local web_home
  62. config_get web_home "$cfg" 'web_home'
  63. local seccomp_path
  64. local MEM
  65. MEM=$(sed -ne 's!^MemTotal:[[:space:]]*\([0-9]*\) kB$!\1!p' /proc/meminfo)
  66. local USE
  67. [ "$MEM" -gt 1 ] && USE=$((MEM * mem_percentage * 10))
  68. config_file="$config_dir/settings.json"
  69. [ -d "$config_dir" ] || {
  70. mkdir -p "$config_dir"
  71. chmod 0755 "$config_dir"
  72. touch "$config_file"
  73. mkdir -p "$config_dir/resume"
  74. mkdir -p "$config_dir/torrents"
  75. mkdir -p "$config_dir/blocklists"
  76. [ -e "$config_dir/stats.json" ] || touch "$config_dir/stats.json"
  77. [ -z "$user" ] || chown -R "$user:$group" "$config_dir"
  78. }
  79. [ -d "$download_dir" ] || {
  80. mkdir -p "$download_dir"
  81. chmod 0755 "$download_dir"
  82. [ -z "$user" ] || chown -R "$user:$group" "$download_dir"
  83. }
  84. [ "$incomplete_dir_enabled" = "0" ] || [ -d "$incomplete_dir" ] || {
  85. mkdir -p "$incomplete_dir"
  86. chmod 0755 "$incomplete_dir"
  87. [ -z "$user" ] || chown -R "$user:$group" "$incomplete_dir"
  88. }
  89. [ "$config_overwrite" = 0 ] || {
  90. echo "{"
  91. append_params "$cfg" \
  92. alt_speed_down alt_speed_enabled alt_speed_time_begin alt_speed_time_day \
  93. alt_speed_time_enabled alt_speed_time_end alt_speed_up blocklist_enabled \
  94. cache_size_mb download_queue_enabled download_queue_size \
  95. dht_enabled encryption idle_seeding_limit idle_seeding_limit_enabled \
  96. incomplete_dir_enabled lazy_bitfield_enabled lpd_enabled message_level \
  97. peer_id_ttl_hours peer_limit_global peer_limit_per_torrent peer_port \
  98. peer_port_random_high peer_port_random_low peer_port_random_on_start \
  99. pex_enabled port_forwarding_enabled preallocation prefetch_enabled \
  100. ratio_limit ratio_limit_enabled rename_partial_files rpc_authentication_required \
  101. rpc_enabled rpc_port rpc_whitelist_enabled queue_stalled_enabled \
  102. queue_stalled_minutes scrape_paused_torrents_enabled script_torrent_done_enabled \
  103. seed_queue_enabled seed_queue_size \
  104. speed_limit_down speed_limit_down_enabled speed_limit_up \
  105. speed_limit_up_enabled start_added_torrents trash_original_torrent_files \
  106. umask upload_slots_per_torrent utp_enabled \
  107. watch_dir_enabled rpc_host_whitelist_enabled
  108. append_params_quotes "$cfg" \
  109. blocklist_url bind_address_ipv4 bind_address_ipv6 download_dir incomplete_dir \
  110. peer_congestion_algorithm peer_socket_tos rpc_bind_address rpc_password rpc_url \
  111. rpc_username rpc_host_whitelist rpc_whitelist script_torrent_done_filename watch_dir
  112. echo "\"invalid-key\": false"
  113. echo "}"
  114. } > "$config_file"
  115. procd_open_instance
  116. procd_set_param command "$PROG"
  117. procd_append_param command -f -g "$config_dir"
  118. procd_set_param user "$user"
  119. procd_set_param group "$group"
  120. procd_set_param nice "$nice"
  121. procd_set_param stderr 1
  122. procd_set_param respawn
  123. seccomp_path="/etc/seccomp/transmission-daemon.json"
  124. if [ -f "$seccomp_path" ]; then
  125. procd_set_param seccomp "$seccomp_path"
  126. fi
  127. if [ -z "$USE" ]; then
  128. procd_set_param limits core="0 0"
  129. else
  130. procd_set_param limits core="0 0" as="$USE $USE"
  131. logger -t transmission "Starting with $USE virt mem"
  132. fi
  133. [ -d "$web_home" ] && procd_set_param env TRANSMISSION_WEB_HOME="$web_home"
  134. procd_add_jail transmission log
  135. procd_add_jail_mount "$config_file"
  136. procd_add_jail_mount_rw "$config_dir/resume"
  137. procd_add_jail_mount_rw "$config_dir/torrents"
  138. procd_add_jail_mount_rw "$config_dir/blocklists"
  139. procd_add_jail_mount_rw "$config_dir/stats.json"
  140. procd_add_jail_mount_rw "$download_dir"
  141. [ -d "$web_home" ] && procd_add_jail_mount_rw "$web_home"
  142. procd_close_instance
  143. }
  144. start_service() {
  145. config_load 'transmission'
  146. config_foreach transmission 'transmission'
  147. }
  148. reload_service() {
  149. procd_send_signal "$PROG"
  150. }
  151. service_triggers() {
  152. procd_add_reload_trigger "transmission"
  153. }