Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Signal processing in MYSQL (SIGTERM,SIGQUIT,SIGHUP, etc.)

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Original, because the programming level of LINUX system is limited, please point out some improper words.

Basic knowledge of signal processing and multithreading first signal processing

In LINUX, signal is a soft interrupt mechanism processed by the kernel, which satisfies the characteristics of simplicity, not being able to carry a lot of information, and must meet certain conditions before it will be sent.

The signal goes through generation-- > blocking signal set-- > pending signal set-- > signal delivery-- > signal processing mode.

First of all, there are many ways to generate signals, such as kill naming, which we often use. Here is a list of some commonly used signals in kill and the default processing method.

(extracted from Xing Wenpeng's LINUX handout)

Click (here) to collapse or open

1) SIGHUP: when a user exits a shell, all processes started by the shell will receive this signal, and the default action is to terminate the process.

2) SIGINT: when the user presses the key combination, the user terminal sends this signal to the running program started by the terminal. Default action

As a termination mileage.

3) SIGQUIT: this signal is generated when the user presses the key combination, and the user terminal sends some messages to the running program started by the terminal

No. The default action is to terminate the process.

4) SIGILL:CPU detected that a process executed an illegal instruction. The default action is to terminate the process and generate a core file

5) SIGTRAP: this signal is generated by breakpoint instructions or other trap instructions. Defaults to action as the termination mileage and generates a core file.

6) SIGABRT: this signal is generated when the abort function is called. The default action is to terminate the process and generate a core file.

7) SIGBUS: illegal access to memory address, including memory alignment error, defaults to terminate the process and generate core files.

8) SIGFPE: issued when a fatal operation error occurs. It includes not only floating-point operation errors, but also all algorithm errors such as overflow and divisor 0. Silent

Recognize it as a termination process and generate a core file.

9) SIGKILL: unconditionally terminates the process. This signal cannot be ignored, processed or blocked. The default action is to terminate the process. It provides the system administrator with

To kill any process.

10) SIGUSE1: a user-defined signal. That is, the programmer can define and use the signal in the program. The default action is to terminate the process.

11) SIGSEGV: indicates that the process made invalid memory access. The default action is to terminate the process and generate a core file.

12) SIGUSR2: this is another user-defined signal that programmers can define and use in the program. The default action is to terminate the process. one

13) SIGPIPE:Broken pipe writes data to a pipe that does not have a reader. The default action is to terminate the process.

14) SIGALRM: the timer times out, which is set by the system call alarm. The default action is to terminate the process.

15) SIGTERM: program end signal, which, unlike SIGKILL, can be blocked and terminated. It is usually used to show that the program exits normally. Execution

This signal is generated by default when shell commands Kill. The default action is to terminate the process.

16) SIGCHLD: when the child process ends, the parent process receives this signal. The default action is to ignore this signal.

17) SIGCONT: stops execution of the process. Signals cannot be ignored, processed or blocked. The default action is to terminate the process.

18) SIGTTIN: the background process reads the terminal console. The default action is to pause the process.

19) SIGTSTP: stop the process from running. This signal is sent when the key combination is pressed. The default action is to pause the process.

21) SIGTTOU: this signal is similar to SIGTTIN and occurs when the background process is about to output data to the terminal. The default action is to pause the process.

22) SIGURG: when there is emergency data on the socket, send some signals to the currently running process, reporting the arrival of emergency data. Such as network out-of-band data

Upon arrival, the default action is to ignore the signal.

23) SIGXFSZ: the process execution time exceeds the CPU time assigned to the process, and the system generates the signal and sends it to the process. Default action as termination

Process.

24) SIGXFSZ: exceeds the maximum file length setting. The default action is to terminate the process.

25) SIGVTALRM: this signal is generated when the virtual clock times out. Similar to SIGALRM, but this signal only calculates the CPU usage time occupied by the process. Silent

Recognize it as a termination process.

26) SGIPROF: similar to SIGVTALRM, it includes the time spent on CPU by the process and the time it takes to execute system calls. Defaults to action as a termination

Cheng.

27) SIGWINCH: issued when the window changes in size. The default action is to ignore the signal.

28) SIGIO: this signal indicates to the process that an asynchronous IO event has been sent. The default action is ignored.

