Skip to content
Snippets Groups Projects
Commit fbf5a39a authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

We already have conn_api.terminate

We don't need another terminate flag.  Use atomics for the appropriate
members of conn_api now instead of the previous (incorrect) volatile.
parent e28ab10b
No related branches found
No related tags found
No related merge requests found
Pipeline #7635 passed
......@@ -365,7 +365,7 @@ conn_send(const void *vbuffer, size_t buflen, unsigned int timeout)
return found;
}
SOCKET
bool
conn_connect(struct bbslist *bbs)
{
char str[64];
......@@ -433,16 +433,16 @@ conn_connect(struct bbslist *bbs)
"The connection type of this entry is not supported by this build.\n"
"Either the protocol was disabled at compile time, or is\n"
"unsupported on this plattform.");
conn_api.terminate = 1;
conn_api.terminate = true;
}
if (conn_api.connect) {
if (conn_api.connect(bbs)) {
conn_api.terminate = 1;
conn_api.terminate = true;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1)
SLEEP(1);
}
else {
while (conn_api.terminate == 0
while ((!conn_api.terminate)
&& (conn_api.input_thread_running == 0 || conn_api.output_thread_running == 0))
SLEEP(1);
}
......@@ -608,7 +608,7 @@ connected:
freeaddrinfo(res);
if (!bbs->hidepopups)
uifc.pop(NULL);
conn_api.terminate = -1;
conn_api.terminate = true;
if (!bbs->hidepopups) {
switch (failcode) {
case FAILURE_RESOLVE:
......
......@@ -5,6 +5,7 @@
#ifndef _CONN_H_
#define _CONN_H_
#include <stdatomic.h>
#include <stdbool.h>
#include "bbslist.h"
......@@ -70,9 +71,9 @@ struct conn_api {
int type;
int nostatus;
cterm_emulation_t emulation;
volatile int input_thread_running;
volatile int output_thread_running;
volatile int terminate;
atomic_int input_thread_running;
atomic_int output_thread_running;
atomic_bool terminate;
unsigned char *rd_buf;
size_t rd_buf_size;
unsigned char *wr_buf;
......@@ -96,7 +97,7 @@ struct conn_buffer {
int conn_recv_upto(void *buffer, size_t buflen, unsigned int timeout);
int conn_send(const void *buffer, size_t buflen, unsigned int timeout);
int conn_send_raw(const void *buffer, size_t buflen, unsigned int timeout);
SOCKET conn_connect(struct bbslist *bbs);
bool conn_connect(struct bbslist *bbs);
int conn_close(void);
bool conn_connected(void);
size_t conn_data_waiting(void);
......
#define WIN32_LEAN_AND_MEAN
#include <stdatomic.h>
#include <stdbool.h>
#include <windows.h>
#include <wincon.h>
......@@ -17,8 +16,6 @@ PROCESS_INFORMATION pi;
HPCON cpty;
enum ciolib_codepage codepage;
static atomic_bool terminate;
static size_t
get_utf8_span(const uint8_t *b, size_t sz)
{
......@@ -64,7 +61,7 @@ conpty_input_thread(void *args)
SetThreadName("PTY Input");
conn_api.input_thread_running = 1;
while (!terminate && !conn_api.terminate) {
while (!conn_api.terminate) {
if (GetExitCodeProcess(pi.hProcess, &ec)) {
if (ec != STILL_ACTIVE)
break;
......@@ -84,7 +81,7 @@ conpty_input_thread(void *args)
if (cps == NULL)
break;
buffered = 0;
while (!terminate && !conn_api.terminate && buffered < sz) {
while (!conn_api.terminate && buffered < sz) {
pthread_mutex_lock(&(conn_inbuf.mutex));
buffer = conn_buf_wait_free(&conn_inbuf, sz - buffered, 100);
buffered += conn_buf_put(&conn_inbuf, cps + buffered, buffer);
......@@ -95,7 +92,7 @@ conpty_input_thread(void *args)
memmove(conn_api.rd_buf, &conn_api.rd_buf[utf8_span], fill);
free(cps);
}
terminate = true;
conn_api.terminate = true;
conn_api.input_thread_running = 2;
}
......@@ -108,7 +105,7 @@ conpty_output_thread(void *args)
SetThreadName("PTY Output");
conn_api.output_thread_running = 1;
while (!terminate && !conn_api.terminate) {
while (!conn_api.terminate) {
if (GetExitCodeProcess(pi.hProcess, &ec)) {
if (ec != STILL_ACTIVE)
break;
......@@ -127,9 +124,9 @@ conpty_output_thread(void *args)
if (utf == NULL)
break;
size_t sent = 0;
while (!terminate && !conn_api.terminate && sent < sz) {
while (!conn_api.terminate && sent < sz) {
if (!WriteFile(inputWrite, utf + sent, sz - sent, &ret, NULL)) {
terminate = true;
conn_api.terminate = true;
break;
}
sent += ret;
......@@ -140,7 +137,7 @@ conpty_output_thread(void *args)
pthread_mutex_unlock(&(conn_outbuf.mutex));
}
}
terminate = true;
conn_api.terminate = true;
conn_api.output_thread_running = 2;
}
......@@ -271,7 +268,6 @@ int conpty_connect(struct bbslist *bbs)
}
conn_api.wr_buf_size = BUFFER_SIZE;
terminate = false;
_beginthread(conpty_output_thread, 0, NULL);
_beginthread(conpty_input_thread, 0, NULL);
......@@ -284,8 +280,7 @@ conpty_close(void)
char garbage[1024];
DWORD ret;
conn_api.terminate = 1;
terminate = true;
conn_api.terminate = true;
TerminateProcess(pi.hProcess, 0);
WaitForSingleObject(pi.hProcess, 1000);
ClosePseudoConsole(cpty);
......
......@@ -5,7 +5,6 @@
#ifdef __unix__
#include <signal.h> // kill()
#include <stdatomic.h>
#include <sys/wait.h> // WEXITSTATUS
#include <unistd.h> /* _POSIX_VDISABLE - needed when termios.h is broken */
......@@ -147,7 +146,6 @@
#include "uifcinit.h"
#include "window.h"
extern int default_font;
static atomic_bool terminated;
#ifdef NEEDS_CFMAKERAW
......@@ -317,7 +315,7 @@ pty_input_thread(void *args)
SetThreadName("PTY Input");
conn_api.input_thread_running = 1;
while (master != -1 && !conn_api.terminate && !terminated) {
while (master != -1 && !conn_api.terminate) {
if ((i = waitpid(child_pid, &status, WNOHANG)))
break;
FD_ZERO(&rds);
......@@ -336,14 +334,14 @@ pty_input_thread(void *args)
break;
}
buffered = 0;
while (buffered < rd && !conn_api.terminate && !terminated) {
while (buffered < rd && !conn_api.terminate) {
pthread_mutex_lock(&(conn_inbuf.mutex));
buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 100);
buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
pthread_mutex_unlock(&(conn_inbuf.mutex));
}
}
terminated = true;
conn_api.terminate = true;
conn_api.input_thread_running = 2;
}
......@@ -362,7 +360,7 @@ pty_output_thread(void *args)
SetThreadName("PTY Output");
conn_api.output_thread_running = 1;
while (master != -1 && !conn_api.terminate && !terminated) {
while (master != -1 && !conn_api.terminate) {
if (waitpid(child_pid, &status, WNOHANG))
break;
pthread_mutex_lock(&(conn_outbuf.mutex));
......@@ -372,7 +370,7 @@ pty_output_thread(void *args)
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0;
while (master != -1 && sent < wr && !conn_api.terminate && !terminated) {
while (master != -1 && sent < wr && !conn_api.terminate) {
FD_ZERO(&wds);
FD_SET(master, &wds);
tv.tv_sec = 0;
......@@ -399,7 +397,7 @@ pty_output_thread(void *args)
if (ret == -1)
break;
}
terminated = true;
conn_api.terminate = true;
conn_api.output_thread_running = 2;
}
......@@ -564,7 +562,6 @@ pty_connect(struct bbslist *bbs)
}
conn_api.wr_buf_size = BUFFER_SIZE;
terminated = false;
_beginthread(pty_output_thread, 0, NULL);
_beginthread(pty_input_thread, 0, NULL);
......@@ -578,8 +575,7 @@ pty_close(void)
char garbage[1024];
int oldmaster;
terminated = true;
conn_api.terminate = 1;
conn_api.terminate = true;
start = time(NULL);
kill(child_pid, SIGHUP);
while (waitpid(child_pid, &status, WNOHANG) == 0) {
......
......@@ -149,7 +149,6 @@ telnet_connect(struct bbslist *bbs)
conn_api.rx_parse_cb = telnet_rx_parse_cb;
conn_api.tx_parse_cb = telnet_tx_parse_cb;
rlogin_clear_terminated();
_beginthread(rlogin_output_thread, 0, NULL);
_beginthread(rlogin_input_thread, 0, bbs);
// Suppress Go Aheads (both directions)
......
......@@ -2,7 +2,6 @@
/* $Id: modem.c,v 1.32 2020/06/27 08:27:39 deuce Exp $ */
#include <stdatomic.h>
#include <stdbool.h>
#include <stdlib.h>
......@@ -17,7 +16,6 @@
static COM_HANDLE com = COM_HANDLE_INVALID;
static bool seven_bits = false;
static atomic_bool terminated;
void
modem_input_thread(void *args)
......@@ -33,7 +31,7 @@ modem_input_thread(void *args)
if ((comGetModemStatus(com) & COM_DSR) == 0)
monitor_dsr = false;
}
while (com != COM_HANDLE_INVALID && !conn_api.terminate && !terminated) {
while (com != COM_HANDLE_INVALID && !conn_api.terminate) {
rd = comReadBuf(com, (char *)conn_api.rd_buf, conn_api.rd_buf_size, NULL, 100);
// Strip high bits... we *should* check the parity
if (seven_bits) {
......@@ -41,7 +39,7 @@ modem_input_thread(void *args)
conn_api.rd_buf[i] &= 0x7f;
}
buffered = 0;
while (com != COM_HANDLE_INVALID && buffered < rd && !conn_api.terminate && !terminated) {
while (com != COM_HANDLE_INVALID && buffered < rd && !conn_api.terminate) {
pthread_mutex_lock(&(conn_inbuf.mutex));
buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 100);
buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
......@@ -56,7 +54,7 @@ modem_input_thread(void *args)
break;
}
}
terminated = true;
conn_api.terminate = true;
if (args != NULL)
comLowerDTR(com);
conn_api.input_thread_running = 2;
......@@ -77,7 +75,7 @@ modem_output_thread(void *args)
if ((comGetModemStatus(com) & COM_DSR) == 0)
monitor_dsr = false;
}
while (com != COM_HANDLE_INVALID && !conn_api.terminate && !terminated) {
while (com != COM_HANDLE_INVALID && !conn_api.terminate) {
pthread_mutex_lock(&(conn_outbuf.mutex));
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
if (wr) {
......@@ -88,7 +86,7 @@ modem_output_thread(void *args)
conn_api.wr_buf[i] &= 0x7f;
}
sent = 0;
while (com != COM_HANDLE_INVALID && sent < wr && !conn_api.terminate && !terminated) {
while (com != COM_HANDLE_INVALID && sent < wr && !conn_api.terminate) {
// coverity[overflow:SUPPRESS]
ret = comWriteBuf(com, conn_api.wr_buf + sent, wr - sent);
if (ret > 0)
......@@ -109,7 +107,7 @@ modem_output_thread(void *args)
break;
}
}
terminated = true;
conn_api.terminate = true;
conn_api.output_thread_running = 2;
}
......@@ -170,7 +168,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Open Port", "`Cannot Open Port`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
if (bbs->bpsrate) {
......@@ -178,7 +176,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -187,7 +185,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Parity", "`Cannot Set Parity`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -195,7 +193,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Data Bits", "`Cannot Set Data Bits`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -213,7 +211,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n"
"comRaiseDTR() returned an error.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -223,7 +221,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Open Modem", "`Cannot Open Modem`\n\n"
"Cannot open the specified modem device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
if (settings.mdm.com_rate) {
......@@ -231,7 +229,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n"
"Cannot open the specified modem device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -240,7 +238,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Parity", "`Cannot Set Parity`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -248,7 +246,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Set Data Bits", "`Cannot Set Data Bits`\n\n"
"Cannot open the specified serial device.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -265,7 +263,7 @@ modem_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n"
"comRaiseDTR() returned an error.\n");
conn_api.terminate = -1;
conn_api.terminate = true;
comClose(com);
return -1;
}
......@@ -297,7 +295,7 @@ modem_connect(struct bbslist *bbs)
"Check your init string and phone number.\n");
}
}
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
if (strstr(respbuf, settings.mdm.init_string)) /* Echo is on */
......@@ -312,7 +310,7 @@ modem_connect(struct bbslist *bbs)
uifcmsg(respbuf, "`Initialization Error`\n\n"
"The modem did not respond favorably to your initialization string.\n");
}
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
......@@ -334,7 +332,7 @@ modem_connect(struct bbslist *bbs)
uifcmsg(respbuf, "`No Answer`\n\n"
"The modem did not connect within 60 seconds.\n");
}
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
if (strstr(respbuf, bbs->addr)) /* Dial command echoed */
......@@ -349,7 +347,7 @@ modem_connect(struct bbslist *bbs)
uifcmsg(respbuf, "`Connection Failed`\n\n"
"SyncTERM was unable to establish a connection.\n");
}
conn_api.terminate = -1;
conn_api.terminate = true;
return -1;
}
......@@ -386,7 +384,6 @@ modem_connect(struct bbslist *bbs)
}
conn_api.wr_buf_size = BUFFER_SIZE;
terminated = false;
if ((bbs->conn_type == CONN_TYPE_SERIAL) || (bbs->conn_type == CONN_TYPE_SERIAL_NORTS)) {
_beginthread(modem_output_thread, 0, (void *)-1);
_beginthread(modem_input_thread, 0, (void *)-1);
......@@ -405,7 +402,7 @@ modem_connect(struct bbslist *bbs)
int
serial_close(void)
{
conn_api.terminate = 1;
conn_api.terminate = true;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1)
SLEEP(1);
......@@ -424,8 +421,7 @@ modem_close(void)
char garbage[1024];
COM_HANDLE oldcom;
terminated = true;
conn_api.terminate = 1;
conn_api.terminate = true;
if ((comGetModemStatus(com) & COM_DCD) == 0) /* DCD already low */
goto CLOSEIT;
......
......@@ -2,7 +2,6 @@
/* $Id: rlogin.c,v 1.38 2020/06/27 00:04:50 deuce Exp $ */
#include <stdatomic.h>
#include <stdlib.h>
#include "bbslist.h"
......@@ -11,12 +10,6 @@
#include "uifcinit.h"
SOCKET rlogin_sock = INVALID_SOCKET;
static bool terminated;
void rlogin_clear_terminated(void)
{
terminated = false;
}
#ifdef __BORLANDC__
#pragma argsused
......@@ -31,13 +24,13 @@ rlogin_input_thread(void *args)
SetThreadName("RLogin Input");
conn_api.input_thread_running = 1;
while (rlogin_sock != INVALID_SOCKET && !conn_api.terminate && !terminated) {
while (rlogin_sock != INVALID_SOCKET && !conn_api.terminate) {
if (socket_readable(rlogin_sock, 100)) {
rd = recv(rlogin_sock, conn_api.rd_buf, conn_api.rd_buf_size, 0);
if (rd <= 0)
break;
buffered = 0;
while (rlogin_sock != INVALID_SOCKET && buffered < rd && !conn_api.terminate && !terminated) {
while (rlogin_sock != INVALID_SOCKET && buffered < rd && !conn_api.terminate) {
pthread_mutex_lock(&(conn_inbuf.mutex));
buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 1000);
buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
......@@ -45,7 +38,7 @@ rlogin_input_thread(void *args)
}
}
}
terminated = true;
conn_api.terminate = true;
conn_api.input_thread_running = 2;
}
......@@ -62,7 +55,7 @@ rlogin_output_thread(void *args)
SetThreadName("RLogin Output");
conn_api.output_thread_running = 1;
while (rlogin_sock != INVALID_SOCKET && !conn_api.terminate && !terminated) {
while (rlogin_sock != INVALID_SOCKET && !conn_api.terminate) {
pthread_mutex_lock(&(conn_outbuf.mutex));
ret = 0;
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
......@@ -70,7 +63,7 @@ rlogin_output_thread(void *args)
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0;
while (rlogin_sock != INVALID_SOCKET && sent < wr && !conn_api.terminate && !terminated) {
while (rlogin_sock != INVALID_SOCKET && sent < wr && !conn_api.terminate) {
if (socket_writable(rlogin_sock, 100)) {
// coverity[overflow:SUPPRESS]
ret = sendsocket(rlogin_sock, conn_api.wr_buf + sent, wr - sent);
......@@ -87,7 +80,7 @@ rlogin_output_thread(void *args)
if (ret < 0)
break;
}
terminated = true;
conn_api.terminate = true;
conn_api.output_thread_running = 2;
}
......@@ -208,7 +201,6 @@ rlogin_connect(struct bbslist *bbs)
return -1;
}
terminated = false;
_beginthread(rlogin_output_thread, 0, NULL);
_beginthread(rlogin_input_thread, 0, NULL);
......@@ -224,8 +216,7 @@ rlogin_close(void)
char garbage[1024];
SOCKET oldsock;
terminated = true;
conn_api.terminate = 1;
conn_api.terminate = true;
oldsock = rlogin_sock;
rlogin_sock = INVALID_SOCKET;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
......
......@@ -2,7 +2,6 @@
#ifndef _RLOGIN_H_
#define _RLOGIN_H_
void rlogin_clear_terminated(void);
int rlogin_connect(struct bbslist *bbs);
int rlogin_close(void);
void rlogin_input_thread(void *args);
......
......@@ -32,7 +32,6 @@ SOCKET ssh_sock = INVALID_SOCKET;
CRYPT_SESSION ssh_session;
int ssh_channel = -1;
atomic_bool ssh_active;
atomic_bool ssh_complete;
pthread_mutex_t ssh_mutex;
pthread_mutex_t ssh_tx_mutex;
int sftp_channel = -1;
......@@ -64,7 +63,7 @@ FlushData(CRYPT_SESSION sess)
{
int ret = cryptFlushData(sess);
if (ret == CRYPT_ERROR_COMPLETE || ret == CRYPT_ERROR_READ)
ssh_complete = true;
conn_api.terminate = true;
return ret;
}
......@@ -75,7 +74,7 @@ PopData(CRYPT_HANDLE e, void *buf, int len, int *copied)
int ret = cryptPopData(e, buf, len, copied);
cryptSetAttribute(ssh_session, CRYPT_OPTION_NET_READTIMEOUT, 30);
if (ret == CRYPT_ERROR_COMPLETE || ret == CRYPT_ERROR_READ)
ssh_complete = true;
conn_api.terminate = true;
return ret;
}
......@@ -84,7 +83,7 @@ PushData(CRYPT_HANDLE e, void *buf, int len, int *copied)
{
int ret = cryptPushData(e, buf, len, copied);
if (ret == CRYPT_ERROR_COMPLETE || ret == CRYPT_ERROR_WRITE)
ssh_complete = true;
conn_api.terminate = true;
return ret;
}
......@@ -192,12 +191,12 @@ ssh_input_thread(void *args)
SetThreadName("SSH Input");
conn_api.input_thread_running = 1;
while (ssh_active && !conn_api.terminate && !ssh_complete) {
while (ssh_active && !conn_api.terminate) {
sftp_do_finish = false;
pthread_mutex_lock(&ssh_mutex);
if (FlushData(ssh_session) == CRYPT_ERROR_COMPLETE) {
pthread_mutex_unlock(&ssh_mutex);
ssh_complete = true;
conn_api.terminate = true;
break;
}
if (ssh_channel != -1) {
......@@ -215,7 +214,7 @@ ssh_input_thread(void *args)
}
pthread_mutex_unlock(&ssh_mutex);
if (both_gone) {
ssh_complete = true;
conn_api.terminate = true;
break;
}
if (sftp_do_finish) {
......@@ -228,7 +227,7 @@ ssh_input_thread(void *args)
pthread_mutex_lock(&ssh_mutex);
if (FlushData(ssh_session) == CRYPT_ERROR_COMPLETE) {
pthread_mutex_unlock(&ssh_mutex);
ssh_complete = true;
conn_api.terminate = true;
break;
}
......@@ -247,7 +246,7 @@ ssh_input_thread(void *args)
}
if (ssh_channel == -1 && sftp_channel == -1) {
pthread_mutex_unlock(&ssh_mutex);
ssh_complete = true;
conn_api.terminate = true;
break;
}
......@@ -316,7 +315,7 @@ ssh_input_thread(void *args)
pthread_mutex_unlock(&ssh_mutex);
if (rd > 0) {
buffered = 0;
while (buffered < rd && ssh_active && !ssh_complete && !conn_api.terminate) {
while (buffered < rd && ssh_active && !conn_api.terminate) {
pthread_mutex_lock(&(conn_inbuf.mutex));
buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 100);
buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
......@@ -344,7 +343,7 @@ ssh_output_thread(void *args)
SetThreadName("SSH Output");
conn_api.output_thread_running = 1;
// coverity[thread1_checks_field:SUPPRESS]
while (ssh_active && !conn_api.terminate && !channel_gone && !ssh_complete) {
while (ssh_active && !conn_api.terminate && !channel_gone) {
pthread_mutex_lock(&(conn_outbuf.mutex));
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
if (wr) {
......@@ -352,12 +351,12 @@ ssh_output_thread(void *args)
pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0;
pthread_mutex_lock(&ssh_tx_mutex);
while (sent < wr && ssh_active && !conn_api.terminate && !channel_gone && !ssh_complete) {
while (sent < wr && ssh_active && !conn_api.terminate && !channel_gone) {
ret = 0;
pthread_mutex_lock(&ssh_mutex);
if (ssh_channel == -1) {
pthread_mutex_unlock(&ssh_mutex);
ssh_complete = true;
conn_api.terminate = true;
channel_gone = true;
break;
}
......@@ -370,8 +369,8 @@ ssh_output_thread(void *args)
}
if (cryptStatusError(status)) {
pthread_mutex_unlock(&ssh_mutex);
if (!ssh_complete) {
ssh_complete = true;
if (!conn_api.terminate) {
conn_api.terminate = true;
if ((status != CRYPT_ERROR_COMPLETE) && (status != CRYPT_ERROR_NOTFOUND)) /* connection closed */
cryptlib_error_message(status, "sending data");
}
......@@ -399,7 +398,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data)
if (sz == 0)
return true;
pthread_mutex_lock(&ssh_tx_mutex);
while (ssh_active && sent < sz && !ssh_complete) {
while (ssh_active && sent < sz && !conn_api.terminate) {
int status;
int ret = 0;
pthread_mutex_lock(&ssh_mutex);
......@@ -414,7 +413,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data)
pthread_mutex_unlock(&ssh_mutex);
if (cryptStatusError(status)) {
if (status != CRYPT_ERROR_COMPLETE && status != CRYPT_ERROR_NOTFOUND) { /* connection closed */
if (!ssh_complete)
if (!conn_api.terminate)
cryptlib_error_message(status, "sending sftp data");
}
break;
......@@ -472,7 +471,7 @@ key_not_present(sftp_filehandle_t f, const char *priv)
sftp_str_t r = NULL;
bool skipread = false;
while (ssh_active && !ssh_complete) {
while (ssh_active && !conn_api.terminate) {
if (skipread) {
old_bufpos = 0;
skipread = false;
......@@ -545,7 +544,7 @@ add_public_key(void *vpriv)
}
}
pthread_mutex_unlock(&ssh_mutex);
if (ssh_complete || !ssh_active)
if (conn_api.terminate || !ssh_active)
break;
SLEEP(10);
};
......@@ -633,7 +632,7 @@ add_public_key(void *vpriv)
* To avoid that, we'll sleep for a second to allow
* the remote to close the channel if it wants to.
*/
for (unsigned sleep_count = 0; sleep_count < 100 && conn_api.terminate == 0 && ssh_active && !ssh_complete; sleep_count++) {
for (unsigned sleep_count = 0; sleep_count < 100 && !conn_api.terminate && ssh_active; sleep_count++) {
SLEEP(10);
}
pthread_mutex_lock(&ssh_tx_mutex);
......@@ -687,7 +686,7 @@ error_popup(struct bbslist *bbs, const char *blurb, int status)
{
if (!bbs->hidepopups)
cryptlib_error_message(status, blurb);
conn_api.terminate = 1;
conn_api.terminate = true;
if (!bbs->hidepopups)
uifc.pop(NULL);
}
......@@ -784,7 +783,6 @@ ssh_connect(struct bbslist *bbs)
return -1;
}
ssh_active = true;
ssh_complete = false;
/* we need to disable Nagle on the socket. */
if (setsockopt(ssh_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&off, sizeof(off)))
......@@ -1040,8 +1038,7 @@ ssh_close(void)
{
char garbage[1024];
conn_api.terminate = 1;
ssh_complete = true;
conn_api.terminate = true;
if (ssh_active) {
cryptSetAttribute(ssh_session, CRYPT_OPTION_NET_READTIMEOUT, 1);
cryptSetAttribute(ssh_session, CRYPT_OPTION_NET_WRITETIMEOUT, 1);
......
/* Copyright (C), 2007 by Stephen Hurd */
#ifndef _MSC_VER
#if defined(__STDC_NO_ATOMICS__)
#error Support for stdatomic.h is required.
#endif
#endif
#include <stdatomic.h>
#include <stdlib.h>
#include "bbslist.h"
......@@ -25,7 +18,6 @@
static SOCKET telnets_sock;
static CRYPT_SESSION telnets_session;
static atomic_bool telnets_active = false;
static pthread_mutex_t telnets_mutex;
static int
......@@ -33,7 +25,7 @@ FlushData(CRYPT_SESSION sess)
{
int ret = cryptFlushData(sess);
if (ret == CRYPT_ERROR_COMPLETE || ret == CRYPT_ERROR_READ)
telnets_active = false;
conn_api.terminate = true;
return ret;
}
......@@ -42,7 +34,7 @@ PopData(CRYPT_HANDLE e, void *buf, int len, int *copied)
{
int ret = cryptPopData(e, buf, len, copied);
if (ret == CRYPT_ERROR_COMPLETE || ret == CRYPT_ERROR_READ)
telnets_active = false;
conn_api.terminate = true;
return ret;
}
......@@ -51,7 +43,7 @@ PushData(CRYPT_HANDLE e, void *buf, int len, int *copied)
{
int ret = cryptPushData(e, buf, len, copied);
if (ret == CRYPT_ERROR_COMPLETE)
telnets_active = false;
conn_api.terminate = true;
return ret;
}
......@@ -64,7 +56,7 @@ telnets_input_thread(void *args)
size_t buffer;
SetThreadName("TelnetS Input");
conn_api.input_thread_running = 1;
while (telnets_active && !conn_api.terminate) {
while (!conn_api.terminate) {
if (!socket_readable(telnets_sock, 100))
continue;
pthread_mutex_lock(&telnets_mutex);
......@@ -75,10 +67,10 @@ telnets_input_thread(void *args)
if (status == CRYPT_ERROR_TIMEOUT)
continue;
if (cryptStatusError(status)) {
if (telnets_active) {
if (!conn_api.terminate) {
if ((status != CRYPT_ERROR_COMPLETE) && (status != CRYPT_ERROR_READ)) /* connection closed */
cryptlib_error_message(status, "recieving data");
telnets_active = false;
conn_api.terminate = true;
}
break;
}
......@@ -103,23 +95,23 @@ telnets_output_thread(void *args)
int status;
SetThreadName("TelnetS Output");
conn_api.output_thread_running = 1;
while (telnets_active && !conn_api.terminate) {
while (!conn_api.terminate) {
pthread_mutex_lock(&(conn_outbuf.mutex));
wr = conn_buf_wait_bytes(&conn_outbuf, 1, 100);
if (wr) {
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0;
while (telnets_active && sent < wr) {
while ((!conn_api.terminate) && sent < wr) {
pthread_mutex_lock(&telnets_mutex);
status = PushData(telnets_session, conn_api.wr_buf + sent, wr - sent, &ret);
pthread_mutex_unlock(&telnets_mutex);
if (cryptStatusError(status)) {
if (telnets_active) {
if (!conn_api.terminate) {
if (status != CRYPT_ERROR_COMPLETE && status != CRYPT_ERROR_READ) { /* connection closed */
cryptlib_error_message(status, "sending data");
}
telnets_active = false;
conn_api.terminate = true;
}
break;
}
......@@ -147,7 +139,6 @@ telnets_connect(struct bbslist *bbs)
if (!bbs->hidepopups)
init_uifc(true, true);
pthread_mutex_init(&telnets_mutex, NULL);
telnets_active = false;
telnets_sock = conn_socket_connect(bbs);
if (telnets_sock == INVALID_SOCKET)
......@@ -205,7 +196,6 @@ telnets_connect(struct bbslist *bbs)
return -1;
}
telnets_active = true;
if (!bbs->hidepopups) {
/* Clear ownership */
uifc.pop(NULL); // TODO: Why is this called twice?
......@@ -219,7 +209,7 @@ telnets_connect(struct bbslist *bbs)
conn_api.terminate = 1;
if (!bbs->hidepopups)
uifc.pop(NULL);
telnets_active = false;
conn_api.terminate = true;
return -1;
}
if (!bbs->hidepopups)
......@@ -249,7 +239,6 @@ telnets_close(void)
char garbage[1024];
conn_api.terminate = 1;
cryptSetAttribute(telnets_session, CRYPT_SESSINFO_ACTIVE, 0);
telnets_active = false;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
conn_recv_upto(garbage, sizeof(garbage), 0);
SLEEP(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment