~drizzle-trunk/libdrizzle/jenkins-Libdrizzle-27

« back to all changes in this revision

Viewing changes to libdrizzle/conn.cc

  • Committer: Andrew Hutchings
  • Date: 2012-12-21 13:28:25 UTC
  • mfrom: (43.2.16 5.0)
  • Revision ID: andrew@linuxjedi.co.uk-20121221132825-77cko8ln7jj9kpb6
Merge 5.0 branch to 5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
  size_t loop_max= 5;
73
73
  while (--loop_max) // Should only loop on cases of ERESTART or EINTR
74
74
  {
75
 
    int error= poll(fds, 1, con->drizzle->timeout);
 
75
    int error= poll(fds, 1, con->timeout);
76
76
    switch (error)
77
77
    {
78
78
    case 1:
174
174
  (void)closesocket(con->fd);
175
175
  con->fd= -1;
176
176
 
177
 
  con->options&= ~DRIZZLE_CON_READY;
 
177
  con->options = (drizzle_con_options_t)((int)con->options & (int)~DRIZZLE_CON_READY);
178
178
  con->packet_number= 0;
179
179
  con->buffer_ptr= con->buffer;
180
180
  con->buffer_size= 0;
193
193
 
194
194
  con->events|= events;
195
195
 
196
 
  if (con->drizzle->event_watch_fn != NULL)
197
 
  {
198
 
    drizzle_return_t ret;
199
 
    ret= con->drizzle->event_watch_fn(con, con->events,
200
 
                                      con->drizzle->event_watch_context);
201
 
    if (ret != DRIZZLE_RETURN_OK)
202
 
    {
203
 
      drizzle_con_close(con);
204
 
      return ret;
205
 
    }
206
 
  }
207
 
 
208
196
  return DRIZZLE_RETURN_OK;
209
197
}
210
198
 
216
204
  }
217
205
 
218
206
  if (revents != 0)
219
 
    con->options|= DRIZZLE_CON_IO_READY;
 
207
    con->options = (drizzle_con_options_t)((int)con->options | (int)DRIZZLE_CON_IO_READY);
220
208
 
221
209
  con->revents= revents;
222
210
 
223
 
  /* Remove external POLLOUT watch if we didn't ask for it. Otherwise we spin
224
 
     forever until another POLLIN state change. This is much more efficient
225
 
     than removing POLLOUT on every state change since some external polling
226
 
     mechanisms need to use a system call to change flags (like Linux epoll). */
227
 
  if (revents & POLLOUT && !(con->events & POLLOUT) &&
228
 
      con->drizzle->event_watch_fn != NULL)
229
 
  {
230
 
    drizzle_return_t ret;
231
 
    ret= con->drizzle->event_watch_fn(con, con->events,
232
 
                                      con->drizzle->event_watch_context);
233
 
    if (ret != DRIZZLE_RETURN_OK)
234
 
    {
235
 
      drizzle_con_close(con);
236
 
      return ret;
237
 
    }
238
 
  }
239
 
 
240
211
  con->events&= (short)~revents;
241
212
 
242
213
  return DRIZZLE_RETURN_OK;
243
214
}
244
215
 
245
 
drizzle_st *drizzle_con_drizzle(const drizzle_con_st *con)
246
 
{
247
 
  if (con == NULL)
248
 
  {
249
 
    return NULL;
250
 
  }
251
 
  return con->drizzle;
252
 
}
253
216
 
254
217
const char *drizzle_con_error(const drizzle_con_st *con)
255
218
{
258
221
    return NULL;
259
222
  }
260
223
 
261
 
  return drizzle_error(con->drizzle);
 
224
  return (const char *)con->last_error;
262
225
}
263
226
 
264
227
int drizzle_con_errno(const drizzle_con_st *con)
268
231
    return 0;
269
232
  }
270
233
 
271
 
  return drizzle_errno(con->drizzle);
 
234
  return con->last_errno;
272
235
}
273
236
 
274
237
uint16_t drizzle_con_error_code(const drizzle_con_st *con)
278
241
    return 0;
279
242
  }
280
243
 
281
 
  return drizzle_error_code(con->drizzle);
 
244
  return con->error_code;
282
245
}
283
246
 
284
247
const char *drizzle_con_sqlstate(const drizzle_con_st *con)
288
251
    return NULL;
