session: introduce api to register session create and destroy callbacks

This commit is contained in:
Jo-Philipp Wich 2013-09-03 12:21:09 +02:00
parent 11f37e2889
commit 60b9c9d919
2 changed files with 32 additions and 0 deletions

View file

@ -27,6 +27,9 @@
static struct avl_tree sessions; static struct avl_tree sessions;
static struct blob_buf buf; static struct blob_buf buf;
static LIST_HEAD(create_callbacks);
static LIST_HEAD(destroy_callbacks);
static const struct blobmsg_policy new_policy = { static const struct blobmsg_policy new_policy = {
.name = "timeout", .type = BLOBMSG_TYPE_INT32 .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 *acl, *nacl;
struct rpc_session_acl_scope *acl_scope, *nacl_scope; struct rpc_session_acl_scope *acl_scope, *nacl_scope;
struct rpc_session_data *data, *ndata; 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); uloop_timeout_cancel(&ses->t);
@ -230,6 +237,7 @@ static struct rpc_session *
rpc_session_create(int timeout) rpc_session_create(int timeout)
{ {
struct rpc_session *ses; struct rpc_session *ses;
struct rpc_session_cb *cb;
ses = calloc(1, sizeof(*ses)); ses = calloc(1, sizeof(*ses));
if (!ses) if (!ses)
@ -246,6 +254,9 @@ rpc_session_create(int timeout)
ses->t.cb = rpc_session_timeout; ses->t.cb = rpc_session_timeout;
rpc_touch_session(ses); rpc_touch_session(ses);
list_for_each_entry(cb, &create_callbacks, list)
cb->cb(ses, cb->priv);
return ses; 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); 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);
}

View file

@ -59,4 +59,13 @@ int rpc_session_api_init(struct ubus_context *ctx);
bool rpc_session_access(const char *sid, const char *scope, bool rpc_session_access(const char *sid, const char *scope,
const char *object, const char *function); 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 #endif