can-utils: slcand: minor fixes and udev scripts
cover can-utils: slcand: minor fixes and udev scripts
This is a set of minor fixes to slcand that I made while setting up slcand with a USB CAN device. I also added configuration files for udev/systemd to automatically start/stop slcand when the usb serial device is plugged in.
It is based off the master branch of can-utils.
https://gitorious.org/linux-can/can-utils.git
Jeremiah Mahler (5):
slcand: improve usage examples
slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS
slcand: consistent use of exit(EXIT_FAILURE) use line discipline number from linux/tty.h
slcand: add example scripts for udev/systemd
doc/50-slcand.rules | 16 ++++++++++++++++ doc/[email protected] | 11 +++++++++++ slcan_attach.c | 5 ++--- slcand.c | 20 ++++++++------------ 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 doc/50-slcand.rules create mode 100644 doc/[email protected]
Signature · 1 line
2.1.3
PATCH 1/5 slcand: improve usage examples
The examples in the usage of slcand use a 'ttyslcan0' device which is an uncommon name. Use a more common ttyUSB0 name which is seen with CANUSB devices. Also, add an example showing that /dev/ttyUSB0 can be use as well.
--- slcand.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
slcand.c +3 -2
@@ -71,8 +71,9 @@ void print_usage(char *prg)fprintf (stderr," -F (stay in foreground; no daemonize)\n" );fprintf (stderr," -h (show this help page)\n" );fprintf (stderr,"\nExamples:\n" ); -fprintf (stderr,"slcand -o -c -f -s6 ttyslcan0\n" ); -fprintf (stderr,"slcand -o -c -f -s6 ttyslcan0 can0\n" ); +fprintf (stderr,"slcand -o -c -f -s6 ttyUSB0\n" ); +fprintf (stderr,"slcand -o -c -f -s6 ttyUSB0 can0\n" ); +fprintf (stderr,"slcand -o -c -f -s6 /dev/ttyUSB0\n" );fprintf (stderr,"\n" );exit (EXIT_FAILURE ); }
Signature · 1 line
2.1.3
PATCH 2/5 slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS
In reply to Jeremiah Mahlerslcand has #defines for EXIT_FAILURE and EXIT_SUCCESS but this is unnecessary since they are already provided by stdlib.h. Remove them.
--- slcand.c | 3 --- 1 file changed, 3 deletions(-)
slcand.c +0 -3
@@ -50,9 +50,6 @@/* The length of ttypath buffer */ #define TTYPATH_LENGTH 64 -#define EXIT_SUCCESS 0 -#define EXIT_FAILURE 1 -/* UART flow control types */ #define FLOW_NONE 0#define FLOW_HW 1
Signature · 1 line
2.1.3
PATCH 3/5 slcand: consistent use of exit(EXIT_FAILURE)
In reply to Jeremiah MahlerSome places used exit(1) and others used exit(EXIT_FAILURE). Be consistent and use exit(EXIT_FAILURE) for all of these instances.
--- slcand.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
slcand.c +3 -3
@@ -334,13 +334,13 @@ int main(int argc, char *argv[])/* set slcan like discipline on given tty */ if (ioctl (fd,TIOCSETD , &ldisc) <0 ) {perror ("ioctl TIOCSETD" ); -exit (1 ); +exit (EXIT_FAILURE ); }/* retrieve the name of the created CAN netdevice */ if (ioctl (fd,SIOCGIFNAME , buf) <0 ) {perror ("ioctl SIOCGIFNAME" ); -exit (1 ); +exit (EXIT_FAILURE ); }syslog (LOG_NOTICE ,"attached TTY %s to netdevice %s\n" , ttypath, buf);
@@ -359,7 +359,7 @@ int main(int argc, char *argv[])if (ioctl (s,SIOCSIFNAME , &ifr) <0 ) {syslog (LOG_NOTICE ,"netdevice %s rename to %s failed\n" , buf, name);perror ("ioctl SIOCSIFNAME rename" ); -exit (1 ); +exit (EXIT_FAILURE ); }else syslog (LOG_NOTICE ,"netdevice %s renamed to %s\n" , buf, name);
Signature · 1 line
2.1.3
PATCH 4/5 use line discipline number from linux/tty.h
In reply to Jeremiah MahlerThe line discipline number is provided in include/linux/tty.h. Use that instead of a #define in each program.
--- slcan_attach.c | 5 ++--- slcand.c | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-)
slcan_attach.c +2 -3
@@ -51,8 +51,7 @@#include <sys/ioctl.h> #include <net/if.h> #include <termios.h> - -#define LDISC_N_SLCAN 17/* default slcan line discipline since Kernel 2.6.25 */ +#include <linux/tty.h> void print_usage (char *prg) {
@@ -77,7 +76,7 @@ void print_usage(char *prg)int main (int argc,char **argv) {int fd; -int ldisc =LDISC_N_SLCAN ; +int ldisc =N_SLCAN ;int detach =0 ;int waitkey =0 ;int send_open =0 ;
slcand.c +2 -4
@@ -37,9 +37,7 @@#include <sys/ioctl.h> #include <net/if.h> #include <termios.h> - -/* default slcan line discipline since Kernel 2.6.25 */ -#define LDISC_N_SLCAN 17 +#include <linux/tty.h> /* Change this to whatever your daemon is called */ #define DAEMON_NAME "slcand"
@@ -180,7 +178,7 @@ int main(int argc, char *argv[])char *btr =NULL ;int run_as_daemon =1 ;char *pch; -int ldisc =LDISC_N_SLCAN ; +int ldisc =N_SLCAN ;int fd; ttypath[0 ] ='\0' ;
Signature · 1 line
2.1.3
Re: [PATCH 4/5] use line discipline number from linux/tty.h
Hello Jeremiah,
thanks for your improvements!
Quoted 2 lines · Jeremiah Mahler
The line discipline number is provided in
include/linux/tty.h. Use that instead of a #define in each program.
I have a toolchain which is based on 2.6.18 kernel includes.
So I probably have to patch the can-utils when building the latest version of can-utils with my 'old' toolchain now.
Btw. you are right to use the common includes and remove this kind of hack.
So I applied your patches 1-4.
The UDEV rules are interesting too. But I wonder if a new 'doc' directory is the right place for it. Currently the can_if script to set the CAN interface bitrates and other configurations is placed in the can-misc repository in the 'etc' directory.
This is probably a wrong place too.
I have no real idea where to put these kind of useful scripts ...
Regards,
Oliver
Re: [PATCH 4/5] use line discipline number from linux/tty.h
Oliver,
Quoted 4 lines · Oliver Hartkopp
Hello Jeremiah,
thanks for your improvements!
Quoted 8 lines
Quoted 2 lines · Jeremiah Mahler
The line discipline number is provided in
include/linux/tty.h. Use that instead of a #define in each program.I have a toolchain which is based on 2.6.18 kernel includes. So I probably have to patch the can-utils when building the latest version of can-utils with my 'old' toolchain now.
Hmm, backward compatibility is good too.
As long as LDISC_N_SLCAN doesn't change the old way will work on every system. Maybe we should just leave it that way.
Quoted 9 lines
Btw. you are right to use the common includes and remove this kind of hack.
So I applied your patches 1-4.
The UDEV rules are interesting too. But I wonder if a new 'doc' directory is the right place for it. Currently the
can_ifscript to set the CAN interface bitrates and other configurations is placed in the can-misc repository in the 'etc' directory.
I wasn't sure about this either.
Debian packages often place example configuration files in doc/examples. This is what I had in mind, except I left out examples/ because two directory levels for two files seemed excessive.
Using etc/ does make logical sense for system wide configuration files. Some projects also use conf/ or config/.
I imagined these scripts being useful as examples. But they certainly could be installed system wide since the commands are not specific to any user. The can-utils .deb package could install these for example.
Quoted 4 lines
This is probably a wrong place too.
I have no real idea where to put these kind of useful scripts ...
I am starting to like the etc/ directory for system wide config files.
If these were user specific configuration files, like '.slcanrc', they would make sense in a doc/examples directory.
Quoted 3 lines
Regards,
Oliver
Signature · 1 line
- Jeremiah Mahler
PATCH 5/5 slcand: add example scripts for udev/systemd
In reply to Jeremiah MahlerThe slcand daemon must be started after a serial CAN device is plugged in to create the network interface (e.g. slcan0). This can be done manually but it is inconvenient.
Add example scripts for udev and systemd that can be used to start slcand automatically when plugged in and to stop it when unplugged.
--- doc/50-slcand.rules | 16 ++++++++++++++++ doc/[email protected] | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 doc/50-slcand.rules create mode 100644 doc/[email protected]
doc/50-slcand.rules new file +16 -0
@@ -0,0 +1,16 @@ +# +# udev/rules.d/50-slcand.rules +# +# When a LAWICEL LWX154GX serial CANUSB device is plugged in, +# tell systemd to start the slcand (daemon) service. When it +# is un-plugged, stop the service. +# +# The $kernel argument to the [email protected] is the +# device name (e.g. ttyUSB0). +# +# sudo systemctl status slcand@ttyUSB0 +# + +ACTION=="add", SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="ffa8", TAG+="systemd", ENV{SYSTEMD_WANTS}="slcand@$kernel" + +ACTION=="remove", ENV{DEVNAME}=="/dev/ttyUSB?", ENV{ID_MODEL_ID}=="ffa8", ENV{ID_VENDOR_ID}=="0403", RUN+="/bin/systemctl stop slcand@$kernel"
doc/[email protected] new file +11 -0
@@ -0,0 +1,11 @@ +# +# systemd/system/[email protected] +# + +[Unit] +Description=Serial CAN daemon (can-utils) + +[Service] +Type=forking +ExecStart=/usr/bin/slcand -o -c -f -s6 %I +# %I is the device name from udev (e.g. ttyUSB0)
Signature · 1 line
2.1.3
PATCH v2 5/5 slcand: add example scripts for udev/systemd
The slcand daemon must be started after a serial CAN device is plugged in to create the network interface (e.g. slcan0). This can be done manually but it is inconvenient.
Add example scripts for udev and systemd that can be used to start slcand automatically when plugged in and to stop it when unplugged.
---
Notes:
Placing these in etc/ allows them put in the same directory hierarchy where they would be normally installed.
etc/systemd/system/[email protected] | 11 +++++++++++ etc/udev/rules.d/50-slcand.rules | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 etc/systemd/system/[email protected] create mode 100644 etc/udev/rules.d/50-slcand.rules
etc/systemd/system/[email protected] new file +11 -0
@@ -0,0 +1,11 @@ +# +# systemd/system/[email protected] +# + +[Unit] +Description=Serial CAN daemon (can-utils) + +[Service] +Type=forking +ExecStart=/usr/bin/slcand -o -c -f -s6 %I +# %I is the device name from udev (e.g. ttyUSB0)
etc/udev/rules.d/50-slcand.rules new file +16 -0
@@ -0,0 +1,16 @@ +# +# udev/rules.d/50-slcand.rules +# +# When a LAWICEL LWX154GX serial CANUSB device is plugged in, +# tell systemd to start the slcand (daemon) service. When it +# is un-plugged, stop the service. +# +# The $kernel argument to the [email protected] is the +# device name (e.g. ttyUSB0). +# +# sudo systemctl status slcand@ttyUSB0 +# + +ACTION=="add", SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="ffa8", TAG+="systemd", ENV{SYSTEMD_WANTS}="slcand@$kernel" + +ACTION=="remove", ENV{DEVNAME}=="/dev/ttyUSB?", ENV{ID_MODEL_ID}=="ffa8", ENV{ID_VENDOR_ID}=="0403", RUN+="/bin/systemctl stop slcand@$kernel"
Signature · 1 line
2.1.4