2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
22 #include "exitcodes.h"
25 # error You need Solaris 10 or later to make use of this code.
33 /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports
37 /** These are used by ports to hold socket events
39 std::vector<port_event_t> events(16);
43 /** Initialize ports engine
45 void SocketEngine::Init()
47 // MAX_DESCRIPTORS is mainly used for display purposes, no problem if ulimit() fails and returns a negative number
48 MAX_DESCRIPTORS = ulimit(4, 0);
50 EngineHandle = port_create();
52 if (EngineHandle == -1)
54 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
55 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
56 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
57 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
58 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
62 /** Shutdown the ports engine
64 void SocketEngine::Deinit()
66 SocketEngine::Close(EngineHandle);
69 void SocketEngine::RecoverFromFork()
73 static int mask_to_events(int event_mask)
76 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
78 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
83 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
89 if (!SocketEngine::AddFdRef(eh))
92 eh->SetEventMask(event_mask);
93 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
95 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
101 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
103 if (mask_to_events(new_mask) != mask_to_events(old_mask))
104 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
107 void SocketEngine::DelFd(EventHandler* eh)
109 int fd = eh->GetFd();
113 port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
115 SocketEngine::DelFdRef(eh);
117 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
120 int SocketEngine::DispatchEvents()
122 struct timespec poll_time;
124 poll_time.tv_sec = 1;
125 poll_time.tv_nsec = 0;
127 unsigned int nget = 1; // used to denote a retrieve request.
128 int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
129 ServerInstance->UpdateTime();
131 // first handle an error condition
135 stats.TotalEvents += nget;
138 for (i = 0; i < nget; i++)
140 port_event_t& ev = events[i];
142 if (ev.portev_source != PORT_SOURCE_FD)
145 // Copy these in case the vector gets resized and ev invalidated
146 const int fd = ev.portev_object;
147 const int portev_events = ev.portev_events;
148 EventHandler* eh = static_cast<EventHandler*>(ev.portev_user);
152 int mask = eh->GetEventMask();
153 if (portev_events & POLLWRNORM)
154 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
155 if (portev_events & POLLRDNORM)
156 mask &= ~FD_READ_WILL_BLOCK;
157 // reinsert port for next time around, pretending to be one-shot for writes
158 eh->SetEventMask(mask);
159 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
160 if (portev_events & POLLRDNORM)
162 eh->OnEventHandlerRead();
163 if (eh != GetRef(fd))
166 if (portev_events & POLLWRNORM)
168 eh->OnEventHandlerWrite();