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.

173 lines
5.0 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 ] || return 1
  39. local config_dir
  40. config_get config_dir "$cfg" 'config_dir' '/var/etc/transmission'
  41. local user
  42. config_get user "$cfg" 'user'
  43. local group
  44. config_get group "$cfg" 'group'
  45. local download_dir
  46. config_get download_dir "$cfg" 'download_dir' '/var/etc/transmission'
  47. local incomplete_dir
  48. config_get incomplete_dir "$cfg" 'incomplete_dir' '/var/etc/transmission'
  49. local incomplete_dir_enabled
  50. config_get incomplete_dir_enabled "$cfg" 'incomplete_dir_enabled' 0
  51. local mem_percentage
  52. config_get mem_percentage "$cfg" 'mem_percentage' '50'
  53. local config_overwrite
  54. config_get config_overwrite "$cfg" config_overwrite 1
  55. local nice
  56. config_get nice "$cfg" nice 0
  57. local web_home
  58. config_get web_home "$cfg" 'web_home'
  59. local seccomp_path
  60. local MEM
  61. MEM=$(sed -ne 's!^MemTotal:[[:space:]]*\([0-9]*\) kB$!\1!p' /proc/meminfo)
  62. local USE
  63. [ "$MEM" -gt 1 ] && USE=$((MEM * mem_percentage * 10))
  64. config_file="$config_dir/settings.json"
  65. [ -d "$config_dir" ] || {
  66. mkdir -p "$config_dir"
  67. chmod 0755 "$config_dir"
  68. touch "$config_file"
  69. mkdir -p "$config_dir/resume"
  70. mkdir -p "$config_dir/torrents"
  71. mkdir -p "$config_dir/blocklists"
  72. [ -e "$config_dir/stats.json" ] || touch "$config_dir/stats.json"
  73. [ -z "$user" ] || chown -R "$user:$group" "$config_dir"
  74. }
  75. [ -d "$download_dir" ] || {
  76. mkdir -p "$download_dir"
  77. chmod 0755 "$download_dir"
  78. [ -z "$user" ] || chown -R "$user:$group" "$download_dir"
  79. }
  80. [ "$incomplete_dir_enabled" = "0" ] || [ -d "$incomplete_dir" ] || {
  81. mkdir -p "$incomplete_dir"
  82. chmod 0755 "$incomplete_dir"
  83. [ -z "$user" ] || chown -R "$user:$group" "$incomplete_dir"
  84. }
  85. [ "$config_overwrite" = 0 ] || {
  86. echo "{"
  87. append_params "$cfg" \
  88. alt_speed_down alt_speed_enabled alt_speed_time_begin alt_speed_time_day \
  89. alt_speed_time_enabled alt_speed_time_end alt_speed_up blocklist_enabled \
  90. cache_size_mb download_queue_enabled download_queue_size \
  91. dht_enabled encryption idle_seeding_limit idle_seeding_limit_enabled \
  92. incomplete_dir_enabled lazy_bitfield_enabled lpd_enabled message_level \
  93. peer_id_ttl_hours peer_limit_global peer_limit_per_torrent peer_port \
  94. peer_port_random_high peer_port_random_low peer_port_random_on_start \
  95. pex_enabled port_forwarding_enabled preallocation prefetch_enabled \
  96. ratio_limit ratio_limit_enabled rename_partial_files rpc_authentication_required \
  97. rpc_enabled rpc_port rpc_whitelist_enabled queue_stalled_enabled \
  98. queue_stalled_minutes scrape_paused_torrents_enabled script_torrent_done_enabled \
  99. seed_queue_enabled seed_queue_size \
  100. speed_limit_down speed_limit_down_enabled speed_limit_up \
  101. speed_limit_up_enabled start_added_torrents trash_original_torrent_files \
  102. umask upload_slots_per_torrent utp_enabled \
  103. watch_dir_enabled rpc_host_whitelist_enabled
  104. append_params_quotes "$cfg" \
  105. blocklist_url bind_address_ipv4 bind_address_ipv6 download_dir incomplete_dir \
  106. peer_congestion_algorithm peer_socket_tos rpc_bind_address rpc_password rpc_url \
  107. rpc_username rpc_host_whitelist rpc_whitelist script_torrent_done_filename watch_dir
  108. echo "\"invalid-key\": false"
  109. echo "}"
  110. } > "$config_file"
  111. procd_open_instance
  112. procd_set_param command "$PROG"
  113. procd_append_param command -f -g "$config_dir"
  114. procd_set_param user "$user"
  115. procd_set_param group "$group"
  116. procd_set_param nice "$nice"
  117. procd_set_param stderr 1
  118. procd_set_param respawn
  119. seccomp_path="/etc/seccomp/transmission-daemon.json"
  120. if [ -f "$seccomp_path" ]; then
  121. procd_set_param seccomp "$seccomp_path"
  122. fi
  123. if [ -z "$USE" ]; then
  124. procd_set_param limits core="0 0"
  125. else
  126. procd_set_param limits core="0 0" as="$USE $USE"
  127. logger -t transmission "Starting with $USE virt mem"
  128. fi
  129. [ -d "$web_home" ] && procd_set_param env TRANSMISSION_WEB_HOME="$web_home"
  130. procd_add_jail transmission log
  131. procd_add_jail_mount "$config_file"
  132. procd_add_jail_mount_rw "$config_dir/resume"
  133. procd_add_jail_mount_rw "$config_dir/torrents"
  134. procd_add_jail_mount_rw "$config_dir/blocklists"
  135. procd_add_jail_mount_rw "$config_dir/stats.json"
  136. procd_add_jail_mount_rw "$download_dir"
  137. procd_close_instance
  138. }
  139. start_service() {
  140. config_load 'transmission'
  141. config_foreach transmission 'transmission'
  142. }
  143. reload_service() {
  144. procd_send_signal "$PROG"
  145. }
  146. service_triggers() {
  147. procd_add_reload_trigger "transmission"
  148. }