Remove boost-log dependency
[platform/core/security/device-certificate-manager.git] / src / dcm-daemon / serviceadapter.cpp
1 /******************************************************************
2  *
3  * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
4  *
5  * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <cstring>
22
23 #include <systemd/sd-daemon.h>
24
25 #include "serviceadapter.h"
26 #include "log.h"
27
28 service_adapter::service_adapter()
29 {
30 }
31
32 service_adapter::~service_adapter()
33 {
34 }
35
36 boost::asio::local::stream_protocol::acceptor service_adapter::create_platform_socket_acceptor(boost::asio::io_service& io_service)
37 {
38         LOGD("Try to get socket from systemd");
39
40         int n = sd_listen_fds(0);
41         if( n > 0 ) {
42                 for(int fd = SD_LISTEN_FDS_START ; fd < SD_LISTEN_FDS_START + n ; ++fd) {
43                         if(sd_is_socket_unix(fd, SOCK_STREAM, 1, fDefaultSocketPath.c_str(), 0)) {
44                                 LOGD("Got UNIX domain socket with fd " << fd);
45
46                                 return boost::asio::local::stream_protocol::acceptor(
47                                         io_service,
48                                         boost::asio::local::stream_protocol(),
49                                         fd);
50                         }
51                 }
52         }
53
54         LOGE("No systemd sockets found");
55
56         throw std::runtime_error("No socket created by systemd");
57 }
58
59 void service_adapter::notify_start_complete()
60 {
61         LOGD("Notify start completed to systemd");
62
63         sd_listen_fds(1);
64         sd_notify(0, "READY=1");
65         fStartCompleteNotified = true;
66 }
67
68 void service_adapter::notify_start_failure(int error)
69 {
70         LOGE("Notify start failure");
71
72         if(!fStartCompleteNotified) {
73                 char buffer[512];
74                 buffer[0] = '\0';
75                 if(!strerror_r(error, buffer, sizeof(buffer)))
76                         sd_notifyf(0, "STATUS=Failed to start up: %s\nERRNO=%d", buffer, error);
77                 else
78                         sd_notifyf(0, "STATUS=Failed to start up: (no message)\nERRNO=%d", error);
79         }
80 }