Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Daemon Launcher

Turn a process into a well-behaved Unix daemon: detach, redirect stdio, write a pidfile, and optionally relaunch on crash.

Source code:

launch_daemon()

launch_daemon() creates a detached daemon process by performing a double fork and returns the PID of the first child process.

int launch_daemon(
    BOOL redirect_stdio_to_null,
    const char *program,
    ...
);

Parameters

KeyTypeDescription
redirect_stdio_to_nullBOOLIf TRUE, redirects standard input, output, and error to /dev/null.
programconst char *The name of the program to execute as a daemon.
...variadicAdditional arguments to pass to the program, terminated by NULL.

Returns

Returns the PID of the first child process if successful, or -1 if an error occurs.

Notes


Linux daemon supervisor

Declared in ydaemon.h. These entry points run a yuno under a parent “watcher” process that relaunches the child on crash. They are only compiled on Linux (#ifdef __linux__).

daemon_run()

daemon_run() starts the watcher / child supervision loop. The parent process keeps running as a watcher that relaunches the child if it dies; the child calls process(process_name, work_dir, domain_dir, cleaning_fn) to do the actual work.

int daemon_run(
    void (*process)(
        const char *process_name,
        const char *work_dir,
        const char *domain_dir,
        void (*cleaning_fn)(void)
    ),
    const char *process_name,
    const char *work_dir,
    const char *domain_dir,
    void (*cleaning_fn)(void)
);

Parameters

KeyTypeDescription
processfunction pointerEntry point invoked in the child. Receives the process name, work dir, domain dir and the cleanup callback.
process_nameconst char *Name of the process (used in /proc lookups and logs).
work_dirconst char *Working directory to chdir into before running.
domain_dirconst char *Domain-specific directory passed through to the child.
cleaning_fnfunction pointerCleanup callback invoked on shutdown. May be NULL.

Returns

Returns 0 on normal shutdown, or a non-zero value on error.

Notes


daemon_shutdown()

daemon_shutdown() requests an orderly shutdown of a running daemon by process name. The function locates the watcher process and signals it; the watcher in turn signals its child and terminates.

void daemon_shutdown(const char *process_name);

Parameters

KeyTypeDescription
process_nameconst char *Name of the running daemon process to shut down.

Returns

This function does not return a value.

Notes

No-op if no process with the given name is found.


get_watcher_pid()

get_watcher_pid() returns the PID of the watcher (parent) process that is supervising the current child, or 0 if the caller is not running under a watcher.

int get_watcher_pid(void);

Returns

The watcher process PID, or 0 if the current process has no watcher.


get_relaunch_times()

get_relaunch_times() returns the number of times the watcher has relaunched its child process since the daemon was started. Useful for diagnostics and stats endpoints.

int get_relaunch_times(void);

Returns

The relaunch counter (0 on the first run, incremented on each restart).


search_process()

search_process() walks /proc looking for running processes whose name matches process_name and invokes a callback for each match.

int search_process(
    const char *process_name,
    void (*cb)(void *self, const char *name, pid_t pid),
    void *self
);

Parameters

KeyTypeDescription
process_nameconst char *Target process name to match against /proc/*/comm (or /proc/*/cmdline).
cbfunction pointerCallback invoked for each matching process. Receives the opaque self, the resolved process name, and the PID.
selfvoid *Opaque pointer forwarded to the callback.

Returns

Returns the number of matches found, or a negative value on error.

Notes

Used internally by daemon_shutdown() to locate the watcher process to signal.


daemon_set_debug_mode()

daemon_set_debug_mode() enables or disables daemon debug mode for the current process. When debug mode is on the supervisor emits extra tracing and may skip behaviours that would make debugging harder (for example, preventing auto-relaunch on crash).

int daemon_set_debug_mode(BOOL set);

Parameters

KeyTypeDescription
setBOOLTRUE to enable debug mode, FALSE to disable it.

Returns

Returns 0 on success.


daemon_get_debug_mode()

daemon_get_debug_mode() returns whether the daemon is currently running in debug mode.

BOOL daemon_get_debug_mode(void);

Returns

TRUE if debug mode is enabled, FALSE otherwise.