diff --git a/winixd/core/app.cpp b/winixd/core/app.cpp index bb5f30f..e07e695 100644 --- a/winixd/core/app.cpp +++ b/winixd/core/app.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2018, Tomasz Sowa + * Copyright (c) 2010-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -151,10 +151,8 @@ App::App() session_manager.set_dependency(&winix_model); // zobaczyc czy wskoczy do przeciazonej metody w session manager session_manager.SetLastContainer(&system.users.last); - session_manager.SetConfig(&config); session_manager.SetCur(&cur); session_manager.SetSystem(&system); - session_manager.SetSynchro(&synchro); post_multi_parser.set_dependency(&winix_base); post_multi_parser.SetConfig(&config); @@ -500,10 +498,10 @@ void App::ProcessRequestThrow() if( !cur.request->dir_tab.empty() ) { cur.mount = system.mounts.CalcCurMount(); - functions.CheckFunctionAndSymlink(); - session_manager.PrepareSession(); - cur.session = session_manager.GetCurSession(); + cur.session = session_manager.PrepareSession(); + functions.CheckFunctionAndSymlink(); // here a function can be changed + cur.session = session_manager.CheckIfFunctionRequireSession(); SetLocale(); if( cur.session->new_session ) @@ -567,7 +565,7 @@ void App::ProcessRequest() log << log2 << config.log_delimiter << logend; ProcessRequestThrow(); - SaveSessionsIfNeeded(); // !! IMPROVE ME move to the session's thread + SaveSessionsIfNeeded(); system.load_avg.StopRequest(); } diff --git a/winixd/core/basethread.cpp b/winixd/core/basethread.cpp index ee41d73..151669d 100644 --- a/winixd/core/basethread.cpp +++ b/winixd/core/basethread.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2018, Tomasz Sowa + * Copyright (c) 2010-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -54,13 +54,17 @@ BaseThread::~BaseThread() } - -void BaseThread::SetSynchro(Synchro * psynchro) +void BaseThread::set_main_log_buffer(PT::WTextStream * log_buffer) { - synchro = psynchro; + main_log.SetLogBuffer(log_buffer); } +void BaseThread::set_main_file_log(PT::FileLog * file_log) +{ + main_log.SetFileLog(file_log); +} + void BaseThread::Mode(int mode) { diff --git a/winixd/core/basethread.h b/winixd/core/basethread.h index fef41a2..6eb3a2b 100644 --- a/winixd/core/basethread.h +++ b/winixd/core/basethread.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2018, Tomasz Sowa + * Copyright (c) 2010-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -54,8 +54,9 @@ public: BaseThread(); virtual ~BaseThread(); - // synchro object (must be set) - void SetSynchro(Synchro * psynchro); + void set_main_log_buffer(PT::WTextStream * log_buffer); + void set_main_file_log(PT::FileLog * file_log); + // work mode // we have two modes: @@ -100,6 +101,13 @@ public: pthread_t ThreadId(); +protected: + + // logger for the main thread + Log main_log; + + // log from WinixBase is meant to be used by the other thread + protected: @@ -126,7 +134,6 @@ protected: protected: - Synchro * synchro; pthread_t thread_id; // thread id - set by StartThread() pthread_cond_t thread_signal; int work_mode; diff --git a/winixd/core/job.cpp b/winixd/core/job.cpp index 21f5af7..0538664 100644 --- a/winixd/core/job.cpp +++ b/winixd/core/job.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2014, Tomasz Sowa + * Copyright (c) 2012-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -183,9 +183,7 @@ void Job::DoJob(PT::Space & job) // second thread (objects not locked) void Job::DoWinixJob(PT::Space & job) { - Lock(); //log << log1 << "standard winix job: " << job.Text(L"type") << logend; - Unlock(); } diff --git a/winixd/core/sessionmanager.cpp b/winixd/core/sessionmanager.cpp index 3d08748..9054a83 100644 --- a/winixd/core/sessionmanager.cpp +++ b/winixd/core/sessionmanager.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2008-2018, Tomasz Sowa + * Copyright (c) 2008-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -77,13 +77,6 @@ void SessionManager::SetCur(Cur * pcur) } -void SessionManager::SetConfig(Config * pconfig) -{ - config = pconfig; - session_tab.SetConfig(pconfig); -} - - void SessionManager::SetSystem(System * psystem) { system = psystem; @@ -195,7 +188,7 @@ SessionContainer::Iterator i = session_tab.End(); } else { - log << log2 << "SM: sessions limit exceeded (" << config->session_max << ")" << logend; + main_log << log2 << "SM: sessions limit exceeded (" << config->session_max << ")" << logend; } if( i != session_tab.End() ) @@ -206,12 +199,12 @@ SessionContainer::Iterator i = session_tab.End(); session->id_index = (unsigned int)session->id; session->id_index += std::rand(); - log << log2 << "SM: created a new session: " << session->id << logend; + main_log << log2 << "SM: created a new session: " << session->id << logend; } else { // there is a problem with generating a new session id - log << log1 << "SM: cannot create a session id" << logend; + main_log << log1 << "SM: cannot create a session id" << logend; } } @@ -223,7 +216,7 @@ void SessionManager::SetTemporarySession() session->SetTimesTo(cur->request->start_time); session->new_session = false; // temporary session was initialized at the beginning - log << log2 << "SM: using temporary session" << logend; + main_log << log2 << "SM: using temporary session" << logend; } @@ -242,15 +235,15 @@ return difference; void SessionManager::SetSessionPutLogInfo(Session & ses, bool has_index, unsigned int difference) { - log << log2 << "SM: session: " << ses.id; + main_log << log2 << "SM: session: " << ses.id; if( has_index ) - log << ", index difference: " << (size_t)difference; + main_log << ", index difference: " << (size_t)difference; if( ses.puser ) - log << log2 << ", user: " << ses.puser->name << ", id: " << ses.puser->id; + main_log << log2 << ", user: " << ses.puser->name << ", id: " << ses.puser->id; - log << log2 << logend; + main_log << log2 << logend; } @@ -264,7 +257,7 @@ void SessionManager::IncrementBanLevel(IPBan * ip_ban) cur->request->start_time + (time_t)config->ban_level_3_delay); PT::Date date(ip_ban->expires); - log << log2 << "SM: this IP address has been banned to: " << date << " UTC" << logend; + main_log << log2 << "SM: this IP address has been banned to: " << date << " UTC" << logend; } @@ -290,7 +283,7 @@ void SessionManager::BrokenCookieCheckBan() } else { - log << log2 << "SM: too many incorrect encoded cookies were sent from this IP" << logend; + main_log << log2 << "SM: too many incorrect encoded cookies were sent from this IP" << logend; IncrementBanLevel(current_ip_ban); } } @@ -308,14 +301,14 @@ void SessionManager::IncorrectSessionCheckBan() } else { - log << log2 << "SM: too many incorrect sessions identifiers were sent from this IP" << logend; + main_log << log2 << "SM: too many incorrect sessions identifiers were sent from this IP" << logend; IncrementBanLevel(current_ip_ban); } } -bool SessionManager::ShouldNoSessionCookieGenerateTmpSession() +void SessionManager::NoSessionCookieWasSent() { if( !current_ip_ban ) current_ip_ban = &AddIPToBanList(cur->request->ip, cur->request->start_time); @@ -324,16 +317,13 @@ bool SessionManager::ShouldNoSessionCookieGenerateTmpSession() { current_ip_ban->no_session_cookie_events += 1; SetFirstExpirationTime(current_ip_ban); - return false; } else { - log << log2 << "SM: too many times you have not sent a session cookie" << logend; + main_log << log2 << "SM: too many times you have not sent a session cookie" << logend; if( config->no_session_cookie_ban_mode == 1 ) IncrementBanLevel(current_ip_ban); - - return true; } } @@ -346,21 +336,21 @@ bool SessionManager::IsSessionCorrect(long id, bool has_index, unsigned int inde if( id == 0 ) { - log << log3 << "SM: id 0 is reserved for the temporary session" << logend; + main_log << log3 << "SM: id 0 is reserved for the temporary session" << logend; IncorrectSessionCheckBan(); return false; } if( s == session_tab.End() ) { - log << log3 << "SM: there is no a session with id: " << id << logend; + main_log << log3 << "SM: there is no a session with id: " << id << logend; IncorrectSessionCheckBan(); return false; } if( s->remove_me ) { - log << log3 << "SM: session: " << id << " is marked for removing" << logend; + main_log << log3 << "SM: session: " << id << " is marked for removing" << logend; return false; } @@ -370,7 +360,7 @@ bool SessionManager::IsSessionCorrect(long id, bool has_index, unsigned int inde if( (size_t)difference > config->session_allow_index_difference ) { - log << log2 << "SM: an incorrect session index for session: " << id + main_log << log2 << "SM: an incorrect session index for session: " << id << ", index difference: " << (size_t)difference << logend; IncorrectSessionCheckBan(); @@ -423,7 +413,7 @@ bool SessionManager::SetSessionFromCookie(const std::wstring & cookie) if( !session_id_manager.DecodeToken(cookie, id, index) ) { - log << log2 << "SM: an incorrect cookie string was sent" << logend; + main_log << log2 << "SM: an incorrect cookie string was sent" << logend; BrokenCookieCheckBan(); return false; } @@ -448,14 +438,14 @@ bool SessionManager::IsIPBanned() if( current_ip_ban->expires != 0 && cur->request->start_time >= current_ip_ban->expires ) { - log << log2 << "SM: resetting events counters for this IP" << logend; + main_log << log2 << "SM: resetting events counters for this IP" << logend; current_ip_ban->ResetEventsCounters(); } else if( current_ip_ban->IsIPBanned() ) { PT::Date date = current_ip_ban->expires; - log << log2 << "SM: this ip is bannned to: " << date << " UTC" << logend; + main_log << log2 << "SM: this ip is bannned to: " << date << " UTC" << logend; return true; } } @@ -480,11 +470,11 @@ return false; * so we always have a session * */ -void SessionManager::PrepareSession() +Session * SessionManager::PrepareSession() { session = nullptr; - if( !IsIPBanned() && cur->request->function ) + if( !IsIPBanned() ) { CookieTab::iterator i = cur->request->cookie_tab.find(config->http_session_id_name); @@ -493,39 +483,54 @@ void SessionManager::PrepareSession() if( !SetSessionFromCookie(i->second) ) { cur->request->cookie_tab.erase(i); - - if( cur->request->function->need_session ) - { - // IP could be banned in SetSessionFromCookie() when the cookie string was incorrect - if( !current_ip_ban || !current_ip_ban->IsIPBanned() ) - { - CreateSession(); - } - } } } else { - if( cur->request->function->need_session ) + if( cur->request->function && cur->request->function->need_session ) { - if( !ShouldNoSessionCookieGenerateTmpSession() ) - { - CreateSession(); - } + NoSessionCookieWasSent(); } } } + if( !session && cur->request->function && cur->request->function->need_session ) + { + if( !current_ip_ban || !current_ip_ban->IsIPBanned() ) + { + CreateSession(); + } + } + if( !session ) { SetTemporarySession(); } session->ip_ban = current_ip_ban; + return session; } +Session * SessionManager::CheckIfFunctionRequireSession() +{ + if( cur->request->function && cur->request->function->need_session ) + { + if( session == &temporary_session ) + { + if( !current_ip_ban || !current_ip_ban->IsIPBanned() ) + { + CreateSession(); + session->ip_ban = current_ip_ban; + } + } + } + + return session; +} + + Session * SessionManager::FindSession(long id) { SessionContainer::Iterator i = session_tab.FindById(id); @@ -596,11 +601,11 @@ SessionContainer::Iterator i = session_tab.FindById(old_id); if( changed ) plugin->Call(&(*i), WINIX_SESSION_CHANGED_ID, old_id, new_id); else - log << log1 << "SM: I cannot create a new session id (still uses old one)" << logend; + main_log << log1 << "SM: I cannot create a new session id (still uses old one)" << logend; } else { - log << log2 << "SM: there is no a session with id: " << old_id << logend; + main_log << log2 << "SM: there is no a session with id: " << old_id << logend; } return changed; @@ -611,7 +616,7 @@ void SessionManager::InitTmpSession() { Session * old_session = cur->session; - log << log4 << "SM: initializing temporary session" << logend; + main_log << log4 << "SM: initializing temporary session" << logend; cur->session = &temporary_session; plugin->Call(WINIX_SESSION_CREATED); @@ -624,7 +629,7 @@ void SessionManager::UninitTmpSession() { Session * old_session = cur->session; - log << log4 << "SM: uninitializing temporary session" << logend; + main_log << log4 << "SM: uninitializing temporary session" << logend; cur->session = &temporary_session; @@ -690,11 +695,11 @@ char file_path[WINIX_OS_PATH_SIZE]; if( !file ) { - log << log1 << "SM: cannot open the session file for writing - sessions lost" << logend; + main_log << log1 << "SM: cannot open the session file for writing - sessions lost" << logend; return; } - log << log2 << "SM: saving sessions" << logend; + main_log << log2 << "SM: saving sessions" << logend; long len = 0; SessionContainer::Iterator i = session_tab.Begin(); @@ -714,7 +719,7 @@ char file_path[WINIX_OS_PATH_SIZE]; file.close(); chmod(file_path, 0600); - log << log2 << "SM: saved " << len << " session(s)" << logend; + main_log << log2 << "SM: saved " << len << " session(s)" << logend; } diff --git a/winixd/core/sessionmanager.h b/winixd/core/sessionmanager.h index 4a45638..393d521 100644 --- a/winixd/core/sessionmanager.h +++ b/winixd/core/sessionmanager.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2008-2018, Tomasz Sowa + * Copyright (c) 2008-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -62,7 +62,6 @@ public: SessionManager(); void SetCur(Cur * pcur); - void SetConfig(Config * pconfig); void SetSystem(System * psystem); void SetLastContainer(LastContainer * plast_container); @@ -71,7 +70,12 @@ public: // can return a null pointer Session * FindSession(long id); - void PrepareSession(); + Session * PrepareSession(); + + // some functions require a session (need_session flag) so if there was + // a temporary session and a function requires a session then we create a new session + Session * CheckIfFunctionRequireSession(); + void DeleteSessions(); // deleting all sessions bool ChangeSessionId(long old_id); @@ -108,7 +112,6 @@ public: private: - Config * config; Cur * cur; System * system; LastContainer * last_container; @@ -133,7 +136,7 @@ private: void SetFirstExpirationTime(IPBan * ip_ban); void BrokenCookieCheckBan(); void IncorrectSessionCheckBan(); - bool ShouldNoSessionCookieGenerateTmpSession(); + void NoSessionCookieWasSent(); /* diff --git a/winixd/core/threadmanager.cpp b/winixd/core/threadmanager.cpp index c6a0b8e..45087e1 100644 --- a/winixd/core/threadmanager.cpp +++ b/winixd/core/threadmanager.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2011-2018, Tomasz Sowa + * Copyright (c) 2011-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -72,27 +72,31 @@ sigset_t set; void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name) { -ThreadItem item; + thread_tab.emplace_back(); + ThreadItem & item = thread_tab.back(); + item.thread_item_data = new ThreadItemData(); + ThreadItemData & data = *item.thread_item_data; item.object = pbase; item.name = thread_name; + item.object->set_dependency(this); - thread_tab.push_back(item); - thread_tab.back().thread_item_data = new ThreadItemData(); - thread_tab.back().object->set_dependency(this); + // main log buffer (from the main thread) + item.object->set_main_log_buffer(log.GetLogBuffer()); + item.object->set_main_file_log(log.GetFileLog()); // the logger buffer and model_connector are different - ThreadItemData & data = *thread_tab.back().thread_item_data; - thread_tab.back().object->set_log_buffer(&data.log_buffer); + item.object->set_log_buffer(&data.log_buffer); //data.postgresql_connector.set_logger(logger); data.postgresql_connector.set_conn_param(config->db_database, config->db_user, config->db_pass); - data.postgresql_connector.set_logger(thread_tab.back().object->get_logger()); + data.postgresql_connector.set_logger(item.object->get_logger()); data.postgresql_connector.set_log_queries(config->log_db_query); data.postgresql_connector.wait_for_connection(); data.model_connector.set_db_connector(data.postgresql_connector); data.model_connector.set_flat_connector(data.json_connector); - thread_tab.back().object->set_model_connector(&data.model_connector); + + item.object->set_model_connector(&data.model_connector); if( were_started ) { @@ -144,8 +148,6 @@ void ThreadManager::StartAll() void ThreadManager::Start(int id, ThreadItem * item) { - item->object->SetSynchro(synchro); - if( item->object->StartThread() ) { log << log4 << "TM: thread " << id << " (" << item->object->ThreadId() << ", name: " diff --git a/winixd/plugins/export/exportthread.cpp b/winixd/plugins/export/exportthread.cpp index 7242488..8eeca3e 100644 --- a/winixd/plugins/export/exportthread.cpp +++ b/winixd/plugins/export/exportthread.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2011-2018, Tomasz Sowa + * Copyright (c) 2011-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -183,10 +183,7 @@ void ExportThread::DoMessage() if( message_work.errors > conn_max_errors ) { message_work.can_remove = true; - - Lock(); log << log1 << "Export: too many errors for uploading " << message_work.path << " (skipping)" << logend << logsave; - Unlock(); } } } @@ -206,9 +203,7 @@ bool ExportThread::Fetch(const char * url) if( !curl ) { - Lock(); log << log1 << "Export: I can't use curl" << logend; - Unlock(); return false; } @@ -232,9 +227,7 @@ bool ExportThread::Fetch(const char * url) if( res != 0 ) { - Lock(); log << log1 << "Export: download failed: " << error_buf << logend << logsave; - Unlock(); return false; } @@ -288,9 +281,7 @@ FILE * file = 0; if( !file ) { - Lock(); log << log1 << "Export: I cannot open the file: " << local_path << logend; - Unlock(); return false; } else @@ -307,9 +298,7 @@ FILE * file = 0; if( !curl ) { - Lock(); log << log1 << "Export: I can't use curl (sending)" << logend; - Unlock(); if( file ) fclose(file); @@ -355,16 +344,12 @@ FILE * file = 0; if( res != 0 ) { - Lock(); log << log1 << "Export: upload failed: " << error_buf << " (" << ftp_server << ")" << logend << logsave; - Unlock(); return false; } else { - Lock(); log << log2 << "Export: uploaded: " << ftp_server << logend << logsave; - Unlock(); } return true;