289
252
  }
290
253
 
291
 
  return drizzle_sqlstate(con->drizzle);
 
254
  return con->sqlstate;
292
255
}
293
256
 
294
257
drizzle_con_options_t drizzle_con_options(const drizzle_con_st *con)
320
283
    return;
321
284
  }
322
285
 
323
 
  con->options|= options;
 
286
  con->options = (drizzle_con_options_t)((int)con->options | (int)options);
324
287
}
325
288
 
326
289
void drizzle_con_remove_options(drizzle_con_st *con,
331
294
    return;
332
295
  }
333
296
 
334
 
  con->options&= ~options;
 
297
  con->options = (drizzle_con_options_t)((int) con->options & (int)~options);
335
298
}
336
299
 
337
300
const char *drizzle_con_host(const drizzle_con_st *con)
700
663
  {
701
664
    if (con->options & DRIZZLE_CON_RAW_PACKET)
702
665
    {
703
 
      drizzle_set_error(con->drizzle, "drizzle_command_write",
 
666
      drizzle_con_set_error(con, "drizzle_command_write",
704
667
                        "connection not ready");
705
668
      *ret_ptr= DRIZZLE_RETURN_NOT_READY;
706
669
      return result;
725
688
      {
726
689
        if (result == old_result)
727
690
        {
728
 
          drizzle_set_error(con->drizzle, "drizzle_command_write", "result struct already in use");
 
691
          drizzle_con_set_error(con, "drizzle_command_write", "result struct already in use");
729
692
          *ret_ptr= DRIZZLE_RETURN_INTERNAL_ERROR;
730
693
          return result;
731
694
        }
818
781
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
819
782
  }
820
783
 
821
 
  drizzle_log_debug(con->drizzle, __func__);
 
784
  drizzle_log_debug(con, __func__);
822
785
 
823
786
  switch (con->socket_type)
824
787
  {
859
822
    ret= getaddrinfo(host, port, &ai, &(tcp->addrinfo));
860
823
    if (ret != 0)
861
824
    {
862
 
      drizzle_set_error(con->drizzle, "drizzle_state_addrinfo", "getaddrinfo:%s", gai_strerror(ret));
 
825
      drizzle_con_set_error(con, "drizzle_state_addrinfo", "getaddrinfo:%s", gai_strerror(ret));
863
826
      return DRIZZLE_RETURN_GETADDRINFO;
864
827
    }
865
828
 
885
848
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
886
849
  }
887
850
 
888
 
  drizzle_log_debug(con->drizzle, "drizzle_state_connect");
 
851
  drizzle_log_debug(con, "drizzle_state_connect");
889
852
 
890
853
  if (con->fd != -1)
891
854
  {
898
861
#ifndef WIN32
899
862
    if ((con->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
900
863
    {
901
 
      con->drizzle->last_errno= errno;
 
864
      con->last_errno= errno;
902
865
      return DRIZZLE_RETURN_COULD_NOT_CONNECT;
903
866
    }
904
867
 
924
887
          }
925
888
 
926
889
        default:
927
 
          con->drizzle->last_errno= errno;
 
890
          con->last_errno= errno;
928
891
          return DRIZZLE_RETURN_COULD_NOT_CONNECT;
929
892
        }
930
893
      }
940
903
    drizzle_return_t dret;
941
904
    if (con->addrinfo_next == NULL)
942
905
    {
943
 
      drizzle_set_error(con->drizzle, __func__, "could not connect");
 
906
      drizzle_con_set_error(con, __func__, "could not connect");
944
907
      drizzle_state_reset(con);
945
908
      return DRIZZLE_RETURN_COULD_NOT_CONNECT;
946
909
    }
950
913
                    con->addrinfo_next->ai_protocol);
951
914
    if (con->fd == -1)
952
915
    {
953
 
      drizzle_set_error(con->drizzle, __func__, "socket:%s", strerror(errno));
954
 
      con->drizzle->last_errno= errno;
 
916
      drizzle_con_set_error(con, __func__, "socket:%s", strerror(errno));
 
917
      con->last_errno= errno;
955
918
      return DRIZZLE_RETURN_COULD_NOT_CONNECT;
956
919
    }
957
920
 
958
921
    dret= _con_setsockopt(con);
959
922
    if (dret != DRIZZLE_RETURN_OK)
960
923
    {
961
 
      con->drizzle->last_errno= errno;
 
924
      con->last_errno= errno;
962
925
      return DRIZZLE_RETURN_COULD_NOT_CONNECT;
963
926
    }
964
927
 
1002
965
      }
1003
966
#endif /* _WIN32 */
1004
967
 
1005
 
      drizzle_log_crazy(con->drizzle, "connect return=%d errno=%s", ret, strerror(errno));
 
968
      drizzle_log_crazy(con, "connect return=%d errno=%s", ret, strerror(errno));
1006
969
 
1007
970
      if (ret == 0)
1008
971
      {
1029
992
        return DRIZZLE_RETURN_OK;
1030
993
      }
1031
994
 
1032
 
      drizzle_set_error(con->drizzle, __func__, "connect:%s", strerror(errno));
1033
 
      con->drizzle->last_errno= errno;
 
995
      drizzle_con_set_error(con, __func__, "connect:%s", strerror(errno));
 
996
      con->last_errno= errno;
1034
997
      return DRIZZLE_RETURN_COULD_NOT_CONNECT;
1035
998
    }
