diff --git a/source/client/connection.cpp b/source/client/connection.cpp index 302d84ab..437ec093 100755 --- a/source/client/connection.cpp +++ b/source/client/connection.cpp @@ -12,6 +12,7 @@ #include "connection.h" #include +#include #include #include #include @@ -65,6 +66,10 @@ #define PORT_STRING 64 static int client_socket = -1; +// Published socket for cancellation from a different thread. The request +// thread owns socket lifetime and XDR cleanup. +static std::atomic cancel_socket{-1}; +static std::atomic cancelled{false}; static SOCKETLIST client_socketlist; // List of open sockets int connectionOpen() @@ -72,6 +77,23 @@ int connectionOpen() return client_socket != -1; } +int connectionCancelled() +{ + return cancelled.load() ? 1 : 0; +} + +int cancelConnection() +{ + int fd = cancel_socket.load(); + if (fd < 0 || cancelled.exchange(true)) return 0; + const char command = 'C'; + bool sent = ::send(fd, &command, 1, MSG_OOB) == 1; + // Wake any blocked recv/select on the query thread. Only the query + // thread can close the socket and destroy the XDR streams. + ::shutdown(fd, SHUT_RDWR); + return sent ? 1 : 0; +} + int reconnect(ENVIRONMENT* environment, XDR** client_input, XDR** client_output, time_t* tv_server_start, int* user_timeout) { @@ -126,6 +148,8 @@ int reconnect(ENVIRONMENT* environment, XDR** client_input, XDR** client_output, client_socket = client_socketlist.sockets[newsocketId].fh; *client_input = client_socketlist.sockets[newsocketId].Input; *client_output = client_socketlist.sockets[newsocketId].Output; + cancel_socket.store(client_socket); + cancelled.store(false); environment->server_change_socket = 0; environment->server_socket = client_socketlist.sockets[newsocketId].fh; @@ -559,6 +583,9 @@ int createConnection(XDR* client_input, XDR* client_output, time_t *tv_server_st putUdaClientSSLSocket(client_socket); #endif + cancel_socket.store(client_socket); + cancelled.store(false); + return 0; } @@ -569,6 +596,7 @@ void closeAllConnections() void closeConnection(ClosedownType type) { + cancel_socket.store(-1); if (client_socket >= 0 && type != ClosedownType::CLOSE_ALL) { closeClientSocket(&client_socketlist, client_socket); } else { @@ -576,6 +604,7 @@ void closeConnection(ClosedownType type) } client_socket = -1; + cancelled.store(false); } int clientWriteout(void* iohandle ALLOW_UNUSED_TYPE, char* buf, int count) diff --git a/source/client/connection.h b/source/client/connection.h index af40d174..157526bd 100755 --- a/source/client/connection.h +++ b/source/client/connection.h @@ -14,6 +14,11 @@ extern "C" { #endif int connectionOpen(); +// Called from a second thread while an API request is blocked. +// Cancels the entire TCP session (POSIX, no TLS, xinetd server only). +LIBRARY_API int cancelConnection(); +// Checked by the request thread; do not destroy XDR from the cancelling thread. +int connectionCancelled(); int reconnect(ENVIRONMENT* environment, XDR** client_input, XDR** client_output, time_t* tv_server_start, int* user_timeout); int createConnection(XDR* client_input, XDR* client_output, time_t *tv_server_start, int user_timeout, int client_version); diff --git a/source/client/udaClient.cpp b/source/client/udaClient.cpp index f4cb53b2..5bd30d3e 100755 --- a/source/client/udaClient.cpp +++ b/source/client/udaClient.cpp @@ -473,6 +473,12 @@ int idamClient(REQUEST_BLOCK* request_block, int* indices) } } + // Cancellation invalidates TCP/XDR framing: only this request thread + // closes the old session and recreates the streams. + if (connectionCancelled()) { + closedown(ClosedownType::CLOSE_SOCKETS, nullptr, client_input, client_output, &reopen_logs); + } + //------------------------------------------------------------------------- // Server Age (secs) = Time since the Server was last called diff --git a/source/client/udaGetAPI.cpp b/source/client/udaGetAPI.cpp index b10273d9..a5e89541 100755 --- a/source/client/udaGetAPI.cpp +++ b/source/client/udaGetAPI.cpp @@ -22,6 +22,7 @@ #include "makeClientRequestBlock.h" #include "startup.h" #include "udaClient.h" +#include "connection.h" #include "accAPI.h" #include "getEnvironment.h" @@ -29,6 +30,11 @@ #include #endif +int udaCancelAPI(void) +{ + return cancelConnection(); +} + //! the principal IDAM API /** All requests are passed via two string arguments. These take multiple forms or patterns.\n\n Data source patterns:\n\n diff --git a/source/client/udaGetAPI.h b/source/client/udaGetAPI.h index 51c61669..56b567c7 100755 --- a/source/client/udaGetAPI.h +++ b/source/client/udaGetAPI.h @@ -14,6 +14,10 @@ extern "C" { # define idamGetBatchAPIWithHost idamGetBatchAPIWithHostFat #endif +// Returns 1 when the urgent cancellation byte was sent, 0 otherwise. +// Call from another thread; not supported with TLS or FATCLIENT. +LIBRARY_API int udaCancelAPI(void); + LIBRARY_API int idamGetAPI(const char *data_object, const char *data_source); LIBRARY_API int idamGetBatchAPI(const char** uda_signals, const char** sources, int count, int* handles); LIBRARY_API int idamGetAPIWithHost(const char *data_object, const char *data_source, const char *host, int port); diff --git a/source/client2/client.cpp b/source/client2/client.cpp index d29a1948..042803a1 100644 --- a/source/client2/client.cpp +++ b/source/client2/client.cpp @@ -310,6 +310,14 @@ int uda::client::Client::get_server_error_stack_record_code(int record) int uda::client::Client::get_requests(RequestBlock& request_block, int* indices) { + + // A previous cancellation invalidated the socket and XDR framing. + // Only the query thread performs the actual teardown/reconnect. + if (connection_.cancelled()) { + closedown(ClosedownType::CLOSE_SOCKETS, &connection_, client_input_, client_output_, + &reopen_logs_, &env_host_, &env_port_); + } + initServerBlock(&server_block_, 0); initUdaErrorStack(); @@ -829,6 +837,11 @@ int uda::client::Client::receive_server_block() return 0; } +bool uda::client::Client::cancel() +{ + return connection_.cancel(); +} + void uda::client::Client::clear() { data_blocks_.clear(); diff --git a/source/client2/client.hpp b/source/client2/client.hpp index b80da5a3..dc576cf6 100644 --- a/source/client2/client.hpp +++ b/source/client2/client.hpp @@ -46,6 +46,10 @@ class Client void set_host(std::string_view host); void set_port(int port); void clear(); + + // Thread-safe cancellation signal; the caller must join the query thread + // before reusing this Client or destroying its XDR streams. + bool cancel(); DATA_BLOCK* current_data_block(); DATA_BLOCK* data_block(int handle); int new_handle(); diff --git a/source/client2/connection.cpp b/source/client2/connection.cpp index bac52e02..605cf9a4 100755 --- a/source/client2/connection.cpp +++ b/source/client2/connection.cpp @@ -65,6 +65,19 @@ int uda::client::Connection::open() return client_socket != -1; } +bool uda::client::Connection::cancel() +{ + const int fd = client_socket; + if (fd < 0 || cancelled_.exchange(true)) return false; + // An out-of-band byte is delivered on the *existing* TCP connection. + // The server's SIGURG handler terminates its connection-specific process. + const char command = 'C'; + const bool sent = ::send(fd, &command, 1, MSG_OOB) == 1; + // Wake a client thread blocked in read/select without destroying XDR here. + ::shutdown(fd, SHUT_RDWR); + return sent; +} + int uda::client::Connection::find_socket(int fh) { int i = 0; @@ -554,6 +567,7 @@ void uda::client::Connection::close_down(ClosedownType type) } client_socket = -1; + cancelled_.store(false); } void update_select_params(int fd, fd_set *rfds, struct timeval *tv) { diff --git a/source/client2/connection.hpp b/source/client2/connection.hpp index a253299d..faa5fe05 100755 --- a/source/client2/connection.hpp +++ b/source/client2/connection.hpp @@ -4,6 +4,7 @@ #define UDA_CLIENT_CONNECTION_H #include +#include #include #include @@ -27,6 +28,12 @@ class Connection { , socket_list() {} int open(); + + // May be called from a second thread while a query is blocking in get(). + // Cancels the entire server session, not just an individual batched request. + bool cancel(); + bool cancelled() const { return cancelled_.load(); } + int reconnect(XDR** client_input, XDR** client_output, time_t* tv_server_start, int* user_timeout); int create(XDR* client_input, XDR* client_output, const HostList& host_list); void close_down(ClosedownType type); @@ -36,6 +43,7 @@ class Connection { private: int client_socket = -1; + std::atomic cancelled_{false}; Environment& environment; std::vector socket_list; // List of open sockets diff --git a/source/client2/udaGetAPI.cpp b/source/client2/udaGetAPI.cpp index 1dd62679..00ba6d22 100644 --- a/source/client2/udaGetAPI.cpp +++ b/source/client2/udaGetAPI.cpp @@ -6,6 +6,11 @@ #include +int udaCancelAPI(void) +{ + return uda::client::ThreadClient::instance().cancel() ? 1 : 0; +} + int udaGetAPI(const char *data_object, const char *data_source) { auto& client = uda::client::ThreadClient::instance(); @@ -66,4 +71,4 @@ int udaGetBatchAPIWithHost(const char** data_signals, const char** data_sources, } catch (uda::exceptions::UDAException& ex) { return -1; } -} \ No newline at end of file +} diff --git a/source/client2/udaGetAPI.h b/source/client2/udaGetAPI.h index a08bb636..efd4ceb7 100755 --- a/source/client2/udaGetAPI.h +++ b/source/client2/udaGetAPI.h @@ -14,6 +14,7 @@ extern "C" { # define idamGetBatchAPIWithHost idamGetBatchAPIWithHostFat #endif +LIBRARY_API int udaCancelAPI(void); LIBRARY_API int udaGetAPI(const char *data_object, const char *data_source); LIBRARY_API int udaGetBatchAPI(const char** uda_signals, const char** sources, int count, int* handles); LIBRARY_API int udaGetAPIWithHost(const char *data_object, const char *data_source, const char *host, int port); diff --git a/source/server/udaServer.cpp b/source/server/udaServer.cpp index 9edae1f1..44968c51 100755 --- a/source/server/udaServer.cpp +++ b/source/server/udaServer.cpp @@ -7,6 +7,10 @@ #endif #include +#include +#include +#include +#include #include #include @@ -41,6 +45,18 @@ # include #endif +void hard_cancel(int) { _exit(0); } + +void enable_hard_cancel() +{ + if (fcntl(STDIN_FILENO, F_SETOWN, getpid()) == -1) return; + + struct sigaction action = {}; + action.sa_handler = hard_cancel; + sigemptyset(&action.sa_mask); + sigaction(SIGURG, &action, nullptr); +} + //-------------------------------------------------------------------------------------- // static globals @@ -146,6 +162,8 @@ int udaServer(CLIENT_BLOCK client_block) if ((err = startupServer(&server_block, server_input, server_output, &io_data)) != 0) return err; + enable_hard_cancel(); + #ifdef SECURITYENABLED err = authenticateClient(&client_block, &server_block); #else diff --git a/source/server2/server.cpp b/source/server2/server.cpp index ffa93517..d31f357d 100644 --- a/source/server2/server.cpp +++ b/source/server2/server.cpp @@ -2,6 +2,9 @@ #include #include +#include +#include +#include #include #include "clientserver/errorLog.h" @@ -20,6 +23,17 @@ # include "authentication/udaServerSSL.h" #endif +void cancel_session(int) { _exit(0); } // Async-signal-safe; no C++ destructors. + +void enable_session_cancel() +{ + if (fcntl(STDIN_FILENO, F_SETOWN, getpid()) == -1) return; + struct sigaction action = {}; + action.sa_handler = cancel_session; + sigemptyset(&action.sa_mask); + sigaction(SIGURG, &action, nullptr); +} + void free_data_blocks(std::vector& data_blocks) { for (auto& data_block : data_blocks) { @@ -159,6 +173,7 @@ void uda::Server::startup() void uda::Server::run() { startup(); + enable_session_cancel(); handshake_client(); if (!server_closedown_) { loop();