diff options
author | Matthias Clasen <mclasen@redhat.com> | 2010-03-25 14:10:32 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2010-03-25 14:10:32 -0400 |
commit | f9a5229aa7f1eae3a0481a41e88d7edbc15a649c (patch) | |
tree | c22015f618fc997cad0777d98086bd2241d9d242 | |
parent | 18778b2ad5e0d1fc412600b0f2481dc957359375 (diff) |
Refactor logging into separate files
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/daemon.c | 131 | ||||
-rw-r--r-- | src/daemon.h | 5 | ||||
-rw-r--r-- | src/main.c | 7 | ||||
-rw-r--r-- | src/user.c | 64 | ||||
-rw-r--r-- | src/util.c | 144 | ||||
-rw-r--r-- | src/util.h | 36 |
7 files changed, 226 insertions, 163 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index e6b60ca..f429de8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,6 +27,8 @@ accounts_daemon_SOURCES = \ daemon.c \ user.h \ user.c \ + util.h \ + util.c \ main.c accounts_daemon_LDADD = \ diff --git a/src/daemon.c b/src/daemon.c index 9dd4d7b..50ae3b9 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -24,11 +24,12 @@ #include <stdlib.h> #include <stdio.h> #include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include <sys/wait.h> #include <pwd.h> #include <unistd.h> #include <errno.h> -#include <syslog.h> #include <glib.h> #include <glib/gi18n.h> @@ -41,6 +42,7 @@ #include "daemon.h" #include "daemon-glue.h" +#include "util.h" #define PATH_PASSWD "/etc/passwd" #define PATH_SHADOW "/etc/shadow" @@ -792,11 +794,6 @@ daemon_new (void) goto error; } - openlog ("accounts-daemon", LOG_PID, LOG_DAEMON); - syslog (LOG_INFO, "started daemon version %s", VERSION); - closelog (); - openlog ("accounts-daemon", 0, LOG_AUTHPRIV); - return daemon; error: @@ -1035,9 +1032,7 @@ daemon_create_user_authorized_cb (Daemon *daemon, return; } - daemon_local_log (daemon, context, - "create user '%s'", - cd->user_name); + sys_log (context, "create user '%s'", cd->user_name); argv[0] = "/usr/sbin/useradd"; argv[1] = "-m"; @@ -1140,9 +1135,7 @@ daemon_delete_user_authorized_cb (Daemon *daemon, return; } - daemon_local_log (daemon, context, - "delete user '%s' (%d)", - pwent->pw_name, ud->uid); + sys_log (context, "delete user '%s' (%d)", pwent->pw_name, ud->uid); argv[0] = "/usr/sbin/userdel"; if (ud->remove_files) { @@ -1425,117 +1418,3 @@ daemon_local_set_automatic_login (Daemon *daemon, return TRUE; } -static gchar * -_polkit_subject_get_cmdline (PolkitSubject *subject, gint *pid, gint *uid) -{ - PolkitSubject *process; - gchar *ret; - gchar *filename; - gchar *contents; - gsize contents_len; - GError *error; - guint n; - - g_return_val_if_fail (subject != NULL, NULL); - - error = NULL; - - ret = NULL; - process = NULL; - filename = NULL; - contents = NULL; - - if (POLKIT_IS_UNIX_PROCESS (subject)) - { - process = g_object_ref (subject); - } - else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) - { - process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), - NULL, - &error); - if (process == NULL) - { - g_warning ("Error getting process for system bus name `%s': %s", - polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)), - error->message); - g_error_free (error); - goto out; - } - } - else - { - g_warning ("Unknown subject type passed to guess_program_name()"); - goto out; - } - - *pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process)); - *uid = polkit_unix_process_get_owner (POLKIT_UNIX_PROCESS (process), NULL); - - filename = g_strdup_printf ("/proc/%d/cmdline", *pid); - - if (!g_file_get_contents (filename, - &contents, - &contents_len, - &error)) - { - g_warning ("Error openeing `%s': %s", - filename, - error->message); - g_error_free (error); - goto out; - } - /* The kernel uses '\0' to separate arguments - replace those with a space. */ - for (n = 0; n < contents_len - 1; n++) - { - if (contents[n] == '\0') - contents[n] = ' '; - } - - ret = g_strdup (contents); - g_strstrip (ret); - - out: - g_free (filename); - g_free (contents); - if (process != NULL) - g_object_unref (process); - return ret; -} - -void daemon_local_log (Daemon *daemon, - DBusGMethodInvocation *context, - const gchar *format, - ...) -{ - va_list args; - gchar *real_format; - gint pid; - gint uid; - - if (context) { - PolkitSubject *subject; - gchar *cmdline; - gchar *id; - - subject = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); - id = polkit_subject_to_string (subject); - cmdline = _polkit_subject_get_cmdline (subject, &pid, &uid); - if (cmdline == NULL) { - cmdline = g_strdup ("<unknown>"); - } - - real_format = g_strdup_printf ("request by %s [%s pid:%d uid:%d]: %s", id, cmdline, pid, uid, format); - - g_free (id); - g_free (cmdline); - g_object_unref (subject); - } - - va_start (args, format); - vsyslog (LOG_NOTICE, real_format, args); - va_end (args); - - g_free (real_format); -} - diff --git a/src/daemon.h b/src/daemon.h index d1d1138..ff90de3 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -95,11 +95,6 @@ gboolean daemon_local_set_automatic_login (Daemon *daemon, gboolean enabled, GError **error); -void daemon_local_log (Daemon *daemon, - DBusGMethodInvocation *context, - const gchar *format, - ...); - /* exported methods */ gboolean daemon_find_user_by_id (Daemon *daemon, @@ -22,8 +22,10 @@ #include "config.h" #include <stdlib.h> +#include <stdarg.h> #include <locale.h> #include <libintl.h> +#include <syslog.h> #include <glib.h> #include <glib/gi18n.h> @@ -183,6 +185,11 @@ main (int argc, char *argv[]) if (daemon == NULL) goto out; + openlog ("accounts-daemon", LOG_PID, LOG_DAEMON); + syslog (LOG_INFO, "started daemon version %s", VERSION); + closelog (); + openlog ("accounts-daemon", 0, LOG_AUTHPRIV); + loop = g_main_loop_new (NULL, FALSE); g_print ("entering main loop\n"); @@ -43,6 +43,7 @@ #include "daemon.h" #include "user.h" #include "user-glue.h" +#include "util.h" enum { PROP_0, @@ -773,9 +774,9 @@ user_change_real_name_authorized_cb (Daemon *daemon, gchar *argv[5]; if (g_strcmp0 (user->real_name, name) != 0) { - daemon_local_log (daemon, context, - "change real name of user '%s' (%d) to '%s'", - user->user_name, user->uid, name); + sys_log (context, + "change real name of user '%s' (%d) to '%s'", + user->user_name, user->uid, name); argv[0] = "/usr/sbin/usermod"; argv[1] = "-c"; @@ -870,9 +871,9 @@ user_change_user_name_authorized_cb (Daemon *daemon, if (g_strcmp0 (user->user_name, name) != 0) { old_name = g_strdup (user->user_name); - daemon_local_log (daemon, context, - "change name of user '%s' (%d) to '%s'", - old_name, user->uid, name); + sys_log (context, + "change name of user '%s' (%d) to '%s'", + old_name, user->uid, name); argv[0] = "/usr/sbin/usermod"; argv[1] = "-l"; @@ -1158,9 +1159,9 @@ user_change_home_dir_authorized_cb (Daemon *daemon, gchar *argv[6]; if (g_strcmp0 (user->home_dir, home_dir) != 0) { - daemon_local_log (daemon, context, - "change home directory of user '%s' (%d) to '%s'", - user->user_name, user->uid, home_dir); + sys_log (context, + "change home directory of user '%s' (%d) to '%s'", + user->user_name, user->uid, home_dir); argv[0] = "/usr/sbin/usermod"; argv[1] = "-m"; @@ -1251,9 +1252,9 @@ user_change_shell_authorized_cb (Daemon *daemon, gchar *argv[5]; if (g_strcmp0 (user->shell, shell) != 0) { - daemon_local_log (daemon, context, - "change shell of user '%s' (%d) to '%s'", - user->user_name, user->uid, shell); + sys_log (context, + "change shell of user '%s' (%d) to '%s'", + user->user_name, user->uid, shell); argv[0] = "/usr/sbin/usermod"; argv[1] = "-s"; @@ -1536,9 +1537,9 @@ user_change_locked_authorized_cb (Daemon *daemon, gchar *argv[4]; if (user->locked != locked) { - daemon_local_log (daemon, context, - "%s account of user '%s' (%d)", - locked ? "locking" : "unlocking", user->user_name, user->uid); + sys_log (context, + "%s account of user '%s' (%d)", + locked ? "locking" : "unlocking", user->user_name, user->uid); argv[0] = "/usr/sbin/usermod"; argv[1] = locked ? "-L" : "-U"; argv[2] = user->user_name; @@ -1613,9 +1614,9 @@ user_change_account_type_authorized_cb (Daemon *daemon, gchar *argv[5]; if (user->account_type != account_type) { - daemon_local_log (daemon, context, - "change account type of user '%s' (%d) to %d", - user->user_name, user->uid, account_type); + sys_log (context, + "change account type of user '%s' (%d) to %d", + user->user_name, user->uid, account_type); grp = getgrnam ("desktop_user_r"); if (grp == NULL) { @@ -1730,11 +1731,11 @@ user_change_password_mode_authorized_cb (Daemon *daemon, gchar *argv[4]; if (user->password_mode != mode) { - daemon_local_log (daemon, context, - "change password mode of user '%s' (%d) to %d", - user->user_name, user->uid, mode); + sys_log (context, + "change password mode of user '%s' (%d) to %d", + user->user_name, user->uid, mode); - g_object_freeze_notify (user); + g_object_freeze_notify (G_OBJECT (user)); if (mode == PASSWORD_MODE_SET_AT_LOGIN || mode == PASSWORD_MODE_NONE) { @@ -1811,7 +1812,7 @@ user_change_password_mode_authorized_cb (Daemon *daemon, save_extra_data (user); - g_object_thaw_notify (user); + g_object_thaw_notify (G_OBJECT (user)); g_signal_emit (user, signals[CHANGED], 0); } @@ -1873,11 +1874,11 @@ user_change_password_authorized_cb (Daemon *daemon, gchar *std_out, *std_err; gchar *argv[5]; - daemon_local_log (daemon, context, - "set password and hint of user '%s' (%d)", - user->user_name, user->uid); + sys_log (context, + "set password and hint of user '%s' (%d)", + user->user_name, user->uid); - g_object_freeze_notify (user); + g_object_freeze_notify (G_OBJECT (user)); argv[0] = "/usr/sbin/usermod"; argv[1] = "-p"; @@ -1924,7 +1925,7 @@ user_change_password_authorized_cb (Daemon *daemon, save_extra_data (user); - g_object_thaw_notify (user); + g_object_thaw_notify (G_OBJECT (user)); g_signal_emit (user, signals[CHANGED], 0); @@ -1941,7 +1942,6 @@ user_set_password (User *user, DBusConnection *connection; DBusError dbus_error; uid_t uid; - const gchar *action_id; gchar **data; connection = dbus_g_connection_get_connection (user->system_bus_connection); @@ -1981,9 +1981,9 @@ user_change_automatic_login_authorized_cb (Daemon *daemon, gboolean enabled = GPOINTER_TO_INT (data); GError *error = NULL; - daemon_local_log (daemon, context, - "%s automatic login for user '%s' (%d)", - enabled ? "enable" : "disable", user->user_name, user->uid); + sys_log (context, + "%s automatic login for user '%s' (%d)", + enabled ? "enable" : "disable", user->user_name, user->uid); if (!daemon_local_set_automatic_login (daemon, user, enabled, &error)) { throw_error (context, ERROR_FAILED, "failed to change automatic login: %s", error->message); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..e12c687 --- /dev/null +++ b/src/util.c @@ -0,0 +1,144 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2009-2010 Red Hat, Inc. + * + * 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. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Written by: Matthias Clasen <mclasen@redhat.com> + */ + +#include "config.h" + +#include <syslog.h> +#include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-lowlevel.h> + +#include <polkit/polkit.h> + +#include "util.h" + + +static gchar * +_polkit_subject_get_cmdline (PolkitSubject *subject, gint *pid, gint *uid) +{ + PolkitSubject *process; + gchar *ret; + gchar *filename; + gchar *contents; + gsize contents_len; + GError *error; + guint n; + + g_return_val_if_fail (subject != NULL, NULL); + + error = NULL; + + ret = NULL; + process = NULL; + filename = NULL; + contents = NULL; + + if (POLKIT_IS_UNIX_PROCESS (subject)) + { + process = g_object_ref (subject); + } + else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) + { + process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), + NULL, + &error); + if (process == NULL) + { + g_warning ("Error getting process for system bus name `%s': %s", + polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject)), + error->message); + g_error_free (error); + goto out; + } + } + else + { + g_warning ("Unknown subject type passed to guess_program_name()"); + goto out; + } + + *pid = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process)); + *uid = polkit_unix_process_get_owner (POLKIT_UNIX_PROCESS (process), NULL); + + filename = g_strdup_printf ("/proc/%d/cmdline", *pid); + + if (!g_file_get_contents (filename, + &contents, + &contents_len, + &error)) + { + g_warning ("Error openeing `%s': %s", + filename, + error->message); + g_error_free (error); + goto out; + } + /* The kernel uses '\0' to separate arguments - replace those with a space. */ + for (n = 0; n < contents_len - 1; n++) + { + if (contents[n] == '\0') + contents[n] = ' '; + } + + ret = g_strdup (contents); + g_strstrip (ret); + + out: + g_free (filename); + g_free (contents); + if (process != NULL) + g_object_unref (process); + return ret; +} + +void +sys_log (DBusGMethodInvocation *context, + const gchar *format, + ...) +{ + va_list args; + gchar *real_format; + gint pid; + gint uid; + + if (context) { + PolkitSubject *subject; + gchar *cmdline; + gchar *id; + + subject = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); + id = polkit_subject_to_string (subject); + cmdline = _polkit_subject_get_cmdline (subject, &pid, &uid); + if (cmdline == NULL) { + cmdline = g_strdup ("<unknown>"); + } + real_format = g_strdup_printf ("request by %s [%s pid:%d uid:%d]: %s", id, cmdline, pid, uid, format); + + g_free (id); + g_free (cmdline); + g_object_unref (subject); + } + + va_start (args, format); + vsyslog (LOG_NOTICE, real_format, args); + va_end (args); + + g_free (real_format); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..ff5e0df --- /dev/null +++ b/src/util.h @@ -0,0 +1,36 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2009-2010 Red Hat, Inc. + * + * 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. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Written by: Matthias Clasen <mclasen@redhat.com> + */ + +#ifndef __UTIL_H__ +#define __UTIL_H__ + +#include <glib.h> +#include <dbus/dbus-glib.h> + +G_BEGIN_DECLS + +void sys_log (DBusGMethodInvocation *context, + const gchar *format, + ...); + +G_END_DECLS + +#endif /* __UTIL_H__ */ |