From: Konrad Lipinski Date: Tue, 2 Aug 2022 07:35:50 +0000 (+0200) Subject: Switch to CLOCK_MONOTONIC_COARSE X-Git-Tag: submit/tizen/20220803.102654~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=12ae9c73bcc53de2215b64e24a6235dc28580a6c;p=platform%2Fcore%2Fsecurity%2Fsecurity-manager.git Switch to CLOCK_MONOTONIC_COARSE All uses of clock_gettime() are fine with coarse granularity. Renamed monotonicNow() to monotonicCoarseNow() to reflect that. Change-Id: Id60e79ca28a888ad98907184b7c11dd9d0b4aeee --- diff --git a/src/common/include/utils.h b/src/common/include/utils.h index b4998880..8ee30be0 100644 --- a/src/common/include/utils.h +++ b/src/common/include/utils.h @@ -50,7 +50,7 @@ namespace SecurityManager { */ int try_catch(const std::function& func); -time_t monotonicNow(); +time_t monotonicCoarseNow(); // Used for measuring function/method/scope execution time class ScopedTimeStamper { diff --git a/src/common/utils.cpp b/src/common/utils.cpp index f30f8a20..071919f8 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (c) 2019-2022 Samsung Electronics Co., Ltd. All rights reserved. * * This file is licensed under the terms of MIT License or the Apache License * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details. @@ -71,9 +71,9 @@ int try_catch(const std::function& func) return SECURITY_MANAGER_ERROR_UNKNOWN; } -time_t monotonicNow() { +time_t monotonicCoarseNow() { struct timespec now; - if (clock_gettime(CLOCK_MONOTONIC_RAW, &now) == -1) { + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &now)) { int err = errno; LogError("Can't access monotonic clock, error: " << GetErrnoString(err)); return 0; diff --git a/src/server/main/socket-manager.cpp b/src/server/main/socket-manager.cpp index c7623412..97b50d4e 100644 --- a/src/server/main/socket-manager.cpp +++ b/src/server/main/socket-manager.cpp @@ -75,7 +75,7 @@ void SocketManager::CreateDefaultReadSocketDescription(int sock) auto &desc = m_socketDescriptionVector[sock]; desc.isOpen = true; - desc.timeout = monotonicNow() + SOCKET_TIMEOUT; + desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT; desc.buffer.InitForInput(); if (false == desc.isTimeout) { @@ -172,7 +172,7 @@ bool SocketManager::GotSigTerm() const { void SocketManager::ReadyForRead(int sock) { auto &desc = m_socketDescriptionVector[sock]; auto &buffer = desc.buffer; - desc.timeout = monotonicNow() + SOCKET_TIMEOUT; + desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT; ssize_t size = read(sock, buffer.Ptr(), buffer.InputSize()); @@ -242,7 +242,7 @@ void SocketManager::ReadyForWrite(int sock) { return; // We do not want to propagate error to next layer } - desc.timeout = monotonicNow() + SOCKET_TIMEOUT; + desc.timeout = monotonicCoarseNow() + SOCKET_TIMEOUT; if (buffer.OutputDone(result)) CloseSocket(sock); } @@ -285,7 +285,7 @@ void SocketManager::MainLoop() { LogDebug("No usable timeout found."); ptrTimeout = NULL; // select will wait without timeout } else { - time_t currentTime = monotonicNow(); + time_t currentTime = monotonicCoarseNow(); auto &pqTimeout = m_timeoutQueue.top(); // 0 means that select won't block and socket will be closed ;-) diff --git a/test/test_misc.cpp b/test/test_misc.cpp index 80716a0f..ee99bbcb 100644 --- a/test/test_misc.cpp +++ b/test/test_misc.cpp @@ -118,7 +118,7 @@ POSITIVE_TEST_CASE(T287_groups_loading) POSITIVE_TEST_CASE(T288_monotonic_clock) { time_t t; - BOOST_REQUIRE_NO_THROW(t = monotonicNow()); + BOOST_REQUIRE_NO_THROW(t = monotonicCoarseNow()); BOOST_REQUIRE_MESSAGE(t > 0, "Invalid monotonic time value"); }