session: introduce api to register session create and destroy callbacks
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 3 Sep 2013 10:21:09 +0000 (12:21 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 3 Sep 2013 10:47:04 +0000 (12:47 +0200)
session.c
session.h

index d970d45b139d2c3614bc9d8cd5917c1ce4b7e3e0..6cc4ffe883dfd249569aa361f0837bde4c46d510 100644 (file)
--- 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);
+}
index bcdcb96adedd6c65e4ae86983b2b97208a72ffe8..59b2864434ad12ca84b1f9773a9a2a811166d02e 100644 (file)
--- 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