Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DoNotMerge] DBus proof-of-concept #533

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ addons:
- libxpm-dev
- libxml2-utils
- libmodbus-dev
- libdbus-1-dev
- libnss3-dev
- libssl-dev
# NOTE: Keep the list above in sync with replicas like deps_driverlibs_cross_i386 below
Expand Down Expand Up @@ -397,6 +398,7 @@ _matrix_linux_gnustd_nowarn_x86_32bit:
- libxpm-dev:i386
- libxml2-utils:i386
- libmodbus-dev:i386
- libdbus-1-dev:i386
- libnss3-dev:i386
- libssl-dev:i386
# See comments above about perl
Expand Down
1 change: 1 addition & 0 deletions ci_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ default|default-alldrv|default-alldrv:no-distcheck|default-all-errors|default-sp
CONFIG_OPTS+=("--with-udev-dir=${BUILD_PREFIX}/etc/udev")
CONFIG_OPTS+=("--with-devd-dir=${BUILD_PREFIX}/etc/devd")
CONFIG_OPTS+=("--with-hotplug-dir=${BUILD_PREFIX}/etc/hotplug")
CONFIG_OPTS+=("--with-dbus-dir=${BUILD_PREFIX}/etc/dbus-1")

# Some OSes have broken cppunit support, it crashes either build/link
# or at run-time. While distros take time to figure out fixes, we can
Expand Down
57 changes: 57 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ if test ! -d "${udevdir}"; then
fi
fi

dbusdir="/etc/dbus-1"
if test ! -d "${dbusdir}"; then
dbusdir=''
fi

devddir='/usr/local/etc/devd'
if test ! -d "${devddir}"; then
devddir='/etc/devd'
Expand Down Expand Up @@ -380,6 +385,8 @@ NUT_CHECK_LIBPOWERMAN
NUT_ARG_WITH([modbus], [build and install modbus drivers], [auto])
NUT_CHECK_LIBMODBUS
NUT_CHECK_LIBAVAHI
NUT_ARG_WITH([dbus], [build and install dbus support], [auto])
NUT_CHECK_LIBDBUS

dnl ----------------------------------------------------------------------
dnl additional USB-related checks
Expand Down Expand Up @@ -696,6 +703,23 @@ NUT_REPORT_FEATURE(
[Define to enable I2C support]
)

dnl ----------------------------------------------------------------------
dnl checks related to --with-dbus

dnl ${nut_with_dbus}: any value except "yes" or "no" is treated as "auto".
if test "${nut_with_dbus}" = "yes" -a "${nut_have_dbus}" != "yes"; then
AC_MSG_ERROR([dbus-1 libraries not found, required for dbus support])
fi

if test "${nut_with_dbus}" != "no"; then
nut_with_dbus="${nut_have_dbus}"
fi

NUT_REPORT_FEATURE([build dbus support], [${nut_with_dbus}], [],
[WITH_DBUS], [Define to enable dbus support])
AM_CONDITIONAL([HAVE_DBUS], [test "${nut_have_dbus}" = "yes"])


dnl ----------------------------------------------------------------------
dnl Check for with-ssl, and --with-nss or --with-openssl
dnl Only one can be enabled at a time, with a preference for OpenSSL
Expand Down Expand Up @@ -1796,6 +1820,34 @@ else
fi
AM_CONDITIONAL(WITH_UDEV, test -n "${udevdir}")

AC_MSG_CHECKING(whether to install dbus rules)
AC_ARG_WITH(dbus-dir,
AS_HELP_STRING([--with-dbus-dir=PATH], [where to install dbus rules (/etc/dbus-1)]),
[
case "${withval}" in
yes)
if test -z "${dbusdir}"; then
AC_MSG_RESULT(no)
AC_MSG_ERROR([dbus directory requested but not found])
fi
;;
auto)
;;
no)
dbusdir=""
;;
*)
dbusdir="${withval}"
;;
esac
], [])
if test -n "${dbusdir}"; then
AC_MSG_RESULT(using ${dbusdir})
else
AC_MSG_RESULT(no)
fi
AM_CONDITIONAL(WITH_DBUS, test -n "${dbusdir}")

dnl FreeBSD devd support:

