]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_ports.cpp
Change the number reported by SocketEngine::GetMaxFds() to be informal
[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         // MAX_DESCRIPTORS is mainly used for display purposes, no problem if ulimit() fails and returns a negative number
52         MAX_DESCRIPTORS = ulimit(4, 0);
53
54         EngineHandle = port_create();
55
56         if (EngineHandle == -1)
57         {
58                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
59                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
60                 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
61                 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
62                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
63         }
64 }
65
66 /** Shutdown the ports engine
67  */
68 void SocketEngine::Deinit()
69 {
70         SocketEngine::Close(EngineHandle);
71 }
72
73 void SocketEngine::RecoverFromFork()
74 {
75 }
76
77 static int mask_to_events(int event_mask)
78 {
79         int rv = 0;
80         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
81                 rv |= POLLRDNORM;
82         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
83                 rv |= POLLWRNORM;
84         return rv;
85 }
86
87 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
88 {
89         int fd = eh->GetFd();
90         if (fd < 0)
91                 return false;
92
93         if (!SocketEngine::AddFdRef(eh))
94                 return false;
95
96         eh->SetEventMask(event_mask);
97         port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
98
99         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
100         ResizeDouble(events);
101
102         return true;
103 }
104
105 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
106 {
107         if (mask_to_events(new_mask) != mask_to_events(old_mask))
108                 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
109 }
110
111 void SocketEngine::DelFd(EventHandler* eh)
112 {
113         int fd = eh->GetFd();
114         if (fd < 0)
115                 return;
116
117         port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
118
119         SocketEngine::DelFdRef(eh);
120
121         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
122 }
123
124 int SocketEngine::DispatchEvents()
125 {
126         struct timespec poll_time;
127
128         poll_time.tv_sec = 1;
129         poll_time.tv_nsec = 0;
130
131         unsigned int nget = 1; // used to denote a retrieve request.
132         int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
133         ServerInstance->UpdateTime();
134
135         // first handle an error condition
136         if (ret == -1)
137                 return -1;
138
139         stats.TotalEvents += nget;
140
141         unsigned int i;
142         for (i = 0; i < nget; i++)
143         {
144                 port_event_t& ev = events[i];
145
146                 if (ev.portev_source != PORT_SOURCE_FD)
147                         continue;
148
149                 // Copy these in case the vector gets resized and ev invalidated
150                 const int fd = ev.portev_object;
151                 const int portev_events = ev.portev_events;
152                 EventHandler* eh = static_cast<EventHandler*>(ev.portev_user);
153                 if (eh->GetFd() < 0)
154                         continue;
155
156                 int mask = eh->GetEventMask();
157                 if (portev_events & POLLWRNORM)
158                         mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
159                 if (portev_events & POLLRDNORM)
160                         mask &= ~FD_READ_WILL_BLOCK;
161                 // reinsert port for next time around, pretending to be one-shot for writes
162                 eh->SetEventMask(mask);
163                 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
164                 if (portev_events & POLLRDNORM)
165                 {
166                         stats.ReadEvents++;
167                         eh->HandleEvent(EVENT_READ);
168                         if (eh != GetRef(fd))
169                                 continue;
170                 }
171                 if (portev_events & POLLWRNORM)
172                 {
173                         stats.WriteEvents++;
174                         eh->HandleEvent(EVENT_WRITE);
175                 }
176         }
177
178         return (int)i;
179 }