9312c730e3de4ffcb1078dd797a767b547d2941b
[platform/core/security/key-manager.git] / src / manager / common / descriptor-set.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       descriptor-set.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include "descriptor-set.h"
23 #include <dpl/log/log.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 namespace CKM {
28
29 DescriptorSet::DescriptorSet() : m_dirty(true), m_fds(NULL) {
30 }
31
32 DescriptorSet::~DescriptorSet() {
33     purge();
34 }
35
36 void DescriptorSet::purge() {
37     for(auto it:m_descriptors)
38         close(it.first);
39     m_descriptors.clear();
40 }
41
42 void DescriptorSet::add(int fd, short events, Callback&& callback) {
43     // map operator[] requires empty DescriptorData constructor
44     auto it = m_descriptors.find(fd);
45     if (it == m_descriptors.end()) {
46         m_descriptors.insert(std::make_pair(fd,DescriptorData(events, std::move(callback))));
47     } else {
48         it->second.events = events;
49         it->second.callback = std::move(callback);
50     }
51     m_dirty = true;
52 }
53
54 void DescriptorSet::remove(int fd, bool close_fd) {
55     if (0 != m_descriptors.erase(fd)) {
56         if (close_fd)
57             close(fd);
58         m_dirty = true;
59     }
60 }
61
62 void DescriptorSet::wait(int timeout_ms) {
63     if(!rebuildPollfd())
64         return;
65
66     // wait
67     int ret = TEMP_FAILURE_RETRY(poll(m_fds, m_descriptors.size(), timeout_ms));
68     if (ret == 0) {
69         ThrowMsg(Timeout, "Poll timeout");
70     } else if (ret < 0) {
71         int err = errno;
72         ThrowMsg(InternalError, "Poll failed " << strerror(err));
73     }
74
75     notify(ret);
76 }
77
78 bool DescriptorSet::rebuildPollfd() {
79     if (m_dirty) {
80        delete[] m_fds;
81        m_fds = NULL;
82        if (m_descriptors.empty()) {
83            LogWarning("Nothing to wait for");
84            return false;
85        }
86
87        m_fds = new pollfd[m_descriptors.size()];
88        size_t idx = 0;
89        for(const auto& it : m_descriptors) {
90            m_fds[idx].fd = it.first;
91            m_fds[idx].events = it.second.events;
92            idx++;
93        }
94        m_dirty = false;
95     }
96     return true;
97 }
98
99 void DescriptorSet::notify(int descCount) {
100     size_t size = m_descriptors.size();
101     for(size_t idx = 0;idx < size;++idx) {
102         const pollfd& pfd = m_fds[idx];
103         if (pfd.revents == 0)
104             continue;
105
106         /*
107          * Descriptors can be added/removed inside observer callback but:
108          * 1. m_fds is not affected. It will be regenerated in next wait()
109          * 2. No m_descriptors iterator will be invalidated
110          * 3. m_descriptors size is stored in local variable
111          */
112         m_descriptors.at(pfd.fd).callback(pfd.fd, pfd.revents);
113         descCount--;
114
115         // no more descriptors to check
116         if (descCount == 0)
117             break;
118     }
119     if (descCount != 0)
120         ThrowMsg(InternalError, "Number of notified descriptors do not match");
121 }
122
123 } /* namespace CKM */