Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions source/client/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "connection.h"

#include <cstdlib>
#include <atomic>
#include <cerrno>
#include <csignal>
#include <vector>
Expand Down Expand Up @@ -65,13 +66,34 @@
#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<int> cancel_socket{-1};
static std::atomic<bool> cancelled{false};
static SOCKETLIST client_socketlist; // List of open sockets

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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -569,13 +596,15 @@ void closeAllConnections()

void closeConnection(ClosedownType type)
{
cancel_socket.store(-1);
if (client_socket >= 0 && type != ClosedownType::CLOSE_ALL) {
closeClientSocket(&client_socketlist, client_socket);
} else {
closeClientSockets(&client_socketlist);
}

client_socket = -1;
cancelled.store(false);
}

int clientWriteout(void* iohandle ALLOW_UNUSED_TYPE, char* buf, int count)
Expand Down
5 changes: 5 additions & 0 deletions source/client/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions source/client/udaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions source/client/udaGetAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
#include "makeClientRequestBlock.h"
#include "startup.h"
#include "udaClient.h"
#include "connection.h"
#include "accAPI.h"
#include "getEnvironment.h"

#ifdef MEMDEBUG
#include <mcheck.h>
#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
Expand Down
4 changes: 4 additions & 0 deletions source/client/udaGetAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions source/client2/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions source/client2/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions source/client2/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions source/client2/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define UDA_CLIENT_CONNECTION_H

#include <vector>
#include <atomic>

#include <clientserver/socketStructs.h>
#include <clientserver/udaStructs.h>
Expand All @@ -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);
Expand All @@ -36,6 +43,7 @@ class Connection {

private:
int client_socket = -1;
std::atomic<bool> cancelled_{false};
Environment& environment;
std::vector<Sockets> socket_list; // List of open sockets

Expand Down
7 changes: 6 additions & 1 deletion source/client2/udaGetAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#include <unordered_map>

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();
Expand Down Expand Up @@ -66,4 +71,4 @@ int udaGetBatchAPIWithHost(const char** data_signals, const char** data_sources,
} catch (uda::exceptions::UDAException& ex) {
return -1;
}
}
}
1 change: 1 addition & 0 deletions source/client2/udaGetAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions source/server/udaServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#endif

#include <tuple>
#include <cstdlib>
#include <unistd.h>
#include <csignal>
#include <fcntl.h>
#include <string>

#include <clientserver/initStructs.h>
Expand Down Expand Up @@ -41,6 +45,18 @@
# include <authentication/udaServerSSL.h>
#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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions source/server2/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <string>
#include <unistd.h>
#include <csignal>
#include <fcntl.h>
#include <sys/socket.h>
#include <fmt/format.h>

#include "clientserver/errorLog.h"
Expand All @@ -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<DataBlock>& data_blocks)
{
for (auto& data_block : data_blocks) {
Expand Down Expand Up @@ -159,6 +173,7 @@ void uda::Server::startup()
void uda::Server::run()
{
startup();
enable_session_cancel();
handshake_client();
if (!server_closedown_) {
loop();
Expand Down
Loading