From 3b22b7cc4f1425390903811b773526aa5a318713 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 9 Nov 2022 23:58:46 +0100 Subject: add initial shim for sd_* functions --- libconsolekit/Makefile.am | 3 + libconsolekit/libconsolekit.c | 58 +++++++++++++++++ libconsolekit/libconsolekit.h | 5 ++ libconsolekit/sd-compat.c | 144 ++++++++++++++++++++++++++++++++++++++++++ libconsolekit/sd-login.h | 8 +++ 5 files changed, 218 insertions(+) create mode 100644 libconsolekit/sd-compat.c create mode 100644 libconsolekit/sd-login.h diff --git a/libconsolekit/Makefile.am b/libconsolekit/Makefile.am index d5fe59c..1b63304 100644 --- a/libconsolekit/Makefile.am +++ b/libconsolekit/Makefile.am @@ -16,11 +16,14 @@ libconsolekitincludedir = $(includedir)/ConsoleKit/ libconsolekitinclude_HEADERS = \ libconsolekit.h \ + sd-login.h \ $(NULL) libconsolekit_la_SOURCES = \ libconsolekit.c \ libconsolekit.h \ + sd-compat.c \ + sd-login.h \ $(NULL) diff --git a/libconsolekit/libconsolekit.c b/libconsolekit/libconsolekit.c index 6d6693f..a86b353 100644 --- a/libconsolekit/libconsolekit.c +++ b/libconsolekit/libconsolekit.c @@ -262,6 +262,64 @@ lib_consolekit_seat_get_active (LibConsoleKit *ck, return TRUE; } +/** + * lib_consolekit_uid_get_sessions: + **/ +gint +lib_consolekit_uid_get_sessions (LibConsoleKit *ck, + uid_t uid, + gchar ***sessions, + GError **error) +{ + GDBusProxy *manager_proxy = NULL; + GVariant *variant = NULL; + + if (ck == NULL) { + g_set_error (error, + CONSOLEKIT_ERROR, + CONSOLEKIT_ERROR_INVALID_INPUT, + "Invalid LibConsoleKit"); + return -1; + } + + /* connect to the ConsoleKit manager */ + manager_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, + NULL, + CK_NAME, + CK_MANAGER_PATH, + CK_MANAGER_NAME, + NULL, + error); + + /* failed to connect */ + if (manager_proxy == NULL) { + return FALSE; + } + + variant = g_dbus_proxy_call_sync (manager_proxy, + "GetSessionsForUnixUser", + g_variant_new ("(u)", uid), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + error); + + /* We're done with the seat proxy */ + g_clear_object(&manager_proxy); + + if (variant == NULL) { + return -1; + } + + g_variant_get (variant, "(^ao)", sessions); + + g_variant_unref (variant); + variant = NULL; + + return g_strv_length (*sessions); +} + /** * lib_consolekit_seat_get_sessions: * @ck : A #LibConsoleKit diff --git a/libconsolekit/libconsolekit.h b/libconsolekit/libconsolekit.h index fd75362..3619898 100644 --- a/libconsolekit/libconsolekit.h +++ b/libconsolekit/libconsolekit.h @@ -142,6 +142,11 @@ gboolean lib_consolekit_pid_get_session (LibConsoleKit *ck, GError **error); +gint +lib_consolekit_uid_get_sessions (LibConsoleKit *ck, + uid_t uid, + gchar ***sessions, + GError **error); #endif /* LIB_CONSOLEKIT_H_ */ diff --git a/libconsolekit/sd-compat.c b/libconsolekit/sd-compat.c new file mode 100644 index 0000000..69a1576 --- /dev/null +++ b/libconsolekit/sd-compat.c @@ -0,0 +1,144 @@ +#include "config.h" + +#include + +#include +#include +#include +#include +#include + +#include "libconsolekit.h" + +int +sd_session_get_class(const char *session, char **class) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + + ck = lib_consolekit_new (); + + lib_consolekit_session_get_class (ck, session, class, &error); + if (error) { + g_warning ("Unable to determine session class: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return 0; +} + +int +sd_session_get_seat(const char *session, char **seat) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + + ck = lib_consolekit_new (); + + lib_consolekit_session_get_seat (ck, session, seat, &error); + if (error) { + g_warning ("Unable to determine seat: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return 0; +} + +int +sd_session_get_type(const char *session, char **type) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + + ck = lib_consolekit_new (); + + lib_consolekit_session_get_type (ck, session, type, &error); + if (error) { + g_warning ("Unable to determine seat type: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return 0; +} + +int +sd_session_get_state(const char *session, char **state) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + + ck = lib_consolekit_new (); + + lib_consolekit_session_get_state (ck, session, state, &error); + if (error) { + g_warning ("Unable to determine seat state: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return 0; +} + +int +sd_session_get_uid(const char *session, uid_t *uid) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + + ck = lib_consolekit_new (); + + lib_consolekit_session_get_uid (ck, session, uid, &error); + if (error) { + g_warning ("Unable to determine session uid: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return 0; +} + +int +sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) +{ + LibConsoleKit *ck = NULL; + GError *error = NULL; + int ret = 0; + + ck = lib_consolekit_new (); + + ret = lib_consolekit_uid_get_sessions (ck, uid, sessions, &error); + if (error) { + g_warning ("Unable to determine session uid: %s", + error ? error->message : ""); + g_error_free (error); + g_object_unref (ck); + return -ENXIO; + } + + g_object_unref (ck); + + return ret; +} diff --git a/libconsolekit/sd-login.h b/libconsolekit/sd-login.h new file mode 100644 index 0000000..262966c --- /dev/null +++ b/libconsolekit/sd-login.h @@ -0,0 +1,8 @@ +#include + +int sd_session_get_class(const char *session, char **class); +int sd_session_get_seat(const char *session, char **seat); +int sd_session_get_type(const char *session, char **type); +int sd_session_get_state(const char *session, char **state); +int sd_session_get_uid(const char *session, uid_t *uid); +int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions); -- cgit v1.2.3