~michaeleguo/ubuntu/trusty/percona-xtradb-cluster-5.5/arm64fix

« back to all changes in this revision

Viewing changes to libmysqld/libmysqld.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-10 14:44:23 UTC
  • Revision ID: package-import@ubuntu.com-20140210144423-f2134l2gxuvq2m6l
Tags: upstream-5.5.34-25.9+dfsg
ImportĀ upstreamĀ versionĀ 5.5.34-25.9+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2001, 2010, 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
#include <sys/socket.h>
 
37
#include <netinet/in.h>
 
38
#include <arpa/inet.h>
 
39
#include <netdb.h>
 
40
#ifdef HAVE_SELECT_H
 
41
#  include <select.h>
 
42
#endif
 
43
#ifdef HAVE_SYS_SELECT_H
 
44
#include <sys/select.h>
 
45
#endif
 
46
#endif
 
47
#ifdef HAVE_SYS_UN_H
 
48
#  include <sys/un.h>
 
49
#endif
 
50
#ifndef INADDR_NONE
 
51
#define INADDR_NONE     -1
 
52
#endif
 
53
 
 
54
extern ulong net_buffer_length;
 
55
extern ulong max_allowed_packet;
 
56
 
 
57
#if defined(__WIN__)
 
58
#define ERRNO WSAGetLastError()
 
59
#define perror(A)
 
60
#else
 
61
#include <errno.h>
 
62
#define ERRNO errno
 
63
#define SOCKET_ERROR -1
 
64
#define closesocket(A) close(A)
 
65
#endif
 
66
 
 
67
#ifdef HAVE_GETPWUID
 
68
struct passwd *getpwuid(uid_t);
 
69
char* getlogin(void);
 
70
#endif
 
71
 
 
72
#ifdef __WIN__
 
73
static my_bool is_NT(void)
 
74
{
 
75
  char *os=getenv("OS");
 
76
  return (os && !strcmp(os, "Windows_NT")) ? 1 : 0;
 
77
}
 
78
#endif
 
79
 
 
80
 
 
81
int mysql_init_character_set(MYSQL *mysql);
 
82
 
 
83
MYSQL * STDCALL
 
84
mysql_real_connect(MYSQL *mysql,const char *host, const char *user,
 
85
                   const char *passwd, const char *db,
 
86
                   uint port, const char *unix_socket,ulong client_flag)
 
87
{
 
88
  char name_buff[USERNAME_LENGTH];
 
89
 
 
90
  DBUG_ENTER("mysql_real_connect");
 
91
  DBUG_PRINT("enter",("host: %s  db: %s  user: %s (libmysqld)",
 
92
                      host ? host : "(Null)",
 
93
                      db ? db : "(Null)",
 
94
                      user ? user : "(Null)"));
 
95
 
 
96
  /* Test whether we're already connected */
 
97
  if (mysql->server_version)
 
98
  {
 
99
    set_mysql_error(mysql, CR_ALREADY_CONNECTED, unknown_sqlstate);
 
100
    DBUG_RETURN(0);
 
101
  }
 
102
 
 
103
  if (!host || !host[0])
 
104
    host= mysql->options.host;
 
105
 
 
106
  if (mysql->options.methods_to_use == MYSQL_OPT_USE_REMOTE_CONNECTION ||
 
107
      (mysql->options.methods_to_use == MYSQL_OPT_GUESS_CONNECTION &&
 
108
       host && *host && strcmp(host,LOCAL_HOST)))
 
109
    DBUG_RETURN(cli_mysql_real_connect(mysql, host, user, 
 
110
                                       passwd, db, port, 
 
111
                                       unix_socket, client_flag));
 
112
 
 
113
  mysql->methods= &embedded_methods;
 
114
 
 
115
  /* use default options */
 
116
  if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)
 
117
  {
 
118
    mysql_read_default_options(&mysql->options,
 
119
                               (mysql->options.my_cnf_file ?
 
120
                                mysql->options.my_cnf_file : "my"),
 
121
                               mysql->options.my_cnf_group);
 
122
    my_free(mysql->options.my_cnf_file);
 
123
    my_free(mysql->options.my_cnf_group);
 
124
    mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
 
125
  }
 