AC_MSG_CHECKING(whether to install FreeBSD devd.conf file)
Expand Down Expand Up @@ -2018,6 +2070,8 @@ AC_SUBST(LIBWRAP_CFLAGS)
AC_SUBST(LIBWRAP_LIBS)
AC_SUBST(LIBLTDL_CFLAGS)
AC_SUBST(LIBLTDL_LIBS)
AC_SUBST(LIBDBUS_CFLAGS)
AC_SUBST(LIBDBUS_LIBS)
AC_SUBST(DRIVER_BUILD_LIST)
AC_SUBST(DRIVER_MAN_LIST)
AC_SUBST(DRIVER_INSTALL_TARGET)
Expand Down Expand Up @@ -2048,6 +2102,7 @@ AC_SUBST(systemdtmpfilesdir)
AC_SUBST(auglensdir)
AC_SUBST(hotplugdir)
AC_SUBST(udevdir)
AC_SUBST(dbusdir)

dnl Filter through known variants first, so automatic choices can be made.
dnl Note that clang identifies as gcc-compatible so should be probed first.
Expand Down Expand Up @@ -2212,6 +2267,8 @@ AC_CONFIG_FILES([
scripts/augeas/nuthostsconf.aug
scripts/augeas/nutupssetconf.aug
scripts/avahi/nut.service
scripts/dbus/Makefile
scripts/dbus/nut-dbus.conf
scripts/devd/Makefile
scripts/devd/nut-usb.conf
scripts/hotplug/Makefile
Expand Down
5 changes: 5 additions & 0 deletions docs/configure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ Core and Client parts.
Enable libltdl (Libtool dlopen abstraction) support.
This is required to build nut-scanner.

--with-dbus (default: auto-detect)

Enable dbus support. upsd will provide device information on the
local system bus.

Other configuration options
---------------------------

Expand Down
1 change: 1 addition & 0 deletions docs/nut.dict
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ datasheet
datastale
dayofweek
dblatex
dbus
dcd
dcn
ddl
Expand Down
67 changes: 67 additions & 0 deletions m4/nut_check_libdbus.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
dnl Check for LIBDBUS compiler flags. On success, set nut_have_dbus="yes"
dnl and set LIBDBUS_CFLAGS and LIBDBUS_LIBS. On failure, set
dnl nut_have_dbus="no". This macro can be run multiple times, but will
dnl do the checking only once.

AC_DEFUN([NUT_CHECK_LIBDBUS],
[
if test -z "${nut_have_dbus_seen}"; then
nut_have_dbus_seen=yes

dnl save CFLAGS and LIBS
CFLAGS_ORIG="${CFLAGS}"
LIBS_ORIG="${LIBS}"

dnl See which version of the dbus library (if any) is installed
AC_MSG_CHECKING(for libdbus version via pkg-config)
DBUS_VERSION="`pkg-config --silence-errors --modversion dbus-1 2>/dev/null`"
if test "$?" != "0" -o -z "${DBUS_VERSION}"; then
DBUS_VERSION="none"
fi
AC_MSG_RESULT(${DBUS_VERSION} found)

AC_MSG_CHECKING(for libdbus cflags)
AC_ARG_WITH(dbus-includes,
AS_HELP_STRING([@<:@--with-dbus-includes=CFLAGS@:>@], [include flags for the dbus library]),
[
case "${withval}" in
yes|no)
AC_MSG_ERROR(invalid option --with(out)-dbus-includes - see docs/configure.txt)
;;
*)
CFLAGS="${withval}"
;;
esac
], [CFLAGS="`pkg-config --silence-errors --cflags dbus-1 2>/dev/null`"])
AC_MSG_RESULT([${CFLAGS}])

AC_MSG_CHECKING(for libdbus ldflags)
AC_ARG_WITH(dbus-libs,
AS_HELP_STRING([@<:@--with-dbus-libs=LIBS@:>@], [linker flags for the dbus library]),
[
case "${withval}" in
yes|no)
AC_MSG_ERROR(invalid option --with(out)-dbus-libs - see docs/configure.txt)
;;
*)
LIBS="${withval}"
;;
esac
], [LIBS="`pkg-config --silence-errors --libs dbus-1 2>/dev/null`"])
AC_MSG_RESULT([${LIBS}])

dnl check if dbus is usable
AC_CHECK_HEADERS(dbus/dbus.h, [nut_have_dbus=yes], [nut_have_dbus=no], [AC_INCLUDES_DEFAULT])
AC_CHECK_FUNCS(dbus_bus_get, [], [nut_have_dbus=no])

if test "${nut_have_dbus}" = "yes"; then
AC_DEFINE(HAVE_DBUS, 1, [Define if you have Freedesktop libdbus installed])
LIBDBUS_CFLAGS="${CFLAGS}"
LIBDBUS_LIBS="${LIBS}"
fi

