summaryrefslogtreecommitdiff
path: root/libfdproto/sessions.c
diff options
context:
space:
mode:
authorLucas de Castro Borges <lucas@gnuabordo.com.br>2024-08-08 14:34:10 -0300
committerLucas de Castro Borges <lucas@gnuabordo.com.br>2024-08-08 14:34:10 -0300
commit56f96f0bdc6187ce19064d311c000656ae68008b (patch)
tree104a7843861befbd4da34a66f03b85d7b9ea0df9 /libfdproto/sessions.c
parent3e8d3ed13f58af810934de696c34bf5bf16ddcc6 (diff)
New upstream version 1.5.0upstream
Diffstat (limited to 'libfdproto/sessions.c')
-rw-r--r--libfdproto/sessions.c316
1 files changed, 167 insertions, 149 deletions
diff --git a/libfdproto/sessions.c b/libfdproto/sessions.c
index b6c94fa..ca81dba 100644
--- a/libfdproto/sessions.c
+++ b/libfdproto/sessions.c
@@ -2,7 +2,7 @@
* Software License Agreement (BSD License) *
* Author: Sebastien Decugis <sdecugis@freediameter.net> *
* *
-* Copyright (c) 2013, WIDE Project and NICT *
+* Copyright (c) 2019, WIDE Project and NICT *
* All rights reserved. *
* *
* Redistribution and use of this software in source and binary forms, with or without modification, are *
@@ -34,7 +34,7 @@
*********************************************************************************************************/
/* Sessions module.
- *
+ *
* Basic functionalities to help implementing User sessions state machines from RFC3588.
*/
@@ -91,15 +91,15 @@ struct state {
/* Session object, one for each value of Session-Id AVP */
struct session {
int eyec; /* Eyecatcher, SI_EYEC */
-
+
os0_t sid; /* The \0-terminated Session-Id */
size_t sidlen; /* cached length of sid */
uint32_t hash; /* computed hash of sid */
struct fd_list chain_h;/* chaining in the hash table of sessions. */
-
+
struct timespec timeout;/* Timeout date for the session */
struct fd_list expire; /* List of expiring sessions, ordered by timeouts. */
-
+
pthread_mutex_t stlock; /* A lock to protect the list of states associated with this session */
struct fd_list states; /* Sentinel for the list of states of this session. */
int msg_cnt;/* Reference counter for the messages pointing to this session */
@@ -125,7 +125,7 @@ static pthread_mutex_t sid_lock = PTHREAD_MUTEX_INITIALIZER;
/* Expiring sessions management */
static struct fd_list exp_sentinel = FD_LIST_INITIALIZER(exp_sentinel); /* list of sessions ordered by their timeout date */
static pthread_mutex_t exp_lock = PTHREAD_MUTEX_INITIALIZER; /* lock protecting the list. */
-static pthread_cond_t exp_cond = PTHREAD_COND_INITIALIZER; /* condvar used by the expiry mecahinsm. */
+static pthread_cond_t exp_cond = PTHREAD_COND_INITIALIZER; /* condvar used by the expiry mechainsm. */
static pthread_t exp_thr = (pthread_t)NULL; /* The expiry thread that handles cleanup of expired sessions */
/* Hierarchy of the locks, to avoid deadlocks:
@@ -140,27 +140,27 @@ static pthread_t exp_thr = (pthread_t)NULL; /* The expiry thread that handles c
static struct session * new_session(os0_t sid, size_t sidlen, uint32_t hash)
{
struct session * sess;
-
+
TRACE_ENTRY("%p %zd", sid, sidlen);
CHECK_PARAMS_DO( sid && sidlen, return NULL );
-
+
CHECK_MALLOC_DO( sess = malloc(sizeof(struct session)), return NULL );
memset(sess, 0, sizeof(struct session));
-
+
sess->eyec = SI_EYEC;
-
+
sess->sid = sid;
sess->sidlen = sidlen;
sess->hash = hash;
fd_list_init(&sess->chain_h, sess);
-
+
CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &sess->timeout), return NULL );
sess->timeout.tv_sec += SESS_DEFAULT_LIFETIME;
fd_list_init(&sess->expire, sess);
-
+
CHECK_POSIX_DO( pthread_mutex_init(&sess->stlock, NULL), return NULL );
fd_list_init(&sess->states, sess);
-
+
return sess;
}
@@ -174,21 +174,21 @@ static void del_session(struct session * s)
CHECK_POSIX_DO( pthread_mutex_destroy(&s->stlock), /* continue */ );
free(s);
}
-
+
/* The expiry thread */
static void * exp_fct(void * arg)
{
fd_log_threadname ( "Session/expire" );
TRACE_ENTRY( "" );
-
-
+
+
do {
struct timespec now;
struct session * first;
-
+
CHECK_POSIX_DO( pthread_mutex_lock(&exp_lock), break );
pthread_cleanup_push( fd_cleanup_mutex, &exp_lock );
-again:
+again:
/* Check if there are expiring sessions available */
if (FD_IS_LIST_EMPTY(&exp_sentinel)) {
/* Just wait for a change or cancelation */
@@ -196,39 +196,53 @@ again:
/* Restart the loop on wakeup */
goto again;
}
-
+
/* Get the pointer to the session that expires first */
first = (struct session *)(exp_sentinel.next->o);
ASSERT( VALIDATE_SI(first) );
-
+
/* Get the current time */
CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &now), break );
/* If first session is not expired, we just wait until it happens */
if ( TS_IS_INFERIOR( &now, &first->timeout ) ) {
-
- CHECK_POSIX_DO2( pthread_cond_timedwait( &exp_cond, &exp_lock, &first->timeout ),
- ETIMEDOUT, /* ETIMEDOUT is a normal error, continue */,
- /* on other error, */ break );
-
- /* on wakeup, loop */
- goto again;
+ int ret;
+
+ ret = pthread_cond_timedwait(&exp_cond, &exp_lock, &first->timeout);
+ switch (ret) {
+ case 0:
+ case ETIMEDOUT:
+ /* on wakeup or time-out, loop */
+ goto again;
+ case EINVAL:
+ if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
+ break;
+ }
+ if (TS_IS_INFERIOR(&now, &first->timeout)) {
+ TRACE_DEBUG(FULL, "'pthread_cond_timedwait(&exp_cond, &exp_lock, &first->timeout)' : timer expired before loop could start");
+ goto again;
+ }
+ /* FALLTHROUGH */
+ default:
+ TRACE_ERROR("ERROR: in 'pthread_cond_timedwait(&exp_cond, &exp_lock, &first->timeout)' :\t%s", strerror(ret));
+ break;
+ }
}
-
+
/* Now, the first session in the list is expired; destroy it */
pthread_cleanup_pop( 0 );
CHECK_POSIX_DO( pthread_mutex_unlock(&exp_lock), break );
-
+
CHECK_FCT_DO( fd_sess_destroy( &first ), break );
-
+
} while (1);
-
+
TRACE_DEBUG(INFO, "A system error occurred in session module! Expiry thread is terminating...");
ASSERT(0);
return NULL;
}
-
-
+
+
/********************************************************************************************************/
@@ -236,19 +250,19 @@ again:
int fd_sess_init(void)
{
int i;
-
+
TRACE_ENTRY( "" );
-
+
/* Initialize the global counters */
sid_h = (uint32_t) time(NULL);
sid_l = 0;
-
+
/* Initialize the hash table */
for (i = 0; i < sizeof(sess_hash) / sizeof(sess_hash[0]); i++) {
fd_list_init( &sess_hash[i].sentinel, NULL );
CHECK_POSIX( pthread_mutex_init(&sess_hash[i].lock, NULL) );
}
-
+
return 0;
}
@@ -257,7 +271,7 @@ int fd_sess_start(void)
{
/* Start session garbage collector (expiry) */
CHECK_POSIX( pthread_create(&exp_thr, NULL, exp_fct, NULL) );
-
+
return 0;
}
@@ -266,9 +280,9 @@ void fd_sess_fini(void)
{
TRACE_ENTRY("");
CHECK_FCT_DO( fd_thr_term(&exp_thr), /* continue */ );
-
+
/* Destroy all sessions in the hash table, and the hash table itself? -- How to do it without a race condition ? */
-
+
return;
}
@@ -276,28 +290,28 @@ void fd_sess_fini(void)
int fd_sess_handler_create ( struct session_handler ** handler, void (*cleanup)(struct sess_state *, os0_t, void *), session_state_dump dumper, void * opaque )
{
struct session_handler *new;
-
+
TRACE_ENTRY("%p %p", handler, cleanup);
-
+
CHECK_PARAMS( handler && cleanup );
-
+
CHECK_MALLOC( new = malloc(sizeof(struct session_handler)) );
memset(new, 0, sizeof(struct session_handler));
-
+
CHECK_POSIX( pthread_mutex_lock(&hdl_lock) );
new->id = ++hdl_id;
CHECK_POSIX( pthread_mutex_unlock(&hdl_lock) );
-
+
new->eyec = SH_EYEC;
new->cleanup = cleanup;
new->state_dump = dumper;
new->opaque = opaque;
-
+
*handler = new;
return 0;
}
-/* Destroy a handler, and all states attached to this handler. This operation is very slow but we don't care since it's rarely used.
+/* Destroy a handler, and all states attached to this handler. This operation is very slow but we don't care since it's rarely used.
* Note that it's better to call this function after all sessions have been deleted... */
int fd_sess_handler_destroy ( struct session_handler ** handler, void ** opaque )
{
@@ -305,20 +319,20 @@ int fd_sess_handler_destroy ( struct session_handler ** handler, void ** opaque
/* place to save the list of states to be cleaned up. We do it after finding them to avoid deadlocks. the "o" field becomes a copy of the sid. */
struct fd_list deleted_states = FD_LIST_INITIALIZER( deleted_states );
int i;
-
+
TRACE_ENTRY("%p", handler);
CHECK_PARAMS( handler && VALIDATE_SH(*handler) );
-
+
del = *handler;
*handler = NULL;
-
+
del->eyec = 0xdead; /* The handler is not valid anymore for any other operation */
-
+
/* Now find all sessions with data registered for this handler, and move this data to the deleted_states list. */
for (i = 0; i < sizeof(sess_hash) / sizeof(sess_hash[0]); i++) {
struct fd_list * li_si;
CHECK_POSIX( pthread_mutex_lock(&sess_hash[i].lock) );
-
+
for (li_si = sess_hash[i].sentinel.next; li_si != &sess_hash[i].sentinel; li_si = li_si->next) { /* for each session in the hash line */
struct fd_list * li_st;
struct session * sess = (struct session *)(li_si->o);
@@ -340,7 +354,7 @@ int fd_sess_handler_destroy ( struct session_handler ** handler, void ** opaque
}
CHECK_POSIX( pthread_mutex_unlock(&sess_hash[i].lock) );
}
-
+
/* Now, delete all states after calling their cleanup handler */
while (!FD_IS_LIST_EMPTY(&deleted_states)) {
struct state * st = (struct state *)(deleted_states.next->o);
@@ -349,13 +363,13 @@ int fd_sess_handler_destroy ( struct session_handler ** handler, void ** opaque
fd_list_unlink(&st->chain);
free(st);
}
-
+
if (opaque)
*opaque = del->opaque;
-
+
/* Free the handler */
free(del);
-
+
return 0;
}
@@ -371,20 +385,20 @@ int fd_sess_new ( struct session ** session, DiamId_t diamid, size_t diamidlen,
struct fd_list * li;
int found = 0;
int ret = 0;
-
+
TRACE_ENTRY("%p %p %zd %p %zd", session, diamid, diamidlen, opt, optlen);
CHECK_PARAMS( session && (diamid || opt) );
- if (diamid) {
+ if (diamid) {
if (!diamidlen) {
diamidlen = strlen(diamid);
- }
+ }
/* We check if the string is a valid DiameterIdentity */
CHECK_PARAMS( fd_os_is_valid_DiameterIdentity((uint8_t *)diamid, diamidlen) );
} else {
diamidlen = 0;
}
- if (opt) {
+ if (opt) {
if (!optlen) {
optlen = strlen((char *)opt);
} else {
@@ -393,7 +407,7 @@ int fd_sess_new ( struct session ** session, DiamId_t diamid, size_t diamidlen,
} else {
optlen = 0;
}
-
+
/* Ok, first create the identifier for the string */
if (diamid == NULL) {
/* opt is the full string */
@@ -409,49 +423,49 @@ int fd_sess_new ( struct session ** session, DiamId_t diamid, size_t diamidlen,
sidlen += 1 + optlen; /* ';opt' */
sidlen++; /* space for the final \0 also */
CHECK_MALLOC( sid = malloc(sidlen) );
-
+
CHECK_POSIX( pthread_mutex_lock(&sid_lock) );
if ( ++sid_l == 0 ) /* overflow */
++sid_h;
sid_h_cpy = sid_h;
sid_l_cpy = sid_l;
CHECK_POSIX( pthread_mutex_unlock(&sid_lock) );
-
+
if (opt) {
sidlen = snprintf((char*)sid, sidlen, "%.*s;%u;%u;%.*s", (int)diamidlen, diamid, sid_h_cpy, sid_l_cpy, (int)optlen, opt);
} else {
sidlen = snprintf((char*)sid, sidlen, "%.*s;%u;%u", (int)diamidlen, diamid, sid_h_cpy, sid_l_cpy);
}
}
-
+
hash = fd_os_hash(sid, sidlen);
-
+
/* Now find the place to add this object in the hash table. */
CHECK_POSIX( pthread_mutex_lock( H_LOCK(hash) ) );
pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(hash) );
-
+
for (li = H_LIST(hash)->next; li != H_LIST(hash); li = li->next) {
int cmp;
struct session * s = (struct session *)(li->o);
-
+
/* The list is ordered by hash and sid (in case of collisions) */
if (s->hash < hash)
continue;
if (s->hash > hash)
break;
-
+
cmp = fd_os_cmp(s->sid, s->sidlen, sid, sidlen);
if (cmp < 0)
continue;
if (cmp > 0)
break;
-
+
/* A session with the same sid was already in the hash table */
found = 1;
*session = s;
break;
}
-
+
/* If the session did not exist, we can create it & link it in global tables */
if (!found) {
CHECK_MALLOC_DO(sess = new_session(sid, sidlen, hash),
@@ -460,16 +474,16 @@ int fd_sess_new ( struct session ** session, DiamId_t diamid, size_t diamidlen,
free(sid);
goto out;
} );
-
+
fd_list_insert_before(li, &sess->chain_h); /* hash table */
sess->msg_cnt++;
} else {
free(sid);
-
- CHECK_POSIX( pthread_mutex_lock(&(*session)->stlock) );
+
+ CHECK_POSIX( pthread_mutex_lock(&(*session)->stlock) );
(*session)->msg_cnt++;
- CHECK_POSIX( pthread_mutex_unlock(&(*session)->stlock) );
-
+ CHECK_POSIX( pthread_mutex_unlock(&(*session)->stlock) );
+
/* it was found: was it previously destroyed? */
if ((*session)->is_destroyed == 0) {
ret = EALREADY;
@@ -478,13 +492,13 @@ int fd_sess_new ( struct session ** session, DiamId_t diamid, size_t diamidlen,
/* the session was marked destroyed, let's re-activate it. */
sess = *session;
sess->is_destroyed = 0;
-
+
/* update the expiry time */
CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &sess->timeout), { ASSERT(0); } );
sess->timeout.tv_sec += SESS_DEFAULT_LIFETIME;
}
}
-
+
/* We must insert in the expiry list */
CHECK_POSIX( pthread_mutex_lock( &exp_lock ) );
pthread_cleanup_push( fd_cleanup_mutex, &exp_lock );
@@ -511,10 +525,10 @@ out:
;
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock( H_LOCK(hash) ) );
-
+
if (ret) /* in case of error */
return ret;
-
+
*session = sess;
return 0;
}
@@ -523,28 +537,28 @@ out:
int fd_sess_fromsid_msg ( uint8_t * sid, size_t len, struct session ** session, int * new)
{
int ret;
-
+
TRACE_ENTRY("%p %zd %p %p", sid, len, session, new);
CHECK_PARAMS( sid && session );
-
+
if (!fd_os_is_valid_os0(sid,len)) {
TRACE_DEBUG(INFO, "Warning: a Session-Id value contains \\0 chars... (len:%zd, begin:'%.*s') => Debug messages may be truncated.", len, (int)len, sid);
}
-
+
/* All the work is done in sess_new */
ret = fd_sess_new ( session, NULL, 0, sid, len );
switch (ret) {
case 0:
case EALREADY:
break;
-
+
default:
CHECK_FCT(ret);
}
-
+
if (new)
*new = ret ? 0 : 1;
-
+
return 0;
}
@@ -552,13 +566,13 @@ int fd_sess_fromsid_msg ( uint8_t * sid, size_t len, struct session ** session,
int fd_sess_getsid ( struct session * session, os0_t * sid, size_t * sidlen )
{
TRACE_ENTRY("%p %p", session, sid);
-
+
CHECK_PARAMS( VALIDATE_SI(session) && sid );
-
+
*sid = session->sid;
if (sidlen)
*sidlen = session->sidlen;
-
+
return 0;
}
@@ -566,18 +580,18 @@ int fd_sess_getsid ( struct session * session, os0_t * sid, size_t * sidlen )
int fd_sess_settimeout( struct session * session, const struct timespec * timeout )
{
struct fd_list * li;
-
+
TRACE_ENTRY("%p %p", session, timeout);
CHECK_PARAMS( VALIDATE_SI(session) && timeout );
-
+
/* Lock -- do we need to lock the hash table as well? I don't think so... */
CHECK_POSIX( pthread_mutex_lock( &exp_lock ) );
pthread_cleanup_push( fd_cleanup_mutex, &exp_lock );
-
+
/* Update the timeout */
fd_list_unlink(&session->expire);
memcpy(&session->timeout, timeout, sizeof(struct timespec));
-
+
/* Find the new position in expire list. We take it in normal order */
for (li = exp_sentinel.next; li != &exp_sentinel; li = li->next) {
struct session * s = (struct session *)(li->o);
@@ -597,7 +611,7 @@ int fd_sess_settimeout( struct session * session, const struct timespec * timeou
/* We're done */
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock( &exp_lock ) );
-
+
return 0;
}
@@ -608,28 +622,30 @@ int fd_sess_destroy ( struct session ** session )
int destroy_now;
os0_t sid;
int ret = 0;
-
+
/* place to save the list of states to be cleaned up. We do it after finding them to avoid deadlocks. the "o" field becomes a copy of the sid. */
struct fd_list deleted_states = FD_LIST_INITIALIZER( deleted_states );
-
+
TRACE_ENTRY("%p", session);
CHECK_PARAMS( session && VALIDATE_SI(*session) );
-
+
sess = *session;
*session = NULL;
-
+
/* Lock the hash line */
CHECK_POSIX( pthread_mutex_lock( H_LOCK(sess->hash) ) );
pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(sess->hash) );
-
+
/* Unlink from the expiry list */
CHECK_POSIX_DO( pthread_mutex_lock( &exp_lock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
+ pthread_cleanup_push( fd_cleanup_mutex, &exp_lock );
if (!FD_IS_LIST_EMPTY(&sess->expire)) {
sess_cnt--;
fd_list_unlink( &sess->expire ); /* no need to signal the condition here */
}
+ pthread_cleanup_pop(0);
CHECK_POSIX_DO( pthread_mutex_unlock( &exp_lock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
-
+
/* Now move all states associated to this session into deleted_states */
CHECK_POSIX_DO( pthread_mutex_lock( &sess->stlock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
while (!FD_IS_LIST_EMPTY(&sess->states)) {
@@ -638,7 +654,7 @@ int fd_sess_destroy ( struct session ** session )
fd_list_insert_before(&deleted_states, &st->chain);
}
CHECK_POSIX_DO( pthread_mutex_unlock( &sess->stlock ), { ASSERT(0); /* otherwise cleanup handler is not pop'd */ } );
-
+
/* Mark the session as destroyed */
destroy_now = (sess->msg_cnt == 0);
if (destroy_now) {
@@ -650,10 +666,10 @@ int fd_sess_destroy ( struct session ** session )
}
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock( H_LOCK(sess->hash) ) );
-
+
if (ret)
return ret;
-
+
/* Now, really delete the states */
while (!FD_IS_LIST_EMPTY(&deleted_states)) {
struct state * st = (struct state *)(deleted_states.next->o);
@@ -662,14 +678,14 @@ int fd_sess_destroy ( struct session ** session )
(*st->hdl->cleanup)(st->state, sid, st->hdl->opaque);
free(st);
}
-
+
/* Finally, destroy the session itself, if it is not referrenced by any message anymore */
if (destroy_now) {
del_session(sess);
} else {
free(sid);
}
-
+
return 0;
}
@@ -679,20 +695,21 @@ int fd_sess_reclaim ( struct session ** session )
struct session * sess;
uint32_t hash;
int destroy_now = 0;
-
+
TRACE_ENTRY("%p", session);
CHECK_PARAMS( session && VALIDATE_SI(*session) );
-
+
sess = *session;
hash = sess->hash;
*session = NULL;
-
+
CHECK_POSIX( pthread_mutex_lock( H_LOCK(hash) ) );
pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(hash) );
CHECK_POSIX_DO( pthread_mutex_lock( &sess->stlock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
pthread_cleanup_push( fd_cleanup_mutex, &sess->stlock );
CHECK_POSIX_DO( pthread_mutex_lock( &exp_lock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
-
+ pthread_cleanup_push( fd_cleanup_mutex, &exp_lock );
+
/* We only do something if the states list is empty */
if (FD_IS_LIST_EMPTY(&sess->states)) {
/* In this case, we do as in destroy */
@@ -705,16 +722,17 @@ int fd_sess_reclaim ( struct session ** session )
sess->is_destroyed = 1;
}
}
-
+
+ pthread_cleanup_pop(0);
CHECK_POSIX_DO( pthread_mutex_unlock( &exp_lock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
pthread_cleanup_pop(0);
CHECK_POSIX_DO( pthread_mutex_unlock( &sess->stlock ), { ASSERT(0); /* otherwise, cleanup not poped on FreeBSD */ } );
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock( H_LOCK(hash) ) );
-
+
if (destroy_now)
del_session(sess);
-
+
return 0;
}
@@ -725,38 +743,38 @@ int fd_sess_state_store ( struct session_handler * handler, struct session * ses
struct fd_list * li;
int already = 0;
int ret = 0;
-
+
TRACE_ENTRY("%p %p %p", handler, session, state);
CHECK_PARAMS( handler && VALIDATE_SH(handler) && session && VALIDATE_SI(session) && (!session->is_destroyed) && state );
-
+
/* Lock the session state list */
CHECK_POSIX( pthread_mutex_lock(&session->stlock) );
pthread_cleanup_push( fd_cleanup_mutex, &session->stlock );
-
+
/* Create the new state object */
CHECK_MALLOC_DO(new = malloc(sizeof(struct state)), { ret = ENOMEM; goto out; } );
memset(new, 0, sizeof(struct state));
-
+
new->eyec = SD_EYEC;
new->state= *state;
fd_list_init(&new->chain, new);
new->hdl = handler;
-
+
/* find place for this state in the list */
for (li = session->states.next; li != &session->states; li = li->next) {
struct state * st = (struct state *)(li->o);
/* The list is ordered by handler's id */
if (st->hdl->id < handler->id)
continue;
-
+
if (st->hdl->id == handler->id) {
TRACE_DEBUG(INFO, "A state was already stored for session '%s' and handler '%p', at location %p", session->sid, st->hdl, st->state);
already = EALREADY;
}
-
+
break;
}
-
+
if (!already) {
fd_list_insert_before(li, &new->chain);
*state = NULL;
@@ -764,10 +782,10 @@ int fd_sess_state_store ( struct session_handler * handler, struct session * ses
free(new);
}
out:
- ;
+ ;
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock(&session->stlock) );
-
+
return ret ?: already;
}
@@ -776,35 +794,35 @@ int fd_sess_state_retrieve ( struct session_handler * handler, struct session *
{
struct fd_list * li;
struct state * st = NULL;
-
+
TRACE_ENTRY("%p %p %p", handler, session, state);
CHECK_PARAMS( handler && VALIDATE_SH(handler) && session && VALIDATE_SI(session) && state );
-
+
*state = NULL;
-
+
/* Lock the session state list */
CHECK_POSIX( pthread_mutex_lock(&session->stlock) );
pthread_cleanup_push( fd_cleanup_mutex, &session->stlock );
-
+
/* find the state in the list */
for (li = session->states.next; li != &session->states; li = li->next) {
st = (struct state *)(li->o);
-
+
/* The list is ordered by handler's id */
if (st->hdl->id > handler->id)
break;
}
-
+
/* If we found the state */
if (st && (st->hdl == handler)) {
fd_list_unlink(&st->chain);
*state = st->state;
free(st);
}
-
+
pthread_cleanup_pop(0);
CHECK_POSIX( pthread_mutex_unlock(&session->stlock) );
-
+
return 0;
}
@@ -813,15 +831,15 @@ int fd_sess_fromsid ( uint8_t * sid, size_t len, struct session ** session, int
{
TRACE_ENTRY("%p %zd %p %p", sid, len, session, new);
CHECK_PARAMS( sid && len && session );
-
+
/* Get the session object */
CHECK_FCT( fd_sess_fromsid_msg ( sid, len, session, new) );
-
+
/* Decrease the refcount */
CHECK_POSIX( pthread_mutex_lock(&(*session)->stlock) );
(*session)->msg_cnt--; /* was increased in fd_sess_new */
CHECK_POSIX( pthread_mutex_unlock(&(*session)->stlock) );
-
+
/* Done */
return 0;
}
@@ -835,7 +853,7 @@ int fd_sess_ref_msg ( struct session * session )
CHECK_POSIX( pthread_mutex_lock(&session->stlock) );
session->msg_cnt++;
CHECK_POSIX( pthread_mutex_unlock(&session->stlock) );
-
+
return 0;
}
@@ -843,25 +861,25 @@ int fd_sess_reclaim_msg ( struct session ** session )
{
int reclaim;
uint32_t hash;
-
+
TRACE_ENTRY("%p", session);
CHECK_PARAMS( session && VALIDATE_SI(*session) );
-
+
/* Lock the hash line to avoid possibility that session is freed while we are reclaiming */
hash = (*session)->hash;
CHECK_POSIX( pthread_mutex_lock( H_LOCK(hash)) );
- pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(hash) );
+ pthread_cleanup_push( fd_cleanup_mutex, H_LOCK(hash) );
/* Update the msg refcount */
CHECK_POSIX( pthread_mutex_lock(&(*session)->stlock) );
reclaim = (*session)->msg_cnt;
(*session)->msg_cnt = reclaim - 1;
CHECK_POSIX( pthread_mutex_unlock(&(*session)->stlock) );
-
+
/* Ok, now unlock the hash line */
pthread_cleanup_pop( 0 );
CHECK_POSIX( pthread_mutex_unlock( H_LOCK(hash) ) );
-
+
/* and reclaim if no message references the session anymore */
if (reclaim == 1) {
CHECK_FCT(fd_sess_reclaim ( session ));
@@ -877,9 +895,9 @@ int fd_sess_reclaim_msg ( struct session ** session )
DECLARE_FD_DUMP_PROTOTYPE(fd_sess_dump, struct session * session, int with_states)
{
FD_DUMP_HANDLE_OFFSET();
-
+
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "{session}(@%p): ", session), return NULL);
-
+
if (!VALIDATE_SI(session)) {
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "INVALID/NULL"), return NULL);
} else {
@@ -889,46 +907,46 @@ DECLARE_FD_DUMP_PROTOTYPE(fd_sess_dump, struct session * session, int with_state
strftime(timebuf, sizeof(timebuf), "%D,%T", localtime_r( &session->timeout.tv_sec , &tm ));
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "'%s'(%zd) h:%x m:%d d:%d to:%s.%06ld",
session->sid, session->sidlen, session->hash, session->msg_cnt, session->is_destroyed,
- timebuf, session->timeout.tv_nsec/1000),
+ timebuf, session->timeout.tv_nsec/1000),
return NULL);
-
+
if (with_states) {
struct fd_list * li;
CHECK_POSIX_DO( pthread_mutex_lock(&session->stlock), /* ignore */ );
pthread_cleanup_push( fd_cleanup_mutex, &session->stlock );
-
+
for (li = session->states.next; li != &session->states; li = li->next) {
struct state * st = (struct state *)(li->o);
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "\n {state i:%d}(@%p): ", st->hdl->id, st), return NULL);
if (st->hdl->state_dump) {
- CHECK_MALLOC_DO( (*st->hdl->state_dump)( FD_DUMP_STD_PARAMS, st->state),
+ CHECK_MALLOC_DO( (*st->hdl->state_dump)( FD_DUMP_STD_PARAMS, st->state),
fd_dump_extend( FD_DUMP_STD_PARAMS, "[dumper error]"));
} else {
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "<%p>", st->state), return NULL);
}
}
-
+
pthread_cleanup_pop(0);
CHECK_POSIX_DO( pthread_mutex_unlock(&session->stlock), /* ignore */ );
}
}
-
+
return *buf;
}
DECLARE_FD_DUMP_PROTOTYPE(fd_sess_dump_hdl, struct session_handler * handler)
{
FD_DUMP_HANDLE_OFFSET();
-
+
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "{sesshdl}(@%p): ", handler), return NULL);
-
+
if (!VALIDATE_SH(handler)) {
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "INVALID/NULL"), return NULL);
} else {
CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "i:%d cl:%p d:%p o:%p", handler->id, handler->cleanup, handler->state_dump, handler->opaque), return NULL);
}
return *buf;
-}
+}
int fd_sess_getcount(uint32_t *cnt)
{