[LinuxPPS] patch for ppsldisc

Folkert van Heusden folkert at vanheusden.com
Thu Sep 18 20:00:56 CEST 2008


Hi,

This patch changes ppsldisc: it now becomes a daemon process by itself
and writes its PID to a file in /var/run (like other daemons do). That
way one can have a cleaner ntpd start/stop-script (kill -9 `cat
/var/run/ppsldisc.pid` instead of a killall).

diff -uNrBbd pps.org/ppsldisc.c pps/ppsldisc.c
--- pps.org/ppsldisc.c	2008-09-18 15:51:52.000000000 +0200
+++ pps/ppsldisc.c	2008-09-18 19:55:44.000000000 +0200
@@ -7,40 +7,141 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifndef N_PPS
 #define N_PPS __N_PPS
 #endif
 
+#define DEFAULT_DEV	"/dev/ttyS0"
+#define DEFAULT_PID	"/var/run/ppsldisc.pid"
+
+void write_pidfile(char *file)
+{
+	FILE *fh = fopen(file, "w");
+	if (!fh)
+	{
+		fprintf(stderr, "Error writing to file %s\n", file);
+		exit(1);
+	}
+
+	fprintf(fh, "%i", getpid());
+
+	fclose(fh);
+}
+
 void usage(char *name)
 {
 	fprintf(stderr, "usage: %s <ttyS>\n", name);
 
+	fprintf(stderr, "-e    exit after setting (might not work in all situations)\n");
+	fprintf(stderr, "-f    do not fork into the background\n");
+	fprintf(stderr, "-d x  use device 'x', default is %s\n", DEFAULT_DEV);
+	fprintf(stderr, "-p x  write pid to file 'x', default is %s\n", DEFAULT_PID);
+	fprintf(stderr, "-v    be more verbose\n");
+	fprintf(stderr, "-V    show version\n");
+	fprintf(stderr, "-h    this help\n");
+
 	exit(EXIT_FAILURE);
 }
 
+void version(void)
+{
+	fprintf(stderr, "ppsldisc v2.0\n\n");
+	fprintf(stderr, "Original version by Rodolfo Giometti\n");
+	fprintf(stderr, "Version 2.0 by folkert at vanheusden.com\n");
+}
+
 int main(int argc, char *argv[])
 {
 	int fd;
 	int ldisc = N_PPS;
 	int ret;
+	char *dev = DEFAULT_DEV;
+	char *pidfile = DEFAULT_PID;
+	int verbose = 0;
+	int c;
+	char dofork = 1, forever = 1;
 
-	if (argc < 2)
+	while((c = getopt(argc, argv, "efd:p:vVh")) != -1)
+	{
+		switch(c)
+		{
+			case 'e':
+				forever = 0;
+				break;
+
+			case 'f':
+				dofork = 0;
+				break;
+
+			case 'd':
+				dev = optarg;
+				break;
+
+			case 'p':
+				pidfile = optarg;
+				break;
+
+			case 'v':
+				verbose++;
+
+			case 'V':
+				version();
+				break;
+
+			case 'h':
 		usage(argv[0]);
+				return 0;
 
-	fd = open(argv[1], O_RDWR);
+			case '?':
+			default:
+				usage(argv[0]);
+				return 1;
+		}
+
+	}
+
+	if (verbose)
+		printf("Opening device %s\n", dev);
+	fd = open(dev, O_RDWR);
 	if (fd < 0) {
 		perror("open");
 		exit(EXIT_FAILURE);
 	}
 
+	if (verbose)
+		printf("Setting ldisc\n");
 	ret = ioctl(fd, TIOCSETD, &ldisc);
 	if (ret < 0) {
 		perror("ioctl(TIOCSETD)");
 		exit(EXIT_FAILURE);
 	}
 
+	if (verbose && dofork)
+		printf("Becoming daemon process\n");
+	if (dofork)
+	{
+		if (daemon(-1, -1) == -1)
+		{
+			perror("daemon");
+			exit(EXIT_FAILURE);
+		}
+	}
+
+	write_pidfile(pidfile);
+
+	if (forever)
+	{
+		if (verbose)
+			printf("Will loop forever\n");
+
+		for(;;)
 	pause();
+	}
+
+	if (verbose)
+		printf("Finished\n");
 
 	return 0;
 }
Binary files pps.org/ppstest and pps/ppstest differ
diff -uNrBbd pps.org/timepps.h pps/timepps.h
--- pps.org/timepps.h	2008-09-18 19:37:49.000000000 +0200
+++ pps/timepps.h	2008-09-18 19:06:46.000000000 +0200
@@ -24,7 +24,7 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 #include <errno.h>
-#include <linux/pps.h>
+#include "../../include/linux/pps.h"
 
 #define LINUXPPS	1		/* signal we are using LinuxPPS */
 


Folkert van Heusden

-- 
MultiTail cok yonlu kullanimli bir program, loglari okumak, verilen
kommandolari yerine getirebilen. Filter, renk verme, merge, 'diff-
view', vs.  http://www.vanheusden.com/multitail/
----------------------------------------------------------------------
Phone: +31-6-41278122, PGP-key: 1F28D8AE, www.vanheusden.com



More information about the LinuxPPS mailing list