diff options
author | Adam <Adam@anope.org> | 2011-11-08 17:29:16 -0500 |
---|---|---|
committer | Adam <Adam@anope.org> | 2011-11-08 17:29:16 -0500 |
commit | b5ff856f47d8e54d12c568462a06351633c29610 (patch) | |
tree | a4e2f96c59ee49aa5e6cacdfd30db6155151ad36 /src | |
parent | 97b9055f92f21cd91af44a3d5dacce0024536cff (diff) |
Windows
Diffstat (limited to 'src')
38 files changed, 943 insertions, 735 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3c21ba477..a51bc3057 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,24 +1,24 @@ # Find all the *.cpp files within the current source directory, and sort the list file(GLOB SRC_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp") -# If using Windows, add the windows.cpp, the win32 threading engine, and the socket engine to the list if(WIN32) + append_to_list(SRC_SRCS win32/dir/dir.cpp) + append_to_list(SRC_SRCS win32/socket.cpp) append_to_list(SRC_SRCS win32/windows.cpp) + append_to_list(SRC_SRCS win32/dl/dl.cpp) + append_to_list(SRC_SRCS win32/pipe/pipe.cpp) + append_to_list(SRC_SRCS win32/pthread/pthread.cpp) append_to_list(SRC_SRCS win32/sigaction/sigaction.cpp) - append_to_list(SRC_SRCS threadengines/threadengine_win32.cpp) - append_to_list(SRC_SRCS socketengines/pipeengine_win32.cpp) -# If not using Windows, add the pthread threading engine to the list -else(WIN32) - append_to_list(SRC_SRCS threadengines/threadengine_pthread.cpp) - # If we have eventfd, use it - if(HAVE_EVENTFD) - append_to_list(SRC_SRCS socketengines/pipeengine_eventfd.cpp) - # Else fall back to pipe - else(HAVE_EVENTFD) - append_to_list(SRC_SRCS socketengines/pipeengine_pipe.cpp) - endif(HAVE_EVENTFD) endif(WIN32) +# If we have eventfd, use it +if(HAVE_EVENTFD) + append_to_list(SRC_SRCS socketengines/pipeengine_eventfd.cpp) +# Else fall back to pipe +else(HAVE_EVENTFD) + append_to_list(SRC_SRCS socketengines/pipeengine_pipe.cpp) +endif(HAVE_EVENTFD) + if(HAVE_EPOLL) append_to_list(SRC_SRCS socketengines/socketengine_epoll.cpp) else(HAVE_EPOLL) diff --git a/src/access.cpp b/src/access.cpp index 74a376b3d..626176e7b 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -71,7 +71,7 @@ AccessProvider::~AccessProvider() { } -ChanAccess::ChanAccess(AccessProvider *p) : provider(p) +ChanAccess::ChanAccess(AccessProvider *p) : Serializable("ChanAccess"), provider(p) { } @@ -79,6 +79,40 @@ ChanAccess::~ChanAccess() { } +Serializable::serialized_data ChanAccess::serialize() +{ + serialized_data data; + + data["provider"] << this->provider->name; + data["ci"] << this->ci->name; + data["mask"] << this->mask; + data["creator"] << this->creator; + data["last_seen"].setType(Serialize::DT_INT) << this->last_seen; + data["created"].setType(Serialize::DT_INT) << this->created; + data["data"] << this->Serialize(); + + return data; +} + +void ChanAccess::unserialize(serialized_data &data) +{ + service_reference<AccessProvider> aprovider(data["provider"].astr()); + ChannelInfo *ci = cs_findchan(data["ci"].astr()); + if (!aprovider || !ci) + return; + + ChanAccess *access = aprovider->Create(); + access->provider = aprovider; + access->ci = ci; + data["mask"] >> access->mask; + data["creator"] >> access->creator; + data["last_seen"] >> access->last_seen; + data["created"] >> access->created; + access->Unserialize(data["data"].astr()); + + ci->AddAccess(access); +} + bool ChanAccess::operator>(ChanAccess &other) { const std::vector<Privilege> &privs = PrivilegeManager::GetPrivileges(); diff --git a/src/base.cpp b/src/base.cpp index e45e6365c..08d5c6e63 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -1,20 +1,17 @@ #include "services.h" #include "modules.h" +#include "oper.h" -std::vector<SerializableBase *> serialized_types; -std::list<SerializableBase *> *serialized_items; +std::vector<Anope::string> SerializeType::type_order; +Anope::map<SerializeType *> SerializeType::types; +std::list<Serializable *> Serializable::serizliable_items; void RegisterTypes() { - Serializable<NickCore>::Alloc.Register("NickCore"); - Serializable<NickAlias>::Alloc.Register("NickAlias"); - Serializable<BotInfo>::Alloc.Register("BotInfo"); - Serializable<ChannelInfo>::Alloc.Register("ChannelInfo"); - Serializable<LogSetting>::Alloc.Register("LogSetting"); - Serializable<ModeLock>::Alloc.Register("ModeLock"); - Serializable<AutoKick>::Alloc.Register("AutoKick"); - Serializable<BadWord>::Alloc.Register("BadWord"); - Serializable<Memo>::Alloc.Register("Memo"); + static SerializeType nc("NickCore", NickCore::unserialize), na("NickAlias", NickAlias::unserialize), bi("BotInfo", BotInfo::unserialize), + ci("ChannelInfo", ChannelInfo::unserialize), access("ChanAccess", ChanAccess::unserialize), logsetting("LogSetting", LogSetting::unserialize), + modelock("ModeLock", ModeLock::unserialize), akick("AutoKick", AutoKick::unserialize), badword("BadWord", BadWord::unserialize), + memo("Memo", Memo::unserialize), xline("XLine", XLine::unserialize); } Base::Base() diff --git a/src/bots.cpp b/src/bots.cpp index a10b7b147..864201f8c 100644 --- a/src/bots.cpp +++ b/src/bots.cpp @@ -13,7 +13,7 @@ Anope::insensitive_map<BotInfo *> BotListByNick; Anope::map<BotInfo *> BotListByUID; -BotInfo::BotInfo(const Anope::string &nnick, const Anope::string &nuser, const Anope::string &nhost, const Anope::string &nreal, const Anope::string &bmodes) : User(nnick, nuser, nhost, ts6_uid_retrieve()), Flags<BotFlag, BI_END>(BotFlagString), botmodes(bmodes) +BotInfo::BotInfo(const Anope::string &nnick, const Anope::string &nuser, const Anope::string &nhost, const Anope::string &nreal, const Anope::string &bmodes) : User(nnick, nuser, nhost, ts6_uid_retrieve()), Flags<BotFlag, BI_END>(BotFlagString), Serializable("BotInfo"), botmodes(bmodes) { this->realname = nreal; this->server = Me; @@ -64,9 +64,9 @@ BotInfo::~BotInfo() BotListByUID.erase(this->uid); } -SerializableBase::serialized_data BotInfo::serialize() +Serializable::serialized_data BotInfo::serialize() { - SerializableBase::serialized_data data; + serialized_data data; data["nick"] << this->nick; data["user"] << this->ident; @@ -79,7 +79,7 @@ SerializableBase::serialized_data BotInfo::serialize() return data; } -void BotInfo::unserialize(SerializableBase::serialized_data &data) +void BotInfo::unserialize(serialized_data &data) { BotInfo *bi = findbot(data["nick"].astr()); if (bi == NULL) diff --git a/src/encrypt.cpp b/src/encrypt.cpp index 0f3bd3d79..2f052be4e 100644 --- a/src/encrypt.cpp +++ b/src/encrypt.cpp @@ -35,7 +35,7 @@ bool enc_decrypt(const Anope::string &src, Anope::string &dest) if (pos == Anope::string::npos) { Log() << "Error: enc_decrypt() called with invalid password string (" << src << ")"; - return -1; + return false; } Anope::string hashm(src.begin(), src.begin() + pos); diff --git a/src/hashcomp.cpp b/src/hashcomp.cpp index cfffd5a10..0095ec74b 100644 --- a/src/hashcomp.cpp +++ b/src/hashcomp.cpp @@ -113,7 +113,7 @@ const char *ci::ci_char_traits::find(const char *s1, int n, char c) * @param s2 The second string * @return true if s1 < s2, else false */ -bool std::less<ci::string>::operator()(const Anope::string &s1, const Anope::string &s2) const +bool ci::less::operator()(const Anope::string &s1, const Anope::string &s2) const { return s1.ci_str().compare(s2.ci_str()) < 0; } @@ -123,7 +123,7 @@ bool std::less<ci::string>::operator()(const Anope::string &s1, const Anope::str * @param s2 The second string * @return true if s1 < s2, else false */ -bool std::less<irc::string>::operator()(const Anope::string &s1, const Anope::string &s2) const +bool irc::less::operator()(const Anope::string &s1, const Anope::string &s2) const { return s1.irc_str().compare(s2.irc_str()) < 0; } diff --git a/src/init.cpp b/src/init.cpp index 4c4d3f93e..d54ad7fa5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -161,7 +161,11 @@ static void write_pidfile() FILE *pidfile = fopen(Config->PIDFilename.c_str(), "w"); if (pidfile) { +#ifdef _WIN32 + fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId())); +#else fprintf(pidfile, "%d\n", static_cast<int>(getpid())); +#endif fclose(pidfile); atexit(remove_pidfile); } @@ -458,10 +462,6 @@ void Init(int ac, char **av) rand_init(); add_entropy_userkeys(); -#ifdef _WIN32 - OnStartup(); -#endif - /* load modules */ Log() << "Loading modules..."; for (std::list<Anope::string>::iterator it = Config->ModulesAutoLoad.begin(), it_end = Config->ModulesAutoLoad.end(); it != it_end; ++it) diff --git a/src/logger.cpp b/src/logger.cpp index d8aed5ee7..9792c9be2 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -380,7 +380,7 @@ void LogInfo::ProcessMessage(const Log *l) Anope::string oldlog = CreateLogName(target, Anope::CurTime - 86400 * this->LogAge); if (IsFile(oldlog)) { - DeleteFile(oldlog.c_str()); + unlink(oldlog.c_str()); Log(LOG_DEBUG) << "Deleted old logfile " << oldlog; } } diff --git a/src/main.cpp b/src/main.cpp index b115137ea..03920424a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,10 +27,9 @@ #include "timers.h" #include "modules.h" -// getrlimit. -#ifndef _WIN32 -# include <sys/time.h> -# include <sys/resource.h> +#ifdef _WIN32 +# include <process.h> +# define execve _execve #endif /******** Global variables! ********/ @@ -47,11 +46,6 @@ bool noexpire = false; /* -noexpire */ bool protocoldebug = false; /* -protocoldebug */ Anope::string binary_dir; /* Used to store base path for Anope */ -#ifdef _WIN32 -# include <process.h> -# define execve _execve -#endif - /* Set to 1 if we are to quit */ bool quitting = false; int return_code = 0; @@ -316,6 +310,10 @@ int main(int ac, char **av, char **envp) /* Clean out the module runtime directory prior to running, just in case files were left behind during a previous run */ ModuleManager::CleanupRuntimeDirectory(); +#ifdef _WIN32 + OnStartup(); +#endif + try { /* General initialization first */ diff --git a/src/memoserv.cpp b/src/memoserv.cpp index 602db9897..8db4f0170 100644 --- a/src/memoserv.cpp +++ b/src/memoserv.cpp @@ -13,9 +13,9 @@ #include "modules.h" #include "memoserv.h" -Memo::Memo() : Flags<MemoFlag>(MemoFlagStrings) { } +Memo::Memo() : Flags<MemoFlag>(MemoFlagStrings), Serializable("Memo") { } -SerializableBase::serialized_data Memo::serialize() +Serializable::serialized_data Memo::serialize() { serialized_data data; @@ -28,7 +28,7 @@ SerializableBase::serialized_data Memo::serialize() return data; } -void Memo::unserialize(SerializableBase::serialized_data &data) +void Memo::unserialize(serialized_data &data) { if (!memoserv) return; diff --git a/src/misc.cpp b/src/misc.cpp index 5e899effd..0941c9bbc 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -49,35 +49,6 @@ bool IsFile(const Anope::string &filename) return false; } -/** - * toupper: Like the ANSI functions, but make sure we return an - * int instead of a (signed) char. - * @param c Char - * @return int - */ -int toupper(char c) -{ - if (islower(c)) - return static_cast<int>(c) - ('a' - 'A'); - else - return static_cast<int>(c); -} - -/*************************************************************************/ - -/** - * tolower: Like the ANSI functions, but make sure we return an - * int instead of a (signed) char. - * @param c Char - * @return int - */ -int tolower(char c) -{ - if (isupper(c)) - return static_cast<int>(c) + ('a' - 'A'); - else - return static_cast<int>(c); -} /*************************************************************************/ diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index 978a5b929..17cc08a53 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -17,52 +17,24 @@ void ModuleManager::CleanupRuntimeDirectory() Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment please wait"; -#ifndef _WIN32 DIR *dirp = opendir(dirbuf.c_str()); if (!dirp) { Log(LOG_DEBUG) << "Cannot open directory (" << dirbuf << ")"; return; } - struct dirent *dp; - while ((dp = readdir(dirp))) + + for (dirent *dp; (dp = readdir(dirp));) { if (!dp->d_ino) continue; if (Anope::string(dp->d_name).equals_cs(".") || Anope::string(dp->d_name).equals_cs("..")) continue; Anope::string filebuf = dirbuf + "/" + dp->d_name; - DeleteFile(filebuf.c_str()); - } - closedir(dirp); -#else - Anope::string szDir = dirbuf + "/*"; - - WIN32_FIND_DATA FileData; - HANDLE hList = FindFirstFile(szDir.c_str(), &FileData); - if (hList != INVALID_HANDLE_VALUE) - { - bool fFinished = false; - while (!fFinished) - { - if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - { - Anope::string filebuf = dirbuf + "/" + FileData.cFileName; - if (!DeleteFile(filebuf.c_str())) - Log(LOG_DEBUG) << "Error deleting file " << filebuf << " - GetLastError() reports " << Anope::LastError(); - } - if (!FindNextFile(hList, &FileData)) - { - if (GetLastError() == ERROR_NO_MORE_FILES) - fFinished = true; - } - } + unlink(filebuf.c_str()); } - else - Log(LOG_DEBUG) << "Invalid File Handle. GetLastError() reports "<< static_cast<int>(GetLastError()); - FindClose(hList); -#endif + closedir(dirp); } /** @@ -131,11 +103,11 @@ static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &out * This function will take a pointer from either dlsym or GetProcAddress and cast it in * a way that won't cause C++ warnings/errors to come up. */ -template <class TYPE> TYPE function_cast(ano_module_t symbol) +template <class TYPE> TYPE function_cast(void *symbol) { union { - ano_module_t symbol; + void *symbol; TYPE function; } cast; cast.symbol = symbol; @@ -160,19 +132,18 @@ ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u) if (ret != MOD_ERR_OK) return ret; - ano_modclearerr(); - - ano_module_t handle = dlopen(pbuf.c_str(), RTLD_LAZY); - const char *err = ano_moderr(); + dlerror(); + void *handle = dlopen(pbuf.c_str(), RTLD_LAZY); + const char *err = dlerror(); if (!handle && err && *err) { Log() << err; return MOD_ERR_NOLOAD; } - ano_modclearerr(); + dlerror(); Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit")); - err = ano_moderr(); + err = dlerror(); if (!func && err && *err) { Log() << "No init function found, not an Anope module"; @@ -305,15 +276,15 @@ ModuleReturn ModuleManager::DeleteModule(Module *m) if (!m || !m->handle) return MOD_ERR_PARAMS; - ano_module_t handle = m->handle; + void *handle = m->handle; Anope::string filename = m->filename; Log(LOG_DEBUG) << "Unloading module " << m->name; - ano_modclearerr(); + dlerror(); void (*destroy_func)(Module *m) = function_cast<void (*)(Module *)>(dlsym(m->handle, "AnopeFini")); - const char *err = ano_moderr(); - if (!destroy_func || err) + const char *err = dlerror(); + if (!destroy_func || (err && *err)) { Log() << "No destroy function found for " << m->name << ", chancing delete..."; delete m; /* we just have to chance they haven't overwrote the delete operator then... */ @@ -322,10 +293,10 @@ ModuleReturn ModuleManager::DeleteModule(Module *m) destroy_func(m); /* Let the module delete it self, just in case */ if (dlclose(handle)) - Log() << ano_moderr(); + Log() << dlerror(); if (!filename.empty()) - DeleteFile(filename.c_str()); + unlink(filename.c_str()); return MOD_ERR_OK; } diff --git a/src/nickalias.cpp b/src/nickalias.cpp index ed739653b..1ef03bb2b 100644 --- a/src/nickalias.cpp +++ b/src/nickalias.cpp @@ -5,7 +5,7 @@ * @param nick The nick * @param nickcore The nickcofe for this nick */ -NickAlias::NickAlias(const Anope::string &nickname, NickCore *nickcore) : Flags<NickNameFlag, NS_END>(NickNameFlagStrings) +NickAlias::NickAlias(const Anope::string &nickname, NickCore *nickcore) : Flags<NickNameFlag, NS_END>(NickNameFlagStrings), Serializable("NickAlias") { if (nickname.empty()) throw CoreException("Empty nick passed to NickAlias constructor"); @@ -103,7 +103,7 @@ void NickAlias::OnCancel(User *) } } -SerializableBase::serialized_data NickAlias::serialize() +Serializable::serialized_data NickAlias::serialize() { serialized_data data; @@ -128,7 +128,7 @@ SerializableBase::serialized_data NickAlias::serialize() return data; } -void NickAlias::unserialize(SerializableBase::serialized_data &data) +void NickAlias::unserialize(serialized_data &data) { NickCore *core = findcore(data["nc"].astr()); if (core == NULL) diff --git a/src/nickcore.cpp b/src/nickcore.cpp index 6c533fb61..05d627529 100644 --- a/src/nickcore.cpp +++ b/src/nickcore.cpp @@ -4,7 +4,7 @@ /** Default constructor * @param display The display nick */ -NickCore::NickCore(const Anope::string &coredisplay) : Flags<NickCoreFlag, NI_END>(NickCoreFlagStrings) +NickCore::NickCore(const Anope::string &coredisplay) : Flags<NickCoreFlag, NI_END>(NickCoreFlagStrings), Serializable("NickCore") { if (coredisplay.empty()) throw CoreException("Empty display passed to NickCore constructor"); @@ -45,7 +45,7 @@ NickCore::~NickCore() } } -SerializableBase::serialized_data NickCore::serialize() +Serializable::serialized_data NickCore::serialize() { serialized_data data; diff --git a/src/operserv.cpp b/src/operserv.cpp index 05d27d6ca..54230d6ab 100644 --- a/src/operserv.cpp +++ b/src/operserv.cpp @@ -15,18 +15,18 @@ /* List of XLine managers we check users against in XLineManager::CheckAll */ std::list<XLineManager *> XLineManager::XLineManagers; -std::map<Anope::string, XLine *, std::less<ci::string> > XLineManager::XLinesByUID; +std::map<Anope::string, XLine *, ci::less> XLineManager::XLinesByUID; -XLine::XLine() +XLine::XLine() : Serializable("XLine") { } -XLine::XLine(const Anope::string &mask, const Anope::string &reason, const Anope::string &uid) : Mask(mask), Created(0), Expires(0), Reason(reason), UID(uid) +XLine::XLine(const Anope::string &mask, const Anope::string &reason, const Anope::string &uid) : Serializable("XLine"), Mask(mask), Created(0), Expires(0), Reason(reason), UID(uid) { manager = NULL; } -XLine::XLine(const Anope::string &mask, const Anope::string &by, const time_t expires, const Anope::string &reason, const Anope::string &uid) : Mask(mask), By(by), Created(Anope::CurTime), Expires(expires), Reason(reason), UID(uid) +XLine::XLine(const Anope::string &mask, const Anope::string &by, const time_t expires, const Anope::string &reason, const Anope::string &uid) : Serializable("XLine"), Mask(mask), By(by), Created(Anope::CurTime), Expires(expires), Reason(reason), UID(uid) { manager = NULL; } @@ -73,7 +73,7 @@ sockaddrs XLine::GetIP() const return addr; } -SerializableBase::serialized_data XLine::serialize() +Serializable::serialized_data XLine::serialize() { serialized_data data; @@ -89,7 +89,7 @@ SerializableBase::serialized_data XLine::serialize() return data; } -void XLine::unserialize(SerializableBase::serialized_data &data) +void XLine::unserialize(serialized_data &data) { service_reference<XLineManager> xlm(data["manager"].astr()); if (!xlm) @@ -327,7 +327,7 @@ std::pair<int, XLine *> XLineManager::CanAdd(const Anope::string &mask, time_t e */ XLine *XLineManager::HasEntry(const Anope::string &mask) { - std::map<Anope::string, XLine *, std::less<ci::string> >::iterator it = XLinesByUID.find(mask); + std::map<Anope::string, XLine *, ci::less>::iterator it = XLinesByUID.find(mask); if (it != XLinesByUID.end() && (it->second->manager == NULL || it->second->manager == this)) return it->second; for (unsigned i = 0, end = this->XLines.size(); i < end; ++i) diff --git a/src/regchannel.cpp b/src/regchannel.cpp index be1562793..a7a2e2c22 100644 --- a/src/regchannel.cpp +++ b/src/regchannel.cpp @@ -12,7 +12,11 @@ #include "services.h" #include "modules.h" -SerializableBase::serialized_data BadWord::serialize() +BadWord::BadWord() : Serializable("BadWord") +{ +} + +Serializable::serialized_data BadWord::serialize() { serialized_data data; @@ -23,7 +27,7 @@ SerializableBase::serialized_data BadWord::serialize() return data; } -void BadWord::unserialize(SerializableBase::serialized_data &data) +void BadWord::unserialize(serialized_data &data) { ChannelInfo *ci = cs_findchan(data["ci"].astr()); if (!ci) @@ -35,7 +39,11 @@ void BadWord::unserialize(SerializableBase::serialized_data &data) ci->AddBadWord(data["word"].astr(), static_cast<BadWordType>(n)); } -SerializableBase::serialized_data AutoKick::serialize() +AutoKick::AutoKick() : Flags<AutoKickFlag>(AutoKickFlagString), Serializable("AutoKick") +{ +} + +Serializable::serialized_data AutoKick::serialize() { serialized_data data; @@ -53,7 +61,7 @@ SerializableBase::serialized_data AutoKick::serialize() return data; } -void AutoKick::unserialize(SerializableBase::serialized_data &data) +void AutoKick::unserialize(serialized_data &data) { ChannelInfo *ci = cs_findchan(data["ci"].astr()); if (ci == NULL) @@ -70,7 +78,15 @@ void AutoKick::unserialize(SerializableBase::serialized_data &data) ci->AddAkick(data["creator"].astr(), data["mask"].astr(), data["reason"].astr(), addtime, lastused); } -SerializableBase::serialized_data ModeLock::serialize() +ModeLock::ModeLock() : Serializable("ModeLock") +{ +} + +ModeLock::ModeLock(ChannelInfo *ch, bool s, ChannelModeName n, const Anope::string &p, const Anope::string &se, time_t c) : Serializable("ModeLock"), ci(ch), set(s), name(n), param(p), setter(se), created(c) +{ +} + +Serializable::serialized_data ModeLock::serialize() { serialized_data data; @@ -87,7 +103,7 @@ SerializableBase::serialized_data ModeLock::serialize() return data; } -void ModeLock::unserialize(SerializableBase::serialized_data &data) +void ModeLock::unserialize(serialized_data &data) { ChannelInfo *ci = cs_findchan(data["ci"].astr()); if (ci == NULL) @@ -113,7 +129,11 @@ void ModeLock::unserialize(SerializableBase::serialized_data &data) ci->mode_locks.insert(std::make_pair(ml.name, ml)); } -SerializableBase::serialized_data LogSetting::serialize() +LogSetting::LogSetting() : Serializable("LogSetting") +{ +} + +Serializable::serialized_data LogSetting::serialize() { serialized_data data; @@ -152,7 +172,7 @@ void LogSetting::unserialize(serialized_data &data) /** Default constructor * @param chname The channel name */ -ChannelInfo::ChannelInfo(const Anope::string &chname) : Flags<ChannelInfoFlag, CI_END>(ChannelInfoFlagStrings), botflags(BotServFlagStrings) +ChannelInfo::ChannelInfo(const Anope::string &chname) : Flags<ChannelInfoFlag, CI_END>(ChannelInfoFlagStrings), Serializable("ChannelInfo"), botflags(BotServFlagStrings) { if (chname.empty()) throw CoreException("Empty channel passed to ChannelInfo constructor"); @@ -197,7 +217,7 @@ ChannelInfo::ChannelInfo(const Anope::string &chname) : Flags<ChannelInfoFlag, C /** Copy constructor * @param ci The ChannelInfo to copy settings to */ -ChannelInfo::ChannelInfo(ChannelInfo &ci) : Flags<ChannelInfoFlag, CI_END>(ChannelInfoFlagStrings), botflags(BotServFlagStrings) +ChannelInfo::ChannelInfo(ChannelInfo &ci) : Flags<ChannelInfoFlag, CI_END>(ChannelInfoFlagStrings), Serializable("ChannelInfo"), botflags(BotServFlagStrings) { *this = ci; @@ -279,7 +299,7 @@ ChannelInfo::~ChannelInfo() --this->founder->channelcount; } -SerializableBase::serialized_data ChannelInfo::serialize() +Serializable::serialized_data ChannelInfo::serialize() { serialized_data data; @@ -319,7 +339,7 @@ SerializableBase::serialized_data ChannelInfo::serialize() return data; } -void ChannelInfo::unserialize(SerializableBase::serialized_data &data) +void ChannelInfo::unserialize(serialized_data &data) { ChannelInfo *ci = new ChannelInfo(data["name"].astr()); if (data.count("founder") > 0) diff --git a/src/socketengines/pipeengine_pipe.cpp b/src/socketengines/pipeengine_pipe.cpp index f2fa0876a..7901931c8 100644 --- a/src/socketengines/pipeengine_pipe.cpp +++ b/src/socketengines/pipeengine_pipe.cpp @@ -10,7 +10,7 @@ Pipe::Pipe() : Socket(-1), WritePipe(-1) flags = fcntl(fds[1], F_GETFL, 0); fcntl(fds[1], F_SETFL, flags | O_NONBLOCK); - this->~Socket(); + this->~Pipe(); this->Sock = fds[0]; this->WritePipe = fds[1]; @@ -20,13 +20,14 @@ Pipe::Pipe() : Socket(-1), WritePipe(-1) Pipe::~Pipe() { - CloseSocket(this->WritePipe); + if (this->WritePipe >= 0) + close(this->WritePipe); } bool Pipe::ProcessRead() { char dummy[512]; - while (read(this->GetFD(), &dummy, 512) == 512); + while (read(this->GetFD(), dummy, 512) == 512); this->OnNotify(); return true; } @@ -40,4 +41,3 @@ void Pipe::Notify() void Pipe::OnNotify() { } - diff --git a/src/socketengines/pipeengine_win32.cpp b/src/socketengines/pipeengine_win32.cpp deleted file mode 100644 index f34472870..000000000 --- a/src/socketengines/pipeengine_win32.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "services.h" - -Pipe::Pipe() : Socket(-1) -{ - sockaddrs localhost; - - localhost.pton(AF_INET, "127.0.0.1"); - - int cfd = socket(AF_INET, SOCK_STREAM, 0), lfd = socket(AF_INET, SOCK_STREAM, 0); - if (cfd == -1) - throw CoreException("Error accepting new socket for Pipe"); - - if (bind(lfd, &localhost.sa, localhost.size()) == -1) - throw CoreException("Error accepting new socket for Pipe"); - if (listen(lfd, 1) == -1) - throw CoreException("Error accepting new socket for Pipe"); - - sockaddrs lfd_addr; - socklen_t sz = sizeof(lfd_addr); - getsockname(lfd, &lfd_addr.sa, &sz); - - if (connect(cfd, &lfd_addr.sa, lfd_addr.size())) - throw CoreException("Error accepting new socket for Pipe"); - CloseSocket(lfd); - - this->WritePipe = cfd; - - SocketEngine::AddSocket(this); -} - -Pipe::~Pipe() -{ - CloseSocket(this->WritePipe); -} - -bool Pipe::ProcessRead() -{ - char dummy[512]; - while (recv(this->GetFD(), dummy, 512, 0) == 512); - this->OnNotify(); - return true; -} - -void Pipe::Notify() -{ - const char dummy = '*'; - send(this->WritePipe, &dummy, 1, 0); -} - -void Pipe::OnNotify() -{ -} - diff --git a/src/socketengines/socketengine_select.cpp b/src/socketengines/socketengine_select.cpp index 663cdd9ca..89a5c47c3 100644 --- a/src/socketengines/socketengine_select.cpp +++ b/src/socketengines/socketengine_select.cpp @@ -1,5 +1,10 @@ #include "module.h" +#ifdef _AIX +# undef FD_ZERO +# define FD_ZERO(p) memset((p), 0, sizeof(*(p))) +#endif /* _AIX */ + static int MaxFD; static unsigned FDCount; static fd_set ReadFDs; @@ -77,7 +82,7 @@ void SocketEngine::Process() */ if (FDCount == 0) { - sleep(Config->ReadTimeout); + sleep(tval.tv_sec); return; } #endif diff --git a/src/sockets.cpp b/src/sockets.cpp index fa79d32f2..5e65a521c 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -285,11 +285,7 @@ ClientSocket *SocketIO::Accept(ListenSocket *s) socklen_t size = sizeof(conaddr); int newsock = accept(s->GetFD(), &conaddr.sa, &size); -#ifndef INVALID_SOCKET - static const int INVALID_SOCKET = -1; -#endif - - if (newsock >= 0 && newsock != INVALID_SOCKET) + if (newsock >= 0) { ClientSocket *ns = s->OnAccept(newsock, conaddr); ns->SetFlag(SF_ACCEPTED); @@ -406,7 +402,7 @@ Socket::Socket(int sock, bool ipv6, int type) : Flags<SocketFlag>(SocketFlagStri Socket::~Socket() { SocketEngine::DelSocket(this); - CloseSocket(this->Sock); + close(this->Sock); this->IO->Destroy(); } @@ -431,13 +427,8 @@ bool Socket::IsIPv6() const */ bool Socket::SetBlocking() { -#ifdef _WIN32 - unsigned long opt = 0; - return !ioctlsocket(this->GetFD(), FIONBIO, &opt); -#else int flags = fcntl(this->GetFD(), F_GETFL, 0); return !fcntl(this->GetFD(), F_SETFL, flags & ~O_NONBLOCK); -#endif } /** Mark a socket as non-blocking @@ -445,13 +436,8 @@ bool Socket::SetBlocking() */ bool Socket::SetNonBlocking() { -#ifdef _WIN32 - unsigned long opt = 1; - return !ioctlsocket(this->GetFD(), FIONBIO, &opt); -#else int flags = fcntl(this->GetFD(), F_GETFL, 0); return !fcntl(this->GetFD(), F_SETFL, flags | O_NONBLOCK); -#endif } /** Bind the socket to an ip and port @@ -503,10 +489,8 @@ ListenSocket::ListenSocket(const Anope::string &bindip, int port, bool ipv6) : S { this->SetNonBlocking(); -#ifndef _WIN32 - int op = 1; + const char op = 1; setsockopt(this->GetFD(), SOL_SOCKET, SO_REUSEADDR, &op, sizeof(op)); -#endif this->bindaddr.pton(IPv6 ? AF_INET6 : AF_INET, bindip, port); this->IO->Bind(this, bindip, port); diff --git a/src/threadengine.cpp b/src/threadengine.cpp index e11b065ea..c186e49a2 100644 --- a/src/threadengine.cpp +++ b/src/threadengine.cpp @@ -1,5 +1,35 @@ #include "services.h" +static inline pthread_attr_t *get_engine_attr() +{ + /* Threadengine attributes used by this thread engine */ + static pthread_attr_t attr; + static bool inited = false; + + if (inited == false) + { + if (pthread_attr_init(&attr)) + throw CoreException("Error calling pthread_attr_init"); + if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) + throw CoreException("Unable to mark threads as joinable"); + inited = true; + } + + return &attr; +} + +/** Entry point used for the threads + * @param parameter A Thread* cast to a void* + */ +static void *entry_point(void *parameter) +{ + Thread *thread = static_cast<Thread *>(parameter); + thread->Run(); + thread->SetExitState(); + pthread_exit(0); + return NULL; +} + /** Threads constructor */ Thread::Thread() : exit(false) @@ -12,6 +42,14 @@ Thread::~Thread() { } +/** Join to the thread, sets the exit state to true + */ +void Thread::Join() +{ + this->SetExitState(); + pthread_join(Handle, NULL); +} + /** Sets the exit state as true informing the thread we want it to shut down */ void Thread::SetExitState() @@ -20,6 +58,25 @@ void Thread::SetExitState() exit = true; } +/** Exit the thread. Note that the thread still must be joined to free resources! + */ +void Thread::Exit() +{ + this->SetExitState(); + pthread_exit(0); +} + +/** Launch the thread + */ +void Thread::Start() +{ + if (pthread_create(&this->Handle, get_engine_attr(), entry_point, this)) + { + this->SetFlag(SF_DEAD); + throw CoreException("Unable to create thread: " + Anope::LastError()); + } +} + /** Returns the exit state of the thread * @return true if we want to exit */ @@ -36,3 +93,67 @@ void Thread::OnNotify() this->SetFlag(SF_DEAD); } +/** Constructor + */ +Mutex::Mutex() +{ + pthread_mutex_init(&mutex, NULL); +} + +/** Destructor + */ +Mutex::~Mutex() +{ + pthread_mutex_destroy(&mutex); +} + +/** Attempt to lock the mutex, will hang until a lock can be achieved + */ +void Mutex::Lock() +{ + pthread_mutex_lock(&mutex); +} + +/** Unlock the mutex, it must be locked first + */ +void Mutex::Unlock() +{ + pthread_mutex_unlock(&mutex); +} + +/** Attempt to lock the mutex, will return true on success and false on fail + * Does not block + * @return true or false + */ +bool Mutex::TryLock() +{ + return pthread_mutex_trylock(&mutex) == 0; +} + +/** Constructor + */ +Condition::Condition() : Mutex() +{ + pthread_cond_init(&cond, NULL); +} + +/** Destructor + */ +Condition::~Condition() +{ + pthread_cond_destroy(&cond); +} + +/** Called to wakeup the waiter + */ +void Condition::Wakeup() +{ + pthread_cond_signal(&cond); +} + +/** Called to wait for a Wakeup() call + */ +void Condition::Wait() +{ + pthread_cond_wait(&cond, &mutex); +} diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp deleted file mode 100644 index 987b8bc61..000000000 --- a/src/threadengines/threadengine_pthread.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include "services.h" - -static inline pthread_attr_t *get_engine_attr() -{ - /* Threadengine attributes used by this thread engine */ - static pthread_attr_t attr; - static bool inited = false; - - if (inited == false) - { - if (pthread_attr_init(&attr)) - throw CoreException("Error calling pthread_attr_init"); - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) - throw CoreException("Unable to mark threads as joinable"); - inited = true; - } - - return &attr; -} - -/** Entry point used for the threads - * @param parameter A Thread* cast to a void* - */ -static void *entry_point(void *parameter) -{ - Thread *thread = static_cast<Thread *>(parameter); - thread->Run(); - thread->SetExitState(); - pthread_exit(0); -} - -/** Join to the thread, sets the exit state to true - */ -void Thread::Join() -{ - this->SetExitState(); - pthread_join(Handle, NULL); -} - -/** Exit the thread. Note that the thread still must be joined to free resources! - */ -void Thread::Exit() -{ - this->SetExitState(); - pthread_exit(0); -} - -/** Launch the thread - */ -void Thread::Start() -{ - if (pthread_create(&this->Handle, get_engine_attr(), entry_point, this)) - { - this->SetFlag(SF_DEAD); - throw CoreException("Unable to create thread: " + Anope::LastError()); - } -} - -/** Constructor - */ -Mutex::Mutex() -{ - pthread_mutex_init(&mutex, NULL); -} - -/** Destructor - */ -Mutex::~Mutex() -{ - pthread_mutex_destroy(&mutex); -} - -/** Attempt to lock the mutex, will hang until a lock can be achieved - */ -void Mutex::Lock() -{ - pthread_mutex_lock(&mutex); -} - -/** Unlock the mutex, it must be locked first - */ -void Mutex::Unlock() -{ - pthread_mutex_unlock(&mutex); -} - -/** Attempt to lock the mutex, will return true on success and false on fail - * Does not block - * @return true or false - */ -bool Mutex::TryLock() -{ - return pthread_mutex_trylock(&mutex) == 0; -} - -/** Constructor - */ -Condition::Condition() : Mutex() -{ - pthread_cond_init(&cond, NULL); -} - -/** Destructor - */ -Condition::~Condition() -{ - pthread_cond_destroy(&cond); -} - -/** Called to wakeup the waiter - */ -void Condition::Wakeup() -{ - pthread_cond_signal(&cond); -} - -/** Called to wait for a Wakeup() call - */ -void Condition::Wait() -{ - pthread_cond_wait(&cond, &mutex); -} diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp deleted file mode 100644 index 5b6739c52..000000000 --- a/src/threadengines/threadengine_win32.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "services.h" - -/** Entry point for the thread - * @param paramter A Thread* cast to a void* - */ -static DWORD WINAPI entry_point(void *parameter) -{ - Thread *thread = static_cast<Thread *>(parameter); - thread->Run(); - thread->SetExitState(); - return 0; -} - -/** Join to the thread, sets the exit state to true - */ -void Thread::Join() -{ - this->SetExitState(); - WaitForSingleObject(Handle, INFINITE); -} - -/** Exit the thread. Note that the thread still must be joined to free resources! - */ -void Thread::Exit() -{ - this->SetExitState(); - ExitThread(0); -} - -/** Launch the thread - */ -void Thread::Start() -{ - this->Handle = CreateThread(NULL, 0, entry_point, this, 0, NULL); - - if (!this->Handle) - { - this->SetFlag(SF_DEAD); - throw CoreException("Unable to create thread: " + Anope::LastError()); - } -} - -/** Constructor - */ -Mutex::Mutex() -{ - InitializeCriticalSection(&mutex); -} - -/** Destructor - */ -Mutex::~Mutex() -{ - DeleteCriticalSection(&mutex); -} - -/** Attempt to lock the mutex, will hang until a lock can be achieved - */ -void Mutex::Lock() -{ - EnterCriticalSection(&mutex); -} - -/** Unlock the mutex, it must be locked first - */ -void Mutex::Unlock() -{ - LeaveCriticalSection(&mutex); -} - -/** Attempt to lock the mutex, will return true on success and false on fail - * Does not block - * @return true or false - */ -bool Mutex::TryLock() -{ - return TryEnterCriticalSection(&mutex); -} - -/** Constructor - */ -Condition::Condition() : Mutex() -{ - cond = CreateEvent(NULL, false, false, NULL); -} - -/** Destructor - */ -Condition::~Condition() -{ - CloseHandle(cond); -} - -/** Called to wakeup the waiter - */ -void Condition::Wakeup() -{ - PulseEvent(cond); -} - -/** Called to wait for a Wakeup() call - */ -void Condition::Wait() -{ - LeaveCriticalSection(&mutex); - WaitForSingleObject(cond, INFINITE); - EnterCriticalSection(&mutex); -} diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h index a631a4a70..cd5215298 100644 --- a/src/win32/anope_windows.h +++ b/src/win32/anope_windows.h @@ -1,71 +1,75 @@ - /* POSIX emulation layer for Windows.
- *
- * Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
- * Copyright (C) 2008-2011 Anope Team <info@anope.org>
- *
- * Please read COPYING and README for further details.
- *
- * Based on the original code of Epona by Lara.
- * Based on the original code of Services by Andy Church.
- */
-
- #ifndef WINDOWS_H
- #define WINDOWS_H
- #ifdef _WIN32
-
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <windows.h>
-#include <sys/timeb.h>
-#include <direct.h>
-#include <io.h>
-#ifdef MODULE_COMPILE
-# define CoreExport __declspec(dllimport)
-# define DllExport __declspec(dllexport)
-#else
-# define CoreExport __declspec(dllexport)
-# define DllExport __declspec(dllimport)
-#endif
-/* We have our own inet_pton and inet_ntop (Windows XP doesn't have its own) */
-#define inet_pton inet_pton_
-#define inet_ntop inet_ntop_
-#define setenv(x, y, z) SetEnvironmentVariable(x, y)
-#define unsetenv(x) SetEnvironmentVariable(x, NULL)
-#define MARK_DEPRECATED
-#if GETTEXT_FOUND
-/* Undefine some functions libintl defines */
-# undef snprintf
-# undef vsnprintf
-# undef printf
-#endif
-#define snprintf _snprintf
-/* VS2008 hates having this define before its own */
-#define vsnprintf _vsnprintf
-#define stat _stat
-#define getcwd(x, y) GetCurrentDirectory(y, x)
-#define getpid GetCurrentProcessId
-#define S_ISREG(x) ((x) & _S_IFREG)
-#ifdef EINPROGRESS
-# undef EINPROGRESS
-#endif
-#define EINPROGRESS WSAEWOULDBLOCK
-
-#include "sigaction/sigaction.h"
-
-namespace Anope
-{
- class string;
-}
-
-extern CoreExport void OnStartup();
-extern CoreExport void OnShutdown();
-extern CoreExport USHORT WindowsGetLanguage(const char *lang);
-extern CoreExport int inet_pton(int af, const char *src, void *dst);
-extern CoreExport const char *inet_ntop(int af, const void *src, char *dst, size_t size);
-extern CoreExport int gettimeofday(timeval *tv, void *);
-extern CoreExport Anope::string GetWindowsVersion();
-extern CoreExport bool SupportedWindowsVersion();
-extern int mkstemp(char *input);
-
- #endif // _WIN32
- #endif // WINDOWS_H
+ /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org> + * Copyright (C) 2008-2011 Anope Team <info@anope.org> + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #ifndef WINDOWS_H + #define WINDOWS_H + #ifdef _WIN32 + +#include <winsock2.h> +#include <ws2tcpip.h> +#include <windows.h> +#include <sys/timeb.h> +#include <direct.h> + +#ifdef MODULE_COMPILE +# define CoreExport __declspec(dllimport) +# define DllExport __declspec(dllexport) +#else +# define CoreExport __declspec(dllexport) +# define DllExport __declspec(dllimport) +#endif + +#define MARK_DEPRECATED + +#if GETTEXT_FOUND +/* Undefine some functions libintl defines */ +# undef snprintf +# undef vsnprintf +# undef printf +#endif + +#define snprintf _snprintf +/* VS2008 hates having this define before its own */ +#define vsnprintf _vsnprintf + +#define stat _stat +#define S_ISREG(x) ((x) & _S_IFREG) + +#ifdef EINPROGRESS +# undef EINPROGRESS +#endif +#define EINPROGRESS WSAEWOULDBLOCK + +#include "socket.h" +#include "dir/dir.h" +#include "dl/dl.h" +#include "pipe/pipe.h" +#include "pthread/pthread.h" +#include "sigaction/sigaction.h" + +namespace Anope +{ + class string; +} + +extern CoreExport void OnStartup(); +extern CoreExport void OnShutdown(); +extern CoreExport USHORT WindowsGetLanguage(const char *lang); +extern CoreExport int gettimeofday(timeval *tv, void *); +extern CoreExport Anope::string GetWindowsVersion(); +extern CoreExport bool SupportedWindowsVersion(); +extern int setenv(const char *name, const char *value, int overwrite); +extern int unsetenv(const char *name); +extern int mkstemp(char *input); +extern void getcwd(char *buf, size_t sz); + +#endif // _WIN32 +#endif // WINDOWS_H diff --git a/src/win32/dir/dir.cpp b/src/win32/dir/dir.cpp new file mode 100644 index 000000000..28a54be98 --- /dev/null +++ b/src/win32/dir/dir.cpp @@ -0,0 +1,47 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include "dir.h" +#include <stdio.h> + +DIR *opendir(const char *path) +{ + char real_path[MAX_PATH]; + _snprintf(real_path, sizeof(real_path), "%s/*", path); + + DIR *d = new DIR(); + d->handle = FindFirstFile(real_path, &d->data); + d->read_first = false; + + if (d->handle == INVALID_HANDLE_VALUE) + { + delete d; + return NULL; + } + + return d; +} + +dirent *readdir(DIR *d) +{ + if (d->read_first == false) + d->read_first = true; + else if (!FindNextFile(d->handle, &d->data)) + return NULL; + + d->ent.d_ino = 1; + d->ent.d_name = d->data.cFileName; + + return &d->ent; +} + +int closedir(DIR *d) +{ + FindClose(d->handle); + delete d; + return 0; +} diff --git a/src/win32/dir/dir.h b/src/win32/dir/dir.h new file mode 100644 index 000000000..8e04c01cc --- /dev/null +++ b/src/win32/dir/dir.h @@ -0,0 +1,26 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include <windows.h> + +struct dirent +{ + int d_ino; + char *d_name; +}; + +struct DIR +{ + dirent ent; + HANDLE handle; + WIN32_FIND_DATA data; + bool read_first; +}; + +DIR *opendir(const char *); +dirent *readdir(DIR *); +int closedir(DIR *); diff --git a/src/win32/dl/dl.cpp b/src/win32/dl/dl.cpp new file mode 100644 index 000000000..8d76e1f0d --- /dev/null +++ b/src/win32/dl/dl.cpp @@ -0,0 +1,30 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include "services.h" + +void *dlopen(const char *filename, int) +{ + return LoadLibrary(filename); +} + +char *dlerror(void) +{ + static Anope::string err = Anope::LastError(); + SetLastError(0); + return err.empty() ? NULL : const_cast<char *>(err.c_str()); +} + +void *dlsym(void *handle, const char *symbol) +{ + return GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol); +} + +int dlclose(void *handle) +{ + return !FreeLibrary(reinterpret_cast<HMODULE>(handle)); +} diff --git a/src/win32/dl/dl.h b/src/win32/dl/dl.h new file mode 100644 index 000000000..686611594 --- /dev/null +++ b/src/win32/dl/dl.h @@ -0,0 +1,13 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#define RTLD_LAZY 0 + +extern void *dlopen(const char *filename, int); +extern char *dlerror(void); +extern void *dlsym(void *handle, const char *symbol); +extern int dlclose(void *handle); diff --git a/src/win32/pipe/pipe.cpp b/src/win32/pipe/pipe.cpp new file mode 100644 index 000000000..94e204d9a --- /dev/null +++ b/src/win32/pipe/pipe.cpp @@ -0,0 +1,61 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include "services.h" + +int pipe(int fds[2]) +{ + sockaddrs localhost; + + localhost.pton(AF_INET, "127.0.0.1"); + + int cfd = socket(AF_INET, SOCK_STREAM, 0), lfd = socket(AF_INET, SOCK_STREAM, 0); + if (cfd == -1 || lfd == -1) + { + close(cfd); + close(lfd); + return -1; + } + + if (bind(lfd, &localhost.sa, localhost.size()) == -1) + { + close(cfd); + close(lfd); + return -1; + } + + if (listen(lfd, 1) == -1) + { + close(cfd); + close(lfd); + return -1; + } + + sockaddrs lfd_addr; + socklen_t sz = sizeof(lfd_addr); + getsockname(lfd, &lfd_addr.sa, &sz); + + if (connect(cfd, &lfd_addr.sa, lfd_addr.size())) + { + close(cfd); + close(lfd); + return -1; + } + + int afd = accept(lfd, NULL, NULL); + if (afd == -1) + { + close(cfd); + return -1; + } + + fds[0] = cfd; + fds[1] = afd; + + return 0; +} + diff --git a/src/win32/pipe/pipe.h b/src/win32/pipe/pipe.h new file mode 100644 index 000000000..1e8538cfb --- /dev/null +++ b/src/win32/pipe/pipe.h @@ -0,0 +1,8 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +extern int pipe(int fds[2]); diff --git a/src/win32/pthread/pthread.cpp b/src/win32/pthread/pthread.cpp new file mode 100644 index 000000000..8c1da7555 --- /dev/null +++ b/src/win32/pthread/pthread.cpp @@ -0,0 +1,117 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include "pthread.h" + +struct ThreadInfo +{ + void *(*entry)(void *); + void *param; +}; + +static DWORD WINAPI entry_point(void *parameter) +{ + ThreadInfo *ti = static_cast<ThreadInfo *>(parameter); + ti->entry(ti->param); + delete ti; + return 0; +} + +int pthread_attr_init(pthread_attr_t *) +{ + /* No need for this */ + return 0; +} + +int pthread_attr_setdetachstate(pthread_attr_t *, int) +{ + /* No need for this */ + return 0; +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param) +{ + ThreadInfo *ti = new ThreadInfo; + ti->entry = entry; + ti->param = param; + + *thread = CreateThread(NULL, 0, entry_point, ti, 0, NULL); + if (!*thread) + { + delete ti; + return -1; + } + + return 0; +} + +int pthread_join(pthread_t thread, void **) +{ + if (WaitForSingleObject(thread, INFINITE) == WAIT_FAILED) + return -1; + return 0; +} + +void pthread_exit(int i) +{ + ExitThread(i); +} + +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *) +{ + InitializeCriticalSection(mutex); + return 0; +} + +int pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + DeleteCriticalSection(mutex); + return 0; +} + +int pthread_mutex_lock(pthread_mutex_t *mutex) +{ + EnterCriticalSection(mutex); + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + return !TryEnterCriticalSection(mutex); +} + +int pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + LeaveCriticalSection(mutex); + return 0; +} + +int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *) +{ + *cond = CreateEvent(NULL, false, false, NULL); + if (*cond == NULL) + return -1; + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + return !CloseHandle(*cond); +} + +int pthread_cond_signal(pthread_cond_t *cond) +{ + return !PulseEvent(*cond); +} + +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +{ + LeaveCriticalSection(mutex); + WaitForSingleObject(*cond, INFINITE); + EnterCriticalSection(mutex); + return 0; +} diff --git a/src/win32/pthread/pthread.h b/src/win32/pthread/pthread.h new file mode 100644 index 000000000..5ec0775ea --- /dev/null +++ b/src/win32/pthread/pthread.h @@ -0,0 +1,34 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#include <Windows.h> + +typedef HANDLE pthread_t; +typedef CRITICAL_SECTION pthread_mutex_t; +typedef HANDLE pthread_cond_t; +typedef int pthread_attr_t; +typedef void pthread_mutexattr_t; +typedef void pthread_condattr_t; + +#define PTHREAD_CREATE_JOINABLE 0 + +extern int pthread_attr_init(pthread_attr_t *); +extern int pthread_attr_setdetachstate(pthread_attr_t *, int); +extern int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); +extern int pthread_join(pthread_t, void **); +extern void pthread_exit(int); + +extern int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +extern int pthread_mutex_destroy(pthread_mutex_t *); +extern int pthread_mutex_lock(pthread_mutex_t *); +extern int pthread_mutex_trylock(pthread_mutex_t *); +extern int pthread_mutex_unlock(pthread_mutex_t *); + +extern int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); +extern int pthread_cond_destroy(pthread_cond_t *); +extern int pthread_cond_signal(pthread_cond_t *); +extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); diff --git a/src/win32/sigaction/sigaction.cpp b/src/win32/sigaction/sigaction.cpp index f65ffaa84..e79187461 100644 --- a/src/win32/sigaction/sigaction.cpp +++ b/src/win32/sigaction/sigaction.cpp @@ -1,31 +1,31 @@ - /* POSIX emulation layer for Windows.
- *
- * Copyright (C) 2008-2011 Anope Team <team@anope.org>
- *
- * Please read COPYING and README for further details.
- *
- * Based on the original code of Epona by Lara.
- * Based on the original code of Services by Andy Church.
- */
-
- #include <windows.h>
- #include "sigaction.h"
- #include <signal.h>
-
- int sigaction(int sig, struct sigaction *action, struct sigaction *old)
- {
- if (sig == -1)
- return 0;
- if (old == NULL)
- {
- if (signal(sig, SIG_DFL) == SIG_ERR)
- return -1;
- }
- else
- {
- if (signal(sig, action->sa_handler) == SIG_ERR)
- return -1;
- }
- return 0;
- }
+ /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #include <windows.h> + #include "sigaction.h" + #include <signal.h> + + int sigaction(int sig, struct sigaction *action, struct sigaction *old) + { + if (sig == -1) + return 0; + if (old == NULL) + { + if (signal(sig, SIG_DFL) == SIG_ERR) + return -1; + } + else + { + if (signal(sig, action->sa_handler) == SIG_ERR) + return -1; + } + return 0; + }
\ No newline at end of file diff --git a/src/win32/sigaction/sigaction.h b/src/win32/sigaction/sigaction.h index 366965f8b..96d67e8e9 100644 --- a/src/win32/sigaction/sigaction.h +++ b/src/win32/sigaction/sigaction.h @@ -1,28 +1,28 @@ - /* POSIX emulation layer for Windows.
- *
- * Copyright (C) 2008-2011 Anope Team <team@anope.org>
- *
- * Please read COPYING and README for further details.
- *
- * Based on the original code of Epona by Lara.
- * Based on the original code of Services by Andy Church.
- */
-
- #define sigemptyset(x) memset((x), 0, sizeof(*(x)))
-
- #ifndef SIGHUP
- # define SIGHUP -1
- #endif
- #ifndef SIGPIPE
- # define SIGPIPE -1
- #endif
-
- struct sigaction
- {
- void (*sa_handler)(int);
- int sa_flags;
- int sa_mask;
- };
-
- extern int sigaction(int, struct sigaction *, struct sigaction *);
-
+ /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + + #define sigemptyset(x) memset((x), 0, sizeof(*(x))) + + #ifndef SIGHUP + # define SIGHUP -1 + #endif + #ifndef SIGPIPE + # define SIGPIPE -1 + #endif + + struct sigaction + { + void (*sa_handler)(int); + int sa_flags; + int sa_mask; + }; + + extern int sigaction(int, struct sigaction *, struct sigaction *); + diff --git a/src/win32/socket.cpp b/src/win32/socket.cpp new file mode 100644 index 000000000..42140ed8b --- /dev/null +++ b/src/win32/socket.cpp @@ -0,0 +1,152 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#define WIN32_NO_OVERRIDE +#include "services.h" + +inline static bool is_socket(int fd) +{ + int optval; + socklen_t optlen = sizeof(optval); + return getsockopt(fd, SOL_SOCKET, SO_TYPE, reinterpret_cast<char *>(&optval), &optlen) == 0; +} + +int read(int fd, char *buf, size_t count) +{ + if (is_socket(fd)) + return recv(fd, buf, count, 0); + else + return _read(fd, buf, count); +} + +int write(int fd, const char *buf, size_t count) +{ + if (is_socket(fd)) + return send(fd, buf, count, 0); + else + return _write(fd, buf, count); +} + +int windows_close(int fd) +{ + if (is_socket(fd)) + return closesocket(fd); + else + return close(fd); +} + +int windows_accept(int fd, struct sockaddr *addr, int *addrlen) +{ + int i = accept(fd, addr, addrlen); + if (i == INVALID_SOCKET) + return -1; + return i; +} + +/** This is inet_pton, but it works on Windows + * @param af The protocol type, AF_INET or AF_INET6 + * @param src The address + * @param dst Struct to put results in + * @return 1 on sucess, -1 on error + */ +int windows_inet_pton(int af, const char *src, void *dst) +{ + int address_length; + sockaddr_storage sa; + sockaddr_in *sin = reinterpret_cast<sockaddr_in *>(&sa); + sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6 *>(&sa); + + switch (af) + { + case AF_INET: + address_length = sizeof(sockaddr_in); + break; + case AF_INET6: + address_length = sizeof(sockaddr_in6); + break; + default: + return -1; + } + + if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length)) + { + switch (af) + { + case AF_INET: + memcpy(dst, &sin->sin_addr, sizeof(in_addr)); + break; + case AF_INET6: + memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr)); + break; + } + return 1; + } + + return 0; +} + +/** This is inet_ntop, but it works on Windows + * @param af The protocol type, AF_INET or AF_INET6 + * @param src Network address structure + * @param dst After converting put it here + * @param size sizeof the dest + * @return dst + */ +const char *windows_inet_ntop(int af, const void *src, char *dst, size_t size) +{ + int address_length; + DWORD string_length = size; + sockaddr_storage sa; + sockaddr_in *sin = reinterpret_cast<sockaddr_in *>(&sa); + sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6 *>(&sa); + + memset(&sa, 0, sizeof(sa)); + + switch (af) + { + case AF_INET: + address_length = sizeof(sockaddr_in); + sin->sin_family = af; + memcpy(&sin->sin_addr, src, sizeof(in_addr)); + break; + case AF_INET6: + address_length = sizeof(sockaddr_in6); + sin6->sin6_family = af; + memcpy(&sin6->sin6_addr, src, sizeof(in6_addr)); + break; + default: + return NULL; + } + + if (!WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&sa), address_length, NULL, dst, &string_length)) + return dst; + + return NULL; +} + +int fcntl(int fd, int cmd, int arg) +{ + if (cmd == F_GETFL) + { + return 0; + } + else if (cmd == F_SETFL) + { + if (arg & O_NONBLOCK) + { + unsigned long opt = 1; + return ioctlsocket(fd, FIONBIO, &opt); + } + else + { + unsigned long opt = 0; + return ioctlsocket(fd, FIONBIO, &opt); + } + } + + return -1; +} diff --git a/src/win32/socket.h b/src/win32/socket.h new file mode 100644 index 000000000..106b9310e --- /dev/null +++ b/src/win32/socket.h @@ -0,0 +1,32 @@ + /* POSIX emulation layer for Windows. + * + * Copyright (C) 2008-2011 Anope Team <team@anope.org> + * + * Please read COPYING and README for further details. + */ + +#define read read_not_used +#define write write_not_used +#include <io.h> +#undef read +#undef write + +#define F_GETFL 0 +#define F_SETFL 1 + +#define O_NONBLOCK 1 + +extern CoreExport int read(int fd, char *buf, size_t count); +extern CoreExport int write(int fd, const char *buf, size_t count); +extern CoreExport int windows_close(int fd); +extern CoreExport int windows_accept(int fd, struct sockaddr *addr, int *addrlen); +extern CoreExport int windows_inet_pton(int af, const char *src, void *dst); +extern CoreExport const char *windows_inet_ntop(int af, const void *src, char *dst, size_t size); +extern CoreExport int fcntl(int fd, int cmd, int arg); + +#ifndef WIN32_NO_OVERRIDE +# define close windows_close +# define accept windows_accept +# define inet_pton windows_inet_pton +# define inet_ntop windows_inet_ntop +#endif diff --git a/src/win32/vsvars32.bat b/src/win32/vsvars32.bat deleted file mode 100644 index f1aee215e..000000000 --- a/src/win32/vsvars32.bat +++ /dev/null @@ -1,66 +0,0 @@ -@SET VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8
-@SET VCINSTALLDIR=%VSINSTALLDIR%\VC
-@SET FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework
-@SET FrameworkVersion=v2.0.50727
-@SET FrameworkSDKDir=%VSINSTALLDIR%\SDK\v2.0
-@SET Framework35Version=v3.5
-@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
-@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR
-
-@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.
-
-@call :GetWindowsSdkDir
-
-@if not "%WindowsSdkDir%" == "" (
- set "PATH=%WindowsSdkDir%bin;%PATH%"
- set "INCLUDE=%WindowsSdkDir%include;%INCLUDE%"
- set "LIB=%WindowsSdkDir%lib;%LIB%"
-)
-
-@rem
-@rem Root of Visual Studio IDE installed files.
-@rem
-@set DevEnvDir=%VSINSTALLDIR%\Common7\IDE
-
-@set PATH=%VSINSTALLDIR%\Common7\IDE;%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common7\Tools;C:\WINDOWS\Microsoft.NET\Framework\v3.5;%VSINSTALLDIR%\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;%VCINSTALLDIR%\VCPackages;%PATH%
-@set INCLUDE=%VCINSTALLDIR%\INCLUDE;%INCLUDE%
-@set LIB=%VCINSTALLDIR%\LIB;%VSINSTALLDIR%\SDK\v2.0\lib;%LIB%
-@set LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v3.5;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;%VCINSTALLDIR%\LIB;%LIBPATH%
-
-@goto end
-
-:GetWindowsSdkDir
-@call :GetWindowsSdk2008DirHelper HKLM > nul 2>&1
-@if errorlevel 1 call :GetWindowsSdk2003DirHelper HKLM > nul 2>&1
-@if errorlevel 1 call :GetWindowsSdk2008DirHelper HKCU > nul 2>&1
-@if errorlevel 1 call :GetWindowsSdk2003DirHelper HKCU > nul 2>&1
-@if errorlevel 1 set WindowsSdkDir=%VCINSTALLDIR%\PlatformSDK\
-@exit /B 0
-
-:GetWindowsSdk2008DirHelper
-@for /F "tokens=1,2*" %%i in ('reg query "%1\SOFTWARE\Microsoft\Microsoft SDKs\Windows" /v "CurrentInstallFolder"') DO (
- if "%%i"=="CurrentInstallFolder" (
- SET "WindowsSdkDir=%%k"
- )
-)
-@if "%WindowsSdkDir%"=="" exit /B 1
-@exit /B 0
-
-:GetWindowsSdk2003DirHelper
-@for /F "tokens=1,2,3*" %%i in ('reg query "%1\SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs\D2FF9F89-8AA2-4373-8A31-C838BF4DBBE1" /v "Install Dir"') DO (
- if "%%i %%j"=="Install Dir" (
- SET "WindowsSdkDir=%%l"
- )
-)
-@if "%WindowsSdkDir%"=="" exit /B 1
-@exit /B 0
-
-:error_no_VSINSTALLDIR
-@echo ERROR: VSINSTALLDIR variable is not set.
-@goto end
-
-:error_no_VCINSTALLDIR
-@echo ERROR: VCINSTALLDIR variable is not set.
-@goto end
-
-:end
diff --git a/src/win32/windows.cpp b/src/win32/windows.cpp index 60edaffbc..139a17736 100644 --- a/src/win32/windows.cpp +++ b/src/win32/windows.cpp @@ -12,13 +12,11 @@ #ifdef _WIN32 #include "services.h" -struct WindowsLanguage +static struct WindowsLanguage { const char *languageName; USHORT windowsLanguageName; -}; - -WindowsLanguage WindowsLanguages[] = { +} WindowsLanguages[] = { {"ca_ES", LANG_CATALAN}, {"de_DE", LANG_GERMAN}, {"el_GR", LANG_GREEK}, @@ -34,7 +32,7 @@ WindowsLanguage WindowsLanguages[] = { {NULL, 0} }; -WSADATA wsa; +static WSADATA wsa; void OnStartup() { @@ -55,87 +53,6 @@ USHORT WindowsGetLanguage(const char *lang) return LANG_NEUTRAL; } -/** This is inet_pton, but it works on Windows - * @param af The protocol type, AF_INET or AF_INET6 - * @param src The address - * @param dst Struct to put results in - * @return 1 on sucess, -1 on error - */ -int inet_pton(int af, const char *src, void *dst) -{ - int address_length; - sockaddr_storage sa; - sockaddr_in *sin = reinterpret_cast<sockaddr_in *>(&sa); - sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6 *>(&sa); - - switch (af) - { - case AF_INET: - address_length = sizeof(sockaddr_in); - break; - case AF_INET6: - address_length = sizeof(sockaddr_in6); - break; - default: - return -1; - } - - if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length)) - { - switch (af) - { - case AF_INET: - memcpy(dst, &sin->sin_addr, sizeof(in_addr)); - break; - case AF_INET6: - memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr)); - break; - } - return 1; - } - - return 0; -} - -/** This is inet_ntop, but it works on Windows - * @param af The protocol type, AF_INET or AF_INET6 - * @param src Network address structure - * @param dst After converting put it here - * @param size sizeof the dest - * @return dst - */ -const char *inet_ntop(int af, const void *src, char *dst, size_t size) -{ - int address_length; - DWORD string_length = size; - sockaddr_storage sa; - sockaddr_in *sin = reinterpret_cast<sockaddr_in *>(&sa); - sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6 *>(&sa); - - memset(&sa, 0, sizeof(sa)); - - switch (af) - { - case AF_INET: - address_length = sizeof(sockaddr_in); - sin->sin_family = af; - memcpy(&sin->sin_addr, src, sizeof(in_addr)); - break; - case AF_INET6: - address_length = sizeof(sockaddr_in6); - sin6->sin6_family = af; - memcpy(&sin6->sin6_addr, src, sizeof(in6_addr)); - break; - default: - return NULL; - } - - if (!WSAAddressToString(reinterpret_cast<LPSOCKADDR>(&sa), address_length, NULL, dst, &string_length)) - return dst; - - return NULL; -} - /** Like gettimeofday(), but it works on Windows. * @param tv A timeval struct * @param tz Should be NULL, it is not used @@ -302,6 +219,16 @@ bool SupportedWindowsVersion() return false; } +int setenv(const char *name, const char *value, int overwrite) +{ + return SetEnvironmentVariable(name, value); +} + +int unsetenv(const char *name) +{ + return SetEnvironmentVariable(name, NULL); +} + int mkstemp(char *input) { input = _mktemp(input); @@ -315,4 +242,9 @@ int mkstemp(char *input) return fd; } +void getcwd(char *buf, size_t sz) +{ + GetCurrentDirectory(sz, buf); +} + #endif |