~drizzle-trunk/drizzle/development

2472.1.1 by Brian Aker
Formatting, and valgrind cleanups (just mismatch of free/delete).
1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
3
 * Drizzle Client & Protocol Library
4
 *
5
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
6
 * All rights reserved.
7
 *
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are
10
 * met:
11
 *
12
 *     * Redistributions of source code must retain the above copyright
13
 * notice, this list of conditions and the following disclaimer.
14
 *
15
 *     * Redistributions in binary form must reproduce the above
16
 * copyright notice, this list of conditions and the following disclaimer
17
 * in the documentation and/or other materials provided with the
18
 * distribution.
19
 *
20
 *     * The names of its contributors may not be used to endorse or
21
 * promote products derived from this software without specific prior
22
 * written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
36
 */
37
38
/**
39
 * @file
40
 * @brief Drizzle Definitions
41
 */
42
2478.1.2 by Brian Aker
Fix race condition in bison build, also fix NULL issue on FreeBSD.
43
#include <libdrizzle/common.h>
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
44
45
/**
46
 * @addtogroup drizzle_static Static Drizzle Declarations
47
 * @ingroup drizzle
48
 * @{
49
 */
50
51
/**
52
 * Names of the verbose levels provided.
53
 */
54
static const char *_verbose_name[DRIZZLE_VERBOSE_MAX]=
55
{
56
  "NEVER",
57
  "FATAL",
58
  "ERROR",
59
  "INFO",
60
  "DEBUG",
61
  "CRAZY"
62
};
63
64
/** @} */
65
66
/*
67
 * Common Definitions
68
 */
69
70
const char *drizzle_version(void)
71
{
72
  return PACKAGE_VERSION;
73
}
74
75
const char *drizzle_bugreport(void)
76
{
77
  return PACKAGE_BUGREPORT;
78
}
79
80
const char *drizzle_verbose_name(drizzle_verbose_t verbose)
81
{
82
  if (verbose >= DRIZZLE_VERBOSE_MAX)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
83
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
84
    return "UNKNOWN";
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
85
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
86
87
  return _verbose_name[verbose];
88
}
89
90
drizzle_st *drizzle_create(drizzle_st *drizzle)
91
{
92
#if defined(_WIN32)
93
  /* if it is MS windows, invoke WSAStartup */
94
  WSADATA wsaData;
95
  if ( WSAStartup( MAKEWORD(2,2), &wsaData ) != 0 )
96
    printf("Error at WSAStartup()\n");
2164.1.3 by Monty Taylor
Merged in various amounts of win32 fixes. Now is quite happy in vs10.
97
#else
1877.1.5 by Andrew Hutchings
Fix min() usage for 32bit
98
  struct sigaction act;
1912.2.1 by Andrew Hutchings
Keep dtr client connection alive during lengthy drizzleslap tests (made longer when using valgrind)
99
  memset(&act, 0, sizeof(act));
1877.1.5 by Andrew Hutchings
Fix min() usage for 32bit
100
101
  act.sa_handler = SIG_IGN;
102
  sigaction(SIGPIPE, &act, NULL);
2164.1.3 by Monty Taylor
Merged in various amounts of win32 fixes. Now is quite happy in vs10.
103
#endif
1877.1.5 by Andrew Hutchings
Fix min() usage for 32bit
104
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
105
  if (drizzle == NULL)
106
  {
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
107
    drizzle= new (std::nothrow) drizzle_st;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
108
    if (drizzle == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
109
    {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
110
      return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
111
    }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
112
113
    drizzle->options= DRIZZLE_ALLOCATED;
114
  }
115
  else
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
116
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
117
    drizzle->options= DRIZZLE_NONE;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
118
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
119
120
  /* @todo remove this default free flag with new API. */
121
  drizzle->options|= DRIZZLE_FREE_OBJECTS;
122
  drizzle->error_code= 0;
123
  /* drizzle->options set above */
124
  drizzle->verbose= DRIZZLE_VERBOSE_NEVER;
125
  drizzle->con_count= 0;
126
  drizzle->pfds_size= 0;
127
  drizzle->query_count= 0;
128
  drizzle->query_new= 0;
129
  drizzle->query_running= 0;
130
  drizzle->last_errno= 0;
131
  drizzle->timeout= -1;
132
  drizzle->con_list= NULL;
133
  drizzle->context= NULL;
134
  drizzle->context_free_fn= NULL;
135
  drizzle->event_watch_fn= NULL;
136
  drizzle->event_watch_context= NULL;
137
  drizzle->log_fn= NULL;
138
  drizzle->log_context= NULL;
139
  drizzle->pfds= NULL;
140
  drizzle->query_list= NULL;
141
  drizzle->sqlstate[0]= 0;
142
  drizzle->last_error[0]= 0;
143
144
  return drizzle;
145
}
146
147
drizzle_st *drizzle_clone(drizzle_st *drizzle, const drizzle_st *from)
148
{
149
  drizzle= drizzle_create(drizzle);
150
  if (drizzle == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
151
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
152
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
153
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
154
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
155
  drizzle->options|= (from->options & int(~DRIZZLE_ALLOCATED));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
156
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
157
  for (drizzle_con_st *con= from->con_list; con != NULL; con= con->next)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
158
  {
159
    if (drizzle_con_clone(drizzle, NULL, con) == NULL)
160
    {
161
      drizzle_free(drizzle);
162
      return NULL;
163
    }
164
  }
165
166
  return drizzle;
167
}
168
169
void drizzle_free(drizzle_st *drizzle)
170
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
171
  if (drizzle == NULL)
172
  {
173
    return;
174
  }
175
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
176
  if (drizzle->context != NULL && drizzle->context_free_fn != NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
177
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
178
    drizzle->context_free_fn(drizzle, drizzle->context);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
179
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
180
181
  if (drizzle->options & DRIZZLE_FREE_OBJECTS)
182
  {
183
    drizzle_con_free_all(drizzle);
184
    drizzle_query_free_all(drizzle);
185
  }
186
  else if (drizzle->options & DRIZZLE_ASSERT_DANGLING)
187
  {
188
    assert(drizzle->con_list == NULL);
2371.3.1 by Andrew Hutchings
Fix incorrect assert (fix is already in libdrizzle-2.0)
189
    assert(drizzle->query_list == NULL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
190
  }
191
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
192
  free(drizzle->pfds);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
193
194
  if (drizzle->options & DRIZZLE_ALLOCATED)
2472.1.1 by Brian Aker
Formatting, and valgrind cleanups (just mismatch of free/delete).
195
  {
196
    delete drizzle;
197
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
198
#if defined(_WIN32)
199
  /* if it is MS windows, invoke WSACleanup() at the end*/
200
  WSACleanup();
201
#endif
202
}
203
204
const char *drizzle_error(const drizzle_st *drizzle)
205
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
206
  if (drizzle == NULL)
207
  {
208
    return NULL;
209
  }
210
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
211
  return (const char *)drizzle->last_error;
212
}
213
214
int drizzle_errno(const drizzle_st *drizzle)
215
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
216
  if (drizzle == NULL)
217
  {
218
    return 0;
219
  }
220
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
221
  return drizzle->last_errno;
222
}
223
224
uint16_t drizzle_error_code(const drizzle_st *drizzle)
225
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
226
  if (drizzle == NULL)
227
  {
228
    return 0;
229
  }
230
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
231
  return drizzle->error_code;
232
}
233
234
const char *drizzle_sqlstate(const drizzle_st *drizzle)
235
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
236
  if (drizzle == NULL)
237
  {
238
    return NULL;
239
  }
240
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
241
  return drizzle->sqlstate;
242
}
243
1992.6.7 by Monty Taylor
Revert -Wc++-compat change.
244
drizzle_options_t drizzle_options(const drizzle_st *drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
245
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
246
  if (drizzle == NULL)
247
  {
248
    return drizzle_options_t();
249
  }
250
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
251
  return drizzle_options_t(drizzle->options);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
252
}
253
1992.6.7 by Monty Taylor
Revert -Wc++-compat change.
254
void drizzle_set_options(drizzle_st *drizzle, drizzle_options_t options)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
255
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
256
  if (drizzle == NULL)
257
  {
258
    return;
259
  }
260
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
261
  drizzle->options= options;
