Commit bdcf008b authored by Philipp Schafft's avatar Philipp Schafft 🦁

Added <event>: Unified handling of events.

<event> has been added and can be used within <kartoffelsalat>
both in <icecast> and <mount>.
<event> takes backend depending <option> child tags.
Currently supported backends:
 - log: send message to error log.
 - exec: executes a program or script.
 - url: delivers the event via HTTP.

within <mount> <on-connect> and <on-disconnect> has been replaced by
<event>. Config parser can on-the-fly convert old tags.
Also <authentication type="url"> within <mount> has been fixed
for those cases with <option name="mount_add" .../> and
<option name="mount_remove" .../> which are now on-the-fly converted
by the parser to corresponding <event> tags.

Please also see TAGs added as per #2098. Some include hints for
documentation updates needed after this change. Those updates
should take place before 2.4.2.
parent 8f706a4c
......@@ -111,7 +111,7 @@ XIPH_PATH_CURL([
AC_CHECK_DECL([CURLOPT_NOSIGNAL],
[ AC_DEFINE([HAVE_AUTH_URL], 1, [Define to compile in auth URL support code])
AC_CHECK_FUNCS([curl_global_init])
ICECAST_OPTIONAL="$ICECAST_OPTIONAL auth_url.o"
ICECAST_OPTIONAL="$ICECAST_OPTIONAL auth_url.o event_url.o"
enable_curl="yes"
XIPH_VAR_APPEND([XIPH_CPPFLAGS],[$CURL_CFLAGS])
XIPH_VAR_PREPEND([XIPH_LIBS],[$CURL_LIBS])
......
......@@ -11,6 +11,7 @@ INCLUDES = -I./common/
noinst_HEADERS = admin.h cfgfile.h logging.h sighandler.h connection.h \
global.h util.h slave.h source.h stats.h refbuf.h client.h \
compat.h fserve.h xslt.h yp.h md5.h \
event.h event_log.h event_exec.h event_url.h \
acl.h auth.h auth_htpasswd.h auth_url.h auth_anonymous.h auth_static.h \
format.h format_ogg.h format_mp3.h format_ebml.h \
format_vorbis.h format_theora.h format_flac.h format_speex.h format_midi.h \
......@@ -19,9 +20,11 @@ icecast_SOURCES = cfgfile.c main.c logging.c sighandler.c connection.c global.c
util.c slave.c source.c stats.c refbuf.c client.c \
xslt.c fserve.c admin.c md5.c \
format.c format_ogg.c format_mp3.c format_midi.c format_flac.c format_ebml.c \
acl.c auth.c auth_htpasswd.c auth_anonymous.c auth_static.c format_kate.c format_skeleton.c format_opus.c
format_kate.c format_skeleton.c format_opus.c \
event.c event_log.c event_exec.c \
acl.c auth.c auth_htpasswd.c auth_anonymous.c auth_static.c
EXTRA_icecast_SOURCES = yp.c \
auth_url.c \
auth_url.c event_url.c \
format_vorbis.c format_theora.c format_speex.c
icecast_DEPENDENCIES = @ICECAST_OPTIONAL@ common/net/libicenet.la common/thread/libicethread.la \
......
......@@ -470,6 +470,7 @@ auth_t *auth_get_authenticator(xmlNodePtr node)
auth->method[i] = 1;
}
/* BEFORE RELEASE 2.4.2 TODO: Migrate this to config_parse_options(). */
option = node->xmlChildrenNode;
while (option)
{
......
This diff is collapsed.
......@@ -24,6 +24,7 @@
struct _mount_proxy;
#include <libxml/tree.h>
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "global.h"
......@@ -58,6 +59,7 @@ typedef struct ice_config_dir_tag {
} ice_config_dir_t;
typedef struct _config_options {
char *type;
char *name;
char *value;
struct _config_options *next;
......@@ -99,10 +101,10 @@ typedef struct _mount_proxy {
ice_config_http_header_t *http_headers; /* additional HTTP headers */
struct event_registration_tag *event;
char *cluster_password;
struct auth_stack_tag *authstack;
char *on_connect;
char *on_disconnect;
unsigned int max_listener_duration;
char *stream_name;
......@@ -155,6 +157,8 @@ typedef struct ice_config_tag {
char *shoutcast_mount;
struct auth_stack_tag *authstack;
struct event_registration_tag *event;
int touch_interval;
ice_config_dir_t *dir_list;
......@@ -226,6 +230,9 @@ void config_clear(ice_config_t *config);
mount_proxy *config_find_mount (ice_config_t *config, const char *mount, mount_type type);
listener_t *config_get_listen_sock (ice_config_t *config, connection_t *con);
config_options_t *config_parse_options(xmlNodePtr node);
void config_clear_options(config_options_t *options);
int config_rehash(void);
ice_config_locks *config_locks(void);
......
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2014, Philipp Schafft <lion@lion.leolix.org>
*/
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include "event.h"
#include "event_log.h"
#include "event_exec.h"
#include "event_url.h"
#include "logging.h"
#include "admin.h"
#define CATMODULE "event"
static mutex_t event_lock;
static event_t *event_queue = NULL;
static int event_running = 0;
static thread_type *event_thread = NULL;
/* work with event_t* */
static void event_addref(event_t *event) {
if (!event)
return;
event->refcount++;
}
static void event_release(event_t *event) {
if (!event)
return;
event->refcount--;
if (event->refcount)
return;
free(event->trigger);
free(event->uri);
free(event->connection_ip);
free(event->client_role);
free(event->client_username);
free(event->client_useragent);
event_release(event->next);
free(event);
}
static void event_push(event_t **event, event_t *next) {
size_t i = 0;
if (!event || !next)
return;
while (*event && i < 128) {
event = &(*event)->next;
i++;
}
if (i == 128) {
ICECAST_LOG_ERROR("Can not push event %p into queue. Queue is full.", event);
return;
}
event_addref(*event = next);
}
static void event_push_reglist(event_t *event, event_registration_t *reglist) {
size_t i;
if (!event || !reglist)
return;
for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++) {
if (!event->reglist[i]) {
event_registration_addref(event->reglist[i] = reglist);
return;
}
}
ICECAST_LOG_ERROR("Can not push reglist %p into event %p. No space left on event.", reglist, event);
}
static event_t *event_new(const char *trigger) {
event_t *ret;
if (!trigger)
return NULL;
ret = calloc(1, sizeof(event_t));
if (!ret)
return NULL;
ret->refcount = 1;
ret->trigger = strdup(trigger);
ret->client_admin_command = ADMIN_COMMAND_ERROR;
if (!ret->trigger) {
event_release(ret);
return NULL;
}
return ret;
}
/* subsystem functions */
static inline void _try_event(event_registration_t *er, event_t *event) {
/* er is already locked */
if (strcmp(er->trigger, event->trigger) != 0)
return;
if (er->emit)
er->emit(er->state, event);
}
static inline void _try_registrations(event_registration_t *er, event_t *event) {
if (!er)
return;
thread_mutex_lock(&er->lock);
while (1) {
/* try registration */
_try_event(er, event);
/* go to next registration */
if (er->next) {
event_registration_t *next = er->next;
thread_mutex_lock(&next->lock);
thread_mutex_unlock(&er->lock);
er = next;
} else {
break;
}
}
thread_mutex_unlock(&er->lock);
}
static void *event_run_thread (void *arg) {
int running = 0;
do {
event_t *event;
size_t i;
thread_mutex_lock(&event_lock);
running = event_running;
if (event_queue) {
event = event_queue;
event_queue = event_queue->next;
} else {
event = NULL;
}
thread_mutex_unlock(&event_lock);
/* sleep if nothing todo and then try again */
if (!event) {
thread_sleep(150000);
continue;
}
for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++)
_try_registrations(event->reglist[i], event);
event_release(event);
} while (running);
return NULL;
}
void event_initialise(void) {
/* create mutex */
thread_mutex_create(&event_lock);
/* initialise everything */
thread_mutex_lock(&event_lock);
event_running = 1;
thread_mutex_unlock(&event_lock);
/* start thread */
event_thread = thread_create("events thread", event_run_thread, NULL, THREAD_ATTACHED);
}
void event_shutdown(void) {
/* stop thread */
if (!event_running)
return;
thread_mutex_lock(&event_lock);
event_running = 0;
thread_mutex_unlock(&event_lock);
/* join thread as soon as it stopped */
thread_join(event_thread);
/* shutdown everything */
thread_mutex_lock(&event_lock);
event_thread = NULL;
event_release(event_queue);
event_queue = NULL;
thread_mutex_unlock(&event_lock);
/* destry mutex */
thread_mutex_destroy(&event_lock);
}
/* basic functions to work with event registrations */
event_registration_t * event_new_from_xml_node(xmlNodePtr node) {
event_registration_t *ret = calloc(1, sizeof(event_registration_t));
config_options_t *options;
int rv;
if(!ret)
return NULL;
ret->refcount = 1;
/* BEFORE RELEASE 2.4.2 DOCUMENT: Document <event type="..." trigger="..."> */
ret->type = (char*)xmlGetProp(node, XMLSTR("type"));
ret->trigger = (char*)xmlGetProp(node, XMLSTR("trigger"));
if (!ret->type || !ret->trigger) {
ICECAST_LOG_ERROR("Event node isn't complet. Type or Trigger missing.");
event_registration_release(ret);
return NULL;
}
options = config_parse_options(node);
if (strcmp(ret->type, EVENT_TYPE_LOG) == 0) {
rv = event_get_log(ret, options);
} else if (strcmp(ret->type, EVENT_TYPE_EXEC) == 0) {
rv = event_get_exec(ret, options);
#ifdef HAVE_AUTH_URL
} else if (strcmp(ret->type, EVENT_TYPE_URL) == 0) {
rv = event_get_url(ret, options);
#endif
} else {
ICECAST_LOG_ERROR("Event backend %s is unknown.", ret->type);
rv = -1;
}
config_clear_options(options);
if (rv != 0) {
ICECAST_LOG_ERROR("Can not set up event backend %s for trigger %s", ret->type, ret->trigger);
event_registration_release(ret);
return NULL;
}
return ret;
}
void event_registration_addref(event_registration_t * er) {
if(!er)
return;
thread_mutex_lock(&er->lock);
er->refcount++;
thread_mutex_unlock(&er->lock);
}
void event_registration_release(event_registration_t *er) {
if(!er)
return;
thread_mutex_lock(&er->lock);
er->refcount--;
if (er->refcount) {
thread_mutex_unlock(&er->lock);
return;
}
if (er->next)
event_registration_release(er->next);
xmlFree(er->type);
xmlFree(er->trigger);
if (er->free)
er->free(er->state);
thread_mutex_unlock(&er->lock);
thread_mutex_destroy(&er->lock);
free(er);
}
void event_registration_push(event_registration_t **er, event_registration_t *tail) {
event_registration_t *next, *cur;
if (!er || !tail)
return;
if (!*er) {
event_registration_addref(*er = tail);
return;
}
event_registration_addref(cur = *er);
thread_mutex_lock(&cur->lock);
while (1) {
next = cur->next;
if (!cur->next)
break;
event_registration_addref(next);
thread_mutex_unlock(&cur->lock);
event_registration_release(cur);
cur = next;
thread_mutex_lock(&cur->lock);
}
event_registration_addref(cur->next = tail);
thread_mutex_unlock(&cur->lock);
event_registration_release(cur);
}
/* event signaling */
void event_emit(event_t *event) {
thread_mutex_lock(&event_lock);
event_push(&event_queue, event);
thread_mutex_unlock(&event_lock);
}
/* this function needs to extract all the infos from the client, source and mount object
* as after return the pointers become invalid.
*/
void event_emit_clientevent(const char *trigger, client_t *client, const char *uri) {
event_t *event = event_new(trigger);
ice_config_t *config;
mount_proxy *mount;
if (!event) {
ICECAST_LOG_ERROR("Can not create event.");
return;
}
if (client) {
const char *tmp;
event->connection_id = client->con->id;
event->connection_time = client->con->con_time;
event->client_admin_command = client->admin_command;
event->connection_ip = strdup(client->con->ip);
if (client->role)
event->client_role = strdup(client->role);
if (client->username)
event->client_username = strdup(client->username);
tmp = httpp_getvar(client->parser, "user-agent");
if (tmp)
event->client_useragent = strdup(tmp);
}
if (uri)
event->uri = strdup(uri);
config = config_get_config();
event_push_reglist(event, config->event);
mount = config_find_mount(config, uri, MOUNT_TYPE_NORMAL);
if (mount && mount->mounttype == MOUNT_TYPE_NORMAL)
event_push_reglist(event, mount->event);
mount = config_find_mount(config, uri, MOUNT_TYPE_DEFAULT);
if (mount && mount->mounttype == MOUNT_TYPE_DEFAULT)
event_push_reglist(event, mount->event);
config_release_config();
event_emit(event);
event_release(event);
}
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2014, Philipp Schafft <lion@lion.leolix.org>
*/
#ifndef __EVENT_H__
#define __EVENT_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "common/httpp/httpp.h"
#include "cfgfile.h"
#include "client.h"
#include "source.h"
/* implemented */
#define EVENT_TYPE_LOG "log"
#define EVENT_TYPE_EXEC "exec"
/* not implemented */
#define EVENT_TYPE_URL "url"
#define MAX_REGLISTS_PER_EVENT 8
struct event_registration_tag;
typedef struct event_registration_tag event_registration_t;
struct event_tag;
typedef struct event_tag event_t;
/* this has no lock member to protect multiple accesses as every non-readonly access is within event.c
* and is protected by global lock or on the same thread anyway.
*/
struct event_tag {
/* refernece counter */
size_t refcount;
/* reference to next element in chain */
event_t *next;
/* event_registration lists.
* They are referenced here to avoid the need to access
* config and global client list each time an event is emitted.
*/
event_registration_t *reglist[MAX_REGLISTS_PER_EVENT];
/* trigger name */
char *trigger;
/* from client */
char *uri; /* from context */
unsigned long connection_id; /* from client->con->id */
char *connection_ip; /* from client->con->ip */
time_t connection_time; /* from client->con->con_time */
char *client_role; /* from client->role */
char *client_username; /* from client->username */
char *client_useragent; /* from httpp_getvar(client->parser, "user-agent") */
int client_admin_command; /* from client->admin_command */
};
struct event_registration_tag {
/* refernece counter */
size_t refcount;
/* reference to next element in chain */
event_registration_t *next;
/* protection */
mutex_t lock;
/* type of event */
char *type;
/* trigger name */
char *trigger;
/* backend state */
void *state;
/* emit events */
int (*emit)(void *state, event_t *event);
/* free backend state */
void (*free)(void *state);
};
/* subsystem functions */
void event_initialise(void);
void event_shutdown(void);
/* basic functions to work with event registrations */
event_registration_t * event_new_from_xml_node(xmlNodePtr node);
void event_registration_addref(event_registration_t *er);
void event_registration_release(event_registration_t *er);
void event_registration_push(event_registration_t **er, event_registration_t *tail);
/* event signaling */
void event_emit_clientevent(const char *trigger, client_t *client, const char *uri);
#define event_emit_global(x) event_emit_clientevent((x), NULL, NULL)
#endif
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2014, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef _WIN32
#include <sys/wait.h>
/* for __setup_empty_script_environment() */
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "event.h"
#include "logging.h"
#define CATMODULE "event_exec"
typedef struct event_exec {
/* REVIEW: Some ideas for future work:
<option type="environ" name="TEST" value="Blubb" />
<option name="argument" value="--test" />
<option type="argument" value="--test" />
<option name="default_arguments" value="false" />
*/
/* name and path of executable */
char *executable;
} event_exec_t;
#ifdef _WIN32
/* TODO #2101: Implement script executing on win* */
#else
/* this sets up the new environment for script execution.
* We ignore most failtures as we can not handle them anyway.
*/
#ifdef HAVE_SETENV
static inline void __update_environ(const char *name, const char *value) {
if (!name || !value) return;
setenv(name, value, 1);
}
#else
#define __update_environ(x,y)
#endif
static inline void __setup_empty_script_environment(event_exec_t *self, event_t *event) {
ice_config_t * config = config_get_config();
mount_proxy *mountinfo;
source_t *source;
char buf[80];
int i;
/* close at least the first 1024 handles */
for (i = 0; i < 1024; i++)
close(i);
/* open null device */
i = open(config->null_device, O_RDWR);
if (i != -1) {
/* attach null device to stdin, stdout and stderr */
if (i != 0)
dup2(i, 0);
if (i != 1)
dup2(i, 1);
if (i != 2)
dup2(i, 2);
/* close null device */
if (i > 2)
close(i);
}
__update_environ("ICECAST_VERSION", ICECAST_VERSION_STRING);
__update_environ("ICECAST_HOSTNAME", config->hostname);
__update_environ("ICECAST_ADMIN", config->admin);
__update_environ("ICECAST_LOGDIR", config->log_dir);
__update_environ("EVENT_URI", event->uri);
__update_environ("EVENT_TRIGGER", event->trigger); /* new name */
__update_environ("SOURCE_ACTION", event->trigger); /* old name */
__update_environ("CLIENT_IP", event->connection_ip);
__update_environ("CLIENT_ROLE", event->client_role);
__update_environ("CLIENT_USERNAME", event->client_username);
__update_environ("CLIENT_USERAGENT", event->client_useragent);
snprintf(buf, sizeof(buf), "%lu", event->connection_id);
__update_environ("CLIENT_ID", buf);
snprintf(buf, sizeof(buf), "%lli", (long long int)event->connection_time);
__update_environ("CLIENT_CONNECTION_TIME", buf);
snprintf(buf, sizeof(buf), "%i", event->client_admin_command);
__update_environ("CLIENT_ADMIN_COMMAND", buf);
mountinfo = config_find_mount(config, event->uri, MOUNT_TYPE_NORMAL);
if (mountinfo) {
__update_environ("MOUNT_NAME", mountinfo->stream_name);
__update_environ("MOUNT_DESCRIPTION", mountinfo->stream_description);
__update_environ("MOUNT_URL", mountinfo->stream_url);
__update_environ("MOUNT_GENRE", mountinfo->stream_genre);
}
avl_tree_rlock(global.source_tree);
source = source_find_mount(event->uri);
if (source) {
__update_environ("SOURCE_MOUNTPOINT", source->mount);
__update_environ("SOURCE_PUBLIC", source->yp_public ? "true" : "false");
__update_environ("SROUCE_HIDDEN", source->hidden ? "true" : "false");
}
avl_tree_unlock(global.source_tree);
config_release_config();
}
static void _run_script (event_exec_t *self, event_t *event) {
pid_t pid, external_pid;
/* do a fork twice so that the command has init as parent */
external_pid = fork();
switch (external_pid)
{
case 0:
switch (pid = fork ())
{
case -1:
ICECAST_LOG_ERROR("Unable to fork %s (%s)", self->executable, strerror (errno));
break;
case 0: /* child */
if (access(self->executable, R_OK|X_OK) != 0) {
ICECAST_LOG_ERROR("Unable to run command %s (%s)", self->executable, strerror(errno));
exit(1);
}
ICECAST_LOG_DEBUG("Starting command %s", self->executable);
__setup_empty_script_environment(self, event);
/* consider to add action here as well */
if (event->uri) {
execl(self->executable, self->executable, event->uri, (char *)NULL);
} else {
execl(self->executable, self->executable, (char *)NULL);
}
exit(1);
default: /* parent */
break;
}
exit (0);
case -1:
ICECAST_LOG_ERROR("Unable to fork %s", strerror (errno));
break;
default: /* parent */
waitpid (external_pid, NULL, 0);
break;
}
}
#endif
static int event_exec_emit(void *state, event_t *event) {
event_exec_t *self = state;
#ifdef _WIN32
ICECAST_LOG_ERROR("<event type=\"exec\" ...> not supported on win*");
#else
_run_script(self, event);
#endif
return 0;
}
static void event_exec_free(void *state) {
event_exec_t *self = state;
free(self);
}
int event_get_exec(event_registration_t *er, config_options_t *options) {
event_exec_t *self = calloc(1, sizeof(event_exec_t));
if (!self)
return -