Skip to content

Commit

Permalink
Merge branch 'master' into cmdset_status
Browse files Browse the repository at this point in the history
  • Loading branch information
jimklimov authored Feb 23, 2019
2 parents c0c478b + 392097a commit 3948f09
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
7 changes: 5 additions & 2 deletions clients/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ if WITH_SSL
libupsclient_la_LIBADD += $(LIBSSL_LIBS)
endif

# libupsclient version information
# Below we set API versions of public libraries
# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
# Note that changes here may have to be reflected in packaging (the shared
# object .so names would differ)

# libupsclient version information
libupsclient_la_LDFLAGS = -version-info 5:0:0

# libnutclient version information
libnutclient_la_SOURCES = nutclient.h nutclient.cpp
libnutclient_la_LDFLAGS = -version-info 1:0:0

15 changes: 15 additions & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
#include "nut_version.h"
const char *UPS_VERSION = NUT_VERSION_MACRO;

#include <stdio.h>
#include <limits.h>

/* Know which bitness we were built for,
* to adjust the search paths for get_libname() */
#if UINTPTR_MAX == 0xffffffffffffffffULL
# define BUILD_64 1
#endif

int nut_debug_level = 0;
int nut_log_level = 0;
static int upslog_flags = UPSLOG_STDERR;
Expand Down Expand Up @@ -691,15 +700,21 @@ const char * search_paths[] = {
LIBDIR,
"/usr"LIBDIR,
"/usr/local"LIBDIR,
#if BUILD_64
// Fall back to explicit preference of 64-bit paths as named on some OSes
"/usr/lib/64",
"/usr/lib64",
#endif
"/usr/lib",
#if BUILD_64
"/lib/64",
"/lib64",
#endif
"/lib",
#if BUILD_64
"/usr/local/lib/64",
"/usr/local/lib64",
#endif
"/usr/local/lib",
#ifdef AUTOTOOLS_TARGET_SHORT_ALIAS
"/usr/lib/" AUTOTOOLS_TARGET_SHORT_ALIAS,
Expand Down
4 changes: 3 additions & 1 deletion docs/nut.dict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 2438 utf-8
personal_ws-1.1 en 2440 utf-8
AAS
ACFAIL
ACFREQ
Expand Down Expand Up @@ -1190,6 +1190,7 @@ apctest
apcupsd
aphel
ar
arg
argc
args
argv
Expand Down Expand Up @@ -2004,6 +2005,7 @@ privProtocol
probu
proc
productid
prog
psu
pw
pwl
Expand Down
99 changes: 91 additions & 8 deletions scripts/upsdrvsvcctl/nut-driver-enumerator.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ UPSLIST_SVCS=""
# Framework-specific implementations are generally hooked here:
hook_registerInstance=""
hook_unregisterInstance=""
hook_refreshSupervizor=""
hook_listInstances=""
hook_listInstances_raw=""
hook_validInstanceName=""
Expand All @@ -176,6 +177,7 @@ case "${SERVICE_FRAMEWORK-}" in
smf)
hook_registerInstance="smf_registerInstance"
hook_unregisterInstance="smf_unregisterInstance"
hook_refreshSupervizor="smf_refreshSupervizor"
hook_listInstances="smf_listInstances"
hook_listInstances_raw="smf_listInstances_raw"
hook_validInstanceName="smf_validInstanceName"
Expand All @@ -189,6 +191,7 @@ case "${SERVICE_FRAMEWORK-}" in
systemd)
hook_registerInstance="systemd_registerInstance"
hook_unregisterInstance="systemd_unregisterInstance"
hook_refreshSupervizor="systemd_refreshSupervizor"
hook_listInstances="systemd_listInstances"
hook_listInstances_raw="systemd_listInstances_raw"
hook_validInstanceName="systemd_validInstanceName"
Expand All @@ -202,6 +205,7 @@ case "${SERVICE_FRAMEWORK-}" in
selftest)
hook_registerInstance="selftest_NOOP"
hook_unregisterInstance="selftest_NOOP"
hook_refreshSupervizor="selftest_NOOP"
hook_listInstances="selftest_NOOP"
hook_listInstances_raw="selftest_NOOP"
hook_validInstanceName="selftest_NOOP"
Expand Down Expand Up @@ -508,9 +512,22 @@ upsconf_debug() {
}