262
}
263
1992.6.7 by Monty Taylor
Revert -Wc++-compat change.
264
void drizzle_add_options(drizzle_st *drizzle, drizzle_options_t options)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
265
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
266
  if (drizzle == NULL)
267
  {
268
    return;
269
  }
270
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
271
  drizzle->options|= options;
272
}
273
274
void drizzle_remove_options(drizzle_st *drizzle, drizzle_options_t options)
275
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
276
  if (drizzle == NULL)
277
  {
278
    return;
279
  }
280
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
281
  drizzle->options&= ~options;
282
}
283
284
void *drizzle_context(const drizzle_st *drizzle)
285
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
286
  if (drizzle == NULL)
287
  {
288
    return NULL;
289
  }
290
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
291
  return drizzle->context;
292
}
293
294
void drizzle_set_context(drizzle_st *drizzle, void *context)
295
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
296
  if (drizzle == NULL)
297
  {
298
    return;
299
  }
300
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
301
  drizzle->context= context;
302
}
303
304
void drizzle_set_context_free_fn(drizzle_st *drizzle,
305
                                 drizzle_context_free_fn *function)
306
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
307
  if (drizzle == NULL)
308
  {
309
    return;
310
  }
311
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
312
  drizzle->context_free_fn= function;
313
}
314
315
int drizzle_timeout(const drizzle_st *drizzle)
316
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
317
  if (drizzle == NULL)
318
  {
319
    return -1;
320
  }
321
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
322
  return drizzle->timeout;
323
}
324
325
void drizzle_set_timeout(drizzle_st *drizzle, int timeout)
326
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
327
  if (drizzle == NULL)
328
  {
329
    return;
330
  }
331
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
332
  drizzle->timeout= timeout;
333
}
334
335
drizzle_verbose_t drizzle_verbose(const drizzle_st *drizzle)
336
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
337
  if (drizzle == NULL)
338
  {
339
    return drizzle_verbose_t();
340
  }
341
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
342
  return drizzle->verbose;
343
}
344
345
void drizzle_set_verbose(drizzle_st *drizzle, drizzle_verbose_t verbose)
346
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
347
  if (drizzle == NULL)
348
  {
349
    return;
350
  }
351
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
352
  drizzle->verbose= verbose;
353
}
354
355
void drizzle_set_log_fn(drizzle_st *drizzle, drizzle_log_fn *function,
356
                        void *context)
357
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
358
  if (drizzle == NULL)
359
  {
360
    return;
361
  }
362
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
363
  drizzle->log_fn= function;
364
  drizzle->log_context= context;
365
}
366
367
void drizzle_set_event_watch_fn(drizzle_st *drizzle,
368
                                drizzle_event_watch_fn *function,
369
                                void *context)
370
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
371
  if (drizzle == NULL)
372
  {
373
    return;
374
  }
375
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
376
  drizzle->event_watch_fn= function;
377
  drizzle->event_watch_context= context;
378
}
379
380
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle, drizzle_con_st *con)
381
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
382
  if (drizzle == NULL)
383
  {
384
    return NULL;
385
  }
386
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
387
  if (con == NULL)
