Skip to content

Commit

Permalink
FreeBSD: generate devd.conf files for USB UPSes
Browse files Browse the repository at this point in the history
This adds a --with-devd-dir=PATH option to ./configure, which defaults to
/usr/local/etc/devd (or /etc/devd, whichever is found first).

Unlike udev, there does not seem to be a way to re-trigger rules at runtime.
This means you will likely need to unplug and replug your UPS after installing
the new nut-usb.conf file.
  • Loading branch information
clepple committed Feb 14, 2014
1 parent d4e6c1f commit d50dbca
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
4 changes: 3 additions & 1 deletion autogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ then
fi
fi

if [ ! -f scripts/hal/ups-nut-device.fdi.in ] || [ ! -f scripts/udev/nut-usbups.rules.in ]
if [ ! -f scripts/hal/ups-nut-device.fdi.in -o \
! -f scripts/udev/nut-usbups.rules.in -o \
! -f scripts/devd/nut-usb.conf.in ]
then
if perl -e 1; then
echo "Regenerating the USB helper files..."
Expand Down
43 changes: 43 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ if test ! -d "${udevdir}"; then
fi
fi

devddir='/usr/local/etc/devd'
if test ! -d "${devddir}"; then
devddir='/etc/devd'
if test ! -d "${devddir}"; then
devddir=''
fi
fi

RUN_AS_USER="nobody"
RUN_AS_GROUP="nobody"
PIDPATH="/var/run"
Expand Down Expand Up @@ -1091,6 +1099,38 @@ else
fi
AM_CONDITIONAL(WITH_UDEV, test -n "${udevdir}")

dnl FreeBSD devd support:

AC_MSG_CHECKING(whether to install FreeBSD devd.conf file)
AC_ARG_WITH(devd-dir,
AS_HELP_STRING([--with-devd-dir=PATH], [where to install devd.conf file (/usr/local/etc/devd or /etc/devd)]),
[
case "${withval}" in
yes)
if test -z "${devddir}"; then
AC_MSG_RESULT(no)
AC_MSG_ERROR(["devd directory requested but not found"])
fi
;;
auto)
;;
no)
devddir=""
;;
*)
devddir="${withval}"
;;
esac
], [])
if test -n "${devddir}"; then
AC_MSG_RESULT(using ${devddir})
else
AC_MSG_RESULT(no)
fi
AM_CONDITIONAL(WITH_DEVD, test -n "${devddir}")

dnl