calc_md5() {
# Tries several ways to produce an MD5 of the "$1" argument
_MD5="`echo "$1" | md5sum 2>/dev/null | awk '{print $1}'`" && [ -n "$_MD5" ] || \
{ _MD5="`echo "$1" | openssl dgst -md5 2>/dev/null | awk '{print $NF}'`" && [ -n "$_MD5" ]; } || \
return 1

echo "$_MD5"
}

calc_md5_file() {
# Tries several ways to produce an MD5 of the file named by "$1" argument
[ -s "$1" ] || return 2

_MD5="`md5sum 2>/dev/null < "$1" | awk '{print $1}'`" && [ -n "$_MD5" ] || \
{ _MD5="`openssl dgst -md5 2>/dev/null < "$1" | awk '{print $NF}'`" && [ -n "$_MD5" ]; } || \
return 1

echo "$_MD5"
}

Expand Down Expand Up @@ -584,6 +601,9 @@ smf_unregisterInstance() {
/usr/sbin/svcadm disable -ts 'nut-driver:'"$1" || false
/usr/sbin/svccfg -s nut-driver delete "$1"
}
smf_refreshSupervizor() {
:
}
smf_listInstances_raw() {
# Newer versions have pattern matching; older SMF might not have this luxury
/usr/bin/svcs -a -H -o fmri | egrep '/nut-driver:'
Expand Down Expand Up @@ -709,6 +729,9 @@ systemd_unregisterInstance() {
rm -rf "${SYSTEMD_CONFPATH}/nut-driver@$1.service.d"
/bin/systemctl reset-failed 'nut-driver@'"$1".service
}
systemd_refreshSupervizor() {
/bin/systemctl daemon-reload
}
systemd_listInstances_raw() {
/bin/systemctl show 'nut-driver@*' -p Id | egrep '=nut-driver' | sed 's,^Id=,,'
}
Expand All @@ -733,7 +756,6 @@ systemd_setSavedMD5() {
Environment='$PROP=$2'
EOF
[ $? = 0 ] && echo "OK" || { echo "FAILED to stash the checksum">&2 ; return 1 ; }
/bin/systemctl daemon-reload
}
systemd_restart_upsd() {
# Do not restart/reload if not already running
Expand Down Expand Up @@ -976,6 +998,7 @@ nut_driver_enumerator_main() {
if [ -n "$UPSLIST_FILE" ]; then
# Add services for sections that are in config file but not yet wrapped
upslist_addSvcs
$hook_refreshSupervizor
upslist_readSvcs "after checking for new config sections to define service instances"
fi
Expand All @@ -997,30 +1020,70 @@ nut_driver_enumerator_main() {
# Return 42 if there was a change applied succesfully
# (but e.g. some services should restart - upsd, maybe upsmon)
if upslist_equals "$UPSLIST_FILE" "$UPSLIST_SVCS" ; then
UPSLIST_EQ_RES=0
upslist_equals "$UPSLIST_FILE" "$UPSLIST_SVCS" || UPSLIST_EQ_RES=$?
# File processing and service startups take a while;
# make sure upsconf did not change while we worked...
# NOTE: Check this at the last moment to minimize
# the chance of still not noticing the change made
# at just the wrong moment.
UPSCONF_CHECKSUM_END="`calc_md5_file "$UPSCONF"`" || true
if [ "$UPSCONF_CHECKSUM_END" != "$UPSCONF_CHECKSUM_START" ] ; then
# NOTE: even if daemonized, the sleep between iterations
# can be configured into an uncomfortably long lag, so
# we should re-sync the system config in any case.
echo "`date -u` : '$UPSCONF' changed while $0 $* was processing its older contents; re-running the script to pick up the late-coming changes"
# Make sure the cycle does not repeat itself due to diffs
# from an ages-old state of the file from when we started.
UPSCONF_CHECKSUM_START="$UPSCONF_CHECKSUM_END"
( nut_driver_enumerator_main ) ; return $?
# The "main" routine at the end of recursions will
# do REPORT_RESTART_42 logic or the error exit-code
fi
if [ "$UPSLIST_EQ_RES" = 0 ] ; then
echo "`date -u` : OK: No more changes to reconcile between ${SERVICE_FRAMEWORK} service instances and device configurations in '$UPSCONF'"
[ "${REPORT_RESTART_42-}" = no ] && return 0 || return 42
fi
return 13
}
daemonize() (
trap '-' 1
# Support (SIG)HUP == signal code 1 to quickly reconfigure,
# e.g. to request it while the sleep is happening or while
# "main" is processing an earlier change of the file.
RECONFIGURE_ASAP=false
trap 'RECONFIGURE_ASAP=true' 1
# Note: this loop would die on errors with config file or
# inability to ensure that it matches the list of services.
# If caller did not `export REPORT_RESTART_42=no` then the
# loop would exit with code 42, and probably trigger restart
# of the service which wraps this daemon do topple others that
# depend on it.
# Note: do not quickly skip the "main" based on full-file
# checksum refresh, to ensure that whatever is configured
# gets applied (e.g. if user disabled some services or they
# died, or some config was not applied due to coding error).
while nut_driver_enumerator_main ; do
sleep $DAEMON_SLEEP &
trap "kill $! ; echo 'Sleep interrupted, processing configs now!'>&2" 1
wait $!
trap '-' 1
if $RECONFIGURE_ASAP ; then
echo "`date -u` : Trapped a SIGHUP during last run of nut_driver_enumerator_main, repeating reconfiguration quickly" >&2
else
sleep $DAEMON_SLEEP &
trap "kill $! ; echo 'Sleep interrupted, processing configs now!'>&2" 1
wait $!
fi
RECONFIGURE_ASAP=false
trap 'RECONFIGURE_ASAP=true' 1
done
exit $?
)
# Save the checksum of ups.conf as early as possible,
# to test in the end that it is still the same file.
UPSCONF_CHECKSUM_START="`calc_md5_file "$UPSCONF"`" || true
# By default, update wrapping of devices into services
if [ $# = 0 ]; then
nut_driver_enumerator_main ; exit $?
Expand Down Expand Up @@ -1110,6 +1173,10 @@ while [ $# -gt 0 ]; do
# Save new checksum of global config
$hook_setSavedMD5 "" "`upsconf_getSection_MD5 ""`"
# Service units were manipulated, including saving of checksums;
# refresh the service management daemon if needed
$hook_refreshSupervizor
if [ -n "$UPSLIST_SVCS" ] ; then
echo "=== The currently defined service instances are:"
echo "$UPSLIST_SVCS"
Expand All @@ -1127,10 +1194,26 @@ while [ $# -gt 0 ]; do
# Return 42 if there was a change applied succesfully
# (but e.g. some services should restart - upsd, maybe upsmon)
if upslist_equals "$UPSLIST_FILE" "$UPSLIST_SVCS" ; then
UPSLIST_EQ_RES=0
upslist_equals "$UPSLIST_FILE" "$UPSLIST_SVCS" || UPSLIST_EQ_RES=$?
# File processing and service startups take a while;
# make sure upsconf did not change while we worked...
# NOTE: Check this at the last moment to minimize
# the chance of still not noticing the change made
# at just the wrong moment.
UPSCONF_CHECKSUM_END="`calc_md5_file "$UPSCONF"`" || true
if [ "$UPSCONF_CHECKSUM_END" != "$UPSCONF_CHECKSUM_START" ] ; then
echo "`date -u` : '$UPSCONF' changed while $0 $* was processing its older contents; re-running the script to pick up the late-coming changes"
$0 ; exit $?
# The "main" routine will do REPORT_RESTART_42 logic too
fi
if [ "$UPSLIST_EQ_RES" = 0 ] ; then
echo "`date -u` : OK: No more changes to reconcile between ${SERVICE_FRAMEWORK} service instances and device configurations in '$UPSCONF'"
[ "${REPORT_RESTART_42-}" = no ] && exit 0 || exit 42
fi
exit 13
;;
--list-devices)
Expand Down
7 changes: 7 additions & 0 deletions tools/nut-scanner/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ libnutscan_la_SOURCES = scan_nut.c scan_ipmi.c \
../../drivers/bcmxcp_ser.c \
../../common/common.c ../../common/str.c
libnutscan_la_LIBADD = $(NETLIBS) $(LIBLTDL_LIBS)
#
# Below we set API versions of public libraries
# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
# Note that changes here may have to be reflected in packaging (the shared
# object .so names would differ)
#
# libnutscan version information
libnutscan_la_LDFLAGS = $(SERLIBS) -version-info 1:0:0
libnutscan_la_CFLAGS = -I$(top_srcdir)/clients -I$(top_srcdir)/include $(LIBLTDL_CFLAGS) -I$(top_srcdir)/drivers

Expand Down

0 comments on commit 3948f09

Please sign in to comment.