blob: be1cd45e20a5caa9fed96de42c5443f70901bc6a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
#!/bin/sh /etc/rc.common
# Copyright © 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# shellcheck shell=ash
# shellcheck disable=SC2034
START=70
STOP=30
USERS_C=/var/etc/nut/upsd.users
UPSD_C=/var/etc/nut/upsd.conf
UPS_C=/var/etc/nut/ups.conf
USE_PROCD=1
get_write_ups_config() {
local ups="$1"
local var="$2"
local def="$3"
local flag="$4"
local val
[ -z "$flag" ] && {
config_get val "$ups" "$var" "$def"
[ -n "$val" ] && [ "$val" != "0" ] && echo "$var = $val" >>"$UPS_C"
}
[ -n "$flag" ] && {
config_get_bool val "$ups" "$var" "$def"
[ "$val" = 1 ] && echo "$var" >>"$UPS_C"
}
}
srv_statepath() {
local statepath
config_get statepath upsd statepath /var/run/nut
STATEPATH="$statepath"
}
srv_runas() {
local runas
[ -n "$RUNAS" ] && return 0
config_get runas upsd runas nut
RUNAS="$runas"
}
listen_address() {
local srv="$1"
config_get address "$srv" address "::1"
config_get port "$srv" port
# shellcheck disable=SC2154
echo "LISTEN $address $port" >>"$UPSD_C"
}
srv_config() {
local srv="$1"
local maxage maxconn certfile runas statepath
# Note runas support requires you make sure USB device file is readable by
# the runas user
config_get runas "$srv" runas nut
RUNAS="$runas"
config_get statepath "$srv" statepath /var/run/nut
STATEPATH="$statepath"
config_get maxage "$srv" maxage
[ -n "$maxage" ] && echo "MAXAGE $maxage" >>"$UPSD_C"
[ -n "$statepath" ] && echo "STATEPATH $statepath" >>"$UPSD_C"
config_get maxconn "$srv" maxconn
[ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>"$UPSD_C"
#NOTE: certs only apply to SSL-enabled version
config_get certfile "$srv" certfile
[ -n "$certfile" ] && echo "CERTFILE $certfile" >>"$UPSD_C"
}
nut_user_add() {
local user="$1"
local a
local val
config_get val "$user" username "$1"
echo "[$val]" >> "$USERS_C"
config_get val "$user" password
echo " password = $val" >> "$USERS_C"
config_get val "$user" actions
for a in $val; do
echo " actions = $a" >> "$USERS_C"
done
instcmd() {
# shellcheck disable=2317
local val="$1"
# shellcheck disable=2317
echo " instcmds = $val" >> "$USERS_C"
}
config_list_foreach "$user" instcmd instcmd
config_get val "$user" upsmon
if [ -n "$val" ]; then
echo " upsmon $val" >> "$USERS_C"
fi
}
build_server_config() {
mkdir -p "$(dirname "$UPSD_C")"
chmod 0640 "$UPS_C"
rm -f "$USERS_C"
rm -f "$UPSD_C"
rm -f /var/etc/nut/nut.conf
echo "# Config file automatically generated from UCI config" > "$USERS_C"
echo "# Config file automatically generated from UCI config" > "$UPSD_C"
config_foreach nut_user_add user
config_foreach listen_address listen_address
config_foreach srv_config upsd
echo "MODE=netserver" >>/var/etc/nut/nut.conf
chmod 0640 "$USERS_C"
chmod 0640 "$UPSD_C"
chmod 0644 /var/etc/nut/nut.conf
if [ -n "$RUNAS" ]; then
chgrp "$(id -gn "$RUNAS")" "$USERS_C"
chgrp "$(id -gn "$RUNAS")" "$UPSD_C"
fi
havesrvcfg=1
}
build_ups_config() {
local ups="$1"
echo "[$ups]" >>"$UPS_C"
get_write_ups_config "$ups" bus
get_write_ups_config "$ups" cable
get_write_ups_config "$ups" community
get_write_ups_config "$ups" desc
get_write_ups_config "$ups" driver "usbhid-ups"
get_write_ups_config "$ups" ignorelb 0 1
get_write_ups_config "$ups" interruptonly 0 1
get_write_ups_config "$ups" interruptsize
get_write_ups_config "$ups" maxreport
get_write_ups_config "$ups" maxstartdelay
get_write_ups_config "$ups" mfr
get_write_ups_config "$ups" model
get_write_ups_config "$ups" nolock 0 1
get_write_ups_config "$ups" notransferoids 0 1
get_write_ups_config "$ups" offdelay
get_write_ups_config "$ups" ondelay
get_write_ups_config "$ups" pollfreq
get_write_ups_config "$ups" port "auto"
get_write_ups_config "$ups" product
get_write_ups_config "$ups" productid
get_write_ups_config "$ups" retrydelay
get_write_ups_config "$ups" sdorder
get_write_ups_config "$ups" sdtime
get_write_ups_config "$ups" serial
get_write_ups_config "$ups" shutdown_delay
get_write_ups_config "$ups" snmp_version
get_write_ups_config "$ups" snmp_retries
get_write_ups_config "$ups" snmp_timeout
get_write_ups_config "$ups" synchronous
get_write_ups_config "$ups" usd
get_write_ups_config "$ups" vendor
get_write_ups_config "$ups" vendorid
# Params specific to NetXML driver
get_write_ups_config "$ups" login
get_write_ups_config "$ups" password
get_write_ups_config "$ups" subscribe 0 1
# shellcheck disable=SC2317
defoverride() {
local overvar="$1"
local defover="$2"
local overtype
local overval
overtype="$(echo "$overvar" | tr '_' '.')"
config_get overval "${defover}_${overvar}" value
[ -n "$overval" ] && echo "${defover}.${overtype} = $overval" >>"$UPS_C"
}
config_list_foreach "$ups" override defoverride override
config_list_foreach "$ups" default defoverride default
other() {
# shellcheck disable=SC2317
local othervar="$1"
# shellcheck disable=SC2317
local othervarflag="$2"
# shellcheck disable=SC2317
local otherval
# shellcheck disable=SC2317
if [ "$othervarflag" = "otherflag" ]; then
config_get_bool otherval "${othervarflag}_${othervar}" value
[ "$otherval" = "1" ] && echo "${othervar}" >>"$UPS_C"
else
config_get otherval "${othervarflag}_${othervar}" value
[ -n "$otherval" ] && echo "${othervar} = $otherval" >>"$UPS_C"
fi
}
config_list_foreach "$ups" other other other
config_list_foreach "$ups" otherflag other otherflag
echo "" >>$UPS_C
haveupscfg=1
}
build_global_driver_config() {
local cfg="$1"
# Global driver config
get_write_ups_config "$cfg" chroot
get_write_ups_config "$cfg" driverpath
get_write_ups_config "$cfg" maxstartdelay
get_write_ups_config "$cfg" maxretry
get_write_ups_config "$cfg" retrydelay
get_write_ups_config "$cfg" pollinterval
get_write_ups_config "$cfg" synchronous
config_get runas "$cfg" user nut
RUNAS="$runas"
echo "" >>"$UPS_C"
}
build_config() {
local STATEPATH=/var/run/nut
mkdir -p "$(dirname "$UPS_C")"
rm -f "$UPS_C"
echo "# Config file automatically generated from UCI config" > "$UPS_C"
chmod 0640 "$UPS_C"
config_load nut_server
srv_runas
srv_statepath
[ -d "${STATEPATH}" ] || {
mkdir -p "${STATEPATH}"
}
chmod 0770 "${STATEPATH}"
if [ -n "$RUNAS" ]; then
chown root:"$(id -gn "$RUNAS")" "${STATEPATH}"
fi
SRV_RUNAS="$RUNAS"
config_foreach build_global_driver_config driver_global
if [ "$SRV_RUNAS" != "$RUNAS" ]; then
echo "WARNING: for proper communication drivers and server must 'runas' the same user" | logger -t nut-server
fi
config_foreach build_ups_config driver
build_server_config
[ -n "$RUNAS" ] && chgrp "$(id -gn "$RUNAS")" "$UPS_C"
}
ensure_usb_ups_access() {
local ups="$1"
local vendorid
local productid
local runas=nut
runas="$RUNAS"
config_load nut_server
config_get vendorid "$ups" vendorid
config_get productid "$ups" productid
config_get serial "$ups" serial
[ -n "$vendorid" ] || return
[ -n "$productid" ] || return
local NL='
'
find /sys/devices -name idVendor -a -path '*usb*'| while IFS="$NL" read -r vendor_path; do
local usb_bus usb_dev device_path
# Filter by vendor ID first
if [ "$(cat "$vendor_path" 2>/dev/null)" != "$vendorid" ]; then
continue
fi
device_path="$(dirname "$vendor_path")"
# Then filter by product ID
if [ "$(cat "$device_path/idProduct" 2>/dev/null)" != "$productid" ]; then
continue
fi
# Next filter by serial, if provided
if [ -n "$serial" ] && [ "$serial" != "$(cat "$device_path"/serial)" ]; then
continue
fi
usb_bus="$(printf "%03d" "$(cat "$device_path"/busnum)")"
usb_dev="$(printf "%03d" "$(cat "$device_path"/devnum)")"
# usb_bus and usb_dev must each be at least 001
# a missing value will be present as 000 due to 'printf "%03d"'
local MISSING_USB_NUM="000"
if [ "$usb_bus" != "$MISSING_USB_NUM" ] && [ "$usb_dev" != "$MISSING_USB_NUM" ]; then
chmod 0660 /dev/bus/usb/"$usb_bus"/"$usb_dev"
chown "${runas:-root}":"$(id -gn "${runas:-root}")" /dev/bus/usb/"$usb_bus"/"$usb_dev"
fi
# Serial numbers are defined as unique, so do not loop further if serial
# was present and matched
if [ -n "$serial" ]; then
break
# If a serial number is not provided we need all vendor:product matches
# to have permissions for NUT as we do not know the matching method here
fi
done
}
# Must be called from start_service
start_ups_driver() {
local ups="$1"
local requested="$2"
local driver
local STATEPATH=/var/run/nut
local RUNAS=nut
# If wanting a specific instance, only start it
if [ "$requested" != "$ups" ] && [ "$requested" != "" ]; then
return 0
fi
# Avoid hotplug inadvertently restarting driver during
# forced shutdown
[ -f /var/run/killpower ] && return 0
if [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then
return 0
fi
# Depends on config_load from start_service
srv_statepath
srv_runas
ensure_usb_ups_access "$ups"
config_get driver "$ups" driver "usbhid-ups"
procd_open_instance "$ups"
procd_set_param respawn
procd_set_param stderr 1
procd_set_param stdout 0 # Subset of stderr
procd_set_param env NUT_QUIET_INIT_UPSNOTIFY=true
procd_set_param env NUT_STATEPATH="${STATEPATH}"
procd_set_param command /lib/nut/"${driver}" -FF -a "$ups" ${RUNAS:+-u "$RUNAS"}
procd_close_instance
haveupscfg=1
}
interface_triggers() {
local action="$1"
local triggerlist trigger
config_get triggerlist upsd triggerlist
# shellcheck disable=SC1091
. /lib/functions/network.sh
if [ -n "$triggerlist" ]; then
for trigger in $triggerlist; do
if [ "$action" = "add_trigger" ]; then
procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-server reload
else
network_is_up "$trigger" && return 0
fi
done
else
if [ "$action" = "add_trigger" ]; then
procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-server reload
else
ubus call network.device status | grep -q '"up": true' && return 0
fi
fi
[ "$action" = "add_trigger" ] || return 1
}
start_server_instance() {
local srv="$1"
procd_open_instance "$srv"
procd_set_param respawn
procd_set_param stderr 1
procd_set_param stdout 0 # Subset of stderr
procd_set_param env NUT_QUIET_INIT_UPSNOTIFY=true
procd_set_param env NUT_STATEPATH="$STATEPATH"
procd_set_param command /usr/sbin/upsd -FF ${RUNAS:+-u "$RUNAS"}
procd_close_instance
}
# shellcheck disable=SC2120
start_service() {
local STATEPATH=/var/run/nut
local haveupscfg=0
local havesrvcfg=0
# Avoid hotplug inadvertently restarting driver during
# forced shutdown
[ -f /var/run/killpower ] && return 0
srv_statepath
config_load nut_server
build_config
should_start_srv=1
[ "$havesrvcfg" = "1" ] || should_start_srv=0
# Avoid crashloop on server (upsd) when no ups is configured; make sure server
# is not running if no ups is found in configuration
[ "$haveupscfg" = "1" ] || should_start_srv=0
interface_triggers "check_interface_up" || should_start_srv=0
[ "$should_start_srv" = "1" ] || return 0
# We only start one service (upsd or one driver) from a given invocation
case "$1" in
"")
config_foreach start_ups_driver driver
start_server_instance upsd
;;
*upsd*)
start_server_instance upsd
;;
*)
config_foreach start_ups_driver driver "$1"
;;
esac
}
server_active() {
local nut_server_active
nut_server_active=$(_procd_ubus_call list | jsonfilter -l 1 -e "@['nut-server']")
[ "$nut_server_active" = "{ }" ] && return 0
}
list_running_instances() {
local service="$1"
local running_instances
running_instances=$(_procd_ubus_call list | jsonfilter -e "@['$service'][@.*.running=true]")
if [ -n "$running_instances" ]; then
json_init
json_load "$running_instances"
json_get_keys instance_names
# shellcheck disable=SC2154
echo "$instance_names"
json_cleanup
fi
}
signal_instance() {
local instance_name="$1"
local process_name="$2"
local signal_command="$3"
local signal="$4"
local pidfile="$5"
local secondary_command="$6"
if [ -s "$pidfile" ]; then
$signal_command | logger -t nut-server
elif pgrep "$process_name" >/dev/null 2>/dev/null; then
procd_send_signal nut-server "$instance_name" "$signal" 2>&1 | logger -t nut-server
fi
if [ -n "$secondary_command" ] && procd_running nut-server "$instance_name"; then
$secondary_command 2>&1 | logger -t nut-server
fi
}
stop_ups_driver() {
local ups="$1" # The ups (driver instance)
local requested="$2"
local driver
# If wanting a specific instance, only stop it
if [ "$requested" != "$ups" ] && [ "$requested" != "" ]; then
return 0
fi
srv_statepath
build_ups_config "$ups"
# If we don't have UPS configuration simply stop all instances
if [ "$haveupscfg" != "1" ]; then
if procd_running nut-server '*' >/dev/null 2>&1; then
procd_kill nut-server '*' 2>&1 | logger -t nut-server
fi
return 0
fi
config_get driver "$ups" driver "usbhid-ups"
if procd_running nut-server "$ups"; then
signal_instance "$ups" "$driver" "/lib/nut/'${driver}' -c exit -a '${ups}'" "TERM" "${STATEPATH}/${driver}-${ups}.pid"
if procd_running nut-server upsd >/dev/null 2>&1; then
signal_instance upsd upsd "upsd -c stop" "TERM" "${STATEPATH}/upsd.pid" "procd_kill nut-server upsd"
fi
fi
}
reload_ups_driver() {
local ups="$1"
local requested="$2"
local driver
# If wanting a specific instance, only reload that instance
if [ "$requested" != "$ups" ] && [ "$requested" != "" ]; then
return 0
fi
# Avoid hotplug inadvertently restarting driver during
# forced shutdown
[ -f /var/run/killpower ] && return 0
if [ -d /var/run/nut ] && [ -f /var/run/nut/disable-hotplug ]; then
return 0
fi
config_get driver "$ups" driver "usbhid-ups"
srv_statepath
# Try to reload, otherwise exit politely, then stop and restart procd instance
if procd_running nut-server "$ups"; then
signal_instance "$ups" "$driver" "/lib/nut/'${driver}' -c reload-or-exit -a '${ups}'" HUP "${STATEPATH}/${driver}-${ups}.pid"
fi
/etc/init.d/nut-server start "$ups" 2>&1 | logger -t nut-server
}
reload_service() {
local should_stop_srv
local STATEPATH=/var/run/nut
local havesrvcfg=0
local haveupscfg=0
local running_instances
local driver
# Avoid hotplug inadvertently restarting driver during forced shutdown
[ -f /var/run/killpower ] && return 0
config_load nut_server
build_config
should_stop_srv=0
[ "$havesrvcfg" = "1" ] || should_stop_srv=1
# Avoid crashloop on server (upsd) when no ups is configured; make sure server
# is not running if no ups is found in configuration
[ "$haveupscfg" = "1" ] || should_stop_srv=1
interface_triggers "check_interface_up" || should_stop_srv=1
if [ "$should_stop_srv" != "0" ]; then
if procd_running nut-server upsd >/dev/null 2>&1; then
procd_kill nut-server upsd 2>&1 | logger -t nut-server
fi
config_foreach stop_ups_driver driver
# Also stop any driver instances which are no longer configured
for instance in $(list_running_instances "nut-server"); do
if [ "$instance" != "upsd" ] && procd_running nut-server "$instance" >/dev/null 2>&1; then
procd_kill nut-server "$instance" 2>&1 | logger -t nut-server
fi
done
fi
# If nut-server was started but has no instances (even upsd)
if server_active; then
logger -t nut-server "nut-server active with no instances"
/etc/init.d/nut-server start 2>&1 | logger -t nut-server
# Otherwise, if we have at least one instance running
elif procd_running nut-server; then
# If server (upsd) is running
if procd_running nut-server upsd; then
# Try to signal server (upsd) to reload configuration
signal_instance "upsd" "upsd" "upsd -c reload" HUP "${STATEPATH}/upsd.pid"
# If server (upsd) is not running
else
# Start server (upsd)
/etc/init.d/nut-server start "upsd" 2>&1 | logger -t nut-server
fi
config_foreach reload_ups_driver driver
# Stop any driver instances which are no longer configured
# We can only reliably do this for instances managed by procd
for instance in $(list_running_instances "nut-server"); do
if [ "$instance" = "upsd" ]; then
continue
fi
unset driver
config_get driver "$instance" driver
if [ -z "$driver" ] && procd_running nut-server "$instance" >/dev/null 2>&1; then
procd_kill nut-server "$instance" 2>&1 | logger -t nut-server
fi
done
# Nut-server is not started, so start it
else
/etc/init.d/nut-server start 2>&1 | logger -t nut-server
fi
}
stop_service() {
config_load nut_server
srv_statepath
# We only handle the first parameter passed
case "$1" in
"")
# If nut-server was started but has no instances (even upsd)
if server_active; then
logger -t nut-server "nut-server active with no instances"
procd_kill nut-server 2>&1 | logger -t nut-server
elif procd_running nut-server; then # if have at least one instance
signal_instance "upsd" "upsd" "upsd -c stop" TERM "${STATEPATH}/upsd.pid" "procd_kill nut-server upsd"
config_foreach stop_ups_driver driver
# Also stop any driver instances which are no longer configured
for instance in $(list_running_instances "nut-server"); do
if [ "$instance" != "upsd" ] && procd_running nut-server "$instance" >/dev/null 2>&1; then
procd_kill nut-server "$instance" 2>&1 | logger -t nut-server
fi
done
# If nut-server active but has no instances (even upsd)
if server_active >/dev/null 2>&1; then
procd_kill nut-server 2>&1 | logger -t nut-server
fi
fi
;;
*upsd*)
if procd_running nut-server upsd; then
signal_instance "upsd" "upsd" "upsd -c stop" TERM "${STATEPATH}/upsd.pid" "procd_kill nut-server upsd"
# If nut-server is active with no instances
if server_active; then
procd_kill nut-server 2>&1 | logger -t nut-server
fi
fi
;;
*)
# We only handle the first parameter, so do not pass in all parameters
config_foreach stop_ups_driver driver "$1"
;;
esac
}
service_triggers() {
config_load nut_server
interface_triggers "add_trigger"
procd_add_reload_trigger "nut_server"
}
|