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.

327 lines
7.9 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" driver "usbhid-ups"
  114. get_write_driver_config "$cfg" port "auto"
  115. get_write_driver_config "$cfg" sdorder
  116. get_write_driver_config "$cfg" desc
  117. get_write_driver_config "$cfg" nolock 0 1
  118. get_write_driver_config "$cfg" ignorelb 0 1
  119. get_write_driver_config "$cfg" mfr
  120. get_write_driver_config "$cfg" model
  121. get_write_driver_config "$cfg" serial
  122. get_write_driver_config "$cfg" sdtime
  123. get_write_driver_config "$cfg" offdelay
  124. get_write_driver_config "$cfg" ondelay
  125. get_write_driver_config "$cfg" pollfreq
  126. get_write_driver_config "$cfg" vendor
  127. get_write_driver_config "$cfg" product
  128. get_write_driver_config "$cfg" bus
  129. get_write_driver_config "$cfg" interruptonly 0 1
  130. get_write_driver_config "$cfg" interruptsize
  131. get_write_driver_config "$cfg" maxreport
  132. get_write_driver_config "$cfg" vendorid
  133. get_write_driver_config "$cfg" productid
  134. get_write_driver_config "$cfg" community
  135. get_write_driver_config "$cfg" snmp_version
  136. get_write_driver_config "$cfg" snmp_retries
  137. get_write_driver_config "$cfg" snmp_timeout
  138. get_write_driver_config "$cfg" notransferoids 0 1
  139. get_write_driver_config "$cfg" maxstartdelay
  140. get_write_driver_config "$cfg" retrydelay
  141. get_write_driver_config "$cfg" synchronous
  142. get_write_driver_config "$cfg" other
  143. get_write_driver_config "$cfg" otherflag
  144. defoverride() {
  145. local overvar="$1"
  146. local defover="$2"
  147. local overtype="$(echo "$overvar" | tr '.' '_')"
  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. if [ "$othervarflag" = "otherflag" ]; then
  157. config_get_bool otherval "${othervarflag}_${overtype}" value
  158. [ "$otherval" = "1" ] && echo "${othervarflag}_${othervar}" >>"$UPS_C"
  159. else
  160. config_get otherval "${othervarflag}_${overtype}" value
  161. [ -n "$otherval" ] && echo "${othervarflag}_${othervar} = $otherval" >>"$UPS_C"
  162. fi
  163. }
  164. config_list_foreach "$cfg" override defoverride override
  165. config_list_foreach "$cfg" default defoverride default
  166. config_list_foreach "$cfg" default other other
  167. config_list_foreach "$cfg" default other otherflag
  168. echo "" >>$UPS_C
  169. havedriver=1
  170. }
  171. build_global_driver_config() {
  172. local cfg="$1"
  173. # Global driver config
  174. get_write_driver_config "$cfg" chroot
  175. get_write_driver_config "$cfg" driverpath
  176. get_write_driver_config "$cfg" maxstartdelay
  177. get_write_driver_config "$cfg" maxretry
  178. get_write_driver_config "$cfg" retrydelay
  179. get_write_driver_config "$cfg" pollinterval
  180. get_write_driver_config "$cfg" synchronous
  181. config_get runas "$cfg" user "nut"
  182. RUNAS="$runas"
  183. upsd_runas
  184. echo "" >>$UPS_C
  185. }
  186. build_config() {
  187. local RUNAS=nut
  188. local STATEPATH=/var/run/nut
  189. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  190. rm -f "$UPS_C"
  191. echo "# Config file automatically generated from UCI config" > "$UPS_C"
  192. chmod 0640 "$UPS_C"
  193. config_load nut_server
  194. config_foreach upsd_statepath upsd
  195. config_foreach build_global_driver_config driver_global
  196. config_foreach build_driver_config driver
  197. [ -n "$RUNAS" ] && chgrp $(id -gn $RUNAS) "$UPS_C"
  198. build_server_config
  199. }
  200. start_driver_instance() {
  201. local cfg="$1"
  202. local requested="$2"
  203. local driver
  204. local STATEPATH=/var/run/nut
  205. local RUNAS=nut
  206. [ "$havedriver" != 1 ] && return
  207. # If wanting a specific instance, only start it
  208. [ "$requested" != "$cfg" ] && [ x"$requested" != x ] && return 0
  209. mkdir -m 0755 -p "$(dirname "$UPS_C")"
  210. [ ! -s "$UPS_C" ] && build_config
  211. # Avoid hotplug inadvertenly restarting driver during
  212. # forced shutdown
  213. [ -f /var/run/killpower ] && return 0
  214. [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ] && return 0
  215. config_foreach upsd_statepath upsd
  216. if [ -n "$RUNAS" ]; then
  217. chown $RUNAS:$(id -gn $RUNAS) "${STATEPATH}"
  218. fi
  219. config_get driver "$cfg" driver "usbhid-ups"
  220. procd_open_instance "$cfg"
  221. procd_set_param respawn
  222. procd_set_param stderr 0
  223. procd_set_param stdout 1
  224. procd_set_param command /lib/nut/${driver} -D -a "$cfg" ${RUNAS:+-u $RUNAS}
  225. procd_close_instance
  226. }
  227. start_server_instance() {
  228. local RUNAS
  229. build_config
  230. [ "$haveserver" != 1 ] && return
  231. procd_open_instance "upsd"
  232. procd_set_param respawn
  233. procd_set_param stderr 0
  234. procd_set_param stdout 1
  235. procd_set_param command /usr/sbin/upsd -D ${RUNAS:+-u $RUNAS}
  236. procd_close_instance
  237. }
  238. start_service() {
  239. local havedriver haveserver
  240. local STATEPATH=/var/run/nut
  241. # Avoid hotplug inadvertenly restarting driver during
  242. # forced shutdown
  243. [ -f /var/run/killpower ] && return 0
  244. [ -f /var/run/nut/disable-hotplug ] && return 0
  245. config_load nut_server
  246. build_config
  247. config_foreach start_driver_instance driver "$@"
  248. start_server_instance "upsd"
  249. }
  250. reload_service() {
  251. stop
  252. sleep 2
  253. local havedriver haveserver
  254. start
  255. }
  256. service_triggers() {
  257. procd_add_reload_trigger "nut_server"
  258. }