[gnome-logs] Warn if the user doesn't have permissions to view journal logs



commit 61192b29feb963c74fdb38d402740fce620f1423
Author: Jonathan Kang <jonathan121537 gmail com>
Date:   Mon Oct 12 17:17:59 2015 +0800

    Warn if the user doesn't have permissions to view journal logs
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726233

 data/gl-window.ui |   32 ++++++++++++++++-
 src/gl-util.c     |  102 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gl-util.h     |    2 +
 src/gl-window.c   |   57 +++++++++++++++++++++++++++++
 4 files changed, 192 insertions(+), 1 deletions(-)
---
diff --git a/data/gl-window.ui b/data/gl-window.ui
index 6f41df1..43eafc7 100644
--- a/data/gl-window.ui
+++ b/data/gl-window.ui
@@ -13,8 +13,38 @@
             </object>
         </child>
         <child>
-            <object class="GlEventView" id="event">
+            <object class="GtkBox" id="event_box">
+                <property name="orientation">vertical</property>
                 <property name="visible">True</property>
+                <child>
+                    <object class="GtkInfoBar" id="info_bar">
+                        <property name="message-type">GTK_MESSAGE_ERROR</property>
+                        <child>
+                            <object class="GtkButtonBox" id="action_area">
+                                <property name="visible">True</property>
+                                <property name="orientation">vertical</property>
+                                <property name="layout_style">center</property>
+                                <child>
+                                    <object class="GtkButton" id="help_button">
+                                        <property name="visible">True</property>
+                                        <property name="halign">center</property>
+                                        <property name="label" translatable="yes">Help</property>
+                                        <signal name="clicked" handler="on_help_button_clicked" 
object="GlWindow"/>
+                                    </object>
+                                </child>
+                            </object>
+                            <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">True</property>
+                            </packing>
+                        </child>
+                    </object>
+                </child>
+                <child>
+                    <object class="GlEventView" id="event">
+                        <property name="visible">True</property>
+                    </object>
+                </child>
             </object>
         </child>
     </template>
diff --git a/src/gl-util.c b/src/gl-util.c
index 7f97768..0e15662 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -21,6 +21,8 @@
 #include "gl-util.h"
 
 #include <glib/gi18n.h>
