Package: apcupsd
Version: 3.14.14-9
Severity: important
Tags: patch
X-Debbugs-Cc: [email protected]
The Modbus RS232 driver incorrectly enables serial parity, causing
communication failure with an APC Smart-UPS 1000.
The problem is in src/drivers/modbus/ModbusRs232Comm.cpp, where the serial port
is configured using:
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
This looks reasonable, but on Debian Sid (glibc 2.43), B9600 is defined as
9600U (0x2580), while PARENB is defined as 0000400 (0x100).
Since 0x2580 contains the 0x100 bit, ORing B9600 into c_cflag unintentionally
enables parity. The port is therefore configured for 9600 8E1 instead of 9600
8N1.
The result is that apcupsd reports COMMLOST and repeatedly logs Modbus CRC
errors.
I confirmed the problem by:
- Using strace to inspect the serial port configuration. The TCSETS2 ioctl
showed PARENB enabled.
- Stopping apcupsd and communicating directly with the UPS using Python and
pyserial at 9600 8N1. The UPS returned valid Modbus responses.
- Rebuilding apcupsd with B9600 removed from the c_cflag assignment.
Communication was immediately restored.
The driver already sets the baud rate correctly using cfsetospeed() and
cfsetispeed(), so including B9600 in c_cflag is unnecessary.
The fix is:
struct termios newtio = {};
newtio.c_cflag = CS8 | CLOCAL | CREAD;
The zero-initialisation also prevents uninitialised fields in the termios
structure.
After installing the patched package, apcupsd correctly identifies the
Smart-UPS 1000, reports ONLINE, and retrieves voltage, load, battery charge,
runtime and other monitoring values.
A unified diff containing the tested fix is attached.
If it helps I'm using a modified cisco FTDI cable to connect from my server to
the UPS.
If you want to test this I can supply the instructions to modify the cable.
Regards
Jon
-- System Information:
Debian Release: forky/sid
APT prefers unstable
APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)
Kernel: Linux 7.3.5+deb14-amd64 (SMP w/4 CPU threads; PREEMPT)
Locale: LANG=en_GB.UTF-8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8),
LANGUAGE=en_GB:en
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
Versions of packages apcupsd depends on:
ii bsdutils 1:2.42.4-1
ii libc6 2.43-8
ii libgcc-s1 16.2.0-6
ii libusb-0.1-4 2:0.1.12-35+b2
ii libwrap0 7.6.q-37
ii lsb-base 11.6
ii sysvinit-utils [lsb-base] 3.18-2
Versions of packages apcupsd recommends:
ii apcupsd-doc 3.14.14-9
ii bsd-mailx [mailx] 8.1.2-0.20220412cvs-1.1
ii s-nail 14.9.25-3
Versions of packages apcupsd suggests:
pn apcupsd-cgi <none>
ii udev 263~rc1-1
-- Configuration Files:
/etc/apcupsd/apccontrol changed:
prefix=/usr
exec_prefix=${prefix}
APCPID=/var/run/apcupsd.pid
APCUPSD=/sbin/apcupsd
SHUTDOWN=/sbin/shutdown
SCRIPTSHELL=/bin/sh
SCRIPTDIR=/etc/apcupsd
WALL=wall
export SYSADMIN=root
export APCUPSD_MAIL="mail"
if [ -f $SCRIPTDIR/config ]; then . $SCRIPTDIR/config ; fi
if [ -f ${SCRIPTDIR}/${1} -a -x ${SCRIPTDIR}/${1} ]
then
${SCRIPTDIR}/${1} ${2} ${3} ${4}
# exit code 99 means he does not want us to do default action
if [ $? = 99 ] ; then
exit 0
fi
fi
case "$1" in
killpower)
echo "Apccontrol doing: ${APCUPSD} --killpower on UPS ${2}" | (${WALL}
2>/dev/null || cat)
sleep 10
${APCUPSD} --killpower
echo "Apccontrol has done: ${APCUPSD} --killpower on UPS ${2}" |
(${WALL} 2>/dev/null || cat)
;;
commfailure)
echo "Warning communications lost with UPS ${2}" | ${WALL}
;;
commok)
echo "Communications restored with UPS ${2}" | ${WALL}
;;
powerout)
;;
onbattery)
echo "Power failure on UPS ${2}. Running on batteries." | ${WALL}
;;
offbattery)
echo "Power has returned on UPS ${2}..." | ${WALL}
;;
mainsback)
if [ -f /etc/apcupsd/powerfail ] ; then
printf "Continuing with shutdown." | ${WALL}
fi
;;
failing)
echo "Battery power exhausted on UPS ${2}. Doing shutdown." | ${WALL}
;;
timeout)
echo "Battery time limit exceeded on UPS ${2}. Doing shutdown." |
${WALL}
;;
loadlimit)
echo "Remaining battery charge below limit on UPS ${2}. Doing
shutdown." | ${WALL}
;;
runlimit)
echo "Remaining battery runtime below limit on UPS ${2}. Doing
shutdown." | ${WALL}
;;
doreboot)
echo "UPS ${2} initiating Reboot Sequence" | ${WALL}
${SHUTDOWN} -r now "apcupsd UPS ${2} initiated reboot"
;;
doshutdown)
echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
/usr/sbin/poweroffvm
${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"
;;
annoyme)
echo "Power problems with UPS ${2}. Please logoff." | ${WALL}
;;
emergency)
echo "Emergency Shutdown. Possible battery failure on UPS ${2}." |
${WALL}
;;
changeme)
echo "Emergency! Batteries have failed on UPS ${2}. Change them NOW" |
${WALL}
;;
remotedown)
echo "Remote Shutdown. Beginning Shutdown Sequence." | ${WALL}
;;
startselftest)
;;
endselftest)
;;
battdetach)
;;
battattach)
;;
*) echo "Usage: ${0##*/} command"
echo " warning: this script is intended to be launched by"
echo " apcupsd and should never be launched by users."
exit 1
;;
esac
/etc/apcupsd/apcupsd.conf changed:
UPSCABLE smart
UPSTYPE modbus
DEVICE /dev/ttyUSB0
LOCKFILE /var/lock
SCRIPTDIR /etc/apcupsd
PWRFAILDIR /etc/apcupsd
NOLOGINDIR /etc
ONBATTERYDELAY 6
BATTERYLEVEL 5
MINUTES 3
TIMEOUT 0
ANNOY 300
ANNOYDELAY 60
NOLOGON disable
KILLDELAY 0
NETSERVER on
NISIP 10.43.1.3
NISPORT 3551
EVENTSFILE /var/log/apcupsd.events
EVENTSFILEMAX 10
UPSCLASS standalone
UPSMODE disable
STATTIME 0
STATFILE /var/log/apcupsd.status
LOGSTATS off
DATATIME 0
/etc/apcupsd/onbattery changed:
HOSTNAME=`hostname`
MSG="$HOSTNAME UPS $1 Oh No, Power Failure"
(
echo "$MSG"
echo " "
/sbin/apcaccess status
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
exit 0
-- no debconf information
--- a/src/drivers/modbus/ModbusRs232Comm.cpp
+++ b/src/drivers/modbus/ModbusRs232Comm.cpp
@@ -77,8 +77,8 @@
if (cmd != -1)
(void)fcntl(_fd, F_SETFL, cmd & ~O_NDELAY);
- struct termios newtio;
- newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
+ struct termios newtio = {};
+ newtio.c_cflag = CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR; /* Ignore errors, raw input */
newtio.c_oflag = 0; /* Raw output */
newtio.c_lflag = 0; /* No local echo */