From: Jo-Philipp Wich Date: Tue, 3 Sep 2013 10:21:09 +0000 (+0200) Subject: session: introduce api to register session create and destroy callbacks X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=60b9c9d919fd22339105f8e1835cb5cd7ff16d9c;p=oweals%2Frpcd.git session: introduce api to register session create and destroy callbacks --- diff --git a/session.c b/session.c index d970d45..6cc4ffe 100644 --- a/session.c +++ b/session.c @@ -27,6 +27,9 @@ static struct avl_tree sessions; static struct blob_buf buf; +static LIST_HEAD(create_callbacks); +static LIST_HEAD(destroy_callbacks); + static const struct blobmsg_policy new_policy = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 }; @@ -200,6 +203,10 @@ rpc_session_destroy(struct rpc_session *ses) struct rpc_session_acl *acl, *nacl; struct rpc_session_acl_scope *acl_scope, *nacl_scope; struct rpc_session_data *data, *ndata; + struct rpc_session_cb *cb; + + list_for_each_entry(cb, &destroy_callbacks, list) + cb->cb(ses, cb->priv); uloop_timeout_cancel(&ses->t); @@ -230,6 +237,7 @@ static struct rpc_session * rpc_session_create(int timeout) { struct rpc_session *ses; + struct rpc_session_cb *cb; ses = calloc(1, sizeof(*ses)); if (!ses) @@ -246,6 +254,9 @@ rpc_session_create(int timeout) ses->t.cb = rpc_session_timeout; rpc_touch_session(ses); + list_for_each_entry(cb, &create_callbacks, list) + cb->cb(ses, cb->priv); + return ses; } @@ -720,3 +731,15 @@ bool rpc_session_access(const char *sid, const char *scope, return rpc_session_acl_allowed(ses, scope, object, function); } + +void rpc_session_create_cb(struct rpc_session_cb *cb) +{ + if (cb && cb->cb) + list_add(&cb->list, &create_callbacks); +} + +void rpc_session_destroy_cb(struct rpc_session_cb *cb) +{ + if (cb && cb->cb) + list_add(&cb->list, &destroy_callbacks); +} diff --git a/session.h b/session.h index bcdcb96..59b2864 100644 --- a/session.h +++ b/session.h @@ -59,4 +59,13 @@ int rpc_session_api_init(struct ubus_context *ctx); bool rpc_session_access(const char *sid, const char *scope, const char *object, const char *function); +struct rpc_session_cb { + struct list_head list; + void (*cb)(struct rpc_session *, void *); + void *priv; +}; + +void rpc_session_create_cb(struct rpc_session_cb *cb); +void rpc_session_destroy_cb(struct rpc_session_cb *cb); + #endif