Skip to content

Commit

Permalink
Merge branch 'errno' of github.com:CendioOssman/tigervnc
Browse files Browse the repository at this point in the history
  • Loading branch information
CendioOssman committed Oct 9, 2024
2 parents 87029b7 + 40df30d commit e758979
Show file tree
Hide file tree
Showing 50 changed files with 206 additions and 190 deletions.
6 changes: 3 additions & 3 deletions common/network/Socket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void network::initSockets() {
WSADATA initResult;

if (WSAStartup(requiredVersion, &initResult) != 0)
throw SocketException("unable to initialise Winsock2", errorNumber);
throw rdr::SocketException("unable to initialise Winsock2", errorNumber);
#else
signal(SIGPIPE, SIG_IGN);
#endif
Expand Down Expand Up @@ -161,7 +161,7 @@ Socket* SocketListener::accept() {

// Accept an incoming connection
if ((new_sock = ::accept(fd, nullptr, nullptr)) < 0)
throw SocketException("unable to accept new connection", errorNumber);
throw rdr::SocketException("unable to accept new connection", errorNumber);

// Create the socket object & check connection is allowed
Socket* s = createSocket(new_sock);
Expand All @@ -179,7 +179,7 @@ void SocketListener::listen(int sock)
if (::listen(sock, 5) < 0) {
int e = errorNumber;
closesocket(sock);
throw SocketException("unable to set socket to listening mode", e);
throw rdr::SocketException("unable to set socket to listening mode", e);
}

fd = sock;
Expand Down
5 changes: 0 additions & 5 deletions common/network/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <limits.h>
#include <rdr/FdInStream.h>
#include <rdr/FdOutStream.h>
#include <rdr/Exception.h>

namespace network {

Expand Down Expand Up @@ -107,10 +106,6 @@ namespace network {
ConnectionFilter* filter;
};

struct SocketException : public rdr::SystemException {
SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
};

}

#endif // __NETWORK_SOCKET_H__
16 changes: 8 additions & 8 deletions common/os/Mutex.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Mutex::Mutex()
systemMutex = new pthread_mutex_t;
ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, nullptr);
if (ret != 0)
throw rdr::SystemException("Failed to create mutex", ret);
throw rdr::PosixException("Failed to create mutex", ret);
#endif
}

Expand All @@ -67,7 +67,7 @@ void Mutex::lock()

ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
if (ret != 0)
throw rdr::SystemException("Failed to lock mutex", ret);
throw rdr::PosixException("Failed to lock mutex", ret);
#endif
}

Expand All @@ -80,7 +80,7 @@ void Mutex::unlock()

ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
if (ret != 0)
throw rdr::SystemException("Failed to unlock mutex", ret);
throw rdr::PosixException("Failed to unlock mutex", ret);
#endif
}

Expand All @@ -97,7 +97,7 @@ Condition::Condition(Mutex* mutex_)
systemCondition = new pthread_cond_t;
ret = pthread_cond_init((pthread_cond_t*)systemCondition, nullptr);
if (ret != 0)
throw rdr::SystemException("Failed to create condition variable", ret);
throw rdr::PosixException("Failed to create condition variable", ret);
#endif
}

Expand All @@ -120,14 +120,14 @@ void Condition::wait()
(CRITICAL_SECTION*)mutex->systemMutex,
INFINITE);
if (!ret)
throw rdr::SystemException("Failed to wait on condition variable", GetLastError());
throw rdr::Win32Exception("Failed to wait on condition variable", GetLastError());
#else
int ret;

ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
(pthread_mutex_t*)mutex->systemMutex);
if (ret != 0)
throw rdr::SystemException("Failed to wait on condition variable", ret);
throw rdr::PosixException("Failed to wait on condition variable", ret);
#endif
}

Expand All @@ -140,7 +140,7 @@ void Condition::signal()

ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
if (ret != 0)
throw rdr::SystemException("Failed to signal condition variable", ret);
throw rdr::PosixException("Failed to signal condition variable", ret);
#endif
}

Expand All @@ -153,6 +153,6 @@ void Condition::broadcast()

ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
if (ret != 0)
throw rdr::SystemException("Failed to broadcast condition variable", ret);
throw rdr::PosixException("Failed to broadcast condition variable", ret);
#endif
}
10 changes: 5 additions & 5 deletions common/os/Thread.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Thread::start()
#ifdef WIN32
*(HANDLE*)threadId = CreateThread(nullptr, 0, startRoutine, this, 0, nullptr);
if (*(HANDLE*)threadId == nullptr)
throw rdr::SystemException("Failed to create thread", GetLastError());
throw rdr::Win32Exception("Failed to create thread", GetLastError());
#else
int ret;
sigset_t all, old;
Expand All @@ -76,14 +76,14 @@ void Thread::start()
sigfillset(&all);
ret = pthread_sigmask(SIG_SETMASK, &all, &old);
if (ret != 0)
throw rdr::SystemException("Failed to mask signals", ret);
throw rdr::PosixException("Failed to mask signals", ret);

