All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts
@ 2014-12-19 15:22 Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 1/5] slcand: improve usage examples Jeremiah Mahler
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

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/slcand@.service | 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/slcand@.service

-- 
2.1.3


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/5] slcand: improve usage examples
  2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
@ 2014-12-19 15:22 ` Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 2/5] slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS Jeremiah Mahler
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

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.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 slcand.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/slcand.c b/slcand.c
index d71ba4d..55775da 100644
--- a/slcand.c
+++ b/slcand.c
@@ -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);
 }
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/5] slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS
  2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 1/5] slcand: improve usage examples Jeremiah Mahler
@ 2014-12-19 15:22 ` Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 3/5] slcand: consistent use of exit(EXIT_FAILURE) Jeremiah Mahler
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

slcand has #defines for EXIT_FAILURE and EXIT_SUCCESS but this is
unnecessary since they are already provided by stdlib.h.  Remove them.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 slcand.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/slcand.c b/slcand.c
index 55775da..c4c81c5 100644
--- a/slcand.c
+++ b/slcand.c
@@ -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
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/5] slcand: consistent use of exit(EXIT_FAILURE)
  2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 1/5] slcand: improve usage examples Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 2/5] slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS Jeremiah Mahler
@ 2014-12-19 15:22 ` Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 4/5] use line discipline number from linux/tty.h Jeremiah Mahler
  2014-12-19 15:22 ` [PATCH 5/5] slcand: add example scripts for udev/systemd Jeremiah Mahler
  4 siblings, 0 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

Some places used exit(1) and others used exit(EXIT_FAILURE).
Be consistent and use exit(EXIT_FAILURE) for all of these instances.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 slcand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/slcand.c b/slcand.c
index c4c81c5..b72fa4e 100644
--- a/slcand.c
+++ b/slcand.c
@@ -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);
 
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/5] use line discipline number from linux/tty.h
  2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
                   ` (2 preceding siblings ...)
  2014-12-19 15:22 ` [PATCH 3/5] slcand: consistent use of exit(EXIT_FAILURE) Jeremiah Mahler
@ 2014-12-19 15:22 ` Jeremiah Mahler
  2014-12-23 17:43   ` Oliver Hartkopp
  2014-12-19 15:22 ` [PATCH 5/5] slcand: add example scripts for udev/systemd Jeremiah Mahler
  4 siblings, 1 reply; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

The line discipline number is provided in include/linux/tty.h.
Use that instead of a #define in each program.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 slcan_attach.c | 5 ++---
 slcand.c       | 6 ++----
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/slcan_attach.c b/slcan_attach.c
index 2c95d22..ab8ff0a 100644
--- a/slcan_attach.c
+++ b/slcan_attach.c
@@ -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;
diff --git a/slcand.c b/slcand.c
index b72fa4e..e5e4752 100644
--- a/slcand.c
+++ b/slcand.c
@@ -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';
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 5/5] slcand: add example scripts for udev/systemd
  2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
                   ` (3 preceding siblings ...)
  2014-12-19 15:22 ` [PATCH 4/5] use line discipline number from linux/tty.h Jeremiah Mahler
@ 2014-12-19 15:22 ` Jeremiah Mahler
  2014-12-27 21:10   ` [PATCH 5/5 v2] " Jeremiah Mahler
  4 siblings, 1 reply; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-19 15:22 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

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.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---
 doc/50-slcand.rules | 16 ++++++++++++++++
 doc/slcand@.service | 11 +++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 doc/50-slcand.rules
 create mode 100644 doc/slcand@.service

diff --git a/doc/50-slcand.rules b/doc/50-slcand.rules
new file mode 100644
index 0000000..bc96e55
--- /dev/null
+++ b/doc/50-slcand.rules
@@ -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 slcand@.service 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"
diff --git a/doc/slcand@.service b/doc/slcand@.service
new file mode 100644
index 0000000..b823549
--- /dev/null
+++ b/doc/slcand@.service
@@ -0,0 +1,11 @@
+#
+# systemd/system/slcand@.service
+#
+
+[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)
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 4/5] use line discipline number from linux/tty.h
  2014-12-19 15:22 ` [PATCH 4/5] use line discipline number from linux/tty.h Jeremiah Mahler
@ 2014-12-23 17:43   ` Oliver Hartkopp
  2014-12-24  6:20     ` Jeremiah Mahler
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Hartkopp @ 2014-12-23 17:43 UTC (permalink / raw)
  To: Jeremiah Mahler; +Cc: linux-can

Hello Jeremiah,

thanks for your improvements!

On 19.12.2014 16:22, Jeremiah Mahler wrote:
> 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 4/5] use line discipline number from linux/tty.h
  2014-12-23 17:43   ` Oliver Hartkopp
@ 2014-12-24  6:20     ` Jeremiah Mahler
  0 siblings, 0 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-24  6:20 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can

Oliver,

On Tue, Dec 23, 2014 at 06:43:57PM +0100, Oliver Hartkopp wrote:
> Hello Jeremiah,
> 
> thanks for your improvements!
> 
> On 19.12.2014 16:22, Jeremiah Mahler wrote:
> >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.

> 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.
> 
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.

> 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.

> Regards,
> Oliver
> 

-- 
- Jeremiah Mahler

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 5/5 v2] slcand: add example scripts for udev/systemd
  2014-12-19 15:22 ` [PATCH 5/5] slcand: add example scripts for udev/systemd Jeremiah Mahler
@ 2014-12-27 21:10   ` Jeremiah Mahler
  0 siblings, 0 replies; 9+ messages in thread
From: Jeremiah Mahler @ 2014-12-27 21:10 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, Jeremiah Mahler

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.

Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
---

Notes:
    Placing these in etc/ allows them put in the same directory
    hierarchy where they would be normally installed.

 etc/systemd/system/slcand@.service | 11 +++++++++++
 etc/udev/rules.d/50-slcand.rules   | 16 ++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 etc/systemd/system/slcand@.service
 create mode 100644 etc/udev/rules.d/50-slcand.rules

diff --git a/etc/systemd/system/slcand@.service b/etc/systemd/system/slcand@.service
new file mode 100644
index 0000000..b823549
--- /dev/null
+++ b/etc/systemd/system/slcand@.service
@@ -0,0 +1,11 @@
+#
+# systemd/system/slcand@.service
+#
+
+[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)
diff --git a/etc/udev/rules.d/50-slcand.rules b/etc/udev/rules.d/50-slcand.rules
new file mode 100644
index 0000000..bc96e55
--- /dev/null
+++ b/etc/udev/rules.d/50-slcand.rules
@@ -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 slcand@.service 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"
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-12-27 21:10 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-19 15:22 [PATCH 0/5] can-utils: slcand: minor fixes and udev scripts Jeremiah Mahler
2014-12-19 15:22 ` [PATCH 1/5] slcand: improve usage examples Jeremiah Mahler
2014-12-19 15:22 ` [PATCH 2/5] slcand: remove #define EXIT_FAILURE, EXIT_SUCCESS Jeremiah Mahler
2014-12-19 15:22 ` [PATCH 3/5] slcand: consistent use of exit(EXIT_FAILURE) Jeremiah Mahler
2014-12-19 15:22 ` [PATCH 4/5] use line discipline number from linux/tty.h Jeremiah Mahler
2014-12-23 17:43   ` Oliver Hartkopp
2014-12-24  6:20     ` Jeremiah Mahler
2014-12-19 15:22 ` [PATCH 5/5] slcand: add example scripts for udev/systemd Jeremiah Mahler
2014-12-27 21:10   ` [PATCH 5/5 v2] " Jeremiah Mahler

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.