]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_ports.cpp
c304001075d3ad02443aa587cb6bf3f17e5d4680
[user/henk/code/inspircd.git] / src / socketengines / socketengine_ports.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "exitcodes.h"
23
24 #ifndef __sun
25 # error You need Solaris 10 or later to make use of this code.
26 #endif
27
28 #include <vector>
29 #include <string>
30 #include <map>
31 #include "inspircd.h"
32 #include "socketengine.h"
33 #include <port.h>
34 #include <iostream>
35 #include <ulimit.h>
36
37 /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports
38  */
39 namespace
40 {
41         /** These are used by ports to hold socket events
42          */
43         std::vector<port_event_t> events(16);
44         int EngineHandle;
45 }
46
47 /** Initialize ports engine
48  */
49 void SocketEngine::Init()
50 {
51         int max = ulimit(4, 0);
52         if (max > 0)
53         {
54                 MAX_DESCRIPTORS = max;
55         }
56         else
57         {
58                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
59                 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
60                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
61         }
62         EngineHandle = port_create();
63
64         if (EngineHandle == -1)
65         {
66                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
67                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
68                 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
69                 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
70                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
71         }
72 }
73
74 /** Shutdown the ports engine
75  */
76 void SocketEngine::Deinit()
77 {
78         SocketEngine::Close(EngineHandle);
79 }
80
81 void SocketEngine::RecoverFromFork()
82 {
83 }
84
85 static int mask_to_events(int event_mask)
86 {
87         int rv = 0;
88         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
89                 rv |= POLLRDNORM;
90         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
91                 rv |= POLLWRNORM;
92         return rv;
93 }
94
95 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
96 {
97         int fd = eh->GetFd();
98         if ((fd < 0) || (fd > GetMaxFds() - 1))
99                 return false;
100
101         if (!SocketEngine::AddFdRef(eh))
102                 return false;
103
104         eh->SetEventMask(event_mask);
105         port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
106
107         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
108         ResizeDouble(events);
109
110         return true;
111 }
112
113 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
114 {
115         if (mask_to_events(new_mask) != mask_to_events(old_mask))
116                 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
117 }
118
119 void SocketEngine::DelFd(EventHandler* eh)
120 {
121         int fd = eh->GetFd();
122         if ((fd < 0) || (fd > GetMaxFds() - 1))
123                 return;
124
125         port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
126
127         SocketEngine::DelFdRef(eh);
128
129         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
130 }
131
132 int SocketEngine::DispatchEvents()
133 {
134         struct timespec poll_time;
135
136         poll_time.tv_sec = 1;
137         poll_time.tv_nsec = 0;
138
139         unsigned int nget = 1; // used to denote a retrieve request.
140         int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
141         ServerInstance->UpdateTime();
142
143         // first handle an error condition
144         if (ret == -1)
145                 return -1;
146
147         stats.TotalEvents += nget;
148
149         unsigned int i;
150         for (i = 0; i < nget; i++)
151         {
152                 port_event_t& ev = events[i];
153
154                 if (ev.portev_source != PORT_SOURCE_FD)
155                         continue;
156
157                 // Copy these in case the vector gets resized and ev invalidated
158                 const int fd = ev.portev_object;
159                 const int portev_events = ev.portev_events;
160                 EventHandler* eh = static_cast<EventHandler*>(ev.portev_user);
161                 if (eh->GetFd() < 0)
162                         continue;
163
164                 int mask = eh->GetEventMask();
165                 if (portev_events & POLLWRNORM)
166                         mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
167                 if (portev_events & POLLRDNORM)
168                         mask &= ~FD_READ_WILL_BLOCK;
169                 // reinsert port for next time around, pretending to be one-shot for writes
170                 eh->SetEventMask(mask);
171                 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
172                 if (portev_events & POLLRDNORM)
173                 {
174                         stats.ReadEvents++;
175                         eh->HandleEvent(EVENT_READ);
176                         if (eh != GetRef(fd))
177                                 continue;
178                 }
179                 if (portev_events & POLLWRNORM)
180                 {
181                         stats.WriteEvents++;
182                         eh->HandleEvent(EVENT_WRITE);
183                 }
184         }
185
186         return (int)i;
187 }