paraslash Paraslash Audio Streaming
About   News   Download   Documentation   Development

Functions
daemon.c File Reference

Some helpers for programs that detach from the console. More...

#include <regex.h>
#include <pwd.h>
#include <sys/types.h>
#include <grp.h>
#include <signal.h>
#include <sys/resource.h>
#include "para.h"
#include "daemon.h"
#include "string.h"
#include "color.h"

Functions

void daemon_set_log_color_or_die (const char *arg)
 Set the color for log messages of the given severity level. More...
 
bool daemon_init_colors_or_die (int color_arg, int color_arg_auto, int color_arg_no, bool logfile_given)
 Initialize color mode if necessary. More...
 
void daemon_set_logfile (const char *logfile_name)
 Init or change the name of the log file. More...
 
void daemon_set_loglevel (int loglevel)
 Control the verbosity for logging. More...
 
int daemon_get_loglevel (void)
 Get the current log level of the daemon. More...
 
void daemon_set_hooks (void(*pre_log_hook)(void), void(*post_log_hook)(void))
 Register functions to be called before and after a message is logged. More...
 
void daemon_set_flag (unsigned flag)
 Set one of the daemon config flags. More...
 
int daemonize (bool parent_waits)
 Do the usual stuff to become a daemon. More...
 
void daemon_close_log (void)
 Close the log file of the daemon. More...
 
void daemon_open_log_or_die (void)
 Open the logfile in append mode. More...
 
void daemon_log_welcome (const char *name)
 Log the startup message containing the paraslash version. More...
 
void daemon_set_priority (int prio)
 Renice the calling process. More...
 
void daemon_drop_privileges_or_die (const char *username, const char *groupname)
 Give up superuser privileges. More...
 
void daemon_set_start_time (void)
 Set the startup time. More...
 
time_t daemon_get_uptime (const struct timeval *current_time)
 Get the uptime. More...
 
__malloc char * daemon_get_uptime_str (const struct timeval *current_time)
 Construct a string containing the current uptime. More...
 
__printf_2_3 void daemon_log (int ll, const char *fmt,...)
 The log function for para_server and para_audiod. More...
 

Detailed Description

Some helpers for programs that detach from the console.

Function Documentation

◆ daemon_set_log_color_or_die()

void daemon_set_log_color_or_die ( const char *  arg)

Set the color for log messages of the given severity level.

Parameters
argMust be of the form "severity:[fg [bg]] [attr]".

References color_parse_or_die(), and NUM_LOGLEVELS.

◆ daemon_init_colors_or_die()

bool daemon_init_colors_or_die ( int  color_arg,
int  color_arg_auto,
int  color_arg_no,
bool  logfile_given 
)

Initialize color mode if necessary.

Parameters
color_argThe argument given to –color.
color_arg_autoThe value for automatic color detection.
color_arg_noThe value to disable colored log messages.
logfile_givenIn auto mode colors are disabled if this value is true.

If color_arg equals color_arg_no, color mode is disabled. That is, calls to para_log() will not produce colored output. If color_arg equals color_arg_auto, the function detects automatically whether to activate colors. Otherwise color mode is enabled.

If color mode is to be enabled, the default colors are set for each loglevel. They can be overwritten by calling daemon_set_log_color_or_die().

Returns
Whether colors have been enabled by the function.

References daemon_set_flag(), and DF_COLOR_LOG.

◆ daemon_set_logfile()

void daemon_set_logfile ( const char *  logfile_name)

Init or change the name of the log file.

Parameters
logfile_nameThe full path of the logfile.

◆ daemon_set_loglevel()

void daemon_set_loglevel ( int  loglevel)

Control the verbosity for logging.

This instructs the daemon to not log subsequent messages whose severity is lower than the given value.

Parameters
loglevelThe new log level.

◆ daemon_get_loglevel()

int daemon_get_loglevel ( void  )

Get the current log level of the daemon.

Returns
Greater or equal than zero and less than NUM_LOGLEVELS. This function never fails.

◆ daemon_set_hooks()

void daemon_set_hooks ( void(*)(void)  pre_log_hook,
void(*)(void)  post_log_hook 
)

Register functions to be called before and after a message is logged.