dnl restore original CFLAGS and LIBS
CFLAGS="${CFLAGS_ORIG}"
LIBS="${LIBS_ORIG}"
fi
])
2 changes: 1 addition & 1 deletion scripts/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ EXTRA_DIST = README \
Windows/halt.c \
Windows/Makefile

SUBDIRS = augeas devd hotplug python systemd udev ufw Solaris upsdrvsvcctl
SUBDIRS = augeas dbus devd hotplug python systemd udev ufw Solaris upsdrvsvcctl
1 change: 1 addition & 0 deletions scripts/dbus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nut-dbus.conf
10 changes: 10 additions & 0 deletions scripts/dbus/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

if WITH_DBUS
dbusrulesdir = $(dbusdir)/system.d
dbusrules_DATA = nut-dbus.conf
endif

EXTRA_DIST = README

DISTCLEANFILES = nut-dbus.conf

30 changes: 30 additions & 0 deletions scripts/dbus/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Desc: DBus access descriptor
File: scripts/dbus/README
Date: 8 March 2018
Auth: Emilien Kia <emilien.kia+dev@gmail.com>

This document introduces the DBus right rules for NUT.
It describes how upsd cn register names and paths on system bus and
how client can send requests to methods of objects.

Installation
------------

For most users, these files will be automatically installed in
/etc/dbus-1 upon "make install", if that directory exists and if
the feature (DBus) has been enabled at configure time. You can
specify an alternate directory with ./configure --with-dbus-dir=DIR.

Manual installation
-------------------

To install them manually, copy the rules file(s) to /etc/dbus-1/system.d
using the command(s):

$ cp -f nut-dbus.conf /etc/dbus-1/system.d/nut-dbus.conf

You will need to refresh the bus to avoid a reboot for these rules to be
active. You can do so using:

$ systemctl reload dbus.service

36 changes: 36 additions & 0 deletions scripts/dbus/nut-dbus.conf.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

<!-- Only Nut user or root can own Upsd and do everything. -->
<policy user="@RUN_AS_USER@">
<allow own="org.networkupstools.Upsd"/>
</policy>
<policy user="root">
<allow own="org.networkupstools.Upsd"/>
</policy>

<!-- Default user can only request in read-only (enumerate and get). -->
<policy context="default">
<allow send_destination="org.networkupstools.Upsd"
send_interface="org.freedesktop.DBus.Introspectable" />

<allow send_destination="org.networkupstools.Upsd"
send_interface="org.freedesktop.DBus.Properties"
send_type="method_call" send_member="Get" />

<allow send_destination="org.networkupstools.Upsd"
send_interface="org.freedesktop.DBus.Properties"
send_type="method_call" send_member="GetAll" />
</policy>

<!-- Members of Nut group can set writable properties. -->
<policy group="@RUN_AS_GROUP@">
<allow send_destination="org.networkupstools.Upsd"
send_interface="org.freedesktop.DBus.Properties"
send_type="method_call" send_member="Set" />

</policy>


</busconfig>
12 changes: 9 additions & 3 deletions server/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ endif
if WITH_SSL
AM_CFLAGS += $(LIBSSL_CFLAGS)
endif
if WITH_DBUS
AM_CFLAGS += $(LIBDBUS_CFLAGS)
endif
LDADD = ../common/libcommon.la ../common/libparseconf.la $(NETLIBS)
if WITH_WRAP
LDADD += $(LIBWRAP_LIBS)
endif
if WITH_SSL
LDADD += $(LIBSSL_LIBS)
endif
if WITH_DBUS
LDADD += $(LIBDBUS_LIBS)
endif

sbin_PROGRAMS = upsd
EXTRA_PROGRAMS = sockdebug

upsd_SOURCES = upsd.c user.c conf.c netssl.c sstate.c desc.c \
netget.c netmisc.c netlist.c netuser.c netset.c netinstcmd.c \
upsd_SOURCES = upsd.c user.c conf.c netssl.c sstate.c desc.c \
netget.c netmisc.c netlist.c netuser.c netset.c netinstcmd.c dbus.c \
conf.h nut_ctype.h desc.h netcmds.h neterr.h netget.h netinstcmd.h \
netlist.h netmisc.h netset.h netuser.h netssl.h sstate.h stype.h upsd.h \
upstype.h user-data.h user.h
upstype.h user-data.h user.h dbus.h

sockdebug_SOURCES = sockdebug.c
Loading