388
  {
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
389
    con= new (std::nothrow) drizzle_con_st;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
390
    if (con == NULL)
391
    {
392
      if (drizzle != NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
393
      {
2472.1.3 by Brian Aker
Mark all cases where you are using realloc.
394
        drizzle_set_error(drizzle, __func__, "Failed to allocate.");
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
395
      }
396
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
397
      return NULL;
398
    }
399
400
    con->options= DRIZZLE_CON_ALLOCATED;
401
  }
402
  else
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
403
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
404
    con->options= 0;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
405
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
406
407
  if (drizzle->con_list != NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
408
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
409
    drizzle->con_list->prev= con;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
410
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
411
  con->next= drizzle->con_list;
412
  con->prev= NULL;
413
  drizzle->con_list= con;
414
  drizzle->con_count++;
415
416
  con->packet_number= 0;
417
  con->protocol_version= 0;
418
  con->state_current= 0;
419
  con->events= 0;
420
  con->revents= 0;
421
  con->capabilities= DRIZZLE_CAPABILITIES_NONE;
422
  con->charset= 0;
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
423
  con->command= drizzle_command_t();
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
424
  con->options|= DRIZZLE_CON_MYSQL;
425
  con->socket_type= DRIZZLE_CON_SOCKET_TCP;
426
  con->status= DRIZZLE_CON_STATUS_NONE;
427
  con->max_packet_size= DRIZZLE_MAX_PACKET_SIZE;
428
  con->result_count= 0;
429
  con->thread_id= 0;
430
  con->backlog= DRIZZLE_DEFAULT_BACKLOG;
431
  con->fd= -1;
432
  con->buffer_size= 0;
433
  con->command_offset= 0;
434
  con->command_size= 0;
435
  con->command_total= 0;
436
  con->packet_size= 0;
437
  con->addrinfo_next= NULL;
438
  con->buffer_ptr= con->buffer;
439
  con->command_buffer= NULL;
440
  con->command_data= NULL;
441
  con->context= NULL;
442
  con->context_free_fn= NULL;
443
  con->drizzle= drizzle;
444
  /* con->next set above */
445
  /* con->prev set above */
446
  con->query= NULL;
447
  /* con->result doesn't need to be set */
448
  con->result_list= NULL;
449
  con->scramble= NULL;
450
  con->socket.tcp.addrinfo= NULL;
451
  con->socket.tcp.host= NULL;
452
  con->socket.tcp.port= 0;
453
  /* con->buffer doesn't need to be set */
454
  con->db[0]= 0;
455
  con->password[0]= 0;
456
  /* con->scramble_buffer doesn't need to be set */
457
  con->server_version[0]= 0;
458
  /* con->state_stack doesn't need to be set */
459
  con->user[0]= 0;
460
461
  return con;
462
}
463
464
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *con,
465
                                  const drizzle_con_st *from)
466
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
467
  if (drizzle == NULL)
468
  {
469
    return NULL;
470
  }
471
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
472
  con= drizzle_con_create(drizzle, con);
473
  if (con == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
474
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
475
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
476
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
477
478
  /* Clear "operational" options such as IO status. */
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
479
  con->options|= (from->options & int(~(
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
480
                  DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
481
                  DRIZZLE_CON_NO_RESULT_READ|DRIZZLE_CON_IO_READY|
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
482
                  DRIZZLE_CON_LISTEN)));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
483
  con->backlog= from->backlog;
484
  strcpy(con->db, from->db);
485
  strcpy(con->password, from->password);
486
  strcpy(con->user, from->user);
487
488
  switch (from->socket_type)
489
  {
490
  case DRIZZLE_CON_SOCKET_TCP:
491
    drizzle_con_set_tcp(con, from->socket.tcp.host, from->socket.tcp.port);
492
    break;
493
494
  case DRIZZLE_CON_SOCKET_UDS:
495
    drizzle_con_set_uds(con, from->socket.uds.sockaddr.sun_path);
496
    break;
497
498
  default:
499
    break;
500
  }
501
502
  return con;
503
}
504
505
void drizzle_con_free(drizzle_con_st *con)
506
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
507
  if (con == NULL)
508
  {
509
    return;
510
  }
511
512
  if (con->context != NULL and con->context_free_fn != NULL)
513
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
514
    con->context_free_fn(con, con->context);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
515
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
516
517
  if (con->drizzle->options & DRIZZLE_FREE_OBJECTS)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
518
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
519
    drizzle_result_free_all(con);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
520
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
521
  else if (con->drizzle->options & DRIZZLE_ASSERT_DANGLING)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
522
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
523
    assert(con->result_list == NULL);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
524
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
525
526
  if (con->fd != -1)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
527
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
528
    drizzle_con_close(con);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
529
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
530
531
  drizzle_con_reset_addrinfo(con);
532
533
  if (con->drizzle->con_list == con)
534
    con->drizzle->con_list= con->next;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
535
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
536
  if (con->prev != NULL)
537
    con->prev->next= con->next;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
538
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
539
  if (con->next != NULL)
540
    con->next->prev= con->prev;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
541
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
542
  con->drizzle->con_count--;
543
544
  if (con->options & DRIZZLE_CON_ALLOCATED)
