X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-session.c;h=c63c6c464c740d644206cfaa9f7d8b8e0dac4b26;hb=afff776f7e27c8be7610cfbd23c4d1be32699ed9;hp=17ca7af81be03a2058e114054de58ad8952bbea4;hpb=5fccb2dcb97fac7fbc16d66e947477e41d30a2b4;p=src%2Fapp-framework-binder.git diff --git a/src/afb-session.c b/src/afb-session.c index 17ca7af8..c63c6c46 100644 --- a/src/afb-session.c +++ b/src/afb-session.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ #define _MAXEXP_ ((time_t)(~(time_t)0)) #define _MAXEXP2_ ((time_t)((((unsigned long long)_MAXEXP_) >> 1))) #define MAX_EXPIRATION (_MAXEXP_ >= 0 ? _MAXEXP_ : _MAXEXP2_) -#define NOW (time(NULL)) +#define NOW (time_now()) /* structure for a cookie added to sessions */ struct cookie @@ -86,6 +87,14 @@ static struct { .mutex = PTHREAD_MUTEX_INITIALIZER }; +/* Get the actual raw time */ +static inline time_t time_now() +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + return ts.tv_sec; +} + /* generate a new fresh 'uuid' */ static void new_uuid(char uuid[SIZEUUID]) { @@ -219,21 +228,12 @@ static void session_destroy (struct afb_session *session) /* update expiration of 'session' according to 'now' */ static void session_update_expiration(struct afb_session *session, time_t now) { - int timeout; time_t expiration; /* compute expiration */ - timeout = session->timeout; - if (timeout == AFB_SESSION_TIMEOUT_INFINITE) + expiration = now + afb_session_timeout(session); + if (expiration < 0) expiration = MAX_EXPIRATION; - else { - if (timeout == AFB_SESSION_TIMEOUT_DEFAULT) - expiration = now + sessions.timeout; - else - expiration = now + timeout; - if (expiration < 0) - expiration = MAX_EXPIRATION; - } /* record the expiration */ session->expiration = expiration; @@ -408,6 +408,30 @@ struct afb_session *afb_session_create (int timeout) return afb_session_get(NULL, timeout, NULL); } +/** + * Returns the timeout of 'session' in seconds + */ +int afb_session_timeout(struct afb_session *session) +{ + int timeout; + + /* compute timeout */ + timeout = session->timeout; + if (timeout == AFB_SESSION_TIMEOUT_DEFAULT) + timeout = sessions.timeout; + if (timeout < 0) + timeout = INT_MAX; + return timeout; +} + +/** + * Returns the second remaining before expiration of 'session' + */ +int afb_session_what_remains(struct afb_session *session) +{ + return (int)(session->expiration - NOW); +} + /* This function will return exiting session or newly created session */ struct afb_session *afb_session_get (const char *uuid, int timeout, int *created) {