From 9e78825cef3db4e13cffbfd024bda365403aa6ba Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Mon, 25 Feb 2008 19:19:31 -0500 Subject: always create the directories we need --- src/ck-event-logger.c | 22 +++++++++++++++--- src/ck-manager.c | 63 +++++++++++++++++++++++++++++++-------------------- src/main.c | 50 +++++++++++++++++++++++++++++----------- 3 files changed, 95 insertions(+), 40 deletions(-) diff --git a/src/ck-event-logger.c b/src/ck-event-logger.c index b821df9..2fded87 100644 --- a/src/ck-event-logger.c +++ b/src/ck-event-logger.c @@ -97,8 +97,10 @@ ck_event_logger_queue_event (CkEventLogger *event_logger, static gboolean open_log_file (CkEventLogger *event_logger) { - int flags; - int fd; + int flags; + int fd; + int res; + char *dirname; /* * Likely errors on rotate: ENFILE, ENOMEM, ENOSPC @@ -108,6 +110,20 @@ open_log_file (CkEventLogger *event_logger) flags |= O_NOFOLLOW; #endif + dirname = g_path_get_dirname (event_logger->priv->log_filename); + /* always make sure we have a directory */ + errno = 0; + res = g_mkdir_with_parents (dirname, + S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + if (res < 0) { + g_warning ("Unable to create directory %s (%s)", + dirname, + g_strerror (errno)); + g_free (dirname); + return FALSE; + } + g_free (dirname); + retry: errno = 0; fd = g_open (event_logger->priv->log_filename, flags, 0600); @@ -115,7 +131,7 @@ retry: if (errno == ENOENT) { fd = g_open (event_logger->priv->log_filename, O_CREAT | O_EXCL | O_APPEND, - S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP | S_IROTH); + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { g_warning ("Couldn't create log file %s (%s)", event_logger->priv->log_filename, diff --git a/src/ck-manager.c b/src/ck-manager.c index 6775e20..bf4926f 100644 --- a/src/ck-manager.c +++ b/src/ck-manager.c @@ -182,41 +182,56 @@ out: static void ck_manager_dump (CkManager *manager) { + int fd; + int res; const char *filename = LOCALSTATEDIR "/run/ConsoleKit/database"; const char *filename_tmp = LOCALSTATEDIR "/run/ConsoleKit/database~"; - if (manager != NULL) { - int fd; - fd = g_open (filename_tmp, O_CREAT | O_WRONLY, 0600); - if (fd == -1) { - g_warning ("Cannot create file %s: %s", filename_tmp, g_strerror (errno)); - goto error; - } + if (manager == NULL) { + return; + } - if (! do_dump (manager, fd)) { - g_warning ("Cannot write to file %s", filename_tmp); - close (fd); - goto error; - } - again: - if (close (fd) != 0) { - if (errno == EINTR) - goto again; - else { - g_warning ("Cannot close fd for %s: %s", filename_tmp, g_strerror (errno)); - goto error; - } - } + /* always make sure we have a directory */ + errno = 0; + res = g_mkdir_with_parents (LOCALSTATEDIR "/run/ConsoleKit", + S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + if (res < 0) { + g_warning ("Unable to create directory %s (%s)", + LOCALSTATEDIR "/run/ConsoleKit", + g_strerror (errno)); + return; + } + + fd = g_open (filename_tmp, O_CREAT | O_WRONLY, 0600); + if (fd == -1) { + g_warning ("Cannot create file %s: %s", filename_tmp, g_strerror (errno)); + goto error; + } - if (g_rename (filename_tmp, filename) != 0) { - g_warning ("Cannot rename %s to %s: %s", filename_tmp, filename, g_strerror (errno)); + if (! do_dump (manager, fd)) { + g_warning ("Cannot write to file %s", filename_tmp); + close (fd); + goto error; + } + again: + if (close (fd) != 0) { + if (errno == EINTR) + goto again; + else { + g_warning ("Cannot close fd for %s: %s", filename_tmp, g_strerror (errno)); goto error; } } + if (g_rename (filename_tmp, filename) != 0) { + g_warning ("Cannot rename %s to %s: %s", filename_tmp, filename, g_strerror (errno)); + goto error; + } + return; error: - /* For security reasons; unlink the existing file since it contains outdated information */ + /* For security reasons; unlink the existing file since it + contains outdated information */ if (g_unlink (filename) != 0) { g_warning ("Cannot unlink %s: %s", filename, g_strerror (errno)); } diff --git a/src/main.c b/src/main.c index 50698ed..adc0d71 100644 --- a/src/main.c +++ b/src/main.c @@ -252,6 +252,42 @@ setup_debug_log (gboolean debug) setup_debug_log_signals (); } +static void +create_pid_file (void) +{ + char *dirname; + int res; + int pf; + ssize_t written; + char pid[9]; + + /* remove old pid file */ + unlink (CONSOLE_KIT_PID_FILE); + + dirname = g_path_get_dirname (CONSOLE_KIT_PID_FILE); + errno = 0; + res = g_mkdir_with_parents (dirname, + S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + if (res < 0) { + g_warning ("Unable to create directory %s (%s)", + dirname, + g_strerror (errno)); + } + g_free (dirname); + + /* make a new pid file */ + if ((pf = open (CONSOLE_KIT_PID_FILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) > 0) { + snprintf (pid, sizeof (pid), "%lu\n", (long unsigned) getpid ()); + written = write (pf, pid, strlen (pid)); + close (pf); + g_atexit (delete_pid); + } else { + g_warning ("Unable to write pid file %s: %s", + CONSOLE_KIT_PID_FILE, + g_strerror (errno)); + } +} + int main (int argc, char **argv) @@ -263,9 +299,6 @@ main (int argc, DBusGConnection *connection; GError *error; int ret; - int pf; - ssize_t written; - char pid[9]; gboolean res; static gboolean debug = FALSE; static gboolean no_daemon = FALSE; @@ -330,16 +363,7 @@ main (int argc, g_debug ("initializing console-kit-daemon %s", VERSION); - /* remove old pid file */ - unlink (CONSOLE_KIT_PID_FILE); - - /* make a new pid file */ - if ((pf = open (CONSOLE_KIT_PID_FILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) > 0) { - snprintf (pid, sizeof (pid), "%lu\n", (long unsigned) getpid ()); - written = write (pf, pid, strlen (pid)); - close (pf); - g_atexit (delete_pid); - } + create_pid_file (); manager = ck_manager_new (); -- cgit