Skip to content

Commit

Permalink
Merge branch 'stdexcept' of github.com:CendioOssman/tigervnc
Browse files Browse the repository at this point in the history
  • Loading branch information
CendioOssman committed Nov 7, 2024
2 parents 7508e98 + 2b78572 commit f7507ae
Show file tree
Hide file tree
Showing 131 changed files with 1,154 additions and 1,058 deletions.
8 changes: 5 additions & 3 deletions common/network/Socket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <fcntl.h>
#include <errno.h>

#include <rdr/Exception.h>

#include <network/Socket.h>

using namespace network;
Expand All @@ -53,7 +55,7 @@ void network::initSockets() {
WSADATA initResult;

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

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

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

fd = sock;
Expand Down
44 changes: 24 additions & 20 deletions common/network/TcpSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
#include <stdlib.h>
#include <unistd.h>

#include <rdr/Exception.h>

#include <network/TcpSocket.h>

#include <rfb/LogWriter.h>
#include <rfb/Configuration.h>
#include <rfb/util.h>
Expand Down Expand Up @@ -82,15 +85,15 @@ int network::findFreeTcpPort (void)
addr.sin_addr.s_addr = INADDR_ANY;

if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
throw SocketException ("unable to create socket", errorNumber);
throw socket_error("unable to create socket", errorNumber);

addr.sin_port = 0;
if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) < 0)
throw SocketException ("unable to find free port", errorNumber);
throw socket_error("unable to find free port", errorNumber);

socklen_t n = sizeof(addr);
if (getsockname (sock, (struct sockaddr *)&addr, &n) < 0)
throw SocketException ("unable to get port number", errorNumber);
throw socket_error("unable to get port number", errorNumber);

closesocket (sock);
return ntohs(addr.sin_port);
Expand Down Expand Up @@ -134,7 +137,7 @@ TcpSocket::TcpSocket(const char *host, int port)
hints.ai_next = nullptr;

if ((result = getaddrinfo(host, nullptr, &hints, &ai)) != 0) {
throw GAIException("unable to resolve host by name", result);
throw getaddrinfo_error("unable to resolve host by name", result);
}

sock = -1;
Expand Down Expand Up @@ -175,7 +178,7 @@ TcpSocket::TcpSocket(const char *host, int port)
if (sock == -1) {
err = errorNumber;
freeaddrinfo(ai);
throw SocketException("unable to create socket", err);
throw socket_error("unable to create socket", err);
}

/* Attempt to connect to the remote host */
Expand All @@ -200,9 +203,9 @@ TcpSocket::TcpSocket(const char *host, int port)

if (sock == -1) {
if (err == 0)
throw Exception("No useful address for host");
throw std::runtime_error("No useful address for host");
else
throw SocketException("unable to connect to socket", err);
throw socket_error("unable to connect to socket", err);
}

// Take proper ownership of the socket
Expand Down Expand Up @@ -299,15 +302,15 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
int sock;

if ((sock = socket (listenaddr->sa_family, SOCK_STREAM, 0)) < 0)
throw SocketException("unable to create listening socket", errorNumber);
throw socket_error("unable to create listening socket", errorNumber);

memcpy (&sa, listenaddr, listenaddrlen);
#ifdef IPV6_V6ONLY
if (listenaddr->sa_family == AF_INET6) {
if (setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one))) {
int e = errorNumber;
closesocket(sock);
throw SocketException("unable to set IPV6_V6ONLY", e);
throw socket_error("unable to set IPV6_V6ONLY", e);
}
}
#endif /* defined(IPV6_V6ONLY) */
Expand All @@ -325,14 +328,14 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
(char *)&one, sizeof(one)) < 0) {
int e = errorNumber;
closesocket(sock);
throw SocketException("unable to create listening socket", e);
throw socket_error("unable to create listening socket", e);
}
#endif

if (bind(sock, &sa.u.sa, listenaddrlen) == -1) {
int e = errorNumber;
closesocket(sock);
throw SocketException("failed to bind socket", e);
throw socket_error("failed to bind socket", e);
}

listen(sock);
Expand Down Expand Up @@ -443,7 +446,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
snprintf (service, sizeof (service) - 1, "%d", port);
service[sizeof (service) - 1] = '\0';
if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0)
throw GAIException("unable to resolve listening address", result);
throw getaddrinfo_error("unable to resolve listening address", result);

