1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768openExtLibletlog=Log.from"systemd"moduleDaemon=struct(* https://github.com/systemd/systemd/blob/cb3108669d623afe58a36077e36ae4c66ff7d1c3/src/systemd/sd-daemon.h#L56 *)(* The first passed file descriptor is fd 3. *)letsd_listen_fds_start=3letbooted=(* https://github.com/systemd/systemd/blob/cb31086/src/libsystemd/sd-daemon/sd-daemon.c#L607 *)tryUnix.access"/run/systemd/system/"Unix.[F_OK];truewithUnix.Unix_error_->falseletlisten_pid=Option.mapint_of_string(Sys.getenv_opt"LISTEN_PID")letlisten_fds():Unix.file_descrlist=(* https://github.com/systemd/systemd/blob/cb31086/src/libsystemd/sd-daemon/sd-daemon.c#L42-L90 *)matchbootedwith|false->log#debug"listen_fds: not booted with systemd";[]|true->matchlisten_pidwith|None->log#debug"listen_fds: no LISTEN_PID";[]|Somelisten_pid->letself_pid=Unix.getpid()inmatchlisten_pid=self_pidwith|false->log#warn"listen_fds: LISTEN_PID %d and process pid %d are not equal, ignoring"listen_pidself_pid;[]|true->letlisten_fds=Option.mapint_of_string(Sys.getenv_opt"LISTEN_FDS")inmatchlisten_fdswith|None->log#warn"listen_fds: LISTEN_PID, but no LISTEN_FDS";[]|Somenwhenn<=0->log#warn"listen_fds: LISTEN_FDS %d is not positive"n;[]|Somen->letfds=List.initn(funx->ExtUnix.All.file_descr_of_int(x+sd_listen_fds_start))inList.iterUnix.set_close_on_execfds;fdsletlisten_fds_lwt()=List.mapLwt_unix.of_unix_file_descr(listen_fds())letget_args()=[("-loglevel",Arg.StringLog.set_loglevels," ([<facil|prefix*>=]debug|info|warn|error[,])+");ExtArg.may_str"logfile"Daemon.logfile"<file> Log file";]letmanage()=Daemon.foreground:=true;Daemon.pidfile:=None;Daemon.runas:=None;Daemon.manage()end