]> rtime.felk.cvut.cz Git - sojka/lightdm.git/commitdiff
Use logrotate to handle files in the default log directory
authorAlexandros Frantzis <alexandros.frantzis@canonical.com>
Tue, 20 Oct 2015 07:50:44 +0000 (10:50 +0300)
committerAlexandros Frantzis <alexandros.frantzis@canonical.com>
Tue, 20 Oct 2015 07:50:44 +0000 (10:50 +0300)
16 files changed:
debian/changelog
debian/lightdm.logrotate [new file with mode: 0644]
src/Makefile.am
src/lightdm.c
src/log-file.c [new file with mode: 0644]
src/log-file.h [new file with mode: 0644]
src/log-mode.h [new file with mode: 0644]
src/process.c
src/process.h
src/seat.c
src/session-child.c
src/session.c
src/session.h
src/unity-system-compositor.c
src/x-server-local.c
src/x-server-xvnc.c

index 36f20f8461cc018ddfb4c4b569e3a84ea13ae262..a39bdb2debc90186ffc5fa27f05ab8a47e459137 100644 (file)
@@ -1,12 +1,16 @@
 lightdm (1.17.0-0ubuntu1) UNRELEASED; urgency=medium
 
   * New upstream release:
-    - ...
+    - Don't backup system log files to *.old when starting. Handling of these
+      log files is now left to the system (e.g., through logrotate).
   * Build with multi-arch
   * debian/patches/xorg-1.17.patch:
     - Fix xserver-allow-tcp=true option not working with X.org 1.17
   * data/apparmor/abstractions/lightdm_chromium-browser: cgroups support for
     guest sessions. (LP: #1504049, LP: #1464958)
+  * debian/lightdm.logrotate:
+    - Use logrotate to handle log files placed in the default system log
+      directory (/var/log/lightdm).
 
  -- Robert Ancell <robert.ancell@canonical.com>  Mon, 12 Oct 2015 14:57:47 +0100
 
diff --git a/debian/lightdm.logrotate b/debian/lightdm.logrotate
new file mode 100644 (file)
index 0000000..fed4a02
--- /dev/null
@@ -0,0 +1,9 @@
+/var/log/lightdm/*.log {
+    daily
+    missingok
+    rotate 7
+    compress
+    notifempty
+    maxsize 10M
+    copytruncate
+}
index 50139f91e289c0c023430edf3a0cbfed1e28486e..c011e12cefd25e5596031b40518f605a24362b0b 100644 (file)
@@ -19,6 +19,9 @@ lightdm_SOURCES = \
        logger.h \
        login1.c \
        login1.h \
+       log-file.c \
+       log-file.h \
+       log-mode.h \
        mir-server.c \
        mir-server.h \
        plymouth.c \
index d9ec9aff6d6edc4070bd0335955bb60ef21e9dd0..3246b6ab9da13e15f8ce0abbe3f9fe4a43232087 100644 (file)
@@ -33,6 +33,7 @@
 #include "shared-data-manager.h"
 #include "user-list.h"
 #include "login1.h"
+#include "log-file.h"
 
 static gchar *config_path = NULL;
 static GMainLoop *loop = NULL;
@@ -124,7 +125,7 @@ log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message,
 static void
 log_init (void)
 {
-    gchar *log_dir, *path, *old_path;
+    gchar *log_dir, *path;
 
     log_timer = g_timer_new ();
 
@@ -133,13 +134,7 @@ log_init (void)
     path = g_build_filename (log_dir, "lightdm.log", NULL);
     g_free (log_dir);
 
-    /* Move old file out of the way */
-    old_path = g_strdup_printf ("%s.old", path);
-    rename (path, old_path);
-    g_free (old_path);
-
-    /* Create new file and log to it */
-    log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+    log_fd = log_file_open (path, LOG_MODE_APPEND);
     fcntl (log_fd, F_SETFD, FD_CLOEXEC);
     g_log_set_default_handler (log_cb, NULL);
 
diff --git a/src/log-file.c b/src/log-file.c
new file mode 100644 (file)
index 0000000..36d1e8f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2015 Alexandros Frantzis
+ * Author: Alexandros Frantzis <alexandros.frantzis@canonical.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#include "log-file.h"
+
+int
+log_file_open (const gchar *log_filename, LogMode log_mode)
+{
+    int open_flags = O_WRONLY | O_CREAT;
+    int log_fd;
+
+    if (log_mode == LOG_MODE_BACKUP_AND_TRUNCATE)
+    {
+        /* Move old file out of the way */
+        gchar *old_filename;
+
+        old_filename = g_strdup_printf ("%s.old", log_filename);
+        rename (log_filename, old_filename);
+        g_free (old_filename);
+
+        open_flags |= O_TRUNC;
+    }
+    else if (log_mode == LOG_MODE_APPEND)
+    {
+        /* Keep appending to it */
+        open_flags |= O_APPEND;
+    }
+    else
+    {
+        g_warning ("Failed to open log file %s: invalid log mode %d specified",
+                   log_filename, log_mode);
+        return -1;
+    }
+
+    /* Open file and log to it */
+    log_fd = open (log_filename, open_flags, 0600);
+    if (log_fd < 0)
+        g_warning ("Failed to open log file %s: %s", log_filename, g_strerror (errno));
+
+    return log_fd;
+}
diff --git a/src/log-file.h b/src/log-file.h
new file mode 100644 (file)
index 0000000..11d1151
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2015 Alexandros Frantzis
+ * Author: Alexandros Frantzis <alexandros.frantzis@canonical.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#ifndef LOG_FILE_H_
+#define LOG_FILE_H_
+
+#include <glib.h>
+
+#include "log-mode.h"
+
+int log_file_open (const gchar *log_filename, LogMode log_mode);
+
+#endif /* !LOG_FILE_H */
diff --git a/src/log-mode.h b/src/log-mode.h
new file mode 100644 (file)
index 0000000..65e39e0
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015 Alexandros Frantzis
+ * Author: Alexandros Frantzis <alexandros.frantzis@canonical.com>
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#ifndef LOG_MODE_H_
+#define LOG_MODE_H_
+
+typedef enum
+{
+    LOG_MODE_INVALID = -1,
+    LOG_MODE_BACKUP_AND_TRUNCATE,
+    LOG_MODE_APPEND
+} LogMode;
+
+#endif /* !LOD_MODE_H_ */
index ebd1274322f3a932a58a5d462ffbff272257d594..d9b7eb9ae16875a3a164533f2cf5bbdf17860c7a 100644 (file)
@@ -17,9 +17,9 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <grp.h>
-#include <glib/gstdio.h>
 #include <config.h>
 
+#include "log-file.h"
 #include "process.h"
 
 enum {
@@ -39,6 +39,7 @@ struct ProcessPrivate
     /* File to log to */
     gchar *log_file;
     gboolean log_stdout;
+    LogMode log_mode;
 
     /* Command to run */
     gchar *command;
@@ -90,16 +91,18 @@ process_new (ProcessRunFunc run_func, gpointer run_func_data)
     Process *process = g_object_new (PROCESS_TYPE, NULL);
     process->priv->run_func = run_func;
     process->priv->run_func_data = run_func_data;
+    process->priv->log_mode = LOG_MODE_INVALID;
     return process;
 }
 
 void
-process_set_log_file (Process *process, const gchar *path, gboolean log_stdout)
+process_set_log_file (Process *process, const gchar *path, gboolean log_stdout, LogMode log_mode)
 {
     g_return_if_fail (process != NULL);
     g_free (process->priv->log_file);
     process->priv->log_file = g_strdup (path);
     process->priv->log_stdout = log_stdout;
+    process->priv->log_mode = log_mode;
 }
 
 void
@@ -193,19 +196,7 @@ process_start (Process *process, gboolean block)
     }
 
     if (process->priv->log_file)
