Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig
Requires: boost-system
-Requires: boost-log
-Requires: boost-thread
%{?systemd_requires}
%global user_name security_fw
dcm_client.cpp
device_certificate_manager.cpp
../shared/protobuf_asio.cpp
+ ../shared/log.cpp
${PROTO_SRCS}
${PROTO_HDRS})
* limitations under the License.
*
******************************************************************/
-#define LOG_TAG "DCM_CLIENT"
#include <cassert>
#include <inttypes.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/coded_stream.h>
#include <mbedtls/ssl.h>
-#include <dlog.h>
#include "dcm_client.h"
#include "dcm_support.pb.h"
#include "device_certificate_manager.h"
#include "protobuf_asio.h"
+#include "log.h"
static_assert(MD_NONE == (unsigned int)MBEDTLS_MD_NONE, "MBEDTLS_MD_NONE mismatch");
static_assert(MD_MD2 == (unsigned int)MBEDTLS_MD_MD2, "MBEDTLS_MD_MD2 mismatch");
std::lock_guard<std::mutex> locker(fLock);
if(fCookie) {
- LOGE("%s: Cookie has already been requested for session %p", __FUNCTION__, this);
+ LOGE("Cookie has already been requested for session %p", this);
// Already created
return DCM_ERROR_SOCKET;
}
boost::asio::local::stream_protocol::endpoint endpoint(DCM_UNIX_SOCKET_PATH);
fSocket->connect(endpoint);
} catch(std::exception& ex) {
- LOGE("%s: Caught exception \"%s\" when connecting socket for session %p", __FUNCTION__, ex.what(), this);
+ LOGE("Caught exception \"%s\" when connecting socket for session %p", ex.what(), this);
return DCM_ERROR_SOCKET;
} catch(...) {
- LOGE("%s: Caught unknown exception when connecting socket for session %p", __FUNCTION__, this);
+ LOGE("Caught unknown exception when connecting socket for session %p", this);
return DCM_ERROR_SOCKET;
}
}
send_receive(request, response);
if(!response.has_associate_context()) {
- LOGE("%s: received response is not context association message in context %p", __FUNCTION__, this);
+ LOGE("Received response is not context association message in context %p", this);
return DCM_ERROR_NO_DATA;
}
auto& assoc_message(response.associate_context());
if(assoc_message.result() != 0) {
- LOGE("%s: Received context association message with error %d in %p", __FUNCTION__, assoc_message.result(), this);
+ LOGE("Received context association message with error %d in %p", assoc_message.result(), this);
return DCM_ERROR_NO_DATA;
}
fKeyType = assoc_message.key_type();
fKeyLength = assoc_message.key_length();
} catch(std::exception& ex) {
- LOGE("%s: Caught exception \"%s\" when establishing cookie for session %p", __FUNCTION__, ex.what(), this);
+ LOGE("Caught exception \"%s\" when establishing cookie for session %p", ex.what(), this);
fSocket.reset();
return DCM_ERROR_SOCKET;
} catch(...) {
- LOGE("%s: Caught unknown exception when establishing cookie for session %p", __FUNCTION__, this);
+ LOGE("Caught unknown exception when establishing cookie for session %p", this);
fSocket.reset();
return DCM_ERROR_SOCKET;
}
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
- LOGE("%s: Trying to request certificate in session %p without connection", __FUNCTION__, this);
+ LOGE("Trying to request certificate in session %p without connection", this);
return DCM_ERROR_SOCKET;
}
send_receive(request, response);
if(!response.has_request_chain()) {
- LOGE("%s: Response from server is not certificate chain response on session %p", __FUNCTION__, this);
+ LOGE("Response from server is not certificate chain response on session %p", this);
return DCM_ERROR_NO_DATA;
}
auto& cert_resp(response.request_chain());
if(cert_resp.result() != 0) {
- LOGE("%s: Server can't respond with certificate chain: error %d in session %p", __FUNCTION__, cert_resp.result(), this);
+ LOGE("Server can't respond with certificate chain: error %d in session %p", cert_resp.result(), this);
return DCM_ERROR_NO_DATA;
}
if(cert_resp.cert_chain().size() == 0) {
- LOGE("%s: Server can't respond with certificate chain: certificate empty", __FUNCTION__);
+ LOGE("Server can't respond with certificate chain: certificate empty");
return DCM_ERROR_NO_DATA;
}
chain.push_back(0);
}
} catch(std::bad_alloc&) {
- LOGE("%s: Out of memory when requesting certificate for session %p", __FUNCTION__, this);
+ LOGE("Out of memory when requesting certificate for session %p", this);
return DCM_ERROR_OUT_OF_MEMORY;
} catch(std::invalid_argument&) {
- LOGE("%s: Invalid argument passed for certificate request for session %p", __FUNCTION__, this);
+ LOGE("Invalid argument passed for certificate request for session %p", this);
return DCM_ERROR_INVALID_PARAMETER;
} catch(std::exception& ex) {
- LOGE("%s: When requesting certificate for session %p received exception : %s", __FUNCTION__, this, ex.what());
+ LOGE("When requesting certificate for session %p received exception : %s", this, ex.what());
return DCM_ERROR_UNKNOWN;
} catch(...) {
- LOGE("%s: When requesting certificate for session %p received exception : %s", __FUNCTION__, this, "Unknown error");
+ LOGE("When requesting certificate for session %p received exception : %s", this, "Unknown error");
return DCM_ERROR_UNKNOWN;
}
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
- LOGE("%s: Trying to request data signing in object %p but there is no connection", __FUNCTION__, this);
+ LOGE("Trying to request data signing in object %p but there is no connection", this);
return DCM_ERROR_SOCKET;
}
// If hash_size == 0 then hash type must be known
if(hash_size == 0) {
if(md == MBEDTLS_MD_NONE) {
- LOGE("%s: Digest type is NONE and hash size is 0", __FUNCTION__);
+ LOGE("Digest type is NONE and hash size is 0");
return DCM_ERROR_INVALID_PARAMETER;
}
const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(md);
if(!md_info) {
- LOGE("%s: Can't find hash data for digest type %d", __FUNCTION__, md);
+ LOGE("Can't find hash data for digest type %d", md);
return DCM_ERROR_INVALID_PARAMETER;
}
// If hash_size != 0 then hash type can be specified
const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(md);
if(!md_info) {
- LOGE("%s: Can't find hash data for digest type %d", __FUNCTION__, md);
+ LOGE("Can't find hash data for digest type %d", md);
return DCM_ERROR_INVALID_PARAMETER;
}
if(hash_size != mbedtls_md_get_size(md_info)) {
- LOGE("%s: Hash size mismatch. Expected %zd but got %zd", __FUNCTION__, hash_size, (size_t)mbedtls_md_get_size(md_info));
+ LOGE("Hash size mismatch. Expected %zd but got %zd", hash_size, (size_t)mbedtls_md_get_size(md_info));
return DCM_ERROR_INVALID_PARAMETER;
}
}
send_receive(request, response);
if(!response.has_sign_data()) {
- LOGE("%s: Response for hash signature has no signature data", __FUNCTION__);
+ LOGE("Response for hash signature has no signature data");
return DCM_ERROR_NO_DATA;
}
auto& sign_resp(response.sign_data());
if(sign_resp.result() != 0) {
- LOGE("%s: Signature request for session %p received error %d", __FUNCTION__, this, sign_resp.result());
+ LOGE("Signature request for session %p received error %d", this, sign_resp.result());
return DCM_ERROR_NO_DATA;
}
const auto& signature = sign_resp.signature();
if(signature.empty()) {
- LOGE("%s: Received signature object is empty for session %p", __FUNCTION__, this);
+ LOGE("Received signature object is empty for session %p", this);
return DCM_ERROR_NO_DATA;
}
digest.resize(signature.size());
memcpy(&digest[0], signature.c_str(), signature.size());
} catch(std::bad_alloc&) {
- LOGE("%s: Out of memory when processing sign request for session %p", __FUNCTION__, this);
+ LOGE("Out of memory when processing sign request for session %p", this);
return DCM_ERROR_OUT_OF_MEMORY;
} catch(...) {
- LOGE("%s: When processing signature for session %p got exception : %s", __FUNCTION__, this, "Unknown error");
+ LOGE("When processing signature for session %p got exception : %s", this, "Unknown error");
return DCM_ERROR_UNKNOWN;
}
* limitations under the License.
*
******************************************************************/
-#define LOG_TAG "DCM_CLIENT"
#include <vector>
#include <cstring>
#include <mbedtls/pk_internal.h>
#include <mbedtls/md.h>
-#include <dlog.h>
#include "device_certificate_manager.h"
#include "dcm_client.h"
+#include "log.h"
#ifndef API_DEVICE_CERTIFICATE_MANAGER_EXPORT
#define API_DEVICE_CERTIFICATE_MANAGER_EXPORT __attribute__((visibility("default")))
FIND_PACKAGE(Boost REQUIRED
COMPONENTS
- log
- thread
system)
PKG_CHECK_MODULES(DAEMON_DEPS
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ../dcm-client/dcm_support.proto)
-ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
-
SET(TARGET_DAEMON "device-certificate-managerd")
ADD_EXECUTABLE(${TARGET_DAEMON}
main.cpp
dcmsession.cpp
serviceadapter.cpp
../shared/protobuf_asio.cpp
+ ../shared/log.cpp
soresolver.cpp
${PROTO_SRCS}
${PROTO_HDRS})
TARGET_LINK_LIBRARIES(${TARGET_DAEMON}
- ${Boost_LOG_LIBRARY}
- ${Boost_THREAD_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${PROTOBUF_LITE_LIBRARIES}
${DAEMON_DEPS_LIBRARIES}
+++ /dev/null
-/******************************************************************
- *
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
- *
- * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************/
-
-#ifndef SHARED_BOOST_LOG_DLOG_SINK_H_
-#define SHARED_BOOST_LOG_DLOG_SINK_H_
-
-#include <boost/log/sinks.hpp>
-#include <functional>
-
-#include <dlog.h>
-
-template<typename AttributeValueT = int> class dlog_direct_severity_mapping :
- public boost::log::sinks::basic_direct_mapping<log_priority, AttributeValueT>
-{
- typedef boost::log::sinks::basic_direct_mapping<log_priority, AttributeValueT> base_type;
-public:
- explicit dlog_direct_severity_mapping(boost::log::attribute_name const& name) :
- base_type(name)
- {
- }
-};
-
-template<typename AttributeValueT = int> class dlog_custom_severity_mapping :
- public boost::log::sinks::basic_custom_mapping<log_priority, AttributeValueT>
-{
- typedef boost::log::sinks::basic_custom_mapping<log_priority, AttributeValueT> base_type;
-public:
- explicit dlog_custom_severity_mapping(boost::log::attribute_name const& name) :
- base_type(name, DLOG_DEBUG)
- {
- }
-};
-
-class dlog_output_backend :
- public boost::log::sinks::basic_formatted_sink_backend<char>
-{
- typedef boost::log::sinks::basic_formatted_sink_backend<char> base_type;
- typedef std::function< log_priority (boost::log::record_view const&) > severity_mapper_type;
-
-private:
- std::string log_domain_;
- severity_mapper_type level_mapper_;
-
- inline void send(log_priority level, string_type const& formatted_message) {
- dlog_print(level, log_domain_.c_str(), "%s", formatted_message.c_str());
- }
-
-public:
- dlog_output_backend()
- {
- }
-
- void consume(boost::log::record_view const& rec, string_type const& formatted_message) {
- if(!level_mapper_) {
- send(DLOG_DEBUG, formatted_message);
- } else {
- send(level_mapper_(rec), formatted_message);
- }
- }
-
- void set_log_domain(const std::string& name)
- {
- log_domain_ = name;
- }
-
- void set_severity_mapper(severity_mapper_type const& mapper)
- {
- level_mapper_ = mapper;
- }
-};
-
-#endif /* SHARED_BOOST_LOG_DLOG_SINK_H_ */
/******************************************************************
*
- * Copyright 2017 - 2019 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
*
* Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
*
#include "dcmserver.h"
#include "dcmsession.h"
-#include "logging.h"
+#include "log.h"
dcm_server::dcm_server(boost::asio::io_service& io_service, boost::asio::local::stream_protocol::acceptor&& acceptor, std::string lib_backend) :
fService(io_service),
fAcceptor(std::move(acceptor)),
fSoResolver(std::make_shared<so_resolver>(lib_backend))
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Construct server object";
+ LOGD("Construct server object");
}
dcm_server::~dcm_server()
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Destroy server object";
+ LOGD("Destroy server object");
}
void dcm_server::start()
void dcm_server::do_accept()
{
- BOOST_LOG_FUNCTION();
-
auto self(this->shared_from_this());
std::shared_ptr<dcm_session> session;
try {
session = std::make_shared<dcm_session>(fService, fTimer, shared_from_this(), fSoResolver);
} catch(std::bad_alloc& ex) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Out of memory when trying to allocate new session";
+ LOGE("Out of memory when trying to allocate new session");
return;
} catch(std::exception& ex) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Can't create new session object: " << ex.what();
+ LOGD("Can't create new session object: " << ex.what());
return;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Start accepting connections";
+ LOGD("Start accepting connections");
session->start_timer();
fAcceptor.async_accept(session->socket(),
[session, self](boost::system::error_code error_code)
{
- BOOST_LOG_FUNCTION();
if(!error_code) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Accepted session";
+ LOGD("Accepted session");
session->start();
} else {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't accept new session " << error_code;
+ LOGE("Can't accept new session " << error_code);
}
self->do_accept();
});
*
******************************************************************/
-#include "dcmsession.h"
-#include "logging.h"
-#include "exception_translator.h"
-#include "dcmserver.h"
-
#include <iostream>
#include <cassert>
#include <map>
#include <cynara-creds-socket.h>
#include <cynara-session.h>
+#include "dcmsession.h"
+#include "log.h"
+#include "exception_translator.h"
+#include "dcmserver.h"
+
extern cynara * gGlobalCynaraInstance;
static inline std::string cynara_error_to_string(int error) {
fServer(server),
fSoResolver(soResolver)
{
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Create new session object " << this;
+ LOGD("Create new session object " << this);
globalSessionCounter++;
}
dcm_session::~dcm_session()
{
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Destroy session object " << this;
+ LOGD("Destroy session object " << this);
}
struct string_free_deleter {
bool dcm_session::verify_privileges(int handle)
{
- BOOST_LOG_FUNCTION();
-
int ret = 0;
char * tmp_str;
pid_t pid = 0;
tmp_str = nullptr;
ret = cynara_creds_socket_get_user(handle, USER_METHOD_DEFAULT, &tmp_str);
if(ret != CYNARA_API_SUCCESS) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't get user from socket : " << ret << " - " << cynara_error_to_string(ret);
+ LOGE("Can't get user from socket : " << ret << " - " << cynara_error_to_string(ret));
return false;
}
user.reset(tmp_str);
tmp_str = nullptr;
ret = cynara_creds_socket_get_client(handle, CLIENT_METHOD_DEFAULT, &tmp_str);
if(ret != CYNARA_API_SUCCESS) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't get client from socket : " << ret << " - " << cynara_error_to_string(ret);
+ LOGE("Can't get client from socket : " << ret << " - " << cynara_error_to_string(ret));
return false;
}
client.reset(tmp_str);
/* Get client PID from socket */
ret = cynara_creds_socket_get_pid(handle, &pid);
if(ret != CYNARA_API_SUCCESS) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't get PID from socket : " << ret << " - " << cynara_error_to_string(ret);
+ LOGE("Can't get PID from socket : " << ret << " - " << cynara_error_to_string(ret));
return false;
}
client_session.reset(cynara_session_from_pid(pid));
if(!client_session) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't get session identifier from PID";
+ LOGE("Can't get session identifier from PID");
return false;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Got new session from " << pid << " with user " <<
- user.get() << ", client ID " << client.get() << " and session ID " << client_session.get();
+ LOGD("Got new session from " << pid << " with user " << user.get() <<
+ ", client ID " << client.get() << " and session ID " << client_session.get());
ret = cynara_check(gGlobalCynaraInstance,
client.get(),
"http://tizen.org/privilege/devicecertificate");
if(ret != CYNARA_API_ACCESS_ALLOWED) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) <<
- "Application access denied - no internet permission for " <<
- pid <<
- " - " <<
- cynara_error_to_string(ret);
-
+ LOGE("Application access denied for " << pid << " - " << cynara_error_to_string(ret));
return false;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Access granted for " << pid;
+ LOGD("Access granted for " << pid);
return true;
}
void dcm_session::start()
{
- BOOST_LOG_FUNCTION();
-
int handle = fSocket.native_handle();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Accepted connection with socket " << fSocket.native_handle();
+ LOGD("Accepted connection with socket " << fSocket.native_handle());
if(verify_privileges(handle)) {
stop_timer();
do_receive();
} else {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Client privilege check failure. Disconnect";
+ LOGE("Client privilege check failure. Disconnect");
start_timer();
}
}
void dcm_session::start_timer()
{
globalSessionCounter--;
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Number of active connections: " << globalSessionCounter;
+ LOGD("Number of active connections: " << globalSessionCounter);
if(globalSessionCounter == 0) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "No active connections, server will be closed after few seconds";
+ LOGD("No active connections, server will be closed after few seconds");
fTimer.expires_from_now(boost::posix_time::seconds(10));
/* operation_aborted error is returned whenever we cancel the timer or we reset the timer using expires_from_now function */
fTimer.async_wait([this](const boost::system::error_code &error) {
if(error != boost::asio::error::operation_aborted) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::normal) << "No active connections, server will be closed";
+ LOGD("No active connections, server will be closed");
fService.stop();
}
});
void dcm_session::stop_timer()
{
globalSessionCounter++;
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Number of active connections: " << globalSessionCounter;
+ LOGD("Number of active connections: " << globalSessionCounter);
fTimer.cancel();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Timer cancelled";
+ LOGD("Timer cancelled");
}
void dcm_session::do_receive() noexcept
{
- BOOST_LOG_FUNCTION();
-
try {
auto self(shared_from_this());
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Read new message";
+ LOGD("Read new message");
fDeserializer.read_message(fSocket,
[self, this](const boost::system::error_code& error, std::size_t bytes_read) {
- BOOST_LOG_FUNCTION();
if(!error) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Received " << bytes_read << " bytes from client";
+ LOGD("Received " << bytes_read << " bytes from client");
decode_message();
} else {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Client disconnected: " << error;
+ LOGE("Client disconnected: " << error);
// Connection object will be released by shared ptr
start_timer();
}
});
} catch(std::exception& ex) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while trying to read message : " << ex.what();
+ LOGE("Caught exception while trying to read message : " << ex.what());
// Connection object will be released by shared ptr
} catch(...) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while trying to read message : " << "unknown";
+ LOGE("Caught unknown exception while trying to read message");
// Connection object will be released by shared ptr
}
}
void dcm_session::decode_message() noexcept
{
- BOOST_LOG_FUNCTION();
try {
// Try to decode whole message
RequestMessage requestMessage;
if(!fDeserializer.decode_received_message(requestMessage)) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't parse message from stream";
+ LOGE("Can't parse message from stream");
// This will terminate connection
return;
}
handle_sign_request(requestMessage.sign_data());
break;
default:
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Incorrect request message type";
+ LOGE("Incorrect request message type");
// This will terminate connection
return;
}
} catch(std::exception& ex) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while parsing message : " << ex.what();
+ LOGE("Caught exception while parsing message : " << ex.what());
// Connection object will be released by shared ptr
} catch(...) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while parsing message : " << "unknown";
+ LOGE("Caught unknown exception while parsing message");
// Connection object will be released by shared ptr
}
}
void dcm_session::reply(const ResponseMessage& resp) noexcept
{
- BOOST_LOG_FUNCTION();
try {
auto self(shared_from_this());
fSerializer.async_write(fSocket,
[self, this](const boost::system::error_code& error, std::size_t bytes_written)
{
- BOOST_LOG_FUNCTION();
if(!error) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Written " << bytes_written << " to socket";
+ LOGD("Written " << bytes_written << " to socket");
do_receive();
} else {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Server disconnected: " << error;
+ LOGE("Server disconnected: " << error);
// Connection object will be released by shared ptr
}
});
} catch(std::exception& ex) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while sending message : " << ex.what();
+ LOGE("Caught exception while sending message : " << ex.what());
// Connection object will be released by shared ptr
} catch(...) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Caught exception while sending message : " << "unknown";
+ LOGE("Caught unknown exception while sending message");
// Connection object will be released by shared ptr
}
}
void dcm_session::handle_context_association(const AssociateKeyContext& message)
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Associate context";
+ LOGD("Associate context");
ResponseMessage msg;
auto * contextResponse = msg.mutable_associate_context();
return;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Associate context from service " <<
- message.service() << " with usage " << message.usage() << " and key type " << message.key_type();
+ LOGD("Associate context from service " << message.service() <<
+ " with usage " << message.usage() << " and key type " << message.key_type());
auto server = fServer.lock();
if(!server) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Server object gone while handling message";
+ LOGE("Server object gone while handling message");
return;
}
contextResponse->set_key_length(crypto_key_length);
}
else {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "No crypto usable backend available";
- throw std::invalid_argument("Unable to find crypto backend");
+ LOGE("No usable backend available");
+ throw std::invalid_argument("Unable to find backend");
}
fCookie = (uintptr_t)fBackendContext.get();
contextResponse->set_context_cookie(fCookie);
void dcm_session::handle_cert_chain(const RequestCertificateChain& message)
{
- BOOST_LOG_FUNCTION();
-
ResponseMessage msg;
auto * certificateResponse = msg.mutable_request_chain();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Request certificate chain";
+ LOGD("Request certificate chain");
if(message.context_cookie() != fCookie) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Received unknown context cookie";
+ LOGE("Received unknown context cookie");
certificateResponse->set_result(-EINVAL);
reply(msg);
return;
}
if(!fBackendContext) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Context not associated with connection";
+ LOGE("Context not associated with connection");
certificateResponse->set_result(-EINVAL);
reply(msg);
return;
}
*certificateResponse->mutable_cert_chain() = cert_chain;
-
certificateResponse->set_result(0);
reply(msg);
}
void dcm_session::handle_sign_request(const SignRequest& message)
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Request data signing";
+ LOGD("Request data signing");
ResponseMessage msg;
auto * signingResponse = msg.mutable_sign_data();
if(message.context_cookie() != fCookie) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Received unknown context cookie";
+ LOGE("Received unknown context cookie");
signingResponse->set_result(-EINVAL);
reply(msg);
return;
}
if(!fBackendContext) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Context not associated with connection";
+ LOGE("Context not associated with connection");
signingResponse->set_result(-EINVAL);
reply(msg);
return;
}
if(message.data_to_sign().size() == 0) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Data to sign is empty and hash type is NONE";
+ LOGE("Data to sign is empty and hash type is NONE");
signingResponse->set_result(-EINVAL);
return;
}
+++ /dev/null
-/******************************************************************
- *
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
- *
- * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************/
-
-#ifndef DCM_DAEMON_LOGGING_H_
-#define DCM_DAEMON_LOGGING_H_
-
-#include <boost/log/common.hpp>
-#include <boost/log/expressions.hpp>
-#include <boost/log/attributes.hpp>
-#include <boost/log/sinks/sync_frontend.hpp>
-#include <boost/log/sinks/syslog_backend.hpp>
-#include <boost/log/sources/logger.hpp>
-#include <boost/log/utility/setup/console.hpp>
-#include <boost/log/utility/setup/common_attributes.hpp>
-#include <boost/log/attributes/timer.hpp>
-#include <boost/log/attributes/named_scope.hpp>
-
-#define LOG_TAG "DCM_SERVER"
-#include <dlog.h>
-
-enum class log_severity {
- debug,
- normal,
- warning,
- error
-};
-
-// Global logger declaration
-BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(dcm_logger, boost::log::sources::severity_logger_mt<log_severity>)
-
-#if defined(NDEBUG) && !defined(DEBUG)
-#else
-#define ENABLE_DEBUG_LOGGING 1
-#endif
-
-#endif /* DCM_DAEMON_LOGGING_H_ */
*
******************************************************************/
-#include <boost/asio.hpp>
-
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/signal.h>
-#include "dcmserver.h"
-#include "logging.h"
-#include "serviceadapter.h"
-
-#include <boost/log/sinks.hpp>
-#include <boost/log/support/date_time.hpp>
-
-#include "boost_log_dlog_sink.h"
-
+#include <boost/asio.hpp>
#include <cynara-client.h>
-BOOST_LOG_ATTRIBUTE_KEYWORD(_scope, "Scope", boost::log::attributes::named_scope::value_type)
-BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime)
-BOOST_LOG_ATTRIBUTE_KEYWORD(_severity, "Severity", log_severity)
-
-void init_logging()
-{
- auto sink = boost::make_shared<boost::log::sinks::synchronous_sink<dlog_output_backend>>();
-
- dlog_custom_severity_mapping<log_severity> mapping("Severity");
-
- mapping[log_severity::debug] = DLOG_DEBUG;
- mapping[log_severity::error] = DLOG_ERROR;
- mapping[log_severity::normal] = DLOG_INFO;
- mapping[log_severity::warning] = DLOG_WARN;
-
- sink->locked_backend()->set_severity_mapper(mapping);
- sink->locked_backend()->set_log_domain(LOG_TAG);
-
-#ifndef ENABLE_DEBUG_LOGGING
- sink->set_filter(_severity >= log_severity::normal);
-#endif
-
- sink->set_formatter(boost::log::expressions::stream
- << boost::log::expressions::attr< unsigned int >("RecordID") // First an attribute "RecordID" is written to the log
- << " [" // then this delimiter separates it from the rest of the line
- << boost::log::expressions::if_(boost::log::expressions::has_attr("Tag"))
- [
- boost::log::expressions::stream << boost::log::expressions::attr< std::string >("Tag") // then goes another attribute named "Tag"
- // Note here we explicitly stated that its type
- // should be std::string. We could omit it just
- // like we did it with the "RecordID", but in this case
- // library would have to detect the actual attribute value
- // type in run time which has the following consequences:
- // - On the one hand, the attribute would have been output
- // even if it has another type (not std::string).
- // - On the other, this detection does not come for free
- // and will result in performance decrease.
- //
- // In general it's better you to specify explicitly which
- // type should an attribute have wherever it is possible.
- // You may specify an MPL sequence of types if the attribute
- // may have more than one type. And you will have to specify
- // it anyway if the library is not familiar with it (see
- // boost/log/utility/type_dispatch/standard_types.hpp for the list
- // of the supported out-of-the-box types).
- << "] [" // yet another delimiter
- ]
- << boost::log::expressions::format_named_scope("Scope", boost::log::keywords::format = "%n", boost::log::keywords::iteration = boost::log::expressions::reverse) << "] "
- << boost::log::expressions::smessage); // here goes the log record text
- boost::log::core::get()->add_sink(sink);
-
- boost::log::add_common_attributes();
- boost::log::core::get()->add_thread_attribute("Scope", boost::log::attributes::named_scope());
-}
+#include "dcmserver.h"
+#include "log.h"
+#include "serviceadapter.h"
cynara * gGlobalCynaraInstance;
int main()
{
int error = 0;
-
- try {
- init_logging();
- } catch(...) {
- std::cerr << "init_logging() failed" << std::endl;
- return EXIT_FAILURE;
- }
-
- BOOST_LOG_FUNCTION();
-
service_adapter serviceAdapter;
cynara_configuration * cynara_conf = nullptr;
error = cynara_configuration_create(&cynara_conf);
if(error != CYNARA_API_SUCCESS) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't initialize Cynara configuration: " << error;
+ LOGE("Can't initialize Cynara configuration: " << error);
serviceAdapter.notify_start_failure(error);
return EXIT_FAILURE;
}
cynara_configuration_destroy(cynara_conf);
if(error != CYNARA_API_SUCCESS) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Can't initialize Cynara instance: " << error;
+ LOGE("Can't initialize Cynara instance: " << error);
serviceAdapter.notify_start_failure(error);
return EXIT_FAILURE;
}
boost::asio::signal_set stop_signals(io_service, SIGINT, SIGTERM);
stop_signals.async_wait([&io_service](const boost::system::error_code&, int sig) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::normal) << "Stopped by signal " << sig;
+ LOGD("Stopped by signal " << sig);
io_service.stop();
});
error = chdir("/");
(void)error; // Don't care
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Create platform socket";
-
std::string lib_backend = "libdcm-backend-api.so.1.0";
auto server(std::make_shared<dcm_server>(io_service, serviceAdapter.create_platform_socket_acceptor(io_service), lib_backend));
boost::asio::signal_set hup_signals(io_service, SIGHUP);
hup_signals.async_wait([](const boost::system::error_code&, int) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::normal) << "Received HUP signal";
+ LOGD("Received HUP signal");
});
serviceAdapter.notify_start_complete();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Start server";
+ LOGD("Start server");
server->start();
io_service.run();
} catch(std::bad_alloc& e) {
serviceAdapter.notify_start_failure(ENOMEM);
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Server failed with OOM exception: " << e.what();
+ LOGE("Server failed with OOM exception: " << e.what());
return EXIT_FAILURE;
} catch(std::exception& e) {
serviceAdapter.notify_start_failure(EFAULT);
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Server failed with exception: " << e.what();
+ LOGE("Server failed with exception: " << e.what());
return EXIT_FAILURE;
} catch(...) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Server failed with unknown exception";
+ LOGE("Server failed with unknown exception");
serviceAdapter.notify_start_failure(EFAULT);
return EXIT_FAILURE;
}
cynara_finish(gGlobalCynaraInstance);
gGlobalCynaraInstance = nullptr;
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::normal) << "Server terminated";
+ LOGD("Server terminated");
return 0;
}
*
******************************************************************/
-#include "serviceadapter.h"
-#include "logging.h"
-
#include <cstring>
#include <systemd/sd-daemon.h>
+#include "serviceadapter.h"
+#include "log.h"
+
service_adapter::service_adapter()
{
}
boost::asio::local::stream_protocol::acceptor service_adapter::create_platform_socket_acceptor(boost::asio::io_service& io_service)
{
- BOOST_LOG_FUNCTION();
-
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Try to get socket from systemd";
+ LOGD("Try to get socket from systemd");
int n = sd_listen_fds(0);
-
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << n << " sockets provided by systemd";
-
if( n > 0 ) {
for(int fd = SD_LISTEN_FDS_START ; fd < SD_LISTEN_FDS_START + n ; ++fd) {
if(sd_is_socket_unix(fd, SOCK_STREAM, 1, fDefaultSocketPath.c_str(), 0)) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Got UNIX domain socket with fd " << fd;
+ LOGD("Got UNIX domain socket with fd " << fd);
return boost::asio::local::stream_protocol::acceptor(
io_service,
}
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "No systemd sockets found";
+ LOGE("No systemd sockets found");
throw std::runtime_error("No socket created by systemd");
}
void service_adapter::notify_start_complete()
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::normal) << "Notify start completed to systemd";
+ LOGD("Notify start completed to systemd");
sd_listen_fds(1);
sd_notify(0, "READY=1");
void service_adapter::notify_start_failure(int error)
{
- BOOST_LOG_FUNCTION();
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Notify start failure";
+ LOGE("Notify start failure");
if(!fStartCompleteNotified) {
char buffer[512];
/******************************************************************
*
- * Copyright 2017 - 2019 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
*
* Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
*
*
******************************************************************/
-#include "soresolver.h"
-#include "logging.h"
#include <dlfcn.h>
+#include "soresolver.h"
+#include "log.h"
+
so_resolver::so_resolver(const std::string& libraryName) :
fLibraryName(libraryName),
fLibraryHandle(nullptr)
so_resolver::~so_resolver()
{
if(fLibraryHandle.load(std::memory_order_relaxed)) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Unloading library " << fLibraryName;
+ LOGD("Unloading library " << fLibraryName);
dlclose(fLibraryHandle.exchange(nullptr, std::memory_order_relaxed));
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Unloaded library " << fLibraryName;
+ LOGD("Unloaded library " << fLibraryName);
}
}
void * so_resolver::resolve_function(const std::string& name) noexcept
{
- BOOST_LOG_FUNCTION();
-
std::unique_lock<std::mutex> locker(fCacheLock);
auto it = fCache.find(name);
void * handle = fLibraryHandle.load(std::memory_order_relaxed);
if(handle) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Resolving symbol " << name << " from " << fLibraryName;
+ LOGD("Resolving symbol " << name << " from " << fLibraryName);
void * sym = dlsym(handle, name.c_str());
if(!sym) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Unable to resolve symbol " << name << " from " <<
- fLibraryName << ": Error is " << dlerror();
+ LOGE("Unable to resolve symbol " << name << " from " <<
+ fLibraryName << ": Error is " << dlerror());
} else {
try {
fCache.emplace(name, sym);
return sym;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Trying to resolve symbol " << name << " from not loaded library " << fLibraryName;
+ LOGE("Trying to resolve symbol " << name << " from not loaded library " << fLibraryName);
return nullptr;
}
bool so_resolver::ensure_loaded() noexcept
{
- BOOST_LOG_FUNCTION();
-
std::unique_lock<std::mutex> locker(fCacheLock);
void * handle = fLibraryHandle.load(std::memory_order_acquire);
if(handle)
return true;
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Loading library " << fLibraryName;
+ LOGD("Loading library " << fLibraryName);
handle = dlopen(fLibraryName.c_str(), RTLD_LAZY | RTLD_LOCAL);
if(!handle) {
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) << "Unable to load library " << fLibraryName << ": " << dlerror();
+ LOGE("Unable to load library " << fLibraryName << ": " << dlerror());
return false;
}
- BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Library loaded " << fLibraryName;
+ LOGD("Library loaded " << fLibraryName);
void * expectedValue = nullptr;
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include <dlog.h>
+#include <syslog.h>
+
+#ifdef NDEBUG
+int __log_level = LOG_ERR;
+#else
+int __log_level = LOG_DEBUG;
+#endif
+
+#define DCM_LOG_TAG "DEVICE_CERTIFICATE_MANAGER"
+
+void dcm_print(int priority, char const *fmt, ...)
+{
+ log_priority dlog_prio;
+
+ switch (priority) {
+ case LOG_EMERG:
+ dlog_prio = DLOG_FATAL;
+ break;
+ case LOG_ERR:
+ dlog_prio = DLOG_ERROR;
+ break;
+ case LOG_WARNING:
+ dlog_prio = DLOG_WARN;
+ break;
+ case LOG_INFO:
+ dlog_prio = DLOG_INFO;
+ break;
+ case LOG_DEBUG:
+ dlog_prio = DLOG_DEBUG;
+ break;
+ default:
+ dlog_prio = DLOG_DEFAULT;
+ }
+
+ va_list ap;
+ va_start(ap, fmt);
+ (void) vprint_system_log(dlog_prio, DCM_LOG_TAG, fmt, ap);
+ va_end(ap);
+}
--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+#ifndef SHARED_LOG_H_
+#define SHARED_LOG_H_
+
+#include <stdlib.h>
+#include <syslog.h>
+#include <sstream>
+
+#define UNUSED __attribute__((unused))
+
+extern int __log_level;
+extern void dcm_print(int priority, char const *fmt, ...);
+
+namespace {
+ template <typename ...Args>
+ void UNUSED __LOG_FUN(int level, const std::stringstream &format, Args&&... args) {
+ dcm_print(level, format.str().c_str(), std::forward<Args>(args)...);
+ }
+
+ template <>
+ void UNUSED __LOG_FUN(int level, const std::stringstream &format) {
+ dcm_print(level, "%s", format.str().c_str());
+ }
+
+ template <typename ...Args>
+ void UNUSED __LOG_FUN(int level, const char *format, Args&&... args) {
+ dcm_print(level, format, std::forward<Args>(args)...);
+ }
+
+ template <>
+ void UNUSED __LOG_FUN(int level, const char *format) {
+ dcm_print(level, "%s", format);
+ }
+
+} // namespace anonymous
+
+#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
+
+#define __LOG(LEVEL, FORMAT, ...) \
+ do { \
+ if (LEVEL <= __log_level) { \
+ std::stringstream __LOG_FORMAT; \
+ __LOG_FORMAT << __FILENAME__ << ": " << __func__ << "(" << __LINE__ << ") > " << FORMAT; \
+ __LOG_FUN(LEVEL, __LOG_FORMAT, ##__VA_ARGS__); \
+ } \
+ } while (0)
+
+#define LOGM(...) __LOG(LOG_EMERG, __VA_ARGS__) /* system is unusable */
+#define LOGE(...) __LOG(LOG_ERR, __VA_ARGS__) /* error conditions */
+#define LOGW(...) __LOG(LOG_WARNING, __VA_ARGS__) /* warning conditions */
+#define LOGI(...) __LOG(LOG_INFO, __VA_ARGS__) /* informational */
+#define LOGD(...) __LOG(LOG_DEBUG, __VA_ARGS__) /* debug-level messages */
+
+#endif /* SHARED_LOG_H_ */
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(../src/dcm-client)
-ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK)
SET(TARGET_TESTS "device-certificate-manager-tests")