chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] daemonize egg: redirect I/O?


From: Elf
Subject: Re: [Chicken-users] daemonize egg: redirect I/O?
Date: Fri, 26 Oct 2007 05:06:34 -0700 (PDT)

On Fri, 26 Oct 2007, Thomas Christian Chust wrote:

Elf wrote:

um, why not just use (duplicate-fileno (portfileno <port>))  ? [...]

Because not the file descriptor of a given port should be duplicated but
rather the file descriptor of a given port should be replaced by a
duplicate of another one.

Of course one could use (portfileno ...) instead of the fileno/...
constants in my example, but it wouldn't make much of a difference,
unless the ports were already set to something different from the
standard -- but in that case it may not be such a good idea to redirect
them. I think it makes much more sense to redirect those descriptors
which are, by default, connected to the controlling terminal of the
process, because they usually become useless after detaching from the
original process group.


yes, i understood that.  hence the comment regarding the
(current-input/output/error-port) procedures being parameters that take an
argument for automatic replacement.  they need to be replaced WITH something,
though, hence the portfileno remark.

(re: the daemon question, i would just use
     (foreign-lambda int "daemon" int int) ,
 but thats just me.)

Isn't daemon(3) a BSD C library function? I don't know whether it is
available everywhere -- and besides that, an implementation in Scheme is
not a lot of work but could offer a great deal of added flexibility in
addition to what deamon(3) can do.

[...]

daemon() has been standard in libc since around 1995.  the only machines that
wont have it are windows boxen. the direct chicken code for daemon() (without type or validity checking), and with an explicit continuation since
return values mean very little in this context, is:

(define (daemon nochdir noclose cnt)
    (call-with-current-continuation
        (lambda (k)
            (process-fork (lambda () (k #t)))
            (exit 0)))
    (create-session)
    (call-with-current-continuation
        (lambda (k)
            (process-fork (lambda () (k #t)))
            (exit 0)))
    (and (= 0 nochdir)
         (change-directory "/"))
    (and-let* ((cls   (= 0 noclose))
               (ipt   (open-input-file "/dev/null"))
               (opt   (open-output-file "/dev/null")))
        (current-input-port ipt)
        (current-output-port opt)
        (current-error-port opt)
        (cnt)))

the call/cc's i believe are necessary because process-fork automatically calls
(_exit 0) after evaling the lambda for the child procs.

-elf





reply via email to

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