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.

145 lines
3.9 KiB

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