From 5e9b511e75fdd30997b91ea3c2894eec8bbe3264 Mon Sep 17 00:00:00 2001 From: Alexandros Frantzis Date: Tue, 20 Oct 2015 10:50:44 +0300 Subject: [PATCH] Use logrotate to handle files in the default log directory --- debian/changelog | 6 +++- debian/lightdm.logrotate | 9 ++++++ src/Makefile.am | 3 ++ src/lightdm.c | 11 ++------ src/log-file.c | 53 +++++++++++++++++++++++++++++++++++ src/log-file.h | 21 ++++++++++++++ src/log-mode.h | 22 +++++++++++++++ src/process.c | 21 ++++---------- src/process.h | 4 ++- src/seat.c | 2 +- src/session-child.c | 14 +++++---- src/session.c | 10 +++++-- src/session.h | 3 +- src/unity-system-compositor.c | 2 +- src/x-server-local.c | 2 +- src/x-server-xvnc.c | 2 +- 16 files changed, 147 insertions(+), 38 deletions(-) create mode 100644 debian/lightdm.logrotate create mode 100644 src/log-file.c create mode 100644 src/log-file.h create mode 100644 src/log-mode.h diff --git a/debian/changelog b/debian/changelog index 36f20f84..a39bdb2d 100644 --- a/debian/changelog +++ b/debian/changelog @@ -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 Mon, 12 Oct 2015 14:57:47 +0100 diff --git a/debian/lightdm.logrotate b/debian/lightdm.logrotate new file mode 100644 index 00000000..fed4a02b --- /dev/null +++ b/debian/lightdm.logrotate @@ -0,0 +1,9 @@ +/var/log/lightdm/*.log { + daily + missingok + rotate 7 + compress + notifempty + maxsize 10M + copytruncate +} diff --git a/src/Makefile.am b/src/Makefile.am index 50139f91..c011e12c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/lightdm.c b/src/lightdm.c index d9ec9aff..3246b6ab 100644 --- a/src/lightdm.c +++ b/src/lightdm.c @@ -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 index 00000000..36d1e8ff --- /dev/null +++ b/src/log-file.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 Alexandros Frantzis + * Author: Alexandros Frantzis + * + * 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 +#include +#include + +#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 index 00000000..11d1151b --- /dev/null +++ b/src/log-file.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2015 Alexandros Frantzis + * Author: Alexandros Frantzis + * + * 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 + +#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 index 00000000..65e39e0a --- /dev/null +++ b/src/log-mode.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2015 Alexandros Frantzis + * Author: Alexandros Frantzis + * + * 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_ */ diff --git a/src/process.c b/src/process.c index ebd12743..d9b7eb9a 100644 --- a/src/process.c +++ b/src/process.c @@ -17,9 +17,9 @@ #include #include #include -#include #include +#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); diff --git a/src/process.h b/src/process.h index b8c50926..8a0f2e17 100644 --- a/src/process.h +++ b/src/process.h @@ -14,6 +14,8 @@ #include +#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); diff --git a/src/seat.c b/src/seat.c index 2e2afd0a..9d00e01d 100644 --- a/src/seat.c +++ b/src/seat.c @@ -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); } diff --git a/src/session-child.c b/src/session-child.c index c5b5c211..d5634171 100644 --- a/src/session-child.c +++ b/src/session-child.c @@ -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); diff --git a/src/session.c b/src/session.c index f7f4ac09..663a3c70 100644 --- a/src/session.c +++ b/src/session.c @@ -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; } diff --git a/src/session.h b/src/session.h index c62303e6..50f9cf2a 100644 --- a/src/session.h +++ b/src/session.h @@ -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); diff --git a/src/unity-system-compositor.c b/src/unity-system-compositor.c index 0b89d2c2..203f2aeb 100644 --- a/src/unity-system-compositor.c +++ b/src/unity-system-compositor.c @@ -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"); diff --git a/src/x-server-local.c b/src/x-server-local.c index dcbfbc0a..384c76c3 100644 --- a/src/x-server-local.c +++ b/src/x-server-local.c @@ -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); diff --git a/src/x-server-xvnc.c b/src/x-server-xvnc.c index aea28177..9b03fff7 100644 --- a/src/x-server-xvnc.c +++ b/src/x-server-xvnc.c @@ -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); -- 2.39.2