linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Armin Burchardt <armin@uni-bremen.de>
To: linux-can@vger.kernel.org
Cc: Armin Burchardt <armin@uni-bremen.de>
Subject: [PATCH 2/3] slcand: add option -p<file> to write pid to a file.
Date: Mon,  8 Jun 2015 13:55:39 +0200	[thread overview]
Message-ID: <1433764540-24917-2-git-send-email-armin@uni-bremen.de> (raw)
In-Reply-To: <1433764540-24917-1-git-send-email-armin@uni-bremen.de>

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


  reply	other threads:[~2015-06-08 11:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2015-06-08 13:00   ` [PATCH 2/3] slcand: add option -p<file> to write pid to a file Marc Kleine-Budde
2015-06-08 13:43     ` Armin Burchardt
2015-06-08 13:55       ` Marc Kleine-Budde
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 ` [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
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   ` [PATCH v2 3/3] slcand: add option -u to set interface up/down state Armin Burchardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433764540-24917-2-git-send-email-armin@uni-bremen.de \
    --to=armin@uni-bremen.de \
    --cc=linux-can@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).