-    {
-        gchar *old_filename;
-
-        /* Move old file out of the way */
-        old_filename = g_strdup_printf ("%s.old", process->priv->log_file);
-        rename (process->priv->log_file, old_filename);
-        g_free (old_filename);
-
-        /* Create new file and log to it */
-        log_fd = g_open (process->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
-        if (log_fd < 0)
-            g_warning ("Failed to open log file %s: %s", process->priv->log_file, g_strerror (errno));
-    }
+        log_fd = log_file_open (process->priv->log_file, process->priv->log_mode);
 
     /* Work out variables to set */
     env_length = g_hash_table_size (process->priv->env);
index b8c5092675e4896d739e7d886241e0661dbe691b..8a0f2e178ee574c198c016be518b0ebfed6de878 100644 (file)
@@ -14,6 +14,8 @@
 
 #include <glib-object.h>
 
+#include "log-mode.h"
+
 G_BEGIN_DECLS
 
 #define PROCESS_TYPE (process_get_type())
@@ -49,7 +51,7 @@ Process *process_get_current (void);
 
 Process *process_new (ProcessRunFunc run_func, gpointer run_func_data);
 
-void process_set_log_file (Process *process, const gchar *path, gboolean log_stdout);
+void process_set_log_file (Process *process, const gchar *path, gboolean log_stdout, LogMode log_mode);
 
 void process_set_clear_environment (Process *process, gboolean clear_environment);
 
index 2e2afd0ae4fe66ab6c9ca79df9d99cf1b5581a9b..9d00e01d2638ab6f2ac8d0cfafbb69da29196715 100644 (file)
@@ -602,7 +602,7 @@ start_session (Seat *seat, Session *session)
         log_filename = g_build_filename (log_dir, filename, NULL);
         g_free (log_dir);
         g_free (filename);
-        session_set_log_file (session, log_filename);
+        session_set_log_file (session, log_filename, LOG_MODE_APPEND);
         g_free (log_filename);
     }
 
