2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "launchpad-common/server_socket.hh"
22 #include <sys/socket.h>
23 #include <sys/types.h>
27 #include "launchpad-common/exception.hh"
28 #include "launchpad-common/log_private.hh"
32 ServerSocket::ServerSocket() {
33 fd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
36 _E("socket() is failed. errno(%d)", errno);
41 ServerSocket::ServerSocket(int fd) : fd_(fd) {}
43 ServerSocket::~ServerSocket() {
47 std::unique_ptr<ClientSocket> ServerSocket::Accept() {
48 struct sockaddr_un addr = { 0, };
49 socklen_t len = static_cast<socklen_t>(sizeof(struct sockaddr_un));
50 auto* addr_ptr = reinterpret_cast<struct sockaddr*>(&addr);
51 int client_fd = accept(GetFd(), addr_ptr, &len);
52 if (client_fd == -1) {
53 _E("accept() is failed. errno(%d)", errno);
57 return std::unique_ptr<ClientSocket>(new ClientSocket(client_fd));
60 void ServerSocket::Bind(const std::string& bindpoint) {
61 struct sockaddr_un sockaddr = { 0, };
62 sockaddr.sun_family = AF_UNIX;
63 snprintf(sockaddr.sun_path, sizeof(sockaddr.sun_path), "%s",
65 struct sockaddr* sockaddr_ptr = reinterpret_cast<struct sockaddr*>(&sockaddr);
66 socklen_t len = static_cast<socklen_t>(sizeof(sockaddr));
68 unlink(bindpoint.c_str());
69 int ret = bind(GetFd(), sockaddr_ptr, len);
72 _E("bind() is failed. errno(%d)", errno);
77 void ServerSocket::Close() {
84 void ServerSocket::Listen(int backlog) {
85 int ret = listen(fd_, backlog);
88 _E("listen() is failed. errno(%d)", errno);
93 int ServerSocket::GetReceiveBufferSize() {
95 socklen_t len = sizeof(int);
96 int ret = getsockopt(fd_, SOL_SOCKET, SO_RCVBUF,
97 reinterpret_cast<void*>(&value), &len);
100 _E("getsockopt() is failed. errno(%d)", errno);
107 int ServerSocket::GetSendBufferSize() {
109 socklen_t len = sizeof(int);
110 int ret = getsockopt(fd_, SOL_SOCKET, SO_SNDBUF,
111 reinterpret_cast<void*>(&value), &len);
114 _E("getsockopt() is failed. errno(%d)", errno);
121 void ServerSocket::SetReceiveBufferSize(int size) {
122 socklen_t len = sizeof(size);
123 int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, len);
126 _E("setsockopt() is failed. errno(%d)", errno);
131 void ServerSocket::SetSendBufferSize(int size) {
132 socklen_t len = sizeof(size);
133 int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, len);
136 _E("setsockopt() is failed. errno(%d)", errno);
141 bool ServerSocket::IsClosed() const {
145 int ServerSocket::GetFd() const {
149 int ServerSocket::RemoveFd() {
155 void ServerSocket::SetCloseOnExec(bool do_close) {
156 int flags = fcntl(fd_, F_GETFD);
157 int ret = fcntl(fd_, F_SETFD,
158 do_close ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
160 _E("fcntl() is failed. errno(%d)", errno);
163 } // namespace launchpad