29) SIGPWR: turn it off. The default action is to terminate the process.

30) SIGSYS: invalid system call. The default action is to terminate the process and generate a core file.

31) real-time signals of SIGRTMIN~ (64) SIGRTMAX:LINUX, which have no fixed meaning (can be customized by the user). All the real-time messages

The default action is to terminate the process, and secondly, our frequent keystrokes can also be generated.

Ctrl+c 2) SIGINT

Ctrl+\ 3) SIGQUIT

Ctrl+z 4) SIGTSTP

Of course, there are many other triggers, such as hardware exceptions, raise functions, abort functions, alarm functions and so on.

The second is the blocking signal set, which can shield the signal that you want to (except signal No. 9 to 19). If the signal will not reach the delivery state after shielding, it will not be mentioned.

Took care of it. We set the blocking signal set through the sigprocmask function, but before we have to set the sigset_t set, through sigaddset sigdelset sigemptyset

Sigfillset and other function settings.

The pending signal set cannot be manipulated and can only be obtained through the sigpending function, but it can control the delivery of the signal together with the blocking signal set.

After the signal is delivered, the signal needs to be processed. The default behavior is listed above, but the signal (except signal no. 9) can be captured and changed. We can customize the function.

As a way to process a signal, this can be captured and processed by the signal function and the sigaction function. The sigaction function is relatively complex and requires a struct sigaction.

Structure variable, which contains the member sa_handler\ sa_mask\ sa_flags\ sa_siaction. You can check the LINUX man page without explanation here.

The above is the signal processing mode under a single process, in multithreading, the common processing mode between threads, but there can be different signal shielding sets, and a unified signal setting is generally used in multithreading.

Masking words and signal processing methods use the pthread_mask function to inherit to each thread, and use functions such as sigwait/sigwaitinfo to set up a separate signal processing thread to unify.

Deal with, this is how MYSQL handles it.

2. Signal processing in MYSQL

First we can find that there is a separate signal processing thread in MYSQL

| | 36 | 1927 | sql/signal_handler | NULL | BACKGROUND | NULL | NULL |

This thread uniformly handles the signals of the entire MYSQLD process, especially when it comes to signal processing such as SIGTERM,SIGQUIT,SIGHUP.

Let's analyze it from the source code trigger.

1. Signal initialization

Void my_init_signals () function

Let's release some source code about signal processing to explain.

Click (here) to collapse or open

/ *

SA_RESETHAND resets handler action to default when entering handler.

SA_NODEFER allows receiving the same signal during handler.

E. g. SIGABRT during our signal handler will dump core (default action).

, /

/ / default processing of some column signals, such as segment errors, floating point exceptions, bus errors, CPU illegal instructions, etc.

/ / change the method to handle_fatal_signal function call, which should print some

/ / status information in case of error. I didn't take a closer look at this callback function.

Sa.sa_flags= SA_RESETHAND | SA_NODEFER

Sa.sa_handler= handle_fatal_signal

/ / Treat all these as fatal and handle them.

(void) sigaction (SIGSEGV, & sa, NULL)

(void) sigaction (SIGABRT, & sa, NULL)

(void) sigaction (SIGBUS, & sa, NULL)

(void) sigaction (SIGILL, & sa, NULL)

(void) sigaction (SIGFPE, & sa, NULL)

}

/ / Ignore SIGPIPE and SIGALRM

/ / Pipeline errors and timer signals are ignored here

Sa.sa_flags= 0

Sa.sa_handler= SIG_IGN

(void) sigaction (SIGPIPE, & sa, NULL)

(void) sigaction (SIGALRM, & sa, NULL)

/ / Custom signal, which terminates socket communication from the annotations

/ / callback function is empty_signal_handler

/ / SIGUSR1 is used to interrupt the socket listener.

Sa.sa_handler= empty_signal_handler

(void) sigaction (SIGUSR1, & sa, NULL)

/ / it is estimated to be some special treatment here because SIGTERM and SIGHUP have been set later.

/ / blocking, which should be related to the platform

/ / Fix signals if ignored by parents (can happen on Mac OS X).

Sa.sa_handler= SIG_DFL