126
 
 
127
  if (!db || !db[0])
 
128
    db=mysql->options.db;
 
129
 
 
130
  if (!user || !user[0])
 
131
    user=mysql->options.user;
 
132
 
 
133
#ifndef NO_EMBEDDED_ACCESS_CHECKS
 
134
  if (!passwd)
 
135
  {
 
136
    passwd=mysql->options.password;
 
137
#if !defined(DONT_USE_MYSQL_PWD)
 
138
    if (!passwd)
 
139
      passwd=getenv("MYSQL_PWD");               /* get it from environment */
 
140
#endif
 
141
  }
 
142
  mysql->passwd= passwd ? my_strdup(passwd,MYF(0)) : NULL;
 
143
#endif /*!NO_EMBEDDED_ACCESS_CHECKS*/
 
144
  if (!user || !user[0])
 
145
  {
 
146
    read_user_name(name_buff);
 
147
    if (name_buff[0])
 
148
      user= name_buff;
 
149
  }
 
150
 
 
151
  if (!user)
 
152
    user= "";
 
153
   /* 
 
154
      We need to alloc some space for mysql->info but don't want to
 
155
      put extra 'my_free's in mysql_close.
 
156
      So we alloc it with the 'user' string to be freed at once
 
157
   */
 
158
  mysql->user= my_strdup(user, MYF(0));
 
159
 
 
160
  port=0;
 
161
  unix_socket=0;
 
162
 
 
163
  client_flag|=mysql->options.client_flag;
 
164
  /* Send client information for access check */
 
165
  client_flag|=CLIENT_CAPABILITIES;
 
166
  if (client_flag & CLIENT_MULTI_STATEMENTS)
 
167
    client_flag|= CLIENT_MULTI_RESULTS;
 
168
  /*
 
169
    no compression in embedded as we don't send any data,
 
170
    and no pluggable auth, as we cannot do a client-server dialog
 
171
  */
 
172
  client_flag&= ~(CLIENT_COMPRESS | CLIENT_PLUGIN_AUTH);
 
173
  if (db)
 
174
    client_flag|=CLIENT_CONNECT_WITH_DB;
 
175
 
 
176
  mysql->info_buffer= my_malloc(MYSQL_ERRMSG_SIZE, MYF(0));
 
177
  mysql->thd= create_embedded_thd(client_flag);
 
178
 
 
179
  init_embedded_mysql(mysql, client_flag);
 
180
 
 
181
  if (mysql_init_character_set(mysql))
 
182
    goto error;
 
183
 
 
184
  if (check_embedded_connection(mysql, db))
 
185
    goto error;
 
186
 
 
187
  mysql->server_status= SERVER_STATUS_AUTOCOMMIT;
 
188
 
 
189
  if (mysql->options.init_commands)
 
190
  {
 
191
    DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
 
192
    char **ptr= (char**)init_commands->buffer;
 
193
    char **end= ptr + init_commands->elements;
 
194
 
 
195
    for (; ptr<end; ptr++)
 
196
    {
 
197
      MYSQL_RES *res;
 
198
      if (mysql_query(mysql,*ptr))
 
199
        goto error;
 
200
      if (mysql->fields)
 
201
      {
 
202
        if (!(res= (*mysql->methods->use_result)(mysql)))
 
203
          goto error;
 
204
        mysql_free_result(res);
 
205
      }
 
206
    }
 
207
  }
 
208
 
 
209
  DBUG_PRINT("exit",("Mysql handler: 0x%lx", (long) mysql));
 
210
  DBUG_RETURN(mysql);
 
211
 
 
212
error:
 
213
  DBUG_PRINT("error",("message: %u (%s)",
 
214
                      mysql->net.last_errno,
 
215
                      mysql->net.last_error));
 
216
  {
 
217
    /* Free alloced memory */
 
218
    my_bool free_me=mysql->free_me;
 
219
    free_old_query(mysql); 
 
220
    mysql->free_me=0;
 
221
    mysql_close(mysql);
 
222
    mysql->free_me=free_me;
 
223
  }
 
224
  DBUG_RETURN(0);
 
225
}
 
226