ret = pthread_create((pthread_t*)threadId, nullptr, startRoutine, this);

pthread_sigmask(SIG_SETMASK, &old, nullptr);

if (ret != 0)
throw rdr::SystemException("Failed to create thread", ret);
throw rdr::PosixException("Failed to create thread", ret);
#endif

running = true;
Expand All @@ -99,13 +99,13 @@ void Thread::wait()

ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE);
if (ret != WAIT_OBJECT_0)
throw rdr::SystemException("Failed to join thread", GetLastError());
throw rdr::Win32Exception("Failed to join thread", GetLastError());
#else
int ret;

ret = pthread_join(*(pthread_t*)threadId, nullptr);
if (ret != 0)
throw rdr::SystemException("Failed to join thread", ret);
throw rdr::PosixException("Failed to join thread", ret);
#endif
}

Expand Down
28 changes: 16 additions & 12 deletions common/rdr/Exception.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ GAIException::GAIException(const char* s, int err_)
strncat(str_, ")", len-1-strlen(str_));
}

SystemException::SystemException(const char* s, int err_)
PosixException::PosixException(const char* s, int err_)
: Exception("%s", s), err(err_)
{
strncat(str_, ": ", len-1-strlen(str_));
strncat(str_, strerror(err), len-1-strlen(str_));
strncat(str_, " (", len-1-strlen(str_));
char buf[20];
sprintf(buf,"%d",err);
strncat(str_, buf, len-1-strlen(str_));
strncat(str_, ")", len-1-strlen(str_));
}

#ifdef WIN32
Win32Exception::Win32Exception(const char* s, unsigned err_)
: Exception("%s", s), err(err_)
{
strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
wchar_t *currStr = new wchar_t[len-strlen(str_)];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, err, 0, currStr, len-1-strlen(str_), nullptr);
Expand All @@ -92,18 +104,10 @@ SystemException::SystemException(const char* s, int err_)
if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
str_[l-2] = 0;

#else
strncat(str_, strerror(err), len-1-strlen(str_));
#endif
strncat(str_, " (", len-1-strlen(str_));
char buf[20];
#ifdef WIN32
if (err < 0)
sprintf(buf, "%x", err);
else
#endif
sprintf(buf,"%d",err);
sprintf(buf,"%d",err);
strncat(str_, buf, len-1-strlen(str_));
strncat(str_, ")", len-1-strlen(str_));
}

#endif
21 changes: 19 additions & 2 deletions common/rdr/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,28 @@ namespace rdr {
virtual const char* str() const { return str_; }
};

struct SystemException : public Exception {
struct PosixException : public Exception {
int err;
SystemException(const char* s, int err_);
PosixException(const char* s, int err_);
};

#ifdef WIN32
struct Win32Exception : public Exception {
unsigned err;
Win32Exception(const char* s, unsigned err_);
};
#endif

#ifdef WIN32
struct SocketException : public Win32Exception {
SocketException(const char* text, unsigned err_) : Win32Exception(text, err_) {}
};
#else
struct SocketException : public PosixException {
SocketException(const char* text, int err_) : PosixException(text, err_) {}
};
#endif

struct GAIException : public Exception {
int err;
GAIException(const char* s, int err_);
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FdInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ size_t FdInStream::readFd(uint8_t* buf, size_t len)
} while (n < 0 && errorNumber == EINTR);

if (n < 0)
throw SystemException("select", errorNumber);
throw SocketException("select", errorNumber);

if (n == 0)
return 0;
Expand All @@ -102,7 +102,7 @@ size_t FdInStream::readFd(uint8_t* buf, size_t len)
} while (n < 0 && errorNumber == EINTR);

if (n < 0)
throw SystemException("read", errorNumber);
throw SocketException("read", errorNumber);
if (n == 0)
throw EndOfStream();

Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FdOutStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ size_t FdOutStream::writeFd(const uint8_t* data, size_t length)
} while (n < 0 && errorNumber == EINTR);

if (n < 0)
throw SystemException("select", errorNumber);
throw SocketException("select", errorNumber);

if (n == 0)
return 0;
Expand All @@ -134,7 +134,7 @@ size_t FdOutStream::writeFd(const uint8_t* data, size_t length)
} while (n < 0 && (errorNumber == EINTR));

