summaryrefslogtreecommitdiff
path: root/src/server/main/include/socket-manager.h
blob: f5683de214624da80a84f586cc99d824212f5a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2014-2022 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * This file is licensed under the terms of MIT License or the Apache License
 * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
 * See the LICENSE file or the notice below for Apache License Version 2.0
 * details.
 *
 * 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        socket-manager.h
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @version     1.0
 * @brief       SocketManager implementation.
 */

#pragma once

#include <vector>
#include <queue>
#include <string>
#include <mutex>
#include <thread>

#include <dpl/exception.h>

namespace SecurityManager {

class Service;

class SocketManager final {
public:
    struct ServiceDescription {
        std::string serviceHandlerPath; // Path to file
        std::string smackLabel;         // Smack label for socket
    };

    struct ConnectionID {
        int sock;          // Socket file descriptor number.
        unsigned counter;  // Unique counter per that descriptor number.
    };

    class Exception {
    public:
        DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
        DECLARE_EXCEPTION_TYPE(Base, InitFailed)
    };
    SocketManager();
    ~SocketManager();
    void MainLoop();

    void RegisterSocketService(Service &service);
    void Close(ConnectionID connectionID);
    void Write(ConnectionID connectionID, MessageBuffer &&messageBuffer);

private:
    void CreateDomainSocket(const ServiceDescription &desc);
    int CreateDomainSocketHelp(const ServiceDescription &desc);
    int GetSocketFromSystemD(const ServiceDescription &desc);

    void ReadyForRead(int sock);
    void ReadyForWrite(int sock);
    void ReadyForAccept();
    bool GotSigTerm() const;
    void ProcessQueue(void);
    void NotifyMe(void);
    void CloseSocket(int sock);

    void RegisterFdForReading(int fd);

    struct SocketDescription {
        bool isOpen = false;
        bool isTimeout = false;
        unsigned counter = 0;
        time_t timeout = 0;
        MessageBuffer buffer;
    };

    void CreateDefaultReadSocketDescription(int sock);

    typedef std::vector<SocketDescription> SocketDescriptionVector;

    struct WriteBuffer {
        ConnectionID connectionID;
        MessageBuffer buffer;
    };

    struct Timeout {
        time_t time;
        int sock;
        bool operator<(const Timeout &second) const {
            return time > second.time; // mininum first!
        }
    };

    SocketDescriptionVector m_socketDescriptionVector;
    Service *m_service = nullptr;
    fd_set m_readSet;
    fd_set m_writeSet;
    int m_maxDesc = 0;
    int m_signalFd, m_listenSock = -1, m_notifyMe;
    std::mutex m_eventQueueMutex;
    std::queue<WriteBuffer> m_writeBufferQueue;
    std::queue<ConnectionID> m_closeQueue;
    std::priority_queue<Timeout> m_timeoutQueue;
};

} // namespace SecurityManager