Parameters
pre_log_hookCalled before the message is logged.
post_log_hookCalled after the message is logged.

The purpose of this function is to provide a primitive for multi-threaded applications to serialize the access to the log facility, preventing interleaving log messages. This can be achieved by having the pre-log hook acquire a lock which blocks the other threads on the attempt to log a message at the same time. The post-log hook is responsible for releasing the lock.

If these hooks are unnecessary, for example because the application is single-threaded, this function does not need to be called.

◆ daemon_set_flag()

void daemon_set_flag ( unsigned  flag)

Set one of the daemon config flags.

Parameters
flagThe flag to set.
See also
daemon_flags.

Referenced by daemon_init_colors_or_die().

◆ daemonize()

int daemonize ( bool  parent_waits)

Do the usual stuff to become a daemon.

Parameters
parent_waitsWhether the parent process should pause before exit.

Fork, become session leader, cd to /, and dup fd 0, 1, 2 to /dev/null. If parent_waits is false, the parent process terminates immediately. Otherwise, a pipe is created prior to the fork() and the parent tries to read a single byte from the reading end of the pipe. The child is supposed to write to the writing end of the pipe after it completed its setup procedure successfully. This behaviour is useful to let the parent process die with an error if the child process aborts early, since in this case the read() will return non-positive.

Returns
This function either succeeds or calls exit(3). If parent_waits is true, the return value is the file descriptor of the writing end of the pipe. Otherwise the function returns zero.
See also
fork(2), setsid(2), dup(2), pause(2).

References PARA_INFO_LOG.

◆ daemon_close_log()

void daemon_close_log ( void  )

Close the log file of the daemon.

◆ daemon_open_log_or_die()

void daemon_open_log_or_die ( void  )

Open the logfile in append mode.

This function either succeeds or exits.

◆ daemon_log_welcome()

void daemon_log_welcome ( const char *  name)

Log the startup message containing the paraslash version.

Parameters
nameThe name of the executable.

First the given name is prefixed with the string "para_". Next the git version is appended. The resulting string is logged with priority "INFO".

References PARA_INFO_LOG.

◆ daemon_set_priority()

void daemon_set_priority ( int  prio)

Renice the calling process.

Parameters
prioThe priority value to set.

Errors are not considered fatal, but a warning message is logged if the underlying call to setpriority(2) fails.

References PARA_WARNING_LOG.

◆ daemon_drop_privileges_or_die()

void daemon_drop_privileges_or_die ( const char *  username,
const char *  groupname 
)

Give up superuser privileges.

Parameters
usernameThe user to switch to.
groupnameThe group to switch to.

This function returns immediately if not invoked with EUID zero. Otherwise, it tries to obtain the GID of groupname and the UID of username. On success, effective and real GID/UID and the saved set-group-ID/set-user-ID are all set accordingly. On errors, an appropriate message is logged and exit() is called to terminate the process.

See also
getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)

References PARA_DEBUG_LOG, PARA_EMERG_LOG, PARA_INFO_LOG, and para_strdup().

◆ daemon_set_start_time()

void daemon_set_start_time ( void  )

Set the startup time.

This should be called once on startup. It sets the start time to the current time. The stored time is used for retrieving the server uptime.

See also
time(2), daemon_get_uptime(), daemon_get_uptime_str().

◆ daemon_get_uptime()

time_t daemon_get_uptime ( const struct timeval *  current_time)

Get the uptime.

Parameters
current_timeThe current time.

The current_time pointer may be NULL. In this case the function obtains the current time from the system.

Returns
This returns the server uptime in seconds, i.e. the difference between the current time and the value stored previously via daemon_set_start_time().

Referenced by daemon_get_uptime_str().

◆ daemon_get_uptime_str()

__malloc char* daemon_get_uptime_str ( const struct timeval *  current_time)

Construct a string containing the current uptime.

Parameters
current_timeSee a daemon_get_uptime().
Returns
A dynamically allocated string of the form "days:hours:minutes".

References daemon_get_uptime(), and make_message().

Referenced by audiod_status_dump().

◆ daemon_log()

__printf_2_3 void daemon_log ( int  ll,
const char *  fmt,
  ... 
)

The log function for para_server and para_audiod.

Parameters
llThe log level.
fmtThe format string describing the log message.