(void) sigaction (SIGTERM, & sa, NULL)

(void) sigaction (SIGHUP, & sa, NULL)

/ / Let's start to set our blocking signal set to take effect through pthread_sigmask

/ / to block SIGQUIT, SIGHUP, SIGTERM, SIGTSTP and other signals

/ / are some commonly used signals that can terminate the process.

Sigset_t set

(void) sigemptyset & set)

/ *

Block SIGQUIT, SIGHUP and SIGTERM.

The signal handler thread does sigwait () on these.

, /

(void) sigaddset (& set, SIGQUIT)

(void) sigaddset (& set, SIGHUP)

(void) sigaddset (& set, SIGTERM)

(void) sigaddset (& set, SIGTSTP)

/ *

Block SIGINT unless debugging to prevent Ctrl+C from causing

Unclean shutdown of the server.

, /

If (! (test_flags & TEST_SIGINT))

(void) sigaddset (& set, SIGINT)

Pthread_sigmask (SIG_SETMASK, & set, NULL)

Here we find that the MYSQL thread actually blocks SIGQUIT, SIGHUP, SIGTERM, SIGTSTP

Signals, through a special thread to process these signals. At the same time, many signals have been recaptured and changed.

For more information on how to deal with it, see the above explanation.

2. Signal processing thread

Start_signal_handler-- > signal_hand to set up the signal processing thread.

Let's focus on the callback function signal_hand. Here are some source codes and explanations.

Click (here) to collapse or open

Extern "C" void * signal_hand (void * arg MY_ATTRIBUTE ((unused)

{

My_thread_init ()

/ / set sigset_t signal set here, and set SIGTERM/SIGQUIT/SIGHUP

Sigset_t set

(void) sigemptyset & set)

(void) sigaddset (& set, SIGTERM)

(void) sigaddset (& set, SIGQUIT)

(void) sigaddset (& set, SIGHUP)

MUTEX related is not considered.

For (;;)

{

Int sig

While (sigwait (& set, & sig) = = EINTR) / / call sigwait blocking capture signal

{}

If (cleanup_done)

{

My_thread_end ()

My_thread_exit (0); / / Safety

Return NULL; / / Avoid compiler warnings

}

Switch (sig) {/ / the following judgment is very important if SIGTERM and SIGQUIT will

/ / pthread_kill terminates all active session threads and each thread will receive

/ / SIGUSR1 signal and then call the empty_signal_handler callback function to carry out

/ / process then close_connections and then my_thread_end shut down

/ / whether the innodb shutdown operation is called here, you can break the point through GDB

/ / on the innobase_shutdown_for_mysql function, I did

/ / the test does call and will be given later, that is to say, kill SIGTERM and

/ / SIGQUIT is safe because it is normal for them to call innodb to close the function

/ / shut down innodb

Case SIGTERM:

Case SIGQUIT:

/ / Switch to the file log message processing.

Query_logger.set_handlers ((log_output_options! = LOG_NONE)?

LOG_FILE: LOG_NONE)

DBUG_PRINT ("info", ("Got signal:% d abort_loop:% d", sig, abort_loop))

If (! abort_loop)

{

Abort_loop= true; / / Mark abort for threads.

/ *

Kill the socket listener.

The main thread will then set socket_listener_active= false

And wait for us to finish all the cleanup below.

, /

Mysql_mutex_lock & LOCK_socket_listener_active)

While (socket_listener_active)

{

DBUG_PRINT ("info", ("Killing socket listener"))

If (pthread_kill (main_thread_id, SIGUSR1))

{

DBUG_ASSERT (false)

Break

}

Mysql_cond_wait & COND_socket_listener_active

& LOCK_socket_listener_active)

}

Mysql_mutex_unlock & LOCK_socket_listener_active)

Close_connections ()

}

My_thread_end ()

My_thread_exit (0)

Return NULL; / / Avoid compiler warnings

Break

Case SIGHUP: / / here is also a lot of concern about what SIGHUP has done.

/ / We can see that he calls reload_acl_and_cache to refresh

/ / one thing, which is given later, there is no other operation

/ / the behavior of this SIGHUP signal has been completely changed

If (! abort_loop)

{

Int not_used

Mysql_print_status (); / / Print some debug info

Reload_acl_and_cache (NULL

(REFRESH_LOG | REFRESH_TABLES | REFRESH_FAST |

REFRESH_GRANT | REFRESH_THREADS | REFRESH_HOSTS)

NULL, & not_used); / / Flush logs

/ / Reenable query logs after the options were reloaded.

Query_logger.set_handlers (log_output_options)

}

Break

Default:

Break; / * purecov: tested * /

}

}

