[gnome-logs] Improve the check for journal permissions



commit 1dffafc58fc8114c7d5e93f7e1341364ff1b9a7e
Author: Dirk Maetens <glitsj16 gmail com>
Date:   Wed Aug 10 18:27:02 2016 +0800

    Improve the check for journal permissions
    
    There are mainly two kind of ways to storage journal files, one is
    persistent and the other is volatile. So both method should be
    considered when checking is the user has permissons to access journal
    files.

 src/gl-util.c   |   40 +++++++++++++++++++++++++-
 src/gl-util.h   |   19 ++++++++++++-
 src/gl-window.c |   83 +++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 119 insertions(+), 23 deletions(-)
---
diff --git a/src/gl-util.c b/src/gl-util.c
index 27eba60..e10ff58 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -325,8 +325,33 @@ gl_util_boot_time_to_display (guint64 realtime_first,
     return time_display;
 }
 
+/**
+ * Determine journal storage type:
+ *
+ * Test existence of possible journal storage paths.
+ *
+ * Returns: a value from GlJournalStorage
+ */
+GlJournalStorage
+gl_util_journal_storage_type (void)
+{
+    if (g_file_test ("/run/log/journal", G_FILE_TEST_EXISTS))
+    {
+        return GL_JOURNAL_STORAGE_VOLATILE;
+    }
+    else
+    {
+        return GL_JOURNAL_STORAGE_PERSISTENT;
+    }
+
+    if ((!g_file_test ("/run/log/journal", G_FILE_TEST_EXISTS)) && (!g_file_test ("/var/log/journal", 
G_FILE_TEST_EXISTS)))
+    {
+        return GL_JOURNAL_STORAGE_NONE;
+    }
+}
+
 gboolean
-gl_util_can_read_system_journal (void)
+gl_util_can_read_system_journal (GlJournalStorage storage_type)
 {
     GFile *file;
     GFileInfo *info;
@@ -342,7 +367,18 @@ gl_util_can_read_system_journal (void)
     }
     sd_id128_to_string (machine, ids);
 
-    path = g_build_filename ("/var/log/journal", ids, "system.journal", NULL);
+    if (storage_type == GL_JOURNAL_STORAGE_PERSISTENT)
+    {
+        path = g_build_filename ("/var/log/journal", ids, "system.journal", NULL);
+    }
+    else if (storage_type == GL_JOURNAL_STORAGE_VOLATILE)
+    {
+        path = g_build_filename ("/run/log/journal", ids, "system.journal", NULL);
+    }
+    else
+    {
+        path = "/dev/null";
+    }
 
     file = g_file_new_for_path (path);
     info = g_file_query_info (file, G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
diff --git a/src/gl-util.h b/src/gl-util.h
index bbe1a2c..1b1c6ac 100644
--- a/src/gl-util.h
+++ b/src/gl-util.h
@@ -35,6 +35,22 @@ typedef enum
     GL_UTIL_CLOCK_FORMAT_12HR
 } GlUtilClockFormat;
 
+/*
+ * GlJournalStorage:
+ * @GL_JOURNAL_STORAGE_NONE: no log data
+ * @GL_JOURNAL_STORAGE_PERSISTENT: log data stored on disk
+ * below /var/log/journal hierarchy
+ * @GL_JOURNAL_STORAGE_VOLATILE:  log data stored in memory
+ * below /run/log/journal hierarchy
+ *
+ * Determine journal storage type, used in warning logic.*/
+typedef enum
+{
+    GL_JOURNAL_STORAGE_NONE,
+    GL_JOURNAL_STORAGE_PERSISTENT,
+    GL_JOURNAL_STORAGE_VOLATILE
+} GlJournalStorage;
+
 void gl_util_on_css_provider_parsing_error (GtkCssProvider *provider,
                                             GtkCssSection *section,
                                             GError *error,
@@ -46,7 +62,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);
+GlJournalStorage gl_util_journal_storage_type (void);
+gboolean gl_util_can_read_system_journal (GlJournalStorage storage_type);
 gboolean gl_util_can_read_user_journal (void);
 
 G_END_DECLS
diff --git a/src/gl-window.c b/src/gl-window.c
index ad66f0f..7e3b87c 100644
--- a/src/gl-window.c
+++ b/src/gl-window.c
@@ -423,6 +423,7 @@ gl_window_init (GlWindow *window)
     GlEventView *event;
     GAction *action_view_boot;
     GArray *boot_ids;
+    GlJournalStorage storage_type;
     GlJournalBootID *boot_id;
     gchar *boot_match;
     GVariant *variant;
@@ -459,32 +460,74 @@ gl_window_init (GlWindow *window)
                                                GTK_STYLE_PROVIDER (provider),
                                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
-    if (!gl_util_can_read_system_journal ())
+    /* Show warnings based on storage type. */
+    storage_type = gl_util_journal_storage_type ();
+    switch (storage_type)
     {
-        GtkWidget *message_label;
-        GtkWidget *content_area;
+        case GL_JOURNAL_STORAGE_PERSISTENT:
+        {
+            if (!gl_util_can_read_system_journal (GL_JOURNAL_STORAGE_PERSISTENT))
+            {
+                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);
+                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);
-    }
+                gtk_widget_show (priv->info_bar);
+            }
 
-    if (!gl_util_can_read_user_journal ())
-    {
-        GtkWidget *message_label;
-        GtkWidget *content_area;
+            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);
+                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);
+                gtk_widget_show (priv->info_bar);
+            }
+            break;
+        }
+        case GL_JOURNAL_STORAGE_VOLATILE:
+        {
+            if (!gl_util_can_read_system_journal (GL_JOURNAL_STORAGE_VOLATILE))
+            {
+                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);
+            }
+            break;
+        }
+        case GL_JOURNAL_STORAGE_NONE:
+        {
+            GtkWidget *message_label;
+            GtkWidget *content_area;
+
+            message_label = gtk_label_new (_("No logs available"));
+            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);
+            break;
+        }
+        default:
+            g_assert_not_reached ();
     }
 
     g_object_unref (provider);


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