2472.1.1 by Brian Aker
Formatting, and valgrind cleanups (just mismatch of free/delete).
545
  {
546
    delete con;
547
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
548
}
549
550
void drizzle_con_free_all(drizzle_st *drizzle)
551
{
552
  while (drizzle->con_list != NULL)
553
    drizzle_con_free(drizzle->con_list);
554
}
555
556
drizzle_return_t drizzle_con_wait(drizzle_st *drizzle)
557
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
558
  if (drizzle == NULL)
559
  {
560
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
561
  }
562
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
563
  struct pollfd *pfds;
564
  if (drizzle->pfds_size < drizzle->con_count)
565
  {
2164.1.4 by Monty Taylor
We need this marked with struct on linux.
566
    pfds= (struct pollfd *)realloc(drizzle->pfds, drizzle->con_count * sizeof(struct pollfd));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
567
    if (pfds == NULL)
568
    {
2472.1.3 by Brian Aker
Mark all cases where you are using realloc.
569
      drizzle_set_error(drizzle, __func__, "Failed to realloc file descriptors.");
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
570
      return DRIZZLE_RETURN_MEMORY;
571
    }
572
573
    drizzle->pfds= pfds;
574
    drizzle->pfds_size= drizzle->con_count;
575
  }
576
  else
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
577
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
578
    pfds= drizzle->pfds;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
579
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
580
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
581
  uint32_t x= 0;
582
  for (drizzle_con_st *con= drizzle->con_list; con != NULL; con= con->next)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
583
  {
584
    if (con->events == 0)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
585
    {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
586
      continue;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
587
    }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
588
589
    pfds[x].fd= con->fd;
590
    pfds[x].events= con->events;
591
    pfds[x].revents= 0;
592
    x++;
593
  }
594
595
  if (x == 0)
596
  {
597
    drizzle_set_error(drizzle, "drizzle_con_wait",
598
                      "no active file descriptors");
599
    return DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS;
600
  }
601
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
602
  int ret;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
603
  while (1)
604
  {
605
    drizzle_log_crazy(drizzle, "poll count=%d timeout=%d", x,
606
                      drizzle->timeout);
607
608
    ret= poll(pfds, x, drizzle->timeout);
609
610
    drizzle_log_crazy(drizzle, "poll return=%d errno=%d", ret, errno);
611
612
    if (ret == -1)
613
    {
614
      if (errno == EINTR)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
615
      {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
616
        continue;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
617
      }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
618
619
      drizzle_set_error(drizzle, "drizzle_con_wait", "poll:%d", errno);
620
      drizzle->last_errno= errno;
621
      return DRIZZLE_RETURN_ERRNO;
622
    }
623
624
    break;
625
  }
626
627
  if (ret == 0)
628
  {
629
    drizzle_set_error(drizzle, "drizzle_con_wait", "timeout reached");
630
    return DRIZZLE_RETURN_TIMEOUT;
631
  }
632
633
  x= 0;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
634
  for (drizzle_con_st *con= drizzle->con_list; con != NULL; con= con->next)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
635
  {
636
    if (con->events == 0)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
637
    {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
638
      continue;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
639
    }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
640
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
641
    drizzle_return_t dret= drizzle_con_set_revents(con, pfds[x].revents);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
642
    if (dret != DRIZZLE_RETURN_OK)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
643
    {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
644
      return dret;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
645
    }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
646
647
    x++;
648
  }
649
650
  return DRIZZLE_RETURN_OK;
651
}
652
653
drizzle_con_st *drizzle_con_ready(drizzle_st *drizzle)
654
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
655
  if (drizzle == NULL)
656
  {
657
    return NULL;
658
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
659
660
  /* We can't keep state between calls since connections may be removed during
661
     processing. If this list ever gets big, we may want something faster. */
662
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
663
  for (drizzle_con_st *con= drizzle->con_list; con != NULL; con= con->next)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
664
  {
665
    if (con->options & DRIZZLE_CON_IO_READY)
666
    {
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
667
      con->options&= int(~DRIZZLE_CON_IO_READY);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
668
      return con;
669
    }
670
  }
671
672
  return NULL;
673
}
674
675
drizzle_con_st *drizzle_con_ready_listen(drizzle_st *drizzle)
676
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
677
  if (drizzle == NULL)
