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.

368 lines
8.7 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. # Params specific to NetXML driver
  144. get_write_driver_config "$cfg" login
  145. get_write_driver_config "$cfg" password
  146. get_write_driver_config "$cfg" subscribe 0 1
  147. defoverride() {
  148. local overvar="$1"
  149. local defover="$2"
  150. local overtype="$(echo "$overvar" | tr '_' '.')"
  151. local overval
  152. config_get overval "${defover}_${overvar}" value
  153. [ -n "$overval" ] && echo "${defover}.${overtype} = $overval" >>"$UPS_C"
  154. }
  155. config_list_foreach "$cfg" override defoverride override
  156. config_list_foreach "$cfg" default defoverride default
  157. other() {
  158. local othervar="$1"
  159. local othervarflag="$2"
  160. local otherval
  161. if [ "$othervarflag" = "otherflag" ]; then
  162. config_get_bool otherval "${othervarflag}_${othervar}" value
  163. [ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C"
  164. else
  165. config_get otherval "${othervarflag}_${othervar}" value
  166. [ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C"
  167. fi
  168. }
  169. config_list_foreach "$cfg" other other other
  170. config_list_foreach "$cfg" otherflag other otherflag
  171. echo "" >>$UPS_C
  172. havedriver=1
  173. }
  174. build_global_driver_config() {
  175. local cfg="$1"
  176. # Global driver config
  177. get_write_driver_config "$cfg" chroot
  178. get_write_driver_config "$cfg" driverpath
  179. get_write_driver_config "$cfg" maxstartdelay
  180. get_write_driver_config "$cfg" maxretry
  181. get_write_driver_config "$cfg" retrydelay
  182. get_write_driver_config "$cfg" pollinterval
  183. get_write_driver_config "$cfg" synchronous
  184. config_get runas "$cfg" user nut
  185. RUNAS="$runas"
  186. echo "" >>"$UPS_C"
  187. }
  188. build_config() {
  189. local STATEPATH=/var/run/nut
  190. mkdir -p "$(dirname "$UPS_C")"
  191. rm -f "$UPS_C"
  192. echo "# Config file automatically generated from UCI config" > "$UPS_C"
  193. chmod 0640 "$UPS_C"
  194. config_load nut_server
  195. upsd_runas
  196. config_foreach build_global_driver_config driver_global
  197. config_foreach build_driver_config driver
  198. upsd_statepath
  199. build_server_config
  200. [ -n "$RUNAS" ] && chgrp "$(id -gn "$RUNAS")" "$UPS_C"
  201. }
  202. start_driver_instance() {
  203. local cfg="$1"
  204. local requested="$2"
  205. local driver
  206. local STATEPATH=/var/run/nut
  207. local RUNAS=nut
  208. [ "$havedriver" != 1 ] && return
  209. # If wanting a specific instance, only start it
  210. if [ "$requested" != "$cfg" ] && [ "$request" != "" ]; then
  211. return 0
  212. fi
  213. mkdir -p "$(dirname "$UPS_C")"
  214. chmod 0755 "$UPS_C"
  215. upsd_statepath
  216. build_config
  217. # Avoid hotplug inadvertenly restarting driver during
  218. # forced shutdown
  219. [ -f /var/run/killpower ] && return 0
  220. if [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then
  221. return 0
  222. fi
  223. if [ -n "$RUNAS" ]; then
  224. chown "$RUNAS":"$(id -gn "$RUNAS")" "${STATEPATH}"
  225. chgrp "$(id -gn "$RUNAS")" "$UPS_C"
  226. fi
  227. config_get driver "$cfg" driver "usbhid-ups"
  228. procd_open_instance "$cfg"
  229. procd_set_param respawn
  230. procd_set_param stderr 0
  231. procd_set_param stdout 1
  232. procd_set_param command /lib/nut/"${driver}" -D -a "$cfg" ${RUNAS:+-u "$RUNAS"}
  233. procd_close_instance
  234. }
  235. interface_triggers() {
  236. local action="$1"
  237. local triggerlist trigger
  238. config_get triggerlist upsd triggerlist
  239. . /lib/functions/network.sh
  240. if [ -n "$triggerlist" ]; then
  241. for trigger in $triggerlist; do
  242. if [ "$action" = "add_trigger" ]; then
  243. procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-server reload
  244. else
  245. network_is_up "$trigger" && return 0
  246. fi
  247. done
  248. else
  249. if [ "$action" = "add_trigger" ]; then
  250. procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-server reload
  251. else
  252. ubus call network.device status | grep -q '"up": true' && return 0
  253. fi
  254. fi
  255. [ "$action" = "add_trigger" ] || return 1
  256. }
  257. start_server_instance() {
  258. local cfg="$1"
  259. [ "$haveserver" != 1 ] && return
  260. interface_triggers "check_interface_up" || return
  261. procd_open_instance "$cfg"
  262. procd_set_param respawn
  263. procd_set_param stderr 0
  264. procd_set_param stdout 1
  265. procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u "$RUNAS"}
  266. procd_close_instance
  267. }
  268. start_service() {
  269. local STATEPATH=/var/run/nut
  270. # Avoid hotplug inadvertenly restarting driver during
  271. # forced shutdown
  272. [ -f /var/run/killpower ] && return 0
  273. config_load nut_server
  274. build_config
  275. case $@ in
  276. "")
  277. config_foreach start_driver_instance driver "$@"
  278. start_server_instance upsd
  279. ;;
  280. *upsd*)
  281. start_server_instance upsd
  282. ;;
  283. *)
  284. config_foreach start_driver_instance driver "$@"
  285. ;;
  286. esac
  287. }
  288. reload_service() {
  289. stop_service "$@"
  290. sleep 2
  291. start_service "$@"
  292. }
  293. service_triggers() {
  294. config_load nut_server
  295. interface_triggers "add_trigger"
  296. procd_add_reload_trigger "nut_server"
  297. }