Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the default location of vineyard IPC socket #1354

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ if(BUILD_VINEYARD_SERVER)
endif()

install_vineyard_target(vineyardd)
install_vineyard_headers("${PROJECT_SOURCE_DIR}/src/server")
if(NOT BUILD_SHARED_LIBS)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
target_compile_options(vineyardd PRIVATE -static-libgcc -static-libstdc++ -Os)
Expand Down
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -1156,3 +1156,28 @@ Copyright 2016-2022 ClickHouse, Inc.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

--------------------------------------------------------------------------------

The file thirdparty/gulrak/filesystem.hpp is imported from the gulrak/filesystem project,
which has the following license:

Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ We thank the following excellent open-source projects:
- `dlmalloc <http://gee.cs.oswego.edu/dl/html/malloc.htmlp>`_, Doug Lea's memory allocator.
- `etcd-cpp-apiv3 <https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3>`_, a C++ API for etcd's v3 client API.
- `flat_hash_map <https://github.com/skarupke/flat_hash_map>`_, an efficient hashmap implementation.
- `gulrak/filesystem <https://github.com/gulrak/filesystem>`_, an implementation of C++17 std::filesystem.
- `libcuckoo <https://github.com/efficient/libcuckoo>`_, libcuckoo, a high-performance, concurrent hash table.
- `mimalloc <https://github.com/microsoft/mimalloc>`_, a general purpose allocator with excellent performance characteristics.
- `nlohmann/json <https://github.com/nlohmann/json>`_, a json library for modern c++.
Expand Down
156 changes: 107 additions & 49 deletions src/server/async/ipc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,99 @@ limitations under the License.
#include <string>
#include <utility>

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "gulrak/filesystem.hpp"

#include "common/util/env.h"
#include "common/util/json.h"
#include "common/util/logging.h"
#include "server/server/vineyard_server.h"

namespace vineyard {

namespace detail {

static bool check_connectable(asio::io_context& context,
std::string const& path) {
asio::local::stream_protocol::socket socket(context);
boost::system::error_code ec;
socket.connect(path, ec);
return !ec;
}

static bool check_listable(asio::io_context& context, std::string const& path,
std::string& error_message) {
// create parent directory
std::error_code ec;
auto socket_path = ghc::filesystem::absolute(ghc::filesystem::path(path), ec);
if (ec) {
error_message =
"Failed to resolve the absolute path for specified socket path '" +
path + "': " + ec.message();
return false;
}
ghc::filesystem::create_directories(socket_path.parent_path(), ec);
if (ec) {
error_message = "Failed to create parent directory '" +
socket_path.parent_path().string() +
"' for specified socket path '" + path +
"': " + ec.message();
return false;
}
if (ghc::filesystem::exists(socket_path, ec)) {
if (ghc::filesystem::is_socket(socket_path, ec)) {
if (check_connectable(context, path)) {
error_message =
"the UNIX-domain socket '" + path +
"' is already inuse and has been listened on,\n\n"
" - please use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help\n\n";
return false;
}
if (!ghc::filesystem::remove(socket_path, ec) &&
ec != std::errc::no_such_file_or_directory) {
error_message =
"Permission error when attempting to write the UNIX-domain socket "
"'" +
path + "': " + strerror(errno) +
",\n\n"
" - please use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help\n\n";
return false;
}
return true;
} else {
error_message =
"the UNIX-domain socket '" + path +
"' is not a named socket,\n\n"
" - please use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help\n\n";
return false;
}
}
// check if we have permission to create the new file
std::ofstream ofs(socket_path.string(), std::ios::out | std::ios::binary);
if (ofs.fail()) {
error_message =
"Permission error when attempting to create the UNIX-domain socket "
"'" +
path + "': " + strerror(errno) +
",\n\n"
" - please use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help\n\n";
return false;
} else {
ofs.close();
ghc::filesystem::remove(socket_path, ec);
return true;
}
}

} // namespace detail

IPCServer::IPCServer(std::shared_ptr<VineyardServer> vs_ptr)
: SocketServer(vs_ptr),
ipc_spec_(vs_ptr_->GetSpec()["ipc_spec"]),
Expand Down Expand Up @@ -65,57 +149,31 @@ void IPCServer::Close() {

asio::local::stream_protocol::endpoint IPCServer::getEndpoint(
asio::io_context& context) {
std::string const& ipc_socket =
ipc_spec_["socket"].get_ref<std::string const&>();
auto endpoint = asio::local::stream_protocol::endpoint(ipc_socket);
if (access(ipc_socket.c_str(), F_OK) == 0) {
// first check if the socket file is writable
if (access(ipc_socket.c_str(), W_OK) != 0) {
std::string reason = strerror(errno);
if (errno == EACCES) {
reason +=
",\n\n - please run vineyardd as root using 'sudo',\n"
" - or use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help";
}
throw std::invalid_argument("cannot launch vineyardd on '" + ipc_socket +
"': " + reason);
}
// then check if the socket file is used by another process, if not, unlink
// it first, otherwise raise an exception.
asio::local::stream_protocol::socket socket(context);
boost::system::error_code ec;
socket.connect(endpoint, ec);
if (!ec) {
std::string message =
"the UNIX-domain socket '" + ipc_socket +
"' has already been listened on,\n\n"
" - please use another IPC socket path to start vineyardd,\n\n"
"\te.g., vineyardd --socket=/tmp/vineyard.sock\n\n"
" for more vineyardd options, see also: vineyard --help\n\n";
throw boost::system::system_error(
asio::error::make_error_code(asio::error::address_in_use), message);
std::string ipc_socket = ipc_spec_["socket"].get<std::string>();
std::string error_message;
if (ipc_socket.empty()) {
ipc_socket = "/var/run/vineyard.sock";
if (detail::check_listable(context, ipc_socket, error_message)) {
::unlink(ipc_socket.c_str());
ipc_spec_["socket"] = ipc_socket;
return asio::local::stream_protocol::endpoint(ipc_socket);
}
} else if (errno == ENOENT) {
// create parent directory
auto socket_path =
boost::filesystem::absolute(boost::filesystem::path(ipc_socket));
boost::system::error_code ec;
boost::filesystem::create_directories(socket_path.parent_path(), ec);
if (ec) {
std::string message = "Failed to create parent directory '" +
socket_path.parent_path().string() +
"' for specific socket path";
throw boost::system::system_error(ec, message);
ipc_socket = read_env("HOME") + "/.local/vineyard/vineyard.sock";
LOG(WARNING)
<< "Failed to listen on default socket '/var/run/vineyard.sock'";
LOG(INFO) << "Falling back to '" << ipc_socket << "' ...";
if (detail::check_listable(context, ipc_socket, error_message)) {
::unlink(ipc_socket.c_str());
ipc_spec_["socket"] = ipc_socket;
return asio::local::stream_protocol::endpoint(ipc_socket);
}
} else {
throw boost::system::system_error(
boost::system::errc::make_error_code(boost::system::errc::io_error),
strerror(errno));
if (detail::check_listable(context, ipc_socket, error_message)) {
::unlink(ipc_socket.c_str());
return asio::local::stream_protocol::endpoint(ipc_socket);
}
}
::unlink(ipc_socket.c_str());
return endpoint;
throw std::invalid_argument(error_message);
}

Status IPCServer::Register(std::shared_ptr<SocketConnection> conn,
Expand Down
2 changes: 1 addition & 1 deletion src/server/async/ipc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class IPCServer : public SocketServer,

void doAccept() override;

const json ipc_spec_;
json ipc_spec_;
asio::local::stream_protocol::acceptor acceptor_;
asio::local::stream_protocol::socket socket_;
};
Expand Down
4 changes: 3 additions & 1 deletion src/server/server/vineyard_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ limitations under the License.
#include <thread>
#include <vector>

#include "gulrak/filesystem.hpp"

#include "common/util/callback.h"
#include "common/util/json.h"
#include "common/util/logging.h"
Expand Down Expand Up @@ -1199,7 +1201,7 @@ Status VineyardServer::Verify(const std::string& username,
}
return callback(Status::OK());
}
if (!boost::filesystem::exists(htpasswd)) {
if (!ghc::filesystem::exists(htpasswd)) {
return callback(
Status::IOError("Failed to find the htpasswd database for verifying"));
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/util/etcd_launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include <vector>

#include "boost/algorithm/string.hpp"
#include "gulrak/filesystem.hpp"

#include "common/util/asio.h"
#include "common/util/env.h"
Expand Down Expand Up @@ -84,8 +85,8 @@ EtcdLauncher::~EtcdLauncher() {
etcd_proc_->wait(err);
}
if (!etcd_data_dir_.empty()) {
boost::system::error_code err;
boost::filesystem::remove_all(boost::filesystem::path(etcd_data_dir_), err);
std::error_code err;
ghc::filesystem::remove_all(ghc::filesystem::path(etcd_data_dir_), err);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/server/util/etcd_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ limitations under the License.
#include <set>
#include <string>

#include "boost/filesystem.hpp"
#include "boost/process.hpp"
#include "etcd/Client.hpp"

Expand Down
2 changes: 1 addition & 1 deletion src/server/util/kubectl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ limitations under the License.
#include <vector>

#include "boost/bind.hpp"
#include "boost/filesystem.hpp"
#include "boost/process.hpp"
#include "gulrak/filesystem.hpp"

#include "common/util/asio.h"

Expand Down
18 changes: 9 additions & 9 deletions src/server/util/proc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ limitations under the License.
#endif

#include "boost/bind.hpp"
#include "boost/filesystem.hpp"
#include "boost/process.hpp"
#include "gulrak/filesystem.hpp"

#include "common/util/asio.h"

Expand Down Expand Up @@ -60,7 +60,7 @@ void Process::Start(const std::string& command,
std::string command_path = command;
{
setenv("LC_ALL", "C", 1);
boost::filesystem::path target;
ghc::filesystem::path target;
if (this->findRelativeProgram(command, target).ok()) {
command_path = target.string();
} else {
Expand Down Expand Up @@ -187,14 +187,14 @@ Status Process::recordLog(Status const& status, std::string const& line) {
}

Status Process::findRelativeProgram(std::string const& name,
boost::filesystem::path& target) {
ghc::filesystem::path& target) {
// try directly finding first
if (boost::filesystem::exists(name)) {
target = boost::filesystem::path(name);
if (ghc::filesystem::exists(name)) {
target = ghc::filesystem::path(name);
return Status::OK();
}

boost::filesystem::path current_location;
ghc::filesystem::path current_location;

#if defined(__APPLE__) && defined(__MACH__)
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
Expand All @@ -216,10 +216,10 @@ Status Process::findRelativeProgram(std::string const& name,

#endif

boost::filesystem::path parent_path = current_location.parent_path();
ghc::filesystem::path parent_path = current_location.parent_path();
target = parent_path.append(name);
boost::system::error_code err;
if (boost::filesystem::exists(target, err)) {
std::error_code err;
if (ghc::filesystem::exists(target, err)) {
return Status::OK();
} else {
return Status::IOError("Failed to get location of current process");
Expand Down
4 changes: 2 additions & 2 deletions src/server/util/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ limitations under the License.
#include <string>
#include <vector>

#include "boost/filesystem.hpp"
#include "boost/process.hpp"
#include "gulrak/filesystem.hpp"

#include "common/util/asio.h"
#include "common/util/callback.h"
Expand Down Expand Up @@ -71,7 +71,7 @@ class Process : public std::enable_shared_from_this<Process> {
Status recordLog(Status const& status, std::string const& line);

Status findRelativeProgram(std::string const& name,
boost::filesystem::path& target);
ghc::filesystem::path& target);
};

} // namespace vineyard
Expand Down
1 change: 0 additions & 1 deletion src/server/util/redis_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
#include <set>
#include <string>

#include "boost/filesystem.hpp"
#include "boost/process.hpp"

#if defined(BUILD_VINEYARDD_REDIS)
Expand Down
5 changes: 4 additions & 1 deletion src/server/util/spec_resolvers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ DEFINE_double(spill_upper_rate, 0.8,
"high watermark of triggering memory spilling");

// ipc
DEFINE_string(socket, "/var/run/vineyard.sock", "IPC socket file location");
DEFINE_string(
socket, "",
"IPC socket file location. Defaults to '/var/run/vineyard.sock' for root "
"users and '~/.vineyard/vineyard.sock' for non-root users.");

// rpc
DEFINE_bool(rpc, true, "Enable RPC service by default");
Expand Down
Loading