15
15
16
16
17
17
#define PLUGIN_VERSION 0x104
18
- #define PLUGIN_STR_VERSION "1.4.4 "
18
+ #define PLUGIN_STR_VERSION "1.4.7 "
19
19
20
20
#define _my_thread_var loc_thread_var
21
21
@@ -295,7 +295,7 @@ static unsigned long long file_rotate_size;
295
295
static unsigned int rotations ;
296
296
static my_bool rotate = TRUE;
297
297
static char logging ;
298
- static int internal_stop_logging = 0 ;
298
+ static volatile int internal_stop_logging = 0 ;
299
299
static char incl_user_buffer [1024 ];
300
300
static char excl_user_buffer [1024 ];
301
301
static char * big_buffer = NULL ;
@@ -533,16 +533,20 @@ static struct st_mysql_show_var audit_status[]=
533
533
#if defined(HAVE_PSI_INTERFACE ) && !defined(FLOGGER_NO_PSI )
534
534
/* These belong to the service initialization */
535
535
static PSI_mutex_key key_LOCK_operations ;
536
+ static PSI_mutex_key key_LOCK_atomic ;
536
537
static PSI_mutex_key key_LOCK_bigbuffer ;
537
538
static PSI_mutex_info mutex_key_list []=
538
539
{
539
540
{ & key_LOCK_operations , "SERVER_AUDIT_plugin::lock_operations" ,
540
541
PSI_FLAG_GLOBAL },
542
+ { & key_LOCK_atomic , "SERVER_AUDIT_plugin::lock_atomic" ,
543
+ PSI_FLAG_GLOBAL },
541
544
{ & key_LOCK_bigbuffer , "SERVER_AUDIT_plugin::lock_bigbuffer" ,
542
545
PSI_FLAG_GLOBAL }
543
546
};
544
547
#endif
545
548
static mysql_mutex_t lock_operations ;
549
+ static mysql_mutex_t lock_atomic ;
546
550
static mysql_mutex_t lock_bigbuffer ;
547
551
548
552
/* The Percona server and partly MySQL don't support */
@@ -553,6 +557,14 @@ static mysql_mutex_t lock_bigbuffer;
553
557
/* worths doing. */
554
558
#define CLIENT_ERROR if (!started_mysql) my_printf_error
555
559
560
+ #define ADD_ATOMIC (x , a ) \
561
+ do { \
562
+ flogger_mutex_lock(&lock_atomic); \
563
+ x+= a; \
564
+ flogger_mutex_unlock(&lock_atomic); \
565
+ } while (0)
566
+
567
+
556
568
static uchar * getkey_user (const char * entry , size_t * length ,
557
569
my_bool nu __attribute__((unused )) )
558
570
{
@@ -731,20 +743,20 @@ static int user_coll_fill(struct user_coll *c, char *users,
731
743
732
744
if (cmp_user && take_over_cmp )
733
745
{
734
- internal_stop_logging = 1 ;
746
+ ADD_ATOMIC ( internal_stop_logging , 1 ) ;
735
747
CLIENT_ERROR (1 , "User '%.*s' was removed from the"
736
748
" server_audit_excl_users." ,
737
749
MYF (ME_JUST_WARNING ), (int ) cmp_length , users );
738
- internal_stop_logging = 0 ;
750
+ ADD_ATOMIC ( internal_stop_logging , -1 ) ;
739
751
blank_user (cmp_user );
740
752
refill_cmp_coll = 1 ;
741
753
}
742
754
else if (cmp_user )
743
755
{
744
- internal_stop_logging = 1 ;
756
+ ADD_ATOMIC ( internal_stop_logging , 1 ) ;
745
757
CLIENT_ERROR (1 , "User '%.*s' is in the server_audit_incl_users, "
746
758
"so wasn't added." , MYF (ME_JUST_WARNING ), (int ) cmp_length , users );
747
- internal_stop_logging = 0 ;
759
+ ADD_ATOMIC ( internal_stop_logging , -1 ) ;
748
760
remove_user (users );
749
761
continue ;
750
762
}
@@ -1252,23 +1264,30 @@ static void change_connection(struct connection_info *cn,
1252
1264
event -> ip , event -> ip_length );
1253
1265
}
1254
1266
1255
- static int write_log (const char * message , int len )
1267
+ static int write_log (const char * message , size_t len , int take_lock )
1256
1268
{
1269
+ int result = 0 ;
1270
+ if (take_lock )
1271
+ flogger_mutex_lock (& lock_operations );
1272
+
1257
1273
if (output_type == OUTPUT_FILE )
1258
1274
{
1259
1275
if (logfile &&
1260
- (is_active = (logger_write (logfile , message , len ) == len )))
1261
- return 0 ;
1276
+ (is_active = (logger_write (logfile , message , len ) == ( int ) len )))
1277
+ goto exit ;
1262
1278
++ log_write_failures ;
1263
- return 1 ;
1279
+ result = 1 ;
1264
1280
}
1265
1281
else if (output_type == OUTPUT_SYSLOG )
1266
1282
{
1267
1283
syslog (syslog_facility_codes [syslog_facility ] |
1268
1284
syslog_priority_codes [syslog_priority ],
1269
- "%s %.*s" , syslog_info , len , message );
1285
+ "%s %.*s" , syslog_info , ( int ) len , message );
1270
1286
}
1271
- return 0 ;
1287
+ exit :
1288
+ if (take_lock )
1289
+ flogger_mutex_unlock (& lock_operations );
1290
+ return result ;
1272
1291
}
1273
1292
1274
1293
@@ -1327,7 +1346,7 @@ static int log_connection(const struct connection_info *cn,
1327
1346
csize += my_snprintf (message + csize , sizeof (message ) - 1 - csize ,
1328
1347
",%.*s,,%d" , cn -> db_length , cn -> db , event -> status );
1329
1348
message [csize ]= '\n' ;
1330
- return write_log (message , csize + 1 );
1349
+ return write_log (message , csize + 1 , 1 );
1331
1350
}
1332
1351
1333
1352
@@ -1348,7 +1367,7 @@ static int log_connection_event(const struct mysql_event_connection *event,
1348
1367
csize += my_snprintf (message + csize , sizeof (message ) - 1 - csize ,
1349
1368
",%.*s,,%d" , event -> database_length , event -> database , event -> status );
1350
1369
message [csize ]= '\n' ;
1351
- return write_log (message , csize + 1 );
1370
+ return write_log (message , csize + 1 , 1 );
1352
1371
}
1353
1372
1354
1373
@@ -1477,21 +1496,28 @@ static size_t escape_string_hide_passwords(const char *str, unsigned int len,
1477
1496
1478
1497
1479
1498
1480
- static int do_log_user (const char * name )
1499
+ static int do_log_user (const char * name , int take_lock )
1481
1500
{
1482
1501
size_t len ;
1502
+ int result ;
1483
1503
1484
1504
if (!name )
1485
1505
return 0 ;
1486
1506
len = strlen (name );
1487
1507
1488
- if (incl_user_coll . n_users )
1489
- return coll_search ( & incl_user_coll , name , len ) != 0 ;
1508
+ if (take_lock )
1509
+ flogger_mutex_lock ( & lock_operations ) ;
1490
1510
1491
- if (excl_user_coll .n_users )
1492
- return coll_search (& excl_user_coll , name , len ) == 0 ;
1511
+ if (incl_user_coll .n_users )
1512
+ result = coll_search (& incl_user_coll , name , len ) != 0 ;
1513
+ else if (excl_user_coll .n_users )
1514
+ result = coll_search (& excl_user_coll , name , len ) == 0 ;
1515
+ else
1516
+ result = 1 ;
1493
1517
1494
- return 1 ;
1518
+ if (take_lock )
1519
+ flogger_mutex_unlock (& lock_operations );
1520
+ return result ;
1495
1521
}
1496
1522
1497
1523
@@ -1588,7 +1614,7 @@ static int filter_query_type(const char *query, struct sa_keyword *kwd)
1588
1614
static int log_statement_ex (const struct connection_info * cn ,
1589
1615
time_t ev_time , unsigned long thd_id ,
1590
1616
const char * query , unsigned int query_len ,
1591
- int error_code , const char * type )
1617
+ int error_code , const char * type , int take_lock )
1592
1618
{
1593
1619
size_t csize ;
1594
1620
char message_loc [1024 ];
@@ -1736,7 +1762,7 @@ static int log_statement_ex(const struct connection_info *cn,
1736
1762
csize += my_snprintf (message + csize , message_size - 1 - csize ,
1737
1763
"\',%d" , error_code );
1738
1764
message [csize ]= '\n' ;
1739
- result = write_log (message , csize + 1 );
1765
+ result = write_log (message , csize + 1 , take_lock );
1740
1766
if (message == big_buffer )
1741
1767
flogger_mutex_unlock (& lock_bigbuffer );
1742
1768
@@ -1750,7 +1776,7 @@ static int log_statement(const struct connection_info *cn,
1750
1776
{
1751
1777
return log_statement_ex (cn , event -> general_time , event -> general_thread_id ,
1752
1778
event -> general_query , event -> general_query_length ,
1753
- event -> general_error_code , type );
1779
+ event -> general_error_code , type , 1 );
1754
1780
}
1755
1781
1756
1782
@@ -1772,7 +1798,7 @@ static int log_table(const struct connection_info *cn,
1772
1798
",%.*s,%.*s," ,event -> database_length , event -> database ,
1773
1799
event -> table_length , event -> table );
1774
1800
message [csize ]= '\n' ;
1775
- return write_log (message , csize + 1 );
1801
+ return write_log (message , csize + 1 , 1 );
1776
1802
}
1777
1803
1778
1804
@@ -1796,7 +1822,7 @@ static int log_rename(const struct connection_info *cn,
1796
1822
event -> new_database_length , event -> new_database ,
1797
1823
event -> new_table_length , event -> new_table );
1798
1824
message [csize ]= '\n' ;
1799
- return write_log (message , csize + 1 );
1825
+ return write_log (message , csize + 1 , 1 );
1800
1826
}
1801
1827
1802
1828
@@ -1988,8 +2014,6 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
1988
2014
if (!thd || internal_stop_logging )
1989
2015
return ;
1990
2016
1991
- flogger_mutex_lock (& lock_operations );
1992
-
1993
2017
if (maria_55_started && debug_server_started &&
1994
2018
event_class == MYSQL_AUDIT_GENERAL_CLASS )
1995
2019
{
@@ -2024,7 +2048,7 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
2024
2048
goto exit_func ;
2025
2049
2026
2050
if (event_class == MYSQL_AUDIT_GENERAL_CLASS && FILTER (EVENT_QUERY ) &&
2027
- cn && do_log_user (cn -> user ))
2051
+ cn && do_log_user (cn -> user , 1 ))
2028
2052
{
2029
2053
const struct mysql_event_general * event =
2030
2054
(const struct mysql_event_general * ) ev ;
@@ -2043,7 +2067,7 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
2043
2067
{
2044
2068
const struct mysql_event_table * event =
2045
2069
(const struct mysql_event_table * ) ev ;
2046
- if (do_log_user (event -> user ))
2070
+ if (do_log_user (event -> user , 1 ))
2047
2071
{
2048
2072
switch (event -> event_subclass )
2049
2073
{
@@ -2109,7 +2133,6 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
2109
2133
}
2110
2134
if (cn )
2111
2135
cn -> log_always = 0 ;
2112
- flogger_mutex_unlock (& lock_operations );
2113
2136
}
2114
2137
2115
2138
@@ -2377,6 +2400,7 @@ static int server_audit_init(void *p __attribute__((unused)))
2377
2400
PSI_server -> register_mutex ("server_audit" , mutex_key_list , 1 );
2378
2401
#endif
2379
2402
flogger_mutex_init (key_LOCK_operations , & lock_operations , MY_MUTEX_INIT_FAST );
2403
+ flogger_mutex_init (key_LOCK_operations , & lock_atomic , MY_MUTEX_INIT_FAST );
2380
2404
flogger_mutex_init (key_LOCK_operations , & lock_bigbuffer , MY_MUTEX_INIT_FAST );
2381
2405
2382
2406
coll_init (& incl_user_coll );
@@ -2464,6 +2488,7 @@ static int server_audit_deinit(void *p __attribute__((unused)))
2464
2488
2465
2489
(void ) free (big_buffer );
2466
2490
flogger_mutex_destroy (& lock_operations );
2491
+ flogger_mutex_destroy (& lock_atomic );
2467
2492
flogger_mutex_destroy (& lock_bigbuffer );
2468
2493
2469
2494
error_header ();
@@ -2553,10 +2578,10 @@ static void log_current_query(MYSQL_THD thd)
2553
2578
return ;
2554
2579
cn = get_loc_info (thd );
2555
2580
if (!ci_needs_setup (cn ) && cn -> query_length &&
2556
- FILTER (EVENT_QUERY ) && do_log_user (cn -> user ))
2581
+ FILTER (EVENT_QUERY ) && do_log_user (cn -> user , 0 ))
2557
2582
{
2558
2583
log_statement_ex (cn , cn -> query_time , thd_get_thread_id (thd ),
2559
- cn -> query , cn -> query_length , 0 , "QUERY" );
2584
+ cn -> query , cn -> query_length , 0 , "QUERY" , 0 );
2560
2585
cn -> log_always = 1 ;
2561
2586
}
2562
2587
}
@@ -2568,12 +2593,13 @@ static void update_file_path(MYSQL_THD thd,
2568
2593
{
2569
2594
char * new_name = (* (char * * ) save ) ? * (char * * ) save : empty_str ;
2570
2595
2571
- if (!maria_55_started || !debug_server_started )
2572
- flogger_mutex_lock (& lock_operations );
2573
- internal_stop_logging = 1 ;
2596
+ ADD_ATOMIC (internal_stop_logging , 1 );
2574
2597
error_header ();
2575
2598
fprintf (stderr , "Log file name was changed to '%s'.\n" , new_name );
2576
2599
2600
+ if (!maria_55_started || !debug_server_started )
2601
+ flogger_mutex_lock (& lock_operations );
2602
+
2577
2603
if (logging )
2578
2604
log_current_query (thd );
2579
2605
@@ -2582,7 +2608,6 @@ static void update_file_path(MYSQL_THD thd,
2582
2608
char * sav_path = file_path ;
2583
2609
2584
2610
file_path = new_name ;
2585
- internal_stop_logging = 1 ;
2586
2611
stop_logging ();
2587
2612
if (start_logging ())
2588
2613
{
@@ -2598,16 +2623,15 @@ static void update_file_path(MYSQL_THD thd,
2598
2623
}
2599
2624
goto exit_func ;
2600
2625
}
2601
- internal_stop_logging = 0 ;
2602
2626
}
2603
2627
2604
2628
strncpy (path_buffer , new_name , sizeof (path_buffer )- 1 );
2605
2629
path_buffer [sizeof (path_buffer )- 1 ]= 0 ;
2606
2630
file_path = path_buffer ;
2607
2631
exit_func :
2608
- internal_stop_logging = 0 ;
2609
2632
if (!maria_55_started || !debug_server_started )
2610
2633
flogger_mutex_unlock (& lock_operations );
2634
+ ADD_ATOMIC (internal_stop_logging , -1 );
2611
2635
}
2612
2636
2613
2637
@@ -2692,8 +2716,8 @@ static void update_output_type(MYSQL_THD thd,
2692
2716
if (output_type == new_output_type )
2693
2717
return ;
2694
2718
2719
+ ADD_ATOMIC (internal_stop_logging , 1 );
2695
2720
flogger_mutex_lock (& lock_operations );
2696
- internal_stop_logging = 1 ;
2697
2721
if (logging )
2698
2722
{
2699
2723
log_current_query (thd );
@@ -2707,8 +2731,8 @@ static void update_output_type(MYSQL_THD thd,
2707
2731
2708
2732
if (logging )
2709
2733
start_logging ();
2710
- internal_stop_logging = 0 ;
2711
2734
flogger_mutex_unlock (& lock_operations );
2735
+ ADD_ATOMIC (internal_stop_logging , -1 );
2712
2736
}
2713
2737
2714
2738
@@ -2756,9 +2780,9 @@ static void update_logging(MYSQL_THD thd,
2756
2780
if (new_logging == logging )
2757
2781
return ;
2758
2782
2783
+ ADD_ATOMIC (internal_stop_logging , 1 );
2759
2784
if (!maria_55_started || !debug_server_started )
2760
2785
flogger_mutex_lock (& lock_operations );
2761
- internal_stop_logging = 1 ;
2762
2786
if ((logging = new_logging ))
2763
2787
{
2764
2788
start_logging ();
@@ -2773,9 +2797,9 @@ static void update_logging(MYSQL_THD thd,
2773
2797
stop_logging ();
2774
2798
}
2775
2799
2776
- internal_stop_logging = 0 ;
2777
2800
if (!maria_55_started || !debug_server_started )
2778
2801
flogger_mutex_unlock (& lock_operations );
2802
+ ADD_ATOMIC (internal_stop_logging , -1 );
2779
2803
}
2780
2804
2781
2805
@@ -2787,16 +2811,16 @@ static void update_mode(MYSQL_THD thd __attribute__((unused)),
2787
2811
if (mode_readonly || new_mode == mode )
2788
2812
return ;
2789
2813
2814
+ ADD_ATOMIC (internal_stop_logging , 1 );
2790
2815
if (!maria_55_started || !debug_server_started )
2791
2816
flogger_mutex_lock (& lock_operations );
2792
- internal_stop_logging = 1 ;
2793
2817
mark_always_logged (thd );
2794
2818
error_header ();
2795
2819
fprintf (stderr , "Logging mode was changed from %d to %d.\n" , mode , new_mode );
2796
2820
mode = new_mode ;
2797
- internal_stop_logging = 0 ;
2798
2821
if (!maria_55_started || !debug_server_started )
2799
2822
flogger_mutex_unlock (& lock_operations );
2823
+ ADD_ATOMIC (internal_stop_logging , -1 );
2800
2824
}
2801
2825
2802
2826
0 commit comments