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.

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