From b77fd803be77217292ba941f59fa40225b72f3e1 Mon Sep 17 00:00:00 2001 From: Radoslaw Bartosiak Date: Wed, 3 Sep 2014 21:13:03 +0200 Subject: [PATCH] Implement cynara-session library Change-Id: I929408dc59db5f4ab115567fd4839258bd086418 --- src/helpers/session/session.cpp | 22 +++++++++++++- src/include/cynara-session.h | 64 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/helpers/session/session.cpp b/src/helpers/session/session.cpp index f65f23b..8c94868 100644 --- a/src/helpers/session/session.cpp +++ b/src/helpers/session/session.cpp @@ -15,9 +15,29 @@ */ /* * @file session.cpp + * @author Radoslaw Bartosiak + * @author Aleksander Zdyb * @author Lukasz Wojciechowski * @version 1.0 * @brief Implementation of external libcynara-session API */ -// Empty initial file +#include +#include +#include + +#include + +#include + +CYNARA_API +char *cynara_session_from_pid(pid_t client_pid) { + std::string path = std::string("/proc/") + std::to_string(client_pid); + + struct stat st; + if (stat(path.c_str(), &st) < 0) + return nullptr; + + std::string session = std::to_string(st.st_ctim.tv_sec) + path; + return strdup(session.c_str()); +} diff --git a/src/include/cynara-session.h b/src/include/cynara-session.h index ab96e0b..d24c57d 100644 --- a/src/include/cynara-session.h +++ b/src/include/cynara-session.h @@ -15,6 +15,8 @@ */ /* * @file cynara-session.h + * \author Aleksander Zdyb + * \author Radoslaw Bartosiak * @author Lukasz Wojciechowski * @version 1.0 * @brief This file contains Cynara session helper APIs. @@ -24,11 +26,71 @@ #ifndef CYNARA_SESSION_H #define CYNARA_SESSION_H +#include + #ifdef __cplusplus extern "C" { #endif -/* empty initial file */ +/** + * \par Description: + * Creates a client session string based on pid and time of creation of client process + * + * \par Purpose: + * This function can be used to create session string identifier used in cynara_check() + * and cynara_async_check() functions defined in client libraries. + * + * \par Typical use case: + * The function is called before the call of one of ...check() functions. + * Returned string is used as client_session param in ...check() function. + * String is released with free() function. + * + * \par Method of function operation: + * The function generates client session based on the pid and start time of the client process. + * Time is acquired from /proc/PID directory. + * + * \par Sync (or) Async: + * This is a synchronous API. + * + * \par Thread safety: + * This function is thread-safe. + * + * \par Important notes: + * Memory for returned string is obtained with malloc(), and should be freed with free(). + * + * \param[in] client_pid client application process identifier (PID). + * + * \return session string on success + * or NULL on error. + */ +char *cynara_session_from_pid(pid_t client_pid); + +/* //sample code + * + * (...) + * + * //create client session + * char *client_session; + * client_session = cynara_session_from_pid(client_pid); + * if (!client_session) { + * //use another way to create session or abandon request sending + * } + * + * //check access (details of this function can be found in cynara-client.h) + * int ret = cynara_check(p_cynara, client, client_session, user, privilege); + * + * //release client_session memory + * free(client_session); + * + * //handle check answer + * if (ret < 0) { + * //handle error + * } else { + * //handle response + * } + * + * (...) + */ #ifdef __cplusplus } -- 2.7.4