From 48d1f7b44af7dd1903e6fb0b33c6c1d45ba5875e Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 28 Oct 2015 16:35:38 +1300 Subject: Add a backup-logs option that can be used to disable existing logging files having a .old suffix added to them --- data/lightdm.conf | 2 ++ src/Makefile.am | 2 ++ src/lightdm.c | 13 ++++------- src/log-file.c | 53 +++++++++++++++++++++++++++++++++++++++++++ src/log-file.h | 26 +++++++++++++++++++++ src/process.c | 21 +++++------------ src/process.h | 4 +++- src/seat.c | 4 +++- src/session-child.c | 13 ++++++----- src/session.c | 10 ++++++-- src/session.h | 3 ++- src/unity-system-compositor.c | 5 ++-- src/x-server-local.c | 5 ++-- src/x-server-xvnc.c | 5 ++-- 14 files changed, 126 insertions(+), 40 deletions(-) create mode 100644 src/log-file.c create mode 100644 src/log-file.h diff --git a/data/lightdm.conf b/data/lightdm.conf index c29b00eb..5896910a 100644 --- a/data/lightdm.conf +++ b/data/lightdm.conf @@ -16,6 +16,7 @@ # sessions-directory = Directory to find sessions # remote-sessions-directory = Directory to find remote sessions # greeters-directory = Directory to find greeters +# backup-logs = True to move add a .old suffix to old log files when opening new ones # [LightDM] #start-default-seat=true @@ -33,6 +34,7 @@ #sessions-directory=/usr/share/lightdm/sessions:/usr/share/xsessions #remote-sessions-directory=/usr/share/lightdm/remote-sessions #greeters-directory=/usr/share/lightdm/greeters:/usr/share/xgreeters +#backup-logs=true # # Seat defaults diff --git a/src/Makefile.am b/src/Makefile.am index da16d630..1004c0a2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,8 @@ lightdm_SOURCES = \ logger.h \ login1.c \ login1.h \ + log-file.c \ + log-file.h \ mir-server.c \ mir-server.h \ plymouth.c \ diff --git a/src/lightdm.c b/src/lightdm.c index d855c4cb..22918885 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; @@ -119,7 +120,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 (); @@ -128,13 +129,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); @@ -1322,6 +1317,8 @@ main (int argc, char **argv) config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER); if (!config_has_key (config_get_instance (), "LightDM", "lock-memory")) config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE); + if (!config_has_key (config_get_instance (), "LightDM", "backup-logs")) + config_set_boolean (config_get_instance (), "LightDM", "backup-logs", TRUE); if (!config_has_key (config_get_instance (), "SeatDefaults", "type")) config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal"); if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-service")) 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..5081bf45 --- /dev/null +++ b/src/log-file.h @@ -0,0 +1,26 @@ +/* + * 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 + +typedef enum +{ + LOG_MODE_INVALID = -1, + LOG_MODE_BACKUP_AND_TRUNCATE, + LOG_MODE_APPEND +} LogMode; + +int log_file_open (const gchar *log_filename, LogMode log_mode); + +#endif /* LOG_FILE_H_ */ diff --git a/src/process.c b/src/process.c index 4328dcd0..f4a4d572 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 1fcde7a8..e6e97052 100644 --- a/src/process.h +++ b/src/process.h @@ -14,6 +14,8 @@ #include +#include "log-file.h" + G_BEGIN_DECLS #define PROCESS_TYPE (process_get_type()) @@ -45,7 +47,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 0fd5be31..c7ed63c9 100644 --- a/src/seat.c +++ b/src/seat.c @@ -475,13 +475,15 @@ start_session (Seat *seat, Session *session) if (IS_GREETER (session)) { gchar *log_dir, *filename, *log_filename; + gboolean backup_logs; log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory"); filename = g_strdup_printf ("%s-greeter.log", display_server_get_name (session_get_display_server (session))); log_filename = g_build_filename (log_dir, filename, NULL); g_free (log_dir); g_free (filename); - session_set_log_file (session, log_filename); + backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); + session_set_log_file (session, log_filename, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND); g_free (log_filename); } diff --git a/src/session-child.c b/src/session-child.c index f673b395..9915e418 100644 --- a/src/session-child.c +++ b/src/session-child.c @@ -26,6 +26,7 @@ #include "session.h" #include "console-kit.h" #include "login1.h" +#include "log-file.h" #include "privileges.h" #include "x-authority.h" #include "configuration.h" @@ -257,7 +258,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 +490,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 +526,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 +682,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 4082deff..e874976d 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; @@ -199,11 +200,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 @@ -626,7 +628,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 e820e137..532ecbb2 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-file.h" G_BEGIN_DECLS @@ -80,7 +81,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 346e423e..fb14a84b 100644 --- a/src/unity-system-compositor.c +++ b/src/unity-system-compositor.c @@ -368,7 +368,7 @@ static gboolean unity_system_compositor_start (DisplayServer *server) { UnitySystemCompositor *compositor = UNITY_SYSTEM_COMPOSITOR (server); - gboolean result; + gboolean result, backup_logs; GString *command; gchar *dir, *log_file, *absolute_command, *value; @@ -401,7 +401,8 @@ 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); + backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); + process_set_log_file (compositor->priv->process, log_file, TRUE, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : 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 390f53d1..2474dff1 100644 --- a/src/x-server-local.c +++ b/src/x-server-local.c @@ -484,7 +484,7 @@ static gboolean x_server_local_start (DisplayServer *display_server) { XServerLocal *server = X_SERVER_LOCAL (display_server); - gboolean result; + gboolean result, backup_logs; gchar *filename, *dir, *log_file, *absolute_command; GString *command; @@ -503,7 +503,8 @@ 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); + backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); + process_set_log_file (server->priv->x_server_process, log_file, TRUE, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : 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 884489dd..f44c639e 100644 --- a/src/x-server-xvnc.c +++ b/src/x-server-xvnc.c @@ -183,7 +183,7 @@ x_server_xvnc_start (DisplayServer *display_server) { XServerXVNC *server = X_SERVER_XVNC (display_server); XAuthority *authority; - gboolean result; + gboolean result, backup_logs; gchar *filename, *run_dir, *dir, *log_file, *absolute_command; GString *command; gchar hostname[1024], *number; @@ -202,7 +202,8 @@ 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); + backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs"); + process_set_log_file (server->priv->x_server_process, log_file, FALSE, backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND); l_debug (display_server, "Logging to %s", log_file); g_free (log_file); g_free (filename); -- cgit v1.2.1