GPSDproxy: send GPS data from gpsd to a remote server

Timo Juhani Lindfors timo.lindfors at iki.fi
Thu Jan 15 02:34:34 CET 2009


Niccolo Rigacci <niccolo at rigacci.org> writes:
> I released a little software to proxy GPS data read from gpsd to 
> a remote server. I use it to store my live track on a Postgres 
> database (running on my home server) via GPRS connection.

Interesting idea. I looked around the code:

    /* Fork into background */
    if (be_daemon) {
        pid = fork();
        if (pid == -1) {
            perror("Could not fork() into background");
        } else if (pid > 0) {
            exit(0);
        }
    }

shouldn't this use two fork()'s? See Stevens's "Advanced Programming
in the Unix Environment" book or take a look at how e.g. tor makes
itself a daemon:

start_daemon(void)
{
  pid_t pid;

  if (start_daemon_called)
    return;
  start_daemon_called = 1;

  if (pipe(daemon_filedes)) {
    log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    log_err(LD_GENERAL,"fork failed. Exiting.");
    exit(1);
  }
  if (pid) {  /* Parent */
    int ok;
    char c;

    close(daemon_filedes[1]); /* we only read */
    ok = -1;
    while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
      if (c == '.')
        ok = 1;
    }
    fflush(stdout);
    if (ok == 1)
      exit(0);
    else
      exit(1); /* child reported error */
  } else { /* Child */
    close(daemon_filedes[0]); /* we only write */

    pid = setsid(); /* Detach from controlling terminal */
    /*
     * Fork one more time, so the parent (the session group leader) can exit.
     * This means that we, as a non-session group leader, can never regain a
     * controlling terminal.   This part is recommended by Stevens's
     * _Advanced Programming in the Unix Environment_.
     */
    if (fork() != 0) {
      exit(0);
    }
    return;
  }
}

Also it could be useful to implement --pid-file option that writes the
pid of the process to a file so that you don't need to resort to
"killall gpsdproxd" in the stop script.






More information about the community mailing list