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.

207 lines
5.5 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. # Check for correct owner
  133. local owner="$(stat --format %U:%G "$datadir" 2> /dev/null)"
  134. if [ -n "$owner" ] && [ "$owner" != "$my_user:$my_group" ]; then
  135. chown -Rh "$my_user:$my_group" "$datadir"
  136. fi
  137. # Start upgrade instance without credentials
  138. sudo -u "$my_user" mysqld --skip-networking --skip-grant-tables --socket=/tmp/mysql_upgrade.sock &
  139. PID="$!"
  140. i=0
  141. # Wait for upgrade instance of db to start
  142. while [ "$i" -lt 15 ] && test \! -S /tmp/mysql_upgrade.sock; do
  143. sleep 1
  144. i="$((i + 1))"
  145. done
  146. [ -S /tmp/mysql_upgrade.sock ] || {
  147. $LOGGER "Failed to start upgrading instance of MariaDB."
  148. exit 1
  149. }
  150. # Upgrade the database
  151. mysql_upgrade --upgrade-system-tables --socket=/tmp/mysql_upgrade.sock
  152. echo "$version" > "$datadir"/.version
  153. # Stop the upgrade instance
  154. kill "$PID"
  155. i=0
  156. while [ "$i" -lt 60 ] && grep -q mysql "/proc/$PID/cmdline"; do
  157. sleep 1
  158. [ "$i" -lt 30 ] || kill "$PID"
  159. i="$((i + 1))"
  160. done
  161. # Use force
  162. if grep -q mysql "/proc/$PID/cmdline"; then
  163. kill -9 "$PID"
  164. fi
  165. fi
  166. # Start daemon
  167. procd_open_instance
  168. # shellcheck disable=SC2154 disable=SC2086
  169. procd_set_param command "$MYSQLD" $options
  170. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  171. # run as user
  172. procd_set_param user "$my_user"
  173. # forward stderr to logd
  174. procd_set_param stderr 1
  175. # use HUP to reload
  176. procd_set_param reload_signal HUP
  177. # terminate using signals
  178. procd_set_param term_timeout 60
  179. procd_close_instance
  180. }