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.

167 lines
4.9 KiB

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