Snowman6286
Snowman6286

Reputation: 150

Boost Asio Error

I've tried to see if anyone else if having this problem, but I haven't found anything online yet. Does anything in this code looks like I'm invoking boost incorrectly?

This code works when I am logged into the machine that is starting the TCP server, but fails when no one is logged in. I stripped the code down to only look at the boost asio logic.

//create _acceptor, which will eventually listen for incomming connections, asynchronously
_acceptor = boost::shared_ptr<tcp::acceptor>(new tcp::acceptor(*_io_service));
_acceptor->open(tcp::endpoint(tcp::v4(), _port).protocol());
_acceptor->set_option(tcp::acceptor::reuse_address(false));

//omitted logic find a port that is open
_acceptor->bind(tcp::endpoint(tcp::v4(), _port));

//omitted error handling logic if open port not found

//Start listening for incoming connections asynchronously.
_acceptor->listen();

sslSocketPtr ssl_socket(sslSocketPtr(new ssl::stream<ip::tcp::socket>(*_io_service, _sslContext)));
_acceptor->async_accept(ssl_socket->lowest_layer(),
    boost::bind(&TCPServer::handle_sslAccept, shared_from_this(), boost::asio::placeholders::error, ssl_socket));

When no one is logged into the machine, the ssl_socket constructor throws the exception: "static_mutex: Access is denied".

If I define BOOST_ASIO_ENABLE_OLD_SSL the code works correctly, but I think that may be contributing to another bug in my code. So I am trying to use the latest SSL logic from Boost.

Any help would be appreciated!

Upvotes: 1

Views: 800

Answers (1)

sehe
sehe

Reputation: 393999

I'm going to assume from the symptoms that you run on Windows.

On windows, static_mutex is implemented as a named (interprocess) mutex and gets "opened" using CreateMutexW:

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object, GetLastError returns ERROR_ALREADY_EXISTS, bInitialOwner is ignored, and the calling thread is not granted ownership. However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use the OpenMutex function.

As you can see you don't have the required permissions. However, you could still have this working if the mutex is always created by a privileged process. In that case you could modify the code that opens an existing named mutex with OpenMutex as the documentation hints.

It's likely easier to run the process under a user that has the required permissions thought

Upvotes: 2

Related Questions