AC_PATH_PROGS([ASCIIDOC], [asciidoc], [none])
AM_CONDITIONAL([MANUALUPDATE], [test "x$ASCIIDOC" != "xnone"])
AC_PATH_PROGS([A2X], [a2x], [none])
Expand Down Expand Up @@ -1174,6 +1214,7 @@ AC_SUBST(RUN_AS_GROUP)
AC_SUBST(SUN_LIBUSB)
AC_SUBST(WORDS_BIGENDIAN)
AC_SUBST(cgiexecdir)
AC_SUBST(devddir)
AC_SUBST(driverexecdir)
AC_SUBST(htmldir)
AC_SUBST(pkgconfigdir)
Expand Down Expand Up @@ -1212,6 +1253,8 @@ AC_OUTPUT([
scripts/augeas/nuthostsconf.aug
scripts/augeas/nutupssetconf.aug
scripts/avahi/nut.service
scripts/devd/Makefile
scripts/devd/nut-usb.conf
scripts/hal/Makefile
scripts/hal/ups-nut-device.fdi
scripts/hotplug/Makefile
Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ upower/95-upower-hid.rules \
Windows/halt.c \
Windows/Makefile

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

if WITH_DEVD
devdconfdir = $(devddir)
devdconf_DATA =
if WITH_USB
devdconf_DATA += nut-usb.conf
endif
endif

EXTRA_DIST = README

DISTCLEANFILES = nut-usb.conf
# we should never remove this one, apart from a distclean-check
#MAINTAINERCLEANFILES = nut-usbups.rules.in
11 changes: 11 additions & 0 deletions scripts/devd/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
On FreeBSD, devd has a similar role to udev on Linux. The devd.conf file
defines actions to perform when devices are plugged in.

The tools/nut-usbinfo.pl script generates nut-usb.conf.in by processing USB
macros in all of the drivers. In this case, the defined action for each
matching UPS is to change the permissions such that the NUT drivers can access
the devices without requiring root privileges. You may need to restart devd and
re-plug in the UPS to trigger the actions.

The format of this configuration file should work with devd on FreeBSD 9.0 and
9.1, at the very least.
26 changes: 26 additions & 0 deletions tools/nut-usbinfo.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (C) 2008 - 2012
# Arnaud Quette <[email protected]>
# dloic (loic.dardant AT gmail DOT com)
# Copyright (C) 2013 - 2014 Charles Lepple <[email protected]>
#
# Based on the usbdevice.pl script, made for the Ubuntu Media Center
# for the final use of the LIRC project.
Expand Down Expand Up @@ -40,8 +41,12 @@
# udev output file
my $outputUdev="../scripts/udev/nut-usbups.rules.in";

# BSD devd output file
my $output_devd="../scripts/devd/nut-usb.conf.in";

# UPower output file
my $outputUPower="../scripts/upower/95-upower-hid.rules";

# tmp output, to allow generating the ENV{UPOWER_VENDOR} header list
my $tmpOutputUPower;
# mfr header flag
Expand Down Expand Up @@ -109,6 +114,10 @@ sub gen_usb_files
print $outUdev 'SUBSYSTEM!="usb", GOTO="nut-usbups_rules_end"'."\n\n";
print $outUdev 'LABEL="nut-usbups_rules_real"'."\n";

open my $out_devd, ">$output_devd" || die "error $output_devd : $!";
print $out_devd '# This file is generated and installed by the Network UPS Tools package.'."\n";
print $out_devd "# Homepage: http://www.networkupstools.org/\n\n";

# UPower file header
open my $outputUPower, ">$outputUPower" || die "error $outputUPower : $!";
print $outputUPower '##############################################################################################################'."\n";
Expand Down Expand Up @@ -147,6 +156,12 @@ sub gen_usb_files
print $outUdev "\n# ".$vendorName{$vendorId}."\n";
}

# devd vendor header
if ($vendorName{$vendorId}) {
print $out_devd "\n# ".$vendorName{$vendorId}."\n";
}


# UPower vendor header flag
$upowerMfrHeaderDone = 0;

Expand All @@ -172,6 +187,17 @@ sub gen_usb_files
print $outUdev "\", ATTR{idProduct}==\"".removeHexPrefix($productId)."\",";
print $outUdev ' MODE="664", GROUP="@RUN_AS_GROUP@"'."\n";

# devd device entry
print $out_devd "# ".$vendor{$vendorId}{$productId}{"comment"}.' - '.$vendor{$vendorId}{$productId}{"driver"}."\n";
print $out_devd "notify 100 {\n\tmatch \"system\"\t\t\"USB\";\n";
print $out_devd "\tmatch \"subsystem\"\t\"DEVICE\";\n";
print $out_devd "\tmatch \"type\"\t\t\"ATTACH\";\n";
print $out_devd "\tmatch \"vendor\"\t\t\"$vendorId\";\n";
#
print $out_devd "\tmatch \"product\"\t\t\"$productId\";\n";
print $out_devd "\taction \"chgrp \@RUN_AS_GROUP\@ /dev/\$device-name*; chmod g+rw /dev/\$device-name*\";\n";
print $out_devd "};\n";

# UPower device entry (only for USB/HID devices!)
if ($vendor{$vendorId}{$productId}{"driver"} eq "usbhid-ups")
{
Expand Down

0 comments on commit d50dbca

Please sign in to comment.