12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#include "socket_server.h"
172498b56bSopenharmony_ci
182498b56bSopenharmony_ci#include <cstdint>
192498b56bSopenharmony_ci#include <chrono>
202498b56bSopenharmony_ci#include <iosfwd>
212498b56bSopenharmony_ci#include <poll.h>
222498b56bSopenharmony_ci#include <securec.h>
232498b56bSopenharmony_ci#include <string>
242498b56bSopenharmony_ci#include <sys/socket.h>
252498b56bSopenharmony_ci#include <sys/un.h>
262498b56bSopenharmony_ci#include <unistd.h>
272498b56bSopenharmony_ci
282498b56bSopenharmony_ci#include "hilog_common.h"
292498b56bSopenharmony_ci
302498b56bSopenharmony_ciextern "C" {
312498b56bSopenharmony_ci#include "init_socket.h"
322498b56bSopenharmony_ci}
332498b56bSopenharmony_ci
342498b56bSopenharmony_cinamespace OHOS {
352498b56bSopenharmony_cinamespace HiviewDFX {
362498b56bSopenharmony_ciSocketServer::SocketServer(const std::string& _socketName, uint32_t socketType)
372498b56bSopenharmony_ci    : socketHandler(0), socketType(socketType), socketName(_socketName)
382498b56bSopenharmony_ci{
392498b56bSopenharmony_ci    serverAddr.sun_family = AF_UNIX;
402498b56bSopenharmony_ci
412498b56bSopenharmony_ci    std::string sockPath(SOCKET_FILE_DIR);
422498b56bSopenharmony_ci    sockPath += socketName;
432498b56bSopenharmony_ci    if (strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), sockPath.c_str()) != EOK) {
442498b56bSopenharmony_ci        return;
452498b56bSopenharmony_ci    }
462498b56bSopenharmony_ci}
472498b56bSopenharmony_ci
482498b56bSopenharmony_ciint SocketServer::Init()
492498b56bSopenharmony_ci{
502498b56bSopenharmony_ci    if (socketName.length()) {
512498b56bSopenharmony_ci        socketHandler = GetControlSocket(socketName.c_str());
522498b56bSopenharmony_ci        if (socketHandler >= 0) {
532498b56bSopenharmony_ci            return socketHandler;
542498b56bSopenharmony_ci        }
552498b56bSopenharmony_ci    }
562498b56bSopenharmony_ci
572498b56bSopenharmony_ci    socketHandler = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
582498b56bSopenharmony_ci    if (socketHandler < 0) {
592498b56bSopenharmony_ci        return socketHandler;
602498b56bSopenharmony_ci    }
612498b56bSopenharmony_ci
622498b56bSopenharmony_ci    (void)unlink(serverAddr.sun_path);
632498b56bSopenharmony_ci
642498b56bSopenharmony_ci    int optval = 1;
652498b56bSopenharmony_ci    int ret = setsockopt(socketHandler, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
662498b56bSopenharmony_ci    if (ret < 0) {
672498b56bSopenharmony_ci        return ret;
682498b56bSopenharmony_ci    }
692498b56bSopenharmony_ci
702498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(bind(socketHandler, (struct sockaddr *)&serverAddr, sizeof(sockaddr_un)));
712498b56bSopenharmony_ci}
722498b56bSopenharmony_ci
732498b56bSopenharmony_ciint SocketServer::Recv(void *buffer, unsigned int bufferLen, int flags)
742498b56bSopenharmony_ci{
752498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(recv(socketHandler, buffer, bufferLen, flags));
762498b56bSopenharmony_ci}
772498b56bSopenharmony_ci
782498b56bSopenharmony_ciint SocketServer::RecvMsg(struct msghdr *hdr, int flags)
792498b56bSopenharmony_ci{
802498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(recvmsg(socketHandler, hdr, flags));
812498b56bSopenharmony_ci}
822498b56bSopenharmony_ci
832498b56bSopenharmony_ciint SocketServer::Listen(unsigned int backlog)
842498b56bSopenharmony_ci{
852498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(listen(socketHandler, backlog));
862498b56bSopenharmony_ci}
872498b56bSopenharmony_ci
882498b56bSopenharmony_ciint SocketServer::Poll(short inEvent, short& outEvent, const std::chrono::milliseconds& timeout)
892498b56bSopenharmony_ci{
902498b56bSopenharmony_ci    pollfd info {socketHandler, inEvent, outEvent};
912498b56bSopenharmony_ci    int result = TEMP_FAILURE_RETRY(poll(&info, 1, timeout.count()));
922498b56bSopenharmony_ci    outEvent = info.revents;
932498b56bSopenharmony_ci    return result;
942498b56bSopenharmony_ci}
952498b56bSopenharmony_ci
962498b56bSopenharmony_ciint SocketServer::Accept()
972498b56bSopenharmony_ci{
982498b56bSopenharmony_ci    socklen_t addressSize = static_cast<socklen_t>(sizeof(serverAddr));
992498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(accept(socketHandler, (struct sockaddr*)&serverAddr, &addressSize));
1002498b56bSopenharmony_ci}
1012498b56bSopenharmony_ci
1022498b56bSopenharmony_ciSocketServer::~SocketServer()
1032498b56bSopenharmony_ci{
1042498b56bSopenharmony_ci    close(socketHandler);
1052498b56bSopenharmony_ci    socketHandler = -1;
1062498b56bSopenharmony_ci}
1072498b56bSopenharmony_ci} // namespace HiviewDFX
1082498b56bSopenharmony_ci} // namespace OHOS
109