678
  {
679
    return NULL;
680
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
681
682
  /* We can't keep state between calls since connections may be removed during
683
     processing. If this list ever gets big, we may want something faster. */
684
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
685
  for (drizzle_con_st *con= drizzle->con_list; con != NULL; con= con->next)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
686
  {
687
    if ((con->options & (DRIZZLE_CON_IO_READY | DRIZZLE_CON_LISTEN)) ==
688
        (DRIZZLE_CON_IO_READY | DRIZZLE_CON_LISTEN))
689
    {
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
690
      con->options&= int(~DRIZZLE_CON_IO_READY);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
691
      return con;
692
    }
693
  }
694
695
  return NULL;
696
}
697
698
/*
699
 * Client Definitions
700
 */
701
702
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle, drizzle_con_st *con,
703
                                    const char *host, in_port_t port,
704
                                    const char *user, const char *password,
705
                                    const char *db,
706
                                    drizzle_con_options_t options)
707
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
708
  if (drizzle == NULL)
709
  {
710
    return NULL;
711
  }
712
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
713
  con= drizzle_con_create(drizzle, con);
714
  if (con == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
715
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
716
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
717
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
718
719
  drizzle_con_set_tcp(con, host, port);
720
  drizzle_con_set_auth(con, user, password);
721
  drizzle_con_set_db(con, db);
722
  drizzle_con_add_options(con, options);
723
724
  return con;
725
}
726
727
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle, drizzle_con_st *con,
728
                                    const char *uds, const char *user,
729
                                    const char *password, const char *db,
730
                                    drizzle_con_options_t options)
731
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
732
  if (drizzle == NULL)
733
  {
734
    return NULL;
735
  }
736
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
737
  con= drizzle_con_create(drizzle, con);
738
  if (con == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
739
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
740
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
741
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
742
743
  drizzle_con_set_uds(con, uds);
744
  drizzle_con_set_auth(con, user, password);
745
  drizzle_con_set_db(con, db);
746
  drizzle_con_add_options(con, options);
747
748
  return con;
749
}
750
751
/*
752
 * Server Definitions
753
 */
754
755
drizzle_con_st *drizzle_con_add_tcp_listen(drizzle_st *drizzle,
756
                                           drizzle_con_st *con,
757
                                           const char *host, in_port_t port,
758
                                           int backlog,
759
                                           drizzle_con_options_t options)
760
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
761
  if (drizzle == NULL)
762
  {
763
    return NULL;
764
  }
765
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
766
  con= drizzle_con_create(drizzle, con);
767
  if (con == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
768
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
769
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
770
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
771
772
  drizzle_con_set_tcp(con, host, port);
773
  drizzle_con_set_backlog(con, backlog);
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
774
  drizzle_con_add_options(con, drizzle_con_options_t(DRIZZLE_CON_LISTEN | int(options)));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
775
776
  return con;
777
}
778
779
drizzle_con_st *drizzle_con_add_uds_listen(drizzle_st *drizzle,
780
                                           drizzle_con_st *con,
781
                                           const char *uds, int backlog,
782
                                           drizzle_con_options_t options)
783
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
784
  if (drizzle == NULL)
785
  {
786
    return NULL;
787
  }
788
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
789
  con= drizzle_con_create(drizzle, con);
790
  if (con == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
791
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
792
    return NULL;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
793
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
794
795
  drizzle_con_set_uds(con, uds);
796
  drizzle_con_set_backlog(con, backlog);
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
797
  drizzle_con_add_options(con, drizzle_con_options_t(DRIZZLE_CON_LISTEN | int(options)));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
798
799
  return con;
800
}
801
802
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle, drizzle_con_st *con,
803
                                   drizzle_return_t *ret_ptr)
804
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
805
  drizzle_return_t unused_ret;
806
  if (ret_ptr == NULL)
807
  {
808
    ret_ptr= &unused_ret;
809
  }
810
811
  if (drizzle == NULL)
