chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] RFC: Daemonize


From: Hans Bulfone
Subject: Re: [Chicken-users] RFC: Daemonize
Date: Wed, 28 Nov 2007 01:19:14 +0100
User-agent: Mutt/1.5.16 (2007-06-09)

hi,

sorry for the late reply, i didn't have much time/energy for chicken
in the last few weeks :(

i'm using the following code in a daemon i wrote:
(iirc i translated it from a perl manpage or something)

(require-extension posix)

(define (daemon:ize)
  (change-directory "/")
  (let ((fd-r (file-open "/dev/null" open/rdonly))
        (fd-w (file-open "/dev/null" open/wronly)))
    (duplicate-fileno fd-r 0)
    (duplicate-fileno fd-w 1)
    (file-close fd-r)
    (file-close fd-w))
  (let ((child-pid (process-fork)))
    (if (not (zero? child-pid))
        (exit 0)))
  (create-session)
  (duplicate-fileno 1 2)
  (void))

i also wanted to create an egg from that code, along with some
syslog functions, but was to lazy to do it as yet :)

perhaps we should merge our code...


On Fri, Nov 16, 2007 at 01:21:25PM -0600, Ozzi wrote:
> 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.

afaik daemon() already calls fork() so i don't think you'd really
need to call (process-fork) then.
otoh - daemon() is like my (daemon:ize) function above, it terminates
the parent process which means your api would change and it would no
longer be directly possible to fork multiple independent daemon processes
from a single master.

i've attached my whole daemon library including the syslog functions.

bye,
hans.

> 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.
>
> Oz
>
>
>
> (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)))
>
>   (run-pre-fork-tests)
>
>   (process-fork
>    (lambda ()
>      ((foreign-lambda int "daemon" int int) 0 0)
>      (on-exit cleanup)
>      (if pidfile (create-pid-file pidfile pid))
>      (set-default-signal-handlers)
>      (proc))) #t)

Attachment: daemon.scm
Description: Text document


reply via email to

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