From a3a068d6d398cba439d50f5f326d4c911ac40bc5 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 30 Jul 2025 19:06:02 +0200 Subject: [PATCH] os: connection: simplify connection error sending It's such a cold and rarely used path, we really don't need writev() for efficiency, so instead doing two trivial write()'s. And the complex size calculation as well as extra padding isn't necessary, if we just make the string of size 4*n. Signed-off-by: Enrico Weigelt, metux IT consult --- os/connection.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/os/connection.c b/os/connection.c index 0d1876485d..7a07670be6 100644 --- a/os/connection.c +++ b/os/connection.c @@ -701,8 +701,6 @@ EstablishNewConnections(int curconn, int ready, void *data) return; } -#define NOROOM "Maximum number of clients reached" - /************ * ErrorConnMax * Fail a connection due to lack of client or file descriptor space @@ -717,29 +715,28 @@ ConnMaxNotify(int fd, int events, void *data) /* try to read the byte-order of the connection */ (void) _XSERVTransRead(trans_conn, &order, 1); if (order == 'l' || order == 'B' || order == 'r' || order == 'R') { - xConnSetupPrefix csp; - char pad[3] = { 0, 0, 0 }; int whichbyte = 1; - struct iovec iov[3]; - csp.success = xFalse; - csp.lengthReason = sizeof(NOROOM) - 1; - csp.length = (sizeof(NOROOM) + 2) >> 2; - csp.majorVersion = X_PROTOCOL; - csp.minorVersion = X_PROTOCOL_REVISION; +/* 36 bytes (with zero) -- needs to be padded to 4*n */ +#define ERR_TEXT "Maximum number of clients reached\0\0" + + xConnSetupPrefix csp = { + .success = xFalse, + .lengthReason = sizeof(ERR_TEXT), + .length = sizeof(ERR_TEXT) >> 2, + .majorVersion = X_PROTOCOL, + .minorVersion = X_PROTOCOL_REVISION, + }; + if (((*(char *) &whichbyte) && (order == 'B' || order == 'R')) || (!(*(char *) &whichbyte) && (order == 'l' || order == 'r'))) { swaps(&csp.majorVersion); swaps(&csp.minorVersion); swaps(&csp.length); } - iov[0].iov_len = sz_xConnSetupPrefix; - iov[0].iov_base = (char *) &csp; - iov[1].iov_len = csp.lengthReason; - iov[1].iov_base = (void *) NOROOM; - iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3; - iov[2].iov_base = pad; - (void) _XSERVTransWritev(trans_conn, iov, 3); + + _XSERVTransWrite(trans_conn, (const char*)&csp, sizeof(csp)); + _XSERVTransWrite(trans_conn, ERR_TEXT, sizeof(ERR_TEXT)); } RemoveNotifyFd(trans_conn->fd); _XSERVTransClose(trans_conn);