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.

156 lines
2.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010-2018 OpenWrt.org
  3. START=95
  4. STOP=10
  5. USE_PROCD=1
  6. #PROCD_DEBUG=1
  7. NAME=mysqld
  8. LOGGER="/usr/bin/logger -p user.err -s -t $NAME --"
  9. COMMAND=/usr/bin/$NAME
  10. mysqld_get_param() {
  11. $COMMAND --print-defaults \
  12. | tr " " "\n" \
  13. | grep -- "--$1" \
  14. | tail -n 1 \
  15. | cut -d= -f2
  16. }
  17. start_service() {
  18. local conf=/etc/mysql/my.cnf
  19. local dir
  20. local user
  21. local group
  22. local logfile
  23. local datadir
  24. local logdir=/var/log/mysql
  25. local rundir=/var/run/mysqld
  26. local tmpdir
  27. local enabled
  28. local log_stderr
  29. local log_stdout
  30. local options
  31. local hint="please fix your server configuration in /etc/mysql/"
  32. if [ ! -x $COMMAND ]; then
  33. $LOGGER $COMMAND is missing
  34. exit 1
  35. fi
  36. if [ ! -r $conf ]; then
  37. $LOGGER $conf cannot be read
  38. exit 1
  39. fi
  40. config_load $NAME
  41. config_get_bool enabled general enabled 0
  42. if [ $enabled -eq 0 ]; then
  43. $LOGGER service not enabled in /etc/config/$NAME
  44. exit 1
  45. fi
  46. config_get_bool log_stderr general log_stderr 1
  47. config_get_bool log_stdout general log_stdout 1
  48. config_get options general options
  49. datadir=$(mysqld_get_param datadir)
  50. logfile=$(mysqld_get_param general_log_file)
  51. tmpdir=$(mysqld_get_param tmpdir)
  52. user=$(mysqld_get_param user)
  53. if [ -z "$datadir" ]; then
  54. $LOGGER datadir is not set
  55. $LOGGER $hint
  56. exit 1
  57. fi
  58. if [ -z "$tmpdir" ]; then
  59. $LOGGER tmpdir is not set
  60. $LOGGER $hint
  61. exit 1
  62. fi
  63. if [ -z "$user" ]; then
  64. $LOGGER user is not set
  65. $LOGGER $hint
  66. exit 1
  67. fi
  68. user_exists "$user" || {
  69. $LOGGER user \""$user"\" does not exist
  70. $LOGGER $hint
  71. exit 1
  72. }
  73. group=$(id -g -n "$user")
  74. group_exists "$group" || {
  75. $LOGGER group \""$group"\" does not exist
  76. $LOGGER user \""$user"\" not configured correctly
  77. exit 1
  78. }
  79. [ -n "$logfile" ] && logdir=$(dirname "$logfile")
  80. # do not touch directories that already exist
  81. # posix shell does not support arrays, hence using awk
  82. awk \
  83. -v user="$user" \
  84. -v group="$group" \
  85. -v a="$datadir" \
  86. -v b="$logdir" \
  87. -v c="$rundir" \
  88. -v d="$tmpdir" \
  89. '
  90. BEGIN {
  91. dir[0]=a
  92. dir[1]=b
  93. dir[2]=c
  94. dir[3]=d
  95. for (x in dir) {
  96. if (system("test ! -e \"" dir[x] "\"" )) {
  97. delete dir[x]
  98. }
  99. }
  100. for (x in dir) {
  101. system("mkdir -p \"" dir[x] "\"" )
  102. system("chmod 750 \"" dir[x] "\"" )
  103. system("chown \"" user "\":\"" group "\" \"" dir[x] "\"" )
  104. }
  105. }
  106. '
  107. if [ ! -f "$datadir/mysql/tables_priv.MYD" ]; then
  108. local args="--force"
  109. local basedir=$(mysqld_get_param basedir)
  110. [ -n "$basedir" ] && args="$args --basedir=$basedir"
  111. $LOGGER Cannot detect privileges table. You might need to run
  112. $LOGGER \'mysql_install_db $args\'
  113. $LOGGER to initialize the system tables.
  114. exit 1
  115. fi
  116. procd_open_instance
  117. procd_set_param command $COMMAND $options
  118. # forward stderr to logd
  119. procd_set_param stderr $log_stderr
  120. # same for stdout
  121. procd_set_param stdout $log_stdout
  122. procd_close_instance
  123. }