MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
libmysqld.c
1 /* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2 
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; version 2 of the License.
6 
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 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include <my_global.h>
17 #include <mysql.h>
18 #include <mysqld_error.h>
19 #include <my_pthread.h>
20 #include <my_sys.h>
21 #include <mysys_err.h>
22 #include <m_string.h>
23 #include <m_ctype.h>
24 #include "errmsg.h"
25 #include <violite.h>
26 #include <sys/stat.h>
27 #include <signal.h>
28 #include <time.h>
29 #include <sql_common.h>
30 #include "embedded_priv.h"
31 #include "client_settings.h"
32 #ifdef HAVE_PWD_H
33 #include <pwd.h>
34 #endif
35 #if !defined(__WIN__)
36 #ifdef HAVE_SELECT_H
37 # include <select.h>
38 #endif
39 #ifdef HAVE_SYS_SELECT_H
40 #include <sys/select.h>
41 #endif
42 #endif
43 #ifdef HAVE_SYS_UN_H
44 # include <sys/un.h>
45 #endif
46 #ifndef INADDR_NONE
47 #define INADDR_NONE -1
48 #endif
49 
50 extern ulong net_buffer_length;
51 extern ulong max_allowed_packet;
52 
53 #if defined(__WIN__)
54 #define ERRNO WSAGetLastError()
55 #define perror(A)
56 #else
57 #include <errno.h>
58 #define ERRNO errno
59 #define SOCKET_ERROR -1
60 #define closesocket(A) close(A)
61 #endif
62 
63 #ifdef HAVE_GETPWUID
64 struct passwd *getpwuid(uid_t);
65 char* getlogin(void);
66 #endif
67 
68 #ifdef __WIN__
69 static my_bool is_NT(void)
70 {
71  char *os=getenv("OS");
72  return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
73 }
74 #endif
75 
76 
77 int mysql_init_character_set(MYSQL *mysql);
78 
79 MYSQL * STDCALL
80 mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
81  const char *passwd, const char *db,
82  uint port, const char *unix_socket,ulong client_flag)
83 {
84  char name_buff[USERNAME_LENGTH + 1];
85 
86  DBUG_ENTER("mysql_real_connect");
87  DBUG_PRINT("enter",("host: %s db: %s user: %s (libmysqld)",
88  host ? host : "(Null)",
89  db ? db : "(Null)",
90  user ? user : "(Null)"));
91 
92  /* Test whether we're already connected */
93  if (mysql->server_version)
94  {
95  set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
96  DBUG_RETURN(0);
97  }
98 
99  if (!host || !host[0])
100  host= mysql->options.host;
101 
102  if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
103  (mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
104  host && *host && strcmp(host,LOCAL_HOST)))
105  DBUG_RETURN(cli_mysql_real_connect(mysql, host, user,
106  passwd, db, port,
107  unix_socket, client_flag));
108 
109  mysql->methods= &embedded_methods;
110 
111  /* use default options */
112  if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
113  {
114  mysql_read_default_options(&mysql->options,
115  (mysql->options.my_cnf_file ?
116  mysql->options.my_cnf_file : "my"),
117  mysql->options.my_cnf_group);
118  my_free(mysql->options.my_cnf_file);
119  my_free(mysql->options.my_cnf_group);
120  mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
121  }
122 
123  if (!db || !db[0])
124  db=mysql->options.db;
125 
126  if (!user || !user[0])
127  user=mysql->options.user;
128 
129 #ifndef NO_EMBEDDED_ACCESS_CHECKS
130  if (!passwd)
131  {
132  passwd=mysql->options.password;
133 #if !defined(DONT_USE_MYSQL_PWD)
134  if (!passwd)
135  passwd=getenv("MYSQL_PWD"); /* get it from environment */
136 #endif
137  }
138  mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
139 #endif
140  if (!user || !user[0])
141  {
142  read_user_name(name_buff);
143  if (name_buff[0])
144  user= name_buff;
145  }
146 
147  if (!user)
148  user= "";
149  /*
150  We need to alloc some space for mysql->info but don't want to
151  put extra 'my_free's in mysql_close.
152  So we alloc it with the 'user' string to be freed at once
153  */
154  mysql->user= my_strdup(user, MYF(0));
155 
156  port=0;
157  unix_socket=0;
158 
159  client_flag|=mysql->options.client_flag;
160  /* Send client information for access check */
161  client_flag|=CLIENT_CAPABILITIES;
162  if (client_flag & CLIENT_MULTI_STATEMENTS)
163  client_flag|= CLIENT_MULTI_RESULTS;
164  /*
165  no compression in embedded as we don't send any data,
166  and no pluggable auth, as we cannot do a client-server dialog
167  */
168  client_flag&= ~(CLIENT_COMPRESS | CLIENT_PLUGIN_AUTH);
169  if (db)
170  client_flag|=CLIENT_CONNECT_WITH_DB;
171 
172  mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
173  mysql->thd= create_embedded_thd(client_flag);
174 
175  init_embedded_mysql(mysql, client_flag);
176 
177  if (mysql_init_character_set(mysql))
178  goto error;
179 
180  if (check_embedded_connection(mysql, db))
181  goto error;
182 
183  mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
184 
185  if (mysql->options.init_commands)
186  {
187  DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
188  char **ptr= (char**)init_commands->buffer;
189  char **end= ptr + init_commands->elements;
190 
191  for (; ptr<end; ptr++)
192  {
193  MYSQL_RES *res;
194  if (mysql_query(mysql,*ptr))
195  goto error;
196  if (mysql->fields)
197  {
198  if (!(res= (*mysql->methods->use_result)(mysql)))
199  goto error;
200  mysql_free_result(res);
201  }
202  }
203  }
204 
205  DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql));
206  DBUG_RETURN(mysql);
207 
208 error:
209  DBUG_PRINT("error",("message: %u (%s)",
210  mysql->net.last_errno,
211  mysql->net.last_error));
212  {
213  /* Free alloced memory */
214  my_bool free_me=mysql->free_me;
215  free_old_query(mysql);
216  mysql->free_me=0;
217  mysql_close(mysql);
218  mysql->free_me=free_me;
219  }
220  DBUG_RETURN(0);
221 }
222