On Sun, 2005-01-09 at 18:31 +0100, Petter Reinholdtsen wrote:
> When gpsd i started in daemon mode, it return an error code instead of
> 0.  The code seem to try to return the pid, but this is not really
> working very well.  This change fixes the problem.  I'm not sure why
> _exit() was used, so I changed it to exit().

The attached patch makes the parent process return 0 on exit, and
optionally stores the PID in a file. I added an option '-P pidfile' to
facilitate this.

This behaviour makes it easier to write start/stop scripts which a)
check on successfull startup (return 0), and b) stop the daemon using
the process id stored e.g. in /var/run/gpsd.pid. I'd appreciate if this
could be included in the next release.

Cheers, Til
Index: gpsd.xml
===================================================================
--- gpsd.xml	(revision 1310)
+++ gpsd.xml	(working copy)
@@ -27,6 +27,7 @@
       <arg choice='opt'>-i <replaceable>initial-position</replaceable></arg>
       <arg choice='opt'>-n </arg>      
       <arg choice='opt'>-h </arg>
+      <arg choice='opt'>-P <replaceable>pidfile</replaceable></arg>
       <arg choice='opt'>-D <replaceable>debuglevel</replaceable></arg>
       <arg choice='opt'>-v </arg>
 </cmdsynopsis>
@@ -134,6 +135,12 @@
 <listitem><para>Display help message and terminate.</para></listitem>
 </varlistentry>
 <varlistentry>
+<term>-P</term>
+<listitem>
+<para>Specify the name and path to record the daemon's process ID.</para>
+</listitem>
+</varlistentry>
+<varlistentry>
 <term>-D</term>
 <listitem>
 <para>Set debug level. At debug levels 2 and above,
Index: gpsd.c
===================================================================
--- gpsd.c	(revision 1310)
+++ gpsd.c	(working copy)
@@ -35,6 +35,7 @@
 
 struct gps_session_t *session;
 static char *device_name = DEFAULT_DEVICE_NAME;
+static char *pid_file = NULL;
 static fd_set all_fds, nmea_fds, watcher_fds;
 static int debuglevel, nfds, in_background = 0;
 
@@ -53,6 +54,18 @@
     exit(10 + sig);
 }
 
+static void store_pid(pid_t pid)
+{
+	FILE *fp;
+
+	if ((fp = fopen(pid_file, "w")) != NULL) {
+		fprintf(fp, "%u\n", pid);
+		(void) fclose(fp);
+	} else {
+		gpsd_report(1, "Cannot create PID file: %s.\n", pid_file);
+	}
+}
+
 static int daemonize(void)
 {
     int fd;
@@ -64,7 +77,9 @@
     case 0:	/* child side */
 	break;
     default:	/* parent side */
-	_exit(pid);
+	if (pid_file)
+		store_pid(pid);
+	exit(0);
     }
 
     if (setsid() == -1)
@@ -114,6 +129,7 @@
 #endif /* TRIPMATE_ENABLE */
 "  -s baud_rate                   = set baud rate on gps device \n\
   -d host[:port]                 = set DGPS server \n\
+  -P pidfile                     = set file to record process ID \n\
   -D integer (default 0)         = set debug level \n\
   -h                             = help message \n",
 	   DEFAULT_DEVICE_NAME, DEFAULT_GPSD_PORT);
@@ -402,7 +418,7 @@
     extern char *optarg;
 
     debuglevel = 1;
-    while ((option = getopt(argc, argv, "D:S:d:hnp:s:v"
+    while ((option = getopt(argc, argv, "D:S:d:hnp:P:s:v"
 #if TRIPMATE_ENABLE || defined(ZODIAC_ENABLE)
 			    "i:"
 #endif /* TRIPMATE_ENABLE || defined(ZODIAC_ENABLE) */
@@ -455,6 +471,9 @@
 	case 'p':
 	    device_name = optarg;
 	    break;
+	case 'P':
+	    pid_file = optarg;
+	    break;
 	case 's':
 	    gpsd_speed = atoi(optarg);
 	    break;

Reply via email to