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.

148 lines
3.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. my_user="mariadb"
  11. my_group="mariadb"
  12. LOGGER="/usr/bin/logger -p user.err -s -t $NAME --"
  13. [ -x "$LOGGER" ] || LOGGER="echo"
  14. MYSQLD="/usr/bin/$NAME"
  15. pidfile=""
  16. # mysqladmin likes to read /root/.my.cnf which could cause issues.
  17. export HOME="/etc/mysql"
  18. # Safeguard (relative paths, core dumps...)
  19. cd /
  20. mysqld_get_param() {
  21. "$MYSQLD" --help --verbose | sed -n 's|^'"$1"'[[:blank:]]\+||p'
  22. }
  23. # Send kill signal to MariaDB process
  24. #
  25. # Usage: boolean mysqld_kill signal
  26. mysql_kill() {
  27. [ -n "$pidfile" ] || pidfile="$(mysqld_get_param pid-file)"
  28. [ -f "$pidfile" ] || return 1
  29. pid="$(cat "$pidfile")"
  30. [ -n "$pid" ] || return 2
  31. kill "$1" "$pid"
  32. }
  33. # Checks if a server is running and accessible.
  34. #
  35. # Supported modes are 'check_alive' and 'check_dead'.
  36. # Both check for pidfile and whether the specified process is running and is
  37. # indeed mysqld. We could use mysqladmin for the check, but to be able to do
  38. # so, mysqladmin requires access to the database, which sounds like overkill
  39. # and potential security issue.
  40. #
  41. # Usage: boolean mysqld_status [check_alive|check_dead]
  42. mysqld_status() {
  43. ps_alive=0
  44. pidfile="$(mysqld_get_param pid-file)"
  45. if [ -f "$pidfile" ] && mysql_kill -0 2> /dev/null && \
  46. [ "$(readlink "/proc/$(cat "$pidfile")/exe")" = "$MYSQLD" ]; then
  47. ps_alive=1
  48. fi
  49. if { [ "$1" = check_alive ] && [ $ps_alive = 1 ]; } || \
  50. { [ "$1" = check_dead ] && [ $ps_alive = 0 ]; }
  51. then
  52. return 0 # EXIT_SUCCESS
  53. else
  54. return 1 # EXIT_FAILURE
  55. fi
  56. }
  57. start_service() {
  58. conf=/etc/mysql/my.cnf
  59. logdir=/var/log/mysql
  60. rundir=/var/run/mysqld
  61. hint="please fix your server configuration in /etc/mysql/"
  62. if [ ! -x "$MYSQLD" ]; then
  63. $LOGGER "$MYSQLD is missing"
  64. exit 1
  65. fi
  66. if [ ! -r "$conf" ]; then
  67. $LOGGER "$conf cannot be read"
  68. exit 1
  69. fi
  70. if mysqld_status check_alive; then
  71. $LOGGER "server is already running"
  72. exit 0
  73. fi
  74. config_load "$NAME"
  75. config_get_bool enabled general enabled 0
  76. # shellcheck disable=SC2154
  77. if [ "$enabled" -eq 0 ]; then
  78. $LOGGER "service not enabled in /etc/config/$NAME"
  79. exit 1
  80. fi
  81. config_get options general options
  82. datadir="$(mysqld_get_param datadir)"
  83. tmpdir="$(mysqld_get_param tmpdir)"
  84. if [ ! -f "$datadir/mysql/tables_priv.MAD" ]; then
  85. args="--force"
  86. basedir="$(mysqld_get_param basedir)"
  87. [ -n "$basedir" ] && args="$args --basedir=$basedir"
  88. $LOGGER "Cannot detect privileges table. You might need to run"
  89. $LOGGER "'mysql_install_db \"$args\"'"
  90. $LOGGER "to initialize the system tables."
  91. $LOGGER "Then hand it ower to MariaDB user"
  92. $LOGGER "'chown -Rh \"$my_user:$my_group\" \"$datadir\"'"
  93. exit 1
  94. fi
  95. for i in "$logdir" "$rundir" "$tmpdir" "$datadir"; do
  96. opts="-m 0750"
  97. if ! [ -e "$i" ]; then
  98. # $rundir needs to be accessible for
  99. # clients
  100. if [ "$i" = "$rundir" ]; then
  101. opts=
  102. fi
  103. # shellcheck disable=SC2086
  104. mkdir -p $opts "$i"
  105. [ -d "$i" ] && chown -Rh "$my_user:$my_group" "$i"
  106. fi
  107. done
  108. # Start daemon
  109. procd_open_instance
  110. # shellcheck disable=SC2154 disable=SC2086
  111. procd_set_param command "$MYSQLD" $options
  112. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  113. # run as user
  114. procd_set_param user "$my_user"
  115. # forward stderr to logd
  116. procd_set_param stderr 1
  117. # use HUP to reload
  118. procd_set_param reload_signal HUP
  119. # terminate using signals
  120. procd_set_param term_timeout 60
  121. procd_close_instance
  122. }