afb-session: Improve readability
[src/app-framework-binder.git] / src / afb-session.c
index 3befb92..c941eef 100644 (file)
 #include "afb-session.h"
 #include "verbose.h"
 
-#define HEADCOUNT    16
-#define COOKEYCOUNT  8
-#define COOKEYMASK   (COOKEYCOUNT - 1)
+#define SIZEUUID       37
+#define HEADCOUNT      16
+#define COOKEYCOUNT    8
+#define COOKEYMASK     (COOKEYCOUNT - 1)
 
 #define NOW (time(NULL))
 
@@ -50,26 +51,26 @@ struct afb_session
        struct afb_session *next; /* link to the next */
        unsigned refcount;
        int timeout;
-       time_t expiration;    // expiration time of the token
+       time_t expiration;      // expiration time of the token
        pthread_mutex_t mutex;
        char idx;
-       char uuid[37];        // long term authentication of remote client
-       char token[37];       // short term authentication of remote client
+       char uuid[SIZEUUID];    // long term authentication of remote client
+       char token[SIZEUUID];   // short term authentication of remote client
        struct cookie *cookies[COOKEYCOUNT];
 };
 
 // Session UUID are store in a simple array [for 10 sessions this should be enough]
 static struct {
-       pthread_mutex_t mutex;          // declare a mutex to protect hash table
+       pthread_mutex_t mutex;  // declare a mutex to protect hash table
        struct afb_session *heads[HEADCOUNT]; // sessions
-       int count;                      // current number of sessions
+       int count;      // current number of sessions
        int max;
        int timeout;
-       char initok[37];
+       char initok[SIZEUUID];
 } sessions;
 
 /* generate a uuid */
-static void new_uuid(char uuid[37])
+static void new_uuid(char uuid[SIZEUUID])
 {
        uuid_t newuuid;
        uuid_generate(newuuid);
@@ -161,6 +162,7 @@ static void destroy (struct afb_session *session)
 
        assert (session != NULL);
 
+       remove_all_cookies(session);
        pthread_mutex_lock(&sessions.mutex);
        prv = &sessions.heads[(int)session->idx];
        while (*prv)
@@ -214,18 +216,31 @@ static time_t cleanup ()
 static struct afb_session *add_session (const char *uuid, int timeout, time_t now, int idx)
 {
        struct afb_session *session;
+       time_t expiration;
 
+       /* check arguments */
        if (!AFB_SESSION_TIMEOUT_IS_VALID(timeout)
         || (uuid && strlen(uuid) >= sizeof session->uuid)) {
                errno = EINVAL;
                return NULL;
        }
 
+       /* check session count */
        if (sessions.count >= sessions.max) {
                errno = EBUSY;
                return NULL;
        }
 
+       /* compute expiration */
+       if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
+               timeout = sessions.timeout;
+       expiration = now + timeout;
+       if (timeout == AFB_SESSION_TIMEOUT_INFINITE || expiration < 0) {
+               expiration = (time_t)(~(time_t)0);
+               if (expiration < 0)
+                       expiration = (time_t)(((unsigned long long)expiration) >> 1);
+       }
+
        /* allocates a new one */
        session = calloc(1, sizeof *session);
        if (session == NULL) {
@@ -233,22 +248,13 @@ static struct afb_session *add_session (const char *uuid, int timeout, time_t no
                return NULL;
        }
 
+       /* initialize */
        pthread_mutex_init(&session->mutex, NULL);
        session->refcount = 1;
        strcpy(session->uuid, uuid);
        strcpy(session->token, sessions.initok);
-
-       /* init timeout */
-       if (timeout == AFB_SESSION_TIMEOUT_DEFAULT)
-               timeout = sessions.timeout;
        session->timeout = timeout;
-       session->expiration = now + timeout;
-       if (timeout == AFB_SESSION_TIMEOUT_INFINITE || session->expiration < 0) {
-               session->expiration = (time_t)(~(time_t)0);
-               if (session->expiration < 0)
-                       session->expiration = (time_t)(((unsigned long long)session->expiration) >> 1);
-       }
-
+       session->expiration = expiration;
        session->idx = (char)idx;
        session->next = sessions.heads[idx];
        sessions.heads[idx] = session;
@@ -257,10 +263,11 @@ static struct afb_session *add_session (const char *uuid, int timeout, time_t no
        return session;
 }
 
+/* create a new session for the given timeout */
 static struct afb_session *new_session (int timeout, time_t now)
 {
        int idx;
-       char uuid[37];
+       char uuid[SIZEUUID];
 
        do {
                new_uuid(uuid);
@@ -293,6 +300,8 @@ struct afb_session *afb_session_search (const char *uuid)
        pthread_mutex_lock(&sessions.mutex);
        cleanup();
        session = search(uuid, pearson4(uuid));
+       if (session)
+               __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
        pthread_mutex_unlock(&sessions.mutex);
        return session;
 
@@ -315,7 +324,7 @@ struct afb_session *afb_session_get (const char *uuid, int timeout, int *created
        else {
                idx = pearson4(uuid);
                session = search(uuid, idx);
-               if (session != NULL) {
+               if (session) {
                        __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
                        pthread_mutex_unlock(&sessions.mutex);
                        if (created)
@@ -346,8 +355,11 @@ void afb_session_unref(struct afb_session *session)
        if (session != NULL) {
                assert(session->refcount != 0);
                if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) {
+                       pthread_mutex_lock(&session->mutex);
                        if (session->uuid[0] == 0)
                                destroy (session);
+                       else
+                               pthread_mutex_unlock(&session->mutex);
                }
        }
 }
@@ -356,12 +368,16 @@ void afb_session_unref(struct afb_session *session)
 void afb_session_close (struct afb_session *session)
 {
        assert(session != NULL);
+       pthread_mutex_lock(&session->mutex);
        if (session->uuid[0] != 0) {
                session->uuid[0] = 0;
-               remove_all_cookies(session);
-               if (session->refcount == 0)
+               remove_all_cookies(session);
+               if (session->refcount == 0) {
                        destroy (session);
+                       return;
+               }
        }
+       pthread_mutex_unlock(&session->mutex);
 }
 
 // Sample Generic Ping Debug API
@@ -424,12 +440,12 @@ static int cookeyidx(const void *key)
  *
  * The behaviour of this function depends on its parameters:
  *
- * @param session the session
- * @param key     the key of the cookie
- * @param makecb  the creation function
- * @param freecb  the release function
- * @param closure an argument
- * @param replace a boolean enforcing replecement of the previous value
+ * @param session      the session
+ * @param key          the key of the cookie
+ * @param makecb       the creation function or NULL
+ * @param freecb       the release function or NULL
+ * @param closure      an argument for makecb or the value if makecb==NULL
+ * @param replace      a boolean enforcing replecement of the previous value
  *
  * @return the value of the cookie
  *