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.

257 lines
6.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright © 2012 OpenWrt.org
  3. #
  4. # This is free software, licensed under the GNU General Public License v2.
  5. # See /LICENSE for more information.
  6. #
  7. START=50
  8. USERS_C=/var/etc/nut/upsd.users
  9. UPSD_C=/var/etc/nut/upsd.conf
  10. UPS_C=/var/etc/nut/ups.conf
  11. USE_PROCD=1
  12. get_write_driver_config() {
  13. local cfg="$1"
  14. local var="$2"
  15. local def="$3"
  16. local flag="$4"
  17. local val
  18. [ -z "$flag" ] && {
  19. config_get val "$cfg" "$var" "$def"
  20. [ -n "$val" ] && [ "$val" != "0" ] && echo "$var = $val" >>"$UPS_C"
  21. }
  22. [ -n "$flag" ] && {
  23. config_get_bool val "$cfg" "$var" "$def"
  24. [ "$val" = 1 ] && echo "$var" >>"$UPS_C"
  25. }
  26. }
  27. upsd_statepath() {
  28. local cfg="$1"
  29. local statepath
  30. config_get statepath "$cfg" statepath "/var/run/nut"
  31. STATEPATH="$statepath"
  32. }
  33. listen_address() {
  34. local cfg="$1"
  35. config_get address "$cfg" address "::1"
  36. config_get port "$cfg" port
  37. echo "LISTEN $address $port" >>"$UPSD_C"
  38. }
  39. upsd_config() {
  40. local cfg="$1"
  41. local maxage maxconn certfile runas statepath
  42. # Note runas support requires you make sure USB device file is readable by
  43. # the runas user
  44. config_get runas "$cfg" runas "nut"
  45. RUNAS="$runas"
  46. config_get statepath "$cfg" statepath "/var/run/nut"
  47. STATEPATH="$statepath"
  48. config_get maxage "$cfg" maxage
  49. [ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"
  50. config_get statepath "$cfg" statepath
  51. [ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"
  52. config_get maxconn "$cfg" maxconn
  53. [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"
  54. #NOTE: certs only apply to SSL-enabled version
  55. config_get certfile "$cfg" certfile
  56. [ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
  57. }
  58. nut_user_add() {
  59. local cfg="$1"
  60. local a
  61. local val
  62. config_get val "$cfg" username "$1"
  63. echo "[$val]" >> "$USERS_C"
  64. config_get val "$cfg" password
  65. echo " password = $val" >> "$USERS_C"
  66. config_get val "$cfg" actions
  67. for a in $val; do
  68. echo " actions = $a" >> "$USERS_C"
  69. done
  70. instcmd() {
  71. local val="$1"
  72. echo " instcmds = $val" >> "$USERS_C"
  73. }
  74. config_list_foreach "$cfg" instcmd instcmd
  75. config_get val "$cfg" upsmon
  76. if [ -n "$val" ]; then
  77. echo " upsmon $val" >> "$USERS_C"
  78. fi
  79. }
  80. build_server_config() {
  81. mkdir -m 0755 -p "$(dirname "$UPSD_C")"
  82. rm -f "$USERS_C"
  83. rm -f "$UPSD_C"
  84. rm -f /var/etc/nut/nut.conf
  85. echo "# Config file automatically generated from UCI config" > "$USERS_C"
  86. echo "# Config file automatically generated from UCI config" > "$UPSD_C"
  87. config_foreach nut_user_add user
  88. config_foreach listen_address listen_address
  89. config_foreach upsd_config upsd
  90. echo "MODE=netserver" >>/var/etc/nut/nut.conf
  91. chmod 0640 "$USERS_C"
  92. chmod 0640 "$UPSD_C"
  93. chmod 0640 /var/etc/nut/nut.conf
  94. [ -d "${STATEPATH}" ] || {
  95. mkdir -m 0750 -p "${STATEPATH}"
  96. }
  97. if [ -n "$RUNAS" ]; then
  98. chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
  99. chgrp $(id -gn $RUNAS) "$USERS_C"
  100. chgrp $(id -gn $RUNAS) "$UPSD_C"
  101. fi
  102. }
  103. build_driver_config() {
  104. local cfg="$1"
  105. local runas
  106. echo "[$cfg]" >>"$UPS_C"
  107. config_get runas "$cfg" runas "nut"
  108. RUNAS="$runas"
  109. get_write_driver_config "$cfg" driver "usbhid-ups"
  110. get_write_driver_config "$cfg" port "auto"
  111. get_write_driver_config "$cfg" mfr
  112. get_write_driver_config "$cfg" model
  113. get_write_driver_config "$cfg" serial
  114. get_write_driver_config "$cfg" sdtime
  115. get_write_driver_config "$cfg" offdelay 20
  116. get_write_driver_config "$cfg" ondelay 30
  117. get_write_driver_config "$cfg" pollfreq 30
  118. get_write_driver_config "$cfg" vendor
  119. get_write_driver_config "$cfg" product
  120. get_write_driver_config "$cfg" bus
  121. get_write_driver_config "$cfg" interruptonly 0 1
  122. get_write_driver_config "$cfg" interruptsize 0
  123. get_write_driver_config "$cfg" maxreport
  124. get_write_driver_config "$cfg" vendorid
  125. get_write_driver_config "$cfg" productid
  126. get_write_driver_config "$cfg" community
  127. get_write_driver_config "$cfg" snmp_version
  128. get_write_driver_config "$cfg" snmp_retries 0
  129. get_write_driver_config "$cfg" snmp_timeout 0
  130. get_write_driver_config "$cfg" notransferoids 0 1
  131. get_write_driver_config "$cfg" other
  132. echo "" >>$UPS_C
  133. }
  134. build_config() {
  135. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  136. rm -f "$UPS_C"
  137. echo "# Config file automatically generated from UCI config" > "$UPS_C"
  138. chmod 0640 "$UPS_C"
  139. config_load nut_server
  140. config_foreach build_driver_config driver
  141. [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
  142. build_server_config
  143. }
  144. start_driver_instance() {
  145. local cfg="$1"
  146. local requested="$2"
  147. local RUNAS=nut
  148. local driver
  149. # If wanting a specific instance, only start it
  150. [ "$requested" != "$cfg" ] && [ x"$requested" != x"" ] && return 0
  151. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  152. [ ! -s "$UPS_C" ] && build_config
  153. # Avoid hotplug inadvertenly restarting driver during
  154. # forced shutdown
  155. [ -f /var/run/killpower ] && return 0
  156. [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
  157. if [ -n "$RUNAS" ]; then
  158. chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
  159. chown $RUNAS:$(id -gn $RUNAS) "$(dirname "$UPS_C")"
  160. fi
  161. config_get driver "$cfg" driver "usbhid-ups"
  162. procd_open_instance "$cfg"
  163. procd_set_param respawn
  164. procd_set_param stderr 0
  165. procd_set_param stdout 1
  166. procd_set_param command /lib/nut/${driver} -D -a "$cfg" ${RUNAS:+-u $RUNAS}
  167. procd_close_instance
  168. }
  169. start_server_instance() {
  170. local RUNAS STATEPATH
  171. build_config
  172. procd_open_instance "upsd"
  173. procd_set_param respawn
  174. procd_set_param stderr 0
  175. procd_set_param stdout 1
  176. procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u $RUNAS}
  177. procd_close_instance
  178. }
  179. start_service() {
  180. local STATEPATH=/var/run/nut
  181. # Avoid hotplug inadvertenly restarting driver during
  182. # forced shutdown
  183. [ -f /var/run/killpower ] && return 0
  184. [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
  185. config_load nut_server
  186. config_foreach upsd_statepath upsd
  187. [ -d "${STATEPATH}" ] || {
  188. mkdir -m 0750 -p "${STATEPATH}"
  189. }
  190. build_config
  191. config_foreach start_driver_instance driver "$@"
  192. [ "$1" != "upsd" ] && [ x"$1" != x"" ] && return 0
  193. start_server_instance "upsd"
  194. }
  195. reload_service() {
  196. stop
  197. sleep 2
  198. start
  199. }
  200. service_triggers() {
  201. procd_add_reload_trigger "nut_server"
  202. }