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.

322 lines
7.6 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. upsd_runas() {
  34. local cfg="$1"
  35. local runas
  36. [ -n "$RUNAS" ] && return
  37. config_get runas "$cfg" runas "nut"
  38. RUNAS="$runas"
  39. }
  40. listen_address() {
  41. local cfg="$1"
  42. config_get address "$cfg" address "::1"
  43. config_get port "$cfg" port
  44. echo "LISTEN $address $port" >>"$UPSD_C"
  45. }
  46. upsd_config() {
  47. local cfg="$1"
  48. local maxage maxconn certfile runas statepath
  49. # Note runas support requires you make sure USB device file is readable by
  50. # the runas user
  51. config_get runas "$cfg" runas "nut"
  52. RUNAS="$runas"
  53. config_get statepath "$cfg" statepath "/var/run/nut"
  54. STATEPATH="$statepath"
  55. config_get maxage "$cfg" maxage
  56. [ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"
  57. [ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"
  58. config_get maxconn "$cfg" maxconn
  59. [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"
  60. #NOTE: certs only apply to SSL-enabled version
  61. config_get certfile "$cfg" certfile
  62. [ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
  63. }
  64. nut_user_add() {
  65. local cfg="$1"
  66. local a
  67. local val
  68. config_get val "$cfg" username "$1"
  69. echo "[$val]" >> "$USERS_C"
  70. config_get val "$cfg" password
  71. echo " password = $val" >> "$USERS_C"
  72. config_get val "$cfg" actions
  73. for a in $val; do
  74. echo " actions = $a" >> "$USERS_C"
  75. done
  76. instcmd() {
  77. local val="$1"
  78. echo " instcmds = $val" >> "$USERS_C"
  79. }
  80. config_list_foreach "$cfg" instcmd instcmd
  81. config_get val "$cfg" upsmon
  82. if [ -n "$val" ]; then
  83. echo " upsmon $val" >> "$USERS_C"
  84. fi
  85. }
  86. build_server_config() {
  87. mkdir -m 0755 -p "$(dirname "$UPSD_C")"
  88. rm -f "$USERS_C"
  89. rm -f "$UPSD_C"
  90. rm -f /var/etc/nut/nut.conf
  91. echo "# Config file automatically generated from UCI config" > "$USERS_C"
  92. echo "# Config file automatically generated from UCI config" > "$UPSD_C"
  93. config_foreach nut_user_add user
  94. config_foreach listen_address listen_address
  95. config_foreach upsd_config upsd
  96. echo "MODE=netserver" >>/var/etc/nut/nut.conf
  97. chmod 0640 "$USERS_C"
  98. chmod 0640 "$UPSD_C"
  99. chmod 0644 /var/etc/nut/nut.conf
  100. [ -d "${STATEPATH}" ] || {
  101. mkdir -m 0750 -p "${STATEPATH}"
  102. }
  103. if [ -n "$RUNAS" ]; then
  104. chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
  105. chgrp $(id -gn $RUNAS) "$USERS_C"
  106. chgrp $(id -gn $RUNAS) "$UPSD_C"
  107. fi
  108. haveserver=1
  109. }
  110. build_driver_config() {
  111. local cfg="$1"
  112. echo "[$cfg]" >>"$UPS_C"
  113. get_write_driver_config "$cfg" bus
  114. get_write_driver_config "$cfg" community
  115. get_write_driver_config "$cfg" desc
  116. get_write_driver_config "$cfg" driver "usbhid-ups"
  117. get_write_driver_config "$cfg" ignorelb 0 1
  118. get_write_driver_config "$cfg" interruptonly 0 1
  119. get_write_driver_config "$cfg" interruptsize
  120. get_write_driver_config "$cfg" maxreport
  121. get_write_driver_config "$cfg" maxstartdelay
  122. get_write_driver_config "$cfg" mfr
  123. get_write_driver_config "$cfg" model
  124. get_write_driver_config "$cfg" nolock 0 1
  125. get_write_driver_config "$cfg" notransferoids 0 1
  126. get_write_driver_config "$cfg" offdelay
  127. get_write_driver_config "$cfg" ondelay
  128. get_write_driver_config "$cfg" pollfreq
  129. get_write_driver_config "$cfg" port "auto"
  130. get_write_driver_config "$cfg" product
  131. get_write_driver_config "$cfg" productid
  132. get_write_driver_config "$cfg" retrydelay
  133. get_write_driver_config "$cfg" sdorder
  134. get_write_driver_config "$cfg" sdtime
  135. get_write_driver_config "$cfg" serial
  136. get_write_driver_config "$cfg" snmp_version
  137. get_write_driver_config "$cfg" snmp_retries
  138. get_write_driver_config "$cfg" snmp_timeout
  139. get_write_driver_config "$cfg" synchronous
  140. get_write_driver_config "$cfg" vendor
  141. get_write_driver_config "$cfg" vendorid
  142. defoverride() {
  143. local overvar="$1"
  144. local defover="$2"
  145. local overtype="$(echo "$overvar" | tr '.' '_')"
  146. config_get overval "${defover}_${overtype}" value
  147. [ -n "$overval" ] && echo "${defover}.${overvar} = $overval" >>"$UPS_C"
  148. }
  149. config_list_foreach "$cfg" override defoverride override
  150. config_list_foreach "$cfg" default defoverride default
  151. other() {
  152. local othervar="$1"
  153. local othervarflag="$2"
  154. if [ "$othervarflag" = "otherflag" ]; then
  155. config_get_bool otherval "${othervarflag}_${othervar}" value
  156. [ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C"
  157. else
  158. config_get otherval "${othervarflag}_${othervar}" value
  159. [ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C"
  160. fi
  161. }
  162. config_list_foreach "$cfg" other other
  163. config_list_foreach "$cfg" other otherflag
  164. echo "" >>$UPS_C
  165. havedriver=1
  166. }
  167. build_global_driver_config() {
  168. local cfg="$1"
  169. # Global driver config
  170. get_write_driver_config "$cfg" chroot
  171. get_write_driver_config "$cfg" driverpath
  172. get_write_driver_config "$cfg" maxstartdelay
  173. get_write_driver_config "$cfg" maxretry
  174. get_write_driver_config "$cfg" retrydelay
  175. get_write_driver_config "$cfg" pollinterval
  176. get_write_driver_config "$cfg" synchronous
  177. config_get runas "$cfg" user "nut"
  178. RUNAS="$runas"
  179. upsd_runas
  180. echo "" >>$UPS_C
  181. }
  182. build_config() {
  183. local STATEPATH=/var/run/nut
  184. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  185. rm -f "$UPS_C"
  186. echo "# Config file automatically generated from UCI config" > "$UPS_C"
  187. chmod 0640 "$UPS_C"
  188. config_load nut_server
  189. config_foreach upsd_statepath upsd
  190. config_foreach build_global_driver_config driver_global
  191. config_foreach build_driver_config driver
  192. [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
  193. build_server_config
  194. }
  195. start_driver_instance() {
  196. local cfg="$1"
  197. local requested="$2"
  198. local driver
  199. local STATEPATH=/var/run/nut
  200. local RUNAS=nut
  201. [ "$havedriver" != 1 ] && return
  202. # If wanting a specific instance, only start it
  203. [ "$requested" != "$cfg" ] && [ x"$requested" != x ] && return 0
  204. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  205. [ ! -s "$UPS_C" ] && build_config
  206. # Avoid hotplug inadvertenly restarting driver during
  207. # forced shutdown
  208. [ -f /var/run/killpower ] && return 0
  209. [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
  210. config_foreach upsd_statepath upsd
  211. if [ -n "$RUNAS" ]; then
  212. chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
  213. fi
  214. config_get driver "$cfg" driver "usbhid-ups"
  215. procd_open_instance "$cfg"
  216. procd_set_param respawn
  217. procd_set_param stderr 0
  218. procd_set_param stdout 1
  219. procd_set_param command /lib/nut/${driver} -D -a "$cfg" ${RUNAS:+-u $RUNAS}
  220. procd_close_instance
  221. }
  222. start_server_instance() {
  223. local RUNAS=nut
  224. build_config
  225. [ "$haveserver" != 1 ] && return
  226. procd_open_instance "upsd"
  227. procd_set_param respawn
  228. procd_set_param stderr 0
  229. procd_set_param stdout 1
  230. procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u $RUNAS}
  231. procd_close_instance
  232. }
  233. start_service() {
  234. local havedriver haveserver
  235. local STATEPATH=/var/run/nut
  236. # Avoid hotplug inadvertenly restarting driver during
  237. # forced shutdown
  238. [ -f /var/run/killpower ] && return 0
  239. [ -f /var/run/nut/disable-hotplug ] && return 0
  240. config_load nut_server
  241. build_config
  242. config_foreach start_driver_instance driver "$@"
  243. start_server_instance "upsd"
  244. }
  245. reload_service() {
  246. stop
  247. sleep 2
  248. local havedriver haveserver
  249. start
  250. }
  251. service_triggers() {
  252. procd_add_reload_trigger "nut_server"
  253. }