1036
999
#ifdef USE_OPENSSL
1054
1017
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
1055
1018
  }
1056
1019
 
1057
 
  drizzle_log_debug(con->drizzle, "drizzle_state_connecting");
 
1020
  drizzle_log_debug(con, "drizzle_state_connecting");
1058
1021
 
1059
1022
  while (1)
1060
1023
  {
1066
1029
      int getsockopt_error;
1067
1030
      if ((getsockopt_error= getsockopt(con->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &error_length)) < 1)
1068
1031
      {
1069
 
        drizzle_set_error(con->drizzle, __func__, strerror(getsockopt_error));
 
1032
        drizzle_con_set_error(con, __func__, strerror(getsockopt_error));
1070
1033
        return DRIZZLE_RETURN_COULD_NOT_CONNECT;
1071
1034
      }
1072
1035
 
1091
1054
    }
1092
1055
 
1093
1056
    ret= drizzle_con_set_events(con, POLLOUT);
1094
 
    if (ret != DRIZZLE_RETURN_OK)
1095
 
    {
1096
 
      return ret;
1097
 
    }
1098
 
 
1099
 
    if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
 
1057
    if (con->options & DRIZZLE_CON_OPTIONS_NON_BLOCKING)
1100
1058
    {
1101
1059
      return DRIZZLE_RETURN_IO_WAIT;
1102
1060
    }
1103
1061
 
1104
 
    ret= drizzle_con_wait(con->drizzle);
 
1062
    ret= drizzle_con_wait(con);
1105
1063
    if (ret != DRIZZLE_RETURN_OK)
1106
1064
    {
1107
1065
      return ret;
1119
1077
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
1120
1078
  }
1121
1079
 
1122
 
  drizzle_log_debug(con->drizzle, "drizzle_state_read");
 
1080
  drizzle_log_debug(con, "drizzle_state_read");
1123
1081
 
1124
1082
  if (con->buffer_size == 0)
1125
1083
    con->buffer_ptr= con->buffer;
1130
1088
  }
1131
1089
 
1132
1090
  if ((con->revents & POLLIN) == 0 &&
1133
 
      (con->drizzle->options & DRIZZLE_NON_BLOCKING))
 
1091
      (con->options & DRIZZLE_CON_OPTIONS_NON_BLOCKING))
