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.

428 lines
9.6 KiB

  1. /*
  2. Copyright (c) 2002 Novell, Inc. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <netdb.h>
  18. #include <sys/stat.h>
  19. #include <monitor.h>
  20. #include <strings.h>
  21. #include <getopt.h>
  22. #include <screen.h>
  23. #include <errno.h>
  24. #include "my_config.h"
  25. #include "my_manage.h"
  26. /******************************************************************************
  27. global variables
  28. ******************************************************************************/
  29. char autoclose;
  30. char basedir[PATH_MAX];
  31. char datadir[PATH_MAX];
  32. char err_log[PATH_MAX];
  33. char out_log[PATH_MAX];
  34. char mysqld[PATH_MAX];
  35. char hostname[PATH_MAX];
  36. char sql_file[PATH_MAX];
  37. char default_option[PATH_MAX];
  38. /******************************************************************************
  39. prototypes
  40. ******************************************************************************/
  41. void start_defaults(int, char*[]);
  42. void finish_defaults();
  43. void read_defaults(arg_list_t *);
  44. void parse_args(int, char*[]);
  45. void get_options(int, char*[]);
  46. void create_paths();
  47. int mysql_install_db(int argc, char *argv[]);
  48. /******************************************************************************
  49. functions
  50. ******************************************************************************/
  51. /******************************************************************************
  52. start_defaults()
  53. Start setting the defaults.
  54. ******************************************************************************/
  55. void start_defaults(int argc, char *argv[])
  56. {
  57. struct stat buf;
  58. int i;
  59. // default options
  60. static char *default_options[] =
  61. {
  62. "--no-defaults",
  63. "--defaults-file=",
  64. "--defaults-extra-file=",
  65. NULL
  66. };
  67. // autoclose
  68. autoclose = FALSE;
  69. // basedir
  70. get_basedir(argv[0], basedir);
  71. // hostname
  72. if (gethostname(hostname,PATH_MAX) < 0)
  73. {
  74. // default
  75. strcpy(hostname,"mysql");
  76. }
  77. // default option
  78. default_option[0] = NULL;
  79. for (i=0; (argc > 1) && default_options[i]; i++)
  80. {
  81. if(!strnicmp(argv[1], default_options[i], strlen(default_options[i])))
  82. {
  83. strncpy(default_option, argv[1], PATH_MAX);
  84. break;
  85. }
  86. }
  87. // set after basedir is established
  88. datadir[0] = NULL;
  89. err_log[0] = NULL;
  90. out_log[0] = NULL;
  91. mysqld[0] = NULL;
  92. sql_file[0] = NULL;
  93. }
  94. /******************************************************************************
  95. finish_defaults()
  96. Finish setting the defaults.
  97. ******************************************************************************/
  98. void finish_defaults()
  99. {
  100. struct stat buf;
  101. int i;
  102. // datadir
  103. if (!datadir[0]) snprintf(datadir, PATH_MAX, "%s/data", basedir);
  104. // err-log
  105. if (!err_log[0]) snprintf(err_log, PATH_MAX, "%s/%s.err", datadir, hostname);
  106. // out-log
  107. if (!out_log[0]) snprintf(out_log, PATH_MAX, "%s/%s.out", datadir, hostname);
  108. // sql-file
  109. if (!sql_file[0]) snprintf(sql_file, PATH_MAX, "%s/bin/init_db.sql", basedir);
  110. // mysqld
  111. if (!mysqld[0]) snprintf(mysqld, PATH_MAX, "%s/bin/mysqld", basedir);
  112. }
  113. /******************************************************************************
  114. read_defaults()
  115. Read the defaults.
  116. ******************************************************************************/
  117. void read_defaults(arg_list_t *pal)
  118. {
  119. arg_list_t al;
  120. char defaults_file[PATH_MAX];
  121. char mydefaults[PATH_MAX];
  122. char line[PATH_MAX];
  123. FILE *fp;
  124. // defaults output file
  125. snprintf(defaults_file, PATH_MAX, "%s/bin/defaults.out", basedir);
  126. remove(defaults_file);
  127. // mysqladmin file
  128. snprintf(mydefaults, PATH_MAX, "%s/bin/my_print_defaults", basedir);
  129. // args
  130. init_args(&al);
  131. add_arg(&al, mydefaults);
  132. if (default_option[0]) add_arg(&al, default_option);
  133. add_arg(&al, "mysqld");
  134. add_arg(&al, "mysql_install_db");
  135. spawn(mydefaults, &al, TRUE, NULL, defaults_file, NULL);
  136. free_args(&al);
  137. // gather defaults
  138. if((fp = fopen(defaults_file, "r")) != NULL)
  139. {
  140. while(fgets(line, PATH_MAX, fp))
  141. {
  142. char *p;
  143. // remove end-of-line character
  144. if ((p = strrchr(line, '\n')) != NULL) *p = '\0';
  145. // add the option as an argument
  146. add_arg(pal, line);
  147. }
  148. fclose(fp);
  149. }
  150. // remove file
  151. remove(defaults_file);
  152. }
  153. /******************************************************************************
  154. parse_args()
  155. Get the options.
  156. ******************************************************************************/
  157. void parse_args(int argc, char *argv[])
  158. {
  159. int index = 0;
  160. int c;
  161. // parse options
  162. enum opts
  163. {
  164. OPT_BASEDIR = 0xFF,
  165. OPT_DATADIR,
  166. OPT_SQL_FILE
  167. };
  168. static struct option options[] =
  169. {
  170. {"autoclose", no_argument, &autoclose, TRUE},
  171. {"basedir", required_argument, 0, OPT_BASEDIR},
  172. {"datadir", required_argument, 0, OPT_DATADIR},
  173. {"sql-file", required_argument, 0, OPT_SQL_FILE},
  174. {0, 0, 0, 0}
  175. };
  176. // we have to reset getopt_long because we use it multiple times
  177. optind = 1;
  178. // turn off error reporting
  179. opterr = 0;
  180. while ((c = getopt_long(argc, argv, "b:h:", options, &index)) >= 0)
  181. {
  182. switch (c)
  183. {
  184. case OPT_BASEDIR:
  185. case 'b':
  186. strcpy(basedir, optarg);
  187. break;
  188. case OPT_DATADIR:
  189. case 'h':
  190. strcpy(datadir, optarg);
  191. break;
  192. case OPT_SQL_FILE:
  193. strcpy(sql_file, optarg);
  194. break;
  195. default:
  196. // ignore
  197. break;
  198. }
  199. }
  200. }
  201. /******************************************************************************
  202. get_options()
  203. Get the options.
  204. ******************************************************************************/
  205. void get_options(int argc, char *argv[])
  206. {
  207. arg_list_t al;
  208. // start defaults
  209. start_defaults(argc, argv);
  210. // default file arguments
  211. init_args(&al);
  212. add_arg(&al, "ignore");
  213. read_defaults(&al);
  214. parse_args(al.argc, al.argv);
  215. free_args(&al);
  216. // command-line arguments
  217. parse_args(argc, argv);
  218. // finish defaults
  219. finish_defaults();
  220. }
  221. /******************************************************************************
  222. create_paths()
  223. Create database paths.
  224. ******************************************************************************/
  225. void create_paths()
  226. {
  227. struct stat info;
  228. char temp[PATH_MAX];
  229. // check for tables
  230. snprintf(temp, PATH_MAX, "%s/mysql/host.frm", datadir);
  231. if (!stat(temp, &info))
  232. {
  233. printf("A database already exists in the directory:\n");
  234. printf("\t%s\n\n", datadir);
  235. exit(-1);
  236. }
  237. // data directory
  238. if (stat(datadir, &info))
  239. {
  240. mkdir(datadir, 0);
  241. }
  242. }
  243. /******************************************************************************
  244. mysql_install_db()
  245. Install the database.
  246. ******************************************************************************/
  247. int mysql_install_db(int argc, char *argv[])
  248. {
  249. arg_list_t al;
  250. int i, j, err;
  251. char skip;
  252. // private options
  253. static char *private_options[] =
  254. {
  255. "--autoclose",
  256. "--sql-file=",
  257. NULL
  258. };
  259. // args
  260. init_args(&al);
  261. add_arg(&al, "%s", mysqld);
  262. // parent args
  263. for(i = 1; i < argc; i++)
  264. {
  265. skip = FALSE;
  266. // skip private arguments
  267. for (j=0; private_options[j]; j++)
  268. {
  269. if(!strnicmp(argv[i], private_options[j], strlen(private_options[j])))
  270. {
  271. skip = TRUE;
  272. break;
  273. }
  274. }
  275. if (!skip) add_arg(&al, "%s", argv[i]);
  276. }
  277. add_arg(&al, "--bootstrap");
  278. add_arg(&al, "--skip-grant-tables");
  279. add_arg(&al, "--skip-innodb");
  280. // spawn mysqld
  281. err = spawn(mysqld, &al, TRUE, sql_file, out_log, err_log);
  282. // free args
  283. free_args(&al);
  284. return err;
  285. }
  286. /******************************************************************************
  287. main()
  288. ******************************************************************************/
  289. int main(int argc, char **argv)
  290. {
  291. // get options
  292. get_options(argc, argv);
  293. // check for an autoclose option
  294. if (!autoclose) setscreenmode(SCR_NO_MODE);
  295. // header
  296. printf("MySQL Server %s, for %s (%s)\n\n", VERSION, SYSTEM_TYPE,
  297. MACHINE_TYPE);
  298. // create paths
  299. create_paths();
  300. // install the database
  301. if (mysql_install_db(argc, argv))
  302. {
  303. printf("ERROR - The database creation failed!\n");
  304. printf(" %s\n", strerror(errno));
  305. printf("See the following log for more infomration:\n");
  306. printf("\t%s\n\n", err_log);
  307. exit(-1);
  308. }
  309. // status
  310. printf("Initial database successfully created in the directory:\n");
  311. printf("\t%s\n", datadir);
  312. // info
  313. printf("\nPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !\n");
  314. printf("\nThis is done with:\n");
  315. printf("\tmysqladmin -u root password 'new-password'\n");
  316. printf("\nSee the manual for more instructions.\n");
  317. printf("\nYou can start the MySQL daemon with:\n");
  318. printf("\tmysqld_safe\n");
  319. printf("\nPlease report any problems with:\n");
  320. printf("\t/mysql/mysqlbug.txt\n");
  321. printf("\nThe latest information about MySQL is available on the web at\n");
  322. printf("\thttp://www.mysql.com\n");
  323. printf("\nSupport MySQL by buying support at http://shop.mysql.com\n\n");
  324. return 0;
  325. }