[Commits] Rev 3862: MDEV-4472 Audit plugin. in file:///home/hf/wmar/mdev-4472/

holyfoot at askmonty.org holyfoot at askmonty.org
Sun Aug 25 07:03:35 EEST 2013


At file:///home/hf/wmar/mdev-4472/

------------------------------------------------------------
revno: 3862
revision-id: holyfoot at askmonty.org-20130825025230-92mvx0sefpylpgxw
parent: holyfoot at askmonty.org-20130822194255-76owptwkf653l8u9
committer: Alexey Botchkov <holyfoot at askmonty.org>
branch nick: mdev-4472
timestamp: Sun 2013-08-25 07:52:30 +0500
message:
  MDEV-4472 Audit plugin.
          additions 2
  
          MDEV-4760 Audit: log is only created when parameters are set in a certain order.
          MDEV-4767 Audit: NULL values for output_type and file_path are exessive.
-------------- next part --------------
=== modified file 'mysql-test/suite/plugins/r/server_audit.result'
--- a/mysql-test/suite/plugins/r/server_audit.result	2013-08-22 19:42:55 +0000
+++ b/mysql-test/suite/plugins/r/server_audit.result	2013-08-25 02:52:30 +0000
@@ -3,14 +3,14 @@ show variables like 'server_audit%';
 Variable_name   Value
 server_audit_events     
 server_audit_excl_users 
-server_audit_file_path  
+server_audit_file_path  server_audit.log
 server_audit_file_rotate_now    
 server_audit_file_rotate_size   1000000
 server_audit_file_rotations     9
 server_audit_incl_users 
 server_audit_logging    OFF
 server_audit_mode       0
-server_audit_output_type        null
+server_audit_output_type        file
 server_audit_syslog_facility    LOG_USER
 server_audit_syslog_ident       mysql-server_auditing
 server_audit_syslog_info        

=== modified file 'plugin/server_audit/server_audit.c'
--- a/plugin/server_audit/server_audit.c	2013-08-22 19:42:55 +0000
+++ b/plugin/server_audit/server_audit.c	2013-08-25 02:52:30 +0000
@@ -103,7 +103,7 @@ static int started_mysql= 0;
 static char *incl_users, *excl_users,
             *file_path, *syslog_info;
 static unsigned int mode, mode_readonly= 0;
-static ulong output_type= 0;
+static ulong output_type;
 static ulong syslog_facility, syslog_priority;
 
 static ulonglong events; /* mask for events to log */
@@ -159,10 +159,10 @@ static TYPELIB events_typelib=
 static MYSQL_SYSVAR_SET(events, events, PLUGIN_VAR_RQCMDARG,
        "Specifies the set of events to monitor. Can be CONNECT, QUERY, TABLE.",
        NULL, NULL, 0, &events_typelib);