1134
1092
  {
1135
1093
    /* non-blocking mode: return IO_WAIT instead of attempting to read. This
1136
1094
     * avoids reading immediately after writing a command, which typically
1187
1145
      break;
1188
1146
    }
1189
1147
#endif /* _WIN32 */     
1190
 
    drizzle_log_crazy(con->drizzle, "read fd=%d return=%zd ssl= %d errno=%s",
 
1148
    drizzle_log_crazy(con, "read fd=%d return=%zd ssl= %d errno=%s",
1191
1149
                      con->fd, read_size, 
1192
1150
                      (con->ssl_state == DRIZZLE_SSL_STATE_HANDSHAKE_COMPLETE) ? 1 : 0,
1193
1151
                      strerror(errno));
1194
1152
 
1195
1153
    if (read_size == 0)
1196
1154
    {
1197
 
      drizzle_set_error(con->drizzle, __func__,
 
1155
      drizzle_con_set_error(con, __func__,
1198
1156
                        "%s:%d lost connection to server (EOF)", __FILE__, __LINE__);
1199
1157
      return DRIZZLE_RETURN_LOST_CONNECTION;
1200
1158
    }
1208
1166
        if (ret != DRIZZLE_RETURN_OK)
1209
1167
          return ret;
1210
1168
 
1211
 
        if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
 
1169
        if (con->options & DRIZZLE_CON_OPTIONS_NON_BLOCKING)
1212
1170
          return DRIZZLE_RETURN_IO_WAIT;
1213
1171
 
1214
 
        ret= drizzle_con_wait(con->drizzle);
 
1172
        ret= drizzle_con_wait(con);
1215
1173
        if (ret != DRIZZLE_RETURN_OK)
1216
1174
          return ret;
1217
1175
 
1231
1189
      }
1232
1190
      else if (errno == EPIPE || errno == ECONNRESET)
1233
1191
      {
1234
 
        drizzle_set_error(con->drizzle, __func__,
 
1192
        drizzle_con_set_error(con, __func__,
1235
1193
                          "%s:%d lost connection to server (%s)",
1236
1194
                          __FILE__, __LINE__, strerror(errno));
1237
1195
        return DRIZZLE_RETURN_LOST_CONNECTION;
1238
1196
      }
1239
1197
 
1240
 
      drizzle_set_error(con->drizzle, __func__, "read:%s", strerror(errno));
1241
 
      con->drizzle->last_errno= errno;
 
1198
      drizzle_con_set_error(con, __func__, "read:%s", strerror(errno));
 
1199
      con->last_errno= errno;
1242
1200
      return DRIZZLE_RETURN_ERRNO;
1243
1201
    }
1244
1202
 
1266
1224
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
1267
1225
  }
1268
1226
 
1269
 
  drizzle_log_debug(con->drizzle, "drizzle_state_write");
 
1227
  drizzle_log_debug(con, "drizzle_state_write");
1270
1228
 
1271
1229
  while (con->buffer_size != 0)
1272
1230
  {
1314
1272
    }
1315
1273
#endif /* _WIN32 */     
1316
1274
 
1317
 
    drizzle_log_crazy(con->drizzle, "write fd=%d return=%zd ssl=%d errno=%s",
 
1275
    drizzle_log_crazy(con, "write fd=%d return=%zd ssl=%d errno=%s",
1318
1276
                      con->fd, write_size,
1319
1277
                      (con->ssl_state == DRIZZLE_SSL_STATE_HANDSHAKE_COMPLETE) ? 1 : 0,
1320
1278
                      strerror(errno));
1331
1289
          return ret;
1332
1290
        }
1333
1291
 
1334
 
        if (con->drizzle->options & DRIZZLE_NON_BLOCKING)
 
1292
        if (con->options & DRIZZLE_CON_OPTIONS_NON_BLOCKING)
1335
1293
        {
1336
1294
          return DRIZZLE_RETURN_IO_WAIT;
1337
1295
        }
1338
1296
 
1339
 
        ret= drizzle_con_wait(con->drizzle);
 
1297
        ret= drizzle_con_wait(con);
1340
1298
        if (ret != DRIZZLE_RETURN_OK)
1341
1299
        {
1342
1300
          return ret;
1350
1308
      }
1351
1309
      else if (errno == EPIPE || errno == ECONNRESET)
1352
1310
      {
1353
 
        drizzle_set_error(con->drizzle, __func__, "%s:%d lost connection to server (%s)", 
 
1311
        drizzle_con_set_error(con, __func__, "%s:%d lost connection to server (%s)", 
1354
1312
                          __FILE__, __LINE__, strerror(errno));
1355
1313
        return DRIZZLE_RETURN_LOST_CONNECTION;
1356
1314
      }
1357
1315
 
1358
 
      drizzle_set_error(con->drizzle, "drizzle_state_write", "write:%s", strerror(errno));
1359
 
      con->drizzle->last_errno= errno;
 
1316
      drizzle_con_set_error(con, "drizzle_state_write", "write:%s", strerror(errno));
 
1317
      con->last_errno= errno;
1360
1318
      return DRIZZLE_RETURN_ERRNO;
1361
1319
    }
1362
1320
 
1394
1352
 
1395
1353
  if (ret == -1 && errno != EOPNOTSUPP)
1396
1354
  {
1397
 
    drizzle_set_error(con->drizzle, __func__, "setsockopt:TCP_NODELAY:%s", strerror(errno));
 
1355
    drizzle_con_set_error(con, __func__, "setsockopt:TCP_NODELAY:%s", strerror(errno));
1398
1356
    return DRIZZLE_RETURN_ERRNO;
1399
1357
  }
1400
1358
 
1411
1369
 
1412
1370
  if (ret == -1)
1413
1371
  {
1414
 
    drizzle_set_error(con->drizzle, __func__, "setsockopt:SO_LINGER:%s", strerror(errno));
 
1372
    drizzle_con_set_error(con, __func__, "setsockopt:SO_LINGER:%s", strerror(errno));
1415
1373
    return DRIZZLE_RETURN_ERRNO;
1416
1374
  }
1417
1375
 
1428
1386
 
1429
1387
  if (ret == -1 && errno != ENOPROTOOPT)
1430
1388
  {
1431
 
    drizzle_set_error(con->drizzle, __func__, "setsockopt:SO_SNDTIMEO:%s", strerror(errno));
 
1389
    drizzle_con_set_error(con, __func__, "setsockopt:SO_SNDTIMEO:%s", strerror(errno));
1432
1390
    return DRIZZLE_RETURN_ERRNO;
1433
1391
  }
1434
1392
 
1442
1400
 
1443
1401
  if (ret == -1 && errno != ENOPROTOOPT)
1444
1402
  {
1445
 
    drizzle_set_error(con->drizzle, __func__,
 
1403
    drizzle_con_set_error(con, __func__,
1446
1404
                      "setsockopt:SO_RCVTIMEO:%s", strerror(errno));
1447
1405
    return DRIZZLE_RETURN_ERRNO;
1448
1406
  }
1455
1413
#endif /* _WIN32 */
1456
1414
  if (ret == -1)
1457
1415
  {
1458
 
    drizzle_set_error(con->drizzle, __func__, "setsockopt:SO_SNDBUF:%s", strerror(errno));
 
1416
    drizzle_con_set_error(con, __func__, "setsockopt:SO_SNDBUF:%s", strerror(errno));
1459
1417
    return DRIZZLE_RETURN_ERRNO;
1460
1418
  }
1461
1419
 
1467
1425
#endif /* _WIN32 */
1468
1426
  if (ret == -1)
1469
1427
  {
1470
 
    drizzle_set_error(con->drizzle, __func__, "setsockopt:SO_RCVBUF:%s", strerror(errno));
 
1428
    drizzle_con_set_error(con, __func__, "setsockopt:SO_RCVBUF:%s", strerror(errno));
1471
1429
    return DRIZZLE_RETURN_ERRNO;
1472
1430
  }
1473
1431
 
1479
1437
 
1480
1438
    if (ret == -1)
1481
1439
    {
1482
 
      drizzle_set_error(con->drizzle, __func__, "setsockopt(SO_NOSIGPIPE): %s", strerror(errno));
 
1440
      drizzle_con_set_error(con, __func__, "setsockopt(SO_NOSIGPIPE): %s", strerror(errno));
1483
1441
      return DRIZZLE_RETURN_ERRNO;
1484
1442
    }
1485
1443
  }
1497
1455
    ret= fcntl(con->fd, F_GETFL, 0);
1498
1456
    if (ret == -1)
1499
1457
    {
1500
 
      drizzle_set_error(con->drizzle, __func__, "fcntl:F_GETFL:%s", strerror(errno));
 
1458
      drizzle_con_set_error(con, __func__, "fcntl:F_GETFL:%s", strerror(errno));
1501
1459
      return DRIZZLE_RETURN_ERRNO;
1502
1460
    }
1503
1461
 
1504
1462
    ret= fcntl(con->fd, F_SETFL, ret | O_NONBLOCK);
1505
1463
    if (ret == -1)
1506
1464
    {
1507
 
      drizzle_set_error(con->drizzle, __func__, "fcntl:F_SETFL:%s", strerror(errno));
 
1465
      drizzle_con_set_error(con, __func__, "fcntl:F_SETFL:%s", strerror(errno));
1508
1466
      return DRIZZLE_RETURN_ERRNO;
1509
1467
    }
1510
1468
  }