Mysql: kill query on timeout

We have an SQL timeout, but it wasn't actually killing the query when
it times out. This is problem when issuing the FLUSH TABLES command
because it occasionally locks up the database if there is a
long-running write query.

Now, we kill the query when the timeout hits which also kills it upon
CTRL-C'ing

See here for the problem (fixes #23):
https://github.com/alestic/ec2-consistent-snapshot/issues/23

Got the idea for the fix from here:
http://www.perlmonks.org/?node_id=885620

I've tested the following:

1. Call: sql_timeout_retry("SELECT SLEEP(60);", 0.5, 10, 5); after
   connecting.
2. Check the processlist while this is happening:
   Before: We see the query in the list all the time because the
           timeout *doesn't* stop the query
   After:  query is rarely seen in the list cause it times out and is
           killed after 0.5s
3. Interrupt the process (Ctrl-C) while it's happening
   Before: Query is still in the processlist until it finishes
   After: Query is killed when the process is killed.
Mysql Queries: kill current on SIGINT and friends

Previously, if you sent SIG{INT,TERM,HUP} to the process the current
mysql query wouldn't be killed (even though the process would). This
potentially would cause a FLUSH TABLES to hang the database until it
was manually killed via the query id.

Now the current query is killed appropriately upon recieving one of
those signals.
Query timeout: don't act in signal handler

Apparently, acting within signal handlers is rather dangerous

See comment by @andyg0808:
https://github.com/iFixit/ec2-consistent-snapshot/pull/4#discussion_r14804117
Timeouts: use double-eval safeguard

Prevent unlikely but possible bug where the code you're timing out
could die *just* before the timeout happend, escaping the eval,
causing the SIGALRM to occurr and thus call `die` before the alarm is
cleared.

See:
http://search.cpan.org/~lbaxter/Sys-SigAction/lib/Sys/SigAction.pm#Note_on_%22Double_evals%22
and:
http://search.cpan.org/dist/DBI/DBI.pm#Signal_Handling_and_Canceling_Operations
Merge kill-query-on-timeout (#4) into ifixit-production

Mysql: kill query on timeout

We have an SQL timeout, but it wasn't actually killing the query when
it times out. This is a problem when issuing the FLUSH TABLES command
because it occasionally locks up the database if there is a
long-running write query.

Now, we kill the query when the timeout hits OR any relevant signal
(INT, TERM, HUP)

See here for the problem (fixes #23):
https://github.com/alestic/ec2-consistent-snapshot/issues/23

Got the idea for the fix from here:
http://www.perlmonks.org/?node_id=885620

I've tested the following:

1. Call: sql_timeout_retry("SELECT SLEEP(60);", 0.5, 10, 5); after
   connecting.
2. Check the processlist while this is happening:
   Before: We see the query in the list all the time because the
           timeout *doesn't* stop the query
   After:  query is rarely seen in the list cause it times out and is
           killed after 0.5s
3. Interrupt the process (Ctrl-C) while it's happening
   Before: Query is still in the processlist until it finishes
   After: Query is killed when the process is killed.

See this pull for much more discussion and test reports:
https://github.com/iFixit/ec2-consistent-snapshot/pull/4