Agent code is not used not built anymore. This commit deletes it.
Change-Id: I93d054707ced2622a9317a8e34620232a288e2a3
+++ /dev/null
-# Copyright (c) 2014-2017 Samsung Electronics Co., Ltd 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.
-#
-# @file CMakeLists.txt
-# @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
-#
-
-PKG_CHECK_MODULES(AGENT_DEP
- REQUIRED
- cynara-agent
- cynara-plugin
- libsystemd
- security-manager
- pkgmgr-info
- )
-
-SET(ASKUSER_AGENT_PATH ${ASKUSER_PATH}/agent)
-
-SET(ASKUSER_SOURCES
- ${ASKUSER_AGENT_PATH}/main/Agent.cpp
- ${ASKUSER_AGENT_PATH}/main/CynaraTalker.cpp
- ${ASKUSER_AGENT_PATH}/main/main.cpp
- ${ASKUSER_AGENT_PATH}/main/PolicyUpdater.cpp
- ${ASKUSER_AGENT_PATH}/main/Policy.cpp
- ${ASKUSER_AGENT_PATH}/ui/NotificationBackend.cpp
- ${ASKUSER_AGENT_PATH}/ui/FdNotifyObject.cpp
- )
-
-INCLUDE_DIRECTORIES(
- ${ASKUSER_PATH}/common
- ${AGENT_DEP_INCLUDE_DIRS}
- ${ASKUSER_AGENT_PATH}
- )
-
-ADD_DEFINITIONS("-fPIE")
-ADD_EXECUTABLE(${TARGET_ASKUSER} ${ASKUSER_SOURCES})
-
-TARGET_LINK_LIBRARIES(${TARGET_ASKUSER}
- ${AGENT_DEP_LIBRARIES}
- ${TARGET_ASKUSER_COMMON}
- "-pthread"
- "-pie"
- )
-
-INSTALL(TARGETS ${TARGET_ASKUSER} DESTINATION ${BIN_INSTALL_DIR})
+++ /dev/null
-/*
- * Copyright (c) 2014-2017 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file Agent.cpp
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file implements main class of ask user agent
- */
-
-#include <chrono>
-#include <cstdlib>
-#include <memory>
-#include <sstream>
-#include <string>
-#include <unistd.h>
-#include <utility>
-
-#include <attributes/attributes.h>
-#include <translator/Translator.h>
-#include <types/AgentErrorMsg.h>
-#include <types/SupportedTypes.h>
-#include <ui/NotificationBackend.h>
-
-#include <log/alog.h>
-
-#include "Agent.h"
-#include "Policy.h"
-
-namespace AskUser {
-
-namespace Agent {
-
-volatile sig_atomic_t Agent::m_stopFlag = 0;
-
-Agent::Agent() : m_cynaraTalker([&](Request *request) -> void { requestHandler(request); }) {
- init();
-}
-
-Agent::~Agent() {
- finish();
-}
-
-void Agent::init() {
- ALOGD("Agent daemon initialized");
-}
-
-void Agent::run() {
- m_cynaraTalker.start();
- m_policyUpdater.start();
-
- while (!m_stopFlag) {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_event.wait_for(lock, std::chrono::milliseconds(1000));
-
- if (m_stopFlag) {
- break;
- }
-
- while (!m_incomingRequests.empty() || !m_incomingResponses.empty()) {
-
- if (!m_incomingRequests.empty()) {
- Request *request = m_incomingRequests.front();
- m_incomingRequests.pop();
- lock.unlock();
-
- ALOGD("Request popped from queue:"
- " type [" << request->type() << "],"
- " id [" << request->id() << "],"
- " data length [" << request->data().size() << "]");
-
- if (request->type() == RT_Close) {
- delete request;
- m_stopFlag = 1;
- break;
- }
-
- processCynaraRequest(request);
-
- lock.lock();
- }
-
- if (!m_incomingResponses.empty()) {
- Response response = m_incomingResponses.front();
- m_incomingResponses.pop();
- lock.unlock();
-
- ALOGD("Response popped from queue:"
- " type [" << response.type() << "],"
- " id [" << response.id() << "]");
-
- processUIResponse(response);
-
- lock.lock();
- }
-
- lock.unlock();
- cleanupUIThreads();
- lock.lock();
- }
- }
-
- ALOGD("Agent task stopped");
-}
-
-void Agent::finish() {
- m_policyUpdater.stop();
- if (!m_cynaraTalker.stop()) {
- ALOGE("Threads could not be stopped. Calling quick_exit()");
- quick_exit(EXIT_SUCCESS);
- }
-
- while (!m_incomingRequests.empty()) {
- Request *request = m_incomingRequests.front();
- m_incomingRequests.pop();
- delete request;
- }
-
- if (!cleanupUIThreads()) {
- ALOGE("At least one of UI threads could not be stopped. Calling quick_exit()");
- quick_exit(EXIT_SUCCESS);
- }
-
- for (auto it = m_requests.begin(); it != m_requests.end();) {
- delete it->second;
- it = m_requests.erase(it);
- }
-
- ALOGD("Agent daemon has stopped commonly");
-}
-
-void Agent::requestHandler(Request *request) {
- ALOGD("Cynara request received:"
- " type [" << request->type() << "],"
- " id [" << request->id() << "],"
- " data length: [" << request->data().size() << "]");
-
- std::unique_lock<std::mutex> lock(m_mutex);
- m_incomingRequests.push(request);
- m_event.notify_one();
-}
-
-void Agent::processCynaraRequest(Request *request) {
- std::unique_ptr<Request> requestPtr(request);
-
- auto existingRequest = m_requests.find(request->id());
- if (existingRequest != m_requests.end()) {
- if (request->type() == RT_Cancel) {
- delete existingRequest->second;
- m_requests.erase(existingRequest);
- m_cynaraTalker.sendResponse(request->type(), request->id());
- dismissUI(request->id());
- } else {
- ALOGE("Incoming request with ID: [" << request->id() << "] is being already processed");
- }
- return;
- }
-
- if (request->type() == RT_Cancel) {
- ALOGE("Cancel request for unknown request: ID: [" << request->id() << "]");
- return;
- }
-
- if (!startUIForRequest(request)) {
- auto data = Translator::Agent::answerToData(Cynara::PolicyType(), AgentErrorMsg::Error);
- m_cynaraTalker.sendResponse(RT_Action, request->id(), data);
- return;
- }
-
- m_requests.insert(std::make_pair(request->id(), request));
- requestPtr.release();
-}
-
-void Agent::processUIResponse(const Response &response) {
- auto requestIt = m_requests.find(response.id());
- if (requestIt != m_requests.end()) {
- Cynara::PluginData pluginData;
- if (response.type() == URT_ERROR) {
- pluginData = Translator::Agent::answerToData(Cynara::PolicyType(),
- AgentErrorMsg::Error);
- } else if (response.type() == URT_TIMEOUT) {
- pluginData = Translator::Agent::answerToData(Cynara::PolicyType(),
- AgentErrorMsg::Timeout);
- } else {
- pluginData = Translator::Agent::answerToData(
- UIResponseToPolicyType(response.type()),
- AgentErrorMsg::NoError);
- }
- m_cynaraTalker.sendResponse(RT_Action, requestIt->second->id(), pluginData);
- auto data = Translator::Agent::dataToRequest(requestIt->second->data());
-
- std::string appId, pkgLabel;
- identifyApp(data.client, data.user, appId, pkgLabel);
-
- switch (response.type()) {
- case URT_NO_LIFE:
- m_policyUpdater.update(appId, data.user, data.privilege, "Deny");
- break;
- case URT_YES_LIFE:
- m_policyUpdater.update(appId, data.user, data.privilege, "Allow");
- break;
- default:
- break;
- }
-
- delete requestIt->second;
- m_requests.erase(requestIt);
- }
-
- dismissUI(response.id());
-}
-
-bool Agent::startUIForRequest(Request *request) {
- auto data = Translator::Agent::dataToRequest(request->data());
-
- AskUIInterfacePtr ui(new NotificationBackend());
-
- auto handler = [&](RequestId requestId, UIResponseType resultType) -> void {
- UIResponseHandler(requestId, resultType);
- };
-
- std::string appId, pkgLabel;
- identifyApp(data.client, data.user, appId, pkgLabel);
-
- bool ret = ui->start(data.client, pkgLabel, data.user, data.privilege, request->id(), handler);
- if (ret) {
- m_UIs.insert(std::make_pair(request->id(), std::move(ui)));
- }
-
- return ret;
-}
-
-void Agent::UIResponseHandler(RequestId requestId, UIResponseType responseType) {
- ALOGD("UI response received: type [" << responseType << "], id [" << requestId << "]");
-
- std::unique_lock<std::mutex> lock(m_mutex);
- m_incomingResponses.push(Response(requestId, responseType));
- m_event.notify_one();
-}
-
-bool Agent::cleanupUIThreads() {
- bool ret = true;
- for (auto it = m_UIs.begin(); it != m_UIs.end();) {
- if (it->second->isDismissing() && it->second->dismiss()) {
- it = m_UIs.erase(it);
- } else {
- ret = false;
- ++it;
- }
- }
- return ret;
-}
-
-void Agent::dismissUI(RequestId requestId) {
- auto it = m_UIs.find(requestId);
- if (it != m_UIs.end()) {
- if (it->second->dismiss()) {
- it = m_UIs.erase(it);
- }
- }
-}
-
-Cynara::PolicyType Agent::UIResponseToPolicyType(UIResponseType responseType) {
- switch (responseType) {
- case URT_YES_ONCE:
- return AskUser::SupportedTypes::Client::ALLOW_ONCE;
- case URT_YES_SESSION:
- return AskUser::SupportedTypes::Client::ALLOW_PER_SESSION;
- case URT_YES_LIFE:
- return AskUser::SupportedTypes::Client::ALLOW_PER_LIFE;
- case URT_NO_ONCE:
- return AskUser::SupportedTypes::Client::DENY_ONCE;
- case URT_NO_SESSION:
- return AskUser::SupportedTypes::Client::DENY_PER_SESSION;
- case URT_NO_LIFE:
- return AskUser::SupportedTypes::Client::DENY_PER_LIFE;
- default:
- return AskUser::SupportedTypes::Client::DENY_ONCE;
- }
-}
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file Agent.h
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file defines main class of ask user agent
- */
-
-#pragma once
-
-#include <csignal>
-#include <map>
-#include <mutex>
-#include <queue>
-#include <types/PolicyType.h>
-
-#include <main/CynaraTalker.h>
-#include <main/PolicyUpdater.h>
-#include <main/Request.h>
-#include <main/Response.h>
-
-#include <ui/AskUIInterface.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-class Agent {
-public:
- Agent();
- ~Agent();
-
- void run();
-
- static void stop() {
- m_stopFlag = 1;
- }
-
-private:
- CynaraTalker m_cynaraTalker;
- PolicyUpdater m_policyUpdater;
- std::map<RequestId, Request *> m_requests;
- std::queue<Request *> m_incomingRequests;
- std::queue<Response> m_incomingResponses;
- std::condition_variable m_event;
- std::mutex m_mutex;
- static volatile sig_atomic_t m_stopFlag;
- std::map<RequestId, AskUIInterfacePtr> m_UIs;
-
- void init();
- void finish();
-
- void requestHandler(Request *request);
- void processCynaraRequest(Request *request);
- bool startUIForRequest(Request *request);
- void UIResponseHandler(RequestId requestId, UIResponseType responseType);
-
- void processUIResponse(const Response &response);
- bool cleanupUIThreads();
- void dismissUI(RequestId requestId);
-
- static Cynara::PolicyType UIResponseToPolicyType(UIResponseType responseType);
-};
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file CynaraTalker.cpp
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file implements class of cynara talker
- */
-
-#include <csignal>
-#include <string>
-
-#include <attributes/attributes.h>
-#include <types/SupportedTypes.h>
-
-#include <log/alog.h>
-
-#include "CynaraTalker.h"
-
-namespace {
-
-class TypeException : std::exception {
-public:
- TypeException(const std::string &msg) : m_what(msg) {};
- virtual const char* what() const noexcept {
- return m_what.c_str();
- }
-
-private:
- std::string m_what;
-};
-
-AskUser::Agent::RequestType cynaraType2AgentType(cynara_agent_msg_type type) {
- switch (type) {
- case CYNARA_MSG_TYPE_ACTION:
- return AskUser::Agent::RT_Action;
- case CYNARA_MSG_TYPE_CANCEL:
- return AskUser::Agent::RT_Cancel;
- }
-
- throw TypeException("Unsupported request type: " + std::to_string(type) +
- " received from cynara.");
-}
-
-cynara_agent_msg_type agentType2CynaraType(AskUser::Agent::RequestType type) {
- switch (type) {
- case AskUser::Agent::RT_Action:
- return CYNARA_MSG_TYPE_ACTION;
- case AskUser::Agent::RT_Cancel:
- return CYNARA_MSG_TYPE_CANCEL;
- default: // let's make compiler happy
- break;
- }
-
- throw TypeException("Invalid response type: " + std::to_string(type) + " to send to cynara.");
-}
-
-}
-
-namespace AskUser {
-
-namespace Agent {
-
-CynaraTalker::CynaraTalker(RequestHandler requestHandler) : m_requestHandler(requestHandler),
- m_cynara(nullptr)
-{
- m_future = m_threadFinished.get_future();
-}
-
-bool CynaraTalker::start() {
- if (!m_requestHandler) {
- ALOGE("Empty request handler!");
- return false;
- }
-
- m_thread = std::thread(&CynaraTalker::run, this);
- return true;
-}
-
-bool CynaraTalker::stop() {
- if (!m_thread.joinable())
- return true;
- // There is no possibility to stop this thread nicely when it waits for requests from cynara
- // We can only try to get rid of thread
- auto status = m_future.wait_for(std::chrono::milliseconds(10));
- if (status == std::future_status::ready) {
- ALOGD("Cynara thread finished and ready to join.");
- m_thread.join();
- return true;
- }
-
- ALOGD("Cynara thread not finished.");
- return false;
-}
-
-void CynaraTalker::run() {
- int ret;
- sigset_t mask;
-
- sigemptyset(&mask);
- sigaddset(&mask, SIGTERM);
- if ((ret = sigprocmask(SIG_BLOCK, &mask, nullptr)) < 0) {
- ALOGE("sigprocmask failed [<<" << ret << "]");
- }
-
- ret = cynara_agent_initialize(&m_cynara, SupportedTypes::Agent::AgentType);
- if (ret != CYNARA_API_SUCCESS) {
- ALOGE("Initialization of cynara structure failed with error: [" << ret << "]");
- m_requestHandler(new Request(RT_Close, 0, nullptr, 0)); // Notify agent he should die
- return;
- }
-
- void *data = nullptr;
-
- try {
- while (true) {
- cynara_agent_msg_type req_type;
- cynara_agent_req_id req_id;
- size_t data_size = 0;
-
- ret = cynara_agent_get_request(m_cynara, &req_type, &req_id, &data, &data_size);
- if (ret != CYNARA_API_SUCCESS) {
- ALOGE("Receiving request from cynara failed with error: [" << ret << "]");
- m_requestHandler(new Request(RT_Close, 0, nullptr, 0));
- break;
- }
-
- try {
- m_requestHandler(new Request(cynaraType2AgentType(req_type), req_id, data,
- data_size));
- } catch (const TypeException &e) {
- ALOGE("TypeException: <" << e.what() << "> Request dropped!");
- }
- free(data);
- data = nullptr;
- }
- } catch (const std::exception &e) {
- ALOGC("Unexpected exception: <" << e.what() << ">");
- } catch (...) {
- ALOGE("Unexpected unknown exception caught!");
- }
-
- free(data);
-
- std::unique_lock<std::mutex> mlock(m_mutex);
- ret = cynara_agent_finish(m_cynara);
- m_cynara = nullptr;
- if (ret != CYNARA_API_SUCCESS) {
- ALOGE("Finishing cynara connection failed with error: [" << ret << "]");
- }
-
- m_threadFinished.set_value(true);
-}
-
-bool CynaraTalker::sendResponse(RequestType requestType, RequestId requestId,
- const Cynara::PluginData &data) {
-
- std::unique_lock<std::mutex> mlock(m_mutex);
-
- if (!m_cynara) {
- ALOGE("Trying to send response using uninitialized cynara connection!");
- return false;
- }
-
- int ret;
- try {
- ret = cynara_agent_put_response(m_cynara, agentType2CynaraType(requestType), requestId,
- data.size() ? data.data() : nullptr, data.size());
- } catch (const TypeException &e) {
- ALOGE("TypeException: <" << e.what() << "> Response dropped!");
- ret = CYNARA_API_INVALID_PARAM;
- }
-
- if (ret != CYNARA_API_SUCCESS) {
- ALOGE("Sending response to cynara failed with error: [" << ret << "]");
- return false;
- }
-
- return true;
-}
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file CynaraTalker.h
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file declares class representing request from cynara service
- */
-
-#pragma once
-
-#include <functional>
-#include <future>
-#include <mutex>
-#include <thread>
-
-#include <cynara-agent.h>
-#include <cynara-plugin.h>
-
-#include <main/Request.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-typedef std::function<void(Request *)> RequestHandler;
-
-class CynaraTalker {
-public:
- CynaraTalker(RequestHandler requestHandler);
- ~CynaraTalker() {}
-
- bool start();
- bool stop();
-
- bool sendResponse(RequestType requestType, RequestId requestId,
- const Cynara::PluginData &data = Cynara::PluginData());
-
-private:
- RequestHandler m_requestHandler;
- cynara_agent *m_cynara;
- std::thread m_thread;
- std::mutex m_mutex;
- std::promise<bool> m_threadFinished;
- std::future<bool> m_future;
-
- void run();
-};
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file Request.h
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file declares class representing request from cynara service
- */
-
-#pragma once
-
-#include <cstdlib>
-#include <vector>
-
-#include <cynara-plugin.h>
-
-#include <types/RequestId.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-typedef enum {
- RT_Action,
- RT_Cancel,
- RT_Close
-} RequestType;
-
-class Request {
-public:
- Request() = default;
- Request(RequestType type, RequestId id, void *data, std::size_t dataSize)
- : m_type(type), m_id(id), m_data(static_cast<char *>(data), dataSize) {}
- ~Request() {}
-
- RequestType type() const {
- return m_type;
- }
-
- RequestId id() const {
- return m_id;
- }
-
- const Cynara::PluginData &data() const {
- return m_data;
- }
-
-private:
- RequestType m_type;
- RequestId m_id;
- Cynara::PluginData m_data;
-};
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd 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.
- */
-/**
- * @file Response.h
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file declares class representing response from user interface
- */
-
-#pragma once
-
-#include <main/Request.h>
-#include <ui/AskUIInterface.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-class Response {
-public:
- Response() = default;
- Response(RequestId requestId, UIResponseType responseType) : m_id(requestId),
- m_type(responseType) {}
- ~Response() {}
-
- RequestId id() const {
- return m_id;
- }
-
- UIResponseType type() const {
- return m_type;
- }
-
-private:
- RequestId m_id;
- UIResponseType m_type;
-};
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd 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
- */
-/**
- * @file src/main/main.cpp
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief Main ask user daemon file
- */
-
-#include <clocale>
-#include <csignal>
-#include <cstdlib>
-#include <cstring>
-#include <exception>
-#include <attributes/attributes.h>
-#include <log/alog.h>
-
-#ifdef BUILD_WITH_SYSTEMD_DAEMON
-#include <systemd/sd-daemon.h>
-#endif
-
-#include "Agent.h"
-
-// Handle kill message from systemd
-void kill_handler(int sig UNUSED) {
- ALOGD("Ask user agent service is going down now");
- AskUser::Agent::Agent::stop();
-}
-
-int main(int argc UNUSED, char **argv UNUSED) {
- init_agent_log();
-
- int ret;
- struct sigaction act;
-
- // Install kill handler - TERM signal will be delivered form systemd to kill this service
- memset(&act, 0, sizeof(act));
- act.sa_handler = &kill_handler;
- if ((ret = sigaction(SIGTERM, &act, NULL)) < 0) {
- ALOGE("sigaction failed [<<" << ret << "]");
- return EXIT_FAILURE;
- }
-
- char *locale = setlocale(LC_ALL, "");
- ALOGD("Current locale is: <" << locale << ">");
-
- try {
- AskUser::Agent::Agent agent;
-
-#ifdef BUILD_WITH_SYSTEMD_DAEMON
- int ret = sd_notify(0, "READY=1");
- if (ret == 0) {
- ALOGW("Agent was not configured to notify its status");
- } else if (ret < 0) {
- ALOGE("sd_notify failed: [" << ret << "]");
- }
-#endif
- agent.run();
- } catch (const std::exception &e) {
- ALOGC("Agent stopped because of unhandled exception: <" << e.what() << ">");
- return EXIT_FAILURE;
- } catch (...) {
- ALOGC("Agent stopped because of unknown unhandled exception.");
- return EXIT_FAILURE;
- }
-
- return EXIT_SUCCESS;
-}
+++ /dev/null
-/*
- * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd 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
- */
-/**
- * @file AskUIInterface.h
- * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
- * @brief This file contains ask user UI interface declaration.
- */
-
-#pragma once
-
-#include <functional>
-#include <memory>
-#include <string>
-
-#include <main/Request.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-typedef enum {
- URT_NO_ONCE,
- URT_NO_SESSION,
- URT_NO_LIFE,
- URT_YES_ONCE,
- URT_YES_SESSION,
- URT_YES_LIFE,
- URT_TIMEOUT,
- URT_ERROR
-} UIResponseType;
-
-typedef std::function<void(RequestId, UIResponseType)> UIResponseCallback;
-
-class AskUIInterface {
-public:
- virtual ~AskUIInterface() {};
-
- virtual bool start(const std::string &client, const std::string &pkgLabel,
- const std::string &user, const std::string &privilege,
- RequestId requestId, UIResponseCallback) = 0;
- virtual bool setOutdated() = 0;
- virtual bool dismiss() = 0;
- virtual bool isDismissing() const = 0;
-};
-
-typedef std::unique_ptr<AskUIInterface> AskUIInterfacePtr;
-
-} // namespace Agent
-
-} // namespace AskUser
+++ /dev/null
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd 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
- */
-/**
- * @file src/monitor/notify/FdNotifyObject.cpp
- * @author Zofia Abramowska <z.abramowska@samsung.com>
- * @version 1.0
- * @brief This file contains implementation of cynara's fd notification object
- */
-
-#include <cerrno>
-#include <cstring>
-#include <unistd.h>
-
-#include <log/alog.h>
-#include <util/SafeFunction.h>
-
-#include "FdNotifyObject.h"
-
-using namespace AskUser::Util;
-
-namespace AskUser {
-
-FdNotifyObject::FdNotifyObject() : m_pipeFd{-1, -1} {
-}
-
-bool FdNotifyObject::init(void) {
- int ret = pipe(m_pipeFd);
- if (ret != 0) {
- int err = errno;
- ALOGE("Couldn't initialize pipes: " << safeStrError(err));
- return false;
- }
- return true;
-}
-
-int FdNotifyObject::getNotifyFd(void) {
- return m_pipeFd[0];
-}
-
-bool FdNotifyObject::notify(void) {
- const char wakeup[] = "w";
- int ret = TEMP_FAILURE_RETRY(write(m_pipeFd[1], wakeup, sizeof(wakeup)));
- if (ret == -1) {
- return false;
- }
- return true;
-}
-
-FdNotifyObject::~FdNotifyObject() {
- if (m_pipeFd[0] != -1)
- close(m_pipeFd[0]);
- if (m_pipeFd[1] != -1)
- close(m_pipeFd[1]);
-}
-
-} /* namespace Cynara */
+++ /dev/null
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd 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
- */
-/**
- * @file src/agent/ui/FdNotifyObject.h
- * @author Zofia Abramowska <z.abramowska@samsung.com>
- * @version 1.0
- * @brief This file contains definition of fd notification object
- */
-
-#pragma once
-
-namespace AskUser {
-
-class FdNotifyObject {
-public:
- FdNotifyObject();
- ~FdNotifyObject();
- int getNotifyFd(void);
- bool init(void);
- bool notify(void);
-private:
- int m_pipeFd[2];
-};
-
-} /* namespace AskUser */
+++ /dev/null
-/*
- * Copyright (c) 2016-2017 Samsung Electronics Co.
- *
- * 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
- */
-/**
- * @file src/daemon/NotificationBackend.cpp
- * @author Zofia Abramowska <z.abramowska@samsung.com>
- * @brief Definition of NotificationBackend class
- */
-
-#include "NotificationBackend.h"
-
-#include <stdexcept>
-#include <string>
-
-#include <exception/Exception.h>
-#include <log/alog.h>
-#include <socket/Poll.h>
-#include <socket/Socket.h>
-#include <translator/Translator.h>
-#include <translator/GuiTranslator.h>
-#include <config/Path.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-namespace {
- UIResponseType NResponseTypeToUI(NResponseType responseType) {
- switch (responseType) {
- case NResponseType::Allow:
- return UIResponseType::URT_YES_ONCE;
- case NResponseType::AllowAlways:
- return UIResponseType::URT_YES_LIFE;
- case NResponseType::DenyAlways:
- return UIResponseType::URT_NO_LIFE;
- case NResponseType::Error:
- return UIResponseType::URT_ERROR;
- case NResponseType::None:
- return UIResponseType::URT_TIMEOUT;
- case NResponseType::Deny:
- default:
- return UIResponseType::URT_NO_ONCE;
- }
- }
-}
-
-bool NotificationBackend::start(const std::string &client, const std::string &pkgLabel,
- const std::string &user, const std::string &privilege,
- RequestId requestId, UIResponseCallback responseCb) {
- if (!m_notify.init()) {
- ALOGW("Couldn't initialize notification object.");
- return false;
- }
- m_responseHandler = responseCb;
- m_request.id = static_cast<NotificationRequestId>(requestId);
- m_request.data.client = client;
- m_request.data.user = user;
- m_request.data.privilege = privilege;
- m_request.pkgLabel = pkgLabel;
-
- m_thread = std::thread(&NotificationBackend::run, this);
- return true;
-}
-bool NotificationBackend::setOutdated() {
- return false;
-}
-bool NotificationBackend::dismiss() {
- ALOGD("Dismissing");
- m_running = false;
- return m_notify.notify();
-}
-bool NotificationBackend::isDismissing() const {
- return m_running;
-}
-
-NotificationBackend::~NotificationBackend() {
- dismiss();
- m_clientSocket.close();
- m_thread.join();
-}
-
-void NotificationBackend::reportError() {
- m_responseHandler(static_cast<RequestId>(m_request.id), NResponseTypeToUI(NResponseType::Error));
-}
-
-bool NotificationBackend::sendRequest()
-{
- std::string data = Translator::Gui::notificationRequestToData(m_request.id,
- m_request.data.client,
- m_request.pkgLabel,
- m_request.data.privilege);
- return m_clientSocket.send(data);
-}
-
-bool NotificationBackend::parseResponse(NotificationResponse response)
-{
- if (response.id != m_request.id) {
- ALOGE("Got response with different id (" << response.id
- << ") than request (" << m_request.id << ")");
- return false;
- }
-
- ALOGD("For user: <" << m_request.data.user
- << "> client: <" << m_request.data.client
- << "> privilege: <" << m_request.data.privilege
- << "> received: <" << Translator::Gui::responseToString(response.response) << ">");
-
- return true;
-}
-
-bool NotificationBackend::recvResponse(NotificationResponse &response)
-{
- int requestId, responseType;
- if (!m_clientSocket.recv(requestId) || !m_clientSocket.recv(responseType)) {
- ALOGE("Failed to fetch response");
- return false;
- }
- response.id = static_cast<NotificationRequestId>(requestId);
- response.response = static_cast<NResponseType>(responseType);
- return true;
-}
-
-void NotificationBackend::run()
-{
- try {
- if (!m_clientSocket.connect(Path::getUserSocketPath(m_request.data.user))) {
- ALOGE("Cannot connect to proper service");
- reportError();
- return;
- }
-
- if (!mainLoop()) {
- reportError();
- return;
- }
-
- } catch (const std::exception &e) {
- ALOGE("Got exception : " << e.what());
- reportError();
- } catch (...) {
- ALOGE("Got unknown exception");
- reportError();
- }
-}
-
-bool NotificationBackend::mainLoop() {
- ALOGD("Notification loop started");
- m_poller.setEvents(m_clientSocket.getFd(), POLLOUT | POLLERR | POLLHUP);
- m_poller.setEvents(m_notify.getNotifyFd(), POLLIN);
- while (!m_running) {
- int eCount = m_poller.wait();
- if (eCount == -1) {
- ALOGE_ERRNO("Polling failed");
- return false;
- }
-
- if (m_poller.getEvents(m_notify.getNotifyFd()) & POLLIN) {
- ALOGW("Got signal to stop");
- return true;
- }
-
- if (m_poller.getEvents(m_clientSocket.getFd()) & POLLERR) {
- ALOGE("Error on socket connection");
- return false;
- }
-
- if (m_poller.getEvents(m_clientSocket.getFd()) & POLLIN) {
- NotificationResponse response;
- if (!recvResponse(response)) {
- ALOGE("Couldn't receive response");
- return false;
- }
- if (!parseResponse(response)) {
- ALOGE("Incorrect response received");
- return false;
- }
- m_responseHandler(static_cast<RequestId>(m_request.id), NResponseTypeToUI(response.response));
- return true;
- }
-
- if (m_poller.getEvents(m_clientSocket.getFd()) & POLLHUP) {
- ALOGE("Service disconnected");
- return false;
- }
-
- if (m_poller.getEvents(m_clientSocket.getFd()) & POLLOUT) {
- if (!sendRequest()) {
- ALOGE("Couldn't send request");
- return false;
- }
-
- m_poller.setEvents(m_clientSocket.getFd(), POLLIN | POLLERR);
- continue;
- }
- }
-
- ALOGD("NotificationBackend loop ended");
- return true;
-}
-
-} /* namespace Agent */
-
-} /* namespace AskUser */
+++ /dev/null
-/*
- * Copyright (c) 2016-2017 Samsung Electronics Co.
- *
- * 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
- */
-/**
- * @file src/daemon/NotificationBackend.cpp
- * @author Zofia Abramowska <z.abramowska@samsung.com>
- * @brief Declaration of NotificationBackend class
- */
-
-#pragma once
-
-#include <atomic>
-#include <functional>
-#include <string>
-#include <thread>
-
-#include <ui/AskUIInterface.h>
-#include <ui/FdNotifyObject.h>
-#include <socket/Socket.h>
-#include <socket/Poll.h>
-#include <types/NotificationResponse.h>
-#include <types/NotificationRequest.h>
-
-namespace AskUser {
-
-namespace Agent {
-
-typedef std::function<void(NotificationResponse)> ResponseHandler;
-
-class NotificationBackend : public AskUIInterface {
-public:
- NotificationBackend() : m_poller(2), m_clientSocket(Socket::PeerType::CLIENT), m_running(false) {}
- virtual ~NotificationBackend();
-
- virtual bool start(const std::string &client, const std::string &pkgLabel,
- const std::string &user, const std::string &privilege, RequestId requestId,
- UIResponseCallback responseCb);
- virtual bool setOutdated();
- virtual bool dismiss();
- virtual bool isDismissing() const;
-
-protected:
- void reportError();
- bool mainLoop();
- void run();
-
- bool sendRequest();
- bool parseResponse(NotificationResponse response);
- bool recvResponse(NotificationResponse &response);
-
- FdNotifyObject m_notify;
- UIResponseCallback m_responseHandler;
- NotificationRequest m_request;
- Poll m_poller;
- Socket m_clientSocket;
- std::thread m_thread;
- std::atomic<bool> m_running;
-};
-
-} /* namespace Agent */
-
-} /* namespace AskUser */