Skip to content
Snippets Groups Projects

Add standalone implementation

Merged Recolic requested to merge impl/py-standalone into master
1 file
+ 54
0
Compare changes
  • Side-by-side
  • Inline
  • 7638d61c
    . · 7638d61c
    Recolic authored
+ 54
0
@@ -10,6 +10,14 @@
@@ -10,6 +10,14 @@
#include <string>
#include <string>
#include <rlib/macro.hpp>
#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;
constexpr auto GNOME_KEYRING_RESULT_OK = 0;
@@ -20,3 +28,49 @@ inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) {
@@ -20,3 +28,49 @@ inline GnomeKeyringResult do_unlock(std::string keyring, std::string password) {
inline std::string keyringResultToString(GnomeKeyringResult res) {
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");
 
}
 
}
 
}
Loading