Skip to content
Snippets Groups Projects
Commit 7638d61c authored by Recolic's avatar Recolic :house_with_garden:
Browse files

.

parent 2162cab1
No related branches found
No related tags found
1 merge request!2Add standalone implementation
......@@ -10,6 +10,14 @@
#include <string>
#include <rlib/macro.hpp>
#include <iostream>
#include <stdexcept>
#include <filesystem>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/socket.h>
constexpr auto GNOME_KEYRING_RESULT_OK = 0;
......@@ -20,3 +28,49 @@ inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) {
inline std::string keyringResultToString(GnomeKeyringResult res) {
}
namespace utils {
inline auto get_control_socket() {
namespace fs = std::filesystem;
// Helper function to check if a path is a socket
auto is_socket = [](const fs::path& path) {
struct stat s;
return stat(path.c_str(), &s) == 0 && S_ISSOCK(s.st_mode);
};
if (const char* gnome_keyring_control = std::getenv("GNOME_KEYRING_CONTROL")) {
fs::path control_socket = fs::path(gnome_keyring_control) / "control";
if (fs::exists(control_socket) && is_socket(control_socket)) {
return control_socket;
}
}
if (const char* xdg_runtime_dir = std::getenv("XDG_RUNTIME_DIR")) {
fs::path control_socket = fs::path(xdg_runtime_dir) / "keyring/control";
if (fs::exists(control_socket) && is_socket(control_socket)) {
return control_socket;
}
}
throw std::runtime_error("Unable to find control socket");
}
inline int connect_unix_socket(std::string path) {
// Create a UNIX socket
int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd == -1) {
throw std::runtime_error("Unable to create unix socket");
}
// Set up socket address
struct sockaddr_un addr {};
addr.sun_family = AF_UNIX;
std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
// Connect to the socket
if (connect(sock_fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1) {
close(sock_fd);
throw std::runtime_error("Unable to connect to unix socket");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment