[ConsoleKit] [PATCH] Refactor ck-log-system-* tools to eliminate copy&paste.

Paul P. Komkoff Jr i at stingr.net
Wed Mar 3 06:07:03 PST 2010


Added the ck-log-system-common.c which is a library with single entry
point log_system_event.
Modified the existing ck-log-system-{start,stop,restart}.c to use
this library.
Added (as noinst_) ck-log-system-main.c - a multicall binary to replace
existing ones in a future.
---
 tools/Makefile.am             |   21 ++++
 tools/ck-log-system-common.c  |  206 +++++++++++++++++++++++++++++++++++++++++
 tools/ck-log-system-main.c    |   83 +++++++++++++++++
 tools/ck-log-system-restart.c |  129 +-------------------------
 tools/ck-log-system-start.c   |  160 +-------------------------------
 tools/ck-log-system-stop.c    |  129 +-------------------------
 6 files changed, 316 insertions(+), 412 deletions(-)
 create mode 100644 tools/ck-log-system-common.c
 create mode 100644 tools/ck-log-system-main.c

diff --git a/tools/Makefile.am b/tools/Makefile.am
index 13c191f..c44b5f7 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -56,6 +56,10 @@ sbin_PROGRAMS = \
 	ck-log-system-stop		\
 	$(NULL)
 
+noinst_PROGRAMS = \
+	ck-log-system-main		\
+	$(NULL)
+
 ck_launch_session_SOURCES =		\
 	ck-launch-session.c		\
 	$(NULL)
@@ -83,12 +87,27 @@ ck_history_LDADD =			\
 	$(top_builddir)/src/libck-event-log.la	\
 	$(NULL)
 
+noinst_LIBRARIES = libck_log_system_common.a
+
+libck_log_system_common_a_SOURCES = ck-log-system-common.c
+
+ck_log_system_main_SOURCES =		\
+	ck-log-system-main.c		\
+	$(NULL)
+
+ck_log_system_main_LDADD =		\
+	$(HISTORY_LIBS)			\
+	libck_log_system_common.a	\
+	$(top_builddir)/src/libck-event-log.la	\
+	$(NULL)
+
 ck_log_system_start_SOURCES =		\
 	ck-log-system-start.c		\
 	$(NULL)
 
 ck_log_system_start_LDADD =		\
 	$(HISTORY_LIBS)			\
+	libck_log_system_common.a	\
 	$(top_builddir)/src/libck-event-log.la	\
 	$(NULL)
 
@@ -98,6 +117,7 @@ ck_log_system_restart_SOURCES =		\
 
 ck_log_system_restart_LDADD =		\
 	$(HISTORY_LIBS)			\
+	libck_log_system_common.a	\
 	$(top_builddir)/src/libck-event-log.la	\
 	$(NULL)
 
@@ -107,6 +127,7 @@ ck_log_system_stop_SOURCES =		\
 
 ck_log_system_stop_LDADD =		\
 	$(HISTORY_LIBS)			\
+	libck_log_system_common.a	\
 	$(top_builddir)/src/libck-event-log.la	\
 	$(NULL)
 
diff --git a/tools/ck-log-system-common.c b/tools/ck-log-system-common.c
new file mode 100644
index 0000000..28e86cb
--- /dev/null
+++ b/tools/ck-log-system-common.c
@@ -0,0 +1,206 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 William Jon McCann <jmccann at redhat.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 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/utsname.h>
+#include <string.h>
+
+#include <locale.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+
+#include "ck-log-event.h"
+
+#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/log/ConsoleKit/history"
+#define LINUX_KERNEL_CMDLINE "/proc/cmdline"
+
+
+/* Adapted from auditd auditd-event.c */
+static gboolean
+open_log_file (const char *filename,
+               int        *fdp,
+               FILE      **filep)
+{
+        int      flags;
+        int      fd;
+        FILE    *file;
+        gboolean ret;
+
+        ret = FALSE;
+
+        /*
+         * Likely errors on rotate: ENFILE, ENOMEM, ENOSPC
+         */
+        flags = O_WRONLY | O_APPEND;
+#ifdef O_NOFOLLOW
+        flags |= O_NOFOLLOW;
+#endif
+
+retry:
+        fd = g_open (filename, flags, 0600);
+        if (fd < 0) {
+                if (errno == ENOENT) {
+                        /* FIXME: should we just skip if file doesn't exist? */
+                        fd = g_open (filename,
+                                     O_CREAT | O_EXCL | O_APPEND,
+                                     S_IRUSR | S_IWUSR | S_IRGRP);
+                        if (fd < 0) {
+                                g_warning ("Couldn't create log file %s (%s)",
+                                           filename,
+                                           g_strerror (errno));
+                                goto out;
+                        }
+
+                        close (fd);
+                        fd = g_open (filename, flags, 0600);
+                } else if (errno == ENFILE) {
+                        /* All system descriptors used, try again... */
+                        goto retry;
+                }
+                if (fd < 0) {
+                        g_warning ("Couldn't open log file %s (%s)",
+                                   filename,
+                                   g_strerror (errno));
+                        goto out;
+                }
+        }
+
+        if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
+                close (fd);
+                g_warning ("Error setting log file CLOEXEC flag (%s)",
+                           g_strerror (errno));
+                goto out;
+        }
+
+        fchown (fd, 0, 0);
+
+        file = fdopen (fd, "a");
+        if (file == NULL) {
+                g_warning ("Error setting up log descriptor (%s)",
+                           g_strerror (errno));
+                close (fd);
+                goto out;
+        }
+
+        /* Set it to line buffering */
+        setlinebuf (file);
+
+        ret = TRUE;
+
+        if (fdp != NULL) {
+                *fdp = fd;
+        }
+        if (filep != NULL) {
+                *filep = file;
+        }
+
+ out:
+
+        return ret;
+}
+
+static gboolean
+write_log_for_event (CkLogEvent *event)
+{
+        GString *str;
+        FILE    *file;
+        int      fd;
+
+        str = g_string_new (NULL);
+
+        ck_log_event_to_string (event, str);
+
+        if (! open_log_file (DEFAULT_LOG_FILENAME, &fd, &file)) {
+                exit (1);
+        }
+
+        if (file != NULL) {
+                int rc;
+
+                rc = fprintf (file, "%s\n", str->str);
+                if (rc <= 0) {
+                        g_warning ("Record was not written to disk (%s)",
+                                   g_strerror (errno));
+                }
+        } else {
+                g_warning ("Log file not open for writing");
+        }
+
+        g_string_free (str, TRUE);
+
+        return TRUE;
+}
+
+static char *
+get_boot_arguments (void)
+{
+        char *contents;
+        gboolean res;
+
+        contents = NULL;
+        res = g_file_get_contents (LINUX_KERNEL_CMDLINE,
+                                   &contents,
+                                   NULL,
+                                   NULL);
+        if (!res) {
+                g_free (contents);
+                contents = NULL;
+        } else {
+                g_strchomp (contents);
+        }
+
+        return contents;
+}
+
+static void
+get_startup_event_info(CkLogEvent *event) {
+        CkLogSystemStartEvent *e = (CkLogSystemStartEvent *) event;
+        struct utsname uts = { 0 };
+
+        e = (CkLogSystemStartEvent *) &event;
+
+        if (uname (&uts) == 0)
+                e->kernel_release = uts.release;
+
+        e->boot_arguments = get_boot_arguments();
+}
+
+void log_system_event(CkLogEventType event_type) {
+  CkLogEvent event = { 0 };
+
+  event.type = event_type;
+  g_get_current_time (&event.timestamp);
+
+  if (event.type == CK_LOG_EVENT_SYSTEM_START)
+    get_startup_event_info(&event);
+
+  write_log_for_event (&event);
+}
diff --git a/tools/ck-log-system-main.c b/tools/ck-log-system-main.c
new file mode 100644
index 0000000..3fa3d86
--- /dev/null
+++ b/tools/ck-log-system-main.c
@@ -0,0 +1,83 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 William Jon McCann <jmccann at redhat.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 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/utsname.h>
+#include <string.h>
+
+#include <locale.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+
+#include "ck-log-event.h"
+
+void log_system_event(CkLogEventType event_type);
+
+struct namemap {
+  const char * const name;
+  const CkLogEventType event_type;
+};
+
+const struct namemap map[] = {
+  { "ck-log-system-start", CK_LOG_EVENT_SYSTEM_START },
+  { "ck-log-system-stop", CK_LOG_EVENT_SYSTEM_STOP },
+  { "ck-log-system-restart", CK_LOG_EVENT_SYSTEM_RESTART },
+  { 0 }};
+
+static CkLogEventType find_event_type(const struct namemap * const map, const char * const name) {
+  int i;
+
+  for (i = 0; map[i].name != NULL; i++)
+    if (strcmp(name, map[i].name) == 0)
+      return map[i].event_type;
+
+  return CK_LOG_EVENT_NONE;
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+  CkLogEventType event_type = CK_LOG_EVENT_NONE;
+
+  if (argc > 0)
+    event_type = find_event_type(map, basename(argv[0]));
+
+  if ((event_type == CK_LOG_EVENT_NONE) && (argc > 1))
+    event_type = find_event_type(map, argv[1]);
+
+  if (event_type == CK_LOG_EVENT_NONE)
+    return 1;
+
+  log_system_event(event_type);
+
+  return 0;
+}
diff --git a/tools/ck-log-system-restart.c b/tools/ck-log-system-restart.c
index e53670f..db9f142 100644
--- a/tools/ck-log-system-restart.c
+++ b/tools/ck-log-system-restart.c
@@ -38,137 +38,12 @@
 
 #include "ck-log-event.h"
 
-#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/log/ConsoleKit/history"
-
-
-/* Adapted from auditd auditd-event.c */
-static gboolean
-open_log_file (const char *filename,
-               int        *fdp,
-               FILE      **filep)
-{
-        int      flags;
-        int      fd;
-        FILE    *file;
-        gboolean ret;
-
-        ret = FALSE;
-
-        /*
-         * Likely errors on rotate: ENFILE, ENOMEM, ENOSPC
-         */
-        flags = O_WRONLY | O_APPEND;
-#ifdef O_NOFOLLOW
-        flags |= O_NOFOLLOW;
-#endif
-
-retry:
-        fd = g_open (filename, flags, 0600);
-        if (fd < 0) {
-                if (errno == ENOENT) {
-                        /* FIXME: should we just skip if file doesn't exist? */
-                        fd = g_open (filename,
-                                     O_CREAT | O_EXCL | O_APPEND,
-                                     S_IRUSR | S_IWUSR | S_IRGRP);
-                        if (fd < 0) {
-                                g_warning ("Couldn't create log file %s (%s)",
-                                           filename,
-                                           g_strerror (errno));
-                                goto out;
-                        }
-
-                        close (fd);
-                        fd = g_open (filename, flags, 0600);
-                } else if (errno == ENFILE) {
-                        /* All system descriptors used, try again... */
-                        goto retry;
-                }
-                if (fd < 0) {
-                        g_warning ("Couldn't open log file %s (%s)",
-                                   filename,
-                                   g_strerror (errno));
-                        goto out;
-                }
-        }
-
-        if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
-                close (fd);
-                g_warning ("Error setting log file CLOEXEC flag (%s)",
-                           g_strerror (errno));
-                goto out;
-        }
-
-        fchown (fd, 0, 0);
-
-        file = fdopen (fd, "a");
-        if (file == NULL) {
-                g_warning ("Error setting up log descriptor (%s)",
-                           g_strerror (errno));
-                close (fd);
-                goto out;
-        }
-
-        /* Set it to line buffering */
-        setlinebuf (file);
-
-        ret = TRUE;
-
-        if (fdp != NULL) {
-                *fdp = fd;
-        }
-        if (filep != NULL) {
-                *filep = file;
-        }
-
- out:
-
-        return ret;
-}
-
-static gboolean
-write_log_for_event (CkLogEvent *event)
-{
-        GString *str;
-        FILE    *file;
-        int      fd;
-
-        str = g_string_new (NULL);
-
-        ck_log_event_to_string (event, str);
-
-        if (! open_log_file (DEFAULT_LOG_FILENAME, &fd, &file)) {
-                exit (1);
-        }
-
-        if (file != NULL) {
-                int rc;
-
-                rc = fprintf (file, "%s\n", str->str);
-                if (rc <= 0) {
-                        g_warning ("Record was not written to disk (%s)",
-                                   g_strerror (errno));
-                }
-        } else {
-                g_warning ("Log file not open for writing");
-        }
-
-        g_string_free (str, TRUE);
-
-        return TRUE;
-}
+void log_system_event(CkLogEventType event_type);
 
 int
 main (int    argc,
       char **argv)
 {
-        CkLogEvent event;
-
-        memset (&event, 0, sizeof (CkLogEvent));
-
-        event.type = CK_LOG_EVENT_SYSTEM_RESTART;
-        g_get_current_time (&event.timestamp);
-
-        write_log_for_event (&event);
-
+        log_system_event(CK_LOG_EVENT_SYSTEM_RESTART);
         return 0;
 }
diff --git a/tools/ck-log-system-start.c b/tools/ck-log-system-start.c
index 0860429..9c3de4e 100644
--- a/tools/ck-log-system-start.c
+++ b/tools/ck-log-system-start.c
@@ -39,168 +39,12 @@
 
 #include "ck-log-event.h"
 
-#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/log/ConsoleKit/history"
-
-#define LINUX_KERNEL_CMDLINE "/proc/cmdline"
-
-/* Adapted from auditd auditd-event.c */
-static gboolean
-open_log_file (const char *filename,
-               int        *fdp,
-               FILE      **filep)
-{
-        int      flags;
-        int      fd;
-        FILE    *file;
-        gboolean ret;
-
-        ret = FALSE;
-
-        /*
-         * Likely errors on rotate: ENFILE, ENOMEM, ENOSPC
-         */
-        flags = O_WRONLY | O_APPEND;
-#ifdef O_NOFOLLOW
-        flags |= O_NOFOLLOW;
-#endif
-
-retry:
-        fd = g_open (filename, flags, 0600);
-        if (fd < 0) {
-                if (errno == ENOENT) {
-                        /* FIXME: should we just skip if file doesn't exist? */
-                        fd = g_open (filename,
-                                     O_CREAT | O_EXCL | O_APPEND,
-                                     S_IRUSR | S_IWUSR | S_IRGRP);
-                        if (fd < 0) {
-                                g_warning ("Couldn't create log file %s (%s)",
-                                           filename,
-                                           g_strerror (errno));
-                                goto out;
-                        }
-
-                        close (fd);
-                        fd = g_open (filename, flags, 0600);
-                } else if (errno == ENFILE) {
-                        /* All system descriptors used, try again... */
-                        goto retry;
-                }
-                if (fd < 0) {
-                        g_warning ("Couldn't open log file %s (%s)",
-                                   filename,
-                                   g_strerror (errno));
-                        goto out;
-                }
-        }
-
-        if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
-                close (fd);
-                g_warning ("Error setting log file CLOEXEC flag (%s)",
-                           g_strerror (errno));
-                goto out;
-        }
-
-        fchown (fd, 0, 0);
-
-        file = fdopen (fd, "a");
-        if (file == NULL) {
-                g_warning ("Error setting up log descriptor (%s)",
-                           g_strerror (errno));
-                close (fd);
-                goto out;
-        }
-
-        /* Set it to line buffering */
-        setlinebuf (file);
-
-        ret = TRUE;
-
-        if (fdp != NULL) {
-                *fdp = fd;
-        }
-        if (filep != NULL) {
-                *filep = file;
-        }
-
- out:
-
-        return ret;
-}
-
-static gboolean
-write_log_for_event (CkLogEvent *event)
-{
-        GString *str;
-        FILE    *file;
-        int      fd;
-
-        str = g_string_new (NULL);
-
-        ck_log_event_to_string (event, str);
-
-        if (! open_log_file (DEFAULT_LOG_FILENAME, &fd, &file)) {
-                exit (1);
-        }
-
-        if (file != NULL) {
-                int rc;
-
-                rc = fprintf (file, "%s\n", str->str);
-                if (rc <= 0) {
-                        g_warning ("Record was not written to disk (%s)",
-                                   g_strerror (errno));
-                }
-        } else {
-                g_warning ("Log file not open for writing");
-        }
-
-        g_string_free (str, TRUE);
-
-        return TRUE;
-}
-
-static char *
-get_boot_arguments (void)
-{
-        char *contents;
-        gboolean res;
-
-        contents = NULL;
-        res = g_file_get_contents (LINUX_KERNEL_CMDLINE,
-                                   &contents,
-                                   NULL,
-                                   NULL);
-        if (!res) {
-                g_free (contents);
-                contents = NULL;
-        } else {
-                g_strchomp (contents);
-        }
-
-        return contents;
-}
+void log_system_event(CkLogEventType event_type);
 
 int
 main (int    argc,
       char **argv)
 {
-        CkLogEvent event;
-        CkLogSystemStartEvent *e;
-        struct utsname uts;
-
-        memset (&event, 0, sizeof (CkLogEvent));
-
-        event.type = CK_LOG_EVENT_SYSTEM_START;
-        g_get_current_time (&event.timestamp);
-        e = (CkLogSystemStartEvent *) &event;
-
-        if (uname (&uts) == 0) {
-                e->kernel_release = uts.release;
-        }
-
-        e->boot_arguments = get_boot_arguments ();
-
-        write_log_for_event (&event);
-
+        log_system_event(CK_LOG_EVENT_SYSTEM_START);
         return 0;
 }
