mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / libmysqld / libmysqld.c
blob508916e79bab8011ce95be557c45488502f1163e
1 /*
2 Copyright (c) 2001-2008 MySQL AB, 2009 Sun Microsystems, Inc.
3 Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <my_global.h>
20 #include <mysql.h>
21 #include <mysql_embed.h>
22 #include <mysqld_error.h>
23 #include <my_pthread.h>
24 #include "embedded_priv.h"
25 #include <my_sys.h>
26 #include <mysys_err.h>
27 #include <m_string.h>
28 #include <m_ctype.h>
29 #include "errmsg.h"
30 #include <violite.h>
31 #include <sys/stat.h>
32 #include <signal.h>
33 #include <time.h>
34 #include "client_settings.h"
35 #ifdef HAVE_PWD_H
36 #include <pwd.h>
37 #endif
38 #if !defined(MSDOS) && !defined(__WIN__)
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <netdb.h>
43 #ifdef HAVE_SELECT_H
44 # include <select.h>
45 #endif
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
48 #endif
49 #endif
50 #ifdef HAVE_SYS_UN_H
51 # include <sys/un.h>
52 #endif
53 #ifndef INADDR_NONE
54 #define INADDR_NONE -1
55 #endif
57 extern ulong net_buffer_length;
58 extern ulong max_allowed_packet;
60 #if defined(MSDOS) || defined(__WIN__)
61 #define ERRNO WSAGetLastError()
62 #define perror(A)
63 #else
64 #include <errno.h>
65 #define ERRNO errno
66 #define SOCKET_ERROR -1
67 #define closesocket(A) close(A)
68 #endif
70 #ifdef HAVE_GETPWUID
71 struct passwd *getpwuid(uid_t);
72 char* getlogin(void);
73 #endif
75 #ifdef __WIN__
76 static my_bool is_NT(void)
78 char *os=getenv("OS");
79 return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
81 #endif
83 /**************************************************************************
84 ** Shut down connection
85 **************************************************************************/
87 static void end_server(MYSQL *mysql)
89 DBUG_ENTER("end_server");
90 free_old_query(mysql);
91 DBUG_VOID_RETURN;
95 int mysql_init_character_set(MYSQL *mysql);
97 MYSQL * STDCALL
98 mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
99 const char *passwd, const char *db,
100 uint port, const char *unix_socket,ulong client_flag)
102 char name_buff[USERNAME_LENGTH];
104 DBUG_ENTER("mysql_real_connect");
105 DBUG_PRINT("enter",("host: %s db: %s user: %s",
106 host ? host : "(Null)",
107 db ? db : "(Null)",
108 user ? user : "(Null)"));
110 if (!host || !host[0])
111 host= mysql->options.host;
113 if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
114 (mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
115 host && *host && strcmp(host,LOCAL_HOST)))
116 DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
117 passwd, db, port,
118 unix_socket, client_flag));
120 mysql->methods= &embedded_methods;
122 /* use default options */
123 if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
125 mysql_read_default_options(&mysql->options,
126 (mysql->options.my_cnf_file ?
127 mysql->options.my_cnf_file : "my"),
128 mysql->options.my_cnf_group);
129 my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
130 my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
131 mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
134 if (!db || !db[0])
135 db=mysql->options.db;
137 if (!user || !user[0])
138 user=mysql->options.user;
140 #ifndef NO_EMBEDDED_ACCESS_CHECKS
141 if (!passwd)
143 passwd=mysql->options.password;
144 #if !defined(DONT_USE_MYSQL_PWD)
145 if (!passwd)
146 passwd=getenv("MYSQL_PWD"); /* get it from environment */
147 #endif
149 mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
150 #endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
151 if (!user || !user[0])
153 read_user_name(name_buff);
154 if (name_buff[0])
155 user= name_buff;
158 if (!user)
159 user= "";
161 We need to alloc some space for mysql->info but don't want to
162 put extra 'my_free's in mysql_close.
163 So we alloc it with the 'user' string to be freed at once
165 mysql->user= my_strdup(user, MYF(0));
167 port=0;
168 unix_socket=0;
170 client_flag|=mysql->options.client_flag;
171 /* Send client information for access check */
172 client_flag|=CLIENT_CAPABILITIES;
173 if (client_flag & CLIENT_MULTI_STATEMENTS)
174 client_flag|= CLIENT_MULTI_RESULTS;
175 client_flag&= ~CLIENT_COMPRESS;
176 if (db)
177 client_flag|=CLIENT_CONNECT_WITH_DB;
179 mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
180 mysql->thd= create_embedded_thd(client_flag);
182 init_embedded_mysql(mysql, client_flag);
184 if (mysql_init_character_set(mysql))
185 goto error;
187 if (check_embedded_connection(mysql, db))
188 goto error;
190 mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
192 if (mysql->options.init_commands)
194 DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
195 char **ptr= (char**)init_commands->buffer;
196 char **end= ptr + init_commands->elements;
198 for (; ptr<end; ptr++)
200 MYSQL_RES *res;
201 if (mysql_query(mysql,*ptr))
202 goto error;
203 if (mysql->fields)
205 if (!(res= (*mysql->methods->use_result)(mysql)))
206 goto error;
207 mysql_free_result(res);
212 DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql));
213 DBUG_RETURN(mysql);
215 error:
216 DBUG_PRINT("error",("message: %u (%s)",
217 mysql->net.last_errno,
218 mysql->net.last_error));
220 /* Free alloced memory */
221 my_bool free_me=mysql->free_me;
222 end_server(mysql);
223 mysql->free_me=0;
224 mysql_close(mysql);
225 mysql->free_me=free_me;
227 DBUG_RETURN(0);