1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
45 #include "inspstring.h"
47 #include "helperfuncs.h"
50 #include "cull_list.h"
52 extern InspIRCd* ServerInstance;
53 extern user_hash clientlist;
55 bool CullList::IsValid(userrec* user)
58 std::map<userrec*,time_t>::iterator es = exempt.find(user);
59 if (es != exempt.end())
62 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
67 * Because there is an undetermined period of time between a user existing,
68 * and this function being called, we have to check for the following condition:
70 * Between CullList::AddItem(u) being called, and CullList::IsValid(u) being called,
71 * the user with the pointer u has quit, but only to be REPLACED WITH A NEW USER WHO
72 * BECAUSE OF ALLOCATION RULES, HAS THE SAME MEMORY ADDRESS! To prevent this, we
73 * cross reference each pointer to the user's signon time, and if the signon times
74 * do not match, we return false here to indicate this user is NOT valid as it
75 * seems to differ from the pointer snapshot we got a few seconds earlier. Should
76 * prevent a few random crashes during netsplits.
78 if (user == u->second)
79 return (u->second->signon == esignon);
84 CullItem::CullItem(userrec* u, std::string &r)
90 CullItem::CullItem(userrec* u, const char* r)
100 userrec* CullItem::GetUser()
105 std::string& CullItem::GetReason()
116 void CullList::AddItem(userrec* user, std::string &reason)
118 if (exempt.find(user) == exempt.end())
120 CullItem item(user,reason);
121 list.push_back(item);
122 exempt[user] = user->signon;
126 void CullList::AddItem(userrec* user, const char* reason)
128 if (exempt.find(user) == exempt.end())
130 CullItem item(user,reason);
131 list.push_back(item);
132 exempt[user] = user->signon;
136 int CullList::Apply()
141 std::vector<CullItem>::iterator a = list.begin();
142 userrec* u = a->GetUser();
143 /* Because ServerInstance->DoOneIteration can
144 * take the user away from us in the middle of
145 * our operation, we should check to see if this
146 * pointer is still valid by iterating the hash.
147 * It's expensive, yes, but the DoOneIteration
148 * call stops it being horrendously bad.
152 kill_link(u,a->GetReason().c_str());
153 list.erase(list.begin());
154 /* So that huge numbers of quits dont block,
155 * we yield back to our mainloop every 15
157 * The DoOneIteration call basically acts
158 * like a software threading mechanism.
160 if (((n++) % 15) == 0)
162 ServerInstance->DoOneIteration(false);