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.

363 lines
8.5 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=70
  8. STOP=30
  9. USERS_C=/var/etc/nut/upsd.users
  10. UPSD_C=/var/etc/nut/upsd.conf
  11. UPS_C=/var/etc/nut/ups.conf
  12. USE_PROCD=1
  13. get_write_driver_config() {
  14. local cfg="$1"
  15. local var="$2"
  16. local def="$3"
  17. local flag="$4"
  18. local val
  19. [ -z "$flag" ] && {
  20. config_get val "$cfg" "$var" "$def"
  21. [ -n "$val" ] && [ "$val" != "0" ] && echo "$var = $val" >>"$UPS_C"
  22. }
  23. [ -n "$flag" ] && {
  24. config_get_bool val "$cfg" "$var" "$def"
  25. [ "$val" = 1 ] && echo "$var" >>"$UPS_C"
  26. }
  27. }
  28. upsd_statepath() {
  29. local statepath
  30. config_get statepath upsd statepath /var/run/nut
  31. STATEPATH="$statepath"
  32. }
  33. upsd_runas() {
  34. local runas
  35. [ -n "$RUNAS" ] && return 0
  36. config_get runas upsd runas nut
  37. RUNAS="$runas"
  38. }
  39. listen_address() {
  40. local cfg="$1"
  41. config_get address "$cfg" address "::1"
  42. config_get port "$cfg" port
  43. echo "LISTEN $address $port" >>"$UPSD_C"
  44. }
  45. upsd_config() {
  46. local cfg="$1"
  47. local maxage maxconn certfile runas statepath
  48. # Note runas support requires you make sure USB device file is readable by
  49. # the runas user
  50. config_get runas "$cfg" runas nut
  51. RUNAS="$runas"
  52. config_get statepath "$cfg" statepath /var/run/nut
  53. STATEPATH="$statepath"
  54. config_get maxage "$cfg" maxage
  55. [ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"
  56. [ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"
  57. config_get maxconn "$cfg" maxconn
  58. [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"
  59. #NOTE: certs only apply to SSL-enabled version
  60. config_get certfile "$cfg" certfile
  61. [ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
  62. }
  63. nut_user_add() {
  64. local cfg="$1"
  65. local a
  66. local val
  67. config_get val "$cfg" username "$1"
  68. echo "[$val]" >> "$USERS_C"
  69. config_get val "$cfg" password
  70. echo " password = $val" >> "$USERS_C"
  71. config_get val "$cfg" actions
  72. for a in $val; do
  73. echo " actions = $a" >> "$USERS_C"
  74. done
  75. instcmd() {
  76. local val="$1"
  77. echo " instcmds = $val" >> "$USERS_C"
  78. }
  79. config_list_foreach "$cfg" instcmd instcmd
  80. config_get val "$cfg" upsmon
  81. if [ -n "$val" ]; then
  82. echo " upsmon $val" >> "$USERS_C"
  83. fi
  84. }
  85. build_server_config() {
  86. mkdir -p "$(dirname "$UPSD_C")"
  87. chmod 0640 "$UPS_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 -p "${STATEPATH}"
  102. chmod 0750 "${STATEPATH}"
  103. }
  104. if [ -n "$RUNAS" ]; then
  105. chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}"
  106. chgrp "$(id -gn "$RUNAS")" "$USERS_C"
  107. chgrp "$(id -gn "$RUNAS")" "$UPSD_C"
  108. fi
  109. haveserver=1
  110. }
  111. build_driver_config() {
  112. local cfg="$1"
  113. echo "[$cfg]" >>"$UPS_C"
  114. get_write_driver_config "$cfg" bus
  115. get_write_driver_config "$cfg" community
  116. get_write_driver_config "$cfg" desc
  117. get_write_driver_config "$cfg" driver "usbhid-ups"
  118. get_write_driver_config "$cfg" ignorelb 0 1
  119. get_write_driver_config "$cfg" interruptonly 0 1
  120. get_write_driver_config "$cfg" interruptsize
  121. get_write_driver_config "$cfg" maxreport
  122. get_write_driver_config "$cfg" maxstartdelay
  123. get_write_driver_config "$cfg" mfr
  124. get_write_driver_config "$cfg" model
  125. get_write_driver_config "$cfg" nolock 0 1
  126. get_write_driver_config "$cfg" notransferoids 0 1
  127. get_write_driver_config "$cfg" offdelay
  128. get_write_driver_config "$cfg" ondelay
  129. get_write_driver_config "$cfg" pollfreq
  130. get_write_driver_config "$cfg" port "auto"
  131. get_write_driver_config "$cfg" product
  132. get_write_driver_config "$cfg" productid
  133. get_write_driver_config "$cfg" retrydelay
  134. get_write_driver_config "$cfg" sdorder
  135. get_write_driver_config "$cfg" sdtime
  136. get_write_driver_config "$cfg" serial
  137. get_write_driver_config "$cfg" snmp_version
  138. get_write_driver_config "$cfg" snmp_retries
  139. get_write_driver_config "$cfg" snmp_timeout
  140. get_write_driver_config "$cfg" synchronous
  141. get_write_driver_config "$cfg" vendor
  142. get_write_driver_config "$cfg" vendorid
  143. defoverride() {
  144. local overvar="$1"
  145. local defover="$2"
  146. local overtype="$(echo "$overvar" | tr '.' '_')"
  147. local overval
  148. config_get overval "${defover}_${overtype}" value
  149. [ -n "$overval" ] && echo "${defover}.${overvar} = $overval" >>"$UPS_C"
  150. }
  151. config_list_foreach "$cfg" override defoverride override
  152. config_list_foreach "$cfg" default defoverride default
  153. other() {
  154. local othervar="$1"
  155. local othervarflag="$2"
  156. local otherval
  157. if [ "$othervarflag" = "otherflag" ]; then
  158. config_get_bool otherval "${othervarflag}_${othervar}" value
  159. [ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C"
  160. else
  161. config_get otherval "${othervarflag}_${othervar}" value
  162. [ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C"
  163. fi
  164. }
  165. config_list_foreach "$cfg" other other
  166. config_list_foreach "$cfg" other otherflag
  167. echo "" >>$UPS_C
  168. havedriver=1
  169. }
  170. build_global_driver_config() {
  171. local cfg="$1"
  172. # Global driver config
  173. get_write_driver_config "$cfg" chroot
  174. get_write_driver_config "$cfg" driverpath
  175. get_write_driver_config "$cfg" maxstartdelay
  176. get_write_driver_config "$cfg" maxretry
  177. get_write_driver_config "$cfg" retrydelay
  178. get_write_driver_config "$cfg" pollinterval
  179. get_write_driver_config "$cfg" synchronous
  180. config_get runas "$cfg" user nut
  181. RUNAS="$runas"
  182. echo "" >>"$UPS_C"
  183. }
  184. build_config() {
  185. local STATEPATH=/var/run/nut
  186. mkdir -p "$(dirname "$UPS_C")"
  187. rm -f "$UPS_C"
  188. echo "# Config file automatically generated from UCI config" > "$UPS_C"
  189. chmod 0640 "$UPS_C"
  190. config_load nut_server
  191. upsd_runas
  192. config_foreach build_global_driver_config driver_global
  193. config_foreach build_driver_config driver
  194. upsd_statepath
  195. build_server_config
  196. [ -n "$RUNAS" ] && chgrp "$(id -gn "$RUNAS")" "$UPS_C"
  197. }
  198. start_driver_instance() {
  199. local cfg="$1"
  200. local requested="$2"
  201. local driver
  202. local STATEPATH=/var/run/nut
  203. local RUNAS=nut
  204. [ "$havedriver" != 1 ] && return
  205. # If wanting a specific instance, only start it
  206. if [ "$requested" != "$cfg" ] && [ "$request" != "" ]; then
  207. return 0
  208. fi
  209. mkdir -p "$(dirname "$UPS_C")"
  210. chmod 0755 "$UPS_C"
  211. upsd_statepath
  212. build_config
  213. # Avoid hotplug inadvertenly restarting driver during
  214. # forced shutdown
  215. [ -f /var/run/killpower ] && return 0
  216. if [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then
  217. return 0
  218. fi
  219. if [ -n "$RUNAS" ]; then
  220. chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}"
  221. chgrp "$(id -gn "$RUNAS")" "$UPS_C"
  222. fi
  223. config_get driver "$cfg" driver "usbhid-ups"
  224. procd_open_instance "$cfg"
  225. procd_set_param respawn
  226. procd_set_param stderr 0
  227. procd_set_param stdout 1
  228. procd_set_param command /lib/nut/"${driver}" -D -a "$cfg" ${RUNAS:+-u "$RUNAS"}
  229. procd_close_instance
  230. }
  231. interface_triggers() {
  232. local action="$1"
  233. local triggerlist trigger
  234. config_get triggerlist upsd triggerlist
  235. . /lib/functions/network.sh
  236. if [ -n "$triggerlist" ]; then
  237. for trigger in $triggerlist; do
  238. if [ "$action" = "add_trigger" ]; then
  239. procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-server reload
  240. else
  241. network_is_up "$trigger" && return 0
  242. fi
  243. done
  244. else
  245. if [ "$action" = "add_trigger" ]; then
  246. procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-server reload
  247. else
  248. ubus call network.device status | grep -q '"up": true' && return 0
  249. fi
  250. fi
  251. [ "$action" = "add_trigger" ] || return 1
  252. }
  253. start_server_instance() {
  254. local cfg="$1"
  255. [ "$haveserver" != 1 ] && return
  256. interface_triggers "check_interface_up" || return
  257. procd_open_instance "$cfg"
  258. procd_set_param respawn
  259. procd_set_param stderr 0
  260. procd_set_param stdout 1
  261. procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u "$RUNAS"}
  262. procd_close_instance
  263. }
  264. start_service() {
  265. local STATEPATH=/var/run/nut
  266. # Avoid hotplug inadvertenly restarting driver during
  267. # forced shutdown
  268. [ -f /var/run/killpower ] && return 0
  269. config_load nut_server
  270. build_config
  271. case $@ in
  272. "")
  273. config_foreach start_driver_instance driver "$@"
  274. start_server_instance upsd
  275. ;;
  276. *upsd*)
  277. start_server_instance upsd
  278. ;;
  279. *)
  280. config_foreach start_driver_instance driver "$@"
  281. ;;
  282. esac
  283. }
  284. reload_service() {
  285. stop_service "$@"
  286. sleep 2
  287. start_service "$@"
  288. }
  289. service_triggers() {
  290. config_load nut_server
  291. interface_triggers "add_trigger"
  292. procd_add_reload_trigger "nut_server"
  293. }