afb-session: Add function to enumerate sessions
[src/app-framework-binder.git] / src / afb-session.c
index dfa383b..17ca7af 100644 (file)
@@ -346,6 +346,28 @@ int afb_session_init (int max_session_count, int timeout, const char *initok)
        return 0;
 }
 
+/**
+ * Iterate the sessions and call 'callback' with
+ * the 'closure' for each session.
+ */
+void afb_session_foreach(void (*callback)(void *closure, struct afb_session *session), void *closure)
+{
+       struct afb_session *session;
+       int idx;
+
+       /* Loop on Sessions Table and remove anything that is older than timeout */
+       sessionset_lock();
+       for (idx = 0 ; idx < HEADCOUNT; idx++) {
+               session = sessions.heads[idx];
+               while (session) {
+                       if (!session->closed)
+                               callback(closure, session);
+                       session = session->next;
+               }
+       }
+       sessionset_unlock();
+}
+
 /**
  * Cleanup the sessionset of its closed or expired sessions
  */
@@ -428,7 +450,6 @@ end:
 struct afb_session *afb_session_addref(struct afb_session *session)
 {
        if (session != NULL) {
-               afb_hook_session_unref(session);
                afb_hook_session_addref(session);
                session->refcount++;
                session_unlock(session);
@@ -444,7 +465,7 @@ void afb_session_unref(struct afb_session *session)
 
        session_lock(session);
        afb_hook_session_unref(session);
-       if (!--session->refcount) {
+       if (--session->refcount) {
                if (session->autoclose)
                        session_close(session);
                if (session->notinset) {