Return NULL; / * purecov: deadcode * /

}

Here we see a lot of things we care about:

What does kill/kill-15/kill-SIGTERM do:

They are all the same: SIGTERM signal MYSQL closes all active connections while cleanly shutting down innodb. I did see the call to the innodb shutdown function through gdb.

It's easy. I just need to call innobase_shutdown_for_mysql () at the breakpoint and kill mysqldpid at the same time.

Click (here) to collapse or open

(gdb) bt

# 0 innobase_shutdown_for_mysql () at / root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/srv/srv0start.cc:2785

# 1 0x00000000019a0f9c in innobase_end (hton=0x2e9a450, type=HA_PANIC_CLOSE) at / root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4360

# 2 0x0000000000f62215 in ha_finalize_handlerton (plugin=0x2fe31a0) at / root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:813

# 3 0x00000000015d3179 in plugin_deinitialize (plugin=0x2fe31a0, ref_check=true) at / root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_plugin.cc:995

# 4 0x00000000015d3562 in reap_plugins () at / root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_plugin.cc:1077

# 5 0x00000000015d54c7 in plugin_shutdown () at / root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_plugin.cc:1845

# 6 0x0000000000ebf5eb in clean_up (print_message=true) at / root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:1336

# 7 0x0000000000ec6c7e in mysqld_main (argc=56, argv=0x2e98768) at / root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5358

# 8 what does 0x0000000000ebd404 in main (argc=9, argv=0x7fffffffe418) at / root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25kill-3/kill-SIGQUIT do:

As above, this is clearly seen in the source code

What does kill-1/kill-SIGHUP do:

It's said to do in the source code interpretation.

Reload_acl_and_cache (NULL, (REFRESH_LOG | REFRESH_TABLES | REFRESH_FAST) |

REFRESH_GRANT | REFRESH_THREADS | REFRESH_HOSTS)

NULL,? _ used); / / Flush logs

Function call about parameter 2

@ param options What should be reset/reloaded (tables, privileges, slave...)

We can see this explanation, so

REFRESH_LOG | REFRESH_TABLES | REFRESH_FAST | REFRESH_GRANT | REFRESH_THREADS | REFRESH_HOSTS

What do they stand for? obviously they are bitmap, and what they represent is explained in the source code as follows:

Click (here) to collapse or open

# define REFRESH_GRANT 1 / * Refresh grant tables * /

# define REFRESH_LOG 2 / * Start on new log file * /

# define REFRESH_TABLES 4 / * close all tables * /

# define REFRESH_HOSTS 8 / * Flush host cache * /

# define REFRESH_STATUS 16 / * Flush status variables * /

# define REFRESH_THREADS 32 / * Flush thread cache * /

# define REFRESH_SLAVE 64 / * Reset master info and restart slave

Thread * /

# define REFRESH_MASTER 128 / * Remove all bin logs in the index

And truncate the index * /

# define REFRESH_ERROR_LOG 256 / * Rotate only the erorr log * /

# define REFRESH_ENGINE_LOG 512 / * Flush all storage engine logs * /

# define REFRESH_BINARY_LOG 1024 / * Flush the binary log * /

# define REFRESH_RELAY_LOG 2048 / * Flush the relay log * /

# define REFRESH_GENERAL_LOG 4096 / * Flush the general log * /

# define REFRESH_SLOW_LOG 8192 / * Flush the slow query log * / they are basically self-explanatory and do not need to be described too much, so we clearly understand that SIGHUP is redefined in mysqld and does all kinds of refresh operations.

Of course, we must not directly kill-9 this signal can not be blocked and can not be captured, but forced to terminate the process at this time it is impossible for innodb to call

Innobase_shutdown_for_mysql to shut down MYSQLD cleanly.

Author Wechat:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report