1 /******************************************************************
3 * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
5 * Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 #include <systemd/sd-daemon.h>
25 #include "serviceadapter.h"
28 service_adapter::service_adapter()
32 service_adapter::~service_adapter()
36 boost::asio::local::stream_protocol::acceptor service_adapter::create_platform_socket_acceptor(boost::asio::io_service& io_service)
38 LOGD("Try to get socket from systemd");
40 int n = sd_listen_fds(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);
46 return boost::asio::local::stream_protocol::acceptor(
48 boost::asio::local::stream_protocol(),
54 LOGE("No systemd sockets found");
56 throw std::runtime_error("No socket created by systemd");
59 void service_adapter::notify_start_complete()
61 LOGD("Notify start completed to systemd");
64 sd_notify(0, "READY=1");
65 fStartCompleteNotified = true;
68 void service_adapter::notify_start_failure(int error)
70 LOGE("Notify start failure");
72 if(!fStartCompleteNotified) {
75 if(!strerror_r(error, buffer, sizeof(buffer)))
76 sd_notifyf(0, "STATUS=Failed to start up: %s\nERRNO=%d", buffer, error);
78 sd_notifyf(0, "STATUS=Failed to start up: (no message)\nERRNO=%d", error);