-#define OUTPUT_NULL 0
-#define OUTPUT_SYSLOG 1
-#define OUTPUT_FILE 2
-static const char *output_type_names[]= { "null", "syslog", "file", 0 };
+#define OUTPUT_SYSLOG 0
+#define OUTPUT_FILE 1
+#define OUTPUT_NO 0xFFFF
+static const char *output_type_names[]= { "syslog", "file", 0 };
 static TYPELIB output_typelib=
 {
     array_elements(output_type_names) - 1, "output_typelib",
@@ -170,11 +170,11 @@ static TYPELIB output_typelib=
 };
 static MYSQL_SYSVAR_ENUM(output_type, output_type, PLUGIN_VAR_RQCMDARG,
        "Desired output type. Possible values - 'syslog', 'file'"
-       " or 'null' as no output.", 0, update_output_type, 0,
+       " or 'null' as no output.", 0, update_output_type, OUTPUT_FILE,
        &output_typelib);
 static MYSQL_SYSVAR_STR(file_path, file_path,
        PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
-       "Path to the log file.", NULL, NULL, "");
+       "Path to the log file.", NULL, NULL, "server_audit.log");
 static MYSQL_SYSVAR_ULONGLONG(file_rotate_size, file_rotate_size,
        PLUGIN_VAR_RQCMDARG, "Maximum size of the log to start the rotation.",
        NULL, NULL,
@@ -409,6 +409,47 @@ static int get_user_host(const char *uh_
 }
 
 
+static int start_logging()
+{
+  if (output_type == OUTPUT_FILE)
+  {
+    logfile= logger_open(file_path, file_rotate_size, rotations);
+    if (logfile == NULL)
+    {
+      error_header();
+      fprintf(stderr, "Could not create file '%s'. Logging was disabled.\n", file_path);
+      logging= 0;
+      return 1;
+    }
+    error_header();
+    fprintf(stderr, "logging started to the file %s.\n", file_path);
+  }
+  else if (output_type == OUTPUT_SYSLOG)
+  {
+    openlog(syslog_ident, LOG_NOWAIT, syslog_facility_codes[syslog_facility]);
+    error_header();
+    fprintf(stderr, "logging started to the syslog.\n");
+  }
+  return 0;
+}
+
+
+static int stop_logging()
+{
+  if (output_type == OUTPUT_FILE && logfile)
+  {
+    logger_close(logfile);
+  }
+  else if (output_type == OUTPUT_SYSLOG)
+  {
+    closelog();
+  }
+  error_header();
+  fprintf(stderr, "logging was stopped.\n");
+  return 0;
+}
+
+
 static struct connection_info *
   add_connection(const struct mysql_event_connection *event)
 {
@@ -470,9 +511,11 @@ static void change_connection(const stru
 
 static int write_log(const char *message, int len)
 {
-  if (output_type == OUTPUT_FILE && logfile)
+  if (output_type == OUTPUT_FILE)
   {
-    return logger_write(logfile, message, len);
+    if (!logfile)
+      return 1;
+    return logger_write(logfile, message, len) != len;
   }
   else if (output_type == OUTPUT_SYSLOG)
   {
@@ -885,14 +928,8 @@ static int server_audit_init(void *p __a
   error_header();
   fprintf(stderr, "STARTED\n");
 
-  if (output_type != OUTPUT_NULL)
-  {
-    ulong param_output_type= output_type;
-    output_type= OUTPUT_NULL;
-    update_output_type(NULL, NULL, NULL, &param_output_type);
-    error_header();
-    fprintf(stderr, "logging is %s.\n", logging ? "enabled" : "disabled");
-  }
+  if (logging)
+    start_logging();
 
   return 0;
 }
@@ -1021,36 +1058,16 @@ static void update_output_type(MYSQL_THD
   if (output_type == new_output_type)
     return;
 
-  if (output_type == OUTPUT_FILE && logfile)
-  {
-    logger_close(logfile);
-  }
-  else if (output_type == OUTPUT_SYSLOG)
-  {
-    closelog();
-  }
-
-  output_type= OUTPUT_NULL;
-
-  if (new_output_type == OUTPUT_FILE)
-  {
-    logfile= logger_open(file_path, file_rotate_size, rotations);
-    if (logfile == NULL)
-    {
-      error_header();
-      fprintf(stderr, "Could not create file '%s'\n", file_path);
-      return;
-    }
-  }
-  else if (new_output_type == OUTPUT_SYSLOG)
-  {
-    openlog(syslog_ident, LOG_NOWAIT, syslog_facility_codes[syslog_facility]);
-  }
+  if (logging)
+    stop_logging();
 
   output_type= new_output_type;
   error_header();
   fprintf(stderr, "Output was redirected to '%s'\n",
           output_type_names[output_type]);
+
+  if (logging)
+    start_logging();
 }
 
 
@@ -1094,9 +1111,10 @@ static void update_logging(MYSQL_THD thd
   if (new_logging == logging)
     return;
 
-  logging= new_logging;
-  error_header();
-  fprintf(stderr, "logging was %s.\n", logging ? "enabled" : "disabled");
+  if ((logging= new_logging))
+    start_logging();
+  else
+    stop_logging();
 }
 
 



More information about the commits mailing list