diff --git a/tools/ck-log-system-stop.c b/tools/ck-log-system-stop.c
index a59a1e9..ebd85e8 100644
--- a/tools/ck-log-system-stop.c
+++ b/tools/ck-log-system-stop.c
@@ -38,137 +38,12 @@
 
 #include "ck-log-event.h"
 
-#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/log/ConsoleKit/history"
-
-
-/* Adapted from auditd auditd-event.c */
-static gboolean
-open_log_file (const char *filename,
-               int        *fdp,
-               FILE      **filep)
-{
-        int      flags;
-        int      fd;
-        FILE    *file;
-        gboolean ret;
-
-        ret = FALSE;
-
-        /*
-         * Likely errors on rotate: ENFILE, ENOMEM, ENOSPC
-         */
-        flags = O_WRONLY | O_APPEND;
-#ifdef O_NOFOLLOW
-        flags |= O_NOFOLLOW;
-#endif
-
-retry:
-        fd = g_open (filename, flags, 0600);
-        if (fd < 0) {
-                if (errno == ENOENT) {
-                        /* FIXME: should we just skip if file doesn't exist? */
-                        fd = g_open (filename,
-                                     O_CREAT | O_EXCL | O_APPEND,
-                                     S_IRUSR | S_IWUSR | S_IRGRP);
-                        if (fd < 0) {
-                                g_warning ("Couldn't create log file %s (%s)",
-                                           filename,
-                                           g_strerror (errno));
-                                goto out;
-                        }
-
-                        close (fd);
-                        fd = g_open (filename, flags, 0600);
-                } else if (errno == ENFILE) {
-                        /* All system descriptors used, try again... */
-                        goto retry;
-                }
-                if (fd < 0) {
-                        g_warning ("Couldn't open log file %s (%s)",
-                                   filename,
-                                   g_strerror (errno));
-                        goto out;
-                }
-        }
-
-        if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
-                close (fd);
-                g_warning ("Error setting log file CLOEXEC flag (%s)",
-                           g_strerror (errno));
-                goto out;
-        }
-
-        fchown (fd, 0, 0);
-
-        file = fdopen (fd, "a");
-        if (file == NULL) {
-                g_warning ("Error setting up log descriptor (%s)",
-                           g_strerror (errno));
-                close (fd);
-                goto out;
-        }
-
-        /* Set it to line buffering */
-        setlinebuf (file);
-
-        ret = TRUE;
-
-        if (fdp != NULL) {
-                *fdp = fd;
-        }
-        if (filep != NULL) {
-                *filep = file;
-        }
-
- out:
-
-        return ret;
-}
-
-static gboolean
-write_log_for_event (CkLogEvent *event)
-{
-        GString *str;
-        FILE    *file;
-        int      fd;
-
-        str = g_string_new (NULL);
-
-        ck_log_event_to_string (event, str);
-
-        if (! open_log_file (DEFAULT_LOG_FILENAME, &fd, &file)) {
-                exit (1);
-        }
-
-        if (file != NULL) {
-                int rc;
-
-                rc = fprintf (file, "%s\n", str->str);
-                if (rc <= 0) {
-                        g_warning ("Record was not written to disk (%s)",
-                                   g_strerror (errno));
-                }
-        } else {
-                g_warning ("Log file not open for writing");
-        }
-
-        g_string_free (str, TRUE);
-
-        return TRUE;
-}
+void log_system_event(CkLogEventType event_type);
 
 int
 main (int    argc,
       char **argv)
 {
-        CkLogEvent event;
-
-        memset (&event, 0, sizeof (CkLogEvent));
-
-        event.type = CK_LOG_EVENT_SYSTEM_STOP;
-        g_get_current_time (&event.timestamp);
-
-        write_log_for_event (&event);
-
+        log_system_event(CK_LOG_EVENT_SYSTEM_STOP);
         return 0;
 }
-- 
1.7.0



More information about the ConsoleKit mailing list