Lely core libraries  2.2.5
daemon.c File Reference
#include "util.h"
#include <lely/libc/threads.h>
#include <lely/util/daemon.h>
#include <lely/util/diag.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/stat.h>
#include <unistd.h>
Include dependency graph for daemon.c:

Go to the source code of this file.

Functions

int daemon_start (const char *name, int(*init)(int, char **), void(*main)(void), void(*fini)(void), int argc, char *argv[])
 Executes the supplied function as a POSIX daemon or Windows service. More...
 
int daemon_signal (int sig)
 Sends a signal to a daemon, triggering the execution of the daemon handler. More...
 
int daemon_status (int status)
 Sets the current daemon status (one of DAEMON_START, DAEMON_STOP, DAEMON_PAUSE or DAEMON_CONTINUE). More...
 
int daemon_stop (void)
 Sends the stop signal to the daemon handler. More...
 
int daemon_reload (void)
 Sends the reload signal to the daemon handler. More...
 
int daemon_pause (void)
 Sends the pause signal to the daemon handler. More...
 
int daemon_continue (void)
 Sends the continue signal to the daemon handler. More...
 
void daemon_get_handler (daemon_handler_t **phandler, void **phandle)
 Retrieves current daemon handler and handle argument. More...
 
void daemon_set_handler (daemon_handler_t *handler, void *handle)
 Sets the current daemon handler and its (optional) handle argument. More...
 
void default_daemon_handler (int sig, void *handle)
 The default daemon_signal() handler. More...
 

Detailed Description

This file is part of the utilities library; it contains the daemon function definitions.

See also
lely/util/daemon.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file daemon.c.

Function Documentation

◆ daemon_start()

int daemon_start ( const char *  name,
int(*)(int, char **)  init,
void(*)(void)  main,
void(*)(void)  fini,
int  argc,
char *  argv[] 
)

Executes the supplied function as a POSIX daemon or Windows service.

This function MUST be called only once, before any threads have been created or any files, sockets, etc. have been opened. On successful execution, this function does not return until the daemon terminates.

This function registers daemon_diag_handler() and daemon_diag_at_handler() as the diagnostic message handlers for any code running as a daemon (this includes init on Windows, but not on other platforms).

Parameters
namea pointer to the name of the daemon. Depending on the platform, the name is used for diagnostic or registration purposes.
inita pointer to the initialization function executed before main (can be NULL).
maina pointer to the function to be executed as a daemon.
finia pointer to the finalization function executed after main (can be NULL).
argcthe number of arguments in argv (unused on Windows as the daemon receives its arguments directly from the Service Control Manager).
argvan array of pointers to arguments to init (unused on Windows). The array MUST be NULL-terminated, i.e., argv[argc] == NULL.
Returns
0 on completion, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 242 of file daemon.c.

◆ daemon_signal()

int daemon_signal ( int  sig)

Sends a signal to a daemon, triggering the execution of the daemon handler.

On POSIX platforms, the handler is executed asynchronously (in a separate thread), and this function can safely be called from a signal() handler.

Definition at line 386 of file daemon.c.

◆ daemon_status()

int daemon_status ( int  status)

Sets the current daemon status (one of DAEMON_START, DAEMON_STOP, DAEMON_PAUSE or DAEMON_CONTINUE).

It is the responsibility of the user to update the status in a timely manner after receiving a start, stop, pause or continue signal.

Definition at line 401 of file daemon.c.

◆ daemon_stop()

int daemon_stop ( void  )

Sends the stop signal to the daemon handler.

This function is equivalent to daemon_signal(DAEMON_STOP). It is the responsibility of the user to invoke daemon_status(DAEMON_STOP) once the daemon is stopped.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 562 of file daemon.c.

◆ daemon_reload()

int daemon_reload ( void  )

Sends the reload signal to the daemon handler.

This function is equivalent to daemon_signal(DAEMON_RELOAD). The user does not need to invoke daemon_status() to acknowledge execution of the reload operation.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 568 of file daemon.c.

◆ daemon_pause()

int daemon_pause ( void  )

Sends the pause signal to the daemon handler.

This function is equivalent to daemon_signal(DAEMON_PAUSE). It is the responsibility of the user to invoke daemon_status(DAEMON_PAUSE) once the daemon is paused.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 574 of file daemon.c.

◆ daemon_continue()

int daemon_continue ( void  )

Sends the continue signal to the daemon handler.

This function is equivalent to daemon_signal(DAEMON_CONTINUE). It is the responsibility of the user to invoke daemon_status(DAEMON_CONTINUE) once the daemon continues its normal operation.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 580 of file daemon.c.

◆ daemon_get_handler()

void daemon_get_handler ( daemon_handler_t **  phandler,
void **  phandle 
)

Retrieves current daemon handler and handle argument.

Parameters
phandlerthe address at which to store a pointer to the current daemon handler (can be NULL).
phandlethe address at which to store a pointer to the handle argument of the current daemon handler (can be NULL).
See also
daemon_set_handler()

Definition at line 586 of file daemon.c.

◆ daemon_set_handler()

void daemon_set_handler ( daemon_handler_t handler,
void *  handle 
)

Sets the current daemon handler and its (optional) handle argument.

The handler is invoked asynchronously (in a separate thread) after the reception of a signal.

See also
daemon_get_handler()

Definition at line 595 of file daemon.c.

◆ default_daemon_handler()

void default_daemon_handler ( int  sig,
void *  handle 
)

The default daemon_signal() handler.

See also
daemon_handler_t

Definition at line 602 of file daemon.c.