try {
createTcpListeners(listeners, ai);
Expand Down Expand Up @@ -482,7 +485,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
try {
new_listeners.push_back(new TcpListener(current->ai_addr,
current->ai_addrlen));
} catch (SocketException& e) {
} catch (socket_error& e) {
// Ignore this if it is due to lack of address family support on
// the interface or on the system
if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT) {
Expand Down Expand Up @@ -607,7 +610,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {

parts = rfb::split(&p[1], '/');
if (parts.size() > 2)
throw Exception("invalid filter specified");
throw std::invalid_argument("invalid filter specified");

if (parts[0].empty()) {
// Match any address
Expand All @@ -630,7 +633,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
}

if ((result = getaddrinfo (parts[0].c_str(), nullptr, &hints, &ai)) != 0) {
throw GAIException("unable to resolve host by name", result);
throw getaddrinfo_error("unable to resolve host by name", result);
}

memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen);
Expand All @@ -641,8 +644,8 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
if (parts.size() > 1) {
if (family == AF_INET &&
(parts[1].find('.') != std::string::npos)) {
throw Exception("mask no longer supported for filter, "
"use prefix instead");
throw std::invalid_argument("mask no longer supported for "
"filter, use prefix instead");
}

pattern.prefixlen = (unsigned int) atoi(parts[1].c_str());
Expand All @@ -655,16 +658,17 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
pattern.prefixlen = 128;
break;
default:
throw Exception("unknown address family");
throw std::runtime_error("unknown address family");
}
}
}

family = pattern.address.u.sa.sa_family;

if (pattern.prefixlen > (family == AF_INET ? 32: 128))
throw Exception("invalid prefix length for filter address: %u",
pattern.prefixlen);
throw std::invalid_argument(rfb::format("invalid prefix length for "
"filter address: %u",
pattern.prefixlen));

// Compute mask from address and prefix length
memset (&pattern.mask, 0, sizeof (pattern.mask));
Expand Down
17 changes: 10 additions & 7 deletions common/network/UnixSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include <stdlib.h>
#include <stddef.h>

#include <rdr/Exception.h>

#include <network/UnixSocket.h>

#include <rfb/LogWriter.h>

using namespace network;
Expand All @@ -50,12 +53,12 @@ UnixSocket::UnixSocket(const char *path)
socklen_t salen;

if (strlen(path) >= sizeof(addr.sun_path))
throw SocketException("socket path is too long", ENAMETOOLONG);
throw socket_error("socket path is too long", ENAMETOOLONG);

// - Create a socket
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1)
throw SocketException("unable to create socket", errno);
throw socket_error("unable to create socket", errno);

// - Attempt to connect
memset(&addr, 0, sizeof(addr));
Expand All @@ -69,7 +72,7 @@ UnixSocket::UnixSocket(const char *path)
}

if (result == -1)
throw SocketException("unable to connect to socket", err);
throw socket_error("unable to connect to socket", err);

setFd(sock);
}
Expand Down Expand Up @@ -116,11 +119,11 @@ UnixListener::UnixListener(const char *path, int mode)
int err, result;

if (strlen(path) >= sizeof(addr.sun_path))
throw SocketException("socket path is too long", ENAMETOOLONG);
throw socket_error("socket path is too long", ENAMETOOLONG);

// - Create a socket
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
throw SocketException("unable to create listening socket", errno);
throw socket_error("unable to create listening socket", errno);

// - Delete existing socket (ignore result)
unlink(path);
Expand All @@ -135,14 +138,14 @@ UnixListener::UnixListener(const char *path, int mode)
umask(saved_umask);
if (result < 0) {
close(fd);
throw SocketException("unable to bind listening socket", err);
throw socket_error("unable to bind listening socket", err);
}

// - Set socket mode
if (chmod(path, mode) < 0) {
err = errno;
close(fd);
throw SocketException("unable to set socket mode", err);
throw socket_error("unable to set socket mode", err);
}

listen(fd);
Expand Down
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::PosixException("Failed to create mutex", ret);
throw rdr::posix_error("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::PosixException("Failed to lock mutex", ret);
throw rdr::posix_error("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::PosixException("Failed to unlock mutex", ret);
throw rdr::posix_error("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::PosixException("Failed to create condition variable", ret);
throw rdr::posix_error("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::Win32Exception("Failed to wait on condition variable", GetLastError());
throw rdr::win32_error("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::PosixException("Failed to wait on condition variable", ret);
throw rdr::posix_error("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::PosixException("Failed to signal condition variable", ret);
throw rdr::posix_error("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::PosixException("Failed to broadcast condition variable", ret);
throw rdr::posix_error("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::Win32Exception("Failed to create thread", GetLastError());
throw rdr::win32_error("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::PosixException("Failed to mask signals", ret);
throw rdr::posix_error("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::PosixException("Failed to create thread", ret);
throw rdr::posix_error("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::Win32Exception("Failed to join thread", GetLastError());
throw rdr::win32_error("Failed to join thread", GetLastError());
#else
int ret;

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

Expand Down
2 changes: 2 additions & 0 deletions common/os/winerrno.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cat /usr/i686-pc-mingw32/sys-root/mingw/include/winerror.h \
| egrep -v 'EINTR|EBADF|EACCES|EFAULT|EINVAL|EMFILE|_QOS|PROVIDER|PROCTABLE'
*/

#include <winsock2.h>

#undef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#undef EINPROGRESS
Expand Down
Loading

0 comments on commit f7507ae

Please sign in to comment.