From 352609297341c88ef9236f574f51c5d194f835f5 Mon Sep 17 00:00:00 2001 From: Ondrej Holy Date: Fri, 2 Nov 2012 16:04:56 +0100 Subject: [PATCH 2/2] accountsservice: Add User.LoginHistory property https://bugzilla.gnome.org/show_bug.cgi?id=681772 --- data/org.freedesktop.Accounts.User.xml | 19 +++++++ src/daemon.c | 98 ++++++++++++++++++++++++++++------ src/libaccountsservice/act-user.c | 39 ++++++++++++++ src/libaccountsservice/act-user.h | 1 + src/user.c | 16 ++++++ 5 files changed, 156 insertions(+), 17 deletions(-) diff --git a/data/org.freedesktop.Accounts.User.xml b/data/org.freedesktop.Accounts.User.xml index 11884d6..63efa79 100644 --- a/data/org.freedesktop.Accounts.User.xml +++ b/data/org.freedesktop.Accounts.User.xml @@ -663,6 +663,25 @@ + + + + + The login history for this user. + Each entry in the array represents a login session. The first two + members are the login time and logout time, as timestamps (seconds + since the epoch). If the session is still running, the logout time + is 0. + + + The a{sv} member is a dictionary containing additional information + about the session. Possible members include 'type' (with values like + ':0', 'tty0', 'pts/0' etc). + + + + + diff --git a/src/daemon.c b/src/daemon.c index f795d4c..446b28c 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -209,17 +209,33 @@ static struct passwd * entry_generator_wtmp (GHashTable *users, gpointer *state) { - GHashTable *login_frequency_hash; + struct history_data { + gint64 login_time; + gint64 logout_time; + gchar *line; + }; + + GHashTable *login_hash, *logout_hash; struct utmpx *wtmp_entry; GHashTableIter iter; - gpointer key, value; + gpointer key, value, ptr; struct passwd *pwent; User *user; struct hash_data *data; + struct state_data *state_data; + struct history_data *history_data; + GVariantBuilder *builder, *builder2; + GList *l; struct hash_data { int frequency; gint64 time; + GList *history; + }; + + struct state_data { + GHashTable *login_hash; + GHashTable *logout_hash; }; if (*state == NULL) { @@ -232,12 +248,32 @@ entry_generator_wtmp (GHashTable *users, utmpxname (_PATH_WTMPX); setutxent (); #endif - *state = g_hash_table_new (g_str_hash, g_str_equal); + *state = g_new (struct state_data, 1); + state_data = *state; + state_data->login_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + state_data->logout_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); } /* Every iteration */ - login_frequency_hash = *state; + state_data = *state; + login_hash = state_data->login_hash; + logout_hash = state_data->logout_hash; while ((wtmp_entry = getutxent ())) { + if (wtmp_entry->ut_type == BOOT_TIME) { + /* Set boot time for missing logout records */ + g_hash_table_iter_init (&iter, logout_hash); + while (g_hash_table_iter_next (&iter, &key, &value)) { + *(gint64 *)value = wtmp_entry->ut_tv.tv_sec; + } + g_hash_table_remove_all (logout_hash); + } else if (wtmp_entry->ut_type == DEAD_PROCESS) { + /* Save corresponding logout time */ + if (g_hash_table_lookup_extended (logout_hash, wtmp_entry->ut_line, &key, &value)) { + *(gint64 *)value = wtmp_entry->ut_tv.tv_sec; + g_hash_table_remove (logout_hash, wtmp_entry->ut_line); + } + } + if (wtmp_entry->ut_type != USER_PROCESS) { continue; } @@ -251,43 +287,71 @@ entry_generator_wtmp (GHashTable *users, continue; } - if (!g_hash_table_lookup_extended (login_frequency_hash, + if (!g_hash_table_lookup_extended (login_hash, wtmp_entry->ut_user, &key, &value)) { data = g_new (struct hash_data, 1); - data->frequency = 1; - data->time = wtmp_entry->ut_tv.tv_sec; - g_hash_table_insert (login_frequency_hash, - g_strdup (wtmp_entry->ut_user), - data); + data->frequency = 0; + data->history = NULL; + + g_hash_table_insert (login_hash, g_strdup (wtmp_entry->ut_user), data); } else { data = value; - data->frequency++; - data->time = wtmp_entry->ut_tv.tv_sec; } + data->frequency++; + data->time = wtmp_entry->ut_tv.tv_sec; + + /* Add zero logout time to change it later on logout record */ + history_data = g_new (struct history_data, 1); + history_data->login_time = wtmp_entry->ut_tv.tv_sec; + history_data->logout_time = 0; + history_data->line = g_strdup (wtmp_entry->ut_line); + data->history = g_list_append (data->history, history_data); + + g_hash_table_insert (logout_hash, g_strdup (wtmp_entry->ut_line), &history_data->logout_time); + return pwent; } /* Last iteration */ endutxent (); - g_hash_table_iter_init (&iter, login_frequency_hash); + g_hash_table_iter_init (&iter, login_hash); while (g_hash_table_iter_next (&iter, &key, &value)) { - User *user; - guint64 frequency = (guint64) GPOINTER_TO_UINT (value); + data = value; user = g_hash_table_lookup (users, key); if (user == NULL) { + for (l = data->history; l != NULL; l = l->next) { + history_data = l->data; + g_free (history_data->line); + } + g_list_free (data->history); continue; } g_object_set (user, "login-frequency", data->frequency, NULL); g_object_set (user, "login-time", data->time, NULL); + + builder = g_variant_builder_new (G_VARIANT_TYPE ("a(xxa{sv})")); + for (l = data->history; l != NULL; l = l->next) { + history_data = l->data; + + builder2 = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (builder2, "{sv}", "type", g_variant_new_string (history_data->line)); + g_variant_builder_add (builder, "(xxa{sv})", history_data->login_time, history_data->logout_time, builder2); + g_variant_builder_unref (builder2); + g_free (history_data->line); + } + g_object_set (user, "login-history", g_variant_new ("a(xxa{sv})", builder), NULL); + g_variant_builder_unref (builder); + g_list_free (data->history); } - g_hash_table_foreach (login_frequency_hash, (GHFunc) g_free, NULL); - g_hash_table_unref (login_frequency_hash); + g_hash_table_unref (login_hash); + g_hash_table_unref (logout_hash); + g_free (state_data); *state = NULL; return NULL; } diff --git a/src/libaccountsservice/act-user.c b/src/libaccountsservice/act-user.c index 33d15a8..9f80e71 100644 --- a/src/libaccountsservice/act-user.c +++ b/src/libaccountsservice/act-user.c @@ -58,6 +58,7 @@ enum { PROP_LOCAL_ACCOUNT, PROP_LOGIN_FREQUENCY, PROP_LOGIN_TIME, + PROP_LOGIN_HISTORY, PROP_ICON_FILE, PROP_LANGUAGE, PROP_X_SESSION, @@ -93,6 +94,7 @@ struct _ActUser { GList *sessions; int login_frequency; gint64 login_time; + GVariant *login_history; ActUserAccountType account_type; ActUserPasswordMode password_mode; @@ -211,6 +213,9 @@ act_user_get_property (GObject *object, case PROP_LOGIN_TIME: g_value_set_int64 (value, user->login_time); break; + case PROP_LOGIN_HISTORY: + g_value_set_variant (value, user->login_history); + break; case PROP_SHELL: g_value_set_string (value, user->shell); break; @@ -357,6 +362,14 @@ act_user_class_init (ActUserClass *class) 0, G_PARAM_READABLE)); g_object_class_install_property (gobject_class, + PROP_LOGIN_HISTORY, + g_param_spec_variant ("login-history", + "Login history", + "The login history for this user.", + G_VARIANT_TYPE ("a(xxa{sv})"), + NULL, + G_PARAM_READABLE)); + g_object_class_install_property (gobject_class, PROP_ICON_FILE, g_param_spec_string ("icon-file", "Icon File", @@ -443,6 +456,7 @@ act_user_init (ActUser *user) user->user_name = NULL; user->real_name = NULL; user->sessions = NULL; + user->login_history = NULL; user->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (user->connection == NULL) { @@ -468,6 +482,7 @@ act_user_finalize (GObject *object) g_free (user->shell); g_free (user->email); g_free (user->location); + g_variant_unref (user->login_history); if (user->accounts_proxy != NULL) { g_object_unref (user->accounts_proxy); @@ -705,6 +720,22 @@ act_user_get_login_time (ActUser *user) { return user->login_time; } +/** + * act_user_get_login_history: + * @user: a #ActUser + * + * Returns the login history for @user. + * + * Returns: (transfer none): a pointer to GVariant of type "a(xxa{sv})" + * which must not be modified or freed, or %NULL. + */ +const GVariant * +act_user_get_login_history (ActUser *user) { + g_return_val_if_fail (ACT_IS_USER (user), NULL); + + return user->login_history; +} + int act_user_collate (ActUser *user1, ActUser *user2) @@ -1072,6 +1103,14 @@ collect_props (const gchar *key, user->login_time = new_login_time; g_object_notify (G_OBJECT (user), "login-time"); } + } else if (strcmp (key, "LoginHistory") == 0) { + GVariant *new_login_history = value; + + if (!g_variant_compare (user->login_history, new_login_history)) { + g_variant_unref (user->login_history); + user->login_history = g_variant_ref (new_login_history); + g_object_notify (G_OBJECT (user), "login-history"); + } } else if (strcmp (key, "IconFile") == 0) { const char *new_icon_file; diff --git a/src/libaccountsservice/act-user.h b/src/libaccountsservice/act-user.h index 70a97c1..31b2cc6 100644 --- a/src/libaccountsservice/act-user.h +++ b/src/libaccountsservice/act-user.h @@ -67,6 +67,7 @@ guint act_user_get_num_sessions (ActUser *user); gboolean act_user_is_logged_in (ActUser *user); int act_user_get_login_frequency (ActUser *user); gint64 act_user_get_login_time (ActUser *user); +const GVariant*act_user_get_login_history (ActUser *user); gboolean act_user_get_locked (ActUser *user); gboolean act_user_get_automatic_login (ActUser *user); gboolean act_user_is_system_account (ActUser *user); diff --git a/src/user.c b/src/user.c index f9e09a6..8838872 100644 --- a/src/user.c +++ b/src/user.c @@ -60,6 +60,7 @@ enum { PROP_LOCATION, PROP_LOGIN_FREQUENCY, PROP_LOGIN_TIME, + PROP_LOGIN_HISTORY, PROP_ICON_FILE, PROP_LOCKED, PROP_PASSWORD_MODE, @@ -92,6 +93,7 @@ struct User { gchar *location; guint64 login_frequency; gint64 login_time; + GVariant *login_history; gchar *icon_file; gchar *default_icon_file; gboolean locked; @@ -1691,6 +1693,12 @@ user_real_get_login_time (AccountsUser *user) return USER (user)->login_time; } +static const GVariant * +user_real_get_login_history (AccountsUser *user) +{ + return USER (user)->login_history; +} + static const gchar * user_real_get_icon_file (AccountsUser *user) { @@ -1781,6 +1789,9 @@ user_set_property (GObject *object, case PROP_LOGIN_TIME: user->login_time = g_value_get_int64 (value); break; + case PROP_LOGIN_HISTORY: + user->login_history = g_variant_ref (g_value_get_variant (value)); + break; case PROP_AUTOMATIC_LOGIN: user->automatic_login = g_value_get_boolean (value); break; @@ -1854,6 +1865,9 @@ user_get_property (GObject *object, case PROP_LOGIN_TIME: g_value_set_int64 (value, user->login_time); break; + case PROP_LOGIN_HISTORY: + g_value_set_variant (value, user->login_history); + break; case PROP_LOCKED: g_value_set_boolean (value, user->locked); break; @@ -1915,6 +1929,7 @@ user_accounts_user_iface_init (AccountsUserIface *iface) iface->get_location = user_real_get_location; iface->get_login_frequency = user_real_get_login_frequency; iface->get_login_time = user_real_get_login_time; + iface->get_login_history = user_real_get_login_history; iface->get_icon_file = user_real_get_icon_file; iface->get_locked = user_real_get_locked; iface->get_password_mode = user_real_get_password_mode; @@ -1944,4 +1959,5 @@ user_init (User *user) user->locked = FALSE; user->automatic_login = FALSE; user->system_account = FALSE; + user->login_history = NULL; } -- 1.7.11.7