* [PATCH 2/3] slcand: add option -p<file> to write pid to a file.
2015-06-08 11:55 [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Armin Burchardt
@ 2015-06-08 11:55 ` Armin Burchardt
2015-06-08 13:00 ` Marc Kleine-Budde
2015-06-08 11:55 ` [PATCH 3/3] slcand: add option -u to set interface up/down state Armin Burchardt
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ messages in thread
From: Armin Burchardt @ 2015-06-08 11:55 UTC (permalink / raw)
To: linux-can; +Cc: Armin Burchardt
Useful in combination with start-stop-daemon.
Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
---
slcand.c | 36 +++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/slcand.c b/slcand.c
index af7ca98..8fd1418 100644
--- a/slcand.c
+++ b/slcand.c
@@ -64,11 +64,13 @@ void print_usage(char *prg)
fprintf(stderr, " -t <type> (set UART flow control type 'hw' or 'sw')\n");
fprintf(stderr, " -b <btr> (set bit time register value)\n");
fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
+ fprintf(stderr, " -p <file> (write PID of daemon to file)\n");
fprintf(stderr, " -h (show this help page)\n");
fprintf(stderr, "\nExamples:\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, "slcand -o -c -f -s6 -p/var/run/slcand-can0.pid /dev/ttyUSB0\n");
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
@@ -178,12 +180,13 @@ int main(int argc, char *argv[])
char *btr = NULL;
int run_as_daemon = 1;
char *pch;
+ char *pidfile = NULL;
int ldisc = N_SLCAN;
int fd;
ttypath[0] = '\0';
- while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
+ while ((opt = getopt(argc, argv, "ocfs:S:t:b:p:?hF")) != -1) {
switch (opt) {
case 'o':
send_open = 1;
@@ -228,6 +231,9 @@ int main(int argc, char *argv[])
case 'F':
run_as_daemon = 0;
break;
+ case 'p':
+ pidfile = optarg;
+ break;
case 'h':
case '?':
default:
@@ -363,6 +369,27 @@ int main(int argc, char *argv[])
signal(SIGTERM, child_handler);
}
+ /* write pidfile */
+ if (pidfile) {
+ FILE *pidfd;
+ pidfd = fopen(pidfile, "w");
+ if (pidfd == NULL) {
+ syslog(LOG_ERR, "could not open pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ if (fprintf(pidfd, "%i\n", getpid()) < 0) {
+ syslog(LOG_ERR, "could not write to pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(pidfd) != 0) {
+ syslog(LOG_ERR, "could not close pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ }
+
/* */
slcand_running = 1;
@@ -391,6 +418,13 @@ int main(int argc, char *argv[])
if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
+ /* Remove pidfile */
+ if (pidfile) {
+ if (unlink(pidfile) < 0) {
+ syslog(LOG_ERR, "Can not remove pidfile '%s': %s", pidfile, strerror(errno));
+ }
+ }
+
/* Finish up */
syslog(LOG_NOTICE, "terminated on %s", ttypath);
closelog();
--
1.9.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH 2/3] slcand: add option -p<file> to write pid to a file.
2015-06-08 11:55 ` [PATCH 2/3] slcand: add option -p<file> to write pid to a file Armin Burchardt
@ 2015-06-08 13:00 ` Marc Kleine-Budde
2015-06-08 13:43 ` Armin Burchardt
0 siblings, 1 reply; 11+ messages in thread
From: Marc Kleine-Budde @ 2015-06-08 13:00 UTC (permalink / raw)
To: Armin Burchardt, linux-can
[-- Attachment #1: Type: text/plain, Size: 3554 bytes --]
On 06/08/2015 01:55 PM, Armin Burchardt wrote:
> Useful in combination with start-stop-daemon.
>
> Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
> ---
> slcand.c | 36 +++++++++++++++++++++++++++++++++++-
> 1 file changed, 35 insertions(+), 1 deletion(-)
>
> diff --git a/slcand.c b/slcand.c
> index af7ca98..8fd1418 100644
> --- a/slcand.c
> +++ b/slcand.c
> @@ -64,11 +64,13 @@ void print_usage(char *prg)
> fprintf(stderr, " -t <type> (set UART flow control type 'hw' or 'sw')\n");
> fprintf(stderr, " -b <btr> (set bit time register value)\n");
> fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
> + fprintf(stderr, " -p <file> (write PID of daemon to file)\n");
> fprintf(stderr, " -h (show this help page)\n");
> fprintf(stderr, "\nExamples:\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, "slcand -o -c -f -s6 -p/var/run/slcand-can0.pid /dev/ttyUSB0\n");
I think "/run/slcand/<IFACE>" would fit better into modern file system
layout. What about having a default for the pid file path?
> fprintf(stderr, "\n");
> exit(EXIT_FAILURE);
> }
> @@ -178,12 +180,13 @@ int main(int argc, char *argv[])
> char *btr = NULL;
> int run_as_daemon = 1;
> char *pch;
> + char *pidfile = NULL;
> int ldisc = N_SLCAN;
> int fd;
>
> ttypath[0] = '\0';
>
> - while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
> + while ((opt = getopt(argc, argv, "ocfs:S:t:b:p:?hF")) != -1) {
> switch (opt) {
> case 'o':
> send_open = 1;
> @@ -228,6 +231,9 @@ int main(int argc, char *argv[])
> case 'F':
> run_as_daemon = 0;
> break;
> + case 'p':
> + pidfile = optarg;
> + break;
> case 'h':
> case '?':
> default:
> @@ -363,6 +369,27 @@ int main(int argc, char *argv[])
> signal(SIGTERM, child_handler);
> }
>
> + /* write pidfile */
> + if (pidfile) {
> + FILE *pidfd;
> + pidfd = fopen(pidfile, "w");
> + if (pidfd == NULL) {
if (!pidfd)
Does it make sense to open in exclusive mode?
Technically speaking, it's not a file descriptor "fd" :)
> + syslog(LOG_ERR, "could not open pidfile");
> + perror(pidfile);
> + exit(EXIT_FAILURE);
> + }
> + if (fprintf(pidfd, "%i\n", getpid()) < 0) {
> + syslog(LOG_ERR, "could not write to pidfile");
> + perror(pidfile);
> + exit(EXIT_FAILURE);
> + }
> + if (fclose(pidfd) != 0) {
> + syslog(LOG_ERR, "could not close pidfile");
> + perror(pidfile);
> + exit(EXIT_FAILURE);
> + }
> + }
> +
> /* */
> slcand_running = 1;
>
> @@ -391,6 +418,13 @@ int main(int argc, char *argv[])
> if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
> syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
>
> + /* Remove pidfile */
> + if (pidfile) {
> + if (unlink(pidfile) < 0) {
> + syslog(LOG_ERR, "Can not remove pidfile '%s': %s", pidfile, strerror(errno));
> + }
> + }
> +
> /* Finish up */
> syslog(LOG_NOTICE, "terminated on %s", ttypath);
> closelog();
>
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH 2/3] slcand: add option -p<file> to write pid to a file.
2015-06-08 13:00 ` Marc Kleine-Budde
@ 2015-06-08 13:43 ` Armin Burchardt
2015-06-08 13:55 ` Marc Kleine-Budde
0 siblings, 1 reply; 11+ messages in thread
From: Armin Burchardt @ 2015-06-08 13:43 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: linux-can
Hi Marc,
> I think "/run/slcand/<IFACE>" would fit better into modern file system
> layout. What about having a default for the pid file path?
I would not expect a daemon to accept a filename (or only parts of it) without
a full path. The /etc/init.d/skeleton of Debian wheezy uses /var/run/$NAME.pid
(for start-stop-daemon's --pidfile option).
Do we know enough about the filesystem layout of existing (embedded)
systems to define
a useful default?
> Does it make sense to open in exclusive mode?
If another daemon is started with the same pidfile we would have to give
up if the file already exists to take advantage of that. Is it
expected of a daemon
using a pidfile to exit with an error or should it just overwrite an
existing file?
Armin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 2/3] slcand: add option -p<file> to write pid to a file.
2015-06-08 13:43 ` Armin Burchardt
@ 2015-06-08 13:55 ` Marc Kleine-Budde
0 siblings, 0 replies; 11+ messages in thread
From: Marc Kleine-Budde @ 2015-06-08 13:55 UTC (permalink / raw)
To: Armin Burchardt; +Cc: linux-can
[-- Attachment #1: Type: text/plain, Size: 1428 bytes --]
On 06/08/2015 03:43 PM, Armin Burchardt wrote:
> Hi Marc,
>
>> I think "/run/slcand/<IFACE>" would fit better into modern file system
>> layout. What about having a default for the pid file path?
>
> I would not expect a daemon to accept a filename (or only parts of it) without
Me neither. I was thinking that "-p" without a path would generate a
pidfile in "/run/slcand/<IFACE>"
> a full path. The /etc/init.d/skeleton of Debian wheezy uses /var/run/$NAME.pid
wheezy (a.k.a. old-old-stable)? Ist that still alive? :D
> (for start-stop-daemon's --pidfile option).
> Do we know enough about the filesystem layout of existing (embedded)
> systems to define
> a useful default?
systemd uses "/run/<daemon>.pid" or "/run/<daemon>/<foo>.pid"
>> Does it make sense to open in exclusive mode?
>
> If another daemon is started with the same pidfile we would have to give
> up if the file already exists to take advantage of that. Is it
> expected of a daemon
> using a pidfile to exit with an error or should it just overwrite an
> existing file?
Right....probably not worth the trouble, keep it as is.
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 3/3] slcand: add option -u to set interface up/down state.
2015-06-08 11:55 [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Armin Burchardt
2015-06-08 11:55 ` [PATCH 2/3] slcand: add option -p<file> to write pid to a file Armin Burchardt
@ 2015-06-08 11:55 ` Armin Burchardt
2015-06-08 12:51 ` [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Marc Kleine-Budde
2015-06-09 9:15 ` [PATCH v2 0/3] slcand: improved patches Armin Burchardt
3 siblings, 0 replies; 11+ messages in thread
From: Armin Burchardt @ 2015-06-08 11:55 UTC (permalink / raw)
To: linux-can; +Cc: Armin Burchardt
Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
---
slcand.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 62 insertions(+), 1 deletion(-)
diff --git a/slcand.c b/slcand.c
index 8fd1418..aa1c79b 100644
--- a/slcand.c
+++ b/slcand.c
@@ -65,6 +65,7 @@ void print_usage(char *prg)
fprintf(stderr, " -b <btr> (set bit time register value)\n");
fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
fprintf(stderr, " -p <file> (write PID of daemon to file)\n");
+ fprintf(stderr, " -u (control interface state ('UP' and 'DOWN')\n");
fprintf(stderr, " -h (show this help page)\n");
fprintf(stderr, "\nExamples:\n");
fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
@@ -159,6 +160,43 @@ static int look_up_uart_speed(long int s)
}
}
+/* set selected interface flags */
+static int candev_setflags(const char* name, __u32 flags, __u32 mask)
+{
+ int err = 0;
+ struct ifreq ifr;
+ int fd;
+
+ if (strlen(name) >= IFNAMSIZ) {
+ return -1;
+ }
+ fd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ perror("socket()");
+ return -1;
+ }
+ /* copy interface name to ioctl data */
+ strncpy(ifr.ifr_name, name, IFNAMSIZ);
+ /* get flags for interface with that name */
+ err = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (err == -1) {
+ perror("SIOCGIFFLAGS");
+ } else {
+ /* clear all flags which are selected in mask */
+ ifr.ifr_flags &= ~mask;
+ /* set requested flags which are selected in mask,
+ * keep values of flags which are not selected. */
+ ifr.ifr_flags |= mask&flags;
+ /* write flags back to interface */
+ err = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (err == -1) {
+ perror("SIOSSIFFLAGS");
+ }
+ }
+ close(fd);
+ return err;
+}
+
int main(int argc, char *argv[])
{
char *tty = NULL;
@@ -173,6 +211,7 @@ int main(int argc, char *argv[])
int send_open = 0;
int send_close = 0;
int send_read_status_flags = 0;
+ int control_ifstate = 0;
char *speed = NULL;
char *uart_speed_str = NULL;
long int uart_speed = 0;
@@ -186,7 +225,7 @@ int main(int argc, char *argv[])
ttypath[0] = '\0';
- while ((opt = getopt(argc, argv, "ocfs:S:t:b:p:?hF")) != -1) {
+ while ((opt = getopt(argc, argv, "ocfs:S:t:b:p:u?hF")) != -1) {
switch (opt) {
case 'o':
send_open = 1;
@@ -234,6 +273,9 @@ int main(int argc, char *argv[])
case 'p':
pidfile = optarg;
break;
+ case 'u':
+ control_ifstate = 1;
+ break;
case 'h':
case '?':
default:
@@ -355,6 +397,18 @@ int main(int argc, char *argv[])
}
}
+ /* set interface state to 'UP' */
+ if (control_ifstate) {
+ if (candev_setflags(name ? name : buf, IFF_UP, IFF_UP) == -1) {
+ syslog(LOG_ERR, "could not set %s state to 'UP'", buf);
+ exit(EXIT_FAILURE);
+ } else {
+ syslog(LOG_NOTICE, "%s: set flag 'UP'", buf);
+ }
+ } else {
+ syslog(LOG_NOTICE, "please remember to set %s to 'UP'", buf);
+ }
+
/* Daemonize (_after_ interface is up to avoid race-conditions,
* ie. between init.d-scripts). */
if (run_as_daemon) {
@@ -397,6 +451,13 @@ int main(int argc, char *argv[])
while (slcand_running)
sleep(1); /* wait 1 second */
+ /* set interface state to 'DOWN' */
+ if (control_ifstate) {
+ /* clear 'IFF_UP' flag */
+ candev_setflags(name ? name : buf, ~IFF_UP, IFF_UP);
+ syslog(LOG_ERR, "could not set %s state to 'DOWN'", name);
+ }
+
/* Reset line discipline */
syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
ldisc = N_TTY;
--
1.9.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up.
2015-06-08 11:55 [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Armin Burchardt
2015-06-08 11:55 ` [PATCH 2/3] slcand: add option -p<file> to write pid to a file Armin Burchardt
2015-06-08 11:55 ` [PATCH 3/3] slcand: add option -u to set interface up/down state Armin Burchardt
@ 2015-06-08 12:51 ` Marc Kleine-Budde
2015-06-09 9:15 ` [PATCH v2 0/3] slcand: improved patches Armin Burchardt
3 siblings, 0 replies; 11+ messages in thread
From: Marc Kleine-Budde @ 2015-06-08 12:51 UTC (permalink / raw)
To: Armin Burchardt, linux-can
[-- Attachment #1: Type: text/plain, Size: 2152 bytes --]
On 06/08/2015 01:55 PM, Armin Burchardt wrote:
> Create interface, then daemonize to avoid race-conditions,
> ie. in init.d-scripts.
>
> Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
> ---
> slcand.c | 35 ++++++++++++++++++-----------------
> 1 file changed, 18 insertions(+), 17 deletions(-)
>
> diff --git a/slcand.c b/slcand.c
> index e5e4752..af7ca98 100644
> --- a/slcand.c
> +++ b/slcand.c
> @@ -255,23 +255,7 @@ int main(int argc, char *argv[])
>
> syslog(LOG_INFO, "starting on TTY device %s", ttypath);
>
> - /* Daemonize */
> - if (run_as_daemon) {
> - if (daemon(0, 0)) {
> - syslog(LOG_ERR, "failed to daemonize");
> - exit(EXIT_FAILURE);
> - }
> - }
> - else {
> - /* Trap signals that we expect to receive */
> - signal(SIGINT, child_handler);
> - signal(SIGTERM, child_handler);
> - }
> -
> - /* */
> - slcand_running = 1;
> -
> - /* Now we are a daemon -- do the work for which we were paid */
> + /* Create file handle for tty device. */
> fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
> if (fd < 0) {
> syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
> @@ -365,6 +349,23 @@ int main(int argc, char *argv[])
> }
> }
>
> + /* Daemonize (_after_ interface is up to avoid race-conditions,
> + * ie. between init.d-scripts). */
> + if (run_as_daemon) {
> + if (daemon(0, 0)) {
> + syslog(LOG_ERR, "failed to daemonize");
> + exit(EXIT_FAILURE);
> + }
> + }
> + else {
Please change to
} else {
> + /* Trap signals that we expect to receive */
> + signal(SIGINT, child_handler);
> + signal(SIGTERM, child_handler);
> + }
> +
> + /* */
Empty comment? I know you just moved the code around :)
> + slcand_running = 1;
> +
> /* The Big Loop */
> while (slcand_running)
> sleep(1); /* wait 1 second */
>
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v2 0/3] slcand: improved patches
2015-06-08 11:55 [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Armin Burchardt
` (2 preceding siblings ...)
2015-06-08 12:51 ` [PATCH 1/3] slcand: daemonize _after_ interface is created, renamed and up Marc Kleine-Budde
@ 2015-06-09 9:15 ` Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 1/3] slcand: daemonize _after_ interface is created and renamed Armin Burchardt
` (2 more replies)
3 siblings, 3 replies; 11+ messages in thread
From: Armin Burchardt @ 2015-06-09 9:15 UTC (permalink / raw)
To: linux-can
These patches contain changes requested on the list.
- coding style ( "} else {" ).
- default pidfile name "/run/slcand.pid". Generating a pidfile
name based on the tty device name would require more effort, this
could be done in a follow-up patch.
- name change of stream variable from pidfd to stream.
In addition:
- candev_setflags(): 'short' is used instead of '__u32'. This
type is also used in the ioctl struct definition.
Armin (second time running 'git send-email' ever)
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v2 1/3] slcand: daemonize _after_ interface is created and renamed
2015-06-09 9:15 ` [PATCH v2 0/3] slcand: improved patches Armin Burchardt
@ 2015-06-09 9:15 ` Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 2/3] slcand: add option -p<file> to write pid to a file Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 3/3] slcand: add option -u to set interface up/down state Armin Burchardt
2 siblings, 0 replies; 11+ messages in thread
From: Armin Burchardt @ 2015-06-09 9:15 UTC (permalink / raw)
To: linux-can; +Cc: Armin Burchardt
Create interface, then daemonize to avoid race-conditions,
ie. in init.d-scripts.
Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
---
slcand.c | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/slcand.c b/slcand.c
index e5e4752..9373361 100644
--- a/slcand.c
+++ b/slcand.c
@@ -255,23 +255,7 @@ int main(int argc, char *argv[])
syslog(LOG_INFO, "starting on TTY device %s", ttypath);
- /* Daemonize */
- if (run_as_daemon) {
- if (daemon(0, 0)) {
- syslog(LOG_ERR, "failed to daemonize");
- exit(EXIT_FAILURE);
- }
- }
- else {
- /* Trap signals that we expect to receive */
- signal(SIGINT, child_handler);
- signal(SIGTERM, child_handler);
- }
-
- /* */
- slcand_running = 1;
-
- /* Now we are a daemon -- do the work for which we were paid */
+ /* Create file handle for tty device. */
fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd < 0) {
syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
@@ -365,7 +349,21 @@ int main(int argc, char *argv[])
}
}
+ /* Daemonize (_after_ interface is created and renamed to avoid race-conditions,
+ * ie. between init.d-scripts). */
+ if (run_as_daemon) {
+ if (daemon(0, 0)) {
+ syslog(LOG_ERR, "failed to daemonize");
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ /* Trap signals that we expect to receive */
+ signal(SIGINT, child_handler);
+ signal(SIGTERM, child_handler);
+ }
+
/* The Big Loop */
+ slcand_running = 1;
while (slcand_running)
sleep(1); /* wait 1 second */
--
1.9.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v2 2/3] slcand: add option -p<file> to write pid to a file.
2015-06-09 9:15 ` [PATCH v2 0/3] slcand: improved patches Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 1/3] slcand: daemonize _after_ interface is created and renamed Armin Burchardt
@ 2015-06-09 9:15 ` Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 3/3] slcand: add option -u to set interface up/down state Armin Burchardt
2 siblings, 0 replies; 11+ messages in thread
From: Armin Burchardt @ 2015-06-09 9:15 UTC (permalink / raw)
To: linux-can; +Cc: Armin Burchardt
Useful in combination with start-stop-daemon.
Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
---
slcand.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/slcand.c b/slcand.c
index 9373361..e3c5f18 100644
--- a/slcand.c
+++ b/slcand.c
@@ -45,6 +45,8 @@
/* Change this to the user under which to run */
#define RUN_AS_USER "root"
+#define DEFAULT_PIDFILE "/run/slcand.pid"
+
/* The length of ttypath buffer */
#define TTYPATH_LENGTH 64
@@ -64,11 +66,13 @@ void print_usage(char *prg)
fprintf(stderr, " -t <type> (set UART flow control type 'hw' or 'sw')\n");
fprintf(stderr, " -b <btr> (set bit time register value)\n");
fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
+ fprintf(stderr, " -p [file] (write PID of daemon to file. without argument, write to '%s')\n", DEFAULT_PIDFILE);
fprintf(stderr, " -h (show this help page)\n");
fprintf(stderr, "\nExamples:\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, "slcand -o -c -f -s6 -p/run/slcand/ttyUSB0.pid /dev/ttyUSB0\n");
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
@@ -171,6 +175,8 @@ int main(int argc, char *argv[])
int send_open = 0;
int send_close = 0;
int send_read_status_flags = 0;
+ int create_pidfile = 0;
+ char *pidfile = NULL;
char *speed = NULL;
char *uart_speed_str = NULL;
long int uart_speed = 0;
@@ -183,7 +189,7 @@ int main(int argc, char *argv[])
ttypath[0] = '\0';
- while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
+ while ((opt = getopt(argc, argv, "ocfs:S:t:b:p::?hF")) != -1) {
switch (opt) {
case 'o':
send_open = 1;
@@ -228,6 +234,15 @@ int main(int argc, char *argv[])
case 'F':
run_as_daemon = 0;
break;
+ case 'p':
+ create_pidfile = 1;
+ /* argument is optional */
+ if (optarg) {
+ pidfile = optarg;
+ } else {
+ pidfile = DEFAULT_PIDFILE;
+ }
+ break;
case 'h':
case '?':
default:
@@ -362,6 +377,27 @@ int main(int argc, char *argv[])
signal(SIGTERM, child_handler);
}
+ /* write pidfile */
+ if (create_pidfile) {
+ FILE *stream;
+ stream = fopen(pidfile, "w");
+ if (stream == NULL) {
+ syslog(LOG_ERR, "could not open pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ if (fprintf(stream, "%i\n", getpid()) < 0) {
+ syslog(LOG_ERR, "could not write to pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(stream) != 0) {
+ syslog(LOG_ERR, "could not close pidfile");
+ perror(pidfile);
+ exit(EXIT_FAILURE);
+ }
+ }
+
/* The Big Loop */
slcand_running = 1;
while (slcand_running)
@@ -388,6 +424,13 @@ int main(int argc, char *argv[])
if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
+ /* Remove pidfile */
+ if (pidfile) {
+ if (unlink(pidfile) < 0) {
+ syslog(LOG_ERR, "Can not remove pidfile '%s': %s", pidfile, strerror(errno));
+ }
+ }
+
/* Finish up */
syslog(LOG_NOTICE, "terminated on %s", ttypath);
closelog();
--
1.9.1
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v2 3/3] slcand: add option -u to set interface up/down state.
2015-06-09 9:15 ` [PATCH v2 0/3] slcand: improved patches Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 1/3] slcand: daemonize _after_ interface is created and renamed Armin Burchardt
2015-06-09 9:15 ` [PATCH v2 2/3] slcand: add option -p<file> to write pid to a file Armin Burchardt
@ 2015-06-09 9:15 ` Armin Burchardt
2 siblings, 0 replies; 11+ messages in thread
From: Armin Burchardt @ 2015-06-09 9:15 UTC (permalink / raw)
To: linux-can; +Cc: Armin Burchardt
Setting the interface up with 'ip link set <CANDEV> up' is not required
when using the -u option.
Maybe we can make this the default behaviour.
Signed-off-by: Armin Burchardt <armin@uni-bremen.de>
---
slcand.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 63 insertions(+), 2 deletions(-)
diff --git a/slcand.c b/slcand.c
index e3c5f18..3829c91 100644
--- a/slcand.c
+++ b/slcand.c
@@ -67,6 +67,7 @@ void print_usage(char *prg)
fprintf(stderr, " -b <btr> (set bit time register value)\n");
fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
fprintf(stderr, " -p [file] (write PID of daemon to file. without argument, write to '%s')\n", DEFAULT_PIDFILE);
+ fprintf(stderr, " -u (control interface state ('UP' and 'DOWN')\n");
fprintf(stderr, " -h (show this help page)\n");
fprintf(stderr, "\nExamples:\n");
fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
@@ -161,6 +162,43 @@ static int look_up_uart_speed(long int s)
}
}
+/* set selected interface flags */
+static int candev_setflags(const char* name, short flags, short mask)
+{
+ int err = 0;
+ struct ifreq ifr;
+ int fd;
+
+ if (strlen(name) >= IFNAMSIZ) {
+ return -1;
+ }
+ fd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ perror("socket()");
+ return -1;
+ }
+ /* copy interface name to ioctl data */
+ strncpy(ifr.ifr_name, name, IFNAMSIZ);
+ /* get flags for interface with that name */
+ err = ioctl(fd, SIOCGIFFLAGS, &ifr);
+ if (err == -1) {
+ perror("SIOCGIFFLAGS");
+ } else {
+ /* clear all flags which are selected in mask */
+ ifr.ifr_flags &= ~mask;
+ /* set requested flags which are selected in mask,
+ * keep values of flags which are not selected. */
+ ifr.ifr_flags |= mask&flags;
+ /* write flags back to interface */
+ err = ioctl(fd, SIOCSIFFLAGS, &ifr);
+ if (err == -1) {
+ perror("SIOSSIFFLAGS");
+ }
+ }
+ close(fd);
+ return err;
+}
+
int main(int argc, char *argv[])
{
char *tty = NULL;
@@ -177,6 +215,7 @@ int main(int argc, char *argv[])
int send_read_status_flags = 0;
int create_pidfile = 0;
char *pidfile = NULL;
+ int control_ifstate = 0;
char *speed = NULL;
char *uart_speed_str = NULL;
long int uart_speed = 0;
@@ -189,7 +228,7 @@ int main(int argc, char *argv[])
ttypath[0] = '\0';
- while ((opt = getopt(argc, argv, "ocfs:S:t:b:p::?hF")) != -1) {
+ while ((opt = getopt(argc, argv, "ocfs:S:t:b:p::u?hF")) != -1) {
switch (opt) {
case 'o':
send_open = 1;
@@ -243,6 +282,9 @@ int main(int argc, char *argv[])
pidfile = DEFAULT_PIDFILE;
}
break;
+ case 'u':
+ control_ifstate = 1;
+ break;
case 'h':
case '?':
default:
@@ -364,7 +406,19 @@ int main(int argc, char *argv[])
}
}
- /* Daemonize (_after_ interface is created and renamed to avoid race-conditions,
+ /* set interface state to 'UP' */
+ if (control_ifstate) {
+ if (candev_setflags(name ? name : buf, IFF_UP, IFF_UP) == -1) {
+ syslog(LOG_ERR, "could not set %s state to 'UP'", buf);
+ exit(EXIT_FAILURE);
+ } else {
+ syslog(LOG_NOTICE, "%s: set flag 'UP'", buf);
+ }
+ } else {
+ syslog(LOG_NOTICE, "please remember to set %s to 'UP'", buf);
+ }
+
+ /* Daemonize (_after_ interface is created, up and renamed to avoid race-conditions,
* ie. between init.d-scripts). */
if (run_as_daemon) {
if (daemon(0, 0)) {
@@ -403,6 +457,13 @@ int main(int argc, char *argv[])
while (slcand_running)
sleep(1); /* wait 1 second */
+ /* set interface state to 'DOWN' */
+ if (control_ifstate) {
+ /* clear 'IFF_UP' flag */
+ candev_setflags(name ? name : buf, ~IFF_UP, IFF_UP);
+ syslog(LOG_ERR, "could not set %s state to 'DOWN'", name);
+ }
+
/* Reset line discipline */
syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
ldisc = N_TTY;
--
1.9.1
^ permalink raw reply related [flat|nested] 11+ messages in thread