You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.3 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010-2018 OpenWrt.org
  3. # shellcheck disable=SC2034
  4. START=95
  5. # shellcheck disable=SC2034
  6. STOP=10
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. NAME=mysqld
  10. LOGGER="/usr/bin/logger -p user.err -s -t $NAME --"
  11. [ -x "$LOGGER" ] || LOGGER="echo"
  12. MYSQLD="/usr/bin/$NAME"
  13. pidfile=""
  14. # mysqladmin likes to read /root/.my.cnf which could cause issues.
  15. export HOME="/etc/mysql"
  16. # Safeguard (relative paths, core dumps...)
  17. cd /
  18. mysqld_get_param() {
  19. "$MYSQLD" --help --verbose | sed -n 's|^'"$1"'[[:blank:]]\+||p'
  20. }
  21. # Send kill signal to MariaDB process
  22. #
  23. # Usage: boolean mysqld_kill signal
  24. mysql_kill() {
  25. [ -n "$pidfile" ] || pidfile="$(mysqld_get_param pid-file)"
  26. [ -f "$pidfile" ] || return 1
  27. pid="$(cat "$pidfile")"
  28. [ -n "$pid" ] || return 2
  29. kill "$1" "$pid"
  30. }
  31. # Checks if a server is running and accessible.
  32. #
  33. # Supported modes are 'check_alive' and 'check_dead'.
  34. # Both check for pidfile and whether the specified process is running and is
  35. # indeed mysqld. We could use mysqladmin for the check, but to be able to do
  36. # so, mysqladmin requires access to the database, which sounds like overkill
  37. # and potential security issue.
  38. #
  39. # Usage: boolean mysqld_status [check_alive|check_dead]
  40. mysqld_status() {
  41. ps_alive=0
  42. pidfile="$(mysqld_get_param pid-file)"
  43. if [ -f "$pidfile" ] && mysql_kill -0 2> /dev/null && \
  44. [ "$(readlink "/proc/$(cat "$pidfile")/exe")" = "$MYSQLD" ]; then
  45. ps_alive=1
  46. fi
  47. if { [ "$1" = check_alive ] && [ $ps_alive = 1 ]; } || \
  48. { [ "$1" = check_dead ] && [ $ps_alive = 0 ]; }
  49. then
  50. return 0 # EXIT_SUCCESS
  51. else
  52. return 1 # EXIT_FAILURE
  53. fi
  54. }
  55. start_service() {
  56. conf=/etc/mysql/my.cnf
  57. logdir=/var/log/mysql
  58. rundir=/var/run/mysqld
  59. version="$(mysqld --version | sed -n 's|.*Ver[[:blank:]]*\([0-9.]*\)-.*|\1|p')"
  60. # Few basic checks
  61. if [ ! -x "$MYSQLD" ]; then
  62. $LOGGER "$MYSQLD is missing"
  63. exit 1
  64. fi
  65. if [ -z "$version" ]; then
  66. $LOGGER "Can't get MariaDB version, something is seriously wrong"
  67. exit 1
  68. fi
  69. if [ ! -r "$conf" ]; then
  70. $LOGGER "$conf cannot be read"
  71. exit 1
  72. fi
  73. if mysqld_status check_alive; then
  74. $LOGGER "server is already running"
  75. exit 0
  76. fi
  77. # Get various config options
  78. config_load "$NAME"
  79. config_get my_user general user "mariadb"
  80. config_get my_group general group "mariadb"
  81. config_get_bool enabled general enabled 0
  82. config_get_bool init_db general init 1
  83. config_get_bool autoupgrade general upgrade 1
  84. config_get options general options
  85. # shellcheck disable=SC2154
  86. if [ "$enabled" -eq 0 ]; then
  87. $LOGGER "service not enabled in /etc/config/$NAME"
  88. exit 1
  89. fi
  90. datadir="$(mysqld_get_param datadir)"
  91. tmpdir="$(mysqld_get_param tmpdir)"
  92. sockdir="$(dirname "$(mysqld_get_param socket)")"
  93. # Make sure we have a working database in datadir
  94. if [ ! -f "$datadir/mysql/tables_priv.MAD" ]; then
  95. args="--force"
  96. basedir="$(mysqld_get_param basedir)"
  97. [ -n "$basedir" ] && args="$args --basedir=$basedir"
  98. # shellcheck disable=SC2154
  99. if [ "$init_db" -gt 0 ]; then
  100. # shellcheck disable=SC2154
  101. mysql_install_db $args --skip-name-resolve --skip-test-db --datadir="$datadir" || exit 1
  102. echo "$version" > "$datadir"/.version
  103. chown -Rh "$my_user:$my_group" "$datadir"
  104. else
  105. $LOGGER "Cannot detect privileges table. You might need to run"
  106. $LOGGER "'mysql_install_db \"$args\"'"
  107. $LOGGER "to initialize the system tables."
  108. $LOGGER "Then hand it ower to MariaDB user"
  109. # shellcheck disable=SC2154
  110. $LOGGER "'chown -Rh \"$my_user:$my_group\" \"$datadir\"'"
  111. exit 1
  112. fi
  113. fi
  114. # Make sure all required directories exists and have correct rights
  115. for i in "$logdir" "$rundir" "$sockdir"; do
  116. opts="-m 0750"
  117. if ! [ -e "$i" ]; then
  118. # $rundir needs to be accessible for
  119. # clients
  120. if [ "$i" = "$rundir" ]; then
  121. opts=
  122. fi
  123. # shellcheck disable=SC2086
  124. mkdir -p $opts "$i"
  125. fi
  126. # shellcheck disable=SC2154
  127. [ -d "$i" ] && chown -Rh "$my_user:$my_group" "$i"
  128. done
  129. # Migration from old versions
  130. # shellcheck disable=SC2154
  131. if [ "$(cat "$datadir"/.version 2> /dev/null)" \!= "$version" ] && [ "$autoupgrade" -gt 0 ]; then
  132. # Start upgrade instance without credentials
  133. sudo -u "$my_user" mysqld --skip-networking --skip-grant-tables --socket=/tmp/mysql_upgrade.sock &
  134. PID="$!"
  135. i=0
  136. # Wait for upgrade instance of db to start
  137. while [ "$i" -lt 15 ] && test \! -S /tmp/mysql_upgrade.sock; do
  138. sleep 1
  139. i="$((i + 1))"
  140. done
  141. [ -S /tmp/mysql_upgrade.sock ] || {
  142. $LOGGER "Failed to start upgrading instance of MariaDB."
  143. exit 1
  144. }
  145. # Upgrade the database
  146. mysql_upgrade --upgrade-system-tables --socket=/tmp/mysql_upgrade.sock
  147. echo "$version" > "$datadir"/.version
  148. # Stop the upgrade instance
  149. kill "$PID"
  150. i=0
  151. while [ "$i" -lt 60 ] && grep -q mysql "/proc/$PID/cmdline"; do
  152. sleep 1
  153. [ "$i" -lt 30 ] || kill "$PID"
  154. i="$((i + 1))"
  155. done
  156. # Use force
  157. if grep -q mysql "/proc/$PID/cmdline"; then
  158. kill -9 "$PID"
  159. fi
  160. fi
  161. # Start daemon
  162. procd_open_instance
  163. # shellcheck disable=SC2154 disable=SC2086
  164. procd_set_param command "$MYSQLD" $options
  165. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  166. # run as user
  167. procd_set_param user "$my_user"
  168. # forward stderr to logd
  169. procd_set_param stderr 1
  170. # use HUP to reload
  171. procd_set_param reload_signal HUP
  172. # terminate using signals
  173. procd_set_param term_timeout 60
  174. procd_close_instance
  175. }