1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192(* OCaml promise library
* http://www.ocsigen.org/lwt
* Copyright (C) 2009 Jérémie Dimino
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, with linking exceptions;
* either version 2.1 of the License, or (at your option) any later
* version. See COPYING file for details.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*)(* [Lwt_sequence] is deprecated – we don't want users outside Lwt using it.
However, it is still used internally by Lwt. So, briefly disable warning 3
("deprecated"), and create a local, non-deprecated alias for
[Lwt_sequence] that can be referred to by the rest of the code in this
module without triggering any more warnings. *)[@@@ocaml.warning"-3"]moduleLwt_sequence=Lwt_sequence[@@@ocaml.warning"+3"]openLwt.Infixletreccopyiclogger=Lwt_io.read_lineic>>=funline->Lwt_log.log?logger~level:Lwt_log.Noticeline>>=fun()->copyicloggerletredirectfdlogger=letfd_r,fd_w=Unix.pipe()inUnix.set_close_on_execfd_r;Unix.dup2fd_wfd;Unix.closefd_w;letic=Lwt_io.of_unix_fd~mode:Lwt_io.inputfd_rinLwt.ignore_result(copyiclogger)letredirect_outputdev_nullfdmode=matchmodewith|`Dev_null->Unix.dup2dev_nullfd|`Close->Unix.closefd|`Keep->()|`Log_default->redirectfdNone|`Loglogger->redirectfd(Somelogger)letdaemonize?(syslog=true)?(stdin=`Dev_null)?(stdout=`Log_default)?(stderr=`Log_default)?(directory="/")?(umask=`Set0o022)()=Unix.chdirdirectory;(* Exit the parent, and continue in the child: *)ifLwt_unix.fork()>0thenbegin(* Do not run exit hooks in the parent. *)Lwt_sequence.iter_node_lLwt_sequence.removeLwt_main.exit_hooks[@ocaml.warning"-3"];exit0end;ifsyslogthenLwt_log.default:=Lwt_log.syslog~facility:`Daemon();(* Redirection of standard IOs *)letdev_null=Unix.openfile"/dev/null"[Unix.O_RDWR]0o666inbeginmatchstdinwith|`Dev_null->Unix.dup2dev_nullUnix.stdin|`Close->Unix.closeUnix.stdin|`Keep->()end;redirect_outputdev_nullUnix.stdoutstdout;redirect_outputdev_nullUnix.stderrstderr;Unix.closedev_null;beginmatchumaskwith|`Keep->()|`Setn->ignore(Unix.umaskn);end;ignore(Unix.setsid())