Paraslash Audio Streaming | |
About News Download Documentation Development |
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... | |
Some helpers for programs that detach from the console.
void daemon_set_log_color_or_die | ( | const char * | arg | ) |
Set the color for log messages of the given severity level.
arg | Must be of the form "severity:[fg [bg]] [attr]". |
References color_parse_or_die(), and NUM_LOGLEVELS.
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.
color_arg | The argument given to –color. |
color_arg_auto | The value for automatic color detection. |
color_arg_no | The value to disable colored log messages. |
logfile_given | In 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().
References daemon_set_flag(), and DF_COLOR_LOG.
void daemon_set_logfile | ( | const char * | logfile_name | ) |
Init or change the name of the log file.
logfile_name | The full path of the logfile. |
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.
loglevel | The new log level. |
int daemon_get_loglevel | ( | void | ) |
Get the current log level of the daemon.
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.
pre_log_hook | Called before the message is logged. |
post_log_hook | Called 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.
void daemon_set_flag | ( | unsigned | flag | ) |
Set one of the daemon config flags.
flag | The flag to set. |
Referenced by daemon_init_colors_or_die().
int daemonize | ( | bool | parent_waits | ) |
Do the usual stuff to become a daemon.
parent_waits | Whether 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.
References PARA_INFO_LOG.
void daemon_close_log | ( | void | ) |
Close the log file of the daemon.
void daemon_open_log_or_die | ( | void | ) |
Open the logfile in append mode.
This function either succeeds or exits.
void daemon_log_welcome | ( | const char * | name | ) |
Log the startup message containing the paraslash version.
name | The 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.
void daemon_set_priority | ( | int | prio | ) |
Renice the calling process.
prio | The 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.
void daemon_drop_privileges_or_die | ( | const char * | username, |
const char * | groupname | ||
) |
Give up superuser privileges.
username | The user to switch to. |
groupname | The 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.
References PARA_DEBUG_LOG, PARA_EMERG_LOG, PARA_INFO_LOG, and para_strdup().
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.
time_t daemon_get_uptime | ( | const struct timeval * | current_time | ) |
Get the uptime.
current_time | The current time. |
The current_time pointer may be NULL
. In this case the function obtains the current time from the system.
Referenced by daemon_get_uptime_str().
__malloc char* daemon_get_uptime_str | ( | const struct timeval * | current_time | ) |
Construct a string containing the current uptime.
current_time | See a daemon_get_uptime(). |
References daemon_get_uptime(), and make_message().
Referenced by audiod_status_dump().
__printf_2_3 void daemon_log | ( | int | ll, |
const char * | fmt, | ||
... | |||
) |
The log function for para_server and para_audiod.
ll | The log level. |
fmt | The format string describing the log message. |