* Major rewrite of event streams

* If you used old event callbacks API, you need to switch to the streams API
    * See clixon_stream.[ch]
  * Old streams API which needs to be removed include:
    * clicon_log_register_callback()
    * subscription_add() --> stream_register()
    * backend_notify() and backend_notify_xml() - use stream_notify() instead
* Example uses "NETCONF" stream instead of "ROUTING"
* Added timeout option -t for clixon_netconf - quit after max time.
This commit is contained in:
Olof hagsand 2018-09-30 14:51:30 +02:00
parent d7fbe75c9e
commit 98f3cd0e32
31 changed files with 597 additions and 635 deletions

View file

@ -69,59 +69,6 @@
#include "backend_client.h"
#include "backend_handle.h"
/*! Add client notification subscription. Ie send notify to this client when event occurs
* @param[in] ce Client entry struct
* @param[in] stream Notification stream name
* @param[in] format How to display event (see enum format_enum)
* @param[in] filter Filter, what to display, eg xpath for format=xml, fnmatch
*
* @see backend_notify - where subscription is made and notify call is made
*/
static struct client_subscription *
client_subscription_add(struct client_entry *ce,
char *stream,
enum format_enum format,
char *filter)
{
struct client_subscription *su = NULL;
if ((su = malloc(sizeof(*su))) == NULL){
clicon_err(OE_PLUGIN, errno, "malloc");
goto done;
}
memset(su, 0, sizeof(*su));
su->su_stream = strdup(stream);
su->su_format = format;
su->su_filter = filter?strdup(filter):strdup("");
su->su_next = ce->ce_subscription;
ce->ce_subscription = su;
done:
return su;
}
/*! Delete stream subscription from client subscription list */
static int
client_subscription_delete(struct client_entry *ce,
struct client_subscription *su0)
{
struct client_subscription *su;
struct client_subscription **su_prev;
su_prev = &ce->ce_subscription; /* this points to stack and is not real backpointer */
for (su = *su_prev; su; su = su->su_next){
if (su == su0){
*su_prev = su->su_next;
free(su->su_stream);
if (su->su_filter)
free(su->su_filter);
free(su);
break;
}
su_prev = &su->su_next;
}
return 0;
}
static struct client_entry *
ce_find_bypid(struct client_entry *ce_list,
int pid)
@ -134,6 +81,31 @@ ce_find_bypid(struct client_entry *ce_list,
return NULL;
}
static int
ce_event_cb(clicon_handle h,
void *event,
void *arg)
{
struct client_entry *ce = (struct client_entry *)arg;
if (send_msg_notify_xml(ce->ce_s, (cxobj*)event) < 0){
if (errno == ECONNRESET || errno == EPIPE){
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
#if 0
/* We should remove here but removal is not possible
from a client since backend_client is not linked.
Maybe we should add it to the plugin, but it feels
"safe" that you cant remove a client.
Instead, the client is (hopefully) removed elsewhere?
*/
backend_client_rm(h, ce);
#endif
}
}
return 0;
}
/*! Remove client entry state
* Close down everything wrt clients (eg sockets, subscriptions)
* Finally actually remove client struct in handle
@ -148,7 +120,6 @@ backend_client_rm(clicon_handle h,
struct client_entry *c;
struct client_entry *c0;
struct client_entry **ce_prev;
struct client_subscription *su;
c0 = backend_client_list(h);
ce_prev = &c0; /* this points to stack and is not real backpointer */
@ -159,8 +130,8 @@ backend_client_rm(clicon_handle h,
close(ce->ce_s);
ce->ce_s = 0;
}
while ((su = ce->ce_subscription) != NULL)
client_subscription_delete(ce, su);
/* for all streams */
stream_cb_delete(h, NULL, ce_event_cb, (void*)ce);
break;
}
ce_prev = &c->ce_next;
@ -900,6 +871,7 @@ from_client_delete_config(clicon_handle h,
return retval;
}
/*! Internal message: Create subscription for notifications see RFC 5277
* @param[in] h Clicon handle
* @param[in] xe Netconf request xml tree
@ -910,7 +882,7 @@ from_client_delete_config(clicon_handle h,
* @example:
* <create-subscription>
* <stream>RESULT</stream> # If not present, events in the default NETCONF stream will be sent.
* <filter>XPATH-EXPR<(filter>
* <filter type="xpath" select="XPATH-EXPR"/>
* <startTime/> # only for replay (NYI)
* <stopTime/> # only for replay (NYI)
* </create-subscription>
@ -922,24 +894,27 @@ from_client_create_subscription(clicon_handle h,
cbuf *cbret)
{
char *stream = "NETCONF";
char *filter = NULL;
int retval = -1;
cxobj *x; /* Genereic xml tree */
cxobj *x; /* Generic xml tree */
cxobj *xfilter; /* Filter xml tree */
char *ftype;
char *selector = NULL;
if ((x = xpath_first(xe, "//stream")) != NULL)
stream = xml_find_value(x, "body");
if ((x = xpath_first(xe, "//filter")) != NULL){
if ((ftype = xml_find_value(x, "type")) != NULL){
if ((xfilter = xpath_first(xe, "//filter")) != NULL){
if ((ftype = xml_find_value(xfilter, "type")) != NULL){
/* Only accept xpath as filter type */
if (strcmp(ftype, "xpath") != 0){
if (netconf_operation_failed(cbret, "application", "Only xpath filter type supported")< 0)
goto done;
goto ok;
}
if ((selector = xml_find_value(xfilter, "select")) == NULL)
goto done;
}
}
if (client_subscription_add(ce, stream, FORMAT_XML, filter) == NULL)
if (stream_cb_add(h, stream, selector, ce_event_cb, (void*)ce) < 0)
goto done;
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
ok:
@ -1334,6 +1309,7 @@ from_client_msg(clicon_handle h,
}
else if (strcmp(name, "close-session") == 0){
xmldb_unlock_all(h, pid);
stream_cb_delete(h, NULL, ce_event_cb, (void*)ce);
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
}
else if (strcmp(name, "kill-session") == 0){

View file

@ -52,19 +52,8 @@ struct client_entry{
int ce_pid; /* Process id */
int ce_uid; /* User id of calling process */
clicon_handle ce_handle; /* clicon config handle (all clients have same?) */
struct client_subscription *ce_subscription; /* notification subscriptions */
};
/* Notification subscription info
* @see subscription in config_handle.c
*/
struct client_subscription{
struct client_subscription *su_next;
int su_s; /* stream socket */
enum format_enum su_format; /* format of notification stream */
char *su_stream;
char *su_filter;
};
/*
* Prototypes

View file

@ -98,7 +98,6 @@ backend_terminate(clicon_handle h)
xmldb_plugin_unload(h); /* unload storage plugin */
backend_handle_exit(h); /* Cannot use h after this */
event_exit();
clicon_log_register_callback(NULL, NULL);
clicon_debug(1, "%s done", __FUNCTION__);
clicon_log_exit();
return 0;
@ -191,7 +190,6 @@ db_merge(clicon_handle h,
return retval;
}
/*! Create backend server socket and register callback
*/
static int
@ -211,45 +209,6 @@ server_socket(clicon_handle h)
return ss;
}
/*! Callback for CLICON log events
* If you make a subscription to CLICON stream, this function is called for every
* log event.
*/
static int
backend_log_cb(int level,
char *msg,
void *arg)
{
int retval = -1;
size_t n;
char *ptr;
char *nptr;
char *newmsg = NULL;
/* backend_notify() will go through all clients and see if any has
registered "CLICON", and if so make a clicon_proto notify message to
those clients.
Sanitize '%' into "%%" to prevent segvfaults in vsnprintf later.
At this stage all formatting is already done */
n = 0;
for(ptr=msg; *ptr; ptr++)
if (*ptr == '%')
n++;
if ((newmsg = malloc(strlen(msg) + n + 1)) == NULL) {
clicon_err(OE_UNIX, errno, "malloc");
return -1;
}
for(ptr=msg, nptr=newmsg; *ptr; ptr++) {
*nptr++ = *ptr;
if (*ptr == '%')
*nptr++ = '%';
}
retval = backend_notify(arg, "CLICON", level, newmsg);
free(newmsg);
return retval;
}
/*! Call plugin_start with -- user options */
static int
plugin_start_useroptions(clicon_handle h,
@ -758,8 +717,6 @@ main(int argc,
if (stream_register(h, "NETCONF", "default NETCONF event stream") < 0)
goto done;
if (stream_register(h, "CLICON", "Clicon logs") < 0)
goto done;
if ((xmldb_plugin = clicon_xmldb_plugin(h)) == NULL){
clicon_log(LOG_ERR, "No xmldb plugin given (specify option CLICON_XMLDB_PLUGIN).\n");
@ -850,9 +807,6 @@ main(int argc,
if ((pid = pidfile_write(pidfile)) < 0)
goto done;
/* Register log notifications */
if (clicon_log_register_callback(backend_log_cb, h) < 0)
goto done;
clicon_log(LOG_NOTICE, "%s: %u Started", __PROGRAM__, getpid());
if (set_signal(SIGTERM, backend_sig_term, NULL) < 0){
clicon_err(OE_DEMON, errno, "Setting signal");

View file

@ -91,7 +91,6 @@ struct backend_handle {
/* ------ end of common handle ------ */
struct client_entry *bh_ce_list; /* The client list */
int bh_ce_nr; /* Number of clients, just increment */
struct handle_subscription *bh_subscription; /* Event subscription list */
cxobj *bh_nacm; /* NACM external struct */
};
@ -121,155 +120,6 @@ backend_handle_exit(clicon_handle h)
return 0;
}
/*! Notify event and distribute to all registered clients
*
* @param[in] h Clicon handle
* @param[in] stream Name of event stream. CLICON is predefined as LOG stream
* @param[in] level Event level (not used yet)
* @param[in] event Actual message as text format
*
* Stream is a string used to qualify the event-stream. Distribute the
* event to all clients registered to this backend.
* XXX: event-log NYI.
* @see also subscription_add()
* @see also backend_notify_xml()
*/
int
backend_notify(clicon_handle h,
char *stream,
int level,
char *event)
{
struct client_entry *ce;
struct client_entry *ce_next;
struct client_subscription *su;
struct handle_subscription *hs;
int retval = -1;
clicon_debug(2, "%s %s", __FUNCTION__, stream);
/* First thru all clients(sessions), and all subscriptions and find matches */
for (ce = backend_client_list(h); ce; ce = ce_next){
ce_next = ce->ce_next;
for (su = ce->ce_subscription; su; su = su->su_next)
if (strcmp(su->su_stream, stream) == 0){
if (strlen(su->su_filter)==0 || fnmatch(su->su_filter, event, 0) == 0){
if (send_msg_notify(ce->ce_s, level, event) < 0){
if (errno == ECONNRESET || errno == EPIPE){
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
#if 0
/* We should remove here but removal is not possible
from a client since backend_client is not linked.
Maybe we should add it to the plugin, but it feels
"safe" that you cant remove a client.
Instead, the client is (hopefully) removed elsewhere?
*/
backend_client_rm(h, ce);
#endif
break;
}
goto done;
}
}
}
}
/* Then go thru all global (handle) subscriptions and find matches */
hs = NULL;
while ((hs = subscription_each(h, hs)) != NULL){
if (hs->hs_format != FORMAT_TEXT)
continue;
if (strcmp(hs->hs_stream, stream))
continue;
if (hs->hs_filter==NULL ||
strlen(hs->hs_filter)==0 ||
fnmatch(hs->hs_filter, event, 0) == 0)
if ((*hs->hs_fn)(h, event, hs->hs_arg) < 0)
goto done;
}
retval = 0;
done:
return retval;
}
/*! Notify event and distribute to all registered clients
*
* @param[in] h Clicon handle
* @param[in] stream Name of event stream. CLICON is predefined as LOG stream
* @param[in] level Event level (not used yet)
* @param[in] x Actual message as xml tree
*
* Stream is a string used to qualify the event-stream. Distribute the
* event to all clients registered to this backend.
* XXX: event-log NYI.
* @see also subscription_add()
* @see also backend_notify()
*/
int
backend_notify_xml(clicon_handle h,
char *stream,
int level,
cxobj *x)
{
struct client_entry *ce;
struct client_entry *ce_next;
struct client_subscription *su;
int retval = -1;
cbuf *cb = NULL;
struct handle_subscription *hs;
clicon_debug(1, "%s %s", __FUNCTION__, stream);
/* Now go thru all clients(sessions), and all subscriptions and find matches */
for (ce = backend_client_list(h); ce; ce = ce_next){
ce_next = ce->ce_next;
for (su = ce->ce_subscription; su; su = su->su_next)
if (strcmp(su->su_stream, stream) == 0){
if (strlen(su->su_filter)==0 || xpath_first(x, "%s", su->su_filter) != NULL){
if (cb==NULL){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_PLUGIN, errno, "cbuf_new");
goto done;
}
if (clicon_xml2cbuf(cb, x, 0, 0) < 0)
goto done;
}
if (send_msg_notify(ce->ce_s, level, cbuf_get(cb)) < 0){
if (errno == ECONNRESET || errno == EPIPE){
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
#if 0
/* We should remove here but removal is not possible
from a client since backend_client is not linked.
Maybe we should add it to the plugin, but it feels
"safe" that you cant remove a client.
Instead, the client is (hopefully) removed elsewhere?
*/
backend_client_rm(h, ce);
#endif
break;
}
goto done;
}
}
}
}
/* Then go thru all global (handle) subscriptions and find matches */
hs = NULL;
while ((hs = subscription_each(h, hs)) != NULL){
if (hs->hs_format != FORMAT_XML)
continue;
if (strcmp(hs->hs_stream, stream))
continue;
if (strlen(hs->hs_filter)==0 || xpath_first(x, "%s", hs->hs_filter) != NULL){
if ((*hs->hs_fn)(h, x, hs->hs_arg) < 0)
goto done;
}
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Add new client, typically frontend such as cli, netconf, restconf
* @param[in] h Clicon handle
* @param[in] addr Address of client
@ -332,111 +182,6 @@ backend_client_delete(clicon_handle h,
return 0;
}
/*! Add subscription given stream name, callback and argument
* @param[in] h Clicon handle
* @param[in] stream Name of event stream
* @param[in] format Expected format of event, eg text or xml
* @param[in] filter Filter to match event, depends on format, eg xpath for xml
* @param[in] fn Callback when event occurs
* @param[in] arg Argument to use with callback. Also handle when deleting
* Note that arg is not a real handle.
* @see subscription_delete
* @see subscription_each
*/
struct handle_subscription *
subscription_add(clicon_handle h,
char *stream,
enum format_enum format,
char *filter,
subscription_fn_t fn,
void *arg)
{
struct backend_handle *bh = handle(h);
struct handle_subscription *hs = NULL;
if ((hs = malloc(sizeof(*hs))) == NULL){
clicon_err(OE_PLUGIN, errno, "malloc");
goto done;
}
memset(hs, 0, sizeof(*hs));
hs->hs_stream = strdup(stream);
hs->hs_format = format;
hs->hs_filter = filter?strdup(filter):NULL;
hs->hs_next = bh->bh_subscription;
hs->hs_fn = fn;
hs->hs_arg = arg;
bh->bh_subscription = hs;
done:
return hs;
}
/*! Delete subscription given stream name, callback and argument
* @param[in] h Clicon handle
* @param[in] stream Name of event stream
* @param[in] fn Callback when event occurs
* @param[in] arg Argument to use with callback and handle
* Note that arg is not a real handle.
* @see subscription_add
* @see subscription_each
*/
int
subscription_delete(clicon_handle h,
char *stream,
subscription_fn_t fn,
void *arg)
{
struct backend_handle *bh = handle(h);
struct handle_subscription *hs;
struct handle_subscription **hs_prev;
hs_prev = &bh->bh_subscription; /* this points to stack and is not real backpointer */
for (hs = *hs_prev; hs; hs = hs->hs_next){
/* XXX arg == hs->hs_arg */
if (strcmp(hs->hs_stream, stream)==0 && hs->hs_fn == fn){
*hs_prev = hs->hs_next;
free(hs->hs_stream);
if (hs->hs_filter)
free(hs->hs_filter);
if (hs->hs_arg)
free(hs->hs_arg);
free(hs);
break;
}
hs_prev = &hs->hs_next;
}
return 0;
}
/*! Iterator over subscriptions
*
* NOTE: Never manipulate the child-list during operation or using the
* same object recursively, the function uses an internal field to remember the
* index used. It works as long as the same object is not iterated concurrently.
*
* @param[in] h clicon handle
* @param[in] hprev iterator, initialize with NULL
* @code
* clicon_handle h;
* struct handle_subscription *hs = NULL;
* while ((hs = subscription_each(h, hs)) != NULL) {
* ...
* }
* @endcode
*/
struct handle_subscription *
subscription_each(clicon_handle h,
struct handle_subscription *hprev)
{
struct backend_handle *bh = handle(h);
struct handle_subscription *hs = NULL;
if (hprev)
hs = hprev->hs_next;
else
hs = bh->bh_subscription;
return hs;
}
int
backend_nacm_list_set(clicon_handle h,
cxobj *xnacm)

View file

@ -43,34 +43,9 @@
/*
* Types
*/
/* Notification subscription info
* @see client_subscription in config_client.h
*/
struct handle_subscription{
struct handle_subscription *hs_next;
enum format_enum hs_format; /* format (enum format_enum) XXX not needed? */
char *hs_stream; /* name of notify stream */
char *hs_filter; /* filter, if format=xml: xpath, if text: fnmatch */
subscription_fn_t hs_fn; /* Callback when event occurs */
void *hs_arg; /* Callback argument */
};
/*
* Prototypes
*/
/* Log for netconf notify function (config_client.c) */
int backend_notify(clicon_handle h, char *stream, int level, char *txt);
int backend_notify_xml(clicon_handle h, char *stream, int level, cxobj *x);
struct handle_subscription *subscription_add(clicon_handle h, char *stream,
enum format_enum format, char *filter,
subscription_fn_t fn, void *arg);
int subscription_delete(clicon_handle h, char *stream,
subscription_fn_t fn, void *arg);
struct handle_subscription *subscription_each(clicon_handle h,
struct handle_subscription *hprev);
#endif /* _CLIXON_BACKEND_HANDLE_H_ */