~drizzle-trunk/drizzle/development

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