Thread/EPoll lifecycle: - Thread::~Thread() now actually joins/frees its worker thread (was empty - the OS thread kept running against already-freed EPoll/Socket objects on any early teardown, a confirmed "double free" crash) - EPoll::~EPoll() calls stop() if never explicitly stopped, for the same reason - epoll_wait() now uses a 1s timeout instead of blocking forever, so a worker thread can actually notice isStopping() and let itself be joined - EPoll::processCommand() iterated threads by value, silently double-freeing the real vector's Thread the moment each copy went out of scope - ThreadScope gained onThreadStart()/onThreadEnd() hooks (actually wired up now - the constructor that was supposed to store the scope discarded it) for per-thread C-library init (e.g. MySQL client) Socket/TCPSocket/TLS: - Socket::writeSocket(): give up and shut down a connection after 50 consecutive zero-progress sends instead of retrying forever - SSL_ERROR_WANT_READ on a write needs a READ to make progress, but resetSocket() re-arms EPOLLWRNORM regardless, which an already-writable TCP socket satisfies immediately - a real stuck connection generated 55GB, then 12.9GB of debug log this way before being caught - Socket::writeSocket() now honors sendData()'s actual return value (previously discarded, entry popped unconditionally after one call) - a large response could get silently truncated past what fit in one write()/SSL_write() call - TLS::registerSocket()/sendData()/receiveData(): `initialized` now only goes true once SSL_set_fd() has actually succeeded, and sendData()/receiveData() check it first - fixes a real SIGSEGV where a new session's own queued greeting could reach SSL_write() on a different epoll thread before this session's handshake setup had run - TCPServer::connectionRequest() now performs the TLS handshake ON THE ACCEPTED SESSION, not the listening server itself (was building SSL state on the wrong object, leaving every session's own `ssl` uninitialized) - TCPServer::receiveData() always uses the plain (non-TLS) read path - the listening socket is never itself a TLS peer - TCPSocket's block-mode literal reading used an unset `blockLength` field instead of `blockSize`, and could busy-spin forever on a multi-packet literal - TCPSocket line scanning replaced an unbounded strcspn() (assumes a NUL-terminated buffer; read past a non-NUL-terminated receive buffer into adjacent heap memory) with a bounds-checked memchr() scan Protocol/session handling: - CommandList::processRequest() now case-folds the command keyword before lookup - SMTP/POP3/IMAP commands are spec-required to be case-insensitive; a client sending "Mail From:" instead of "MAIL FROM:" previously failed to dispatch at all - TCPSession::protocol() no longer drops blank lines while a handler has grabbed input (e.g. __SMTP_DATA reading a message body, an IMAP APPEND literal) - those handlers depend on seeing blank lines as real content, not commands; this silently corrupted any message body containing a blank line - TCPSession's uuid alias fixed: streaming a raw uuid_t via operator<< triggered libstdc++'s unsigned-char* overload (strlen() on 16 binary bytes, a real stack-buffer-overflow read), and `alias` pointed at a stack-local that was already destroyed by the time the constructor returned - INotify::processEvent() only treats event->name as a NUL-terminated string when event->len > 0 (per inotify(7)) instead of unconditional strlen() on a field the kernel doesn't guarantee is even present - a confirmed heap-buffer-overflow read that had been misdiagnosed as a MySQL thread-safety issue
123 lines
3.9 KiB
C++
123 lines
3.9 KiB
C++
#ifndef __EPoll_h__
|
|
#define __EPoll_h__
|
|
|
|
#include "Log.h"
|
|
#include "Socket.h"
|
|
#include "Thread.h"
|
|
#include "TCPSession.h"
|
|
#include "Command.h"
|
|
|
|
namespace core {
|
|
|
|
///
|
|
/// EPoll
|
|
///
|
|
/// Manage socket events from the epoll system call.
|
|
///
|
|
/// Use this object to establish a socket server using the epoll network structure of Linux.
|
|
///
|
|
/// Use this object to establish the basis of working with multiple sockets of all sorts
|
|
/// using the epoll capabilities of the Linux platform.
|
|
/// Socket objects can register with EPoll which will establish a communication mechanism
|
|
/// with that socket.
|
|
///
|
|
/// The maximum number of sockets to communicate with is specified on the
|
|
/// start method.
|
|
///
|
|
/// Threads are used to establish a read queue for epoll. The desired number of threads (or
|
|
/// queues) is established by a parameter on the start method.
|
|
///
|
|
|
|
class EPoll : public Command {
|
|
|
|
public:
|
|
|
|
long long eventId = 0;
|
|
|
|
///
|
|
/// The constructor for the BMAEPoll object.
|
|
///
|
|
|
|
EPoll();
|
|
|
|
///
|
|
/// The destructor for the BMAEPoll object.
|
|
///
|
|
|
|
~EPoll();
|
|
|
|
///
|
|
/// Use the start() method to initiate the threads and begin epoll queue processing.
|
|
///
|
|
/// @param numberOfThreads the number of threads to start for processing epoll entries.
|
|
/// @param maxSockets the maximum number of open sockets that epoll will manage.
|
|
/// @param scope optional - if given, its onThreadStart()/onThreadEnd() run once on
|
|
/// each worker thread, at the top/bottom of that thread's own lifetime. The SAME
|
|
/// scope instance is shared across every worker thread - see ThreadScope's own
|
|
/// doc comment. Not owned by EPoll; caller keeps it alive for the server's lifetime.
|
|
///
|
|
|
|
bool start(int numberOfThreads, int maxSockets, ThreadScope *scope = NULL); ///< Start the BMAEPoll processing.
|
|
|
|
///
|
|
/// Use the stop() method to initiate the shutdown process for the epoll socket management.
|
|
///
|
|
/// A complete shutdown of all managed sockets will be initiated by this method call.
|
|
///
|
|
|
|
bool stop(); ///< Stop and shut down the BMAEPoll processing.
|
|
|
|
///
|
|
/// This method returns a true if the stop() method has been called and the epoll system
|
|
/// is shutting.
|
|
///
|
|
|
|
bool isStopping(); ///< Returns a true if the stop command has been requested.
|
|
|
|
///
|
|
/// Use this method to obtain the current descriptor socket number for the epoll function call.
|
|
///
|
|
|
|
int getDescriptor(); ///< Return the descriptor for the ePoll socket.
|
|
|
|
///
|
|
/// The maximum number of sockets that can be managed by the epoll system.
|
|
///
|
|
|
|
int maxSockets; ///< The maximum number of socket allowed.
|
|
|
|
///
|
|
/// Receive the epoll events and dispatch the event to the socket making the request.
|
|
///
|
|
|
|
void eventReceived(struct epoll_event event); ///< Dispatch event to appropriate socket.
|
|
|
|
///
|
|
/// The processCommand() method displays the thread array to the requesting console via the
|
|
/// session passed as parameter.
|
|
///
|
|
/// @param session the session to write the requested data to.
|
|
///
|
|
|
|
int processCommand(coreutils::ZString &request, TCPSession &session) override; ///<Output the threads array to the console.
|
|
|
|
private:
|
|
|
|
int epfd;
|
|
int numberOfThreads;
|
|
std::vector<Thread> threads;
|
|
volatile bool terminateThreads;
|
|
// Guards ~EPoll()'s own call to stop() - false until start()
|
|
// actually runs, so a never-started EPoll (numberOfThreads and
|
|
// threads both still default/empty) doesn't have its destructor
|
|
// iterate a garbage thread count. See ~EPoll()'s own doc comment
|
|
// in EPoll.cpp for what this is actually protecting against.
|
|
bool started;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|