From 9ee6693fb3ffdc58e17d6f13bf49a395a95ed4c6 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Mon, 13 Dec 2021 10:00:16 +0000 Subject: [PATCH] Update init.d support --- .gitignore | 1 + INSTALL | 15 ++-- configure.ac | 14 ++- script/Makefile.am | 23 ++++- script/ethercatctl.in | 14 ++- script/init.d/Makefile.am | 34 ------- script/init.d/ethercat.in | 168 +++-------------------------------- script/sysconfig/Makefile.am | 36 -------- script/sysconfig/ethercat | 70 --------------- 9 files changed, 60 insertions(+), 315 deletions(-) delete mode 100644 script/init.d/Makefile.am delete mode 100644 script/sysconfig/Makefile.am delete mode 100644 script/sysconfig/ethercat diff --git a/.gitignore b/.gitignore index 3b085f26..f8d8bfd7 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ lib/libethercat.la lib/libethercat.pc libtool master/*.o.d +script/ethercat script/ethercat.service script/ethercatctl script/init.d/ethercat diff --git a/INSTALL b/INSTALL index d211cedb..72ca8b6a 100644 --- a/INSTALL +++ b/INSTALL @@ -16,11 +16,11 @@ section of the documentation available from http://etherlab.org/en/ethercat. ------------------------------------------------------------------------------- -For the impatient: The procedure mainly consists of calling +For the impatient, the procedure mainly consists of calling: $ ./bootstrap # to create the configure script, if downloaded from the repo -$ ./configure +$ ./configure --sysconfdir=/etc $ make all modules ... and as root: @@ -28,12 +28,10 @@ $ make all modules # make modules_install install # depmod -... and linking the init script and copying the sysconfig file from $PREFIX/etc -to the appropriate locations and customizing the sysconfig file. +... and then customizing the appropriate configuration file: -# ln -s ${PREFIX}/etc/init.d/ethercat /etc/init.d/ethercat -# cp ${PREFIX}/etc/sysconfig/ethercat /etc/sysconfig/ethercat -# vi /etc/sysconfig/ethercat +# vi /etc/ethercat.conf # For systemd based distro +# vi /etc/sysconfig/ethercat # For init.d based distro Make sure, that the 'udev' package is installed, to automatically create the EtherCAT character devices. The character devices will be created with mode @@ -44,7 +42,8 @@ access, create a udev rule like this: Now you can start the EtherCAT master: -# /etc/init.d/ethercat start +# systemctl start ethercat # For systemd based distro +# /etc/init.d/ethercat start # For init.d based distro Have a look at the examples/ subdirectory for some application examples. diff --git a/configure.ac b/configure.ac index 1be4a2eb..b0cfae14 100644 --- a/configure.ac +++ b/configure.ac @@ -47,6 +47,7 @@ AC_CONFIG_HEADERS([config.h]) AC_CONFIG_SRCDIR([config.h.in]) AC_CONFIG_MACRO_DIR([m4]) AC_PROG_SED +AC_PROG_MKDIR_P m4_ifndef([PKG_INSTALLDIR], [m4_fatal([pkgconf or pkg-config not found or too old (`pkg-config --version` must be >= 0.27)])]) PKG_PROG_PKG_CONFIG(0.27) PKG_INSTALLDIR @@ -1140,6 +1141,16 @@ else AC_MSG_RESULT([no]) fi +#------------------------------------------------------------------------------ +# init.d support +#------------------------------------------------------------------------------ + +AC_ARG_ENABLE([initd], + AS_HELP_STRING([--disable-initd], + [Disable /etc/init.d script support (default: enabled)]) +) +AM_CONDITIONAL(HAVE_INITD, test "x$enable_initd" != "xno") + #------------------------------------------------------------------------------ # systemd service support #------------------------------------------------------------------------------ @@ -1208,9 +1219,6 @@ AC_CONFIG_FILES([ master/Kbuild master/Makefile script/Makefile - script/init.d/Makefile - script/init.d/ethercat - script/sysconfig/Makefile tool/Makefile tty/Kbuild tty/Makefile diff --git a/script/Makefile.am b/script/Makefile.am index fe4e8876..be1e26d1 100644 --- a/script/Makefile.am +++ b/script/Makefile.am @@ -31,15 +31,14 @@ # #------------------------------------------------------------------------------ -SUBDIRS = init.d sysconfig - sbin_SCRIPTS = ethercatctl dist_sysconf_DATA = ethercat.conf script_templates = \ ethercatctl.in \ - ethercat.service.in + ethercat.service.in \ + init.d/ethercat.in EXTRA_DIST = \ $(script_templates) \ @@ -47,12 +46,21 @@ EXTRA_DIST = \ CLEANFILES = \ ethercatctl \ - ethercat.service + ethercat.service \ + init.d/ethercat \ + ethercat if HAVE_SYSTEMD systemdsystemunit_DATA = ethercat.service endif +if HAVE_INITD +initdir = $(sysconfdir)/init.d +init_SCRIPTS = init.d/ethercat +sysdir = $(sysconfdir)/sysconfig +sys_DATA = ethercat +endif + # Any precious variable used inside script_templates should appear here expand_script = $(SED) \ @@ -67,4 +75,11 @@ ethercatctl: $(srcdir)/ethercatctl.in Makefile ethercat.service: $(srcdir)/ethercat.service.in Makefile $(expand_script) < $< > $@ +init.d/ethercat: $(srcdir)/init.d/ethercat.in Makefile + $(MKDIR_P) init.d && \ + $(expand_script) < $< > $@ + +ethercat: $(srcdir)/ethercat.conf Makefile + $(SED) -e 's,for use with ethercatctl,for use with init.d,g' < $< > $@ + #------------------------------------------------------------------------------ diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 2da6e95b..eb09cda2 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -41,7 +41,15 @@ ETHERCAT=@bindir@/ethercat #------------------------------------------------------------------------------ -ETHERCAT_CONFIG=@sysconfdir@/ethercat.conf +if [ "$1" = "-c" ]; then + ETHERCAT_CONFIG="$2" + COMMAND="$3" +else + ETHERCAT_CONFIG=@sysconfdir@/ethercat.conf + COMMAND="$1" +fi + +#------------------------------------------------------------------------------ if [ ! -r ${ETHERCAT_CONFIG} ]; then echo ${ETHERCAT_CONFIG} not existing; @@ -75,7 +83,7 @@ parse_mac_address() { #------------------------------------------------------------------------------ -case "${1}" in +case "$COMMAND" in start) # construct DEVICES and BACKUPS from configuration variables @@ -213,7 +221,7 @@ status) #------------------------------------------------------------------------------ *) - echo "USAGE: $0 {start|stop|restart|status}" + echo "USAGE: $0 [-c path/to/ethercat.conf] {start|stop|restart|status}" exit 1 ;; esac diff --git a/script/init.d/Makefile.am b/script/init.d/Makefile.am deleted file mode 100644 index 2392e417..00000000 --- a/script/init.d/Makefile.am +++ /dev/null @@ -1,34 +0,0 @@ -#------------------------------------------------------------------------------ -# -# $Id$ -# -# Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH -# -# This file is part of the IgH EtherCAT Master. -# -# The IgH EtherCAT Master is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License version 2, as -# published by the Free Software Foundation. -# -# The IgH EtherCAT Master is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -# Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with the IgH EtherCAT Master; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# -# --- -# -# The license mentioned above concerns the source code only. Using the EtherCAT -# technology and brand is only permitted in compliance with the industrial -# property and similar rights of Beckhoff Automation GmbH. -# -#------------------------------------------------------------------------------ - -initdir = $(sysconfdir)/init.d - -init_SCRIPTS = ethercat - -#------------------------------------------------------------------------------ diff --git a/script/init.d/ethercat.in b/script/init.d/ethercat.in index 922889fe..fd7a9b24 100755 --- a/script/init.d/ethercat.in +++ b/script/init.d/ethercat.in @@ -47,27 +47,7 @@ #------------------------------------------------------------------------------ -LSMOD=/sbin/lsmod -MODPROBE=/sbin/modprobe -RMMOD=/sbin/rmmod -MODINFO=/sbin/modinfo -ETHERCAT=@prefix@/bin/ethercat -MASTER_ARGS= - -#------------------------------------------------------------------------------ - -ETHERCAT_CONFIG=/etc/sysconfig/ethercat - -if [ ! -r ${ETHERCAT_CONFIG} ]; then - echo ${ETHERCAT_CONFIG} not existing; - if [ "${1}" = "stop" ]; then - exit 0 - else - exit 6 - fi -fi - -. ${ETHERCAT_CONFIG} +ETHERCATCTL="@sbindir@/ethercatctl -c @sysconfdir@/sysconfig/ethercat" #------------------------------------------------------------------------------ @@ -97,41 +77,6 @@ exit_fail() { #------------------------------------------------------------------------------ -print_running() { - if [ -r /etc/rc.status ]; then - rc_reset - rc_status -v - else - echo " running" - fi -} - -#------------------------------------------------------------------------------ - -print_dead() { - if [ -r /etc/rc.status ]; then - rc_failed - rc_status -v - else - echo " dead" - fi -} - -#------------------------------------------------------------------------------ - -parse_mac_address() { - if [ -z "${1}" ]; then - MAC="" - elif echo "${1}" | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then - MAC=${1} - else - echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} - exit_fail - fi -} - -#------------------------------------------------------------------------------ - if [ -r /etc/rc.status ]; then . /etc/rc.status rc_reset @@ -142,85 +87,21 @@ case "${1}" in start) echo -n "Starting EtherCAT master @VERSION@ " - # construct DEVICES and BACKUPS from configuration variables - DEVICES="" - BACKUPS="" - MASTER_INDEX=0 - while true; do - DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}") - BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}") - if [ -z "${DEVICE}" ]; then break; fi - - if [ ${MASTER_INDEX} -gt 0 ]; then - DEVICES=${DEVICES}, - BACKUPS=${BACKUPS}, - fi - - parse_mac_address "${DEVICE}" - DEVICES=${DEVICES}${MAC} - - parse_mac_address "${BACKUP}" - BACKUPS=${BACKUPS}${MAC} - - MASTER_INDEX=$((${MASTER_INDEX} + 1)) - done - - # load master module - if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master "${MASTER_ARGS}" \ - main_devices="${DEVICES}" backup_devices="${BACKUPS}"; then + if $ETHERCATCTL start; then + exit_success + else exit_fail fi - - # check for modules to replace - for MODULE in ${DEVICE_MODULES}; do - ECMODULE=ec_${MODULE} - if ! ${MODINFO} "${ECMODULE}" > /dev/null; then - continue # ec_* module not found - fi - if [ "${MODULE}" != "generic" ]; then - if ${LSMOD} | grep "^${MODULE} " > /dev/null; then - if ! ${RMMOD} "${MODULE}"; then - exit_fail - fi - fi - fi - if ! ${MODPROBE} ${MODPROBE_FLAGS} "${ECMODULE}"; then - if [ "${MODULE}" != "generic" ]; then - ${MODPROBE} ${MODPROBE_FLAGS} "${MODULE}" # try to restore - fi - exit_fail - fi - done - - exit_success ;; stop) echo -n "Shutting down EtherCAT master @VERSION@ " - # unload EtherCAT device modules - for MODULE in ${DEVICE_MODULES} master; do - ECMODULE=ec_${MODULE} - if ! ${LSMOD} | grep -q "^${ECMODULE} "; then - continue # ec_* module not loaded - fi - if ! ${RMMOD} "${ECMODULE}"; then - exit_fail - fi; - done - - sleep 1 - - # reload previous modules - for MODULE in ${DEVICE_MODULES}; do - if [ "${MODULE}" != "generic" ]; then - if ! ${MODPROBE} ${MODPROBE_FLAGS} "${MODULE}"; then - echo Warning: Failed to restore "${MODULE}". - fi - fi - done - - exit_success + if $ETHERCATCTL stop; then + exit_success + else + exit_fail + fi ;; restart) @@ -231,35 +112,8 @@ restart) ;; status) - echo "Checking for EtherCAT master @VERSION@ " - - # count masters in configuration file - MASTER_COUNT=0 - while true; do - DEVICE=$(eval echo "\${MASTER${MASTER_COUNT}_DEVICE}") - if [ -z "${DEVICE}" ]; then break; fi - MASTER_COUNT=$((${MASTER_COUNT} + 1)) - done - - RESULT=0 - - for i in $(seq 0 "$((${MASTER_COUNT} - 1))"); do - echo -n "Master${i} " - - # Check if the master is in idle or operation phase - ${ETHERCAT} master --master "${i}" 2>/dev/null | \ - grep -qE 'Phase:[[:space:]]*Idle|Phase:[[:space:]]*Operation' - EXITCODE=$? - - if [ ${EXITCODE} -eq 0 ]; then - print_running - else - print_dead - RESULT=1 - fi - done - - exit ${RESULT} + $ETHERCATCTL status + exit $? ;; *) diff --git a/script/sysconfig/Makefile.am b/script/sysconfig/Makefile.am deleted file mode 100644 index 844b0eb0..00000000 --- a/script/sysconfig/Makefile.am +++ /dev/null @@ -1,36 +0,0 @@ -#------------------------------------------------------------------------------ -# -# $Id$ -# -# Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH -# -# This file is part of the IgH EtherCAT Master. -# -# The IgH EtherCAT Master is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License version 2, as -# published by the Free Software Foundation. -# -# The IgH EtherCAT Master is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -# Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with the IgH EtherCAT Master; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# -# --- -# -# The license mentioned above concerns the source code only. Using the EtherCAT -# technology and brand is only permitted in compliance with the industrial -# property and similar rights of Beckhoff Automation GmbH. -# -#------------------------------------------------------------------------------ - -sysdir = $(sysconfdir)/sysconfig - -sys_DATA = ethercat - -EXTRA_DIST = ethercat - -#------------------------------------------------------------------------------ diff --git a/script/sysconfig/ethercat b/script/sysconfig/ethercat deleted file mode 100644 index bf326d7c..00000000 --- a/script/sysconfig/ethercat +++ /dev/null @@ -1,70 +0,0 @@ -#------------------------------------------------------------------------------ -# -# EtherCAT master sysconfig file -# -# $Id$ -# -# vim: spelllang=en spell tw=78 -# -#------------------------------------------------------------------------------ - -# -# Main Ethernet devices. -# -# The MASTER_DEVICE variable specifies the Ethernet device for a master -# with index 'X'. -# -# Specify the MAC address (hexadecimal with colons) of the Ethernet device to -# use. Example: "00:00:08:44:ab:66" -# -# The broadcast address "ff:ff:ff:ff:ff:ff" has a special meaning: It tells -# the master to accept the first device offered by any Ethernet driver. -# -# The MASTER_DEVICE variables also determine, how many masters will be -# created: A non-empty variable MASTER0_DEVICE will create one master, adding a -# non-empty variable MASTER1_DEVICE will create a second master, and so on. -# -MASTER0_DEVICE="" -#MASTER1_DEVICE="" - -# -# Backup Ethernet devices -# -# The MASTER_BACKUP variables specify the devices used for redundancy. They -# behaves nearly the same as the MASTER_DEVICE variable, except that it -# does not interpret the ff:ff:ff:ff:ff:ff address. -# -#MASTER0_BACKUP="" - -# -# Ethernet driver modules to use for EtherCAT operation. -# -# Specify a non-empty list of Ethernet drivers, that shall be used for -# EtherCAT operation. -# -# Except for the generic Ethernet driver module, the init script will try to -# unload the usual Ethernet driver modules in the list and replace them with -# the EtherCAT-capable ones. If a certain (EtherCAT-capable) driver is not -# found, a warning will appear. -# -# Possible values: 8139too, e100, e1000, e1000e, r8169, generic, ccat, igb. -# Separate multiple drivers with spaces. -# -# Note: The e100, e1000, e1000e, r8169, ccat and igb drivers are not built by -# default. Enable them with the --enable- configure switches. -# -# Attention: When using the generic driver, the corresponding Ethernet device -# has to be activated (with OS methods, for example 'ip link set ethX up'), -# before the master is started, otherwise all frames will time out. -# -DEVICE_MODULES="" - -# -# Flags for loading kernel modules. -# -# This can usually be left empty. Adjust this variable, if you have problems -# with module loading. -# -#MODPROBE_FLAGS="-b" - -#------------------------------------------------------------------------------