Lely core libraries  2.3.4
daemon.h File Reference
#include <lely/util/util.h>
Include dependency graph for daemon.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void daemon_handler_t(int sig, void *handle)
 The function type of a handler for daemon_signal(). More...
 

Enumerations

enum  {
  DAEMON_START, DAEMON_STOP, DAEMON_PAUSE, DAEMON_CONTINUE,
  DAEMON_RELOAD, DAEMON_USER_MIN, DAEMON_USER_MAX = 255
}
 

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_stop (void)
 Sends the stop 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...
 
int daemon_reload (void)
 Sends the reload signal to the daemon handler. 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...
 
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 header file is part of the utilities library; it contains the daemon declarations.

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.h.

Typedef Documentation

◆ daemon_handler_t

typedef void daemon_handler_t(int sig, void *handle)

The function type of a handler for daemon_signal().

Note that daemon handlers are executed in a separate thread and do not interrupt the execution of the main program.

Parameters
sigthe signal specified to daemon_signal().
handlethe handle argument specified to daemon_set_handler().

Definition at line 68 of file daemon.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
DAEMON_START 

The status indicating the daemon has started.

DAEMON_STOP 

The signal/status indicating the daemon MUST terminate/has terminated.

DAEMON_PAUSE 

The signal/status indicating the daemon SHOULD pause/has paused.

DAEMON_CONTINUE 

The signal/status indicating the daemon SHOULD continue/has continued normal operation.

DAEMON_RELOAD 

The signal indicating the daemon SHOULD reload its configuration.

DAEMON_USER_MIN 

The smallest possible value of a user-defined signal.

DAEMON_USER_MAX 

The largest possible value of a user-defined signal.

Definition at line 27 of file daemon.h.

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 251 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 578 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 590 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 596 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 584 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 402 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 417 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 602 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 611 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 618 of file daemon.c.