if (n < 0)
throw SystemException("write", errorNumber);
throw SocketException("write", errorNumber);

gettimeofday(&lastWrite, nullptr);

Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FileInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ FileInStream::FileInStream(const char *fileName)
{
file = fopen(fileName, "rb");
if (!file)
throw SystemException("fopen", errno);
throw PosixException("fopen", errno);
}

FileInStream::~FileInStream(void) {
Expand All @@ -48,7 +48,7 @@ bool FileInStream::fillBuffer()
size_t n = fread((uint8_t*)end, 1, availSpace(), file);
if (n == 0) {
if (ferror(file))
throw SystemException("fread", errno);
throw PosixException("fread", errno);
if (feof(file))
throw EndOfStream();
return false;
Expand Down
6 changes: 3 additions & 3 deletions common/rdr/RandomStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ bool RandomStream::fillBuffer() {
#ifdef RFB_HAVE_WINCRYPT
if (provider) {
if (!CryptGenRandom(provider, availSpace(), (uint8_t*)end))
throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
throw rdr::Win32Exception("unable to CryptGenRandom", GetLastError());
end += availSpace();
} else {
#else
#ifndef WIN32
if (fp) {
size_t n = fread((uint8_t*)end, 1, availSpace(), fp);
if (n <= 0)
throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
errno);
throw rdr::PosixException("reading /dev/urandom or /dev/random failed",
errno);
end += n;
} else {
#else
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/TLSInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
in->readBytes((uint8_t*)data, size);
} catch (EndOfStream&) {
return 0;
} catch (SystemException &e) {
} catch (SocketException& e) {
vlog.error("Failure reading TLS data: %s", e.str());
gnutls_transport_set_errno(self->session, e.err);
self->saved_exception = new SystemException(e);
self->saved_exception = new SocketException(e);
return -1;
} catch (Exception& e) {
vlog.error("Failure reading TLS data: %s", e.str());
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/TLSOutStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ ssize_t TLSOutStream::push(gnutls_transport_ptr_t str, const void* data,
try {
out->writeBytes((const uint8_t*)data, size);
out->flush();
} catch (SystemException &e) {
} catch (SocketException& e) {
vlog.error("Failure sending TLS data: %s", e.str());
gnutls_transport_set_errno(self->session, e.err);
self->saved_exception = new SystemException(e);
self->saved_exception = new SocketException(e);
return -1;
} catch (Exception& e) {
vlog.error("Failure sending TLS data: %s", e.str());
Expand Down
4 changes: 2 additions & 2 deletions common/rfb/SSecurityRSAAES.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void SSecurityRSAAES::loadPrivateKey()
{
FILE* file = fopen(keyFile, "rb");
if (!file)
throw rdr::SystemException("failed to open key file", errno);
throw rdr::PosixException("failed to open key file", errno);
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
if (size == 0 || size > MaxKeyFileSize) {
Expand All @@ -167,7 +167,7 @@ void SSecurityRSAAES::loadPrivateKey()
std::vector<uint8_t> data(size);
if (fread(data.data(), 1, data.size(), file) != size) {
fclose(file);
throw rdr::SystemException("failed to read key", errno);
throw rdr::PosixException("failed to read key", errno);
}
fclose(file);

Expand Down
2 changes: 1 addition & 1 deletion unix/tx/TXDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TXDialog : public TXWindow, public TXDeleteWindowCallback {
FD_ZERO(&rfds);
FD_SET(ConnectionNumber(dpy), &rfds);
int n = select(FD_SETSIZE, &rfds, nullptr, nullptr, nullptr);
if (n < 0) throw rdr::SystemException("select",errno);
if (n < 0) throw rdr::SocketException("select", errno);
}
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion unix/vncconfig/vncconfig.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ int main(int argc, char** argv)
FD_ZERO(&rfds);
FD_SET(ConnectionNumber(dpy), &rfds);
int n = select(FD_SETSIZE, &rfds, nullptr, nullptr, tvp);
if (n < 0) throw rdr::SystemException("select",errno);
if (n < 0) throw rdr::SocketException("select", errno);
}

XCloseDisplay(dpy);
Expand Down
2 changes: 1 addition & 1 deletion unix/x0vncserver/x0vncserver.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ int main(int argc, char** argv)
vlog.debug("Interrupted select() system call");
continue;
} else {
throw rdr::SystemException("select", errno);
throw rdr::SocketException("select", errno);
}
}

Expand Down
Loading

0 comments on commit e758979

Please sign in to comment.