[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] RFC: Daemonize

From: Ozzi
Subject: [Chicken-users] RFC: Daemonize
Date: Fri, 16 Nov 2007 13:21:25 -0600
User-agent: Thunderbird (Macintosh/20070728)

I'm most definitely not a scheme guru, so if someone with half a clue would take a look at my little daemonize egg below and let me know what they think, I'd appreciate it.

I ended up using the (foreign-lambda int "daemon" int int) approach.

I use on-exit to remove the PID file, which means that the daemon have to handle signals and exit cleanly. I added a default handler for signal/term to call (exit), there are probably other default handlers that should be added.


(define (daemonize proc #!key (pidfile #f))

  (define (create-pid-file filename pid)
    (with-output-to-file filename (lambda () (print pid))))

  (define (remove-pid-file filename)
    (delete-file* filename))

  (define (run-pre-fork-tests)
    (when pidfile
        (when (file-exists? pidfile)
          (error "PID file exists."))
        (unless (file-exists? (pathname-directory pidfile))
          (error "Directory for PID file does not exist."))
        (unless (and
                 (file-read-access? (pathname-directory pidfile))
                 (file-write-access? (pathname-directory pidfile))
                 (file-execute-access? (pathname-directory pidfile)))
          (error "Insuficcient rights to create PID file."))))

  (define (set-default-signal-handlers)
    (set-signal-handler! signal/term (lambda (signum) (exit))))

  (define (cleanup)
    (if pidfile (remove-pid-file pidfile)))


   (lambda ()
     ((foreign-lambda int "daemon" int int) 0 0)
     (on-exit cleanup)
     (if pidfile (create-pid-file pidfile pid))
     (proc))) #t)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]