]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_kqueue.cpp
Close logfile on rehash and reopen (it was only doing this on sighup for some reason)
[user/henk/code/inspircd.git] / src / socketengine_kqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "exitcodes.h"
16 #include <sys/types.h>
17 #include <sys/event.h>
18 #include <sys/time.h>
19 #include "socketengine_kqueue.h"
20
21
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 {
24         EngineHandle = kqueue();
25         if (EngineHandle == -1)
26         {
27                 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
28                 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
29                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30                 printf("ERROR: this is a fatal error, exiting now.");
31                 InspIRCd::Exit(EXIT_STATUS_SOCKETENGINE);
32         }
33         CurrentSetSize = 0;
34 }
35
36 KQueueEngine::~KQueueEngine()
37 {
38         ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
39         close(EngineHandle);
40 }
41
42 bool KQueueEngine::AddFd(EventHandler* eh)
43 {
44         int fd = eh->GetFd();
45
46         ServerInstance->Log(DEBUG,"KQueueEngine::AddFd(%d)",fd);
47
48         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
49         {
50                 ServerInstance->Log(DEBUG,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
51                 return false;
52         }
53         if (GetRemainingFds() <= 1)
54         {
55                 ServerInstance->Log(DEBUG,"ERROR: System out of file descriptors!");
56                 return false;
57         }
58
59         if (ref[fd])
60         {
61                 ServerInstance->Log(DEBUG,"ERROR: Slot already occupied");
62                 return false;
63         }
64
65         ref[fd] = eh;
66         ServerInstance->Log(DEBUG,"Add socket %d",fd);
67
68         struct kevent ke;
69         ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
70         EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
71
72         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
73         if (i == -1)
74         {
75                 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
76                 return false;
77         }
78
79         CurrentSetSize++;
80         return true;
81 }
82
83 bool KQueueEngine::DelFd(EventHandler* eh)
84 {
85         int fd = eh->GetFd();
86
87         ServerInstance->Log(DEBUG,"KQueueEngine::DelFd(%d)",fd);
88
89         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
90                 return false;
91
92         struct kevent ke;
93         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
94
95         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
96         
97         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
98
99         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
100
101         if ((j < 0) && (i < 0))
102                 return false;
103
104         CurrentSetSize--;
105         ref[fd] = NULL;
106
107         return true;
108 }
109
110 void KQueueEngine::WantWrite(EventHandler* eh)
111 {
112         /** When changing an item in a kqueue, there is no 'modify' call
113          * as in epoll. Instead, we add the item again, and this overwrites
114          * the original setting rather than adding it twice. See man kqueue.
115          */
116         struct kevent ke;
117         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
118         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
119         if (i == -1)
120         {
121                 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d for wanting write", eh->GetFd());
122         }
123 }
124
125 int KQueueEngine::GetMaxFds()
126 {
127         return MAX_DESCRIPTORS;
128 }
129
130 int KQueueEngine::GetRemainingFds()
131 {
132         return MAX_DESCRIPTORS - CurrentSetSize;
133 }
134
135 int KQueueEngine::DispatchEvents()
136 {
137         ts.tv_nsec = 5000L;
138         ts.tv_sec = 0;
139         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
140         for (int j = 0; j < i; j++)
141         {
142                 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ke_list[j].flags & EVFILT_WRITE ? "write" : "read", ke_list[j].ident);
143                 if (ke_list[j].flags & EV_EOF)
144                 {
145                         ServerInstance->Log(DEBUG,"kqueue: Error on FD %d", ke_list[j].ident);
146                         /* We love you kqueue, oh yes we do *sings*!
147                          * kqueue gives us the error number directly in the EOF state!
148                          * Unlike smelly epoll and select, where we have to getsockopt
149                          * to get the error, this saves us time and cpu cycles. Go BSD!
150                          */
151                         if (ref[ke_list[j].ident])
152                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
153                         continue;
154                 }
155                 if (ke_list[j].flags & EVFILT_WRITE)
156                 {
157                         /* This looks wrong but its right. As above, theres no modify 
158                          * call in kqueue. See the manpage.
159                          */
160                         struct kevent ke;
161                         EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
162                         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
163                         if (i == -1)
164                         {
165                                 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d back to just wanting to read!", ke_list[j].ident);
166                         }
167                         if (ref[ke_list[j].ident])
168                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
169                 }
170                 else
171                 {
172                         if (ref[ke_list[j].ident])
173                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
174                 }
175         }
176
177         return i;
178 }
179
180 std::string KQueueEngine::GetName()
181 {
182         return "kqueue";
183 }