Bug #31361 Patch to make mysqld_safe react on signals HUP and TERM
Submitted: 3 Oct 2007 1:20 Modified: 16 Aug 2017 13:43
Reporter: Christian Hammers (Silver Quality Contributor) (OCA) Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: Packaging Severity:S4 (Feature request)
Version:5.1 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution, qc

[3 Oct 2007 1:20] Christian Hammers
Description:
The below patch executes e.g. "mysqladmin refresh" on "kill -HUP mysqld_safe"
or "mysqladmin shutdown" on "kill -TERM mysqld_safe".
Those signals are currently ignored.

Note that this requires that either the mysql root user has no password or the unix root account a ~/.my.cnf with the password.

Note also that bash ignores the QUIT signal so use TERM when testing :)

bye,

-christian-

How to repeat:
-

Suggested fix:
--- old/scripts/mysqld_safe.sh  2006-07-29 13:12:34.000000000 +0200
+++ old/scripts/mysqld_safe.sh  2006-07-29 13:14:08.000000000 +0200
@@ -16,8 +16,6 @@
 # This command can be used as pipe to syslog. With "-s" it also logs to stderr.
 ERR_LOGGER="logger -p daemon.err -t mysqld_safe -i"
 
-trap '' 1 2 3 15                       # we shouldn't let anyone kill us
-
 umask 007
 
 defaults=
@@ -122,7 +122,7 @@
       # sed buffers output (only GNU sed supports a -u (unbuffered) option)
       # which means that messages may not get sent to syslog until the
       # mysqld process quits.
-      cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p daemon.error"
+      cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p daemon.error & wait"
       ;;
     *)
       echo "Internal program error (non-fatal):" \
@@ -352,6 +350,13 @@
 fi
 
 #
+# From now on, we catch signals to do a proper shutdown of mysqld
+# when signalled to do so.
+#
+trap '/usr/bin/mysqladmin refresh' 1 # HUP
+trap '/usr/bin/mysqladmin shutdown' 2 3 15 # INT QUIT and TERM
+
+#
 # Uncomment the following lines if you want all tables to be automatically
 # checked and repaired during startup. You should add sensible key_buffer
 # and sort_buffer values to my.cnf to improve check performance or require
[3 Oct 2007 12:00] Valeriy Kravchuk
Thank you for a patch contributed.
[8 Jun 2009 22:34] liz drachnik
Hello Christian - 

In order for us to continue the process of reviewing your contribution to MySQL - We need you to review and sign the Sun|MySQL contributor agreement (the "SCA")

The process is explained here: 
http://forge.mysql.com/wiki/Sun_Contributor_Agreement

Getting a signed/approved SCA on file will help us facilitate your contribution-- this one, and others in the future.

Thank you ! 

Liz Drachnik  - Program Manager - MySQL
[6 Jul 2009 21:30] Christian Hammers
Meanwhile you should have received the agreement. If not, please contact me again.

BTW, the patch was recently claimed to be buggy, too, in http://bugs.debian.org/527623

The solution proposed there was something like the following, I don't have a complete patch:

---------------------------------------------------------
that [edit: he's refering to my proposal] patch is still wrong.

The first time a HUP is received, we run the code in the trap
and call wait which will wait for both the refresh command and
the mysqld one.

But we won't return from that trap until mysqld dies, and in the
trap the HUP signal is blocked, which means any subsequent
HUP will not be handled.

A better way could be to implement some proper event handling as in:

trap : HUP INT QUIT TERM
while :; do
  action=
  mysqld ... & 
  while :; do
    signal=NONE
    wait || signal=$(kill -l "$?")

    case $signal in
      (INT|TERM|QUIT) mysqladmin ... shutdown; exit;;
      (HUP) mysqladmin ... refresh;;
      (NONE) break;; # mysqld died
      (*) unexpected ... die;;
    esac
  esac
done
[16 Aug 2017 13:43] Terje Røsten
Posted by developer:
 
I don't see the need to send signals to mysqld_safe to all as signals can be sent to mysqld directly
(mysqld  has it's own pid file). 

The requirement of empty root password or password in  ~/.my.cnf is also blocker for this change.

Besides, Debian 9 and most other Linux distros are using systemd where mysqld_safe
is not used.