index c5b5c211edf9fbe67a08d5a62dc1a07b3d30b524..d5634171e99ffc81cc47d54122332b7023299a24 100644 (file)
@@ -26,6 +26,8 @@
 #include "session.h"
 #include "console-kit.h"
 #include "login1.h"
+#include "log-file.h"
+#include "log-mode.h"
 #include "privileges.h"
 #include "x-authority.h"
 #include "configuration.h"
@@ -257,7 +259,8 @@ session_child_run (int argc, char **argv)
     int i, version, fd, result;
     gboolean auth_complete = TRUE;
     User *user = NULL;
-    gchar *log_filename, *log_backup_filename = NULL;
+    gchar *log_filename;
+    LogMode log_mode = LOG_MODE_BACKUP_AND_TRUNCATE;
     gsize env_length;
     gsize command_argc;
     gchar **command_argv;
@@ -488,6 +491,8 @@ session_child_run (int argc, char **argv)
 
     /* Get the command to run (blocks) */
     log_filename = read_string ();
+    if (version >= 3)
+        read_data (&log_mode, sizeof (log_mode));
     if (version >= 1)
     {
         g_free (tty);
@@ -522,11 +527,9 @@ session_child_run (int argc, char **argv)
     /* Redirect stderr to a log file */
     if (log_filename)
     {
-        log_backup_filename = g_strdup_printf ("%s.old", log_filename);
         if (g_path_is_absolute (log_filename))
         {
-            rename (log_filename, log_backup_filename);
-            fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
+            fd = log_file_open (log_filename, log_mode);
             dup2 (fd, STDERR_FILENO);
             close (fd);
             g_free (log_filename);
@@ -680,8 +683,7 @@ session_child_run (int argc, char **argv)
 
         if (log_filename)
         {
-            rename (log_filename, log_backup_filename);
-            fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
+            fd = log_file_open (log_filename, log_mode);
             if (fd >= 0)
             {
                 dup2 (fd, STDERR_FILENO);
index f7f4ac09657065a94a70f22b57bb5a96e254bc58..663a3c705fab4dc33e507cdc11d288e8da59f61f 100644 (file)
@@ -84,6 +84,7 @@ struct SessionPrivate
 
     /* File to log to */
     gchar *log_filename;
+    LogMode log_mode;
 
     /* tty this session is running on */
     gchar *tty;
@@ -198,11 +199,12 @@ session_get_is_guest (Session *session)
 }
 
 void
-session_set_log_file (Session *session, const gchar *filename)
+session_set_log_file (Session *session, const gchar *filename, LogMode log_mode)
 {
     g_return_if_fail (session != NULL);
     g_free (session->priv->log_filename);
     session->priv->log_filename = g_strdup (filename);
+    session->priv->log_mode = log_mode;
 }
 
 void
@@ -619,7 +621,7 @@ session_real_start (Session *session)
     close (from_child_input);
 
     /* Indicate what version of the protocol we are using */
-    version = 2;
+    version = 3;
     write_data (session, &version, sizeof (version));
 
     /* Send configuration */
@@ -791,6 +793,7 @@ session_real_run (Session *session)
     if (session->priv->log_filename)
         l_debug (session, "Logging to %s", session->priv->log_filename);
     write_string (session, session->priv->log_filename);
+    write_data (session, &session->priv->log_mode, sizeof (session->priv->log_mode));
     write_string (session, session->priv->tty);
     write_string (session, x_authority_filename);
     g_free (x_authority_filename);
@@ -857,9 +860,11 @@ session_stop (Session *session)
     if (session_get_is_authenticated (session) && !session->priv->command_run)
     {
         gsize n = 0;
+        LogMode log_mode = LOG_MODE_INVALID;
 
         session->priv->command_run = TRUE;
         write_string (session, NULL); // log filename
+        write_data (session, &log_mode, sizeof (log_mode)); // log mode
         write_string (session, NULL); // tty
         write_string (session, NULL); // xauth filename
         write_string (session, NULL); // xdisplay
@@ -903,6 +908,7 @@ session_init (Session *session)
 {
     session->priv = G_TYPE_INSTANCE_GET_PRIVATE (session, SESSION_TYPE, SessionPrivate);
     session->priv->log_filename = g_strdup (".xsession-errors");
+    session->priv->log_mode = LOG_MODE_BACKUP_AND_TRUNCATE;
     session->priv->to_child_input = -1;
     session->priv->from_child_output = -1;
 }
index c62303e66b1b20781480b3df8f0696a3ffcea8e7..50f9cf2a664320eb456a906fd709fb081617d118 100644 (file)
@@ -23,6 +23,7 @@ typedef struct Session Session;
 #include "accounts.h"
 #include "x-authority.h"
 #include "logger.h"
+#include "log-mode.h"
 
 G_BEGIN_DECLS
 
@@ -84,7 +85,7 @@ void session_set_is_guest (Session *session, gboolean is_guest);
 
 gboolean session_get_is_guest (Session *session);
 
-void session_set_log_file (Session *session, const gchar *filename);
+void session_set_log_file (Session *session, const gchar *filename, LogMode log_mode);
 
 void session_set_display_server (Session *session, DisplayServer *display_server);
 
index 0b89d2c23f4e2324137196caf2af051f43fc9779..203f2aeba1252c808eeee52494ec70e59cad5651 100644 (file)
@@ -407,7 +407,7 @@ unity_system_compositor_start (DisplayServer *server)
 
     /* Setup environment */
     compositor->priv->process = process_new (run_cb, compositor);
-    process_set_log_file (compositor->priv->process, log_file, TRUE);
+    process_set_log_file (compositor->priv->process, log_file, TRUE, LOG_MODE_APPEND);
     g_free (log_file);
     process_set_clear_environment (compositor->priv->process, TRUE);
     process_set_env (compositor->priv->process, "XDG_SEAT", "seat0");
index dcbfbc0a50127b43a1b4d6e28dbc50d78f1629ca..384c76c3ab98a5461e1e634a955714be167d59c9 100644 (file)
@@ -493,7 +493,7 @@ x_server_local_start (DisplayServer *display_server)
     filename = g_strdup_printf ("%s.log", display_server_get_name (display_server));
     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
     log_file = g_build_filename (dir, filename, NULL);
-    process_set_log_file (server->priv->x_server_process, log_file, TRUE);
+    process_set_log_file (server->priv->x_server_process, log_file, TRUE, LOG_MODE_APPEND);
     l_debug (display_server, "Logging to %s", log_file);
     g_free (log_file);
     g_free (filename);
index aea2817733f37a6a4d1759247bd6308998124271..9b03fff7e38cf00fd9bb9c284e6f79e5cab821e7 100644 (file)
@@ -201,7 +201,7 @@ x_server_xvnc_start (DisplayServer *display_server)
     filename = g_strdup_printf ("%s.log", display_server_get_name (display_server));
     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
     log_file = g_build_filename (dir, filename, NULL);
-    process_set_log_file (server->priv->x_server_process, log_file, FALSE);
+    process_set_log_file (server->priv->x_server_process, log_file, FALSE, LOG_MODE_APPEND);
     l_debug (display_server, "Logging to %s", log_file);
     g_free (log_file);
     g_free (filename);