mariadb: Use procd and run as user
authorMichal Hrusecky <michal.hrusecky@turris.com>
Tue, 13 Oct 2020 06:52:42 +0000 (08:52 +0200)
committerMichal Hrusecky <michal.hrusecky@turris.com>
Wed, 14 Oct 2020 07:47:53 +0000 (09:47 +0200)
Drop mysqld_safe and use procd instead. Also run as a user.

Signed-off-by: Michal Hrusecky <michal.hrusecky@turris.com>
utils/mariadb/files/mysqld.init

index f1d775404a9572064b4fbf7594cef567ab6bfe49..d502e2baf36301ee4c079a0f9237412ad9372d5c 100644 (file)
@@ -5,14 +5,17 @@
 START=95
 # shellcheck disable=SC2034
 STOP=10
+# shellcheck disable=SC2034
+USE_PROCD=1
 
 NAME=mysqld
+my_user="mariadb"
+my_group="mariadb"
 
 LOGGER="/usr/bin/logger -p user.err -s -t $NAME --"
 [ -x "$LOGGER" ] || LOGGER="echo"
 
 MYSQLD="/usr/bin/$NAME"
-MYSQLDSAFE="/usr/bin/mysqld_safe"
 
 pidfile=""
 
@@ -63,25 +66,28 @@ mysqld_status() {
        fi
 }
 
-start() {
+start_service() {
        conf=/etc/mysql/my.cnf
        logdir=/var/log/mysql
        rundir=/var/run/mysqld
 
        hint="please fix your server configuration in /etc/mysql/"
  
-       for i in "$MYSQLD" "$MYSQLDSAFE"; do
-               if [ ! -x "$i" ]; then
-                       $LOGGER "$i is missing"
-                       exit 1
-               fi
-       done
+       if [ ! -x "$MYSQLD" ]; then
+               $LOGGER "$MYSQLD is missing"
+               exit 1
+       fi
 
        if [ ! -r "$conf" ]; then
                $LOGGER "$conf cannot be read"
                exit 1
        fi
 
+       if mysqld_status check_alive; then
+               $LOGGER "server is already running"
+               exit 0
+       fi
+
        config_load "$NAME"
 
        config_get_bool enabled general enabled 0
@@ -116,48 +122,39 @@ start() {
                $LOGGER "Cannot detect privileges table. You might need to run"
                $LOGGER "'mysql_install_db \"$args\"'"
                $LOGGER "to initialize the system tables."
+               $LOGGER "Then hand it ower to MariaDB user"
+               $LOGGER "'chown -Rh \"$my_user:$my_group\" \"$datadir\"'"
                exit 1
        fi
 
-       # Start daemon
-       if mysqld_status check_alive; then
-               $LOGGER "server is already running"
-       else
-               for i in "$logdir" "$rundir"; do
-                       opts="-m 0750"
-                       if ! [ -e "$i" ]; then
-                               # $rundir needs to be accessible for
-                               # clients
-                               if [ "$i" = "$rundir" ]; then
-                                       opts=
-                               fi
-                               # shellcheck disable=SC2086
-                               mkdir -p $opts "$i"
-                               [ -d "$i" ] && chown mariadb:mariadb "$i"
+       for i in "$logdir" "$rundir" "$tmpdir" "$datadir"; do
+               opts="-m 0750"
+               if ! [ -e "$i" ]; then
+                       # $rundir needs to be accessible for
+                       # clients
+                       if [ "$i" = "$rundir" ]; then
+                               opts=
                        fi
-               done
-               # shellcheck disable=SC2154,SC2086
-               "$MYSQLDSAFE" $options >/dev/null 2>&1 &
-       fi
-}
-
-stop() {
-       timeout="0"
-       while mysqld_status check_alive && [ "$timeout" -lt 60 ]; do
-               mysql_kill -TERM
-               sleep 1
-               timeout="$(($timeout + 1))"
+                       # shellcheck disable=SC2086
+                       mkdir -p $opts "$i"
+                       [ -d "$i" ] && chown -Rh "$my_user:$my_group" "$i"
+               fi
        done
-       if ! mysqld_status check_dead; then
-               $LOGGER "server is failing to stop"
-               mysql_kill -KILL
-       fi
-}
 
-reload() {
-       if mysqld_status check_alive; then
-               mysql_kill -HUP
-       else
-               $LOGGER "server is not running"
-       fi
+       # Start daemon
+       procd_open_instance
+
+       # shellcheck disable=SC2086
+       procd_set_param command "$MYSQLD" $options
+       procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
+       # run as user
+       procd_set_param user "$my_user"
+       # forward stderr to logd
+       procd_set_param stderr 1
+       # use HUP to reload
+       procd_set_param reload_signal HUP
+       # terminate using signals
+       procd_set_param term_timeout 60
+
+       procd_close_instance
 }