812
  {
813
    return NULL;
814
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
815
816
  while (1)
817
  {
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
818
    drizzle_con_st *ready;
819
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
820
    if ((ready= drizzle_con_ready_listen(drizzle)) != NULL)
821
    {
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
822
      int fd= accept(ready->fd, NULL, NULL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
823
824
      con= drizzle_con_create(drizzle, con);
825
      if (con == NULL)
826
      {
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
827
        (void)closesocket(fd);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
828
        *ret_ptr= DRIZZLE_RETURN_MEMORY;
829
        return NULL;
830
      }
831
832
      *ret_ptr= drizzle_con_set_fd(con, fd);
833
      if (*ret_ptr != DRIZZLE_RETURN_OK)
834
      {
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
835
        (void)closesocket(fd);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
836
        return NULL;
837
      }
838
839
      if (ready->options & DRIZZLE_CON_MYSQL)
840
        drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
841
842
      *ret_ptr= DRIZZLE_RETURN_OK;
843
      return con;
844
    }
845
846
    if (drizzle->options & DRIZZLE_NON_BLOCKING)
847
    {
848
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
849
      return NULL;
850
    }
851
852
    for (ready= drizzle->con_list; ready != NULL; ready= ready->next)
853
    {
854
      if (ready->options & DRIZZLE_CON_LISTEN)
855
        drizzle_con_set_events(ready, POLLIN);
856
    }
857
858
    *ret_ptr= drizzle_con_wait(drizzle);
859
    if (*ret_ptr != DRIZZLE_RETURN_OK)
860
      return NULL;
861
  }
862
}
863
864
/*
865
 * Local Definitions
866
 */
867
868
void drizzle_set_error(drizzle_st *drizzle, const char *function,
869
                       const char *format, ...)
870
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
871
  if (drizzle == NULL)
872
  {
873
    return;
874
  }
875
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
876
  size_t size;
2269.2.2 by Marc Isambart
Use int to store result of printf instead of size_t
877
  int written;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
878
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
879
  va_list args;
880
881
  size= strlen(function);
2464.1.2 by Brian Aker
Fix compiling issues for 1.0, and cleanup header files.
882
  char *ptr= (char *)memcpy(log_buffer, function, size);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
883
  ptr+= size;
884
  ptr[0]= ':';
885
  size++;
886
  ptr++;
887
888
  va_start(args, format);
2269.2.2 by Marc Isambart
Use int to store result of printf instead of size_t
889
  written= vsnprintf(ptr, DRIZZLE_MAX_ERROR_SIZE - size, format, args);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
890
  va_end(args);
891
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
892
  if (written < 0)
893
  {
894
    size= DRIZZLE_MAX_ERROR_SIZE;
895
  }
896
  else
897
  {
898
    size+= written;
899
  }
900
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
901
  if (size >= DRIZZLE_MAX_ERROR_SIZE)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
902
  {
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
903
    size= DRIZZLE_MAX_ERROR_SIZE - 1;
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
904
  }
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
905
  log_buffer[size]= 0;
906
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
907
  if (drizzle->log_fn == NULL)
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
908
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
909
    memcpy(drizzle->last_error, log_buffer, size + 1);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
910
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
911
  else
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
912
  {
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
913
    drizzle->log_fn(log_buffer, DRIZZLE_VERBOSE_ERROR, drizzle->log_context);
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
914
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
915
}
916
917
void drizzle_log(drizzle_st *drizzle, drizzle_verbose_t verbose,
918
                 const char *format, va_list args)
919
{
2472.1.2 by Brian Aker
Null safety fix for libdrizzle
920
  if (drizzle == NULL)
921
  {
922
    return;
923
  }
924
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
925
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
926
927
  if (drizzle->log_fn == NULL)
928
  {
929
    printf("%5s: ", drizzle_verbose_name(verbose));
930
    vprintf(format, args);
931
    printf("\n");
932
  }
933
  else
934
  {
935
    vsnprintf(log_buffer, DRIZZLE_MAX_ERROR_SIZE, format, args);
2269.2.1 by Marc Isambart
Various libdrizzle Windows fixes, including closesocket() instead of close(), snprintf handling and WSAECONNREFUSED mapping
936
    log_buffer[DRIZZLE_MAX_ERROR_SIZE-1]= 0;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
937
    drizzle->log_fn(log_buffer, verbose, drizzle->log_context);
938
  }
939
}