+#include <systemd/sd-id128.h>
+#include <systemd/sd-journal.h>
 
 /**
  * GlUtilTimestamps:
@@ -247,3 +249,103 @@ gl_util_boot_time_to_display (guint64 realtime_first,
 
     return time_display;
 }
+
+gboolean
+gl_util_can_read_system_journal (void)
+{
+    GFile *file;
+    GFileInfo *info;
+    gint ret;
+    gchar *path;
+    gchar ids[33];
+    sd_id128_t machine;
+
+    ret = sd_id128_get_machine (&machine);
+    if (ret < 0)
+    {
+        g_critical ("Error getting machine id: %s", g_strerror (-ret));
+    }
+    sd_id128_to_string (machine, ids);
+
+    path = g_build_filename ("/var/log/journal", ids, "system.journal", NULL);
+
+    file = g_file_new_for_path (path);
+    info = g_file_query_info (file, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
+                              G_FILE_QUERY_INFO_NONE, NULL, NULL);
+
+    g_free (path);
+    g_object_unref (file);
+
+    if (g_file_info_get_attribute_boolean (info,
+                                           G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
+    {
+        g_object_unref (info);
+
+        return TRUE;
+    }
+    else
+    {
+        g_object_unref (info);
+
+        return FALSE;
+    }
+}
+
+gboolean
+gl_util_can_read_user_journal (void)
+{
+    GFile *file;
+    GFileInfo *info;
+    gint ret;
+    gchar *path;
+    gchar ids[33];
+    gchar *filename;
+    gchar *uid;
+    uid_t user_id;
+    sd_id128_t machine;
+    GError *error = NULL;
+    GCredentials *credentials;
+
+    credentials = g_credentials_new ();
+    user_id = g_credentials_get_unix_user (credentials, &error);
+    if (error != NULL)
+    {
+        g_debug ("Unable to get uid: %s", error->message);
+        g_error_free (error);
+    }
+    uid = g_strdup_printf ("%d", user_id);
+    filename = g_strconcat ("/user-", uid, ".journal", NULL);
+
+    ret = sd_id128_get_machine (&machine);
+    if (ret < 0)
+    {
+        g_critical ("Error getting machine id: %s", g_strerror (-ret));
+    }
+    sd_id128_to_string (machine, ids);
+
+    path = g_build_filename ("/var/log/journal", ids, filename, NULL);
+
+    file = g_file_new_for_path (path);
+    info = g_file_query_info (file, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
+                              G_FILE_QUERY_INFO_NONE, NULL, NULL);
+
+    g_free (uid);
+    g_free (path);
+    g_free (filename);
+    g_object_unref (file);
+    g_object_unref (credentials);
+
+    if (g_file_info_get_attribute_boolean (info,
+                                           G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
+    {
+        g_object_unref (info);
+
+        return TRUE;
+    }
+    else
+    {
+        g_object_unref (info);
+
+        return FALSE;
+    }
+}
diff --git a/src/gl-util.h b/src/gl-util.h
index 4971564..a70910f 100644
--- a/src/gl-util.h
+++ b/src/gl-util.h
@@ -45,6 +45,8 @@ gchar * gl_util_timestamp_to_display (guint64 microsecs,
 gint gl_util_get_uid (void);
 gchar * gl_util_boot_time_to_display (guint64 timestamp_first,
                                       guint64 timestamp_last);
+gboolean gl_util_can_read_system_journal (void);
+gboolean gl_util_can_read_user_journal (void);
 
 G_END_DECLS
 
diff --git a/src/gl-window.c b/src/gl-window.c
index 0a06050..ad66f0f 100644
--- a/src/gl-window.c
+++ b/src/gl-window.c
@@ -37,6 +37,7 @@ typedef struct
 {
     GtkWidget *event_toolbar;
     GtkWidget *event;
+    GtkWidget *info_bar;
 } GlWindowPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GlWindow, gl_window, GTK_TYPE_APPLICATION_WINDOW)
@@ -344,6 +345,30 @@ on_gl_window_key_press_event (GlWindow *window,
     return GDK_EVENT_PROPAGATE;
 }
 
+static void
+on_help_button_clicked (GlWindow *window,
+                        gint response_id,
+                        gpointer user_data)
+{
+    GlWindowPrivate *priv;
+    GtkWindow *parent;
+    GError *error = NULL;
+
+    parent = GTK_WINDOW (window);
+    priv = gl_window_get_instance_private (GL_WINDOW (window));
+
+    gtk_show_uri (gtk_window_get_screen (parent), "help:gnome-logs/permissions",
+                  GDK_CURRENT_TIME, &error);
+
+    if (error)
+    {
+        g_debug ("Error while opening help: %s", error->message);
+        g_error_free (error);
+    }
+
+    gtk_widget_hide (priv->info_bar);
+}
+
 void
 gl_window_set_sort_order (GlWindow *window,
                           GlSortOrder sort_order)
@@ -379,9 +404,13 @@ gl_window_class_init (GlWindowClass *klass)
                                                   event_toolbar);
     gtk_widget_class_bind_template_child_private (widget_class, GlWindow,
                                                   event);
+    gtk_widget_class_bind_template_child_private (widget_class, GlWindow,
+                                                  info_bar);
 
     gtk_widget_class_bind_template_callback (widget_class,
                                              on_gl_window_key_press_event);
+    gtk_widget_class_bind_template_callback (widget_class,
+                                             on_help_button_clicked);
 }
 
 static void
@@ -430,6 +459,34 @@ gl_window_init (GlWindow *window)
                                                GTK_STYLE_PROVIDER (provider),
                                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
+    if (!gl_util_can_read_system_journal ())
+    {
+        GtkWidget *message_label;
+        GtkWidget *content_area;
+
+        message_label = gtk_label_new (_("Unable to read system logs"));
+        gtk_widget_set_hexpand (GTK_WIDGET (message_label), TRUE);
+        gtk_widget_show (message_label);
+        content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->info_bar));
+        gtk_container_add (GTK_CONTAINER (content_area), message_label);
+
+        gtk_widget_show (priv->info_bar);
+    }
+
+    if (!gl_util_can_read_user_journal ())
+    {
+        GtkWidget *message_label;
+        GtkWidget *content_area;
+
+        message_label = gtk_label_new (_("Unable to read user logs"));
+        gtk_widget_set_hexpand (GTK_WIDGET (message_label), TRUE);
+        gtk_widget_show (message_label);
+        content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (priv->info_bar));
+        gtk_container_add (GTK_CONTAINER (content_area), message_label);
+
+        gtk_widget_show (priv->info_bar);
+    }
+
     g_object_unref (provider);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]